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_lflag, ECHO | ECHONL | ICANON | ISIG | IEXTEN);
156 SET(term.c_lflag, ISIG);
157 CLR(term.c_iflag, ICRNL | IGNCR | INLCR | IUCLC | IXON);
158 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
169 if (!changed && tcgetattr(fd, &oterm) != 0)
171 (void) memcpy(&term, &oterm, sizeof(term));
172 /* Set terminal to half-cooked mode */
174 term.c_cc[VTIME] = 0;
175 CLR(term.c_lflag, ECHO | ECHONL | ICANON | IEXTEN);
176 SET(term.c_lflag, ISIG);
178 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
180 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
181 term_erase = term.c_cc[VERASE];
182 term_kill = term.c_cc[VKILL];
196 if (tcgetattr(src, &tt) != 0)
198 /* XXX - add TCSANOW compat define */
199 if (tcsetattr(dst, TCSANOW|TCSASOFT, &tt) != 0)
211 if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0)
213 (void) memcpy(&term, &oterm, sizeof(term));
214 /* Set terminal to raw mode */
215 /* XXX - how to support isig? */
216 CLR(term.c_lflag, ECHO);
217 SET(term.sg_flags, RAW);
218 if (ioctl(fd, TIOCSETP, &term) == 0) {
229 if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0)
231 (void) memcpy(&term, &oterm, sizeof(term));
232 /* Set terminal to half-cooked mode */
233 CLR(term.c_lflag, ECHO);
234 SET(term.sg_flags, CBREAK);
235 if (ioctl(fd, TIOCSETP, &term) == 0) {
236 term_erase = term.sg_erase;
237 term_kill = term.sg_kill;
254 if (ioctl(src, TIOCGETP, &b) != 0 || ioctl(src, TIOCGETC, &tc) != 0 ||
255 ioctl(src, TIOCGETD, &l) != 0 || ioctl(src, TIOCGLTC, &lc) != 0 ||
256 ioctl(src, TIOCLGET, &lb)) {
259 if (ioctl(dst, TIOCSETP, &b) != 0 || ioctl(dst, TIOCSETC, &tc) != 0 ||
260 ioctl(dst, TIOCSLTC, &lc) != 0 || ioctl(dst, TIOCLSET, &lb) != 0 ||
261 ioctl(dst, TIOCSETD, &l) != 0) {