kernel: restore setting KTS_NONE
[minix.git] / commands / stty / stty.c
blob309cbc5becccac2ba09f8fe8045266019bdcd98f
1 /* stty - set terminal mode Author: Andy Tanenbaum */
3 /*
4 Adapted to POSIX 1003.2 by Philip Homburg.
5 */
7 #ifdef __minix_vmd
8 #define _MINIX_SOURCE
9 #endif
11 #include <assert.h>
12 #include <ctype.h>
13 #include <errno.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <termios.h>
18 #ifdef __minix
19 #include <sys/types.h>
20 #include <sys/ioctl.h>
21 #endif
22 #ifdef __NBSD_LIBC
23 #include <unistd.h>
24 #endif
26 /* Default settings, the Minix ones are defined in <termios.h> */
28 #ifndef TCTRL_DEF
29 #define TCTRL_DEF (PARENB | CREAD | CS7)
30 #endif
32 #ifndef TSPEED_DEF
33 #define TSPEED_DEF B1200
34 #endif
36 #ifndef TINPUT_DEF
37 #define TINPUT_DEF (BRKINT | IGNPAR | ISTRIP | ICRNL)
38 #endif
40 #ifndef TOUTPUT_DEF
41 #define TOUTPUT_DEF OPOST
42 #endif
44 #ifndef TLOCAL_DEF
45 #define TLOCAL_DEF (ISIG | IEXTEN | ICANON | ECHO | ECHOE)
46 #endif
48 #ifndef TEOF_DEF
49 #define TEOF_DEF '\4' /* ^D */
50 #endif
51 #ifndef TEOL_DEF
52 #ifdef _POSIX_VDISABLE
53 #define TEOL_DEF _POSIX_VDISABLE
54 #else
55 #define TEOL_DEF '\n'
56 #endif
57 #endif
58 #ifndef TERASE_DEF
59 #define TERASE_DEF '\10' /* ^H */
60 #endif
61 #ifndef TINTR_DEF
62 #define TINTR_DEF '\177' /* ^? */
63 #endif
64 #ifndef TKILL_DEF
65 #define TKILL_DEF '\25' /* ^U */
66 #endif
67 #ifndef TQUIT_DEF
68 #define TQUIT_DEF '\34' /* ^\ */
69 #endif
70 #ifndef TSUSP_DEF
71 #define TSUSP_DEF '\32' /* ^Z */
72 #endif
73 #ifndef TSTART_DEF
74 #define TSTART_DEF '\21' /* ^Q */
75 #endif
76 #ifndef TSTOP_DEF
77 #define TSTOP_DEF '\23' /* ^S */
78 #endif
79 #ifndef TMIN_DEF
80 #define TMIN_DEF 1
81 #endif
82 #ifndef TTIME_DEF
83 #define TTIME_DEF 0
84 #endif
88 char *prog_name;
89 struct termios termios;
90 int column= 0, max_column=80; /* Assume 80 character terminals. */
91 #ifdef __minix
92 struct winsize winsize;
93 #endif
95 #define PROTO(a) a
97 int main PROTO(( int argc, char **argv ));
98 void report PROTO(( int flags ));
99 int option PROTO(( char *opt, char *next ));
100 int match PROTO(( char *s1, char *s2 ));
101 void prctl PROTO(( int c ));
102 speed_t long2speed PROTO(( long num ));
103 long speed2long PROTO(( unsigned long speed ));
104 void print_flags PROTO(( unsigned long flags, unsigned long flag,
105 unsigned long def, char *string, int all ));
106 void output PROTO(( char *s ));
107 void do_print_char PROTO(( unsigned chr, unsigned def, char *name, int all ));
108 void do_print_num PROTO(( unsigned num, unsigned def, char *name, int all ));
109 void set_saved_settings PROTO(( char *opt ));
110 void set_control PROTO(( int option, char *value ));
111 void set_min_tim PROTO(( int option, char *value ));
113 #define print_char(c,d,n,a) (do_print_char((unsigned)(c),(unsigned)(d),(n),(a)))
114 #define print_num(m,d,n,a) (do_print_num((unsigned)(m),(unsigned)(d),(n),(a)))
116 int main(argc, argv)
117 int argc;
118 char *argv[];
120 int flags, k;
122 prog_name= argv[0];
123 flags= 0;
125 /* Stty with no arguments just reports on current status. */
126 if (tcgetattr(0, &termios) == -1)
128 fprintf(stderr, "%s: can't read ioctl parameters from stdin: %s\n",
129 prog_name, strerror(errno));
130 exit(1);
132 #ifdef __minix
133 if (ioctl(0, TIOCGWINSZ, &winsize) == -1)
135 fprintf(stderr, "%s: can't get screen size from stdin: %s\n",
136 prog_name, strerror(errno));
137 exit(1);
139 if (winsize.ws_col != 0)
140 max_column= winsize.ws_col;
141 #endif
143 if (argc == 2)
145 if (!strcmp(argv[1], "-a"))
146 flags |= 1;
147 else if (!strcmp(argv[1], "-g"))
148 flags |= 2;
150 if (argc == 1 || flags) {
151 report(flags);
152 exit(0);
155 /* Process the options specified. */
156 for (k= 1; k < argc; k++)
157 k += option(argv[k], k+1 < argc ? argv[k+1] : "");
159 #ifdef __minix
160 if (ioctl(0, TIOCSWINSZ, &winsize) == -1)
162 fprintf(stderr, "%s: can't set screen size to stdin: %s\n",
163 prog_name, strerror(errno));
164 exit(1);
166 #endif
167 if (tcsetattr(0, TCSANOW, &termios) == -1)
169 fprintf(stderr, "%s: can't set terminal parameters to stdin: %s\n",
170 prog_name, strerror(errno));
171 exit(1);
173 exit(0);
178 void report(flags)
179 int flags;
181 int i, all;
182 tcflag_t c_cflag, c_iflag, c_oflag, c_lflag;
183 char line[80];
184 speed_t ispeed, ospeed;
186 if (flags & 2)
187 { /* We have to write the termios structure in a encoded form
188 * to stdout.
190 printf(":%x:%x:%x:%x:%x:%x", termios.c_iflag, termios.c_oflag,
191 termios.c_cflag, termios.c_lflag, cfgetispeed(&termios),
192 cfgetospeed(&termios));
193 for (i= 0; i<NCCS; i++)
194 printf(":%x", termios.c_cc[i]);
195 printf(":\n");
196 return;
199 all= !!flags;
201 /* Start with the baud rate. */
202 ispeed= cfgetispeed(&termios);
203 ospeed= cfgetospeed(&termios);
204 if (ispeed != ospeed)
206 sprintf(line, "ispeed %lu baud; ospeed %lu baud;",
207 speed2long(ispeed), speed2long(ospeed));
208 output(line);
210 else if (all || ospeed != TSPEED_DEF)
212 sprintf(line, "speed %lu baud;", speed2long(ospeed));
213 output(line);
216 /* The control modes. */
218 c_cflag= termios.c_cflag;
219 if (all || (c_cflag & CSIZE) != (TCTRL_DEF & CSIZE))
221 switch (c_cflag & CSIZE)
223 case CS5: output("cs5"); break;
224 case CS6: output("cs6"); break;
225 case CS7: output("cs7"); break;
226 case CS8: output("cs8"); break;
227 default: output("cs??"); break;
230 print_flags(c_cflag, PARENB, TCTRL_DEF, "-parenb", all);
231 print_flags(c_cflag, PARODD, TCTRL_DEF, "-parodd", all);
232 print_flags(c_cflag, HUPCL, TCTRL_DEF, "-hupcl", all);
233 print_flags(c_cflag, CSTOPB, TCTRL_DEF, "-cstopb", all);
234 print_flags(c_cflag, CREAD, TCTRL_DEF, "-cread", all);
235 print_flags(c_cflag, CLOCAL, TCTRL_DEF, "-clocal", all);
237 if (all)
239 printf("\n");
240 column= 0;
243 /* The input flags. */
245 c_iflag= termios.c_iflag;
247 print_flags(c_iflag, IGNBRK, TINPUT_DEF, "-ignbrk", all);
248 print_flags(c_iflag, BRKINT, TINPUT_DEF, "-brkint", all);
249 print_flags(c_iflag, IGNPAR, TINPUT_DEF, "-ignpar", all);
250 print_flags(c_iflag, PARMRK, TINPUT_DEF, "-parmrk", all);
251 print_flags(c_iflag, INPCK, TINPUT_DEF, "-inpck", all);
252 print_flags(c_iflag, ISTRIP, TINPUT_DEF, "-istrip", all);
253 print_flags(c_iflag, INLCR, TINPUT_DEF, "-inlcr", all);
254 print_flags(c_iflag, IGNCR, TINPUT_DEF, "-igncr", all);
255 print_flags(c_iflag, ICRNL, TINPUT_DEF, "-icrnl", all);
256 print_flags(c_iflag, IXON, TINPUT_DEF, "-ixon", all);
257 print_flags(c_iflag, IXOFF, TINPUT_DEF, "-ixoff", all);
258 print_flags(c_iflag, IXANY, TINPUT_DEF, "-ixany", all);
260 if (all)
262 printf("\n");
263 column= 0;
266 /* The output flags. */
268 c_oflag= termios.c_oflag;
270 print_flags(c_oflag, OPOST, TOUTPUT_DEF, "-opost", all);
271 #ifdef __minix
272 print_flags(c_oflag, ONLCR, TOUTPUT_DEF, "-onlcr", all);
273 print_flags(c_oflag, XTABS, TOUTPUT_DEF, "-xtabs", all);
274 print_flags(c_oflag, ONOEOT, TOUTPUT_DEF, "-onoeot", all);
275 #endif
276 if (all)
278 printf("\n");
279 column= 0;
282 /* The local flags. */
284 c_lflag= termios.c_lflag;
286 print_flags(c_lflag, ISIG, TLOCAL_DEF, "-isig", all);
287 print_flags(c_lflag, ICANON, TLOCAL_DEF, "-icanon", all);
288 print_flags(c_lflag, IEXTEN, TLOCAL_DEF, "-iexten", all);
289 print_flags(c_lflag, ECHO, TLOCAL_DEF, "-echo", all);
290 print_flags(c_lflag, ECHOE, TLOCAL_DEF, "-echoe", all);
291 print_flags(c_lflag, ECHOK, TLOCAL_DEF, "-echok", all);
292 print_flags(c_lflag, ECHONL, TLOCAL_DEF, "-echonl", all);
293 print_flags(c_lflag, NOFLSH, TLOCAL_DEF, "-noflsh", all);
294 #ifdef TOSTOP
295 print_flags(c_lflag, TOSTOP, TLOCAL_DEF, "-tostop", all);
296 #endif
297 #ifdef __minix
298 print_flags(c_lflag, LFLUSHO, TLOCAL_DEF, "-lflusho", all);
299 #endif
301 if (all)
303 printf("\n");
304 column= 0;
307 /* The special control characters. */
309 print_char(termios.c_cc[VEOF], TEOF_DEF, "eof", all);
310 print_char(termios.c_cc[VEOL], TEOL_DEF, "eol", all);
311 print_char(termios.c_cc[VERASE], TERASE_DEF, "erase", all);
312 print_char(termios.c_cc[VINTR], TINTR_DEF, "intr", all);
313 print_char(termios.c_cc[VKILL], TKILL_DEF, "kill", all);
314 print_char(termios.c_cc[VQUIT], TQUIT_DEF, "quit", all);
315 print_char(termios.c_cc[VSUSP], TSUSP_DEF, "susp", all);
316 print_char(termios.c_cc[VSTART], TSTART_DEF, "start", all);
317 print_char(termios.c_cc[VSTOP], TSTOP_DEF, "stop", all);
318 #ifdef __minix
319 print_char(termios.c_cc[VREPRINT], TREPRINT_DEF, "rprnt", all);
320 print_char(termios.c_cc[VLNEXT], TLNEXT_DEF, "lnext", all);
321 print_char(termios.c_cc[VDISCARD], TDISCARD_DEF, "flush", all);
322 #endif
323 print_num(termios.c_cc[VMIN], TMIN_DEF, "min", all);
324 print_num(termios.c_cc[VTIME], TTIME_DEF, "time", all);
325 if (all)
327 printf("\n");
328 column= 0;
331 #ifdef __minix
332 /* Screen size */
333 if (all || winsize.ws_row != 0 || winsize.ws_col != 0)
335 sprintf(line, "%d rows %d columns", winsize.ws_row,
336 winsize.ws_col);
337 output(line);
340 if (all || winsize.ws_ypixel != 0 || winsize.ws_xpixel != 0)
342 sprintf(line, "%d ypixels %d xpixels", winsize.ws_ypixel,
343 winsize.ws_xpixel);
344 output(line);
347 if (all)
349 printf("\n");
350 column= 0;
352 #endif
354 if (column != 0)
356 printf("\n");
357 column= 0;
361 int option(opt, next)
362 char *opt, *next;
364 char *check;
365 speed_t speed;
366 long num;
368 /* The control options. */
370 if (match(opt, "clocal")) {
371 termios.c_cflag |= CLOCAL;
372 return 0;
374 if (match(opt, "-clocal")) {
375 termios.c_cflag &= ~CLOCAL;
376 return 0;
379 if (match(opt, "cread")) {
380 termios.c_cflag |= CREAD;
381 return 0;
383 if (match(opt, "-cread")) {
384 termios.c_cflag &= ~CREAD;
385 return 0;
388 if (match(opt, "cs5")) {
389 termios.c_cflag &= ~CSIZE;
390 termios.c_cflag |= CS5;
391 return 0;
393 if (match(opt, "cs6")) {
394 termios.c_cflag &= ~CSIZE;
395 termios.c_cflag |= CS6;
396 return 0;
398 if (match(opt, "cs7")) {
399 termios.c_cflag &= ~CSIZE;
400 termios.c_cflag |= CS7;
401 return 0;
403 if (match(opt, "cs8")) {
404 termios.c_cflag &= ~CSIZE;
405 termios.c_cflag |= CS8;
406 return 0;
409 if (match(opt, "cstopb")) {
410 termios.c_cflag |= CSTOPB;
411 return 0;
413 if (match(opt, "-cstopb")) {
414 termios.c_cflag &= ~CSTOPB;
415 return 0;
418 if (match(opt, "hupcl") || match(opt, "hup")) {
419 termios.c_cflag |= HUPCL;
420 return 0;
422 if (match(opt, "-hupcl") || match(opt, "-hup")) {
423 termios.c_cflag &= ~HUPCL;
424 return 0;
427 if (match(opt, "parenb")) {
428 termios.c_cflag |= PARENB;
429 return 0;
431 if (match(opt, "-parenb")) {
432 termios.c_cflag &= ~PARENB;
433 return 0;
436 if (match(opt, "parodd")) {
437 termios.c_cflag |= PARODD;
438 return 0;
440 if (match(opt, "-parodd")) {
441 termios.c_cflag &= ~PARODD;
442 return 0;
445 num= strtol(opt, &check, 10);
446 if (check[0] == '\0')
448 speed= long2speed(num);
449 if (speed == (speed_t)-1)
451 fprintf(stderr, "%s: illegal speed: '%s'\n", prog_name, opt);
452 return 0;
454 /* Speed OK */
455 cfsetispeed(&termios, speed);
456 cfsetospeed(&termios, speed);
457 return 0;
460 if (match(opt, "ispeed")) {
461 num= strtol(next, &check, 10);
462 if (check != '\0')
464 speed= long2speed(num);
465 if (speed == (speed_t)-1)
467 fprintf(stderr, "%s: illegal speed: '%s'\n", prog_name,
468 opt);
469 return 1;
471 cfsetispeed(&termios, speed);
472 return 1;
474 else
476 fprintf(stderr, "%s: invalid argument to ispeed: '%s'\n",
477 prog_name, next);
478 return 1;
482 if (match(opt, "ospeed")) {
483 num= strtol(next, &check, 10);
484 if (check != '\0')
486 speed= long2speed(num);
487 if (speed == (speed_t)-1)
489 fprintf(stderr, "%s: illegal speed: '%s'\n", prog_name,
490 opt);
491 return 1;
493 cfsetospeed(&termios, speed);
494 return 1;
496 else
498 fprintf(stderr, "%s: invalid argument to ospeed: %s\n",
499 prog_name, next);
500 return 1;
504 /* Input modes. */
506 if (match(opt, "brkint")) {
507 termios.c_iflag |= BRKINT;
508 return 0;
510 if (match(opt, "-brkint")) {
511 termios.c_iflag &= ~BRKINT;
512 return 0;
515 if (match(opt, "icrnl")) {
516 termios.c_iflag |= ICRNL;
517 return 0;
519 if (match(opt, "-icrnl")) {
520 termios.c_iflag &= ~ICRNL;
521 return 0;
524 if (match(opt, "ignbrk")) {
525 termios.c_iflag |= IGNBRK;
526 return 0;
528 if (match(opt, "-ignbrk")) {
529 termios.c_iflag &= ~IGNBRK;
530 return 0;
533 if (match(opt, "igncr")) {
534 termios.c_iflag |= IGNCR;
535 return 0;
537 if (match(opt, "-igncr")) {
538 termios.c_iflag &= ~IGNCR;
539 return 0;
542 if (match(opt, "ignpar")) {
543 termios.c_iflag |= IGNPAR;
544 return 0;
546 if (match(opt, "-ignpar")) {
547 termios.c_iflag &= ~IGNPAR;
548 return 0;
551 if (match(opt, "inlcr")) {
552 termios.c_iflag |= INLCR;
553 return 0;
555 if (match(opt, "-inlcr")) {
556 termios.c_iflag &= ~INLCR;
557 return 0;
560 if (match(opt, "inpck")) {
561 termios.c_iflag |= INPCK;
562 return 0;
564 if (match(opt, "-inpck")) {
565 termios.c_iflag &= ~INPCK;
566 return 0;
569 if (match(opt, "istrip")) {
570 termios.c_iflag |= ISTRIP;
571 return 0;
573 if (match(opt, "-istrip")) {
574 termios.c_iflag &= ~ISTRIP;
575 return 0;
578 if (match(opt, "ixoff")) {
579 termios.c_iflag |= IXOFF;
580 return 0;
582 if (match(opt, "-ixoff")) {
583 termios.c_iflag &= ~IXOFF;
584 return 0;
587 if (match(opt, "ixon")) {
588 termios.c_iflag |= IXON;
589 return 0;
591 if (match(opt, "-ixon")) {
592 termios.c_iflag &= ~IXON;
593 return 0;
596 if (match(opt, "parmrk")) {
597 termios.c_iflag |= PARMRK;
598 return 0;
600 if (match(opt, "-parmrk")) {
601 termios.c_iflag &= ~PARMRK;
602 return 0;
605 if (match(opt, "ixany")) {
606 termios.c_iflag |= IXANY;
607 return 0;
609 if (match(opt, "-ixany")) {
610 termios.c_iflag &= ~IXANY;
611 return 0;
614 /* Output modes. */
616 if (match(opt, "opost")) {
617 termios.c_oflag |= OPOST;
618 return 0;
620 if (match(opt, "-opost")) {
621 termios.c_oflag &= ~OPOST;
622 return 0;
624 #ifdef __minix
625 if (match(opt, "onlcr")) {
626 termios.c_oflag |= ONLCR;
627 return 0;
629 if (match(opt, "-onlcr")) {
630 termios.c_oflag &= ~ONLCR;
631 return 0;
634 if (match(opt, "xtabs")) {
635 termios.c_oflag |= XTABS;
636 return 0;
638 if (match(opt, "-xtabs")) {
639 termios.c_oflag &= ~XTABS;
640 return 0;
643 if (match(opt, "onoeot")) {
644 termios.c_oflag |= ONOEOT;
645 return 0;
647 if (match(opt, "-onoeot")) {
648 termios.c_oflag &= ~ONOEOT;
649 return 0;
651 #endif
653 /* Local modes. */
655 if (match(opt, "echo")) {
656 termios.c_lflag |= ECHO;
657 return 0;
659 if (match(opt, "-echo")) {
660 termios.c_lflag &= ~ECHO;
661 return 0;
664 if (match(opt, "echoe")) {
665 termios.c_lflag |= ECHOE;
666 return 0;
668 if (match(opt, "-echoe")) {
669 termios.c_lflag &= ~ECHOE;
670 return 0;
673 if (match(opt, "echok")) {
674 termios.c_lflag |= ECHOK;
675 return 0;
677 if (match(opt, "-echok")) {
678 termios.c_lflag &= ~ECHOK;
679 return 0;
682 if (match(opt, "echonl")) {
683 termios.c_lflag |= ECHONL;
684 return 0;
686 if (match(opt, "-echonl")) {
687 termios.c_lflag &= ~ECHONL;
688 return 0;
691 if (match(opt, "icanon")) {
692 termios.c_lflag |= ICANON;
693 return 0;
695 if (match(opt, "-icanon")) {
696 termios.c_lflag &= ~ICANON;
697 return 0;
700 if (match(opt, "iexten")) {
701 termios.c_lflag |= IEXTEN;
702 return 0;
704 if (match(opt, "-iexten")) {
705 termios.c_lflag &= ~IEXTEN;
706 return 0;
709 if (match(opt, "isig")) {
710 termios.c_lflag |= ISIG;
711 return 0;
713 if (match(opt, "-isig")) {
714 termios.c_lflag &= ~ISIG;
715 return 0;
718 if (match(opt, "noflsh")) {
719 termios.c_lflag |= NOFLSH;
720 return 0;
722 if (match(opt, "-noflsh")) {
723 termios.c_lflag &= ~NOFLSH;
724 return 0;
727 if (match(opt, "tostop")) {
728 termios.c_lflag |= TOSTOP;
729 return 0;
731 if (match(opt, "-tostop")) {
732 termios.c_lflag &= ~TOSTOP;
733 return 0;
736 #ifdef __minix
737 if (match(opt, "lflusho")) {
738 termios.c_lflag |= LFLUSHO;
739 return 0;
741 if (match(opt, "-lflusho")) {
742 termios.c_lflag &= ~LFLUSHO;
743 return 0;
745 #endif
747 /* The special control characters. */
748 if (match(opt, "eof")) {
749 set_control(VEOF, next);
750 return 1;
753 if (match(opt, "eol")) {
754 set_control(VEOL, next);
755 return 1;
758 if (match(opt, "erase")) {
759 set_control(VERASE, next);
760 return 1;
763 if (match(opt, "intr")) {
764 set_control(VINTR, next);
765 return 1;
768 if (match(opt, "kill")) {
769 set_control(VKILL, next);
770 return 1;
773 if (match(opt, "quit")) {
774 set_control(VQUIT, next);
775 return 1;
778 if (match(opt, "susp")) {
779 set_control(VSUSP, next);
780 return 1;
783 if (match(opt, "start")) {
784 set_control(VSTART, next);
785 return 1;
788 if (match(opt, "stop")) {
789 set_control(VSTOP, next);
790 return 1;
792 #ifdef __minix
793 if (match(opt, "rprnt")) {
794 set_control(VREPRINT, next);
795 return 1;
798 if (match(opt, "lnext")) {
799 set_control(VLNEXT, next);
800 return 1;
803 if (match(opt, "flush")) {
804 set_control(VDISCARD, next);
805 return 1;
807 #endif
809 if (match(opt, "min")) {
810 set_min_tim(VMIN, next);
811 return 1;
814 if (match(opt, "time")) {
815 set_min_tim(VTIME, next);
816 return 1;
819 /* Special modes. */
820 if (opt[0] == ':')
822 set_saved_settings(opt);
823 return 0;
826 if (match(opt, "cooked") || match(opt, "raw")) {
827 int x = opt[0] == 'c' ? 1 : 0;
829 option(x + "-icrnl", ""); /* off in raw mode, on in cooked mode */
830 option(x + "-ixon", "");
831 option(x + "-opost", "");
832 option(x + "-onlcr", "");
833 option(x + "-isig", "");
834 option(x + "-icanon", "");
835 option(x + "-iexten", "");
836 option(x + "-echo", "");
837 return 0;
840 if (match(opt, "evenp") || match(opt, "parity")) {
841 option("parenb", "");
842 option("cs7", "");
843 option("-parodd", "");
844 return 0;
847 if (match(opt, "oddp")) {
848 option("parenb", "");
849 option("cs7", "");
850 option("parodd", "");
851 return 0;
854 if (match(opt, "-parity") || match(opt, "-evenp") || match(opt, "-oddp")) {
855 option("-parenb", "");
856 option("cs8", "");
857 return 0;
860 if (match(opt, "nl")) {
861 option("icrnl", "");
862 return 0;
865 if (match(opt, "-nl")) {
866 option("-icrnl", "");
867 option("-inlcr", "");
868 option("-igncr", "");
869 return 0;
872 if (match(opt, "ek")) {
873 termios.c_cc[VERASE]= TERASE_DEF;;
874 termios.c_cc[VKILL]= TKILL_DEF;;
875 return 0;
878 if (match(opt, "sane"))
880 /* Reset all terminal attributes to a sane state, except things like
881 * line speed and parity, because it can't be known what their sane
882 * values are.
884 termios.c_iflag= (TINPUT_DEF & ~(IGNPAR|ISTRIP|INPCK))
885 | (termios.c_iflag & (IGNPAR|ISTRIP|INPCK));
886 termios.c_oflag= (TOUTPUT_DEF & ~(XTABS))
887 | (termios.c_oflag & (XTABS));
888 termios.c_cflag= (TCTRL_DEF & ~(CLOCAL|CSIZE|CSTOPB|PARENB|PARODD))
889 | (termios.c_cflag & (CLOCAL|CSIZE|CSTOPB|PARENB|PARODD));
890 termios.c_lflag= (TLOCAL_DEF & ~(ECHOE|ECHOK))
891 | (termios.c_lflag & (ECHOE|ECHOK));
892 if (termios.c_lflag & ICANON) {
893 termios.c_cc[VMIN]= TMIN_DEF;
894 termios.c_cc[VTIME]= TTIME_DEF;
896 termios.c_cc[VEOF]= TEOF_DEF;
897 termios.c_cc[VEOL]= TEOL_DEF;
898 termios.c_cc[VERASE]= TERASE_DEF;
899 termios.c_cc[VINTR]= TINTR_DEF;
900 termios.c_cc[VKILL]= TKILL_DEF;
901 termios.c_cc[VQUIT]= TQUIT_DEF;
902 termios.c_cc[VSUSP]= TSUSP_DEF;
903 #ifdef __minix
904 termios.c_cc[VREPRINT]= TREPRINT_DEF;
905 termios.c_cc[VLNEXT]= TLNEXT_DEF;
906 termios.c_cc[VDISCARD]= TDISCARD_DEF;
907 #endif
908 termios.c_cc[VSTART]= TSTART_DEF;
909 termios.c_cc[VSTOP]= TSTOP_DEF;
910 if (!(termios.c_lflag & ICANON)) {
911 termios.c_cc[VMIN]= TMIN_DEF;
912 termios.c_cc[VTIME]= TTIME_DEF;
914 return 0;
917 #ifdef __minix
918 if (match(opt, "cols"))
920 num= strtol(next, &check, 0);
921 if (check[0] != '\0')
923 fprintf(stderr, "%s: illegal parameter to cols: '%s'\n",
924 prog_name, next);
925 return 1;
927 winsize.ws_col= num;
928 return 1;
931 if (match(opt, "rows"))
933 num= strtol(next, &check, 0);
934 if (check[0] != '\0')
936 fprintf(stderr, "%s: illegal parameter to rows: '%s'\n",
937 prog_name, next);
938 return 1;
940 winsize.ws_row= num;
941 return 1;
944 if (match(opt, "xpixels"))
946 num= strtol(next, &check, 0);
947 if (check[0] != '\0')
949 fprintf(stderr, "%s: illegal parameter to xpixels: '%s'\n",
950 prog_name, next);
951 return 1;
953 winsize.ws_xpixel= num;
954 return 1;
957 if (match(opt, "ypixels"))
959 num= strtol(next, &check, 0);
960 if (check[0] != '\0')
962 fprintf(stderr, "%s: illegal parameter to ypixels: '%s'\n",
963 prog_name, next);
964 return 1;
966 winsize.ws_ypixel= num;
967 return 1;
969 #endif /* __minix */
971 fprintf(stderr, "%s: unknown mode: %s\n", prog_name, opt);
972 return 0;
975 int match(s1, s2)
976 char *s1, *s2;
979 while (1) {
980 if (*s1 == 0 && *s2 == 0) return(1);
981 if (*s1 == 0 || *s2 == 0) return (0);
982 if (*s1 != *s2) return (0);
983 s1++;
984 s2++;
988 void prctl(c)
989 char c;
991 if (c < ' ')
992 printf("^%c", 'A' + c - 1);
993 else if (c == 0177)
994 printf("^?");
995 else
996 printf("%c", c);
999 struct s2s {
1000 speed_t ts;
1001 long ns;
1002 } s2s[] = {
1003 { B0, 0 },
1004 { B50, 50 },
1005 { B75, 75 },
1006 { B110, 110 },
1007 { B134, 134 },
1008 { B150, 150 },
1009 { B200, 200 },
1010 { B300, 300 },
1011 { B600, 600 },
1012 { B1200, 1200 },
1013 { B1800, 1800 },
1014 { B2400, 2400 },
1015 { B4800, 4800 },
1016 { B9600, 9600 },
1017 { B19200, 19200 },
1018 { B38400, 38400 },
1019 #ifdef __minix
1020 { B57600, 57600 },
1021 { B115200, 115200 },
1022 #ifdef B230400
1023 { B230400, 230400 },
1024 #endif
1025 #ifdef B460800
1026 { B460800, 460800 },
1027 #endif
1028 #ifdef B921600
1029 { B921600, 921600 },
1030 #endif
1031 #endif
1034 speed_t long2speed(num)
1035 long num;
1037 struct s2s *sp;
1039 for (sp = s2s; sp < s2s + (sizeof(s2s) / sizeof(s2s[0])); sp++) {
1040 if (sp->ns == num) return sp->ts;
1042 return -1;
1045 long speed2long(speed)
1046 unsigned long speed;
1048 struct s2s *sp;
1050 for (sp = s2s; sp < s2s + (sizeof(s2s) / sizeof(s2s[0])); sp++) {
1051 if (sp->ts == speed) return sp->ns;
1053 return -1;
1056 void print_flags(flags, flag, def, string, all)
1057 unsigned long flags;
1058 unsigned long flag;
1059 unsigned long def;
1060 char *string;
1061 int all;
1063 if (!(flags & flag))
1065 if (all || (def & flag))
1066 output(string);
1067 return;
1069 string++;
1070 if (all || !(def & flag))
1071 output(string);
1074 void output(s)
1075 char *s;
1077 int len;
1079 len= strlen(s);
1080 if (column + len + 3 >= max_column)
1082 printf("\n");
1083 column= 0;
1085 if (column)
1087 putchar(' ');
1088 column++;
1090 fputs(s, stdout);
1091 column += len;
1094 void do_print_char(chr, def, name, all)
1095 unsigned chr;
1096 unsigned def;
1097 char *name;
1098 int all;
1100 char line[20];
1102 if (!all && chr == def)
1103 return;
1105 #ifdef _POSIX_VDISABLE
1106 if (chr == _POSIX_VDISABLE)
1107 sprintf(line, "%s = <undef>", name);
1108 else
1109 #endif
1110 if (chr < ' ')
1111 sprintf(line, "%s = ^%c", name, chr + '@');
1112 else if (chr == 127)
1113 sprintf(line, "%s = ^?", name);
1114 else
1115 sprintf(line, "%s = %c", name, chr);
1116 output(line);
1119 void do_print_num(num, def, name, all)
1120 unsigned num;
1121 unsigned def;
1122 char *name;
1123 int all;
1125 char line[20];
1127 if (!all && num == def)
1128 return;
1129 sprintf(line, "%s = %u", name, num);
1130 output(line);
1133 void set_saved_settings(opt)
1134 char *opt;
1136 long num;
1137 char *check;
1138 tcflag_t c_oflag, c_cflag, c_lflag, c_iflag;
1139 cc_t c_cc[NCCS];
1140 speed_t ispeed, ospeed;
1141 int i;
1143 check= opt;
1144 num= strtol(check+1, &check, 16);
1145 if (check[0] != ':')
1147 fprintf(stderr, "error in saved settings '%s'\n", opt);
1148 return;
1150 c_iflag= num;
1152 num= strtol(check+1, &check, 16);
1153 if (check[0] != ':')
1155 fprintf(stderr, "error in saved settings '%s'\n", opt);
1156 return;
1158 c_oflag= num;
1160 num= strtol(check+1, &check, 16);
1161 if (check[0] != ':')
1163 fprintf(stderr, "error in saved settings '%s'\n", opt);
1164 return;
1166 c_cflag= num;
1168 num= strtol(check+1, &check, 16);
1169 if (check[0] != ':')
1171 fprintf(stderr, "error in saved settings '%s'\n", opt);
1172 return;
1174 c_lflag= num;
1176 num= strtol(check+1, &check, 16);
1177 if (check[0] != ':')
1179 fprintf(stderr, "error in saved settings '%s'\n", opt);
1180 return;
1182 ispeed= num;
1184 num= strtol(check+1, &check, 16);
1185 if (check[0] != ':')
1187 fprintf(stderr, "error in saved settings '%s'\n", opt);
1188 return;
1190 ospeed= num;
1192 for(i=0; i<NCCS; i++)
1194 num= strtol(check+1, &check, 16);
1195 if (check[0] != ':')
1197 fprintf(stderr, "error in saved settings '%s'\n", opt);
1198 return;
1200 c_cc[i]= num;
1202 if (check[1] != '\0')
1204 fprintf(stderr, "error in saved settings '%s'\n", opt);
1205 return;
1207 termios.c_iflag= c_iflag;
1208 termios.c_oflag= c_oflag;
1209 termios.c_cflag= c_cflag;
1210 termios.c_lflag= c_lflag;
1212 cfsetispeed(&termios, ispeed);
1213 cfsetospeed(&termios, ospeed);
1215 for(i=0; i<NCCS; i++)
1216 termios.c_cc[i]= c_cc[i];
1219 void set_control(option, value)
1220 int option;
1221 char *value;
1223 int chr;
1225 if (match(value, "undef") || match(value, "^-")) {
1226 #ifdef _POSIX_VDISABLE
1227 chr= _POSIX_VDISABLE;
1228 #else
1229 fprintf(stderr,
1230 "stty: unable to set option to _POSIX_VDISABLE\n");
1231 return;
1232 #endif
1233 } else if (match(value, "^?"))
1234 chr= '\177';
1235 else if (strlen(value) == 2 && value[0] == '^') {
1236 chr= toupper(value[1]) - '@';
1237 if (chr < 0 || chr >= 32) {
1238 fprintf(stderr, "stty: illegal option value: '%s'\n",
1239 value);
1240 return;
1242 } else if (strlen(value) == 1)
1243 chr= value[0];
1244 else {
1245 fprintf(stderr, "stty: illegal option value: '%s'\n", value);
1246 return;
1249 assert(option >= 0 && option < NCCS);
1250 termios.c_cc[option]= chr;
1253 void set_min_tim(option, value)
1254 int option;
1255 char *value;
1257 long num;
1258 char *check;
1260 num= strtol(value, &check, 0);
1261 if (check[0] != '\0') {
1262 fprintf(stderr, "stty: illegal option value: '%s'\n", value);
1263 return;
1266 if ((cc_t)num != num) {
1267 fprintf(stderr, "stty: illegal option value: '%s'\n", value);
1268 return;
1270 assert(option >= 0 && option < NCCS);
1271 termios.c_cc[option]= num;
1275 * $PchId: stty.c,v 1.7 2001/05/02 15:04:42 philip Exp $