xref: /openbsd-src/usr.bin/ssh/sshtty.c (revision 046f3ef4ba6097f092278f848784e6130a497dfa)
1*046f3ef4Sdjm /* $OpenBSD: sshtty.c,v 1.14 2010/01/09 05:04:24 djm Exp $ */
20eaaa964Sstevesk /*
30eaaa964Sstevesk  * Author: Tatu Ylonen <ylo@cs.hut.fi>
40eaaa964Sstevesk  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
50eaaa964Sstevesk  *                    All rights reserved
60eaaa964Sstevesk  *
70eaaa964Sstevesk  * As far as I am concerned, the code I have written for this software
80eaaa964Sstevesk  * can be used freely for any purpose.  Any derived versions of this
90eaaa964Sstevesk  * software must be clearly marked as such, and if the derived work is
100eaaa964Sstevesk  * incompatible with the protocol description in the RFC file, it must be
110eaaa964Sstevesk  * called by a name other than "ssh" or "Secure Shell".
120eaaa964Sstevesk  */
130eaaa964Sstevesk /*
140eaaa964Sstevesk  * Copyright (c) 2001 Markus Friedl.  All rights reserved.
150eaaa964Sstevesk  * Copyright (c) 2001 Kevin Steves.  All rights reserved.
160eaaa964Sstevesk  *
170eaaa964Sstevesk  * Redistribution and use in source and binary forms, with or without
180eaaa964Sstevesk  * modification, are permitted provided that the following conditions
190eaaa964Sstevesk  * are met:
200eaaa964Sstevesk  * 1. Redistributions of source code must retain the above copyright
210eaaa964Sstevesk  *    notice, this list of conditions and the following disclaimer.
220eaaa964Sstevesk  * 2. Redistributions in binary form must reproduce the above copyright
230eaaa964Sstevesk  *    notice, this list of conditions and the following disclaimer in the
240eaaa964Sstevesk  *    documentation and/or other materials provided with the distribution.
250eaaa964Sstevesk  *
260eaaa964Sstevesk  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
270eaaa964Sstevesk  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
280eaaa964Sstevesk  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
290eaaa964Sstevesk  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
300eaaa964Sstevesk  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
310eaaa964Sstevesk  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
320eaaa964Sstevesk  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
330eaaa964Sstevesk  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
340eaaa964Sstevesk  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
350eaaa964Sstevesk  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
360eaaa964Sstevesk  */
370eaaa964Sstevesk 
38bd9502d5Sderaadt #include <sys/types.h>
391042aed5Sstevesk #include <stdio.h>
405caac674Sstevesk #include <termios.h>
41bd9502d5Sderaadt #include <pwd.h>
420eaaa964Sstevesk 
43e16ee852Sderaadt #include "sshpty.h"
440eaaa964Sstevesk 
450eaaa964Sstevesk static struct termios _saved_tio;
460eaaa964Sstevesk static int _in_raw_mode = 0;
470eaaa964Sstevesk 
4831c39fefSdjm struct termios *
get_saved_tio(void)490eaaa964Sstevesk get_saved_tio(void)
500eaaa964Sstevesk {
5131c39fefSdjm 	return _in_raw_mode ? &_saved_tio : NULL;
520eaaa964Sstevesk }
530eaaa964Sstevesk 
540eaaa964Sstevesk void
leave_raw_mode(int quiet)55*046f3ef4Sdjm leave_raw_mode(int quiet)
560eaaa964Sstevesk {
570eaaa964Sstevesk 	if (!_in_raw_mode)
580eaaa964Sstevesk 		return;
59*046f3ef4Sdjm 	if (tcsetattr(fileno(stdin), TCSADRAIN, &_saved_tio) == -1) {
60*046f3ef4Sdjm 		if (!quiet)
610eaaa964Sstevesk 			perror("tcsetattr");
62*046f3ef4Sdjm 	} else
630eaaa964Sstevesk 		_in_raw_mode = 0;
640eaaa964Sstevesk }
650eaaa964Sstevesk 
660eaaa964Sstevesk void
enter_raw_mode(int quiet)67*046f3ef4Sdjm enter_raw_mode(int quiet)
680eaaa964Sstevesk {
690eaaa964Sstevesk 	struct termios tio;
700eaaa964Sstevesk 
710eaaa964Sstevesk 	if (tcgetattr(fileno(stdin), &tio) == -1) {
72*046f3ef4Sdjm 		if (!quiet)
730eaaa964Sstevesk 			perror("tcgetattr");
740eaaa964Sstevesk 		return;
750eaaa964Sstevesk 	}
760eaaa964Sstevesk 	_saved_tio = tio;
770eaaa964Sstevesk 	tio.c_iflag |= IGNPAR;
780eaaa964Sstevesk 	tio.c_iflag &= ~(ISTRIP | INLCR | IGNCR | ICRNL | IXON | IXANY | IXOFF);
79328f812fSmarkus #ifdef IUCLC
80328f812fSmarkus 	tio.c_iflag &= ~IUCLC;
81328f812fSmarkus #endif
820eaaa964Sstevesk 	tio.c_lflag &= ~(ISIG | ICANON | ECHO | ECHOE | ECHOK | ECHONL);
830eaaa964Sstevesk #ifdef IEXTEN
840eaaa964Sstevesk 	tio.c_lflag &= ~IEXTEN;
850eaaa964Sstevesk #endif
860eaaa964Sstevesk 	tio.c_oflag &= ~OPOST;
870eaaa964Sstevesk 	tio.c_cc[VMIN] = 1;
880eaaa964Sstevesk 	tio.c_cc[VTIME] = 0;
89*046f3ef4Sdjm 	if (tcsetattr(fileno(stdin), TCSADRAIN, &tio) == -1) {
90*046f3ef4Sdjm 		if (!quiet)
910eaaa964Sstevesk 			perror("tcsetattr");
92*046f3ef4Sdjm 	} else
930eaaa964Sstevesk 		_in_raw_mode = 1;
940eaaa964Sstevesk }
95