📄 stty.c
字号:
/* stty - set terminal mode Author: Andy Tanenbaum *//* Adapted to POSIX 1003.2 by Philip Homburg. */#ifdef __minix_vmd#define _MINIX_SOURCE#endif#include <assert.h>#include <ctype.h>#include <errno.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <termios.h>#ifdef __minix#include <sys/types.h>#include <sys/ioctl.h>#endif/* Default settings, the Minix ones are defined in <termios.h> */#ifndef TCTRL_DEF#define TCTRL_DEF (PARENB | CREAD | CS7)#endif#ifndef TSPEED_DEF#define TSPEED_DEF B1200#endif#ifndef TINPUT_DEF#define TINPUT_DEF (BRKINT | IGNPAR | ISTRIP | ICRNL)#endif#ifndef TOUTPUT_DEF#define TOUTPUT_DEF OPOST#endif#ifndef TLOCAL_DEF#define TLOCAL_DEF (ISIG | IEXTEN | ICANON | ECHO | ECHOE)#endif#ifndef TEOF_DEF#define TEOF_DEF '\4' /* ^D */#endif#ifndef TEOL_DEF#ifdef _POSIX_VDISABLE#define TEOL_DEF _POSIX_VDISABLE#else#define TEOL_DEF '\n'#endif#endif#ifndef TERASE_DEF#define TERASE_DEF '\10' /* ^H */#endif#ifndef TINTR_DEF#define TINTR_DEF '\177' /* ^? */#endif#ifndef TKILL_DEF#define TKILL_DEF '\25' /* ^U */#endif#ifndef TQUIT_DEF#define TQUIT_DEF '\34' /* ^\ */#endif#ifndef TSUSP_DEF#define TSUSP_DEF '\32' /* ^Z */#endif#ifndef TSTART_DEF#define TSTART_DEF '\21' /* ^Q */#endif#ifndef TSTOP_DEF#define TSTOP_DEF '\23' /* ^S */#endif#ifndef TMIN_DEF#define TMIN_DEF 1#endif#ifndef TTIME_DEF#define TTIME_DEF 0#endifchar *prog_name;struct termios termios;int column= 0, max_column=80; /* Assume 80 character terminals. */#ifdef __minixstruct winsize winsize;#endif#if __minix#define PROTO(a) _ARGS(a)#else#define PROTO(a) ()#endifvoid main PROTO(( int argc, char **argv ));void report PROTO(( int flags ));int option PROTO(( char *opt, char *next ));int match PROTO(( char *s1, char *s2 ));void prctl PROTO(( int c ));speed_t long2speed PROTO(( long num ));long speed2long PROTO(( unsigned long speed ));void print_flags PROTO(( unsigned long flags, unsigned long flag, unsigned long def, char *string, int all ));void output PROTO(( char *s ));void do_print_char PROTO(( unsigned chr, unsigned def, char *name, int all ));void do_print_num PROTO(( unsigned num, unsigned def, char *name, int all ));void set_saved_settings PROTO(( char *opt ));void set_control PROTO(( int option, char *value ));void set_min_tim PROTO(( int option, char *value ));#define print_char(c,d,n,a) (do_print_char((unsigned)(c),(unsigned)(d),(n),(a)))#define print_num(m,d,n,a) (do_print_num((unsigned)(m),(unsigned)(d),(n),(a)))void main(argc, argv)int argc;char *argv[];{ int flags, k; prog_name= argv[0]; flags= 0; /* Stty with no arguments just reports on current status. */ if (tcgetattr(0, &termios) == -1) { fprintf(stderr, "%s: can't read ioctl parameters from stdin: %s\n", prog_name, strerror(errno)); exit(1); }#ifdef __minix if (ioctl(0, TIOCGWINSZ, &winsize) == -1) { fprintf(stderr, "%s: can't get screen size from stdin: %s\n", prog_name, strerror(errno)); exit(1); } if (winsize.ws_col != 0) max_column= winsize.ws_col;#endif if (argc == 2) { if (!strcmp(argv[1], "-a")) flags |= 1; else if (!strcmp(argv[1], "-g")) flags |= 2; } if (argc == 1 || flags) { report(flags); exit(0); } /* Process the options specified. */ for (k= 1; k < argc; k++) k += option(argv[k], k+1 < argc ? argv[k+1] : ""); if (tcsetattr(0, TCSANOW, &termios) == -1) { fprintf(stderr, "%s: can't set terminal parameters to stdin: %s\n", prog_name, strerror(errno)); exit(1); }#ifdef __minix if (ioctl(0, TIOCSWINSZ, &winsize) == -1) { fprintf(stderr, "%s: can't set screen size to stdin: %s\n", prog_name, strerror(errno)); exit(1); }#endif exit(0);}void report(flags)int flags;{ int i, all; tcflag_t c_cflag, c_iflag, c_oflag, c_lflag; char line[80]; speed_t ispeed, ospeed; if (flags & 2) { /* We have to write the termios structure in a encoded form * to stdout. */ printf(":%x:%x:%x:%x:%x:%x", termios.c_iflag, termios.c_oflag, termios.c_cflag, termios.c_lflag, cfgetispeed(&termios), cfgetospeed(&termios)); for (i= 0; i<NCCS; i++) printf(":%x", termios.c_cc[i]); printf(":\n"); return; } all= !!flags; /* Start with the baud rate. */ ispeed= cfgetispeed(&termios); ospeed= cfgetospeed(&termios); if (ispeed != ospeed) { sprintf(line, "ispeed %lu baud; ospeed %lu baud;", speed2long(ispeed), speed2long(ospeed)); output(line); } else if (all || ospeed != TSPEED_DEF) { sprintf(line, "speed %lu baud;", speed2long(ospeed)); output(line); } /* The control modes. */ c_cflag= termios.c_cflag; if (all || (c_cflag & CSIZE) != (TCTRL_DEF & CSIZE)) { switch (c_cflag & CSIZE) { case CS5: output("cs5"); break; case CS6: output("cs6"); break; case CS7: output("cs7"); break; case CS8: output("cs8"); break; default: output("cs??"); break; } } print_flags(c_cflag, PARENB, TCTRL_DEF, "-parenb", all); print_flags(c_cflag, PARODD, TCTRL_DEF, "-parodd", all); print_flags(c_cflag, HUPCL, TCTRL_DEF, "-hupcl", all); print_flags(c_cflag, CSTOPB, TCTRL_DEF, "-cstopb", all); print_flags(c_cflag, CREAD, TCTRL_DEF, "-cread", all); print_flags(c_cflag, CLOCAL, TCTRL_DEF, "-clocal", all); if (all) { printf("\n"); column= 0; } /* The input flags. */ c_iflag= termios.c_iflag; print_flags(c_iflag, IGNBRK, TINPUT_DEF, "-ignbrk", all); print_flags(c_iflag, BRKINT, TINPUT_DEF, "-brkint", all); print_flags(c_iflag, IGNPAR, TINPUT_DEF, "-ignpar", all); print_flags(c_iflag, PARMRK, TINPUT_DEF, "-parmrk", all); print_flags(c_iflag, INPCK, TINPUT_DEF, "-inpck", all); print_flags(c_iflag, ISTRIP, TINPUT_DEF, "-istrip", all); print_flags(c_iflag, INLCR, TINPUT_DEF, "-inlcr", all); print_flags(c_iflag, IGNCR, TINPUT_DEF, "-igncr", all); print_flags(c_iflag, ICRNL, TINPUT_DEF, "-icrnl", all); print_flags(c_iflag, IXON, TINPUT_DEF, "-ixon", all); print_flags(c_iflag, IXOFF, TINPUT_DEF, "-ixoff", all); print_flags(c_iflag, IXANY, TINPUT_DEF, "-ixany", all); if (all) { printf("\n"); column= 0; } /* The output flags. */ c_oflag= termios.c_oflag; print_flags(c_oflag, OPOST, TOUTPUT_DEF, "-opost", all);#ifdef __minix print_flags(c_oflag, ONLCR, TOUTPUT_DEF, "-onlcr", all); print_flags(c_oflag, XTABS, TOUTPUT_DEF, "-xtabs", all); print_flags(c_oflag, ONOEOT, TOUTPUT_DEF, "-onoeot", all);#endif if (all) { printf("\n"); column= 0; } /* The local flags. */ c_lflag= termios.c_lflag; print_flags(c_lflag, ISIG, TLOCAL_DEF, "-isig", all); print_flags(c_lflag, ICANON, TLOCAL_DEF, "-icanon", all); print_flags(c_lflag, IEXTEN, TLOCAL_DEF, "-iexten", all); print_flags(c_lflag, ECHO, TLOCAL_DEF, "-echo", all); print_flags(c_lflag, ECHOE, TLOCAL_DEF, "-echoe", all); print_flags(c_lflag, ECHOK, TLOCAL_DEF, "-echok", all); print_flags(c_lflag, ECHONL, TLOCAL_DEF, "-echonl", all); print_flags(c_lflag, NOFLSH, TLOCAL_DEF, "-noflsh", all);#ifdef __minix print_flags(c_lflag, LFLUSHO, TLOCAL_DEF, "-lflusho", all);#endif if (all) { printf("\n"); column= 0; } /* The special control characters. */ print_char(termios.c_cc[VEOF], TEOF_DEF, "eof", all); print_char(termios.c_cc[VEOL], TEOL_DEF, "eol", all); print_char(termios.c_cc[VERASE], TERASE_DEF, "erase", all); print_char(termios.c_cc[VINTR], TINTR_DEF, "intr", all); print_char(termios.c_cc[VKILL], TKILL_DEF, "kill", all); print_char(termios.c_cc[VQUIT], TQUIT_DEF, "quit", all); print_char(termios.c_cc[VSUSP], TSUSP_DEF, "susp", all); print_char(termios.c_cc[VSTART], TSTART_DEF, "start", all); print_char(termios.c_cc[VSTOP], TSTOP_DEF, "stop", all);#ifdef __minix print_char(termios.c_cc[VREPRINT], TREPRINT_DEF, "rprnt", all); print_char(termios.c_cc[VLNEXT], TLNEXT_DEF, "lnext", all); print_char(termios.c_cc[VDISCARD], TDISCARD_DEF, "flush", all);#endif print_num(termios.c_cc[VMIN], TMIN_DEF, "min", all); print_num(termios.c_cc[VTIME], TTIME_DEF, "time", all); if (all) { printf("\n"); column= 0; }#ifdef __minix /* Screen size */ if (all || winsize.ws_row != 0 || winsize.ws_col != 0) { sprintf(line, "%d rows %d columns", winsize.ws_row, winsize.ws_col); output(line); } if (all) { printf("\n"); column= 0; }#endif if (column != 0) { printf("\n"); column= 0; }}int option(opt, next)char *opt, *next;{ char *check; speed_t speed; long num; /* The control options. */ if (match(opt, "clocal")) { termios.c_cflag |= CLOCAL; return 0; } if (match(opt, "-clocal")) { termios.c_cflag &= ~CLOCAL; return 0; } if (match(opt, "cread")) { termios.c_cflag |= CREAD; return 0; } if (match(opt, "-cread")) { termios.c_cflag &= ~CREAD; return 0; } if (match(opt, "cs5")) { termios.c_cflag &= ~CSIZE; termios.c_cflag |= CS5; return 0; } if (match(opt, "cs6")) { termios.c_cflag &= ~CSIZE; termios.c_cflag |= CS6; return 0; } if (match(opt, "cs7")) { termios.c_cflag &= ~CSIZE; termios.c_cflag |= CS7; return 0; } if (match(opt, "cs8")) { termios.c_cflag &= ~CSIZE; termios.c_cflag |= CS8; return 0; } if (match(opt, "cstopb")) { termios.c_cflag |= CSTOPB; return 0; } if (match(opt, "-cstopb")) { termios.c_cflag &= ~CSTOPB; return 0; } if (match(opt, "hupcl") || match(opt, "hup")) { termios.c_cflag |= HUPCL; return 0; } if (match(opt, "-hupcl") || match(opt, "-hup")) { termios.c_cflag &= ~HUPCL; return 0; } if (match(opt, "parenb")) { termios.c_cflag |= PARENB; return 0; } if (match(opt, "-parenb")) { termios.c_cflag &= ~PARENB; return 0; } if (match(opt, "parodd")) { termios.c_cflag |= PARODD; return 0; } if (match(opt, "-parodd")) { termios.c_cflag &= ~PARODD; return 0; } num= strtol(opt, &check, 10); if (check[0] == '\0') { speed= long2speed(num); if (speed == (speed_t)-1) { fprintf(stderr, "%s: illegal speed: '%s'\n", prog_name, opt); return 0; } /* Speed OK */ cfsetispeed(&termios, speed); cfsetospeed(&termios, speed); return 0; } if (match(opt, "ispeed")) { num= strtol(next, &check, 10); if (check != '\0') { speed= long2speed(num); if (speed == (speed_t)-1) { fprintf(stderr, "%s: illegal speed: '%s'\n", prog_name, opt); return 1; } cfsetispeed(&termios, speed); return 1; } else { fprintf(stderr, "%s: invalid argument to ispeed: '%s'\n", prog_name, next); return 1; } } if (match(opt, "ospeed")) { num= strtol(next, &check, 10); if (check != '\0') { speed= long2speed(num); if (speed == (speed_t)-1) { fprintf(stderr, "%s: illegal speed: '%s'\n", prog_name, opt); return 1; } cfsetospeed(&termios, speed); return 1; } else { fprintf(stderr, "%s: invalid argument to ospeed: %s\n", prog_name, next); return 1; } } /* Input modes. */ if (match(opt, "brkint")) { termios.c_iflag |= BRKINT; return 0; } if (match(opt, "-brkint")) { termios.c_iflag &= ~BRKINT; return 0; } if (match(opt, "icrnl")) { termios.c_iflag |= ICRNL; return 0; } if (match(opt, "-icrnl")) { termios.c_iflag &= ~ICRNL; return 0; } if (match(opt, "ignbrk")) { termios.c_iflag |= IGNBRK; return 0; } if (match(opt, "-ignbrk")) { termios.c_iflag &= ~IGNBRK; return 0; } if (match(opt, "igncr")) { termios.c_iflag |= IGNCR; return 0; } if (match(opt, "-igncr")) { termios.c_iflag &= ~IGNCR; return 0; } if (match(opt, "ignpar")) { termios.c_iflag |= IGNPAR; return 0; } if (match(opt, "-ignpar")) { termios.c_iflag &= ~IGNPAR; return 0; } if (match(opt, "inlcr")) { termios.c_iflag |= INLCR; return 0; } if (match(opt, "-inlcr")) { termios.c_iflag &= ~INLCR; return 0; } if (match(opt, "inpck")) { termios.c_iflag |= INPCK; return 0; } if (match(opt, "-inpck")) { termios.c_iflag &= ~INPCK; return 0; } if (match(opt, "istrip")) { termios.c_iflag |= ISTRIP; return 0; } if (match(opt, "-istrip")) { termios.c_iflag &= ~ISTRIP; return 0; } if (match(opt, "ixoff")) { termios.c_iflag |= IXOFF; return 0; } if (match(opt, "-ixoff")) { termios.c_iflag &= ~IXOFF; return 0; } if (match(opt, "ixon")) { termios.c_iflag |= IXON; return 0; } if (match(opt, "-ixon")) { termios.c_iflag &= ~IXON; return 0; } if (match(opt, "parmrk")) { termios.c_iflag |= PARMRK; return 0; } if (match(opt, "-parmrk")) { termios.c_iflag &= ~PARMRK; return 0; } if (match(opt, "ixany")) { termios.c_iflag |= IXANY; return 0; } if (match(opt, "-ixany")) { termios.c_iflag &= ~IXANY; return 0; } /* Output modes. */ if (match(opt, "opost")) { termios.c_oflag |= OPOST; return 0; } if (match(opt, "-opost")) { termios.c_oflag &= ~OPOST; return 0; }#ifdef __minix
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -