xref: /netbsd-src/bin/stty/stty.c (revision 0282eceed17c83a603266c044f7df6bf826636b3)
1*0282eceeSchristos /* $NetBSD: stty.c,v 1.24 2019/09/06 16:28:53 christos Exp $ */
249f0ad86Scgd 
361f28255Scgd /*-
4fe5a9095Smycroft  * Copyright (c) 1989, 1991, 1993, 1994
5fe5a9095Smycroft  *	The Regents of the University of California.  All rights reserved.
661f28255Scgd  *
761f28255Scgd  * Redistribution and use in source and binary forms, with or without
861f28255Scgd  * modification, are permitted provided that the following conditions
961f28255Scgd  * are met:
1061f28255Scgd  * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd  *    notice, this list of conditions and the following disclaimer.
1261f28255Scgd  * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd  *    notice, this list of conditions and the following disclaimer in the
1461f28255Scgd  *    documentation and/or other materials provided with the distribution.
15b5b29542Sagc  * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd  *    may be used to endorse or promote products derived from this software
1761f28255Scgd  *    without specific prior written permission.
1861f28255Scgd  *
1961f28255Scgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd  * SUCH DAMAGE.
3061f28255Scgd  */
3161f28255Scgd 
32e028842eSchristos #include <sys/cdefs.h>
3361f28255Scgd #ifndef lint
342fe2731dSlukem __COPYRIGHT("@(#) Copyright (c) 1989, 1991, 1993, 1994\
352fe2731dSlukem  The Regents of the University of California.  All rights reserved.");
3661f28255Scgd #endif /* not lint */
3761f28255Scgd 
3861f28255Scgd #ifndef lint
3949f0ad86Scgd #if 0
4049f0ad86Scgd static char sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
4149f0ad86Scgd #else
42*0282eceeSchristos __RCSID("$NetBSD: stty.c,v 1.24 2019/09/06 16:28:53 christos Exp $");
4349f0ad86Scgd #endif
4461f28255Scgd #endif /* not lint */
4561f28255Scgd 
4661f28255Scgd #include <sys/types.h>
47fe5a9095Smycroft 
4861f28255Scgd #include <ctype.h>
49fe5a9095Smycroft #include <err.h>
50fe5a9095Smycroft #include <errno.h>
51fe5a9095Smycroft #include <fcntl.h>
529655f5c2Schristos #include <locale.h>
53fe5a9095Smycroft #include <stdio.h>
5461f28255Scgd #include <stdlib.h>
5561f28255Scgd #include <string.h>
56fe5a9095Smycroft #include <unistd.h>
57fe5a9095Smycroft 
5861f28255Scgd #include "stty.h"
5961f28255Scgd #include "extern.h"
6061f28255Scgd 
61fe5a9095Smycroft int
main(int argc,char * argv[])6230b25d1cSperry main(int argc, char *argv[])
6361f28255Scgd {
6461f28255Scgd 	struct info i;
6561f28255Scgd 	enum FMT fmt;
6661f28255Scgd 	int ch;
6761f28255Scgd 
6830b25d1cSperry 	setprogname(argv[0]);
699655f5c2Schristos 	(void)setlocale(LC_ALL, "");
7030b25d1cSperry 
7132ee4be5Schristos 	fmt = STTY_NOTSET;
7261f28255Scgd 	i.fd = STDIN_FILENO;
7361f28255Scgd 
7461f28255Scgd 	opterr = 0;
75fe5a9095Smycroft 	while (optind < argc &&
76a11d15b1Smycroft 	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
77fe5a9095Smycroft 	    (ch = getopt(argc, argv, "aef:g")) != -1)
7861f28255Scgd 		switch(ch) {
7961f28255Scgd 		case 'a':		/* undocumented: POSIX compatibility */
8032ee4be5Schristos 			fmt = STTY_POSIX;
8161f28255Scgd 			break;
8261f28255Scgd 		case 'e':
835ac821faSchristos 			fmt = STTY_BSD;
8461f28255Scgd 			break;
8561f28255Scgd 		case 'f':
8661f28255Scgd 			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
8735723768Smycroft 				err(1, "%s", optarg);
8861f28255Scgd 			break;
8961f28255Scgd 		case 'g':
9032ee4be5Schristos 			fmt = STTY_GFLAG;
9161f28255Scgd 			break;
9261f28255Scgd 		case '?':
9361f28255Scgd 		default:
9461f28255Scgd 			goto args;
9561f28255Scgd 		}
9661f28255Scgd 
9761f28255Scgd args:	argc -= optind;
9861f28255Scgd 	argv += optind;
9961f28255Scgd 
10061f28255Scgd 	if (tcgetattr(i.fd, &i.t) < 0)
10135723768Smycroft 		err(1, "tcgetattr");
102*0282eceeSchristos 	if (ioctl(i.fd, TIOCGLINED, i.ldisc) < 0)
103*0282eceeSchristos 		warn("TIOCGLINED");
10461f28255Scgd 	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
10535723768Smycroft 		warn("TIOCGWINSZ");
106700d3ab2Schristos 	if (ioctl(i.fd, TIOCGQSIZE, &i.queue) < 0)
107700d3ab2Schristos 		warn("TIOCGQSIZE");
10861f28255Scgd 
10961f28255Scgd 	switch(fmt) {
11032ee4be5Schristos 	case STTY_NOTSET:
11161f28255Scgd 		if (*argv)
11261f28255Scgd 			break;
11361f28255Scgd 		/* FALLTHROUGH */
11432ee4be5Schristos 	case STTY_BSD:
11532ee4be5Schristos 	case STTY_POSIX:
116700d3ab2Schristos 		print(&i.t, &i.win, i.queue, i.ldisc, fmt);
11761f28255Scgd 		break;
11832ee4be5Schristos 	case STTY_GFLAG:
119301199f5Smycroft 		gprint(&i.t);
12061f28255Scgd 		break;
12161f28255Scgd 	}
12261f28255Scgd 
12361f28255Scgd 	for (i.set = i.wset = 0; *argv; ++argv) {
12461f28255Scgd 		if (ksearch(&argv, &i))
12561f28255Scgd 			continue;
12661f28255Scgd 
12761f28255Scgd 		if (csearch(&argv, &i))
12861f28255Scgd 			continue;
12961f28255Scgd 
13061f28255Scgd 		if (msearch(&argv, &i))
13161f28255Scgd 			continue;
13261f28255Scgd 
13332ee4be5Schristos 		if (isdigit((unsigned char)**argv)) {
13461f28255Scgd 			int speed;
13561f28255Scgd 
13661f28255Scgd 			speed = atoi(*argv);
13761f28255Scgd 			cfsetospeed(&i.t, speed);
13861f28255Scgd 			cfsetispeed(&i.t, speed);
13961f28255Scgd 			i.set = 1;
14061f28255Scgd 			continue;
14161f28255Scgd 		}
14261f28255Scgd 
14361f28255Scgd 		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
14461f28255Scgd 			gread(&i.t, *argv + sizeof("gfmt1") - 1);
1454a9126e1Sderaadt 			i.set = 1;
14661f28255Scgd 			continue;
14761f28255Scgd 		}
14861f28255Scgd 
14935723768Smycroft 		warnx("illegal option -- %s", *argv);
15035723768Smycroft 		usage();
15161f28255Scgd 	}
15261f28255Scgd 
15361f28255Scgd 	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
15435723768Smycroft 		err(1, "tcsetattr");
15561f28255Scgd 	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
15635723768Smycroft 		warn("TIOCSWINSZ");
15761f28255Scgd 	exit(0);
1589dc385beSmycroft 	/* NOTREACHED */
15961f28255Scgd }
160fe5a9095Smycroft 
161fe5a9095Smycroft void
usage(void)16230b25d1cSperry usage(void)
163fe5a9095Smycroft {
164fe5a9095Smycroft 
165d1752d5dSwiz 	(void)fprintf(stderr, "usage: %s [-a|-e|-g] [-f file] [operand ...]\n", getprogname());
166fe5a9095Smycroft 	exit(1);
1679dc385beSmycroft 	/* NOTREACHED */
168fe5a9095Smycroft }
169