2 * Copyright (c) 2011-2012 Todd C. Miller <Todd.Miller@courtesan.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
28 #endif /* STDC_HEADERS */
30 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
34 #endif /* HAVE_STRING_H */
37 #endif /* HAVE_STRINGS_H */
41 #include "sudo_debug.h"
56 #ifndef _POSIX_VDISABLE
58 # define _POSIX_VDISABLE VDISABLE
60 # define _POSIX_VDISABLE 0
64 static struct termios term, oterm;
70 term_restore(int fd, int flush)
72 debug_decl(term_restore, SUDO_DEBUG_UTIL)
76 flags |= flush ? TCSAFLUSH : TCSADRAIN;
77 if (tcsetattr(fd, flags, &oterm) != 0)
87 debug_decl(term_noecho, SUDO_DEBUG_UTIL)
89 if (!changed && tcgetattr(fd, &oterm) != 0)
91 (void) memcpy(&term, &oterm, sizeof(term));
92 CLR(term.c_lflag, ECHO|ECHONL);
94 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
96 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
104 term_raw(int fd, int isig)
107 debug_decl(term_raw, SUDO_DEBUG_UTIL)
109 if (!changed && tcgetattr(fd, &oterm) != 0)
111 (void) memcpy(&term, &oterm, sizeof(term));
112 /* Set terminal to raw mode */
114 term.c_cc[VTIME] = 0;
115 CLR(term.c_iflag, ICRNL | IGNCR | INLCR | IUCLC | IXON);
116 CLR(term.c_oflag, OPOST);
117 CLR(term.c_lflag, ECHO | ICANON | ISIG | IEXTEN);
119 SET(term.c_lflag, ISIG);
120 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
130 debug_decl(term_cbreak, SUDO_DEBUG_UTIL)
132 if (!changed && tcgetattr(fd, &oterm) != 0)
134 (void) memcpy(&term, &oterm, sizeof(term));
135 /* Set terminal to half-cooked mode */
137 term.c_cc[VTIME] = 0;
138 CLR(term.c_lflag, ECHO | ECHONL | ICANON | IEXTEN);
139 SET(term.c_lflag, ISIG);
141 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
143 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
144 term_erase = term.c_cc[VERASE];
145 term_kill = term.c_cc[VKILL];
153 term_copy(int src, int dst)
156 debug_decl(term_copy, SUDO_DEBUG_UTIL)
158 if (tcgetattr(src, &tt) != 0)
160 if (tcsetattr(dst, TCSANOW|TCSASOFT, &tt) != 0)