stty.c revision 24348
1229997Sken/*-
2229997Sken * Copyright (c) 1989, 1991, 1993, 1994
3232604Strasz *	The Regents of the University of California.  All rights reserved.
4313371Smav *
5229997Sken * Redistribution and use in source and binary forms, with or without
6229997Sken * modification, are permitted provided that the following conditions
7232604Strasz * are met:
8232604Strasz * 1. Redistributions of source code must retain the above copyright
9232604Strasz *    notice, this list of conditions and the following disclaimer.
10229997Sken * 2. Redistributions in binary form must reproduce the above copyright
11229997Sken *    notice, this list of conditions and the following disclaimer in the
12229997Sken *    documentation and/or other materials provided with the distribution.
13229997Sken * 3. All advertising materials mentioning features or use of this software
14229997Sken *    must display the following acknowledgement:
15229997Sken *	This product includes software developed by the University of
16229997Sken *	California, Berkeley and its contributors.
17229997Sken * 4. Neither the name of the University nor the names of its contributors
18229997Sken *    may be used to endorse or promote products derived from this software
19229997Sken *    without specific prior written permission.
20229997Sken *
21229997Sken * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22229997Sken * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23229997Sken * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24229997Sken * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25229997Sken * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26229997Sken * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27229997Sken * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28229997Sken * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29229997Sken * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30229997Sken * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31229997Sken * SUCH DAMAGE.
32229997Sken *
33229997Sken *	$Id: stty.c,v 1.9 1997/02/22 14:05:54 peter Exp $
34229997Sken */
35229997Sken
36229997Sken#ifndef lint
37229997Skenstatic char const copyright[] =
38313371Smav"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
39229997Sken	The Regents of the University of California.  All rights reserved.\n";
40229997Sken#endif /* not lint */
41229997Sken
42229997Sken#ifndef lint
43229997Skenstatic char const sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
44229997Sken#endif /* not lint */
45229997Sken
46229997Sken#include <sys/types.h>
47229997Sken
48229997Sken#include <ctype.h>
49229997Sken#include <err.h>
50229997Sken#include <errno.h>
51313371Smav#include <fcntl.h>
52229997Sken#include <stdio.h>
53229997Sken#include <stdlib.h>
54229997Sken#include <string.h>
55313371Smav#include <unistd.h>
56265642Smav
57229997Sken#include "stty.h"
58229997Sken#include "extern.h"
59229997Sken
60229997Skenint
61229997Skenmain(argc, argv)
62288732Smav	int argc;
63229997Sken	char *argv[];
64229997Sken{
65288732Smav	struct info i;
66229997Sken	enum FMT fmt;
67229997Sken	int ch;
68229997Sken
69229997Sken	fmt = NOTSET;
70229997Sken	i.fd = STDIN_FILENO;
71229997Sken
72288732Smav	opterr = 0;
73288732Smav	while (optind < argc &&
74229997Sken	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
75229997Sken	    (ch = getopt(argc, argv, "aef:g")) != -1)
76313371Smav		switch(ch) {
77313371Smav		case 'a':		/* undocumented: POSIX compatibility */
78313371Smav			fmt = POSIX;
79313371Smav			break;
80313371Smav		case 'e':
81313371Smav			fmt = BSD;
82313371Smav			break;
83313371Smav		case 'f':
84313371Smav			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
85313371Smav				err(1, "%s", optarg);
86313371Smav			break;
87313371Smav		case 'g':
88313371Smav			fmt = GFLAG;
89313371Smav			break;
90313371Smav		case '?':
91313371Smav		default:
92229997Sken			goto args;
93313371Smav		}
94313371Smav
95313371Smavargs:	argc -= optind;
96313371Smav	argv += optind;
97313371Smav
98313371Smav	if (tcgetattr(i.fd, &i.t) < 0)
99313371Smav		errx(1, "stdin isn't a terminal");
100229997Sken	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
101229997Sken		err(1, "TIOCGETD");
102229997Sken	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
103229997Sken		warn("TIOCGWINSZ: %s\n", strerror(errno));
104229997Sken
105229997Sken	checkredirect();			/* conversion aid */
106288728Smav
107313371Smav	switch(fmt) {
108313371Smav	case NOTSET:
109313371Smav		if (*argv)
110313371Smav			break;
111313371Smav		/* FALLTHROUGH */
112313371Smav	case BSD:
113313371Smav	case POSIX:
114313371Smav		print(&i.t, &i.win, i.ldisc, fmt);
115313371Smav		break;
116313371Smav	case GFLAG:
117313371Smav		gprint(&i.t, &i.win, i.ldisc);
118229997Sken		break;
119229997Sken	}
120229997Sken
121313371Smav	for (i.set = i.wset = 0; *argv; ++argv) {
122313371Smav		if (ksearch(&argv, &i))
123313371Smav			continue;
124265642Smav
125313371Smav		if (csearch(&argv, &i))
126229997Sken			continue;
127229997Sken
128229997Sken		if (msearch(&argv, &i))
129229997Sken			continue;
130229997Sken
131229997Sken		if (isdigit(**argv)) {
132229997Sken			int speed;
133229997Sken
134229997Sken			speed = atoi(*argv);
135288732Smav			cfsetospeed(&i.t, speed);
136229997Sken			cfsetispeed(&i.t, speed);
137313369Smav			i.set = 1;
138313369Smav			continue;
139229997Sken		}
140313371Smav
141313371Smav		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
142229997Sken			gread(&i.t, *argv + sizeof("gfmt1") - 1);
143313371Smav			i.set = 1;
144313371Smav			continue;
145313371Smav		}
146313371Smav
147229997Sken		warnx("illegal option -- %s", *argv);
148229997Sken		usage();
149229997Sken	}
150229997Sken
151229997Sken	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
152288727Smav		err(1, "tcsetattr");
153232604Strasz	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
154232604Strasz		warn("TIOCSWINSZ");
155229997Sken	exit(0);
156229997Sken}
157229997Sken
158229997Skenvoid
159229997Skenusage()
160229997Sken{
161230334Sken
162230334Sken	(void)fprintf(stderr, "usage: stty: [-a|-e|-g] [-f file] [options]\n");
163230334Sken	exit (1);
164313369Smav}
165230334Sken