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