/* vi: set sw=4 ts=4: */
-/* stty -- change and print terminal line settings
- Copyright (C) 1990-1999 Free Software Foundation, Inc.
-
- Licensed under GPLv2 or later, see file LICENSE in this source tree.
-*/
+/*
+ * stty -- change and print terminal line settings
+ * Copyright (C) 1990-1999 Free Software Foundation, Inc.
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
/* Usage: stty [-ag] [-F device] [setting...]
-
- Options:
- -a Write all current settings to stdout in human-readable form.
- -g Write all current settings to stdout in stty-readable form.
- -F Open and use the specified device instead of stdin
-
- If no args are given, write to stdout the baud rate and settings that
- have been changed from their defaults. Mode reading and changes
- are done on the specified device, or stdin if none was specified.
-
- David MacKenzie <djm@gnu.ai.mit.edu>
-
- Special for busybox ported by Vladimir Oleynik <dzo@simtreas.ru> 2001
-
- */
+ *
+ * Options:
+ * -a Write all current settings to stdout in human-readable form.
+ * -g Write all current settings to stdout in stty-readable form.
+ * -F Open and use the specified device instead of stdin
+ *
+ * If no args are given, write to stdout the baud rate and settings that
+ * have been changed from their defaults. Mode reading and changes
+ * are done on the specified device, or stdin if none was specified.
+ *
+ * David MacKenzie <djm@gnu.ai.mit.edu>
+ *
+ * Special for busybox ported by Vladimir Oleynik <dzo@simtreas.ru> 2001
+ */
+//config:config STTY
+//config: bool "stty (8.6 kb)"
+//config: default y
+//config: help
+//config: stty is used to change and print terminal line settings.
+
+//applet:IF_STTY(APPLET_NOEXEC(stty, stty, BB_DIR_BIN, BB_SUID_DROP, stty))
+
+//kbuild:lib-$(CONFIG_STTY) += stty.o
+
+//usage:#define stty_trivial_usage
+//usage: "[-a|g] [-F DEVICE] [SETTING]..."
+//usage:#define stty_full_usage "\n\n"
+//usage: "Without arguments, prints baud rate, line discipline,\n"
+//usage: "and deviations from stty sane\n"
+//usage: "\n -F DEVICE Open device instead of stdin"
+//usage: "\n -a Print all current settings in human-readable form"
+//usage: "\n -g Print in stty-readable form"
+//usage: "\n [SETTING] See manpage"
#include "libbb.h"
+#include "common_bufsiz.h"
#ifndef _POSIX_VDISABLE
# define _POSIX_VDISABLE ((unsigned char) 0)
#if defined(VEOL2) && !defined(CEOL2)
# define CEOL2 _POSIX_VDISABLE
#endif
+/* glibc-2.12.1 uses only VSWTC name */
+#if defined(VSWTC) && !defined(VSWTCH)
+# define VSWTCH VSWTC
+#endif
/* ISC renamed swtch to susp for termios, but we'll accept either name */
#if defined(VSUSP) && !defined(VSWTCH)
# define VSWTCH VSUSP
#ifndef CRDLY
# define CRDLY 0
#endif
+#ifndef CMSPAR
+# define CMSPAR 0
+#endif
#ifndef CRTSCTS
# define CRTSCTS 0
#endif
#ifndef XCASE
# define XCASE 0
#endif
+#ifndef IUTF8
+# define IUTF8 0
+#endif
/* Which speeds to set */
enum speed_setting {
/* Which member(s) of 'struct termios' a mode uses */
enum {
- /* Do NOT change the order or values, as mode_type_flag()
- * depends on them */
control, input, output, local, combination
};
+static tcflag_t *get_ptr_to_tcflag(unsigned type, const struct termios *mode)
+{
+ static const uint8_t tcflag_offsets[] ALIGN1 = {
+ 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 (type <= local) {
+ return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
+ }
+ return NULL;
+}
/* Flags for 'struct mode_info' */
#define SANE_SET 1 /* Set in 'sane' mode */
#define MI_ENTRY(N,T,F,B,M) N "\0"
/* Mode names given on command line */
-static const char mode_name[] =
+static const char mode_name[] ALIGN1 =
MI_ENTRY("evenp", combination, REV | OMIT, 0, 0 )
MI_ENTRY("parity", combination, REV | OMIT, 0, 0 )
MI_ENTRY("oddp", combination, REV | OMIT, 0, 0 )
#endif
MI_ENTRY("parenb", control, REV, PARENB, 0 )
MI_ENTRY("parodd", control, REV, PARODD, 0 )
+#if CMSPAR
+ MI_ENTRY("cmspar", control, REV, CMSPAR, 0 )
+#endif
MI_ENTRY("cs5", control, 0, CS5, CSIZE)
MI_ENTRY("cs6", control, 0, CS6, CSIZE)
MI_ENTRY("cs7", control, 0, CS7, CSIZE)
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 )
+ MI_ENTRY("tandem", input, OMIT | REV, IXOFF, 0 )
#if IUCLC
MI_ENTRY("iuclc", input, SANE_UNSET | REV, IUCLC, 0 )
#endif
#endif
#if IMAXBEL
MI_ENTRY("imaxbel", input, SANE_SET | REV, IMAXBEL, 0 )
+#endif
+#if IUTF8
+ MI_ENTRY("iutf8", input, SANE_UNSET | REV, IUTF8, 0 )
#endif
MI_ENTRY("opost", output, SANE_SET | REV, OPOST, 0 )
#if OLCUC
#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("crterase", local, OMIT | REV, 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 )
#endif
#if ECHOPRT
MI_ENTRY("echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 )
- MI_ENTRY("prterase", local, REV | OMIT, ECHOPRT, 0 )
+ MI_ENTRY("prterase", local, OMIT | REV, ECHOPRT, 0 )
#endif
#if ECHOCTL
MI_ENTRY("echoctl", local, SANE_SET | REV, ECHOCTL, 0 )
- MI_ENTRY("ctlecho", local, REV | OMIT, ECHOCTL, 0 )
+ MI_ENTRY("ctlecho", local, OMIT | REV, ECHOCTL, 0 )
#endif
#if ECHOKE
MI_ENTRY("echoke", local, SANE_SET | REV, ECHOKE, 0 )
- MI_ENTRY("crtkill", local, REV | OMIT, ECHOKE, 0 )
+ MI_ENTRY("crtkill", local, OMIT | REV, ECHOKE, 0 )
+#endif
+ MI_ENTRY("flusho", local, SANE_UNSET | REV, FLUSHO, 0 )
+#ifdef EXTPROC
+ MI_ENTRY("extproc", local, SANE_UNSET | REV, EXTPROC, 0 )
#endif
;
#endif
MI_ENTRY("parenb", control, REV, PARENB, 0 )
MI_ENTRY("parodd", control, REV, PARODD, 0 )
+#if CMSPAR
+ MI_ENTRY("cmspar", control, REV, CMSPAR, 0 )
+#endif
MI_ENTRY("cs5", control, 0, CS5, CSIZE)
MI_ENTRY("cs6", control, 0, CS6, CSIZE)
MI_ENTRY("cs7", control, 0, CS7, CSIZE)
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 )
+ MI_ENTRY("tandem", input, OMIT | REV, IXOFF, 0 )
#if IUCLC
MI_ENTRY("iuclc", input, SANE_UNSET | REV, IUCLC, 0 )
#endif
#endif
#if IMAXBEL
MI_ENTRY("imaxbel", input, SANE_SET | REV, IMAXBEL, 0 )
+#endif
+#if IUTF8
+ MI_ENTRY("iutf8", input, SANE_UNSET | REV, IUTF8, 0 )
#endif
MI_ENTRY("opost", output, SANE_SET | REV, OPOST, 0 )
#if OLCUC
#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("crterase", local, OMIT | REV, 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 )
#endif
#if ECHOPRT
MI_ENTRY("echoprt", local, SANE_UNSET | REV, ECHOPRT, 0 )
- MI_ENTRY("prterase", local, REV | OMIT, ECHOPRT, 0 )
+ MI_ENTRY("prterase", local, OMIT | REV, ECHOPRT, 0 )
#endif
#if ECHOCTL
MI_ENTRY("echoctl", local, SANE_SET | REV, ECHOCTL, 0 )
- MI_ENTRY("ctlecho", local, REV | OMIT, ECHOCTL, 0 )
+ MI_ENTRY("ctlecho", local, OMIT | REV, ECHOCTL, 0 )
#endif
#if ECHOKE
MI_ENTRY("echoke", local, SANE_SET | REV, ECHOKE, 0 )
- MI_ENTRY("crtkill", local, REV | OMIT, ECHOKE, 0 )
+ MI_ENTRY("crtkill", local, OMIT | REV, ECHOKE, 0 )
+#endif
+ MI_ENTRY("flusho", local, SANE_UNSET | REV, FLUSHO, 0 )
+#ifdef EXTPROC
+ MI_ENTRY("extproc", local, SANE_UNSET | REV, EXTPROC, 0 )
#endif
};
#define CI_ENTRY(n,s,o) n "\0"
/* Name given on command line */
-static const char control_name[] =
+static const char control_name[] ALIGN1 =
CI_ENTRY("intr", CINTR, VINTR )
CI_ENTRY("quit", CQUIT, VQUIT )
CI_ENTRY("erase", CERASE, VERASE )
#undef CI_ENTRY
#define CI_ENTRY(n,s,o) { s, o },
-static const struct control_info control_info[] = {
+static const struct control_info control_info[] ALIGN2 = {
/* This should be verbatim cut-n-paste copy of the above CI_ENTRYs */
CI_ENTRY("intr", CINTR, VINTR )
CI_ENTRY("quit", CQUIT, VQUIT )
unsigned max_col;
/* Current position, to know when to wrap */
unsigned current_col;
- char buf[10];
} FIX_ALIASING;
-#define G (*(struct globals*)&bb_common_bufsiz1)
+#define G (*(struct globals*)bb_common_bufsiz1)
#define INIT_G() do { \
+ setup_common_bufsiz(); \
G.device_name = bb_msg_standard_input; \
G.max_col = 80; \
+ G.current_col = 0; /* we are noexec, must clear */ \
} while (0)
-
-/* Return a string that is the printable representation of character CH */
-/* Adapted from 'cat' by Torbjorn Granlund */
-static const char *visible(unsigned ch)
-{
- char *bpout = G.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 G.buf;
-}
-
-static tcflag_t *mode_type_flag(unsigned type, const struct termios *mode)
-{
- static const uint8_t tcflag_offsets[] ALIGN1 = {
- 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 (type <= local) {
- return (tcflag_t*) (((char*)mode) + tcflag_offsets[type]);
- }
- return NULL;
-}
-
static void set_speed_or_die(enum speed_setting type, const char *arg,
struct termios *mode)
{
G.current_col++;
if (buf[0] != '\n') {
if (G.current_col + buflen >= G.max_col) {
- bb_putchar('\n');
G.current_col = 0;
- } else
+ bb_putchar('\n');
+ } else {
bb_putchar(' ');
+ }
}
}
fputs(buf, stdout);
const char *fmt_str = "%lu %lu\n\0ispeed %lu baud; ospeed %lu baud;";
unsigned long ispeed, ospeed;
- ospeed = ispeed = cfgetispeed(mode);
- if (ispeed == 0 || ispeed == (ospeed = cfgetospeed(mode))) {
+ ispeed = cfgetispeed(mode);
+ ospeed = cfgetospeed(mode);
+ if (ispeed == 0 || ispeed == ospeed) {
ispeed = ospeed; /* in case ispeed was 0 */
//________ 0123 4 5 6 7 8 9
fmt_str = "%lu\n\0\0\0\0\0speed %lu baud;";
display_speed(mode, 1);
if (all)
display_window_size(1);
-#ifdef HAVE_C_LINE
+#ifdef __linux__
wrapf("line = %u;\n", mode->c_line);
#else
newline();
#endif
for (i = 0; i != CIDX_min; ++i) {
+ char ch;
+ char buf10[10];
+
/* If swtch is the same as susp, don't print both */
#if VSWTCH == VSUSP
if (i == CIDX_swtch)
continue;
}
#endif
- wrapf("%s = %s;", nth_string(control_name, i),
- visible(mode->c_cc[control_info[i].offset]));
+ ch = mode->c_cc[control_info[i].offset];
+ if (ch == _POSIX_VDISABLE)
+ strcpy(buf10, "<undef>");
+ else
+ visible(ch, buf10, 0);
+ wrapf("%s = %s;", nth_string(control_name, i), buf10);
}
#if VEOF == VMIN
if ((mode->c_lflag & ICANON) == 0)
prev_type = mode_info[i].type;
}
- bitsp = mode_type_flag(mode_info[i].type, mode);
+ bitsp = get_ptr_to_tcflag(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 (all || (mode_info[i].flags & SANE_UNSET))
static void sane_mode(struct termios *mode)
{
int i;
- tcflag_t *bitsp;
for (i = 0; i < NUM_control_info; ++i) {
#if VMIN == VEOF
}
for (i = 0; i < NUM_mode_info; ++i) {
+ tcflag_t val;
+ tcflag_t *bitsp = get_ptr_to_tcflag(mode_info[i].type, mode);
+
+ if (!bitsp)
+ continue;
+ val = *bitsp & ~((unsigned long)mode_info[i].mask);
if (mode_info[i].flags & SANE_SET) {
- bitsp = mode_type_flag(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(mode_info[i].type, mode);
- *bitsp = *bitsp & ~((unsigned long)mode_info[i].mask)
- & ~mode_info[i].bits;
+ *bitsp = val | mode_info[i].bits;
+ } else
+ if (mode_info[i].flags & SANE_UNSET) {
+ *bitsp = val & ~mode_info[i].bits;
}
}
}
{
tcflag_t *bitsp;
- bitsp = mode_type_flag(info->type, mode);
+ bitsp = get_ptr_to_tcflag(info->type, mode);
if (bitsp) {
+ tcflag_t val = *bitsp & ~info->mask;
if (reversed)
- *bitsp = *bitsp & ~info->mask & ~info->bits;
+ *bitsp = val & ~info->bits;
else
- *bitsp = (*bitsp & ~info->mask) | info->bits;
+ *bitsp = val | info->bits;
return;
}
- /* Combination mode */
+ /* !bitsp - it's a "combination" mode */
if (info == &mode_info[IDX_evenp] || info == &mode_info[IDX_parity]) {
if (reversed)
mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
else
- mode->c_cflag = (mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7;
+ mode->c_cflag = (mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7;
} else if (info == &mode_info[IDX_oddp]) {
if (reversed)
mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;
else
- mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB;
+ mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB;
} else if (info == &mode_info[IDX_nl]) {
if (reversed) {
mode->c_iflag = (mode->c_iflag | ICRNL) & ~INLCR & ~IGNCR;
- mode->c_oflag = (mode->c_oflag | ONLCR) & ~OCRNL & ~ONLRET;
+ mode->c_oflag = (mode->c_oflag | ONLCR) & ~OCRNL & ~ONLRET;
} else {
mode->c_iflag = mode->c_iflag & ~ICRNL;
if (ONLCR) mode->c_oflag = mode->c_oflag & ~ONLCR;
}
switch (param) {
-#ifdef HAVE_C_LINE
+#ifdef __linux__
case param_line:
# ifndef TIOCGWINSZ
xatoul_range_sfx(argnext, 1, INT_MAX, stty_suffixes);
/* Specifying both -a and -g is an error */
if ((stty_state & (STTY_verbose_output | STTY_recoverable_output)) ==
- (STTY_verbose_output | STTY_recoverable_output))
- bb_error_msg_and_die("verbose and stty-readable output styles are mutually exclusive");
+ (STTY_verbose_output | STTY_recoverable_output)
+ ) {
+ bb_error_msg_and_die("-a and -g are mutually exclusive");
+ }
/* Specifying -a or -g with non-options is an error */
- if (!(stty_state & STTY_noargs)
- && (stty_state & (STTY_verbose_output | STTY_recoverable_output))
+ if ((stty_state & (STTY_verbose_output | STTY_recoverable_output))
+ && !(stty_state & STTY_noargs)
) {
- bb_error_msg_and_die("modes may not be set when specifying an output style");
+ bb_error_msg_and_die("modes may not be set when -a or -g is used");
}
/* Now it is safe to start doing things */
perror_on_device_and_die("%s");
if (stty_state & (STTY_verbose_output | STTY_recoverable_output | STTY_noargs)) {
- get_terminal_width_height(STDOUT_FILENO, &G.max_col, NULL);
+ G.max_col = get_terminal_width(STDOUT_FILENO);
output_func(&mode, display_all);
return EXIT_SUCCESS;
}
}
switch (param) {
-#ifdef HAVE_C_LINE
+#ifdef __linux__
case param_line:
mode.c_line = xatoul_sfx(argnext, stty_suffixes);
stty_state |= STTY_require_set_attr;
perror_on_device_and_die("%s");
if (memcmp(&mode, &new_mode, sizeof(mode)) != 0) {
-#if CIBAUD
+/*
+ * I think the below chunk is not necessary on Linux.
+ * If you are deleting it, also delete STTY_speed_was_set bit -
+ * it is only ever checked here.
+ */
+#if 0 /* was "if CIBAUD" */
/* SunOS 4.1.3 (at least) has the problem that after this sequence,
tcgetattr (&m1); tcsetattr (&m1); tcgetattr (&m2);
sometimes (m1 != m2). The only difference is in the four bits