xref: /csrg-svn/bin/stty/stty.c (revision 66642)
147843Sbostic /*-
2*66642Spendry  * Copyright (c) 1989, 1991, 1993, 1994
361098Sbostic  *	The Regents of the University of California.  All rights reserved.
447843Sbostic  *
549269Sbostic  * %sccs.include.redist.c%
619907Sdist  */
719907Sdist 
813073Ssam #ifndef lint
961098Sbostic static char copyright[] =
10*66642Spendry "@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
1161098Sbostic 	The Regents of the University of California.  All rights reserved.\n";
1247843Sbostic #endif /* not lint */
1319907Sdist 
1419907Sdist #ifndef lint
15*66642Spendry static char sccsid[] = "@(#)stty.c	8.3 (Berkeley) 04/02/94";
1647843Sbostic #endif /* not lint */
1719907Sdist 
1838674Smarc #include <sys/types.h>
1959523Sbostic 
2059523Sbostic #include <ctype.h>
2159523Sbostic #include <err.h>
2259523Sbostic #include <errno.h>
2348945Sbostic #include <fcntl.h>
2448945Sbostic #include <stdio.h>
2548945Sbostic #include <stdlib.h>
2648945Sbostic #include <string.h>
2759523Sbostic #include <unistd.h>
2859523Sbostic 
2948945Sbostic #include "stty.h"
3048945Sbostic #include "extern.h"
311179Sbill 
3259523Sbostic int
main(argc,argv)3338674Smarc main(argc, argv)
3448945Sbostic 	int argc;
3551828Sbostic 	char *argv[];
361179Sbill {
3750007Sbostic 	struct info i;
3848945Sbostic 	enum FMT fmt;
3950007Sbostic 	int ch;
401179Sbill 
4148945Sbostic 	fmt = NOTSET;
4250007Sbostic 	i.fd = STDIN_FILENO;
4350007Sbostic 
4448974Sbostic 	opterr = 0;
4551828Sbostic 	while (optind < argc &&
4651828Sbostic 	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
4749974Sbostic 	    (ch = getopt(argc, argv, "aef:g")) != EOF)
4848945Sbostic 		switch(ch) {
4948945Sbostic 		case 'a':		/* undocumented: POSIX compatibility */
5048945Sbostic 			fmt = POSIX;
5148945Sbostic 			break;
5248945Sbostic 		case 'e':
5348945Sbostic 			fmt = BSD;
5448945Sbostic 			break;
5548945Sbostic 		case 'f':
5650007Sbostic 			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
5759523Sbostic 				err(1, "%s", optarg);
5848945Sbostic 			break;
5948945Sbostic 		case 'g':
6048945Sbostic 			fmt = GFLAG;
6148945Sbostic 			break;
6248945Sbostic 		case '?':
6348945Sbostic 		default:
6448945Sbostic 			goto args;
6548945Sbostic 		}
6648945Sbostic 
6748945Sbostic args:	argc -= optind;
6848945Sbostic 	argv += optind;
6948945Sbostic 
7050007Sbostic 	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
7159523Sbostic 		err(1, "TIOCGETD");
7250007Sbostic 	if (tcgetattr(i.fd, &i.t) < 0)
7359523Sbostic 		err(1, "tcgetattr");
7450007Sbostic 	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
7548945Sbostic 		warn("TIOCGWINSZ: %s\n", strerror(errno));
7638674Smarc 
7748945Sbostic 	checkredirect();			/* conversion aid */
7848945Sbostic 
7948945Sbostic 	switch(fmt) {
8048945Sbostic 	case NOTSET:
8148945Sbostic 		if (*argv)
8248945Sbostic 			break;
8348945Sbostic 		/* FALLTHROUGH */
8448945Sbostic 	case BSD:
8548945Sbostic 	case POSIX:
8650007Sbostic 		print(&i.t, &i.win, i.ldisc, fmt);
8748945Sbostic 		break;
8848945Sbostic 	case GFLAG:
8950007Sbostic 		gprint(&i.t, &i.win, i.ldisc);
9048945Sbostic 		break;
911179Sbill 	}
9238674Smarc 
9350007Sbostic 	for (i.set = i.wset = 0; *argv; ++argv) {
9450010Sbostic 		if (ksearch(&argv, &i))
9548945Sbostic 			continue;
9650007Sbostic 
9750010Sbostic 		if (csearch(&argv, &i))
9850010Sbostic 			continue;
9950007Sbostic 
10050010Sbostic 		if (msearch(&argv, &i))
10150007Sbostic 			continue;
10248945Sbostic 
10338674Smarc 		if (isdigit(**argv)) {
10450010Sbostic 			int speed;
10550010Sbostic 
10650010Sbostic 			speed = atoi(*argv);
10750010Sbostic 			cfsetospeed(&i.t, speed);
10850010Sbostic 			cfsetispeed(&i.t, speed);
10951070Sbostic 			i.set = 1;
11050007Sbostic 			continue;
11138674Smarc 		}
11250010Sbostic 
11348945Sbostic 		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
11450007Sbostic 			gread(&i.t, *argv + sizeof("gfmt1") - 1);
11550007Sbostic 			continue;
11643323Smarc 		}
11748945Sbostic 
11859523Sbostic 		warnx("illegal option -- %s", *argv);
11959523Sbostic 		usage();
1201179Sbill 	}
12148945Sbostic 
12250007Sbostic 	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
12359523Sbostic 		err(1, "tcsetattr");
12450007Sbostic 	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
12559523Sbostic 		warn("TIOCSWINSZ");
12638674Smarc 	exit(0);
1271179Sbill }
12859523Sbostic 
12959523Sbostic void
usage()13059523Sbostic usage()
13159523Sbostic {
13266564Spendry 
13366564Spendry 	(void)fprintf(stderr, "usage: stty: [-a|-e|-g] [-f file] [options]\n");
13459523Sbostic 	exit (1);
13559523Sbostic }
136