2 * Copyright (c) 2009-2010 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 */
46 # include <sys/ioctl.h>
47 # endif /* HAVE_TERMIO_H */
48 #endif /* HAVE_TERMIOS_H */
65 #ifndef _POSIX_VDISABLE
67 # define _POSIX_VDISABLE VDISABLE
69 # define _POSIX_VDISABLE 0
74 * Compat macros for non-termios systems.
76 #ifndef HAVE_TERMIOS_H
79 # define termios termio
80 # define tcgetattr(f, t) ioctl(f, TCGETA, t)
81 # define tcsetattr(f, a, t) ioctl(f, a, t)
83 # define TCSAFLUSH TCSETAF
85 # define TCSADRAIN TCSETAW
88 # define termios sgttyb
89 # define c_lflag sg_flags
90 # define tcgetattr(f, t) ioctl(f, TIOCGETP, t)
91 # define tcsetattr(f, a, t) ioctl(f, a, t)
93 # define TCSAFLUSH TIOCSETP
95 # define TCSADRAIN TIOCSETN
96 # endif /* HAVE_TERMIO_H */
97 #endif /* HAVE_TERMIOS_H */
99 typedef struct termios sudo_term_t;
101 static sudo_term_t term, oterm;
107 term_restore(fd, flush)
112 int flags = TCSASOFT;
113 flags |= flush ? TCSAFLUSH : TCSADRAIN;
114 if (tcsetattr(fd, flags, &oterm) != 0)
125 if (!changed && tcgetattr(fd, &oterm) != 0)
127 (void) memcpy(&term, &oterm, sizeof(term));
128 CLR(term.c_lflag, ECHO|ECHONL);
130 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
132 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
139 #if defined(HAVE_TERMIOS_H) || defined(HAVE_TERMIO_H)
148 if (!changed && tcgetattr(fd, &oterm) != 0)
150 (void) memcpy(&term, &oterm, sizeof(term));
151 /* Set terminal to raw mode */
153 term.c_cc[VTIME] = 0;
154 CLR(term.c_iflag, ICRNL | IGNCR | INLCR | IUCLC | IXON);
155 SET(term.c_oflag, OPOST);
156 CLR(term.c_lflag, ECHO | ICANON | ISIG | IEXTEN);
158 SET(term.c_lflag, ISIG);
159 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
170 if (!changed && tcgetattr(fd, &oterm) != 0)
172 (void) memcpy(&term, &oterm, sizeof(term));
173 /* Set terminal to half-cooked mode */
175 term.c_cc[VTIME] = 0;
176 CLR(term.c_lflag, ECHO | ECHONL | ICANON | IEXTEN);
177 SET(term.c_lflag, ISIG);
179 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
181 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
182 term_erase = term.c_cc[VERASE];
183 term_kill = term.c_cc[VKILL];
197 if (tcgetattr(src, &tt) != 0)
199 /* XXX - add TCSANOW compat define */
200 if (tcsetattr(dst, TCSANOW|TCSASOFT, &tt) != 0)
212 if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0)
214 (void) memcpy(&term, &oterm, sizeof(term));
215 /* Set terminal to raw mode */
216 /* XXX - how to support isig? */
217 CLR(term.c_lflag, ECHO);
218 SET(term.sg_flags, RAW);
219 if (ioctl(fd, TIOCSETP, &term) == 0) {
230 if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0)
232 (void) memcpy(&term, &oterm, sizeof(term));
233 /* Set terminal to half-cooked mode */
234 CLR(term.c_lflag, ECHO);
235 SET(term.sg_flags, CBREAK);
236 if (ioctl(fd, TIOCSETP, &term) == 0) {
237 term_erase = term.sg_erase;
238 term_kill = term.sg_kill;
255 if (ioctl(src, TIOCGETP, &b) != 0 || ioctl(src, TIOCGETC, &tc) != 0 ||
256 ioctl(src, TIOCGETD, &l) != 0 || ioctl(src, TIOCGLTC, &lc) != 0 ||
257 ioctl(src, TIOCLGET, &lb)) {
260 if (ioctl(dst, TIOCSETP, &b) != 0 || ioctl(dst, TIOCSETC, &tc) != 0 ||
261 ioctl(dst, TIOCSLTC, &lc) != 0 || ioctl(dst, TIOCLSET, &lb) != 0 ||
262 ioctl(dst, TIOCSETD, &l) != 0) {