xref: /freebsd/bin/stty/stty.c (revision 99e8005137088aafb1350e23b113d69b01b0820f)
1 /*-
2  * Copyright (c) 1989, 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static char const copyright[] =
36 "@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
43 #endif
44 static const char rcsid[] =
45   "$FreeBSD$";
46 #endif /* not lint */
47 
48 #include <sys/types.h>
49 
50 #include <ctype.h>
51 #include <err.h>
52 #include <errno.h>
53 #include <fcntl.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 
59 #include "stty.h"
60 #include "extern.h"
61 
62 int main __P((int, char *[]));
63 
64 int
65 main(argc, argv)
66 	int argc;
67 	char *argv[];
68 {
69 	struct info i;
70 	enum FMT fmt;
71 	int ch;
72 
73 	fmt = NOTSET;
74 	i.fd = STDIN_FILENO;
75 
76 	opterr = 0;
77 	while (optind < argc &&
78 	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
79 	    (ch = getopt(argc, argv, "aef:g")) != -1)
80 		switch(ch) {
81 		case 'a':		/* undocumented: POSIX compatibility */
82 			fmt = POSIX;
83 			break;
84 		case 'e':
85 			fmt = BSD;
86 			break;
87 		case 'f':
88 			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
89 				err(1, "%s", optarg);
90 			break;
91 		case 'g':
92 			fmt = GFLAG;
93 			break;
94 		case '?':
95 		default:
96 			goto args;
97 		}
98 
99 args:	argc -= optind;
100 	argv += optind;
101 
102 	if (tcgetattr(i.fd, &i.t) < 0)
103 		errx(1, "stdin isn't a terminal");
104 	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
105 		err(1, "TIOCGETD");
106 	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
107 		warn("TIOCGWINSZ");
108 
109 	checkredirect();			/* conversion aid */
110 
111 	switch(fmt) {
112 	case NOTSET:
113 		if (*argv)
114 			break;
115 		/* FALLTHROUGH */
116 	case BSD:
117 	case POSIX:
118 		print(&i.t, &i.win, i.ldisc, fmt);
119 		break;
120 	case GFLAG:
121 		gprint(&i.t, &i.win, i.ldisc);
122 		break;
123 	}
124 
125 	for (i.set = i.wset = 0; *argv; ++argv) {
126 		if (ksearch(&argv, &i))
127 			continue;
128 
129 		if (csearch(&argv, &i))
130 			continue;
131 
132 		if (msearch(&argv, &i))
133 			continue;
134 
135 		if (isdigit(**argv)) {
136 			speed_t speed;
137 
138 			speed = atoi(*argv);
139 			cfsetospeed(&i.t, speed);
140 			cfsetispeed(&i.t, speed);
141 			i.set = 1;
142 			continue;
143 		}
144 
145 		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
146 			gread(&i.t, *argv + sizeof("gfmt1") - 1);
147 			i.set = 1;
148 			continue;
149 		}
150 
151 		warnx("illegal option -- %s", *argv);
152 		usage();
153 	}
154 
155 	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
156 		err(1, "tcsetattr");
157 	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
158 		warn("TIOCSWINSZ");
159 	exit(0);
160 }
161 
162 void
163 usage()
164 {
165 
166 	(void)fprintf(stderr, "usage: stty [-a|-e|-g] [-f file] [options]\n");
167 	exit (1);
168 }
169