/* * This is the Minix implementation of stty. * * Author: Andy Tanenbaum * Adapted to POSIX 1003.2 by: Philip Homburg * * Copyright (c) 1987,1997, 2006, Vrije Universiteit, Amsterdam, The * Netherlands All rights reserved. Redistribution and use of the MINIX 3 * 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 the Vrije Universiteit * 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. Any deviations from these conditions require * written permission from the copyright holder in advance */ #ifdef __minix_vmd #define _MINIX_SOURCE #endif #include #include #include #include #include #include #include #ifdef __minix #include #include #endif #ifdef __NBSD_LIBC #include #endif /* Default settings, the Minix ones are defined in */ #ifndef TCTRL_DEF #define TCTRL_DEF (PARENB | CREAD | CS7) #endif #ifndef TSPEED_DEF #define TSPEED_DEF B1200 #endif #ifndef TINPUT_DEF #define TINPUT_DEF (BRKINT | IGNPAR | ISTRIP | ICRNL) #endif #ifndef TOUTPUT_DEF #define TOUTPUT_DEF OPOST #endif #ifndef TLOCAL_DEF #define TLOCAL_DEF (ISIG | IEXTEN | ICANON | ECHO | ECHOE) #endif #ifndef TEOF_DEF #define TEOF_DEF '\4' /* ^D */ #endif #ifndef TEOL_DEF #ifdef _POSIX_VDISABLE #define TEOL_DEF _POSIX_VDISABLE #else #define TEOL_DEF '\n' #endif #endif #ifndef TERASE_DEF #define TERASE_DEF '\10' /* ^H */ #endif #ifndef TINTR_DEF #define TINTR_DEF '\177' /* ^? */ #endif #ifndef TKILL_DEF #define TKILL_DEF '\25' /* ^U */ #endif #ifndef TQUIT_DEF #define TQUIT_DEF '\34' /* ^\ */ #endif #ifndef TSUSP_DEF #define TSUSP_DEF '\32' /* ^Z */ #endif #ifndef TSTART_DEF #define TSTART_DEF '\21' /* ^Q */ #endif #ifndef TSTOP_DEF #define TSTOP_DEF '\23' /* ^S */ #endif #ifndef TMIN_DEF #define TMIN_DEF 1 #endif #ifndef TTIME_DEF #define TTIME_DEF 0 #endif char *prog_name; struct termios termios; int column= 0, max_column=80; /* Assume 80 character terminals. */ #ifdef __minix struct winsize winsize; #endif #define PROTO(a) a int main PROTO(( int argc, char **argv )); void report PROTO(( int flags )); int option PROTO(( char *opt, char *next )); int match PROTO(( char *s1, char *s2 )); void prctl PROTO(( int c )); speed_t long2speed PROTO(( long num )); long speed2long PROTO(( unsigned long speed )); void print_flags PROTO(( unsigned long flags, unsigned long flag, unsigned long def, char *string, int all )); void output PROTO(( char *s )); void do_print_char PROTO(( unsigned chr, unsigned def, char *name, int all )); void do_print_num PROTO(( unsigned num, unsigned def, char *name, int all )); void set_saved_settings PROTO(( char *opt )); void set_control PROTO(( int option, char *value )); void set_min_tim PROTO(( 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))) int main(argc, argv) 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 __minix 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] : ""); #ifdef __minix 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 if (tcsetattr(0, TCSANOW, &termios) == -1) { fprintf(stderr, "%s: can't set terminal parameters to stdin: %s\n", prog_name, strerror(errno)); exit(1); } exit(0); } void report(flags) int flags; { int i, all; tcflag_t c_cflag, c_iflag, c_oflag, c_lflag; 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(speed) 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(flags, flag, def, string, all) unsigned long flags; unsigned long flag; unsigned long def; char *string; int all; { if (!(flags & flag)) { if (all || (def & flag)) output(string); return; } string++; if (all || !(def & flag)) output(string); } void output(s) 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(chr, def, name, all) unsigned chr; unsigned def; 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(num, def, name, all) unsigned num; unsigned def; char *name; int all; { char line[20]; if (!all && num == def) return; sprintf(line, "%s = %u", name, num); output(line); } void set_saved_settings(opt) 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(option, value) 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; } /* * $PchId: stty.c,v 1.7 2001/05/02 15:04:42 philip Exp $ */