⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stty.c

📁 手机嵌入式Linux下可用的busybox源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* vi: set sw=4 ts=4: *//* stty -- change and print terminal line settings   Copyright (C) 1990-1999 Free Software Foundation, Inc.   This program is free software; you can redistribute it and/or modify   it under the terms of the GNU General Public License as published by   the Free Software Foundation; either version 2, or (at your option)   any later version.   You should have received a copy of the GNU General Public License   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] [-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   *///#define TEST#include <termios.h>#include <sys/ioctl.h>#include <getopt.h>#include <sys/param.h>#include <unistd.h>#ifndef STDIN_FILENO# define STDIN_FILENO 0#endif#ifndef STDOUT_FILENO# define STDOUT_FILENO 1#endif#include <stdlib.h>#include <string.h>#include <assert.h>#include <ctype.h>#include <errno.h>#include <limits.h>#include <memory.h>#include <fcntl.h>#include "busybox.h"#define STREQ(a, b) (strcmp ((a), (b)) == 0)#ifndef _POSIX_VDISABLE# define _POSIX_VDISABLE ((unsigned char) 0)#endif#define Control(c) ((c) & 0x1f)/* Canonical values for control characters. */#ifndef CINTR# define CINTR Control ('c')#endif#ifndef CQUIT# define CQUIT 28#endif#ifndef CERASE# define CERASE 127#endif#ifndef CKILL# define CKILL Control ('u')#endif#ifndef CEOF# define CEOF Control ('d')#endif#ifndef CEOL# define CEOL _POSIX_VDISABLE#endif#ifndef CSTART# define CSTART Control ('q')#endif#ifndef CSTOP# define CSTOP Control ('s')#endif#ifndef CSUSP# define CSUSP Control ('z')#endif#if defined(VEOL2) && !defined(CEOL2)# define CEOL2 _POSIX_VDISABLE#endif/* ISC renamed swtch to susp for termios, but we'll accept either name.  */#if defined(VSUSP) && !defined(VSWTCH)# define VSWTCH VSUSP# define CSWTCH CSUSP#endif#if defined(VSWTCH) && !defined(CSWTCH)# define CSWTCH _POSIX_VDISABLE#endif/* SunOS 5.3 loses (^Z doesn't work) if `swtch' is the same as `susp'.   So the default is to disable `swtch.'  */#if defined (__sparc__) && defined (__svr4__)# undef CSWTCH# define CSWTCH _POSIX_VDISABLE#endif#if defined(VWERSE) && !defined (VWERASE)       /* AIX-3.2.5 */# define VWERASE VWERSE#endif#if defined(VDSUSP) && !defined (CDSUSP)# define CDSUSP Control ('y')#endif#if !defined(VREPRINT) && defined(VRPRNT)       /* Irix 4.0.5 */# define VREPRINT VRPRNT#endif#if defined(VREPRINT) && !defined(CRPRNT)# define CRPRNT Control ('r')#endif#if defined(VWERASE) && !defined(CWERASE)# define CWERASE Control ('w')#endif#if defined(VLNEXT) && !defined(CLNEXT)# define CLNEXT Control ('v')#endif#if defined(VDISCARD) && !defined(VFLUSHO)# define VFLUSHO VDISCARD#endif#if defined(VFLUSH) && !defined(VFLUSHO)        /* Ultrix 4.2 */# define VFLUSHO VFLUSH#endif#if defined(CTLECH) && !defined(ECHOCTL)        /* Ultrix 4.3 */# define ECHOCTL CTLECH#endif#if defined(TCTLECH) && !defined(ECHOCTL)       /* Ultrix 4.2 */# define ECHOCTL TCTLECH#endif#if defined(CRTKIL) && !defined(ECHOKE)         /* Ultrix 4.2 and 4.3 */# define ECHOKE CRTKIL#endif#if defined(VFLUSHO) && !defined(CFLUSHO)# define CFLUSHO Control ('o')#endif#if defined(VSTATUS) && !defined(CSTATUS)# define CSTATUS Control ('t')#endif/* Which speeds to set.  */enum speed_setting {	input_speed, output_speed, both_speeds};/* What to output and how.  */enum output_type {	changed, all, recoverable       /* Default, -a, -g.  */};/* Which member(s) of `struct termios' a mode uses.  */enum mode_type {	control, input, output, local, combination};static const char evenp     [] = "evenp";static const char raw       [] = "raw";static const char stty_min  [] = "min";static const char stty_time [] = "time";static const char stty_swtch[] = "swtch";static const char stty_eol  [] = "eol";static const char stty_eof  [] = "eof";static const char parity    [] = "parity";static const char stty_oddp [] = "oddp";static const char stty_nl   [] = "nl";static const char stty_ek   [] = "ek";static const char stty_sane [] = "sane";static const char cbreak    [] = "cbreak";static const char stty_pass8[] = "pass8";static const char litout    [] = "litout";static const char cooked    [] = "cooked";static const char decctlq   [] = "decctlq";static const char stty_tabs [] = "tabs";static const char stty_lcase[] = "lcase";static const char stty_LCASE[] = "LCASE";static const char stty_crt  [] = "crt";static const char stty_dec  [] = "dec";/* Flags for `struct mode_info'. */#define SANE_SET 1              /* Set in `sane' mode.                  */#define SANE_UNSET 2            /* Unset in `sane' mode.                */#define REV 4                   /* Can be turned off by prepending `-'. */#define OMIT 8                  /* Don't display value.                 *//* Each mode.  */struct mode_info {	const char *name;       /* Name given on command line.           */	enum mode_type type;    /* Which structure element to change.    */	char flags;             /* Setting and display options.          */	unsigned long bits;     /* Bits to set for this mode.            */	unsigned long mask;     /* Other bits to turn off for this mode. */};static const struct  mode_info mode_info[] = {	{"parenb",   control,     REV,               PARENB,     0 },	{"parodd",   control,     REV,               PARODD,     0 },	{"cs5",      control,     0,                 CS5,     CSIZE},	{"cs6",      control,     0,                 CS6,     CSIZE},	{"cs7",      control,     0,                 CS7,     CSIZE},	{"cs8",      control,     0,                 CS8,     CSIZE},	{"hupcl",    control,     REV,               HUPCL,      0 },	{"hup",      control,     REV        | OMIT, HUPCL,      0 },	{"cstopb",   control,     REV,               CSTOPB,     0 },	{"cread",    control,     SANE_SET   | REV,  CREAD,      0 },	{"clocal",   control,     REV,               CLOCAL,     0 },#ifdef CRTSCTS	{"crtscts",  control,     REV,               CRTSCTS,    0 },#endif	{"ignbrk",   input,       SANE_UNSET | REV,  IGNBRK,     0 },	{"brkint",   input,       SANE_SET   | REV,  BRKINT,     0 },	{"ignpar",   input,       REV,               IGNPAR,     0 },	{"parmrk",   input,       REV,               PARMRK,     0 },	{"inpck",    input,       REV,               INPCK,      0 },	{"istrip",   input,       REV,               ISTRIP,     0 },	{"inlcr",    input,       SANE_UNSET | REV,  INLCR,      0 },	{"igncr",    input,       SANE_UNSET | REV,  IGNCR,      0 },	{"icrnl",    input,       SANE_SET   | REV,  ICRNL,      0 },	{"ixon",     input,       REV,               IXON,       0 },	{"ixoff",    input,       SANE_UNSET | REV,  IXOFF,      0 },	{"tandem",   input,       REV        | OMIT, IXOFF,      0 },#ifdef IUCLC	{"iuclc",    input,       SANE_UNSET | REV,  IUCLC,      0 },#endif#ifdef IXANY	{"ixany",    input,       SANE_UNSET | REV,  IXANY,      0 },#endif#ifdef IMAXBEL	{"imaxbel",  input,       SANE_SET   | REV,  IMAXBEL,    0 },#endif	{"opost",    output,      SANE_SET   | REV,  OPOST,      0 },#ifdef OLCUC	{"olcuc",    output,      SANE_UNSET | REV,  OLCUC,      0 },#endif#ifdef OCRNL	{"ocrnl",    output,      SANE_UNSET | REV,  OCRNL,      0 },#endif#ifdef ONLCR	{"onlcr",    output,      SANE_SET   | REV,  ONLCR,      0 },#endif#ifdef ONOCR	{"onocr",    output,      SANE_UNSET | REV,  ONOCR,      0 },#endif#ifdef ONLRET	{"onlret",   output,      SANE_UNSET | REV,  ONLRET,     0 },#endif#ifdef OFILL	{"ofill",    output,      SANE_UNSET | REV,  OFILL,      0 },#endif#ifdef OFDEL	{"ofdel",    output,      SANE_UNSET | REV,  OFDEL,      0 },#endif#ifdef NLDLY	{"nl1",      output,      SANE_UNSET,        NL1,     NLDLY},	{"nl0",      output,      SANE_SET,          NL0,     NLDLY},#endif#ifdef CRDLY	{"cr3",      output,      SANE_UNSET,        CR3,     CRDLY},	{"cr2",      output,      SANE_UNSET,        CR2,     CRDLY},	{"cr1",      output,      SANE_UNSET,        CR1,     CRDLY},	{"cr0",      output,      SANE_SET,          CR0,     CRDLY},#endif#ifdef TABDLY	{"tab3",     output,      SANE_UNSET,        TAB3,   TABDLY},	{"tab2",     output,      SANE_UNSET,        TAB2,   TABDLY},	{"tab1",     output,      SANE_UNSET,        TAB1,   TABDLY},	{"tab0",     output,      SANE_SET,          TAB0,   TABDLY},#else# ifdef OXTABS	{"tab3",     output,      SANE_UNSET,        OXTABS,     0 },# endif#endif#ifdef BSDLY	{"bs1",      output,      SANE_UNSET,        BS1,     BSDLY},	{"bs0",      output,      SANE_SET,          BS0,     BSDLY},#endif#ifdef VTDLY	{"vt1",      output,      SANE_UNSET,        VT1,     VTDLY},	{"vt0",      output,      SANE_SET,          VT0,     VTDLY},#endif#ifdef FFDLY	{"ff1",      output,      SANE_UNSET,        FF1,     FFDLY},	{"ff0",      output,      SANE_SET,          FF0,     FFDLY},#endif	{"isig",     local,       SANE_SET   | REV,  ISIG,       0 },	{"icanon",   local,       SANE_SET   | REV,  ICANON,     0 },#ifdef IEXTEN	{"iexten",   local,       SANE_SET   | REV,  IEXTEN,     0 },#endif	{"echo",     local,       SANE_SET   | REV,  ECHO,       0 },	{"echoe",    local,       SANE_SET   | REV,  ECHOE,      0 },	{"crterase", local,       REV        | OMIT, ECHOE,      0 },	{"echok",    local,       SANE_SET   | REV,  ECHOK,      0 },	{"echonl",   local,       SANE_UNSET | REV,  ECHONL,     0 },	{"noflsh",   local,       SANE_UNSET | REV,  NOFLSH,     0 },#ifdef XCASE	{"xcase",    local,       SANE_UNSET | REV,  XCASE,      0 },#endif#ifdef TOSTOP	{"tostop",   local,       SANE_UNSET | REV,  TOSTOP,     0 },#endif#ifdef ECHOPRT	{"echoprt",  local,       SANE_UNSET | REV,  ECHOPRT,    0 },	{"prterase", local,       REV | OMIT,        ECHOPRT,    0 },#endif#ifdef ECHOCTL	{"echoctl",  local,       SANE_SET   | REV,  ECHOCTL,    0 },	{"ctlecho",  local,       REV        | OMIT, ECHOCTL,    0 },#endif#ifdef ECHOKE	{"echoke",   local,       SANE_SET   | REV,  ECHOKE,     0 },	{"crtkill",  local,       REV        | OMIT, ECHOKE,     0 },#endif	{evenp,      combination, REV        | OMIT, 0,          0 },	{parity,     combination, REV        | OMIT, 0,          0 },	{stty_oddp,  combination, REV        | OMIT, 0,          0 },	{stty_nl,    combination, REV        | OMIT, 0,          0 },	{stty_ek,    combination, OMIT,              0,          0 },	{stty_sane,  combination, OMIT,              0,          0 },	{cooked,     combination, REV        | OMIT, 0,          0 },	{raw,        combination, REV        | OMIT, 0,          0 },	{stty_pass8, combination, REV        | OMIT, 0,          0 },	{litout,     combination, REV        | OMIT, 0,          0 },	{cbreak,     combination, REV        | OMIT, 0,          0 },#ifdef IXANY	{decctlq,    combination, REV        | OMIT, 0,          0 },#endif#if defined (TABDLY) || defined (OXTABS)	{stty_tabs,  combination, REV        | OMIT, 0,          0 },#endif#if defined(XCASE) && defined(IUCLC) && defined(OLCUC)	{stty_lcase, combination, REV        | OMIT, 0,          0 },	{stty_LCASE, combination, REV        | OMIT, 0,          0 },#endif	{stty_crt,   combination, OMIT,              0,          0 },	{stty_dec,   combination, OMIT,              0,          0 },};static const int NUM_mode_info =	(sizeof(mode_info) / sizeof(struct mode_info));/* Control character settings.  */struct control_info {	const char *name;                       /* Name given on command line.  */	unsigned char saneval;          /* Value to set for `stty sane'.  */	int offset;                                     /* Offset in c_cc.  */};/* Control characters. */static const struct  control_info control_info[] = {	{"intr",     CINTR,   VINTR},	{"quit",     CQUIT,   VQUIT},	{"erase",    CERASE,  VERASE},	{"kill",     CKILL,   VKILL},	{stty_eof,   CEOF,    VEOF},	{stty_eol,   CEOL,    VEOL},#ifdef VEOL2	{"eol2",     CEOL2,   VEOL2},#endif#ifdef VSWTCH	{stty_swtch, CSWTCH,  VSWTCH},#endif	{"start",    CSTART,  VSTART},	{"stop",     CSTOP,   VSTOP},	{"susp",     CSUSP,   VSUSP},#ifdef VDSUSP	{"dsusp",    CDSUSP,  VDSUSP},#endif#ifdef VREPRINT	{"rprnt",    CRPRNT,  VREPRINT},#endif#ifdef VWERASE	{"werase",   CWERASE, VWERASE},#endif#ifdef VLNEXT	{"lnext",    CLNEXT,  VLNEXT},#endif#ifdef VFLUSHO	{"flush",    CFLUSHO, VFLUSHO},#endif#ifdef VSTATUS	{"status",   CSTATUS, VSTATUS},#endif	/* These must be last because of the display routines. */	{stty_min,   1,       VMIN},	{stty_time,  0,       VTIME},};static const int NUM_control_info =	(sizeof(control_info) / sizeof(struct control_info));static const char *  visible(unsigned int ch);static unsigned long baud_to_value(speed_t speed);static int           recover_mode(char *arg, struct termios *mode);static int           screen_columns(void);static int           set_mode(const struct mode_info *info,					int reversed, struct termios *mode);static speed_t       string_to_baud(const char *arg);static tcflag_t*     mode_type_flag(enum mode_type type, struct termios *mode);static void          display_all(struct termios *mode, int fd,					const char *device_name);static void          display_changed(struct termios *mode);static void          display_recoverable(struct termios *mode);static void          display_settings(enum output_type output_type,					struct termios *mode, int fd,					const char *device_name);static void          display_speed(struct termios *mode, int fancy);static void          display_window_size(int fancy, int fd,					const char *device_name);static void          sane_mode(struct termios *mode);static void          set_control_char(const struct control_info *info,					const char *arg, struct termios *mode);static void          set_speed(enum speed_setting type,					const char *arg, struct termios *mode);static void          set_window_size(int rows, int cols, int fd,					const char *device_name);/* The width of the screen, for output wrapping. */static int max_col;/* Current position, to know when to wrap. */static int current_col;/* Print format string MESSAGE and optional args.   Wrap to next line first if it won't fit.   Print a space first unless MESSAGE will start a new line. */static void wrapf(const char *message, ...){	va_list args;	char buf[1024];                 /* Plenty long for our needs. */	int buflen;	va_start(args, message);	vsprintf(buf, message, args);	va_end(args);	buflen = strlen(buf);	if (current_col + (current_col > 0) + buflen >= max_col) {		putchar('\n');		current_col = 0;	}	if (current_col > 0) {		putchar(' ');		current_col++;	}	fputs(buf, stdout);	current_col += buflen;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -