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