xref: /csrg-svn/bin/stty/stty.c (revision 51070)
147843Sbostic /*-
248945Sbostic  * Copyright (c) 1989, 1991 The Regents of the University of California.
347843Sbostic  * All rights reserved.
447843Sbostic  *
549269Sbostic  * %sccs.include.redist.c%
619907Sdist  */
719907Sdist 
813073Ssam #ifndef lint
919907Sdist char copyright[] =
1048945Sbostic "@(#) Copyright (c) 1989, 1991 The Regents of the University of California.\n\
1119907Sdist  All rights reserved.\n";
1247843Sbostic #endif /* not lint */
1319907Sdist 
1419907Sdist #ifndef lint
15*51070Sbostic static char sccsid[] = "@(#)stty.c	5.29 (Berkeley) 09/10/91";
1647843Sbostic #endif /* not lint */
1719907Sdist 
1838674Smarc #include <sys/types.h>
1948945Sbostic #include <fcntl.h>
2038674Smarc #include <errno.h>
2148945Sbostic #include <unistd.h>
2248945Sbostic #include <stdio.h>
2338674Smarc #include <ctype.h>
2448945Sbostic #include <stdlib.h>
2548945Sbostic #include <string.h>
2648945Sbostic #include "stty.h"
2748945Sbostic #include "extern.h"
281179Sbill 
2950010Sbostic char *usage = "usage: stty: [-eg] [-f file] [options]";
3038674Smarc 
3138674Smarc main(argc, argv)
3248945Sbostic 	int argc;
3348945Sbostic 	char **argv;
341179Sbill {
3550007Sbostic 	struct info i;
3648945Sbostic 	enum FMT fmt;
3750007Sbostic 	int ch;
381179Sbill 
3948945Sbostic 	fmt = NOTSET;
4050007Sbostic 	i.fd = STDIN_FILENO;
4150007Sbostic 
4248974Sbostic 	opterr = 0;
4349974Sbostic 	while (strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
4449974Sbostic 	    (ch = getopt(argc, argv, "aef:g")) != EOF)
4548945Sbostic 		switch(ch) {
4648945Sbostic 		case 'a':		/* undocumented: POSIX compatibility */
4748945Sbostic 			fmt = POSIX;
4848945Sbostic 			break;
4948945Sbostic 		case 'e':
5048945Sbostic 			fmt = BSD;
5148945Sbostic 			break;
5248945Sbostic 		case 'f':
5350007Sbostic 			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
5449974Sbostic 				err("%s: %s", optarg, strerror(errno));
5548945Sbostic 			break;
5648945Sbostic 		case 'g':
5748945Sbostic 			fmt = GFLAG;
5848945Sbostic 			break;
5948945Sbostic 		case '?':
6048945Sbostic 		default:
6148945Sbostic 			goto args;
6248945Sbostic 		}
6348945Sbostic 
6448945Sbostic args:	argc -= optind;
6548945Sbostic 	argv += optind;
6648945Sbostic 
6750007Sbostic 	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
6848945Sbostic 		err("TIOCGETD: %s", strerror(errno));
6950007Sbostic 	if (tcgetattr(i.fd, &i.t) < 0)
7048945Sbostic 		err("tcgetattr: %s", strerror(errno));
7150007Sbostic 	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
7248945Sbostic 		warn("TIOCGWINSZ: %s\n", strerror(errno));
7338674Smarc 
7448945Sbostic 	checkredirect();			/* conversion aid */
7548945Sbostic 
7648945Sbostic 	switch(fmt) {
7748945Sbostic 	case NOTSET:
7848945Sbostic 		if (*argv)
7948945Sbostic 			break;
8048945Sbostic 		/* FALLTHROUGH */
8148945Sbostic 	case BSD:
8248945Sbostic 	case POSIX:
8350007Sbostic 		print(&i.t, &i.win, i.ldisc, fmt);
8448945Sbostic 		break;
8548945Sbostic 	case GFLAG:
8650007Sbostic 		gprint(&i.t, &i.win, i.ldisc);
8748945Sbostic 		break;
881179Sbill 	}
8938674Smarc 
9050007Sbostic 	for (i.set = i.wset = 0; *argv; ++argv) {
9150010Sbostic 		if (ksearch(&argv, &i))
9248945Sbostic 			continue;
9350007Sbostic 
9450010Sbostic 		if (csearch(&argv, &i))
9550010Sbostic 			continue;
9650007Sbostic 
9750010Sbostic 		if (msearch(&argv, &i))
9850007Sbostic 			continue;
9948945Sbostic 
10038674Smarc 		if (isdigit(**argv)) {
10150010Sbostic 			int speed;
10250010Sbostic 
10350010Sbostic 			speed = atoi(*argv);
10450010Sbostic 			cfsetospeed(&i.t, speed);
10550010Sbostic 			cfsetispeed(&i.t, speed);
106*51070Sbostic 			i.set = 1;
10750007Sbostic 			continue;
10838674Smarc 		}
10950010Sbostic 
11048945Sbostic 		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
11150007Sbostic 			gread(&i.t, *argv + sizeof("gfmt1") - 1);
11250007Sbostic 			continue;
11343323Smarc 		}
11448945Sbostic 
11550007Sbostic 		err("illegal option -- %s\n%s", *argv, usage);
1161179Sbill 	}
11748945Sbostic 
11850007Sbostic 	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
11948945Sbostic 		err("tcsetattr: %s", strerror(errno));
12050007Sbostic 	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
12148945Sbostic 		warn("TIOCSWINSZ: %s", strerror(errno));
12238674Smarc 	exit(0);
1231179Sbill }
124