diff options
Diffstat (limited to 'coreutils/stty.c')
-rw-r--r-- | coreutils/stty.c | 493 |
1 files changed, 206 insertions, 287 deletions
diff --git a/coreutils/stty.c b/coreutils/stty.c index a67a17c..a3a98d9 100644 --- a/coreutils/stty.c +++ b/coreutils/stty.c @@ -30,9 +30,9 @@ //#define TEST +#include <stddef.h> #include <termios.h> #include <sys/ioctl.h> -#include <getopt.h> #include <sys/param.h> #include <unistd.h> @@ -155,13 +155,10 @@ enum speed_setting { input_speed, output_speed, both_speeds }; -/* What to output and how. */ -enum output_type { - changed, all, recoverable /* Default, -a, -g. */ -}; - /* Which member(s) of `struct termios' a mode uses. */ enum mode_type { + /* Do NOT change the order or values, as mode_type_flag() + * depends on them. */ control, input, output, local, combination }; @@ -199,156 +196,159 @@ static const char stty_dec [] = "dec"; /* Each mode. */ struct mode_info { const char *name; /* Name given on command line. */ - enum mode_type type; /* Which structure element to change. */ + /* enum mode_type type; */ + char type; /* Which structure element to change. */ char flags; /* Setting and display options. */ + unsigned short mask; /* Other bits to turn off for this mode. */ unsigned long bits; /* Bits to set for this mode. */ - unsigned long mask; /* Other bits to turn off for this mode. */ }; +#define MI_ENTRY(N,T,F,B,M) { N, T, F, M, B } + static const struct mode_info mode_info[] = { - {"parenb", control, REV, PARENB, 0 }, - {"parodd", control, REV, PARODD, 0 }, - {"cs5", control, 0, CS5, CSIZE}, - {"cs6", control, 0, CS6, CSIZE}, - {"cs7", control, 0, CS7, CSIZE}, - {"cs8", control, 0, CS8, CSIZE}, - {"hupcl", control, REV, HUPCL, 0 }, - {"hup", control, REV | OMIT, HUPCL, 0 }, - {"cstopb", control, REV, CSTOPB, 0 }, - {"cread", control, SANE_SET | REV, CREAD, 0 }, - {"clocal", control, REV, CLOCAL, 0 }, + MI_ENTRY("parenb", control, REV, PARENB, 0 ), + MI_ENTRY("parodd", control, REV, PARODD, 0 ), + MI_ENTRY("cs5", control, 0, CS5, CSIZE), + MI_ENTRY("cs6", control, 0, CS6, CSIZE), + MI_ENTRY("cs7", control, 0, CS7, CSIZE), + MI_ENTRY("cs8", control, 0, CS8, CSIZE), + MI_ENTRY("hupcl", control, REV, HUPCL, 0 ), + MI_ENTRY("hup", control, REV | OMIT, HUPCL, 0 ), + MI_ENTRY("cstopb", control, REV, CSTOPB, 0 ), + MI_ENTRY("cread", control, SANE_SET | REV, CREAD, 0 ), + MI_ENTRY("clocal", control, REV, CLOCAL, 0 ), #ifdef CRTSCTS - {"crtscts", control, REV, CRTSCTS, 0 }, -#endif - {"ignbrk", input, SANE_UNSET | REV, IGNBRK, 0 }, - {"brkint", input, SANE_SET | REV, BRKINT, 0 }, - {"ignpar", input, REV, IGNPAR, 0 }, - {"parmrk", input, REV, PARMRK, 0 }, - {"inpck", input, REV, INPCK, 0 }, - {"istrip", input, REV, ISTRIP, 0 }, - {"inlcr", input, SANE_UNSET | REV, INLCR, 0 }, - {"igncr", input, SANE_UNSET | REV, IGNCR, 0 }, - {"icrnl", input, SANE_SET | REV, ICRNL, 0 }, - {"ixon", input, REV, IXON, 0 }, - {"ixoff", input, SANE_UNSET | REV, IXOFF, 0 }, - {"tandem", input, REV | OMIT, IXOFF, 0 }, + MI_ENTRY("crtscts", control, REV, CRTSCTS, 0 ), +#endif + MI_ENTRY("ignbrk", input, SANE_UNSET | REV, IGNBRK, 0 ), + MI_ENTRY("brkint", input, SANE_SET | REV, BRKINT, 0 ), + MI_ENTRY("ignpar", input, REV, IGNPAR, 0 ), + MI_ENTRY("parmrk", input, REV, PARMRK, 0 ), + MI_ENTRY("inpck", input, REV, INPCK, 0 ), + MI_ENTRY("istrip", input, REV, ISTRIP, 0 ), + MI_ENTRY("inlcr", input, SANE_UNSET | REV, INLCR, 0 ), + MI_ENTRY("igncr", input, SANE_UNSET | REV, IGNCR, 0 ), + MI_ENTRY("icrnl", input, SANE_SET | REV, ICRNL, 0 ), + MI_ENTRY("ixon", input, REV, IXON, 0 ), + MI_ENTRY("ixoff", input, SANE_UNSET | REV, IXOFF, 0 ), + MI_ENTRY("tandem", input, REV | OMIT, IXOFF, 0 ), #ifdef IUCLC - {"iuclc", input, SANE_UNSET | REV, IUCLC, 0 }, + MI_ENTRY("iuclc", input, SANE_UNSET | REV, IUCLC, 0 ), #endif #ifdef IXANY - {"ixany", input, SANE_UNSET | REV, IXANY, 0 }, + MI_ENTRY("ixany", input, SANE_UNSET | REV, IXANY, 0 ), #endif #ifdef IMAXBEL - {"imaxbel", input, SANE_SET | REV, IMAXBEL, 0 }, + MI_ENTRY("imaxbel", input, SANE_SET | REV, IMAXBEL, 0 ), #endif - {"opost", output, SANE_SET | REV, OPOST, 0 }, + MI_ENTRY("opost", output, SANE_SET | REV, OPOST, 0 ), #ifdef OLCUC - {"olcuc", output, SANE_UNSET | REV, OLCUC, 0 }, + MI_ENTRY("olcuc", output, SANE_UNSET | REV, OLCUC, 0 ), #endif #ifdef OCRNL - {"ocrnl", output, SANE_UNSET | REV, OCRNL, 0 }, + MI_ENTRY("ocrnl", output, SANE_UNSET | REV, OCRNL, 0 ), #endif #ifdef ONLCR - {"onlcr", output, SANE_SET | REV, ONLCR, 0 }, + MI_ENTRY("onlcr", output, SANE_SET | REV, ONLCR, 0 ), #endif #ifdef ONOCR - {"onocr", output, SANE_UNSET | REV, ONOCR, 0 }, + MI_ENTRY("onocr", output, SANE_UNSET | REV, ONOCR, 0 ), #endif #ifdef ONLRET - {"onlret", output, SANE_UNSET | REV, ONLRET, 0 }, + MI_ENTRY("onlret", output, SANE_UNSET | REV, ONLRET, 0 ), #endif #ifdef OFILL - {"ofill", output, SANE_UNSET | REV, OFILL, 0 }, + MI_ENTRY("ofill", output, SANE_UNSET | REV, OFILL, 0 ), #endif #ifdef OFDEL - {"ofdel", output, SANE_UNSET | REV, OFDEL, 0 }, + MI_ENTRY("ofdel", output, SANE_UNSET | REV, OFDEL, 0 ), #endif #ifdef NLDLY - {"nl1", output, SANE_UNSET, NL1, NLDLY}, - {"nl0", output, SANE_SET, NL0, NLDLY}, + MI_ENTRY("nl1", output, SANE_UNSET, NL1, NLDLY), + MI_ENTRY("nl0", output, SANE_SET, NL0, NLDLY), #endif #ifdef CRDLY - {"cr3", output, SANE_UNSET, CR3, CRDLY}, - {"cr2", output, SANE_UNSET, CR2, CRDLY}, - {"cr1", output, SANE_UNSET, CR1, CRDLY}, - {"cr0", output, SANE_SET, CR0, CRDLY}, + MI_ENTRY("cr3", output, SANE_UNSET, CR3, CRDLY), + MI_ENTRY("cr2", output, SANE_UNSET, CR2, CRDLY), + MI_ENTRY("cr1", output, SANE_UNSET, CR1, CRDLY), + MI_ENTRY("cr0", output, SANE_SET, CR0, CRDLY), #endif #ifdef TABDLY - {"tab3", output, SANE_UNSET, TAB3, TABDLY}, - {"tab2", output, SANE_UNSET, TAB2, TABDLY}, - {"tab1", output, SANE_UNSET, TAB1, TABDLY}, - {"tab0", output, SANE_SET, TAB0, TABDLY}, + MI_ENTRY("tab3", output, SANE_UNSET, TAB3, TABDLY), + MI_ENTRY("tab2", output, SANE_UNSET, TAB2, TABDLY), + MI_ENTRY("tab1", output, SANE_UNSET, TAB1, TABDLY), + MI_ENTRY("tab0", output, SANE_SET, TAB0, TABDLY), #else # ifdef OXTABS - {"tab3", output, SANE_UNSET, OXTABS, 0 }, + MI_ENTRY("tab3", output, SANE_UNSET, OXTABS, 0 ), # endif #endif #ifdef BSDLY - {"bs1", output, SANE_UNSET, BS1, BSDLY}, - {"bs0", output, SANE_SET, BS0, BSDLY}, + MI_ENTRY("bs1", output, SANE_UNSET, BS1, BSDLY), + MI_ENTRY("bs0", output, SANE_SET, BS0, BSDLY), #endif #ifdef VTDLY - {"vt1", output, SANE_UNSET, VT1, VTDLY}, - {"vt0", output, SANE_SET, VT0, VTDLY}, + MI_ENTRY("vt1", output, SANE_UNSET, VT1, VTDLY), + MI_ENTRY("vt0", output, SANE_SET, VT0, VTDLY), #endif #ifdef FFDLY - {"ff1", output, SANE_UNSET, FF1, FFDLY}, - {"ff0", output, SANE_SET, FF0, FFDLY}, + MI_ENTRY("ff1", output, SANE_UNSET, FF1, FFDLY), + MI_ENTRY("ff0", output, SANE_SET, FF0, FFDLY), #endif - {"isig", local, SANE_SET | REV, ISIG, 0 }, - {"icanon", local, SANE_SET | REV, ICANON, 0 }, + MI_ENTRY("isig", local, SANE_SET | REV, ISIG, 0 ), + MI_ENTRY("icanon", local, SANE_SET | REV, ICANON, 0 ), #ifdef IEXTEN - {"iexten", local, SANE_SET | REV, IEXTEN, 0 }, -#endif - {"echo", local, SANE_SET | REV, ECHO, 0 }, - {"echoe", local, SANE_SET | REV, ECHOE, 0 }, - {"crterase", local, REV | OMIT, ECHOE, 0 }, - {"echok", local, SANE_SET | REV, ECHOK, 0 }, - {"echonl", local, SANE_UNSET | REV, ECHONL, 0 }, - {"noflsh", local, SANE_UNSET | REV, NOFLSH, 0 }, + MI_ENTRY("iexten", local, SANE_SET | REV, IEXTEN, 0 ), +#endif + MI_ENTRY("echo", local, SANE_SET | REV, ECHO, 0 ), + MI_ENTRY("echoe", local, SANE_SET | REV, ECHOE, 0 ), + MI_ENTRY("crterase", local, REV | OMIT, ECHOE, 0 ), + MI_ENTRY("echok", local, SANE_SET | REV, ECHOK, 0 ), + MI_ENTRY("echonl", local, SANE_UNSET | REV, ECHONL, 0 ), + MI_ENTRY("noflsh", local, SANE_UNSET | REV, NOFLSH, 0 ), #ifdef XCASE - {"xcase", local, SANE_UNSET | REV, XCASE, 0 }, + MI_ENTRY("xcase", local, SANE_UNSET | REV, XCASE, 0 ), #endif #ifdef TOSTOP - {"tostop", local, SANE_UNSET | REV, TOSTOP, 0 }, + MI_ENTRY("tostop", local, SANE_UNSET | REV, TOSTOP, 0 ), #endif #ifdef ECHOPRT - {"echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 }, - {"prterase", local, REV | OMIT, ECHOPRT, 0 }, + MI_ENTRY("echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 ), + MI_ENTRY("prterase", local, REV | OMIT, ECHOPRT, 0 ), #endif #ifdef ECHOCTL - {"echoctl", local, SANE_SET | REV, ECHOCTL, 0 }, - {"ctlecho", local, REV | OMIT, ECHOCTL, 0 }, + MI_ENTRY("echoctl", local, SANE_SET | REV, ECHOCTL, 0 ), + MI_ENTRY("ctlecho", local, REV | OMIT, ECHOCTL, 0 ), #endif #ifdef ECHOKE - {"echoke", local, SANE_SET | REV, ECHOKE, 0 }, - {"crtkill", local, REV | OMIT, ECHOKE, 0 }, -#endif - {evenp, combination, REV | OMIT, 0, 0 }, - {parity, combination, REV | OMIT, 0, 0 }, - {stty_oddp, combination, REV | OMIT, 0, 0 }, - {stty_nl, combination, REV | OMIT, 0, 0 }, - {stty_ek, combination, OMIT, 0, 0 }, - {stty_sane, combination, OMIT, 0, 0 }, - {cooked, combination, REV | OMIT, 0, 0 }, - {raw, combination, REV | OMIT, 0, 0 }, - {stty_pass8, combination, REV | OMIT, 0, 0 }, - {litout, combination, REV | OMIT, 0, 0 }, - {cbreak, combination, REV | OMIT, 0, 0 }, + MI_ENTRY("echoke", local, SANE_SET | REV, ECHOKE, 0 ), + MI_ENTRY("crtkill", local, REV | OMIT, ECHOKE, 0 ), +#endif + MI_ENTRY(evenp, combination, REV | OMIT, 0, 0 ), + MI_ENTRY(parity, combination, REV | OMIT, 0, 0 ), + MI_ENTRY(stty_oddp, combination, REV | OMIT, 0, 0 ), + MI_ENTRY(stty_nl, combination, REV | OMIT, 0, 0 ), + MI_ENTRY(stty_ek, combination, OMIT, 0, 0 ), + MI_ENTRY(stty_sane, combination, OMIT, 0, 0 ), + MI_ENTRY(cooked, combination, REV | OMIT, 0, 0 ), + MI_ENTRY(raw, combination, REV | OMIT, 0, 0 ), + MI_ENTRY(stty_pass8, combination, REV | OMIT, 0, 0 ), + MI_ENTRY(litout, combination, REV | OMIT, 0, 0 ), + MI_ENTRY(cbreak, combination, REV | OMIT, 0, 0 ), #ifdef IXANY - {decctlq, combination, REV | OMIT, 0, 0 }, + MI_ENTRY(decctlq, combination, REV | OMIT, 0, 0 ), #endif #if defined (TABDLY) || defined (OXTABS) - {stty_tabs, combination, REV | OMIT, 0, 0 }, + MI_ENTRY(stty_tabs, combination, REV | OMIT, 0, 0 ), #endif #if defined(XCASE) && defined(IUCLC) && defined(OLCUC) - {stty_lcase, combination, REV | OMIT, 0, 0 }, - {stty_LCASE, combination, REV | OMIT, 0, 0 }, + MI_ENTRY(stty_lcase, combination, REV | OMIT, 0, 0 ), + MI_ENTRY(stty_LCASE, combination, REV | OMIT, 0, 0 ), #endif - {stty_crt, combination, OMIT, 0, 0 }, - {stty_dec, combination, OMIT, 0, 0 }, + MI_ENTRY(stty_crt, combination, OMIT, 0, 0 ), + MI_ENTRY(stty_dec, combination, OMIT, 0, 0 ), }; static const int NUM_mode_info = @@ -359,7 +359,7 @@ static const int NUM_mode_info = struct control_info { const char *name; /* Name given on command line. */ unsigned char saneval; /* Value to set for `stty sane'. */ - int offset; /* Offset in c_cc. */ + unsigned char offset; /* Offset in c_cc. */ }; /* Control characters. */ @@ -408,7 +408,6 @@ static const int NUM_control_info = static const char * visible(unsigned int ch); -static unsigned long baud_to_value(speed_t speed); static int recover_mode(char *arg, struct termios *mode); static int screen_columns(void); static int set_mode(const struct mode_info *info, @@ -416,12 +415,11 @@ static int set_mode(const struct mode_info *info, static speed_t string_to_baud(const char *arg); static tcflag_t* mode_type_flag(enum mode_type type, struct termios *mode); static void display_all(struct termios *mode, int fd, - const char *device_name); -static void display_changed(struct termios *mode); -static void display_recoverable(struct termios *mode); -static void display_settings(enum output_type output_type, - struct termios *mode, int fd, - const char *device_name); + const char *device_name); +static void display_changed(struct termios *mode, int fd, + const char *device_name); +static void display_recoverable(struct termios *mode, int fd, + const char *device_name); static void display_speed(struct termios *mode, int fancy); static void display_window_size(int fancy, int fd, const char *device_name); @@ -479,7 +477,7 @@ extern int main(int argc, char **argv) #endif { struct termios mode; - enum output_type output_type; + void (*output_func)(struct termios *, int, const char *); int optc; int require_set_attr; int speed_was_set; @@ -491,7 +489,7 @@ extern int main(int argc, char **argv) int fd; const char *device_name; - output_type = changed; + output_func = display_changed; verbose_output = 0; recoverable_output = 0; @@ -502,17 +500,17 @@ extern int main(int argc, char **argv) switch (optc) { case 'a': verbose_output = 1; - output_type = all; + output_func = display_all; break; case 'g': recoverable_output = 1; - output_type = recoverable; + output_func = display_recoverable; break; case 'F': if (file_name) - error_msg_and_die("only one device may be specified"); + bb_error_msg_and_die("only one device may be specified"); file_name = optarg; break; @@ -529,12 +527,12 @@ extern int main(int argc, char **argv) noargs = 0; /* Specifying both -a and -g gets an error. */ - if (verbose_output && recoverable_output) - error_msg_and_die ("verbose and stty-readable output styles are mutually exclusive"); + if (verbose_output & recoverable_output) + bb_error_msg_and_die ("verbose and stty-readable output styles are mutually exclusive"); /* Specifying any other arguments with -a or -g gets an error. */ - if (!noargs && (verbose_output || recoverable_output)) - error_msg_and_die ("modes may not be set when specifying an output style"); + if (~noargs & (verbose_output | recoverable_output)) + bb_error_msg_and_die ("modes may not be set when specifying an output style"); /* FIXME: it'd be better not to open the file until we've verified that all arguments are valid. Otherwise, we could end up doing @@ -547,26 +545,26 @@ extern int main(int argc, char **argv) device_name = file_name; fd = open(device_name, O_RDONLY | O_NONBLOCK); if (fd < 0) - perror_msg_and_die("%s", device_name); + bb_perror_msg_and_die("%s", device_name); if ((fdflags = fcntl(fd, F_GETFL)) == -1 || fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) - perror_msg_and_die("%s: couldn't reset non-blocking mode", + bb_perror_msg_and_die("%s: couldn't reset non-blocking mode", device_name); } else { fd = 0; - device_name = "standard input"; + device_name = bb_msg_standard_input; } /* Initialize to all zeroes so there is no risk memcmp will report a spurious difference in an uninitialized portion of the structure. */ memset(&mode, 0, sizeof(mode)); if (tcgetattr(fd, &mode)) - perror_msg_and_die("%s", device_name); + bb_perror_msg_and_die("%s", device_name); - if (verbose_output || recoverable_output || noargs) { + if (verbose_output | recoverable_output | noargs) { max_col = screen_columns(); current_col = 0; - display_settings(output_type, &mode, fd, device_name); + output_func(&mode, fd, device_name); return EXIT_SUCCESS; } @@ -610,13 +608,13 @@ extern int main(int argc, char **argv) } if (match_found == 0 && reversed) - error_msg_and_die("invalid argument `%s'", --argv[k]); + bb_error_msg_and_die("invalid argument `%s'", --argv[k]); if (match_found == 0) for (i = 0; i < NUM_control_info; ++i) if (STREQ(argv[k], control_info[i].name)) { if (k == argc - 1) - error_msg_and_die("missing argument to `%s'", argv[k]); + bb_error_msg_and_die("missing argument to `%s'", argv[k]); match_found = 1; ++k; set_control_char(&control_info[i], argv[k], &mode); @@ -627,14 +625,14 @@ extern int main(int argc, char **argv) if (match_found == 0) { if (STREQ(argv[k], "ispeed")) { if (k == argc - 1) - error_msg_and_die("missing argument to `%s'", argv[k]); + bb_error_msg_and_die("missing argument to `%s'", argv[k]); ++k; set_speed(input_speed, argv[k], &mode); speed_was_set = 1; require_set_attr = 1; } else if (STREQ(argv[k], "ospeed")) { if (k == argc - 1) - error_msg_and_die("missing argument to `%s'", argv[k]); + bb_error_msg_and_die("missing argument to `%s'", argv[k]); ++k; set_speed(output_speed, argv[k], &mode); speed_was_set = 1; @@ -643,16 +641,16 @@ extern int main(int argc, char **argv) #ifdef TIOCGWINSZ else if (STREQ(argv[k], "rows")) { if (k == argc - 1) - error_msg_and_die("missing argument to `%s'", argv[k]); + bb_error_msg_and_die("missing argument to `%s'", argv[k]); ++k; - set_window_size((int) parse_number(argv[k], stty_suffixes), + set_window_size((int) bb_xparse_number(argv[k], stty_suffixes), -1, fd, device_name); } else if (STREQ(argv[k], "cols") || STREQ(argv[k], "columns")) { if (k == argc - 1) - error_msg_and_die("missing argument to `%s'", argv[k]); + bb_error_msg_and_die("missing argument to `%s'", argv[k]); ++k; set_window_size(-1, - (int) parse_number(argv[k], stty_suffixes), + (int) bb_xparse_number(argv[k], stty_suffixes), fd, device_name); } else if (STREQ(argv[k], "size")) { max_col = screen_columns(); @@ -663,9 +661,9 @@ extern int main(int argc, char **argv) #ifdef HAVE_C_LINE else if (STREQ(argv[k], "line")) { if (k == argc - 1) - error_msg_and_die("missing argument to `%s'", argv[k]); + bb_error_msg_and_die("missing argument to `%s'", argv[k]); ++k; - mode.c_line = parse_number(argv[k], stty_suffixes); + mode.c_line = bb_xparse_number(argv[k], stty_suffixes); require_set_attr = 1; } #endif @@ -679,7 +677,7 @@ extern int main(int argc, char **argv) speed_was_set = 1; require_set_attr = 1; } else - error_msg_and_die("invalid argument `%s'", argv[k]); + bb_error_msg_and_die("invalid argument `%s'", argv[k]); } } @@ -687,7 +685,7 @@ extern int main(int argc, char **argv) struct termios new_mode; if (tcsetattr(fd, TCSADRAIN, &mode)) - perror_msg_and_die("%s", device_name); + bb_perror_msg_and_die("%s", device_name); /* POSIX (according to Zlotnick's book) tcsetattr returns zero if it performs *any* of the requested operations. This means it @@ -700,7 +698,7 @@ extern int main(int argc, char **argv) spurious difference in an uninitialized portion of the structure. */ memset(&new_mode, 0, sizeof(new_mode)); if (tcgetattr(fd, &new_mode)) - perror_msg_and_die("%s", device_name); + bb_perror_msg_and_die("%s", device_name); /* Normally, one shouldn't use memcmp to compare structures that may have `holes' containing uninitialized data, but we have been @@ -723,7 +721,7 @@ extern int main(int argc, char **argv) new_mode.c_cflag &= (~CIBAUD); if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0) #endif - error_msg_and_die ("%s: unable to perform all requested operations", + bb_error_msg_and_die ("%s: unable to perform all requested operations", device_name); } } @@ -896,9 +894,9 @@ set_mode(const struct mode_info *info, int reversed, struct termios *mode) #endif } } else if (reversed) - *bitsp = *bitsp & ~info->mask & ~info->bits; + *bitsp = *bitsp & ~((unsigned long)info->mask) & ~info->bits; else - *bitsp = (*bitsp & ~info->mask) | info->bits; + *bitsp = (*bitsp & ~((unsigned long)info->mask)) | info->bits; return 1; } @@ -910,7 +908,7 @@ set_control_char(const struct control_info *info, const char *arg, unsigned char value; if (info->name == stty_min || info->name == stty_time) - value = parse_number(arg, stty_suffixes); + value = bb_xparse_number(arg, stty_suffixes); else if (arg[0] == '\0' || arg[1] == '\0') value = arg[0]; else if (STREQ(arg, "^-") || STREQ(arg, "undef")) @@ -921,7 +919,7 @@ set_control_char(const struct control_info *info, const char *arg, else value = arg[1] & ~0140; /* Non-letters get weird results. */ } else - value = parse_number(arg, stty_suffixes); + value = bb_xparse_number(arg, stty_suffixes); mode->c_cc[info->offset] = value; } @@ -931,10 +929,13 @@ set_speed(enum speed_setting type, const char *arg, struct termios *mode) speed_t baud; baud = string_to_baud(arg); - if (type == input_speed || type == both_speeds) + + if (type != output_speed) { /* either input or both */ cfsetispeed(mode, baud); - if (type == output_speed || type == both_speeds) + } + if (type != input_speed) { /* either output or both */ cfsetospeed(mode, baud); + } } #ifdef TIOCGWINSZ @@ -953,7 +954,7 @@ set_window_size(int rows, int cols, int fd, const char *device_name) if (get_win_size(fd, &win)) { if (errno != EINVAL) - perror_msg_and_die("%s", device_name); + bb_perror_msg_and_die("%s", device_name); memset(&win, 0, sizeof(win)); } @@ -975,32 +976,28 @@ set_window_size(int rows, int cols, int fd, const char *device_name) ttysz.ts_lines = win.ws_row; ttysz.ts_cols = win.ws_col; - win.ws_row = 1; - win.ws_col = 1; - - if (ioctl(fd, TIOCSWINSZ, (char *) &win)) - perror_msg_and_die("%s", device_name); + win.ws_row = win.ws_col = 1; - if (ioctl(fd, TIOCSSIZE, (char *) &ttysz)) - perror_msg_and_die("%s", device_name); + if ((ioctl(fd, TIOCSWINSZ, (char *) &win) != 0) + || (ioctl(fd, TIOCSSIZE, (char *) &ttysz) != 0)) { + bb_perror_msg_and_die("%s", device_name); return; } # endif if (ioctl(fd, TIOCSWINSZ, (char *) &win)) - perror_msg_and_die("%s", device_name); + bb_perror_msg_and_die("%s", device_name); } static void display_window_size(int fancy, int fd, const char *device_name) { + const char *fmt_str = "%s" "\0" "%s: no size information for this device"; struct winsize win; if (get_win_size(fd, &win)) { - if (errno != EINVAL) - perror_msg_and_die("%s", device_name); - if (!fancy) - perror_msg_and_die("%s: no size information for this device", - device_name); + if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) { + bb_perror_msg_and_die(fmt_str, device_name); + } } else { wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n", win.ws_row, win.ws_col); @@ -1012,6 +1009,9 @@ static void display_window_size(int fancy, int fd, const char *device_name) static int screen_columns(void) { + int columns; + const char *s; + #ifdef TIOCGWINSZ struct winsize win; @@ -1025,51 +1025,29 @@ static int screen_columns(void) return win.ws_col; #endif - if (getenv("COLUMNS")) - return atoi(getenv("COLUMNS")); - return 80; -} - -static tcflag_t *mode_type_flag(enum mode_type type, struct termios *mode) -{ - switch (type) { - case control: - return &mode->c_cflag; - - case input: - return &mode->c_iflag; - - case output: - return &mode->c_oflag; - - case local: - return &mode->c_lflag; - - default: /* combination: */ - return NULL; + columns = 80; + if ((s = getenv("COLUMNS"))) { + columns = atoi(s); } + return columns; } -static void -display_settings(enum output_type output_type, struct termios *mode, - int fd, const char *device_name) +static tcflag_t *mode_type_flag(enum mode_type type, struct termios *mode) { - switch (output_type) { - case changed: - display_changed(mode); - break; - - case all: - display_all(mode, fd, device_name); - break; - - case recoverable: - display_recoverable(mode); - break; + static const unsigned char tcflag_offsets[] = { + offsetof(struct termios, c_cflag), /* control */ + offsetof(struct termios, c_iflag), /* input */ + offsetof(struct termios, c_oflag), /* output */ + offsetof(struct termios, c_lflag) /* local */ + }; + + if (((unsigned int) type) <= local) { + return (tcflag_t *)(((char *) mode) + tcflag_offsets[(int)type]); } + return NULL; } -static void display_changed(struct termios *mode) +static void display_changed(struct termios *mode, int fd, const char *device_name) { int i; int empty_line; @@ -1206,18 +1184,25 @@ display_all(struct termios *mode, int fd, const char *device_name) static void display_speed(struct termios *mode, int fancy) { - if (cfgetispeed(mode) == 0 || cfgetispeed(mode) == cfgetospeed(mode)) - wrapf(fancy ? "speed %lu baud;" : "%lu\n", - baud_to_value(cfgetospeed(mode))); - else - wrapf(fancy ? "ispeed %lu baud; ospeed %lu baud;" : "%lu %lu\n", - baud_to_value(cfgetispeed(mode)), - baud_to_value(cfgetospeed(mode))); + unsigned long ispeed, ospeed; + const char *fmt_str = + "%lu %lu\n\0" "ispeed %lu baud; ospeed %lu baud;\0" + "%lu\n\0" "\0\0\0\0" "speed %lu baud;"; + + ospeed = ispeed = cfgetispeed(mode); + if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) { + ispeed = ospeed; /* in case ispeed was 0 */ + fmt_str += 43; + } + if (fancy) { + fmt_str += 9; + } + wrapf(fmt_str, bb_baud_to_value(ispeed), bb_baud_to_value(ospeed)); if (!fancy) current_col = 0; } -static void display_recoverable(struct termios *mode) +static void display_recoverable(struct termios *mode, int fd, const char *device_name) { int i; @@ -1259,62 +1244,9 @@ static int recover_mode(char *arg, struct termios *mode) return 1; } -struct speed_map { - speed_t speed; /* Internal form. */ - unsigned long value; /* Numeric value. */ -}; - -static const struct speed_map speeds[] = { - {B0, 0}, - {B50, 50}, - {B75, 75}, - {B110, 110}, - {B134, 134}, - {B150, 150}, - {B200, 200}, - {B300, 300}, - {B600, 600}, - {B1200, 1200}, - {B1800, 1800}, - {B2400, 2400}, - {B4800, 4800}, - {B9600, 9600}, - {B19200, 19200}, - {B38400, 38400}, -#ifdef B57600 - {B57600, 57600}, -#endif -#ifdef B115200 - {B115200, 115200}, -#endif -#ifdef B230400 - {B230400, 230400}, -#endif -#ifdef B460800 - {B460800, 460800}, -#endif -}; - -static const int NUM_SPEEDS = (sizeof(speeds) / sizeof(struct speed_map)); - static speed_t string_to_baud(const char *arg) { - int i; - - for (i = 0; i < NUM_SPEEDS; ++i) - if (parse_number(arg, 0) == speeds[i].value) - return speeds[i].speed; - return (speed_t) - 1; -} - -static unsigned long baud_to_value(speed_t speed) -{ - int i; - - for (i = 0; i < NUM_SPEEDS; ++i) - if (speed == speeds[i].speed) - return speeds[i].value; - return 0; + return bb_value_to_baud(bb_xparse_number(arg, 0)); } static void sane_mode(struct termios *mode) @@ -1333,10 +1265,12 @@ static void sane_mode(struct termios *mode) for (i = 0; i < NUM_mode_info; ++i) { if (mode_info[i].flags & SANE_SET) { bitsp = mode_type_flag(mode_info[i].type, mode); - *bitsp = (*bitsp & ~mode_info[i].mask) | mode_info[i].bits; + *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask)) + | mode_info[i].bits; } else if (mode_info[i].flags & SANE_UNSET) { bitsp = mode_type_flag(mode_info[i].type, mode); - *bitsp = *bitsp & ~mode_info[i].mask & ~mode_info[i].bits; + *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask) + & ~mode_info[i].bits; } } } @@ -1349,47 +1283,32 @@ static const char *visible(unsigned int ch) static char buf[10]; char *bpout = buf; - if (ch == _POSIX_VDISABLE) + if (ch == _POSIX_VDISABLE) { return "<undef>"; + } - if (ch >= 32) { - if (ch < 127) - *bpout++ = ch; - else if (ch == 127) { - *bpout++ = '^'; - *bpout++ = '?'; - } else { - *bpout++ = 'M', *bpout++ = '-'; - if (ch >= 128 + 32) { - if (ch < 128 + 127) - *bpout++ = ch - 128; - else { - *bpout++ = '^'; - *bpout++ = '?'; - } - } else { - *bpout++ = '^'; - *bpout++ = ch - 128 + 64; - } - } - } else { + if (ch >= 128) { + ch -= 128; + *bpout++ = 'M'; + *bpout++ = '-'; + } + + if (ch < 32) { *bpout++ = '^'; *bpout++ = ch + 64; + } else if (ch < 127) { + *bpout++ = ch; + } else { + *bpout++ = '^'; + *bpout++ = '?'; } + *bpout = '\0'; return (const char *) buf; } #ifdef TEST -const char *applet_name = "stty"; +const char *bb_applet_name = "stty"; #endif - -/* -Local Variables: -c-file-style: "linux" -c-basic-offset: 4 -tab-width: 4 -End: -*/ |