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

📄 stty.c

📁 手机嵌入式Linux下可用的busybox源码
💻 C
📖 第 1 页 / 共 3 页
字号:
static const struct suffix_mult stty_suffixes[] = {	{"b",  512 },	{"k",  1024},	{"B",  1024},	{NULL, 0   }};#ifndef TESTextern int stty_main(int argc, char **argv)#elseextern int main(int argc, char **argv)#endif{	struct termios mode;	enum   output_type output_type;	int    optc;	int    require_set_attr;	int    speed_was_set;	int    verbose_output;	int    recoverable_output;	int    k;	int    noargs = 1;	char * file_name = NULL;	int    fd;	const char *device_name;	output_type = changed;	verbose_output = 0;	recoverable_output = 0;	/* Don't print error messages for unrecognized options.  */	opterr = 0;	while ((optc = getopt(argc, argv, "agF:")) != -1) {		switch (optc) {		case 'a':			verbose_output = 1;			output_type = all;			break;		case 'g':			recoverable_output = 1;			output_type = recoverable;			break;		case 'F':			if (file_name)				error_msg_and_die("only one device may be specified");			file_name = optarg;			break;		default:                /* unrecognized option */			noargs = 0;			break;		}		if (noargs == 0)			break;	}	if (optind < argc)		noargs = 0;	/* Specifying both -a and -g gets an error.  */	if (verbose_output && recoverable_output)		error_msg_and_die ("verbose and stty-readable output styles are mutually exclusive");	/* Specifying any other arguments with -a or -g gets an error.  */	if (!noargs && (verbose_output || recoverable_output))		error_msg_and_die ("modes may not be set when specifying an output style");	/* FIXME: it'd be better not to open the file until we've verified	   that all arguments are valid.  Otherwise, we could end up doing	   only some of the requested operations and then failing, probably	   leaving things in an undesirable state.  */	if (file_name) {		int fdflags;		device_name = file_name;		fd = open(device_name, O_RDONLY | O_NONBLOCK);		if (fd < 0)			perror_msg_and_die("%s", device_name);		if ((fdflags = fcntl(fd, F_GETFL)) == -1			|| fcntl(fd, F_SETFL, fdflags & ~O_NONBLOCK) < 0)			perror_msg_and_die("%s: couldn't reset non-blocking mode",							   device_name);	} 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(fd, &mode))		perror_msg_and_die("%s", device_name);	if (verbose_output || recoverable_output || noargs) {		max_col = screen_columns();		current_col = 0;		display_settings(output_type, &mode, fd, device_name);		return EXIT_SUCCESS;	}	speed_was_set = 0;	require_set_attr = 0;	k = 0;	while (++k < argc) {		int match_found = 0;		int reversed = 0;		int i;		if (argv[k][0] == '-') {			char *find_dev_opt;			++argv[k];     /* Handle "-a", "-ag", "-aF/dev/foo", "-aF /dev/foo", etc.	Find the options that have been parsed.  This is really	gross, but it's needed because stty SETTINGS look like options to	getopt(), so we need to work around things in a really horrible	way.  If any new options are ever added to stty, the short option	MUST NOT be a letter which is the first letter of one of the	possible stty settings.     */			find_dev_opt = strchr(argv[k], 'F'); /* find -*F* */			if(find_dev_opt) {				if(find_dev_opt[1]==0)  /* -*F   /dev/foo */					k++;            /* skip  /dev/foo */				continue;   /* else -*F/dev/foo - no skip */			}			if(argv[k][0]=='a' || argv[k][0]=='g')				continue;			/* Is not options - is reverse params */			reversed = 1;		}		for (i = 0; i < NUM_mode_info; ++i)			if (STREQ(argv[k], mode_info[i].name)) {				match_found = set_mode(&mode_info[i], reversed, &mode);				require_set_attr = 1;				break;			}		if (match_found == 0 && reversed)			error_msg_and_die("invalid argument `%s'", --argv[k]);		if (match_found == 0)			for (i = 0; i < NUM_control_info; ++i)				if (STREQ(argv[k], control_info[i].name)) {					if (k == argc - 1)					    error_msg_and_die("missing argument to `%s'", argv[k]);					match_found = 1;					++k;					set_control_char(&control_info[i], argv[k], &mode);					require_set_attr = 1;					break;				}		if (match_found == 0) {			if (STREQ(argv[k], "ispeed")) {				if (k == argc - 1)				    error_msg_and_die("missing argument to `%s'", argv[k]);				++k;				set_speed(input_speed, argv[k], &mode);				speed_was_set = 1;				require_set_attr = 1;			} else if (STREQ(argv[k], "ospeed")) {				if (k == argc - 1)				    error_msg_and_die("missing argument to `%s'", argv[k]);				++k;				set_speed(output_speed, argv[k], &mode);				speed_was_set = 1;				require_set_attr = 1;			}#ifdef TIOCGWINSZ			else if (STREQ(argv[k], "rows")) {				if (k == argc - 1)				    error_msg_and_die("missing argument to `%s'", argv[k]);				++k;				set_window_size((int) parse_number(argv[k], stty_suffixes),								-1, fd, device_name);			} else if (STREQ(argv[k], "cols") || STREQ(argv[k], "columns")) {				if (k == argc - 1)				    error_msg_and_die("missing argument to `%s'", argv[k]);				++k;				set_window_size(-1,						(int) parse_number(argv[k], stty_suffixes),						fd, device_name);			} else if (STREQ(argv[k], "size")) {				max_col = screen_columns();				current_col = 0;				display_window_size(0, fd, device_name);			}#endif#ifdef HAVE_C_LINE			else if (STREQ(argv[k], "line")) {				if (k == argc - 1)					error_msg_and_die("missing argument to `%s'", argv[k]);				++k;				mode.c_line = parse_number(argv[k], stty_suffixes);				require_set_attr = 1;			}#endif			else if (STREQ(argv[k], "speed")) {				max_col = screen_columns();				display_speed(&mode, 0);			} else if (recover_mode(argv[k], &mode) == 1)				require_set_attr = 1;			else if (string_to_baud(argv[k]) != (speed_t) - 1) {				set_speed(both_speeds, argv[k], &mode);				speed_was_set = 1;				require_set_attr = 1;			} else				error_msg_and_die("invalid argument `%s'", argv[k]);		}	}	if (require_set_attr) {		struct termios new_mode;		if (tcsetattr(fd, TCSADRAIN, &mode))			perror_msg_and_die("%s", device_name);		/* POSIX (according to Zlotnick's book) tcsetattr returns zero if		   it performs *any* of the requested operations.  This means it		   can report `success' when it has actually failed to perform		   some proper subset of the requested operations.  To detect		   this partial failure, get the current terminal attributes and		   compare them to the requested ones.  */		/* 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(fd, &new_mode))			perror_msg_and_die("%s", device_name);		/* Normally, one shouldn't use memcmp to compare structures that		   may have `holes' containing uninitialized data, but we have been		   careful to initialize the storage of these two variables to all		   zeroes.  One might think it more efficient simply to compare the		   modified fields, but that would require enumerating those fields --		   and not all systems have the same fields in this structure.  */		if (memcmp(&mode, &new_mode, sizeof(mode)) != 0) {#ifdef CIBAUD			/* SunOS 4.1.3 (at least) has the problem that after this sequence,			   tcgetattr (&m1); tcsetattr (&m1); tcgetattr (&m2);			   sometimes (m1 != m2).  The only difference is in the four bits			   of the c_cflag field corresponding to the baud rate.  To save			   Sun users a little confusion, don't report an error if this			   happens.  But suppress the error only if we haven't tried to			   set the baud rate explicitly -- otherwise we'd never give an			   error for a true failure to set the baud rate.  */			new_mode.c_cflag &= (~CIBAUD);			if (speed_was_set || memcmp(&mode, &new_mode, sizeof(mode)) != 0)#endif				error_msg_and_die ("%s: unable to perform all requested operations",					 device_name);		}	}	return EXIT_SUCCESS;}/* Return 0 if not applied because not reversible; otherwise return 1.  */static intset_mode(const struct mode_info *info, int reversed, struct termios *mode){	tcflag_t *bitsp;	if (reversed && (info->flags & REV) == 0)		return 0;	bitsp = mode_type_flag(info->type, mode);	if (bitsp == NULL) {		/* Combination mode. */		if (info->name == evenp || info->name == parity) {			if (reversed)				mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;			else				mode->c_cflag =					(mode->c_cflag & ~PARODD & ~CSIZE) | PARENB | CS7;		} else if (info->name == stty_oddp) {			if (reversed)				mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;			else				mode->c_cflag =					(mode->c_cflag & ~CSIZE) | CS7 | PARODD | PARENB;		} else if (info->name == stty_nl) {			if (reversed) {				mode->c_iflag = (mode->c_iflag | ICRNL) & ~INLCR & ~IGNCR;				mode->c_oflag = (mode->c_oflag#ifdef ONLCR								 | ONLCR#endif					)#ifdef OCRNL					& ~OCRNL#endif#ifdef ONLRET					& ~ONLRET#endif					;			} else {				mode->c_iflag = mode->c_iflag & ~ICRNL;#ifdef ONLCR				mode->c_oflag = mode->c_oflag & ~ONLCR;#endif			}		} else if (info->name == stty_ek) {			mode->c_cc[VERASE] = CERASE;			mode->c_cc[VKILL] = CKILL;		} else if (info->name == stty_sane)			sane_mode(mode);		else if (info->name == cbreak) {			if (reversed)				mode->c_lflag |= ICANON;			else				mode->c_lflag &= ~ICANON;		} else if (info->name == stty_pass8) {			if (reversed) {				mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;				mode->c_iflag |= ISTRIP;			} else {				mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;				mode->c_iflag &= ~ISTRIP;			}		} else if (info->name == litout) {			if (reversed) {				mode->c_cflag = (mode->c_cflag & ~CSIZE) | CS7 | PARENB;				mode->c_iflag |= ISTRIP;				mode->c_oflag |= OPOST;			} else {				mode->c_cflag = (mode->c_cflag & ~PARENB & ~CSIZE) | CS8;				mode->c_iflag &= ~ISTRIP;				mode->c_oflag &= ~OPOST;			}		} else if (info->name == raw || info->name == cooked) {			if ((info->name[0] == 'r' && reversed)				|| (info->name[0] == 'c' && !reversed)) {				/* Cooked mode. */				mode->c_iflag |= BRKINT | IGNPAR | ISTRIP | ICRNL | IXON;				mode->c_oflag |= OPOST;				mode->c_lflag |= ISIG | ICANON;#if VMIN == VEOF				mode->c_cc[VEOF] = CEOF;#endif#if VTIME == VEOL				mode->c_cc[VEOL] = CEOL;#endif			} else {				/* Raw mode. */				mode->c_iflag = 0;				mode->c_oflag &= ~OPOST;				mode->c_lflag &= ~(ISIG | ICANON#ifdef XCASE								   | XCASE#endif					);				mode->c_cc[VMIN] = 1;				mode->c_cc[VTIME] = 0;			}		}#ifdef IXANY		else if (info->name == decctlq) {			if (reversed)				mode->c_iflag |= IXANY;			else				mode->c_iflag &= ~IXANY;		}#endif#ifdef TABDLY		else if (info->name == stty_tabs) {			if (reversed)				mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB3;			else				mode->c_oflag = (mode->c_oflag & ~TABDLY) | TAB0;		}#else# ifdef OXTABS		else if (info->name == stty_tabs) {			if (reversed)				mode->c_oflag = mode->c_oflag | OXTABS;			else				mode->c_oflag = mode->c_oflag & ~OXTABS;		}# endif#endif#if defined(XCASE) && defined(IUCLC) && defined(OLCUC)		else if (info->name == stty_lcase || info->name == stty_LCASE) {			if (reversed) {				mode->c_lflag &= ~XCASE;				mode->c_iflag &= ~IUCLC;				mode->c_oflag &= ~OLCUC;			} else {				mode->c_lflag |= XCASE;				mode->c_iflag |= IUCLC;				mode->c_oflag |= OLCUC;			}		}#endif		else if (info->name == stty_crt)			mode->c_lflag |= ECHOE#ifdef ECHOCTL				| ECHOCTL#endif#ifdef ECHOKE				| ECHOKE#endif				;		else if (info->name == stty_dec) {			mode->c_cc[VINTR] = 3;  /* ^C */			mode->c_cc[VERASE] = 127;       /* DEL */			mode->c_cc[VKILL] = 21; /* ^U */			mode->c_lflag |= ECHOE#ifdef ECHOCTL				| ECHOCTL#endif#ifdef ECHOKE				| ECHOKE#endif				;#ifdef IXANY			mode->c_iflag &= ~IXANY;#endif		}	} else if (reversed)		*bitsp = *bitsp & ~info->mask & ~info->bits;	else		*bitsp = (*bitsp & ~info->mask) | info->bits;	return 1;}static voidset_control_char(const struct control_info *info, const char *arg,				 struct termios *mode){	unsigned char value;	if (info->name == stty_min || info->name == stty_time)		value = parse_number(arg, stty_suffixes);	else if (arg[0] == '\0' || arg[1] == '\0')		value = arg[0];	else if (STREQ(arg, "^-") || STREQ(arg, "undef"))		value = _POSIX_VDISABLE;	else if (arg[0] == '^' && arg[1] != '\0') {     /* Ignore any trailing junk. */		if (arg[1] == '?')			value = 127;		else			value = arg[1] & ~0140; /* Non-letters get weird results. */	} else		value = parse_number(arg, stty_suffixes);	mode->c_cc[info->offset] = value;}static voidset_speed(enum speed_setting type, const char *arg, struct termios *mode){	speed_t baud;

⌨️ 快捷键说明

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