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 */
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)
74 flags |= flush ? TCSAFLUSH : TCSADRAIN;
75 if (tcsetattr(fd, flags, &oterm) != 0)
85 if (!changed && tcgetattr(fd, &oterm) != 0)
87 (void) memcpy(&term, &oterm, sizeof(term));
88 CLR(term.c_lflag, ECHO|ECHONL);
90 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
92 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
100 term_raw(int fd, int isig)
104 if (!changed && tcgetattr(fd, &oterm) != 0)
106 (void) memcpy(&term, &oterm, sizeof(term));
107 /* Set terminal to raw mode */
109 term.c_cc[VTIME] = 0;
110 CLR(term.c_iflag, ICRNL | IGNCR | INLCR | IUCLC | IXON);
111 CLR(term.c_oflag, OPOST);
112 CLR(term.c_lflag, ECHO | ICANON | ISIG | IEXTEN);
114 SET(term.c_lflag, ISIG);
115 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
125 if (!changed && tcgetattr(fd, &oterm) != 0)
127 (void) memcpy(&term, &oterm, sizeof(term));
128 /* Set terminal to half-cooked mode */
130 term.c_cc[VTIME] = 0;
131 CLR(term.c_lflag, ECHO | ECHONL | ICANON | IEXTEN);
132 SET(term.c_lflag, ISIG);
134 term.c_cc[VSTATUS] = _POSIX_VDISABLE;
136 if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
137 term_erase = term.c_cc[VERASE];
138 term_kill = term.c_cc[VKILL];
146 term_copy(int src, int dst)
150 if (tcgetattr(src, &tt) != 0)
152 if (tcsetattr(dst, TCSANOW|TCSASOFT, &tt) != 0)