Imported Upstream version 1.7.4
[debian/sudo] / term.c
1 /*
2  * Copyright (c) 2009-2010 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_lflag, ECHO | ECHONL | ICANON | ISIG | IEXTEN);
155     if (isig)
156         SET(term.c_lflag, ISIG);
157     CLR(term.c_iflag, ICRNL | IGNCR | INLCR | IUCLC | IXON);
158     if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
159         changed = 1;
160         return(1);
161     }
162     return(0);
163 }
164
165 int
166 term_cbreak(fd)
167     int fd;
168 {
169     if (!changed && tcgetattr(fd, &oterm) != 0)
170         return(0);
171     (void) memcpy(&term, &oterm, sizeof(term));
172     /* Set terminal to half-cooked mode */
173     term.c_cc[VMIN] = 1;
174     term.c_cc[VTIME] = 0;
175     CLR(term.c_lflag, ECHO | ECHONL | ICANON | IEXTEN);
176     SET(term.c_lflag, ISIG);
177 #ifdef VSTATUS
178     term.c_cc[VSTATUS] = _POSIX_VDISABLE;
179 #endif
180     if (tcsetattr(fd, TCSADRAIN|TCSASOFT, &term) == 0) {
181         term_erase = term.c_cc[VERASE];
182         term_kill = term.c_cc[VKILL];
183         changed = 1;
184         return(1);
185     }
186     return(0);
187 }
188
189 int
190 term_copy(src, dst)
191     int src;
192     int dst;
193 {
194     struct termios tt;
195
196     if (tcgetattr(src, &tt) != 0)
197         return(0);
198     /* XXX - add TCSANOW compat define */
199     if (tcsetattr(dst, TCSANOW|TCSASOFT, &tt) != 0)
200         return(0);
201     return(1);
202 }
203
204 #else /* SGTTY */
205
206 int
207 term_raw(fd, isig)
208     int fd;
209     int isig;
210 {
211     if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0)
212         return(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) {
219         changed = 1;
220         return(1);
221     }
222     return(0);
223 }
224
225 int
226 term_cbreak(fd)
227     int fd;
228 {
229     if (!changed && ioctl(fd, TIOCGETP, &oterm) != 0)
230         return(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;
238         changed = 1;
239         return(1);
240     }
241     return(0);
242 }
243
244 int
245 term_copy(src, dst)
246     int src;
247     int dst;
248 {
249     struct sgttyb b;
250     struct tchars tc;
251     struct ltchars lc;
252     int l, lb;
253
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)) {
257         return(0);
258     }
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) {
262         return(0);
263     }
264     return(1);
265 }
266
267 #endif