stty.c revision 9393
1169689Skan/*-
2169689Skan * Copyright (c) 1989, 1991, 1993, 1994
3169689Skan *	The Regents of the University of California.  All rights reserved.
4169689Skan *
5169689Skan * Redistribution and use in source and binary forms, with or without
6169689Skan * modification, are permitted provided that the following conditions
7169689Skan * are met:
8169689Skan * 1. Redistributions of source code must retain the above copyright
9169689Skan *    notice, this list of conditions and the following disclaimer.
10169689Skan * 2. Redistributions in binary form must reproduce the above copyright
11169689Skan *    notice, this list of conditions and the following disclaimer in the
12169689Skan *    documentation and/or other materials provided with the distribution.
13169689Skan * 3. All advertising materials mentioning features or use of this software
14169689Skan *    must display the following acknowledgement:
15169689Skan *	This product includes software developed by the University of
16169689Skan *	California, Berkeley and its contributors.
17169689Skan * 4. Neither the name of the University nor the names of its contributors
18169689Skan *    may be used to endorse or promote products derived from this software
19169689Skan *    without specific prior written permission.
20169689Skan *
21169689Skan * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24169689Skan * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25169689Skan * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26169689Skan * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27169689Skan * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28169689Skan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29169689Skan * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30169689Skan * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31169689Skan * SUCH DAMAGE.
32169689Skan *
33169689Skan *	$Id: stty.c,v 1.5 1995/07/02 08:54:27 joerg Exp $
34169689Skan */
35169689Skan
36169689Skan#ifndef lint
37169689Skanstatic char copyright[] =
38169689Skan"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
39169689Skan	The Regents of the University of California.  All rights reserved.\n";
40169689Skan#endif /* not lint */
41169689Skan
42169689Skan#ifndef lint
43169689Skanstatic char sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
44169689Skan#endif /* not lint */
45169689Skan
46169689Skan#include <sys/types.h>
47169689Skan
48169689Skan#include <ctype.h>
49169689Skan#include <err.h>
50169689Skan#include <errno.h>
51169689Skan#include <fcntl.h>
52169689Skan#include <stdio.h>
53169689Skan#include <stdlib.h>
54169689Skan#include <string.h>
55169689Skan#include <unistd.h>
56169689Skan
57169689Skan#include "stty.h"
58169689Skan#include "extern.h"
59169689Skan
60169689Skanint
61169689Skanmain(argc, argv)
62169689Skan	int argc;
63169689Skan	char *argv[];
64169689Skan{
65169689Skan	struct info i;
66169689Skan	enum FMT fmt;
67169689Skan	int ch;
68169689Skan
69169689Skan	fmt = NOTSET;
70169689Skan	i.fd = STDIN_FILENO;
71169689Skan
72169689Skan	opterr = 0;
73169689Skan	while (optind < argc &&
74169689Skan	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
75169689Skan	    (ch = getopt(argc, argv, "aef:g")) != EOF)
76169689Skan		switch(ch) {
77169689Skan		case 'a':		/* undocumented: POSIX compatibility */
78169689Skan			fmt = POSIX;
79169689Skan			break;
80169689Skan		case 'e':
81169689Skan			fmt = BSD;
82169689Skan			break;
83169689Skan		case 'f':
84169689Skan			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
85169689Skan				err(1, "%s", optarg);
86169689Skan			break;
87169689Skan		case 'g':
88169689Skan			fmt = GFLAG;
89169689Skan			break;
90169689Skan		case '?':
91169689Skan		default:
92169689Skan			goto args;
93169689Skan		}
94169689Skan
95169689Skanargs:	argc -= optind;
96169689Skan	argv += optind;
97169689Skan
98169689Skan	if (tcgetattr(i.fd, &i.t) < 0)
99169689Skan		errx(1, "stdin isn't a terminal");
100169689Skan	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
101169689Skan		err(1, "TIOCGETD");
102169689Skan	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
103169689Skan		warn("TIOCGWINSZ: %s\n", strerror(errno));
104169689Skan
105169689Skan	checkredirect();			/* conversion aid */
106169689Skan
107169689Skan	switch(fmt) {
108169689Skan	case NOTSET:
109169689Skan		if (*argv)
110169689Skan			break;
111169689Skan		/* FALLTHROUGH */
112169689Skan	case BSD:
113169689Skan	case POSIX:
114169689Skan		print(&i.t, &i.win, i.ldisc, fmt);
115169689Skan		break;
116169689Skan	case GFLAG:
117169689Skan		gprint(&i.t, &i.win, i.ldisc);
118169689Skan		break;
119169689Skan	}
120169689Skan
121169689Skan	for (i.set = i.wset = 0; *argv; ++argv) {
122169689Skan		if (ksearch(&argv, &i))
123169689Skan			continue;
124169689Skan
125169689Skan		if (csearch(&argv, &i))
126169689Skan			continue;
127169689Skan
128169689Skan		if (msearch(&argv, &i))
129169689Skan			continue;
130169689Skan
131169689Skan		if (isdigit(**argv)) {
132169689Skan			int speed;
133169689Skan
134169689Skan			speed = atoi(*argv);
135169689Skan			cfsetospeed(&i.t, speed);
136169689Skan			cfsetispeed(&i.t, speed);
137169689Skan			i.set = 1;
138169689Skan			continue;
139169689Skan		}
140169689Skan
141169689Skan		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
142169689Skan			gread(&i.t, *argv + sizeof("gfmt1") - 1);
143261188Spfg			i.set = 1;
144261188Spfg			continue;
145169689Skan		}
146169689Skan
147169689Skan		warnx("illegal option -- %s", *argv);
148169689Skan		usage();
149169689Skan	}
150169689Skan
151169689Skan	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
152169689Skan		err(1, "tcsetattr");
153169689Skan	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
154169689Skan		warn("TIOCSWINSZ");
155169689Skan	exit(0);
156169689Skan}
157169689Skan
158169689Skanvoid
159169689Skanusage()
160169689Skan{
161169689Skan
162169689Skan	(void)fprintf(stderr, "usage: stty: [-a|-e|-g] [-f file] [options]\n");
163169689Skan	exit (1);
164169689Skan}
165169689Skan