config: more tweaks
[oweals/busybox.git] / coreutils / stty.c
index d1e74f437e946251f5ff5f8a6c535ebcf3d11e3e..6251f2aef5defdb8b7bbfcc0e3a81e8de8f5523a 100644 (file)
@@ -1,25 +1,34 @@
 /* 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
+ */
+//config:config STTY
+//config:      bool "stty (8.9 kb)"
+//config:      default y
+//config:      help
+//config:      stty is used to change and print terminal line settings.
 
-   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
+//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]..."
@@ -32,6 +41,7 @@
 //usage:     "\n       [SETTING]       See manpage"
 
 #include "libbb.h"
+#include "common_bufsiz.h"
 
 #ifndef _POSIX_VDISABLE
 # define _POSIX_VDISABLE ((unsigned char) 0)
 #ifndef CRDLY
 # define CRDLY 0
 #endif
+#ifndef CMSPAR
+# define CMSPAR 0
+#endif
 #ifndef CRTSCTS
 # define CRTSCTS 0
 #endif
@@ -317,7 +330,7 @@ enum {
 #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 )
@@ -343,6 +356,9 @@ static const char mode_name[] =
 #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)
@@ -467,6 +483,10 @@ static const char mode_name[] =
 #if ECHOKE
        MI_ENTRY("echoke",   local,       SANE_SET   | REV,  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
        ;
 
@@ -500,6 +520,9 @@ static const struct mode_info mode_info[] = {
 #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)
@@ -624,6 +647,10 @@ static const struct mode_info mode_info[] = {
 #if ECHOKE
        MI_ENTRY("echoke",   local,       SANE_SET   | REV,  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
 };
 
@@ -680,7 +707,7 @@ enum {
 #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  )
@@ -722,7 +749,7 @@ static const char control_name[] =
 #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   )
@@ -773,44 +800,15 @@ struct globals {
        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 void set_speed_or_die(enum speed_setting type, const char *arg,
                                        struct termios *mode)
 {
@@ -856,10 +854,11 @@ static void wrapf(const char *message, ...)
                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);
@@ -1038,6 +1037,9 @@ static void do_display(const struct termios *mode, int all)
 #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)
@@ -1051,8 +1053,12 @@ static void do_display(const struct termios *mode, int all)
                        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)
@@ -1428,7 +1434,7 @@ int stty_main(int argc UNUSED_PARAM, char **argv)
                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;
        }