2 * Copyright (c) 2011 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>
20 #include <sys/param.h>
29 #endif /* STDC_HEADERS */
31 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
35 #endif /* HAVE_STRING_H */
38 #endif /* HAVE_STRINGS_H */
42 #include "sudo_debug.h"
57 #ifndef _POSIX_VDISABLE
59 # define _POSIX_VDISABLE VDISABLE
61 # define _POSIX_VDISABLE 0
65 static struct termios term, oterm;
71 term_restore(int fd, int flush)
73 debug_decl(term_restore, SUDO_DEBUG_UTIL)
77 flags |= flush ? TCSAFLUSH : TCSADRAIN;
78 if (tcsetattr(fd, flags, &oterm) != 0)
88 debug_decl(term_noecho, SUDO_DEBUG_UTIL)
90 if (!changed && tcgetattr(fd, &oterm) != 0)
92 (void) memcpy(&term, &oterm, sizeof(term));
93 CLR(term.c_lflag, ECHO|ECHONL);
95 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
97 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
105 term_raw(int fd, int isig)
108 debug_decl(term_raw, SUDO_DEBUG_UTIL)
110 if (!changed && tcgetattr(fd, &oterm) != 0)
112 (void) memcpy(&term, &oterm, sizeof(term));
113 /* Set terminal to raw mode */
115 term.c_cc[VTIME] = 0;
116 CLR(term.c_iflag, ICRNL | IGNCR | INLCR | IUCLC | IXON);
117 CLR(term.c_oflag, OPOST);
118 CLR(term.c_lflag, ECHO | ICANON | ISIG | IEXTEN);
120 SET(term.c_lflag, ISIG);
121 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
131 debug_decl(term_cbreak, SUDO_DEBUG_UTIL)
133 if (!changed && tcgetattr(fd, &oterm) != 0)
135 (void) memcpy(&term, &oterm, sizeof(term));
136 /* Set terminal to half-cooked mode */
138 term.c_cc[VTIME] = 0;
139 CLR(term.c_lflag, ECHO | ECHONL | ICANON | IEXTEN);
140 SET(term.c_lflag, ISIG);
142 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
144 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
145 term_erase = term.c_cc[VERASE];
146 term_kill = term.c_cc[VKILL];
154 term_copy(int src, int dst)
157 debug_decl(term_copy, SUDO_DEBUG_UTIL)
159 if (tcgetattr(src, &tt) != 0)
161 if (tcsetattr(dst, TCSANOW|TCSASOFT, &tt) != 0)