1 /*
   2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
   3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
   4  *                    All rights reserved
   5  *
   6  * As far as I am concerned, the code I have written for this software
   7  * can be used freely for any purpose.  Any derived versions of this
   8  * software must be clearly marked as such, and if the derived work is
   9  * incompatible with the protocol description in the RFC file, it must be
  10  * called by a name other than "ssh" or "Secure Shell".
  11  */
  12 /*
  13  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
  14  * Copyright (c) 2001 Kevin Steves.  All rights reserved.
  15  *
  16  * Redistribution and use in source and binary forms, with or without
  17  * modification, are permitted provided that the following conditions
  18  * are met:
  19  * 1. Redistributions of source code must retain the above copyright
  20  *    notice, this list of conditions and the following disclaimer.
  21  * 2. Redistributions in binary form must reproduce the above copyright
  22  *    notice, this list of conditions and the following disclaimer in the
  23  *    documentation and/or other materials provided with the distribution.
  24  *
  25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  26  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  30  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35  */
  36 
  37 #include "includes.h"
  38 RCSID("$OpenBSD: sshtty.c,v 1.3 2002/03/04 17:27:39 stevesk Exp $");
  39 
  40 #pragma ident   "%Z%%M% %I%     %E% SMI"
  41 
  42 #include "sshtty.h"
  43 #include "log.h"
  44 
  45 static struct termios _saved_tio;
  46 static int _in_raw_mode = 0;
  47 
  48 int
  49 in_raw_mode(void)
  50 {
  51         return _in_raw_mode;
  52 }
  53 
  54 struct termios
  55 get_saved_tio(void)
  56 {
  57         return _saved_tio;
  58 }
  59 
  60 void
  61 leave_raw_mode(void)
  62 {
  63         if (!_in_raw_mode)
  64                 return;
  65         if (tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio) == -1)
  66                 perror("tcsetattr");
  67         else
  68                 _in_raw_mode = 0;
  69 
  70         fatal_remove_cleanup((void (*) (void *)) leave_raw_mode, NULL);
  71 }
  72 
  73 void
  74 enter_raw_mode(void)
  75 {
  76         struct termios tio;
  77 
  78         if (tcgetattr(fileno(stdin), &tio) == -1) {
  79                 perror("tcgetattr");
  80                 return;
  81         }
  82         _saved_tio = tio;
  83         tio.c_iflag |= IGNPAR;
  84         tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
  85         tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
  86 #ifdef IEXTEN
  87         tio.c_lflag &= ~IEXTEN;
  88 #endif
  89         tio.c_oflag &= ~OPOST;
  90         tio.c_cc[VMIN] = 1;
  91         tio.c_cc[VTIME] = 0;
  92         if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) == -1)
  93                 perror("tcsetattr");
  94         else
  95                 _in_raw_mode = 1;
  96 
  97         fatal_add_cleanup((void (*) (void *)) leave_raw_mode, NULL);
  98 }