xref: /csrg-svn/bin/stty/stty.c (revision 51828)
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*51828Sbostic static char sccsid[] = "@(#)stty.c	5.32 (Berkeley) 12/03/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 
2951599Scael char *usage = "usage: stty: [-a|-e|-g] [-f file] [options]";
3038674Smarc 
3138674Smarc main(argc, argv)
3248945Sbostic 	int argc;
33*51828Sbostic 	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;
43*51828Sbostic 	while (optind < argc &&
44*51828Sbostic 	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
4549974Sbostic 	    (ch = getopt(argc, argv, "aef:g")) != EOF)
4648945Sbostic 		switch(ch) {
4748945Sbostic 		case 'a':		/* undocumented: POSIX compatibility */
4848945Sbostic 			fmt = POSIX;
4948945Sbostic 			break;
5048945Sbostic 		case 'e':
5148945Sbostic 			fmt = BSD;
5248945Sbostic 			break;
5348945Sbostic 		case 'f':
5450007Sbostic 			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
5549974Sbostic 				err("%s: %s", optarg, strerror(errno));
5648945Sbostic 			break;
5748945Sbostic 		case 'g':
5848945Sbostic 			fmt = GFLAG;
5948945Sbostic 			break;
6048945Sbostic 		case '?':
6148945Sbostic 		default:
6248945Sbostic 			goto args;
6348945Sbostic 		}
6448945Sbostic 
6548945Sbostic args:	argc -= optind;
6648945Sbostic 	argv += optind;
6748945Sbostic 
6850007Sbostic 	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
6948945Sbostic 		err("TIOCGETD: %s", strerror(errno));
7050007Sbostic 	if (tcgetattr(i.fd, &i.t) < 0)
7148945Sbostic 		err("tcgetattr: %s", strerror(errno));
7250007Sbostic 	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
7348945Sbostic 		warn("TIOCGWINSZ: %s\n", strerror(errno));
7438674Smarc 
7548945Sbostic 	checkredirect();			/* conversion aid */
7648945Sbostic 
7748945Sbostic 	switch(fmt) {
7848945Sbostic 	case NOTSET:
7948945Sbostic 		if (*argv)
8048945Sbostic 			break;
8148945Sbostic 		/* FALLTHROUGH */
8248945Sbostic 	case BSD:
8348945Sbostic 	case POSIX:
8450007Sbostic 		print(&i.t, &i.win, i.ldisc, fmt);
8548945Sbostic 		break;
8648945Sbostic 	case GFLAG:
8750007Sbostic 		gprint(&i.t, &i.win, i.ldisc);
8848945Sbostic 		break;
891179Sbill 	}
9038674Smarc 
9150007Sbostic 	for (i.set = i.wset = 0; *argv; ++argv) {
9250010Sbostic 		if (ksearch(&argv, &i))
9348945Sbostic 			continue;
9450007Sbostic 
9550010Sbostic 		if (csearch(&argv, &i))
9650010Sbostic 			continue;
9750007Sbostic 
9850010Sbostic 		if (msearch(&argv, &i))
9950007Sbostic 			continue;
10048945Sbostic 
10138674Smarc 		if (isdigit(**argv)) {
10250010Sbostic 			int speed;
10350010Sbostic 
10450010Sbostic 			speed = atoi(*argv);
10550010Sbostic 			cfsetospeed(&i.t, speed);
10650010Sbostic 			cfsetispeed(&i.t, speed);
10751070Sbostic 			i.set = 1;
10850007Sbostic 			continue;
10938674Smarc 		}
11050010Sbostic 
11148945Sbostic 		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
11250007Sbostic 			gread(&i.t, *argv + sizeof("gfmt1") - 1);
11350007Sbostic 			continue;
11443323Smarc 		}
11548945Sbostic 
11650007Sbostic 		err("illegal option -- %s\n%s", *argv, usage);
1171179Sbill 	}
11848945Sbostic 
11950007Sbostic 	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
12048945Sbostic 		err("tcsetattr: %s", strerror(errno));
12150007Sbostic 	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
12248945Sbostic 		warn("TIOCSWINSZ: %s", strerror(errno));
12338674Smarc 	exit(0);
1241179Sbill }
125