-
-
-static void
-set_mode(const struct mode_info *info, int reversed, struct termios *mode)
-{
- tcflag_t *bitsp;
-
- bitsp = mode_type_flag(EMT(info->type), mode);
-
- if (bitsp == NULL) {
- /* Combination mode */
- if (info->name == evenp || info->name == parity) {
- if (reversed)
- mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
- else
- mode->c_cflag =
- (mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7;
- } else if (info->name == stty_oddp) {
- if (reversed)
- mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
- else
- mode->c_cflag =
- (mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB;
- } else if (info->name == stty_nl) {
- if (reversed) {
- mode->c_iflag = (mode->c_iflag | ICRNL) & ~INLCR & ~IGNCR;
- mode->c_oflag = (mode->c_oflag
-#ifdef ONLCR
- | ONLCR
-#endif
- )
-#ifdef OCRNL
- & ~OCRNL
-#endif
-#ifdef ONLRET
- & ~ONLRET
-#endif
- ;
- } else {
- mode->c_iflag = mode->c_iflag & ~ICRNL;
-#ifdef ONLCR
- mode->c_oflag = mode->c_oflag & ~ONLCR;
-#endif
- }
- } else if (info->name == stty_ek) {
- mode->c_cc[VERASE] = CERASE;
- mode->c_cc[VKILL] = CKILL;
- } else if (info->name == stty_sane)
- sane_mode(mode);
- else if (info->name == cbreak) {
- if (reversed)
- mode->c_lflag |= ICANON;
- else
- mode->c_lflag &= ~ICANON;
- } else if (info->name == stty_pass8) {
- if (reversed) {
- mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
- mode->c_iflag |= ISTRIP;
- } else {
- mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
- mode->c_iflag &= ~ISTRIP;
- }
- } else if (info->name == litout) {
- if (reversed) {
- mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;
- mode->c_iflag |= ISTRIP;
- mode->c_oflag |= OPOST;
- } else {
- mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
- mode->c_iflag &= ~ISTRIP;
- mode->c_oflag &= ~OPOST;
- }
- } else if (info->name == raw || info->name == cooked) {
- if ((info->name[0] == 'r' && reversed)
- || (info->name[0] == 'c' && !reversed)) {
- /* Cooked mode */
- mode->c_iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;
- mode->c_oflag |= OPOST;
- mode->c_lflag |= ISIG | ICANON;
-#if VMIN == VEOF
- mode->c_cc[VEOF] = CEOF;
-#endif
-#if VTIME == VEOL
- mode->c_cc[VEOL] = CEOL;
-#endif
- } else {
- /* Raw mode */
- mode->c_iflag = 0;
- mode->c_oflag &= ~OPOST;
- mode->c_lflag &= ~(ISIG | ICANON
-#ifdef XCASE
- | XCASE
-#endif
- );
- mode->c_cc[VMIN] = 1;
- mode->c_cc[VTIME] = 0;
- }
- }
-#ifdef IXANY
- else if (info->name == decctlq) {
- if (reversed)
- mode->c_iflag |= IXANY;
- else
- mode->c_iflag &= ~IXANY;
- }
-#endif
-#ifdef TABDLY
- else if (info->name == stty_tabs) {
- if (reversed)
- mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB3;
- else
- mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB0;
- }
-#else
-# ifdef OXTABS
- else if (info->name == stty_tabs) {
- if (reversed)
- mode->c_oflag = mode->c_oflag | OXTABS;
- else
- mode->c_oflag = mode->c_oflag & ~OXTABS;
- }
-# endif
-#endif
-#if defined(XCASE) && defined(IUCLC) && defined(OLCUC)
- else if (info->name == stty_lcase || info->name == stty_LCASE) {
- if (reversed) {
- mode->c_lflag &= ~XCASE;
- mode->c_iflag &= ~IUCLC;
- mode->c_oflag &= ~OLCUC;
- } else {
- mode->c_lflag |= XCASE;
- mode->c_iflag |= IUCLC;
- mode->c_oflag |= OLCUC;
- }
- }
-#endif
- else if (info->name == stty_crt)
- mode->c_lflag |= ECHOE
-#ifdef ECHOCTL
- | ECHOCTL
-#endif
-#ifdef ECHOKE
- | ECHOKE
-#endif
- ;
- else if (info->name == stty_dec) {
- mode->c_cc[VINTR] = 3; /* ^C */
- mode->c_cc[VERASE] = 127; /* DEL */
- mode->c_cc[VKILL] = 21; /* ^U */
- mode->c_lflag |= ECHOE
-#ifdef ECHOCTL
- | ECHOCTL
-#endif
-#ifdef ECHOKE
- | ECHOKE
-#endif
- ;
-#ifdef IXANY
- mode->c_iflag &= ~IXANY;
-#endif
- }
- } else if (reversed)
- *bitsp = *bitsp & ~((unsigned long)info->mask) & ~info->bits;
- else
- *bitsp = (*bitsp & ~((unsigned long)info->mask)) | info->bits;
-}
-
-static void
-set_control_char(const struct control_info *info, const char *arg,
- struct termios *mode)
-{
- unsigned char value;
-
- if (info->name == stty_min || info->name == stty_time)
- 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"))
- value = _POSIX_VDISABLE;
- else if (arg[0] == '^' && arg[1] != '\0') { /* Ignore any trailing junk */
- if (arg[1] == '?')
- value = 127;
- else
- value = arg[1] & ~0140; /* Non-letters get weird results */
- } else
- value = bb_xparse_number(arg, stty_suffixes);
- mode->c_cc[info->offset] = value;
-}
-
-static void
-set_speed(enum speed_setting type, const char *arg, struct termios *mode)
-{
- speed_t baud;
-
- baud = string_to_baud(arg);
-
- if (type != output_speed) { /* either input or both */
- cfsetispeed(mode, baud);
- }
- if (type != input_speed) { /* either output or both */
- cfsetospeed(mode, baud);
- }
-}
-
-#ifdef TIOCGWINSZ
-
-static int get_win_size(int fd, struct winsize *win)
-{
- return ioctl(fd, TIOCGWINSZ, (char *) win);
-}
-
-static void
-set_window_size(int rows, int cols)
-{
- struct winsize win;
-
- if (get_win_size(STDIN_FILENO, &win)) {
- if (errno != EINVAL)
- perror_on_device("%s");
- memset(&win, 0, sizeof(win));
- }
-
- if (rows >= 0)
- win.ws_row = rows;
- if (cols >= 0)
- win.ws_col = cols;
-
-# ifdef TIOCSSIZE
- /* Alexander Dupuy <dupuy@cs.columbia.edu> wrote:
- The following code deals with a bug in the SunOS 4.x (and 3.x?) kernel.
- This comment from sys/ttold.h describes Sun's twisted logic - a better
- test would have been (ts_lines > 64k || ts_cols > 64k || ts_cols == 0).
- At any rate, the problem is gone in Solaris 2.x */
-
- if (win.ws_row == 0 || win.ws_col == 0) {
- struct ttysize ttysz;
-
- ttysz.ts_lines = win.ws_row;
- ttysz.ts_cols = win.ws_col;
-
- win.ws_row = win.ws_col = 1;
-
- if ((ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win) != 0)
- || (ioctl(STDIN_FILENO, TIOCSSIZE, (char *) &ttysz) != 0)) {
- perror_on_device("%s");
- }
- return;
- }
-# endif
-
- if (ioctl(STDIN_FILENO, TIOCSWINSZ, (char *) &win))
- perror_on_device("%s");
-}
-
-static void display_window_size(int fancy)
-{
- const char *fmt_str = "%s" "\0" "%s: no size information for this device";
- struct winsize win;
-
- if (get_win_size(STDIN_FILENO, &win)) {
- if ((errno != EINVAL) || ((fmt_str += 2), !fancy)) {
- perror_on_device(fmt_str);
- }
- } else {
- wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n",
- win.ws_row, win.ws_col);
- if (!fancy)
- current_col = 0;
- }
-}
-#endif
-
-static int screen_columns(void)
-{
- int columns;
- const char *s;
-
-#ifdef TIOCGWINSZ
- struct winsize win;
-
- /* With Solaris 2.[123], this ioctl fails and errno is set to
- EINVAL for telnet (but not rlogin) sessions.
- On ISC 3.0, it fails for the console and the serial port
- (but it works for ptys).
- It can also fail on any system when stdout isn't a tty.
- In case of any failure, just use the default */
- if (get_win_size(STDOUT_FILENO, &win) == 0 && win.ws_col > 0)
- return win.ws_col;
-#endif
-
- columns = 80;
- if ((s = getenv("COLUMNS"))) {
- columns = atoi(s);
- }
- return columns;
-}
-
-static tcflag_t *mode_type_flag(enum mode_type type, const struct termios *mode)
-{
- 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(const struct termios *mode)
-{
- int i;
- int empty_line;
- tcflag_t *bitsp;
- unsigned long mask;
- enum mode_type prev_type = control;
-
- display_speed(mode, 1);
-#ifdef HAVE_C_LINE
- wrapf("line = %d;", mode->c_line);
-#endif
- putchar('\n');
- current_col = 0;
-
- empty_line = 1;
- for (i = 0; control_info[i].name != stty_min; ++i) {
- if (mode->c_cc[control_info[i].offset] == control_info[i].saneval)
- continue;
- /* If swtch is the same as susp, don't print both */
-#if VSWTCH == VSUSP
- if (control_info[i].name == stty_swtch)
- continue;
-#endif
- /* If eof uses the same slot as min, only print whichever applies */
-#if VEOF == VMIN
- if ((mode->c_lflag & ICANON) == 0
- && (control_info[i].name == stty_eof
- || control_info[i].name == stty_eol)) continue;
-#endif
-
- empty_line = 0;
- wrapf("%s = %s;", control_info[i].name,
- visible(mode->c_cc[control_info[i].offset]));
- }
- if ((mode->c_lflag & ICANON) == 0) {
- wrapf("min = %d; time = %d;\n", (int) mode->c_cc[VMIN],
- (int) mode->c_cc[VTIME]);
- } else if (empty_line == 0)
- putchar('\n');
- current_col = 0;
-
- empty_line = 1;
- for (i = 0; i < NUM_mode_info; ++i) {
- if (mode_info[i].flags & OMIT)
- continue;
- if (EMT(mode_info[i].type) != prev_type) {
- if (empty_line == 0) {
- putchar('\n');
- current_col = 0;
- empty_line = 1;
- }
- prev_type = EMT(mode_info[i].type);
- }
-
- bitsp = mode_type_flag(EMT(mode_info[i].type), mode);
- mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
- if ((*bitsp & mask) == mode_info[i].bits) {
- if (mode_info[i].flags & SANE_UNSET) {
- wrapf("%s", mode_info[i].name);
- empty_line = 0;
- }
- } else if ((mode_info[i].flags & (SANE_SET | REV)) == (SANE_SET | REV)) {
- wrapf("-%s", mode_info[i].name);
- empty_line = 0;
- }
- }
- if (empty_line == 0)
- putchar('\n');
- current_col = 0;
-}
-
-static void
-display_all(const struct termios *mode)
-{
- int i;
- tcflag_t *bitsp;
- unsigned long mask;
- enum mode_type prev_type = control;
-
- display_speed(mode, 1);
-#ifdef TIOCGWINSZ
- display_window_size(1);
-#endif
-#ifdef HAVE_C_LINE
- wrapf("line = %d;", mode->c_line);
-#endif
- putchar('\n');
- current_col = 0;
-
- for (i = 0; control_info[i].name != stty_min; ++i) {
- /* If swtch is the same as susp, don't print both */
-#if VSWTCH == VSUSP
- if (control_info[i].name == stty_swtch)
- continue;
-#endif
- /* If eof uses the same slot as min, only print whichever applies */
-#if VEOF == VMIN
- if ((mode->c_lflag & ICANON) == 0
- && (control_info[i].name == stty_eof
- || control_info[i].name == stty_eol)) continue;
-#endif
- wrapf("%s = %s;", control_info[i].name,
- visible(mode->c_cc[control_info[i].offset]));
- }
-#if VEOF == VMIN
- if ((mode->c_lflag & ICANON) == 0)
-#endif
- wrapf("min = %d; time = %d;", mode->c_cc[VMIN], mode->c_cc[VTIME]);
- if (current_col != 0)
- putchar('\n');
- current_col = 0;
-
- for (i = 0; i < NUM_mode_info; ++i) {
- if (mode_info[i].flags & OMIT)
- continue;
- if (EMT(mode_info[i].type) != prev_type) {
- putchar('\n');
- current_col = 0;
- prev_type = EMT(mode_info[i].type);
- }
-
- bitsp = mode_type_flag(EMT(mode_info[i].type), mode);
- mask = mode_info[i].mask ? mode_info[i].mask : mode_info[i].bits;
- if ((*bitsp & mask) == mode_info[i].bits)
- wrapf("%s", mode_info[i].name);
- else if (mode_info[i].flags & REV)
- wrapf("-%s", mode_info[i].name);
- }
- putchar('\n');
- current_col = 0;
-}
-
-static void display_speed(const struct termios *mode, int fancy)
-{
- 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, tty_baud_to_value(ispeed), tty_baud_to_value(ospeed));
- if (!fancy)
- current_col = 0;
-}
-
-static void display_recoverable(const struct termios *mode)
-{
- int i;
-
- printf("%lx:%lx:%lx:%lx",
- (unsigned long) mode->c_iflag, (unsigned long) mode->c_oflag,
- (unsigned long) mode->c_cflag, (unsigned long) mode->c_lflag);
- for (i = 0; i < NCCS; ++i)
- printf(":%x", (unsigned int) mode->c_cc[i]);
- putchar('\n');
-}
-
-static int recover_mode(const char *arg, struct termios *mode)
-{
- int i, n;
- unsigned int chr;
- unsigned long iflag, oflag, cflag, lflag;
-
- /* Scan into temporaries since it is too much trouble to figure out
- the right format for `tcflag_t' */
- if (sscanf(arg, "%lx:%lx:%lx:%lx%n",
- &iflag, &oflag, &cflag, &lflag, &n) != 4)
- return 0;
- mode->c_iflag = iflag;
- mode->c_oflag = oflag;
- mode->c_cflag = cflag;
- mode->c_lflag = lflag;
- arg += n;
- for (i = 0; i < NCCS; ++i) {
- if (sscanf(arg, ":%x%n", &chr, &n) != 1)
- return 0;
- mode->c_cc[i] = chr;
- arg += n;
- }
-
- /* Fail if there are too many fields */
- if (*arg != '\0')
- return 0;
-
- return 1;
-}
-
-static speed_t string_to_baud(const char *arg)
-{
- return tty_value_to_baud(bb_xparse_number(arg, 0));
-}
-
-static void sane_mode(struct termios *mode)
-{
- int i;
- tcflag_t *bitsp;
-
- for (i = 0; i < NUM_control_info; ++i) {
-#if VMIN == VEOF
- if (control_info[i].name == stty_min)
- break;
-#endif
- mode->c_cc[control_info[i].offset] = control_info[i].saneval;
- }
-
- for (i = 0; i < NUM_mode_info; ++i) {
- if (mode_info[i].flags & SANE_SET) {
- bitsp = mode_type_flag(EMT(mode_info[i].type), mode);
- *bitsp = (*bitsp & ~((unsigned long)mode_info[i].mask))
- | mode_info[i].bits;
- } else if (mode_info[i].flags & SANE_UNSET) {
- bitsp = mode_type_flag(EMT(mode_info[i].type), mode);
- *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
- & ~mode_info[i].bits;
- }
- }
-}
-
-/* Return a string that is the printable representation of character CH */
-/* Adapted from `cat' by Torbjorn Granlund */
-
-static const char *visible(unsigned int ch)
-{
- static char buf[10];
- char *bpout = buf;
-
- if (ch == _POSIX_VDISABLE) {
- return "<undef>";
- }
-
- 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 buf;
-}