cmdedit: small optimizations
[oweals/busybox.git] / coreutils / stty.c
1 /* vi: set sw=4 ts=4: */
2 /* stty -- change and print terminal line settings
3    Copyright (C) 1990-1999 Free Software Foundation, Inc.
4
5    Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
6 */
7 /* Usage: stty [-ag] [-F device] [setting...]
8
9    Options:
10    -a Write all current settings to stdout in human-readable form.
11    -g Write all current settings to stdout in stty-readable form.
12    -F Open and use the specified device instead of stdin
13
14    If no args are given, write to stdout the baud rate and settings that
15    have been changed from their defaults.  Mode reading and changes
16    are done on the specified device, or stdin if none was specified.
17
18    David MacKenzie <djm@gnu.ai.mit.edu>
19
20    Special for busybox ported by Vladimir Oleynik <dzo@simtreas.ru> 2001
21
22    */
23
24 #include "busybox.h"
25
26 #ifndef _POSIX_VDISABLE
27 # define _POSIX_VDISABLE ((unsigned char) 0)
28 #endif
29
30 #define Control(c) ((c) & 0x1f)
31 /* Canonical values for control characters */
32 #ifndef CINTR
33 # define CINTR Control('c')
34 #endif
35 #ifndef CQUIT
36 # define CQUIT 28
37 #endif
38 #ifndef CERASE
39 # define CERASE 127
40 #endif
41 #ifndef CKILL
42 # define CKILL Control('u')
43 #endif
44 #ifndef CEOF
45 # define CEOF Control('d')
46 #endif
47 #ifndef CEOL
48 # define CEOL _POSIX_VDISABLE
49 #endif
50 #ifndef CSTART
51 # define CSTART Control('q')
52 #endif
53 #ifndef CSTOP
54 # define CSTOP Control('s')
55 #endif
56 #ifndef CSUSP
57 # define CSUSP Control('z')
58 #endif
59 #if defined(VEOL2) && !defined(CEOL2)
60 # define CEOL2 _POSIX_VDISABLE
61 #endif
62 /* ISC renamed swtch to susp for termios, but we'll accept either name */
63 #if defined(VSUSP) && !defined(VSWTCH)
64 # define VSWTCH VSUSP
65 # define CSWTCH CSUSP
66 #endif
67 #if defined(VSWTCH) && !defined(CSWTCH)
68 # define CSWTCH _POSIX_VDISABLE
69 #endif
70
71 /* SunOS 5.3 loses (^Z doesn't work) if 'swtch' is the same as 'susp'.
72    So the default is to disable 'swtch.'  */
73 #if defined (__sparc__) && defined (__svr4__)
74 # undef CSWTCH
75 # define CSWTCH _POSIX_VDISABLE
76 #endif
77
78 #if defined(VWERSE) && !defined (VWERASE)       /* AIX-3.2.5 */
79 # define VWERASE VWERSE
80 #endif
81 #if defined(VDSUSP) && !defined (CDSUSP)
82 # define CDSUSP Control('y')
83 #endif
84 #if !defined(VREPRINT) && defined(VRPRNT)       /* Irix 4.0.5 */
85 # define VREPRINT VRPRNT
86 #endif
87 #if defined(VREPRINT) && !defined(CRPRNT)
88 # define CRPRNT Control('r')
89 #endif
90 #if defined(VWERASE) && !defined(CWERASE)
91 # define CWERASE Control('w')
92 #endif
93 #if defined(VLNEXT) && !defined(CLNEXT)
94 # define CLNEXT Control('v')
95 #endif
96 #if defined(VDISCARD) && !defined(VFLUSHO)
97 # define VFLUSHO VDISCARD
98 #endif
99 #if defined(VFLUSH) && !defined(VFLUSHO)        /* Ultrix 4.2 */
100 # define VFLUSHO VFLUSH
101 #endif
102 #if defined(CTLECH) && !defined(ECHOCTL)        /* Ultrix 4.3 */
103 # define ECHOCTL CTLECH
104 #endif
105 #if defined(TCTLECH) && !defined(ECHOCTL)       /* Ultrix 4.2 */
106 # define ECHOCTL TCTLECH
107 #endif
108 #if defined(CRTKIL) && !defined(ECHOKE)         /* Ultrix 4.2 and 4.3 */
109 # define ECHOKE CRTKIL
110 #endif
111 #if defined(VFLUSHO) && !defined(CFLUSHO)
112 # define CFLUSHO Control('o')
113 #endif
114 #if defined(VSTATUS) && !defined(CSTATUS)
115 # define CSTATUS Control('t')
116 #endif
117
118 /* Which speeds to set */
119 enum speed_setting {
120         input_speed, output_speed, both_speeds
121 };
122
123 /* Which member(s) of 'struct termios' a mode uses */
124 enum {
125         /* Do NOT change the order or values, as mode_type_flag()
126          * depends on them */
127         control, input, output, local, combination
128 };
129
130 static const char evenp     [] = "evenp";
131 static const char raw       [] = "raw";
132 static const char stty_min  [] = "min";
133 static const char stty_time [] = "time";
134 static const char stty_swtch[] = "swtch";
135 static const char stty_eol  [] = "eol";
136 static const char stty_eof  [] = "eof";
137 static const char parity    [] = "parity";
138 static const char stty_oddp [] = "oddp";
139 static const char stty_nl   [] = "nl";
140 static const char stty_ek   [] = "ek";
141 static const char stty_sane [] = "sane";
142 static const char cbreak    [] = "cbreak";
143 static const char stty_pass8[] = "pass8";
144 static const char litout    [] = "litout";
145 static const char cooked    [] = "cooked";
146 static const char decctlq   [] = "decctlq";
147 static const char stty_tabs [] = "tabs";
148 static const char stty_lcase[] = "lcase";
149 static const char stty_LCASE[] = "LCASE";
150 static const char stty_crt  [] = "crt";
151 static const char stty_dec  [] = "dec";
152
153 /* Flags for 'struct mode_info' */
154 #define SANE_SET 1              /* Set in 'sane' mode                  */
155 #define SANE_UNSET 2            /* Unset in 'sane' mode                */
156 #define REV 4                   /* Can be turned off by prepending '-' */
157 #define OMIT 8                  /* Don't display value                 */
158
159 /* Each mode */
160 struct mode_info {
161         const char * const name;      /* Name given on command line           */
162         const unsigned char type;     /* Which structure element to change    */
163         const unsigned char flags;    /* Setting and display options          */
164         /* were using short here, but ppc32 was unhappy: */
165         const tcflag_t mask;          /* Other bits to turn off for this mode */
166         const tcflag_t bits;          /* Bits to set for this mode            */
167 };
168
169 /* We can optimize it further by using name[8] instead of char *name */
170 /* but beware of "if (info->name == evenp)" checks! */
171 /* Need to replace them with "if (info == &mode_info[EVENP_INDX])" */
172
173 #define MI_ENTRY(N,T,F,B,M) { N, T, F, M, B }
174
175 static const struct mode_info mode_info[] = {
176         MI_ENTRY("parenb",   control,     REV,               PARENB,     0 ),
177         MI_ENTRY("parodd",   control,     REV,               PARODD,     0 ),
178         MI_ENTRY("cs5",      control,     0,                 CS5,     CSIZE),
179         MI_ENTRY("cs6",      control,     0,                 CS6,     CSIZE),
180         MI_ENTRY("cs7",      control,     0,                 CS7,     CSIZE),
181         MI_ENTRY("cs8",      control,     0,                 CS8,     CSIZE),
182         MI_ENTRY("hupcl",    control,     REV,               HUPCL,      0 ),
183         MI_ENTRY("hup",      control,     REV        | OMIT, HUPCL,      0 ),
184         MI_ENTRY("cstopb",   control,     REV,               CSTOPB,     0 ),
185         MI_ENTRY("cread",    control,     SANE_SET   | REV,  CREAD,      0 ),
186         MI_ENTRY("clocal",   control,     REV,               CLOCAL,     0 ),
187 #ifdef CRTSCTS
188         MI_ENTRY("crtscts",  control,     REV,               CRTSCTS,    0 ),
189 #endif
190         MI_ENTRY("ignbrk",   input,       SANE_UNSET | REV,  IGNBRK,     0 ),
191         MI_ENTRY("brkint",   input,       SANE_SET   | REV,  BRKINT,     0 ),
192         MI_ENTRY("ignpar",   input,       REV,               IGNPAR,     0 ),
193         MI_ENTRY("parmrk",   input,       REV,               PARMRK,     0 ),
194         MI_ENTRY("inpck",    input,       REV,               INPCK,      0 ),
195         MI_ENTRY("istrip",   input,       REV,               ISTRIP,     0 ),
196         MI_ENTRY("inlcr",    input,       SANE_UNSET | REV,  INLCR,      0 ),
197         MI_ENTRY("igncr",    input,       SANE_UNSET | REV,  IGNCR,      0 ),
198         MI_ENTRY("icrnl",    input,       SANE_SET   | REV,  ICRNL,      0 ),
199         MI_ENTRY("ixon",     input,       REV,               IXON,       0 ),
200         MI_ENTRY("ixoff",    input,       SANE_UNSET | REV,  IXOFF,      0 ),
201         MI_ENTRY("tandem",   input,       REV        | OMIT, IXOFF,      0 ),
202 #ifdef IUCLC
203         MI_ENTRY("iuclc",    input,       SANE_UNSET | REV,  IUCLC,      0 ),
204 #endif
205 #ifdef IXANY
206         MI_ENTRY("ixany",    input,       SANE_UNSET | REV,  IXANY,      0 ),
207 #endif
208 #ifdef IMAXBEL
209         MI_ENTRY("imaxbel",  input,       SANE_SET   | REV,  IMAXBEL,    0 ),
210 #endif
211         MI_ENTRY("opost",    output,      SANE_SET   | REV,  OPOST,      0 ),
212 #ifdef OLCUC
213         MI_ENTRY("olcuc",    output,      SANE_UNSET | REV,  OLCUC,      0 ),
214 #endif
215 #ifdef OCRNL
216         MI_ENTRY("ocrnl",    output,      SANE_UNSET | REV,  OCRNL,      0 ),
217 #endif
218 #ifdef ONLCR
219         MI_ENTRY("onlcr",    output,      SANE_SET   | REV,  ONLCR,      0 ),
220 #endif
221 #ifdef ONOCR
222         MI_ENTRY("onocr",    output,      SANE_UNSET | REV,  ONOCR,      0 ),
223 #endif
224 #ifdef ONLRET
225         MI_ENTRY("onlret",   output,      SANE_UNSET | REV,  ONLRET,     0 ),
226 #endif
227 #ifdef OFILL
228         MI_ENTRY("ofill",    output,      SANE_UNSET | REV,  OFILL,      0 ),
229 #endif
230 #ifdef OFDEL
231         MI_ENTRY("ofdel",    output,      SANE_UNSET | REV,  OFDEL,      0 ),
232 #endif
233 #ifdef NLDLY
234         MI_ENTRY("nl1",      output,      SANE_UNSET,        NL1,     NLDLY),
235         MI_ENTRY("nl0",      output,      SANE_SET,          NL0,     NLDLY),
236 #endif
237 #ifdef CRDLY
238         MI_ENTRY("cr3",      output,      SANE_UNSET,        CR3,     CRDLY),
239         MI_ENTRY("cr2",      output,      SANE_UNSET,        CR2,     CRDLY),
240         MI_ENTRY("cr1",      output,      SANE_UNSET,        CR1,     CRDLY),
241         MI_ENTRY("cr0",      output,      SANE_SET,          CR0,     CRDLY),
242 #endif
243
244 #ifdef TABDLY
245         MI_ENTRY("tab3",     output,      SANE_UNSET,        TAB3,   TABDLY),
246         MI_ENTRY("tab2",     output,      SANE_UNSET,        TAB2,   TABDLY),
247         MI_ENTRY("tab1",     output,      SANE_UNSET,        TAB1,   TABDLY),
248         MI_ENTRY("tab0",     output,      SANE_SET,          TAB0,   TABDLY),
249 #else
250 # ifdef OXTABS
251         MI_ENTRY("tab3",     output,      SANE_UNSET,        OXTABS,     0 ),
252 # endif
253 #endif
254
255 #ifdef BSDLY
256         MI_ENTRY("bs1",      output,      SANE_UNSET,        BS1,     BSDLY),
257         MI_ENTRY("bs0",      output,      SANE_SET,          BS0,     BSDLY),
258 #endif
259 #ifdef VTDLY
260         MI_ENTRY("vt1",      output,      SANE_UNSET,        VT1,     VTDLY),
261         MI_ENTRY("vt0",      output,      SANE_SET,          VT0,     VTDLY),
262 #endif
263 #ifdef FFDLY
264         MI_ENTRY("ff1",      output,      SANE_UNSET,        FF1,     FFDLY),
265         MI_ENTRY("ff0",      output,      SANE_SET,          FF0,     FFDLY),
266 #endif
267         MI_ENTRY("isig",     local,       SANE_SET   | REV,  ISIG,       0 ),
268         MI_ENTRY("icanon",   local,       SANE_SET   | REV,  ICANON,     0 ),
269 #ifdef IEXTEN
270         MI_ENTRY("iexten",   local,       SANE_SET   | REV,  IEXTEN,     0 ),
271 #endif
272         MI_ENTRY("echo",     local,       SANE_SET   | REV,  ECHO,       0 ),
273         MI_ENTRY("echoe",    local,       SANE_SET   | REV,  ECHOE,      0 ),
274         MI_ENTRY("crterase", local,       REV        | OMIT, ECHOE,      0 ),
275         MI_ENTRY("echok",    local,       SANE_SET   | REV,  ECHOK,      0 ),
276         MI_ENTRY("echonl",   local,       SANE_UNSET | REV,  ECHONL,     0 ),
277         MI_ENTRY("noflsh",   local,       SANE_UNSET | REV,  NOFLSH,     0 ),
278 #ifdef XCASE
279         MI_ENTRY("xcase",    local,       SANE_UNSET | REV,  XCASE,      0 ),
280 #endif
281 #ifdef TOSTOP
282         MI_ENTRY("tostop",   local,       SANE_UNSET | REV,  TOSTOP,     0 ),
283 #endif
284 #ifdef ECHOPRT
285         MI_ENTRY("echoprt",  local,       SANE_UNSET | REV,  ECHOPRT,    0 ),
286         MI_ENTRY("prterase", local,       REV | OMIT,        ECHOPRT,    0 ),
287 #endif
288 #ifdef ECHOCTL
289         MI_ENTRY("echoctl",  local,       SANE_SET   | REV,  ECHOCTL,    0 ),
290         MI_ENTRY("ctlecho",  local,       REV        | OMIT, ECHOCTL,    0 ),
291 #endif
292 #ifdef ECHOKE
293         MI_ENTRY("echoke",   local,       SANE_SET   | REV,  ECHOKE,     0 ),
294         MI_ENTRY("crtkill",  local,       REV        | OMIT, ECHOKE,     0 ),
295 #endif
296         MI_ENTRY(evenp,      combination, REV        | OMIT, 0,          0 ),
297         MI_ENTRY(parity,     combination, REV        | OMIT, 0,          0 ),
298         MI_ENTRY(stty_oddp,  combination, REV        | OMIT, 0,          0 ),
299         MI_ENTRY(stty_nl,    combination, REV        | OMIT, 0,          0 ),
300         MI_ENTRY(stty_ek,    combination, OMIT,              0,          0 ),
301         MI_ENTRY(stty_sane,  combination, OMIT,              0,          0 ),
302         MI_ENTRY(cooked,     combination, REV        | OMIT, 0,          0 ),
303         MI_ENTRY(raw,        combination, REV        | OMIT, 0,          0 ),
304         MI_ENTRY(stty_pass8, combination, REV        | OMIT, 0,          0 ),
305         MI_ENTRY(litout,     combination, REV        | OMIT, 0,          0 ),
306         MI_ENTRY(cbreak,     combination, REV        | OMIT, 0,          0 ),
307 #ifdef IXANY
308         MI_ENTRY(decctlq,    combination, REV        | OMIT, 0,          0 ),
309 #endif
310 #if defined(TABDLY) || defined(OXTABS)
311         MI_ENTRY(stty_tabs,  combination, REV        | OMIT, 0,          0 ),
312 #endif
313 #if defined(XCASE) && defined(IUCLC) && defined(OLCUC)
314         MI_ENTRY(stty_lcase, combination, REV        | OMIT, 0,          0 ),
315         MI_ENTRY(stty_LCASE, combination, REV        | OMIT, 0,          0 ),
316 #endif
317         MI_ENTRY(stty_crt,   combination, OMIT,              0,          0 ),
318         MI_ENTRY(stty_dec,   combination, OMIT,              0,          0 ),
319 };
320
321 enum {
322         NUM_mode_info = (sizeof(mode_info) / sizeof(mode_info[0]))
323 };
324
325 /* Control character settings */
326 struct control_info {
327         const char * const name;               /* Name given on command line */
328         const unsigned char saneval;          /* Value to set for 'stty sane' */
329         const unsigned char offset;           /* Offset in c_cc */
330 };
331
332 /* Control characters */
333
334 static const struct control_info control_info[] = {
335         {"intr",     CINTR,   VINTR},
336         {"quit",     CQUIT,   VQUIT},
337         {"erase",    CERASE,  VERASE},
338         {"kill",     CKILL,   VKILL},
339         {stty_eof,   CEOF,    VEOF},
340         {stty_eol,   CEOL,    VEOL},
341 #ifdef VEOL2
342         {"eol2",     CEOL2,   VEOL2},
343 #endif
344 #ifdef VSWTCH
345         {stty_swtch, CSWTCH,  VSWTCH},
346 #endif
347         {"start",    CSTART,  VSTART},
348         {"stop",     CSTOP,   VSTOP},
349         {"susp",     CSUSP,   VSUSP},
350 #ifdef VDSUSP
351         {"dsusp",    CDSUSP,  VDSUSP},
352 #endif
353 #ifdef VREPRINT
354         {"rprnt",    CRPRNT,  VREPRINT},
355 #endif
356 #ifdef VWERASE
357         {"werase",   CWERASE, VWERASE},
358 #endif
359 #ifdef VLNEXT
360         {"lnext",    CLNEXT,  VLNEXT},
361 #endif
362 #ifdef VFLUSHO
363         {"flush",    CFLUSHO, VFLUSHO},
364 #endif
365 #ifdef VSTATUS
366         {"status",   CSTATUS, VSTATUS},
367 #endif
368         /* These must be last because of the display routines */
369         {stty_min,   1,       VMIN},
370         {stty_time,  0,       VTIME},
371 };
372
373 enum {
374         NUM_control_info = (sizeof(control_info) / sizeof(control_info[0]))
375 };
376
377 /* The width of the screen, for output wrapping */
378 static unsigned max_col = 80; /* default */
379 /* Current position, to know when to wrap */
380 static unsigned current_col;
381 static const char *device_name = bb_msg_standard_input;
382
383 /* Return a string that is the printable representation of character CH */
384 /* Adapted from 'cat' by Torbjorn Granlund */
385 static const char *visible(unsigned int ch)
386 {
387         static char buf[10];
388         char *bpout = buf;
389
390         if (ch == _POSIX_VDISABLE)
391                 return "<undef>";
392
393         if (ch >= 128) {
394                 ch -= 128;
395                 *bpout++ = 'M';
396                 *bpout++ = '-';
397         }
398
399         if (ch < 32) {
400                 *bpout++ = '^';
401                 *bpout++ = ch + 64;
402         } else if (ch < 127) {
403                 *bpout++ = ch;
404         } else {
405                 *bpout++ = '^';
406                 *bpout++ = '?';
407         }
408
409         *bpout = '\0';
410         return buf;
411 }
412
413 static tcflag_t *mode_type_flag(unsigned type, const struct termios *mode)
414 {
415         static const unsigned char tcflag_offsets[] = {
416                 offsetof(struct termios, c_cflag), /* control */
417                 offsetof(struct termios, c_iflag), /* input */
418                 offsetof(struct termios, c_oflag), /* output */
419                 offsetof(struct termios, c_lflag), /* local */
420         };
421
422         if (type <= local) {
423                 return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
424         }
425         return NULL;
426 }
427
428 static void set_speed_or_die(enum speed_setting type, const char * const arg,
429                                         struct termios * const mode)
430 {
431         speed_t baud;
432
433         baud = tty_value_to_baud(xatou(arg));
434
435         if (type != output_speed) {     /* either input or both */
436                 cfsetispeed(mode, baud);
437         }
438         if (type != input_speed) {      /* either output or both */
439                 cfsetospeed(mode, baud);
440         }
441 }
442
443 static ATTRIBUTE_NORETURN void perror_on_device_and_die(const char *fmt)
444 {
445         bb_perror_msg_and_die(fmt, device_name);
446 }
447
448 static void perror_on_device(const char *fmt)
449 {
450         bb_perror_msg(fmt, device_name);
451 }
452
453 /* Print format string MESSAGE and optional args.
454    Wrap to next line first if it won't fit.
455    Print a space first unless MESSAGE will start a new line */
456 static void wrapf(const char *message, ...)
457 {
458         char buf[128];
459         va_list args;
460         int buflen;
461
462         va_start(args, message);
463         buflen = vsnprintf(buf, sizeof(buf), message, args);
464         va_end(args);
465         /* We seem to be called only with suitable lengths, but check if
466            somebody failed to adhere to this assumption just to be sure.  */
467         if (!buflen || buflen >= sizeof(buf)) return;
468
469         if (current_col > 0) {
470                 current_col++;
471                 if (buf[0] != '\n') {
472                         if (current_col + buflen >= max_col) {
473                                 putchar('\n');
474                                 current_col = 0;
475                         } else
476                                 putchar(' ');
477                 }
478         }
479         fputs(buf, stdout);
480         current_col += buflen;
481         if (buf[buflen-1] == '\n')
482                 current_col = 0;
483 }
484
485 static void set_window_size(const int rows, const int cols)
486 {
487         struct winsize win = { 0, 0, 0, 0};
488
489         if (ioctl(STDIN_FILENO, TIOCGWINSZ, &win)) {
490                 if (errno != EINVAL) {
491                         goto bail;
492                 }
493                 memset(&win, 0, sizeof(win));
494         }
495
496         if (rows >= 0)
497                 win.ws_row = rows;
498         if (cols >= 0)
499                 win.ws_col = cols;
500
501         if (ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win))
502 bail:
503                 perror_on_device("%s");
504 }
505
506 static void display_window_size(const int fancy)
507 {
508         const char *fmt_str = "%s\0%s: no size information for this device";
509         unsigned width, height;
510
511         if (get_terminal_width_height(STDIN_FILENO, &width, &height)) {
512                 if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
513                         perror_on_device(fmt_str);
514                 }
515         } else {
516                 wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
517                                 height, width);
518         }
519 }
520
521 static const struct suffix_mult stty_suffixes[] = {
522         {"b",  512 },
523         {"k",  1024},
524         {"B",  1024},
525         {NULL, 0   }
526 };
527
528 static const struct mode_info *find_mode(const char *name)
529 {
530         int i;
531         for (i = 0; i < NUM_mode_info; ++i)
532                 if (!strcmp(name, mode_info[i].name))
533                         return &mode_info[i];
534         return 0;
535 }
536
537 static const struct control_info *find_control(const char *name)
538 {
539         int i;
540         for (i = 0; i < NUM_control_info; ++i)
541                 if (!strcmp(name, control_info[i].name))
542                         return &control_info[i];
543         return 0;
544 }
545
546 enum {
547         param_need_arg = 0x80,
548         param_line   = 1 | 0x80,
549         param_rows   = 2 | 0x80,
550         param_cols   = 3 | 0x80,
551         param_size   = 4,
552         param_speed  = 5,
553         param_ispeed = 6 | 0x80,
554         param_ospeed = 7 | 0x80,
555 };
556
557 static int find_param(const char * const name)
558 {
559         const char * const params[] = {
560                 "line",
561                 "rows",
562                 "cols",
563                 "columns",
564                 "size",
565                 "speed",
566                 "ispeed",
567                 "ospeed",
568                 NULL
569         };
570         int i = index_in_str_array(params, name);
571         if (i) {
572                 if (!(i == 4 || i == 5))
573                         i |= 0x80;
574         }
575         return i;
576 }
577
578 static int recover_mode(const char *arg, struct termios *mode)
579 {
580         int i, n;
581         unsigned int chr;
582         unsigned long iflag, oflag, cflag, lflag;
583
584         /* Scan into temporaries since it is too much trouble to figure out
585            the right format for 'tcflag_t' */
586         if (sscanf(arg, "%lx:%lx:%lx:%lx%n",
587                            &iflag, &oflag, &cflag, &lflag, &n) != 4)
588                 return 0;
589         mode->c_iflag = iflag;
590         mode->c_oflag = oflag;
591         mode->c_cflag = cflag;
592         mode->c_lflag = lflag;
593         arg += n;
594         for (i = 0; i < NCCS; ++i) {
595                 if (sscanf(arg, ":%x%n", &chr, &n) != 1)
596                         return 0;
597                 mode->c_cc[i] = chr;
598                 arg += n;
599         }
600
601         /* Fail if there are too many fields */
602         if (*arg != '\0')
603                 return 0;
604
605         return 1;
606 }
607
608 static void display_recoverable(const struct termios *mode,
609                                                                 const int ATTRIBUTE_UNUSED dummy)
610 {
611         int i;
612         printf("%lx:%lx:%lx:%lx",
613                    (unsigned long) mode->c_iflag, (unsigned long) mode->c_oflag,
614                    (unsigned long) mode->c_cflag, (unsigned long) mode->c_lflag);
615         for (i = 0; i < NCCS; ++i)
616                 printf(":%x", (unsigned int) mode->c_cc[i]);
617         putchar('\n');
618 }
619
620 static void display_speed(const struct termios *mode, int fancy)
621 {
622                              //01234567 8 9
623         const char *fmt_str = "%lu %lu\n\0ispeed %lu baud; ospeed %lu baud;";
624         unsigned long ispeed, ospeed;
625
626         ospeed = ispeed = cfgetispeed(mode);
627         if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
628                 ispeed = ospeed;                /* in case ispeed was 0 */
629                          //0123 4 5 6 7 8 9
630                 fmt_str = "%lu\n\0\0\0\0\0speed %lu baud;";
631         }
632         if (fancy) fmt_str += 9;
633         wrapf(fmt_str, tty_baud_to_value(ispeed), tty_baud_to_value(ospeed));
634 }
635
636 static void do_display(const struct termios *mode, const int all)
637 {
638         int i;
639         tcflag_t *bitsp;
640         unsigned long mask;
641         int prev_type = control;
642
643         display_speed(mode, 1);
644         if (all)
645                 display_window_size(1);
646 #ifdef HAVE_C_LINE
647         wrapf("line = %d;\n", mode->c_line);
648 #else
649         wrapf("\n");
650 #endif
651
652         for (i = 0; control_info[i].name != stty_min; ++i) {
653                 /* If swtch is the same as susp, don't print both */
654 #if VSWTCH == VSUSP
655                 if (control_info[i].name == stty_swtch)
656                         continue;
657 #endif
658                 /* If eof uses the same slot as min, only print whichever applies */
659 #if VEOF == VMIN
660                 if ((mode->c_lflag & ICANON) == 0
661                         && (control_info[i].name == stty_eof
662                                 || control_info[i].name == stty_eol)) continue;
663 #endif
664                 wrapf("%s = %s;", control_info[i].name,
665                           visible(mode->c_cc[control_info[i].offset]));
666         }
667 #if VEOF == VMIN
668         if ((mode->c_lflag & ICANON) == 0)
669 #endif
670                 wrapf("min = %d; time = %d;", mode->c_cc[VMIN], mode->c_cc[VTIME]);
671         if (current_col) wrapf("\n");
672
673         for (i = 0; i < NUM_mode_info; ++i) {
674                 if (mode_info[i].flags & OMIT)
675                         continue;
676                 if (mode_info[i].type != prev_type) {
677                         /* wrapf("\n"); */
678                         if (current_col) wrapf("\n");
679                         prev_type = mode_info[i].type;
680                 }
681
682                 bitsp = mode_type_flag(mode_info[i].type, mode);
683                 mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
684                 if ((*bitsp & mask) == mode_info[i].bits) {
685                         if (all || (mode_info[i].flags & SANE_UNSET))
686                                 wrapf("%s", mode_info[i].name);
687                 } else {
688                         if ((all && mode_info[i].flags & REV) ||
689                                  (!all &&
690                                   (mode_info[i].flags & (SANE_SET | REV)) == (SANE_SET | REV)))
691                                 wrapf("-%s", mode_info[i].name);
692                 }
693         }
694         if (current_col) wrapf("\n");
695 }
696
697 static void sane_mode(struct termios *mode)
698 {
699         int i;
700         tcflag_t *bitsp;
701
702         for (i = 0; i < NUM_control_info; ++i) {
703 #if VMIN == VEOF
704                 if (control_info[i].name == stty_min)
705                         break;
706 #endif
707                 mode->c_cc[control_info[i].offset] = control_info[i].saneval;
708         }
709
710         for (i = 0; i < NUM_mode_info; ++i) {
711                 if (mode_info[i].flags & SANE_SET) {
712                         bitsp = mode_type_flag(mode_info[i].type, mode);
713                         *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask))
714                                 | mode_info[i].bits;
715                 } else if (mode_info[i].flags & SANE_UNSET) {
716                         bitsp = mode_type_flag(mode_info[i].type, mode);
717                         *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
718                                 & ~mode_info[i].bits;
719                 }
720         }
721 }
722
723 /* Save set_mode from #ifdef forest plague */
724 #ifndef ONLCR
725 #define ONLCR 0
726 #endif
727 #ifndef OCRNL
728 #define OCRNL 0
729 #endif
730 #ifndef ONLRET
731 #define ONLRET 0
732 #endif
733 #ifndef XCASE
734 #define XCASE 0
735 #endif
736 #ifndef IXANY
737 #define IXANY 0
738 #endif
739 #ifndef TABDLY
740 #define TABDLY 0
741 #endif
742 #ifndef OXTABS
743 #define OXTABS 0
744 #endif
745 #ifndef IUCLC
746 #define IUCLC 0
747 #endif
748 #ifndef OLCUC
749 #define OLCUC 0
750 #endif
751 #ifndef ECHOCTL
752 #define ECHOCTL 0
753 #endif
754 #ifndef ECHOKE
755 #define ECHOKE 0
756 #endif
757
758 static void set_mode(const struct mode_info *info, int reversed,
759                                         struct termios *mode)
760 {
761         tcflag_t *bitsp;
762
763         bitsp = mode_type_flag(info->type, mode);
764
765         if (bitsp) {
766                 if (reversed)
767                         *bitsp = *bitsp & ~info->mask & ~info->bits;
768                 else
769                         *bitsp = (*bitsp & ~info->mask) | info->bits;
770                 return;
771         }
772
773         /* Combination mode */
774         if (info->name == evenp || info->name == parity) {
775                 if (reversed)
776                         mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
777                 else
778                         mode->c_cflag = (mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7;
779         } else if (info->name == stty_oddp) {
780                 if (reversed)
781                         mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
782                 else
783                         mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB;
784         } else if (info->name == stty_nl) {
785                 if (reversed) {
786                         mode->c_iflag = (mode->c_iflag | ICRNL) & ~INLCR & ~IGNCR;
787                         mode->c_oflag = (mode->c_oflag | ONLCR) & ~OCRNL & ~ONLRET;
788                 } else {
789                         mode->c_iflag = mode->c_iflag & ~ICRNL;
790                         if (ONLCR) mode->c_oflag = mode->c_oflag & ~ONLCR;
791                 }
792         } else if (info->name == stty_ek) {
793                 mode->c_cc[VERASE] = CERASE;
794                 mode->c_cc[VKILL] = CKILL;
795         } else if (info->name == stty_sane) {
796                 sane_mode(mode);
797         }
798         else if (info->name == cbreak) {
799                 if (reversed)
800                         mode->c_lflag |= ICANON;
801                 else
802                         mode->c_lflag &= ~ICANON;
803         } else if (info->name == stty_pass8) {
804                 if (reversed) {
805                         mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
806                         mode->c_iflag |= ISTRIP;
807                 } else {
808                         mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
809                         mode->c_iflag &= ~ISTRIP;
810                 }
811         } else if (info->name == litout) {
812                 if (reversed) {
813                         mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
814                         mode->c_iflag |= ISTRIP;
815                         mode->c_oflag |= OPOST;
816                 } else {
817                         mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
818                         mode->c_iflag &= ~ISTRIP;
819                         mode->c_oflag &= ~OPOST;
820                 }
821         } else if (info->name == raw || info->name == cooked) {
822                 if ((info->name[0] == 'r' && reversed)
823                         || (info->name[0] == 'c' && !reversed)) {
824                         /* Cooked mode */
825                         mode->c_iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;
826                         mode->c_oflag |= OPOST;
827                         mode->c_lflag |= ISIG | ICANON;
828 #if VMIN == VEOF
829                         mode->c_cc[VEOF] = CEOF;
830 #endif
831 #if VTIME == VEOL
832                         mode->c_cc[VEOL] = CEOL;
833 #endif
834                 } else {
835                         /* Raw mode */
836                         mode->c_iflag = 0;
837                         mode->c_oflag &= ~OPOST;
838                         mode->c_lflag &= ~(ISIG | ICANON | XCASE);
839                         mode->c_cc[VMIN] = 1;
840                         mode->c_cc[VTIME] = 0;
841                 }
842         }
843         else if (IXANY && info->name == decctlq) {
844                 if (reversed)
845                         mode->c_iflag |= IXANY;
846                 else
847                         mode->c_iflag &= ~IXANY;
848         }
849         else if (TABDLY && info->name == stty_tabs) {
850                 if (reversed)
851                         mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB3;
852                 else
853                         mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB0;
854         }
855         else if (OXTABS && info->name == stty_tabs) {
856                 if (reversed)
857                         mode->c_oflag |= OXTABS;
858                 else
859                         mode->c_oflag &= ~OXTABS;
860         }
861         else if (XCASE && IUCLC && OLCUC
862         && (info->name == stty_lcase || info->name == stty_LCASE)) {
863                 if (reversed) {
864                         mode->c_lflag &= ~XCASE;
865                         mode->c_iflag &= ~IUCLC;
866                         mode->c_oflag &= ~OLCUC;
867                 } else {
868                         mode->c_lflag |= XCASE;
869                         mode->c_iflag |= IUCLC;
870                         mode->c_oflag |= OLCUC;
871                 }
872         }
873         else if (info->name == stty_crt) {
874                 mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE;
875         }
876         else if (info->name == stty_dec) {
877                 mode->c_cc[VINTR] = 3; /* ^C */
878                 mode->c_cc[VERASE] = 127; /* DEL */
879                 mode->c_cc[VKILL] = 21; /* ^U */
880                 mode->c_lflag |= ECHOE | ECHOCTL | ECHOKE;
881                 if (IXANY) mode->c_iflag &= ~IXANY;
882         }
883 }
884
885 static void set_control_char_or_die(const struct control_info *info,
886                         const char *arg, struct termios *mode)
887 {
888         unsigned char value;
889
890         if (info->name == stty_min || info->name == stty_time)
891                 value = xatoul_range_sfx(arg, 0, 0xff, stty_suffixes);
892         else if (arg[0] == '\0' || arg[1] == '\0')
893                 value = arg[0];
894         else if (!strcmp(arg, "^-") || !strcmp(arg, "undef"))
895                 value = _POSIX_VDISABLE;
896         else if (arg[0] == '^') { /* Ignore any trailing junk (^Cjunk) */
897                 value = arg[1] & 0x1f; /* Non-letters get weird results */
898                 if (arg[1] == '?')
899                         value = 127;
900         } else
901                 value = xatoul_range_sfx(arg, 0, 0xff, stty_suffixes);
902         mode->c_cc[info->offset] = value;
903 }
904
905 #define STTY_require_set_attr   (1<<0)
906 #define STTY_speed_was_set      (1<<1)
907 #define STTY_verbose_output     (1<<2)
908 #define STTY_recoverable_output (1<<3)
909 #define STTY_noargs             (1<<4)
910 int stty_main(int argc, char **argv)
911 {
912         struct termios mode;
913         void (*output_func)(const struct termios *, const int);
914         const char *file_name = NULL;
915         int display_all = 0;
916         int stty_state;
917         int k;
918
919         stty_state = STTY_noargs;
920         output_func = do_display;
921
922         /* First pass: only parse/verify command line params */
923         k = 0;
924         while (argv[++k]) {
925                 const struct mode_info *mp;
926                 const struct control_info *cp;
927                 const char *arg = argv[k];
928                 const char *argnext = argv[k+1];
929                 int param;
930
931                 if (arg[0] == '-') {
932                         int i;
933                         mp = find_mode(arg+1);
934                         if (mp) {
935                                 if (!(mp->flags & REV))
936                                         goto invalid_argument;
937                                 stty_state &= ~STTY_noargs;
938                                 continue;
939                         }
940                         /* It is an option - parse it */
941                         i = 0;
942                         while (arg[++i]) {
943                                 switch (arg[i]) {
944                                 case 'a':
945                                         stty_state |= STTY_verbose_output;
946                                         output_func = do_display;
947                                         display_all = 1;
948                                         break;
949                                 case 'g':
950                                         stty_state |= STTY_recoverable_output;
951                                         output_func = display_recoverable;
952                                         break;
953                                 case 'F':
954                                         if (file_name)
955                                                 bb_error_msg_and_die("only one device may be specified");
956                                         file_name = &arg[i+1]; /* "-Fdevice" ? */
957                                         if (!file_name[0]) { /* nope, "-F device" */
958                                                 int p = k+1; /* argv[p] is argnext */
959                                                 file_name = argnext;
960                                                 if (!file_name)
961                                                         bb_error_msg_and_die(bb_msg_requires_arg, "-F");
962                                                 /* remove -F param from arg[vc] */
963                                                 --argc;
964                                                 while (argv[p]) { argv[p] = argv[p+1]; ++p; }
965                                         }
966                                         goto end_option;
967                                 default:
968                                         goto invalid_argument;
969                                 }
970                         }
971 end_option:
972                         continue;
973                 }
974
975                 mp = find_mode(arg);
976                 if (mp) {
977                         stty_state &= ~STTY_noargs;
978                         continue;
979                 }
980
981                 cp = find_control(arg);
982                 if (cp) {
983                         if (!argnext)
984                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
985                         /* called for the side effect of xfunc death only */
986                         set_control_char_or_die(cp, argnext, &mode);
987                         stty_state &= ~STTY_noargs;
988                         ++k;
989                         continue;
990                 }
991
992                 param = find_param(arg);
993                 if (param & param_need_arg) {
994                         if (!argnext)
995                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
996                         ++k;
997                 }
998
999                 switch (param) {
1000 #ifdef HAVE_C_LINE
1001                 case param_line:
1002 # ifndef TIOCGWINSZ
1003                         xatoul_range_sfx(argnext, 1, INT_MAX, stty_suffixes);
1004                         break;
1005 # endif /* else fall-through */
1006 #endif
1007 #ifdef TIOCGWINSZ
1008                 case param_rows:
1009                 case param_cols:
1010                         xatoul_range_sfx(argnext, 1, INT_MAX, stty_suffixes);
1011                         break;
1012                 case param_size:
1013 #endif
1014                 case param_speed:
1015                         break;
1016                 case param_ispeed:
1017                         /* called for the side effect of xfunc death only */
1018                         set_speed_or_die(input_speed, argnext, &mode);
1019                         break;
1020                 case param_ospeed:
1021                         /* called for the side effect of xfunc death only */
1022                         set_speed_or_die(output_speed, argnext, &mode);
1023                         break;
1024                 default:
1025                         if (recover_mode(arg, &mode) == 1) break;
1026                         if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) break;
1027 invalid_argument:
1028                         bb_error_msg_and_die("invalid argument '%s'", arg);
1029                 }
1030                 stty_state &= ~STTY_noargs;
1031         }
1032
1033         /* Specifying both -a and -g is an error */
1034         if ((stty_state & (STTY_verbose_output | STTY_recoverable_output)) ==
1035                 (STTY_verbose_output | STTY_recoverable_output))
1036                 bb_error_msg_and_die("verbose and stty-readable output styles are mutually exclusive");
1037         /* Specifying -a or -g with non-options is an error */
1038         if (!(stty_state & STTY_noargs) &&
1039                 (stty_state & (STTY_verbose_output | STTY_recoverable_output)))
1040                 bb_error_msg_and_die("modes may not be set when specifying an output style");
1041
1042         /* Now it is safe to start doing things */
1043         if (file_name) {
1044                 int fd, fdflags;
1045                 device_name = file_name;
1046                 fd = xopen(device_name, O_RDONLY | O_NONBLOCK);
1047                 if (fd != STDIN_FILENO) {
1048                         dup2(fd, STDIN_FILENO);
1049                         close(fd);
1050                 }
1051                 fdflags = fcntl(STDIN_FILENO, F_GETFL);
1052                 if (fdflags < 0 ||
1053                         fcntl(STDIN_FILENO, F_SETFL, fdflags & ~O_NONBLOCK) < 0)
1054                         perror_on_device_and_die("%s: cannot reset non-blocking mode");
1055         }
1056
1057         /* Initialize to all zeroes so there is no risk memcmp will report a
1058            spurious difference in an uninitialized portion of the structure */
1059         memset(&mode, 0, sizeof(mode));
1060         if (tcgetattr(STDIN_FILENO, &mode))
1061                 perror_on_device_and_die("%s");
1062
1063         if (stty_state & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) {
1064                 get_terminal_width_height(STDOUT_FILENO, &max_col, NULL);
1065                 output_func(&mode, display_all);
1066                 return EXIT_SUCCESS;
1067         }
1068
1069         /* Second pass: perform actions */
1070         k = 0;
1071         while (argv[++k]) {
1072                 const struct mode_info *mp;
1073                 const struct control_info *cp;
1074                 const char *arg = argv[k];
1075                 const char *argnext = argv[k+1];
1076                 int param;
1077
1078                 if (arg[0] == '-') {
1079                         mp = find_mode(arg+1);
1080                         if (mp) {
1081                                 set_mode(mp, 1 /* reversed */, &mode);
1082                                 stty_state |= STTY_require_set_attr;
1083                         }
1084                         /* It is an option - already parsed. Skip it */
1085                         continue;
1086                 }
1087
1088                 mp = find_mode(arg);
1089                 if (mp) {
1090                         set_mode(mp, 0 /* non-reversed */, &mode);
1091                         stty_state |= STTY_require_set_attr;
1092                         continue;
1093                 }
1094
1095                 cp = find_control(arg);
1096                 if (cp) {
1097                         ++k;
1098                         set_control_char_or_die(cp, argnext, &mode);
1099                         stty_state |= STTY_require_set_attr;
1100                         continue;
1101                 }
1102
1103                 param = find_param(arg);
1104                 if (param & param_need_arg) {
1105                         ++k;
1106                 }
1107
1108                 switch (param) {
1109 #ifdef HAVE_C_LINE
1110                 case param_line:
1111                         mode.c_line = xatoul_sfx(argnext, stty_suffixes);
1112                         stty_state |= STTY_require_set_attr;
1113                         break;
1114 #endif
1115 #ifdef TIOCGWINSZ
1116                 case param_cols:
1117                         set_window_size(-1, xatoul_sfx(argnext, stty_suffixes));
1118                         break;
1119                 case param_size:
1120                         display_window_size(0);
1121                         break;
1122                 case param_rows:
1123                         set_window_size(xatoul_sfx(argnext, stty_suffixes), -1);
1124                         break;
1125 #endif
1126                 case param_speed:
1127                         display_speed(&mode, 0);
1128                         break;
1129                 case param_ispeed:
1130                         set_speed_or_die(input_speed, argnext, &mode);
1131                         stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
1132                         break;
1133                 case param_ospeed:
1134                         set_speed_or_die(output_speed, argnext, &mode);
1135                         stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
1136                         break;
1137                 default:
1138                         if (recover_mode(arg, &mode) == 1)
1139                                 stty_state |= STTY_require_set_attr;
1140                         else /* true: if (tty_value_to_baud(xatou(arg)) != (speed_t) -1) */{
1141                                 set_speed_or_die(both_speeds, arg, &mode);
1142                                 stty_state |= (STTY_require_set_attr | STTY_speed_was_set);
1143                         } /* else - impossible (caught in the first pass):
1144                                 bb_error_msg_and_die("invalid argument '%s'", arg); */
1145                 }
1146         }
1147
1148         if (stty_state & STTY_require_set_attr) {
1149                 struct termios new_mode;
1150
1151                 if (tcsetattr(STDIN_FILENO, TCSADRAIN, &mode))
1152                         perror_on_device_and_die("%s");
1153
1154                 /* POSIX (according to Zlotnick's book) tcsetattr returns zero if
1155                    it performs *any* of the requested operations.  This means it
1156                    can report 'success' when it has actually failed to perform
1157                    some proper subset of the requested operations.  To detect
1158                    this partial failure, get the current terminal attributes and
1159                    compare them to the requested ones */
1160
1161                 /* Initialize to all zeroes so there is no risk memcmp will report a
1162                    spurious difference in an uninitialized portion of the structure */
1163                 memset(&new_mode, 0, sizeof(new_mode));
1164                 if (tcgetattr(STDIN_FILENO, &new_mode))
1165                         perror_on_device_and_die("%s");
1166
1167                 if (memcmp(&mode, &new_mode, sizeof(mode)) != 0) {
1168 #ifdef CIBAUD
1169                         /* SunOS 4.1.3 (at least) has the problem that after this sequence,
1170                            tcgetattr (&m1); tcsetattr (&m1); tcgetattr (&m2);
1171                            sometimes (m1 != m2).  The only difference is in the four bits
1172                            of the c_cflag field corresponding to the baud rate.  To save
1173                            Sun users a little confusion, don't report an error if this
1174                            happens.  But suppress the error only if we haven't tried to
1175                            set the baud rate explicitly -- otherwise we'd never give an
1176                            error for a true failure to set the baud rate */
1177
1178                         new_mode.c_cflag &= (~CIBAUD);
1179                         if ((stty_state & STTY_speed_was_set)
1180                          || memcmp(&mode, &new_mode, sizeof(mode)) != 0)
1181 #endif
1182                                 perror_on_device_and_die("%s: cannot perform all requested operations");
1183                 }
1184         }
1185
1186         return EXIT_SUCCESS;
1187 }