Imported Upstream version 1.7.6p1
[debian/sudo] / term.c
1 /*
2  * Copyright (c) 2009-2011 Todd C. Miller <Todd.Miller@courtesan.com>
3  *
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.
7  *
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.
15  */
16
17 #include <config.h>
18
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <stdio.h>
22 #ifdef STDC_HEADERS
23 # include <stdlib.h>
24 # include <stddef.h>
25 #else
26 # ifdef HAVE_STDLIB_H
27 #  include <stdlib.h>
28 # endif
29 #endif /* STDC_HEADERS */
30 #ifdef HAVE_STRING_H
31 # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
32 #  include <memory.h>
33 # endif
34 # include <string.h>
35 #endif /* HAVE_STRING_H */
36 #ifdef HAVE_STRINGS_H
37 # include <strings.h>
38 #endif /* HAVE_STRINGS_H */
39 #ifdef HAVE_TERMIOS_H
40 # include <termios.h>
41 #else
42 # ifdef HAVE_TERMIO_H
43 #  include <termio.h>
44 # else
45 #  include <sgtty.h>
46 #  include <sys/ioctl.h>
47 # endif /* HAVE_TERMIO_H */
48 #endif /* HAVE_TERMIOS_H */
49
50 #include "sudo.h"
51
52 #ifndef TCSASOFT
53 # define TCSASOFT       0
54 #endif
55 #ifndef ECHONL
56 # define ECHONL         0
57 #endif
58 #ifndef IEXTEN
59 # define IEXTEN         0
60 #endif
61 #ifndef IUCLC
62 # define IUCLC          0
63 #endif
64
65 #ifndef _POSIX_VDISABLE
66 # ifdef VDISABLE
67 #  define _POSIX_VDISABLE       VDISABLE
68 # else
69 #  define _POSIX_VDISABLE       0
70 # endif
71 #endif
72
73 /*
74  * Compat macros for non-termios systems.
75  */
76 #ifndef HAVE_TERMIOS_H
77 # ifdef HAVE_TERMIO_H
78 #  undef termios
79 #  define termios               termio
80 #  define tcgetattr(f, t)       ioctl(f, TCGETA, t)
81 #  define tcsetattr(f, a, t)    ioctl(f, a, t)
82 #  undef TCSAFLUSH
83 #  define TCSAFLUSH             TCSETAF
84 #  undef TCSADRAIN
85 #  define TCSADRAIN             TCSETAW
86 # else /* SGTTY */
87 #  undef termios
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)
92 #  undef TCSAFLUSH
93 #  define TCSAFLUSH             TIOCSETP
94 #  undef TCSADRAIN
95 #  define TCSADRAIN             TIOCSETN
96 # endif /* HAVE_TERMIO_H */
97 #endif /* HAVE_TERMIOS_H */
98
99 typedef struct termios sudo_term_t;
100
101 static sudo_term_t term, oterm;
102 static int changed;
103 int term_erase;
104 int term_kill;
105
106 int
107 term_restore(fd, flush)
108     int fd;
109     int flush;
110 {
111     if (changed) {
112         int flags = TCSASOFT;
113         flags |= flush ? TCSAFLUSH : TCSADRAIN;
114         if (tcsetattr(fd, flags, &oterm) != 0)
115             return 0;
116         changed = 0;
117     }
118     return 1;
119 }
120
121 int
122 term_noecho(fd)
123     int fd;
124 {
125     if (!changed && tcgetattr(fd, &oterm) != 0)
126         return 0;
127     (void) memcpy(&term, &oterm, sizeof(term));
128     CLR(term.c_lflag, ECHO|ECHONL);
129 #ifdef VSTATUS
130     term.c_cc[VSTATUS] = _POSIX_VDISABLE;
131 #endif
132     if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
133         changed = 1;
134         return 1;
135     }
136     return 0;
137 }
138
139 #if defined(HAVE_TERMIOS_H) || defined(HAVE_TERMIO_H)
140
141 int
142 term_raw(fd, isig)
143     int fd;
144     int isig;
145 {
146     struct termios term;
147
148     if (!changed && tcgetattr(fd, &oterm) != 0)
149         return 0;
150     (void) memcpy(&term, &oterm, sizeof(term));
151     /* Set terminal to raw mode */
152     term.c_cc[VMIN] = 1;
153     term.c_cc[VTIME] = 0;
154     CLR(term.c_iflag, ICRNL | IGNCR | INLCR | IUCLC | IXON);
155     CLR(term.c_oflag, OPOST);
156     CLR(term.c_lflag, ECHO | ICANON | ISIG | IEXTEN);
157     if (isig)
158         SET(term.c_lflag, ISIG);
159     if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
160         changed = 1;
161         return 1;
162     }
163     return 0;
164 }
165
166 int
167 term_cbreak(fd)
168     int fd;
169 {
170     if (!changed && tcgetattr(fd, &oterm) != 0)
171         return 0;
172     (void) memcpy(&term, &oterm, sizeof(term));
173     /* Set terminal to half-cooked mode */
174     term.c_cc[VMIN] = 1;
175     term.c_cc[VTIME] = 0;
176     CLR(term.c_lflag, ECHO | ECHONL | ICANON | IEXTEN);
177     SET(term.c_lflag, ISIG);
178 #ifdef VSTATUS
179     term.c_cc[VSTATUS] = _POSIX_VDISABLE;
180 #endif
181     if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
182         term_erase = term.c_cc[VERASE];
183         term_kill = term.c_cc[VKILL];
184         changed = 1;
185         return 1;
186     }
187     return 0;
188 }
189
190 int
191 term_copy(src, dst)
192     int src;
193     int dst;
194 {
195     struct termios tt;
196
197     if (tcgetattr(src, &tt) != 0)
198         return 0;
199     /* XXX - add TCSANOW compat define */
200     if (tcsetattr(dst, TCSANOW|TCSASOFT, &tt) != 0)
201         return 0;
202     return 1;
203 }
204
205 #else /* SGTTY */
206
207 int
208 term_raw(fd, isig)
209     int fd;
210     int isig;
211 {
212     if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0)
213         return 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) {
220         changed = 1;
221         return 1;
222     }
223     return 0;
224 }
225
226 int
227 term_cbreak(fd)
228     int fd;
229 {
230     if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0)
231         return 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;
239         changed = 1;
240         return 1;
241     }
242     return 0;
243 }
244
245 int
246 term_copy(src, dst)
247     int src;
248     int dst;
249 {
250     struct sgttyb b;
251     struct tchars tc;
252     struct ltchars lc;
253     int l, lb;
254
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)) {
258         return 0;
259     }
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) {
263         return 0;
264     }
265     return 1;
266 }
267
268 #endif