]> granicus.if.org Git - strace/blob - term.c
Initial revision
[strace] / term.c
1 /*
2  * Copyright (c) 1993, 1994, 1995, 1996 Rick Sladkey <jrs@world.std.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  *      $Id$
28  */
29
30 #include "defs.h"
31
32 #ifdef __GLIBC__
33 #include <termio.h>
34 #endif /* __GLIBC__ */
35 #include <termios.h>
36 #ifdef HAVE_SYS_FILIO_H
37 #include <sys/filio.h>
38 #endif
39
40 static struct xlat tcxonc_options[] = {
41         { TCOOFF,       "TCOOFF"        },
42         { TCOON,        "TCOON"         },
43         { TCIOFF,       "TCIOFF"        },
44         { TCION,        "TCION"         },
45         { 0,            NULL            },
46 };
47
48 static struct xlat tcflsh_options[] = {
49         { TCIFLUSH,     "TCIFLUSH"      },
50         { TCOFLUSH,     "TCOFLUSH"      },
51         { TCIOFLUSH,    "TCIOFLUSH"     },
52         { 0,            NULL            },
53 };
54
55 static struct xlat baud_options[] = {
56         { B0,           "B0"            },
57         { B50,          "B50"           },
58         { B75,          "B75"           },
59         { B110,         "B110"          },
60         { B134,         "B134"          },
61         { B150,         "B150"          },
62         { B200,         "B200"          },
63         { B300,         "B300"          },
64         { B600,         "B600"          },
65         { B1200,        "B1200"         },
66         { B1800,        "B1800"         },
67         { B2400,        "B2400"         },
68         { B4800,        "B4800"         },
69         { B9600,        "B9600"         },
70 #ifdef B19200
71         { B19200,       "B19200"        },
72 #endif
73 #ifdef B38400
74         { B38400,       "B38400"        },
75 #endif
76 #ifdef EXTA
77         { EXTA,         "EXTA"          },
78 #endif
79 #ifdef EXTB
80         { EXTB,         "EXTB"          },
81 #endif
82         { 0,            NULL            },
83 };
84
85 static struct xlat modem_flags[] = {
86 #ifdef TIOCM_LE
87         { TIOCM_LE,     "TIOCM_LE",     },
88 #endif
89 #ifdef TIOCM_DTR
90         { TIOCM_DTR,    "TIOCM_DTR",    },
91 #endif
92 #ifdef TIOCM_RTS
93         { TIOCM_RTS,    "TIOCM_RTS",    },
94 #endif
95 #ifdef TIOCM_ST
96         { TIOCM_ST,     "TIOCM_ST",     },
97 #endif
98 #ifdef TIOCM_SR
99         { TIOCM_SR,     "TIOCM_SR",     },
100 #endif
101 #ifdef TIOCM_CTS
102         { TIOCM_CTS,    "TIOCM_CTS",    },
103 #endif
104 #ifdef TIOCM_CAR
105         { TIOCM_CAR,    "TIOCM_CAR",    },
106 #endif
107 #ifdef TIOCM_CD
108         { TIOCM_CD,     "TIOCM_CD",     },
109 #endif
110 #ifdef TIOCM_RNG
111         { TIOCM_RNG,    "TIOCM_RNG",    },
112 #endif
113 #ifdef TIOCM_RI
114         { TIOCM_RI,     "TIOCM_RI",     },
115 #endif
116 #ifdef TIOCM_DSR
117         { TIOCM_DSR,    "TIOCM_DSR",    },
118 #endif
119         { 0,            NULL,           },
120 };
121
122
123 int
124 term_ioctl(tcp, code, arg)
125 struct tcb *tcp;
126 long code, arg;
127 {
128         struct termios tios;
129         struct termio tio;
130         struct winsize ws;
131 #ifdef TIOCGSIZE
132         struct  ttysize ts;
133 #endif
134         int i;
135
136         if (entering(tcp))
137                 return 0;
138
139         switch (code) {
140
141         /* ioctls with termios or termio args */
142
143 #ifdef TCGETS
144         case TCGETS:
145                 if (syserror(tcp))
146                         return 0;
147         case TCSETS:
148         case TCSETSW:
149         case TCSETSF:
150                 if (!verbose(tcp) || umove(tcp, arg, &tios) < 0)
151                         return 0;
152                 if (abbrev(tcp)) {
153                         tprintf(", {");
154                         printxval(baud_options, tios.c_cflag & CBAUD, "B???");
155                         tprintf(" %sopost %sisig %sicanon %secho ...}",
156                                 (tios.c_oflag & OPOST) ? "" : "-",
157                                 (tios.c_lflag & ISIG) ? "" : "-",
158                                 (tios.c_lflag & ICANON) ? "" : "-",
159                                 (tios.c_lflag & ECHO) ? "" : "-");
160                         return 1;
161                 }
162                 tprintf(", {c_iflags=%#lx, c_oflags=%#lx, ",
163                         (long) tios.c_iflag, (long) tios.c_oflag);
164                 tprintf("c_cflags=%#lx, c_lflags=%#lx, ",
165                         (long) tios.c_cflag, (long) tios.c_lflag);
166 #ifndef SVR4
167                 tprintf("c_line=%u, ", tios.c_line);
168 #endif
169                 if (!(tios.c_lflag & ICANON))
170                         tprintf("c_cc[VMIN]=%d, c_cc[VTIME]=%d, ",
171                                 tios.c_cc[VMIN], tios.c_cc[VTIME]);
172                 tprintf("c_cc=\"");
173                 for (i = 0; i < NCCS; i++)
174                         tprintf("\\x%02x", tios.c_cc[i]);
175                 tprintf("\"}");
176                 return 1;
177 #endif /* TCGETS */
178
179 #ifdef TCGETA
180         case TCGETA:
181                 if (syserror(tcp))
182                         return 0;
183         case TCSETA:
184         case TCSETAW:
185         case TCSETAF:
186                 if (!verbose(tcp) || umove(tcp, arg, &tio) < 0)
187                         return 0;
188                 if (abbrev(tcp)) {
189                         tprintf(", {");
190                         printxval(baud_options, tio.c_cflag & CBAUD, "B???");
191                         tprintf(" %sopost %sisig %sicanon %secho ...}",
192                                 (tio.c_oflag & OPOST) ? "" : "-",
193                                 (tio.c_lflag & ISIG) ? "" : "-",
194                                 (tio.c_lflag & ICANON) ? "" : "-",
195                                 (tio.c_lflag & ECHO) ? "" : "-");
196                         return 1;
197                 }
198                 tprintf(", {c_iflags=%#lx, c_oflags=%#lx, ",
199                         (long) tio.c_iflag, (long) tio.c_oflag);
200                 tprintf("c_cflags=%#lx, c_lflags=%#lx, ",
201                         (long) tio.c_cflag, (long) tio.c_lflag);
202                 tprintf("c_line=%u, ", tio.c_line);
203 #ifdef _VMIN
204                 if (!(tio.c_lflag & ICANON))
205                         tprintf("c_cc[_VMIN]=%d, c_cc[_VTIME]=%d, ",
206                                 tio.c_cc[_VMIN], tio.c_cc[_VTIME]);
207 #else /* !_VMIN */
208                 if (!(tio.c_lflag & ICANON))
209                         tprintf("c_cc[VMIN]=%d, c_cc[VTIME]=%d, ",
210                                 tio.c_cc[VMIN], tio.c_cc[VTIME]);
211 #endif /* !_VMIN */
212                 tprintf("c_cc=\"");
213                 for (i = 0; i < NCC; i++)
214                         tprintf("\\x%02x", tio.c_cc[i]);
215                 tprintf("\"}");
216                 return 1;
217 #endif /* TCGETA */
218
219         /* ioctls with winsize or ttysize args */
220
221 #ifdef TIOCGWINSZ
222         case TIOCGWINSZ:
223                 if (syserror(tcp))
224                         return 0;
225         case TIOCSWINSZ:
226                 if (!verbose(tcp) || umove(tcp, arg, &ws) < 0)
227                         return 0;
228                 tprintf(", {ws_row=%d, ws_col=%d, ws_xpixel=%d, ws_ypixel=%d}",
229                         ws.ws_row, ws.ws_col, ws.ws_xpixel, ws.ws_ypixel);
230                 return 1;
231 #endif /* TIOCGWINSZ */
232
233 #ifdef TIOCGSIZE
234         case TIOCGSIZE:
235                 if (syserror(tcp))
236                         return 0;
237         case TIOCSSIZE:
238                 if (!verbose(tcp) || umove(tcp, arg, &ts) < 0)
239                         return 0;
240                 tprintf(", {ts_lines=%d, ts_cols=%d}",
241                         ts.ts_lines, ts.ts_cols);
242                 return 1;
243 #endif
244
245         /* ioctls with a direct decodable arg */
246
247         case TCXONC:
248                 tprintf(", ");
249                 printxval(tcxonc_options, arg, "TC???");
250                 return 1;
251         case TCFLSH:
252                 tprintf(", ");
253                 printxval(tcflsh_options, arg, "TC???");
254                 return 1;
255
256         /* ioctls with an indirect parameter displayed as modem flags */
257
258 #ifdef TIOCMGET
259         case TIOCMGET:
260         case TIOCMBIS:
261         case TIOCMBIC:
262         case TIOCMSET:
263                 if (umove(tcp, arg, &arg) < 0)
264                         return 0;
265                 tprintf(", [");
266                 if (!printflags(modem_flags, arg))
267                         tprintf("0");
268                 tprintf("]");
269                 return 1;
270 #endif /* TIOCMGET */
271
272         /* ioctls with an indirect parameter displayed in decimal */
273
274         case TIOCSPGRP:
275         case TIOCGPGRP:
276 #ifdef TIOCGETPGRP
277         case TIOCGETPGRP:
278 #endif
279 #ifdef TIOCSETPGRP
280         case TIOCSETPGRP:
281 #endif
282 #ifdef FIONREAD
283         case FIONREAD:
284 #endif
285         case TIOCOUTQ:
286 #ifdef FIONBIO
287         case FIONBIO:
288 #endif
289 #ifdef FIOASYNC
290         case FIOASYNC:
291 #endif
292 #ifdef FIOGETOWN
293         case FIOGETOWN:
294 #endif
295 #ifdef FIOSETOWN
296         case FIOSETOWN:
297 #endif
298 #ifdef TIOCGETD
299         case TIOCGETD:
300 #endif
301 #ifdef TIOCSETD
302         case TIOCSETD:
303 #endif
304 #ifdef TIOCPKT
305         case TIOCPKT:
306 #endif
307 #ifdef TIOCREMOTE
308         case TIOCREMOTE:
309 #endif
310 #ifdef TIOCUCNTL
311         case TIOCUCNTL:
312 #endif
313 #ifdef TIOCTCNTL
314         case TIOCTCNTL:
315 #endif
316 #ifdef TIOCSIGNAL
317         case TIOCSIGNAL:
318 #endif
319 #ifdef TIOCSSOFTCAR
320         case TIOCSSOFTCAR:
321 #endif
322 #ifdef TIOCGSOFTCAR
323         case TIOCGSOFTCAR:
324 #endif
325 #ifdef TIOCISPACE
326         case TIOCISPACE:
327 #endif
328 #ifdef TIOCISIZE
329         case TIOCISIZE:
330 #endif
331 #ifdef TIOCSINTR
332         case TIOCSINTR:
333 #endif
334                 tprintf(", ");
335                 printnum(tcp, arg, "%d");
336                 return 1;
337
338 #if 0
339         /* ioctls with an indirect parameter displayed in hex */
340
341                 tprintf(", ");
342                 printnum(tcp, arg, "%#x");
343                 return 1;
344 #endif
345
346         /* ioctls with an indirect parameter displayed as a char */
347
348 #ifdef TIOCSTI
349         case TIOCSTI:
350 #endif
351                 tprintf(", ");
352                 printstr(tcp, arg, 1);
353                 return 1;
354
355         /* ioctls with no parameters */
356
357 #ifdef TIOCSCTTY
358         case TIOCSCTTY:
359 #endif
360 #ifdef TIOCNOTTY
361         case TIOCNOTTY:
362 #endif
363 #ifdef FIOCLEX
364         case FIOCLEX:
365 #endif
366 #ifdef FIONCLEX
367         case FIONCLEX:
368 #endif
369 #ifdef TIOCCONS
370         case TIOCCONS:
371 #endif
372                 return 1;
373
374         /* ioctls which are unknown */
375
376         default:
377                 return 0;
378         }
379 }
380