stty.c revision 9384
15532Sjkh/*-
25531Sjkh * Copyright (c) 1989, 1991, 1993, 1994
35388Sache *	The Regents of the University of California.  All rights reserved.
45345Sbde *
55345Sbde * Redistribution and use in source and binary forms, with or without
65345Sbde * modification, are permitted provided that the following conditions
75345Sbde * are met:
85429Sache * 1. Redistributions of source code must retain the above copyright
95429Sache *    notice, this list of conditions and the following disclaimer.
105380Sache * 2. Redistributions in binary form must reproduce the above copyright
115345Sbde *    notice, this list of conditions and the following disclaimer in the
125382Sache *    documentation and/or other materials provided with the distribution.
135380Sache * 3. All advertising materials mentioning features or use of this software
145382Sache *    must display the following acknowledgement:
155345Sbde *	This product includes software developed by the University of
165382Sache *	California, Berkeley and its contributors.
175382Sache * 4. Neither the name of the University nor the names of its contributors
185382Sache *    may be used to endorse or promote products derived from this software
195382Sache *    without specific prior written permission.
205531Sjkh *
215531Sjkh * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
225531Sjkh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
235532Sjkh * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
245531Sjkh * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
255531Sjkh * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
265380Sache * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
275382Sache * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
285382Sache * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
295380Sache * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
305345Sbde * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
315380Sache * SUCH DAMAGE.
325345Sbde *
335380Sache *	$Id: stty.c,v 1.4 1995/05/30 00:07:28 rgrimes Exp $
345380Sache */
355380Sache
365380Sache#ifndef lint
375380Sachestatic char copyright[] =
385380Sache"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
395380Sache	The Regents of the University of California.  All rights reserved.\n";
405380Sache#endif /* not lint */
415380Sache
425380Sache#ifndef lint
435380Sachestatic char sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
445380Sache#endif /* not lint */
455380Sache
465380Sache#include <sys/types.h>
475380Sache
485380Sache#include <ctype.h>
495380Sache#include <err.h>
505380Sache#include <errno.h>
515380Sache#include <fcntl.h>
525380Sache#include <stdio.h>
535380Sache#include <stdlib.h>
545380Sache#include <string.h>
555380Sache#include <unistd.h>
565380Sache
575380Sache#include "stty.h"
585380Sache#include "extern.h"
595380Sache
605380Sacheint
615380Sachemain(argc, argv)
625380Sache	int argc;
635380Sache	char *argv[];
645380Sache{
655380Sache	struct info i;
665380Sache	enum FMT fmt;
675380Sache	int ch;
685380Sache
695380Sache	fmt = NOTSET;
705380Sache	i.fd = STDIN_FILENO;
715380Sache
725380Sache	opterr = 0;
735380Sache	while (optind < argc &&
745380Sache	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
755380Sache	    (ch = getopt(argc, argv, "aef:g")) != EOF)
765380Sache		switch(ch) {
77		case 'a':		/* undocumented: POSIX compatibility */
78			fmt = POSIX;
79			break;
80		case 'e':
81			fmt = BSD;
82			break;
83		case 'f':
84			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
85				err(1, "%s", optarg);
86			break;
87		case 'g':
88			fmt = GFLAG;
89			break;
90		case '?':
91		default:
92			goto args;
93		}
94
95args:	argc -= optind;
96	argv += optind;
97
98	if (tcgetattr(i.fd, &i.t) < 0) {
99		warn("tcgetattr: not running on a terminal");
100		exit(1);
101	}
102	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
103		err(1, "TIOCGETD");
104	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
105		warn("TIOCGWINSZ: %s\n", strerror(errno));
106
107	checkredirect();			/* conversion aid */
108
109	switch(fmt) {
110	case NOTSET:
111		if (*argv)
112			break;
113		/* FALLTHROUGH */
114	case BSD:
115	case POSIX:
116		print(&i.t, &i.win, i.ldisc, fmt);
117		break;
118	case GFLAG:
119		gprint(&i.t, &i.win, i.ldisc);
120		break;
121	}
122
123	for (i.set = i.wset = 0; *argv; ++argv) {
124		if (ksearch(&argv, &i))
125			continue;
126
127		if (csearch(&argv, &i))
128			continue;
129
130		if (msearch(&argv, &i))
131			continue;
132
133		if (isdigit(**argv)) {
134			int speed;
135
136			speed = atoi(*argv);
137			cfsetospeed(&i.t, speed);
138			cfsetispeed(&i.t, speed);
139			i.set = 1;
140			continue;
141		}
142
143		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
144			gread(&i.t, *argv + sizeof("gfmt1") - 1);
145			i.set = 1;
146			continue;
147		}
148
149		warnx("illegal option -- %s", *argv);
150		usage();
151	}
152
153	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
154		err(1, "tcsetattr");
155	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
156		warn("TIOCSWINSZ");
157	exit(0);
158}
159
160void
161usage()
162{
163
164	(void)fprintf(stderr, "usage: stty: [-a|-e|-g] [-f file] [options]\n");
165	exit (1);
166}
167