Patch generated: on Wed Apr 1 10:54:52 EST 1998 by tytso@rsts-11.mit.edu against shellutils version 1.16 =================================================================== RCS file: ChangeLog,v retrieving revision 1.1 diff -u -r1.1 ChangeLog --- ChangeLog 1998/04/01 14:39:04 1.1 +++ ChangeLog 1998/04/01 15:54:50 @@ -1,3 +1,18 @@ +1998-03-31 Theodore Ts'o + + * src/stty.c (main): Fix broken options parsing that worked only + by serendipity (getopt_long_only already parsed short options; no + need to parse them again manually!). Add support for the --file + option, which allows the user to specify the device whose line + settings are to be set. This is necessary because POSIX ttys will + block waiting for carrier detect to go high if CLOCAL is not set, + unless the device is opened with the O_NONBLOCK flag. + Unfortunately, the shell doesn't use this flag, so users lose. + Opening the device in stty is the easist way to fix this. We use + the same option (-f) used by the BSDI stty. + (speeds): Add support for 230400 and 460800 line speeds, which are + supported by Linux. + Sun Jan 26 12:51:05 1997 Jim Meyering * Version 1.16. =================================================================== RCS file: src/RCS/stty.c,v retrieving revision 1.1 diff -u -r1.1 src/stty.c --- src/stty.c 1998/04/01 07:01:02 1.1 +++ src/stty.c 1998/04/01 15:16:38 @@ -15,15 +15,16 @@ along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* Usage: stty [-ag] [--all] [--save] [setting...] +/* Usage: stty [-ag] [--all] [--save] [-f device] [--file=device] [setting...] Options: -a, --all Write all current settings to stdout in human-readable form. -g, --save Write all current settings to stdout in stty-readable form. + -f, --file 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 stdin. + are done on the specified device, or stdin if none was specified. David MacKenzie */ @@ -401,19 +402,23 @@ static speed_t string_to_baud __P ((const char *arg)); static tcflag_t *mode_type_flag __P ((enum mode_type type, struct termios *mode)); -static void display_all __P ((struct termios *mode)); +static void display_all __P ((struct termios *mode, int fd, + const char *device)); static void display_changed __P ((struct termios *mode)); static void display_recoverable __P ((struct termios *mode)); static void display_settings __P ((enum output_type output_type, - struct termios *mode)); + struct termios *mode, int fd, + const char *device)); static void display_speed __P ((struct termios *mode, int fancy)); -static void display_window_size __P ((int fancy)); +static void display_window_size __P ((int fancy, int fd, + const char *device)); static void sane_mode __P ((struct termios *mode)); static void set_control_char __P ((struct control_info *info, const char *arg, struct termios *mode)); static void set_speed __P ((enum speed_setting type, const char *arg, struct termios *mode)); -static void set_window_size __P ((int rows, int cols)); +static void set_window_size __P ((int rows, int cols, int fd, + const char *device)); /* The width of the screen, for output wrapping. */ static int max_col; @@ -425,6 +430,7 @@ { {"all", no_argument, NULL, 'a'}, {"save", no_argument, NULL, 'g'}, + {"file", required_argument, NULL, 'f'}, {NULL, 0, NULL, 0} }; @@ -476,15 +482,17 @@ else { printf (_("\ -Usage: %s [SETTING]...\n\ - or: %s OPTION\n\ +Usage: %s [-f device] [--file=device] [SETTING]...\n\ + or: %s [-f device] [--file=device] [-a|--all]\n\ + or: %s [-f device] [--file=device] [-g|--save]\n\ "), - program_name, program_name); + program_name, program_name, program_name); printf (_("\ Print or change terminal characteristics.\n\ \n\ -a, --all print all current settings in human-readable form\n\ -g, --save print all current settings in a stty-readable form\n\ + -f, --file open and use the specified device instead of stdin\n\ --help display this help and exit\n\ --version output version information and exit\n\ \n\ @@ -648,6 +656,29 @@ exit (status); } +/* + * Return 1 if the string only contains valid options + */ +int valid_options(char *opt, const char *valid_opts, + const char *valid_arg_opts) +{ + char ch; + + if (*opt++ != '-') + return 0; + + while (ch = *opt) { + opt++; + if (strchr(valid_opts, ch)) + continue; + if (strchr(valid_arg_opts, ch)) + return 1; + return 0; + } + return 1; +} + + int main (int argc, char **argv) { @@ -659,6 +690,10 @@ int verbose_output; int recoverable_output; int k; + int noargs = 1; + char *file_name = NULL, *cp; + int fd, fdflags; + const char *device_name; program_name = argv[0]; setlocale (LC_ALL, ""); @@ -673,7 +708,7 @@ /* Recognize the long options only. */ opterr = 0; - while ((optc = getopt_long_only (argc, argv, "ag", longopts, (int *) 0)) + while ((optc = getopt_long_only (argc, argv, "agf:", longopts, (int *) 0)) != EOF) { switch (optc) @@ -688,43 +723,50 @@ output_type = recoverable; break; + case 'f': + file_name = optarg; + break; + default: break; } } - /* Recognize short options and combinations: -a, -g, -ag, and -ga. - They need not precede non-options. We cannot use GNU getopt because - it would treat -tabs and -ixany as uses of the -a option. */ - for (k = optind; k < argc; k++) + /* Clear out the options that have been parsed */ + for (k = 1; k < argc; k++) { - if (argv[k][0] == '-') - { - if (argv[k][1] == 'a' - && argv[k][2] == '\0') - { - ++optind; - verbose_output = 1; - } - else if (argv[k][1] == 'g' - && argv[k][2] == '\0') - { - ++optind; - recoverable_output = 1; - } - else if ((argv[k][1] == 'g' - && argv[k][2] == 'a' - && argv[k][3] == '\0') - || (argv[k][1] == 'a' - && argv[k][2] == 'g' - && argv[k][3] == '\0')) - { - ++optind; - verbose_output = 1; - recoverable_output = 1; - } - } - } + if (!argv[k]) + continue; + if (!strcmp(argv[k], "--file")) + { + argv[k+1] = 0; + argv[k] = 0; + } + else if (!strcmp(argv[k], "--all") || + !strcmp(argv[k], "--save") || + !strncmp(argv[k], "--file=", 7)) + argv[k] = 0; + else if (valid_options(argv[k], "ag", "f")) { + if (!strcmp(argv[k], "-file") || + argv[k][strlen(argv[k])-1] == 'f') + argv[k+1] = 0; + argv[k] = 0; + } else + noargs = 0; + } + +#if 0 + /* For debugging purposes, print out the argument */ + for (k=1; k < argc; k++) { + if (argv[k]) + printf("arg: %s\n", argv[k]); + else + printf("arg: none\n"); + } + printf("File_name = %s\n", file_name ? file_name : "NONE"); + printf("noargs = %d\n", noargs); +#endif + /* Specifying both -a and -g gets an error. */ if (verbose_output && recoverable_output) @@ -733,20 +775,35 @@ mutually exclusive")); /* Specifying any other arguments with -a or -g gets an error. */ - if (argc - optind > 0 && (verbose_output || recoverable_output)) + if (!noargs && (verbose_output || recoverable_output)) error (2, 0, _("when specifying an output style, modes may not be set")); + if (file_name) + { + device_name = file_name; + fd = open(device_name, O_RDONLY|O_NONBLOCK); + if (fd < 0) + error(1, errno, device_name); + if ((fdflags = fcntl(fd, F_GETFL)) == -1 + || fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0) + error(1, errno, _("couldn't reset non-blocking mode")); + } else + { + fd = 0; + device_name = _("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 (0, &mode)) - error (1, errno, _("standard input")); + if (tcgetattr (fd, &mode)) + error (1, errno, device_name); - if (verbose_output || recoverable_output || argc == 1) + if (verbose_output || recoverable_output || noargs) { max_col = screen_columns (); current_col = 0; - display_settings (output_type, &mode); + display_settings (output_type, &mode, fd, device_name); exit (0); } @@ -759,6 +816,11 @@ int reversed = 0; int i; + if (argv[k] == 0) { + k++; + continue; + } + if (argv[k][0] == '-') { ++argv[k]; @@ -832,7 +894,8 @@ usage (1); } ++k; - set_window_size ((int) integer_arg (argv[k]), -1); + set_window_size ((int) integer_arg (argv[k]), -1, + fd, device_name); } else if (!strcmp (argv[k], "cols") || !strcmp (argv[k], "columns")) @@ -843,13 +906,14 @@ usage (1); } ++k; - set_window_size (-1, (int) integer_arg (argv[k])); + set_window_size (-1, (int) integer_arg (argv[k]), + fd, device_name); } else if (!strcmp (argv[k], "size")) { max_col = screen_columns (); current_col = 0; - display_window_size (0); + display_window_size (0, fd, device_name); } #endif #ifdef HAVE_C_LINE @@ -893,8 +957,8 @@ { struct termios new_mode; - if (tcsetattr (0, TCSADRAIN, &mode)) - error (1, errno, _("standard input")); + if (tcsetattr (fd, TCSADRAIN, &mode)) + error (1, errno, device_name); /* POSIX (according to Zlotnick's book) tcsetattr returns zero if it performs *any* of the requested operations. This means it @@ -906,8 +970,8 @@ /* Initialize to all zeroes so there is no risk memcmp will report a spurious difference in an uninitialized portion of the structure. */ memset (&new_mode, 0, sizeof (new_mode)); - if (tcgetattr (0, &new_mode)) - error (1, errno, _("standard input")); + if (tcgetattr (fd, &new_mode)) + error (1, errno, device_name); /* Normally, one shouldn't use memcmp to compare structures that may have `holes' containing uninitialized data, but we have been @@ -934,7 +998,8 @@ { size_t i; error (1, 0, - _("standard input: unable to perform all requested operations")); + _("%s: unable to perform all requested operations"), + device_name); printf (_("new_mode: mode\n")); for (i = 0; i < sizeof (new_mode); i++) printf ("0x%02x: 0x%02x\n", @@ -1201,14 +1266,14 @@ } static void -set_window_size (int rows, int cols) +set_window_size (int rows, int cols, int fd, const char *device) { struct winsize win; - if (get_win_size (STDIN_FILENO, &win)) + if (get_win_size (fd, &win)) { if (errno != EINVAL) - error (1, errno, _("standard input")); + error (1, errno, device); memset (&win, 0, sizeof (win)); } @@ -1249,28 +1314,28 @@ win.ws_row = 1; win.ws_col = 1; - if (ioctl (STDIN_FILENO, TIOCSWINSZ, (char *) &win)) - error (1, errno, _("standard input")); + if (ioctl (fd, TIOCSWINSZ, (char *) &win)) + error (1, errno, device); - if (ioctl (STDIN_FILENO, TIOCSSIZE, (char *) &ttysz)) - error (1, errno, _("standard input")); + if (ioctl (fd, TIOCSSIZE, (char *) &ttysz)) + error (1, errno, device); return; } # endif - if (ioctl (STDIN_FILENO, TIOCSWINSZ, (char *) &win)) - error (1, errno, _("standard input")); + if (ioctl (fd, TIOCSWINSZ, (char *) &win)) + error (1, errno, device); } static void -display_window_size (int fancy) +display_window_size (int fancy, int fd, const char *device) { struct winsize win; - if (get_win_size (STDIN_FILENO, &win)) + if (get_win_size (fd, &win)) { if (errno != EINVAL) - error (1, errno, _("standard input")); + error (1, errno, device); if (!fancy) error (1, 0, _("no size information for this device")); } @@ -1331,7 +1396,8 @@ } static void -display_settings (enum output_type output_type, struct termios *mode) +display_settings (enum output_type output_type, struct termios *mode, + int fd, const char *device) { switch (output_type) { @@ -1340,7 +1406,7 @@ break; case all: - display_all (mode); + display_all (mode, fd, device); break; case recoverable: @@ -1434,7 +1500,7 @@ } static void -display_all (struct termios *mode) +display_all (struct termios *mode, int fd, const char *device) { int i; tcflag_t *bitsp; @@ -1443,7 +1509,7 @@ display_speed (mode, 1); #ifdef TIOCGWINSZ - display_window_size (1); + display_window_size (1, fd, device); #endif #ifdef HAVE_C_LINE wrapf ("line = %d;", mode->c_line); @@ -1590,6 +1656,12 @@ #endif #ifdef B115200 {"115200", B115200, 115200}, +#endif +#ifdef B230400 + {"230400", B230400, 230400}, +#endif +#ifdef B460800 + {"460800", B460800, 460800}, #endif {NULL, 0, 0} }; =================================================================== RCS file: man/RCS/stty.1,v retrieving revision 1.1 diff -u -r1.1 man/stty.1 --- man/stty.1 1998/04/01 09:26:14 1.1 +++ man/stty.1 1998/04/01 09:28:09 @@ -2,10 +2,12 @@ .SH NAME stty \- change and print terminal line settings .SH SYNOPSIS -.B stty +.B stty +[-f device] [--file=device] [setting...] .br .B stty +[-f device] [--file=device] {\-a,\-\-all,\-g,\-\-help,\-\-save,\-\-version} .SH DESCRIPTION This documentation is no longer being maintained and may be inaccurate =================================================================== RCS file: doc/sh-utils.texi,v retrieving revision 1.1 diff -u -r1.1 doc/sh-utils.texi --- doc/sh-utils.texi 1998/04/01 14:29:18 1.1 +++ doc/sh-utils.texi 1998/04/01 14:38:06 @@ -1109,15 +1109,16 @@ Synopses: @example -stty [@var{setting}]@dots{} +stty [@var{option}] [@var{setting}]@dots{} stty [@var{option}] @end example -If given no arguments, @code{stty} prints the baud rate, line +If given no line settings, @code{stty} prints the baud rate, line discipline number (on systems that support it), and line settings that have been changed from the values set by @samp{stty sane}. -Mode reading and setting are performed on the tty line connected to -standard input. +By default, mode reading and setting are performed on the tty line +connected to standard input, although this can be modified by the +@samp{--file} option. @code{stty} accepts many non-option arguments that change aspects of the terminal line operation, as described below. @@ -1129,7 +1130,19 @@ @itemx --all @opindex -a @opindex --all -Print all current settings in human-readable form. +Print all current settings in human-readable form. This option may not +be used in combination with any line settings. + +@item -f @var{device} +@itemx --file @var{device} +@opindex -f +@opindex --file +Set the line opened by the filename specified in @var{device} instead of +the tty line connected to standard input. This option is necessary +because opening a POSIX tty requires the @code{O_NONDELAY} flag to prevent +a POSIX tty from blocking until the carrier detect line is high if +the @code{clocal} flag is not set. Hence, it is not possible to allow +the shell to open the device in the traditional manner. @item -g @itemx --save @@ -1137,7 +1150,8 @@ @opindex --save @cindex machine-readable @code{stty} output Print all current settings in a form that can be used as an argument to -another @code{stty} command to restore the current settings. +another @code{stty} command to restore the current settings. This option +may not be used in combination with any line settings. @end table