From 2c3359cc22dfc144be1ce83f5a144cd0222e8ad6 Mon Sep 17 00:00:00 2001 From: Alan Cox Date: Tue, 30 Dec 2014 11:33:47 +0000 Subject: [PATCH] utils: Add stty --- Applications/util/Makefile | 1 + Applications/util/stty.c | 1247 ++++++++++++++++++++++++++++++++++++ 2 files changed, 1248 insertions(+) create mode 100644 Applications/util/stty.c diff --git a/Applications/util/Makefile b/Applications/util/Makefile index 6a22d3e4..8308a8bb 100644 --- a/Applications/util/Makefile +++ b/Applications/util/Makefile @@ -70,6 +70,7 @@ SRCS = banner.c \ sleep.c \ ssh.c \ sort.c \ + stty.c \ sum.c \ su.c \ sync.c \ diff --git a/Applications/util/stty.c b/Applications/util/stty.c new file mode 100644 index 00000000..b2aab947 --- /dev/null +++ b/Applications/util/stty.c @@ -0,0 +1,1247 @@ +/* stty - set terminal mode Author: Andy Tanenbaum */ + +/* + Copyright (c) 1987,1997, Prentice Hall + All rights reserved. + + Redistribution and use of the MINIX operating system in source and + binary forms, with or without modification, are permitted provided + that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above + copyright notice, this list of conditions and the following + disclaimer in the documentation and/or other materials provided + with the distribution. + + * Neither the name of Prentice Hall nor the names of the software + authors or contributors may be used to endorse or promote + products derived from this software without specific prior + written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND + CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR + BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE + OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +/* + Adapted to POSIX 1003.2 by Philip Homburg. + */ + + /* + * Tiny changes for use in ELKS by Harry Kalogirou + * + */ + +#include +#include +#include +#include +#include +#include +#include + +/* Default settings */ +#define TCTRL_DEF (PARENB | CREAD | CS7) + +#define TSPEED_DEF B1200 + +#define TINPUT_DEF (BRKINT | IGNPAR | ISTRIP | ICRNL) + +#define TOUTPUT_DEF OPOST + +#define TLOCAL_DEF (ISIG | IEXTEN | ICANON | ECHO | ECHOE) + +#define TEOF_DEF '\4' /* ^D */ + +#define TEOL_DEF _POSIX_VDISABLE + +#define TERASE_DEF '\10' /* ^H */ + +#define TINTR_DEF '\177' /* ^? */ + +#define TKILL_DEF '\25' /* ^U */ + +#define TQUIT_DEF '\34' /* ^\ */ + +#define TSUSP_DEF '\32' /* ^Z */ + +#define TSTART_DEF '\21' /* ^Q */ + +#define TSTOP_DEF '\23' /* ^S */ + +#define TMIN_DEF 1 + +#define TTIME_DEF 0 + +char *prog_name; +struct termios termios; +int column= 0, max_column=80; /* Assume 80 character terminals. */ + +void main(int argc, char **argv); +void report(int flags); +int option(char *opt, char *next); +int match(const char *s1, const char *s2); +void prctl(char c); +speed_t long2speed(long num); +long speed2long(unsigned long speed); +void print_flags(unsigned long flags, unsigned long flag, + unsigned long def, const char *string, int all); +void output(const char *s); +void do_print_char(unsigned chr, unsigned def, const char *name, int all); +void do_print_num(unsigned num, unsigned def, const char *name, int all); +void set_saved_settings(char *opt); +void set_control(int option, char *value); +void set_min_tim(int option, char *value); + +#define print_char(c,d,n,a) (do_print_char((unsigned)(c),(unsigned)(d),(n),(a))) +#define print_num(m,d,n,a) (do_print_num((unsigned)(m),(unsigned)(d),(n),(a))) + +void main(int argc, char *argv[]) +{ + int flags, k; + + prog_name= argv[0]; + flags= 0; + + /* Stty with no arguments just reports on current status. */ + if (tcgetattr(0, &termios) == -1) + { + fprintf(stderr, "%s: can't read ioctl parameters from stdin: %s\n", + prog_name, strerror(errno)); + exit(1); + } +#ifdef TIOCGWINSZ + if (ioctl(0, TIOCGWINSZ, &winsize) == -1) + { + fprintf(stderr, "%s: can't get screen size from stdin: %s\n", + prog_name, strerror(errno)); + exit(1); + } + if (winsize.ws_col != 0) + max_column= winsize.ws_col; +#endif + + if (argc == 2) + { + if (!strcmp(argv[1], "-a")) + flags |= 1; + else if (!strcmp(argv[1], "-g")) + flags |= 2; + } + if (argc == 1 || flags) { + report(flags); + exit(0); + } + + /* Process the options specified. */ + for (k= 1; k < argc; k++) + k += option(argv[k], k+1 < argc ? argv[k+1] : ""); + + if (tcsetattr(0, TCSANOW, &termios) == -1) + { + fprintf(stderr, "%s: can't set terminal parameters to stdin: %s\n", + prog_name, strerror(errno)); + exit(1); + } +#ifdef TIOCSWINSZ + if (ioctl(0, TIOCSWINSZ, &winsize) == -1) + { + fprintf(stderr, "%s: can't set screen size to stdin: %s\n", + prog_name, strerror(errno)); + exit(1); + } +#endif + exit(0); +} + + + +void report(int flags) +{ + int i, all; + tcflag_t c_cflag, c_iflag, c_oflag, c_lflag; + int tmp; + char line[80]; + speed_t ispeed, ospeed; + + if (flags & 2) + { /* We have to write the termios structure in a encoded form + * to stdout. + */ + printf(":%x:%x:%x:%x:%x:%x", termios.c_iflag, termios.c_oflag, + termios.c_cflag, termios.c_lflag, cfgetispeed(&termios), + cfgetospeed(&termios)); + for (i= 0; ins == num) return sp->ts; + } + return -1; +} + +long speed2long(unsigned long speed) +{ + struct s2s *sp; + + for (sp = s2s; sp < s2s + (sizeof(s2s) / sizeof(s2s[0])); sp++) { + if (sp->ts == speed) return sp->ns; + } + return -1; +} + +void print_flags(unsigned long flags, unsigned long flag, unsigned long def, + const char *string, int all) +{ + if (!(flags & flag)) + { + if (all || (def & flag)) + output(string); + return; + } + string++; + if (all || !(def & flag)) + output(string); +} + +void output(const char *s) +{ + int len; + + len= strlen(s); + if (column + len + 3 >= max_column) + { + printf("\n"); + column= 0; + } + if (column) + { + putchar(' '); + column++; + } + fputs(s, stdout); + column += len; +} + +void do_print_char(unsigned chr, unsigned def, const char *name, int all) +{ + char line[20]; + + if (!all && chr == def) + return; + +#ifdef _POSIX_VDISABLE + if (chr == _POSIX_VDISABLE) + sprintf(line, "%s = ", name); + else +#endif + if (chr < ' ') + sprintf(line, "%s = ^%c", name, chr + '@'); + else if (chr == 127) + sprintf(line, "%s = ^?", name); + else + sprintf(line, "%s = %c", name, chr); + output(line); +} + +void do_print_num(unsigned num, unsigned def, const char *name, int all) +{ + char line[20]; + + if (!all && num == def) + return; + sprintf(line, "%s = %u", name, num); + output(line); +} + +void set_saved_settings(char *opt) +{ + long num; + char *check; + tcflag_t c_oflag, c_cflag, c_lflag, c_iflag; + cc_t c_cc[NCCS]; + speed_t ispeed, ospeed; + int i; + + check= opt; + num= strtol(check+1, &check, 16); + if (check[0] != ':') + { + fprintf(stderr, "error in saved settings '%s'\n", opt); + return; + } + c_iflag= num; + + num= strtol(check+1, &check, 16); + if (check[0] != ':') + { + fprintf(stderr, "error in saved settings '%s'\n", opt); + return; + } + c_oflag= num; + + num= strtol(check+1, &check, 16); + if (check[0] != ':') + { + fprintf(stderr, "error in saved settings '%s'\n", opt); + return; + } + c_cflag= num; + + num= strtol(check+1, &check, 16); + if (check[0] != ':') + { + fprintf(stderr, "error in saved settings '%s'\n", opt); + return; + } + c_lflag= num; + + num= strtol(check+1, &check, 16); + if (check[0] != ':') + { + fprintf(stderr, "error in saved settings '%s'\n", opt); + return; + } + ispeed= num; + + num= strtol(check+1, &check, 16); + if (check[0] != ':') + { + fprintf(stderr, "error in saved settings '%s'\n", opt); + return; + } + ospeed= num; + + for(i=0; i= 32) { + fprintf(stderr, "stty: illegal option value: '%s'\n", + value); + return; + } + } else if (strlen(value) == 1) + chr= value[0]; + else { + fprintf(stderr, "stty: illegal option value: '%s'\n", value); + return; + } + +/* assert(option >= 0 && option < NCCS);*/ + termios.c_cc[option]= chr; +} + +void set_min_tim(int option, char *value) +{ + long num; + char *check; + + num= strtol(value, &check, 0); + if (check[0] != '\0') { + fprintf(stderr, "stty: illegal option value: '%s'\n", value); + return; + } + + if ((cc_t)num != num) { + fprintf(stderr, "stty: illegal option value: '%s'\n", value); + return; + } +/* assert(option >= 0 && option < NCCS);*/ + termios.c_cc[option]= num; +} + +/* + * $PchHeader: /mount/hd2/minix/cmd/simple/RCS/stty.c,v 1.4 1995/05/23 08:23:16 philip Exp $ + */ -- 2.34.1