stty.c revision 24348
135951Sbde/*-
215903Swosch * Copyright (c) 1989, 1991, 1993, 1994
315903Swosch *	The Regents of the University of California.  All rights reserved.
415903Swosch *
515903Swosch * Redistribution and use in source and binary forms, with or without
615903Swosch * modification, are permitted provided that the following conditions
715903Swosch * are met:
815903Swosch * 1. Redistributions of source code must retain the above copyright
915903Swosch *    notice, this list of conditions and the following disclaimer.
1015903Swosch * 2. Redistributions in binary form must reproduce the above copyright
1115903Swosch *    notice, this list of conditions and the following disclaimer in the
1215903Swosch *    documentation and/or other materials provided with the distribution.
1315903Swosch * 3. All advertising materials mentioning features or use of this software
1415903Swosch *    must display the following acknowledgement:
1515903Swosch *	This product includes software developed by the University of
1615903Swosch *	California, Berkeley and its contributors.
1715903Swosch * 4. Neither the name of the University nor the names of its contributors
1815903Swosch *    may be used to endorse or promote products derived from this software
1915903Swosch *    without specific prior written permission.
2015903Swosch *
2115903Swosch * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2215903Swosch * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2315903Swosch * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2415903Swosch * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2515903Swosch * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2615903Swosch * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2724861Sjkh * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2824861Sjkh * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291845Swollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3015903Swosch * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3114986Swosch * SUCH DAMAGE.
3214986Swosch *
3314986Swosch *	$Id: stty.c,v 1.9 1997/02/22 14:05:54 peter Exp $
3435951Sbde */
3535951Sbde
3635951Sbde#ifndef lint
3735951Sbdestatic char const copyright[] =
3835951Sbde"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
3935951Sbde	The Regents of the University of California.  All rights reserved.\n";
4035951Sbde#endif /* not lint */
4135951Sbde
4235951Sbde#ifndef lint
4335951Sbdestatic char const sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
4435951Sbde#endif /* not lint */
4535951Sbde
4635951Sbde#include <sys/types.h>
4735951Sbde
4835951Sbde#include <ctype.h>
4935951Sbde#include <err.h>
5035951Sbde#include <errno.h>
5135951Sbde#include <fcntl.h>
5235951Sbde#include <stdio.h>
5335951Sbde#include <stdlib.h>
5435951Sbde#include <string.h>
5535951Sbde#include <unistd.h>
5635951Sbde
5735951Sbde#include "stty.h"
5835951Sbde#include "extern.h"
5935951Sbde
6035951Sbdeint
6135951Sbdemain(argc, argv)
6235951Sbde	int argc;
6335951Sbde	char *argv[];
6435951Sbde{
6535951Sbde	struct info i;
6635951Sbde	enum FMT fmt;
6735951Sbde	int ch;
6835951Sbde
6935951Sbde	fmt = NOTSET;
7035951Sbde	i.fd = STDIN_FILENO;
7135951Sbde
7235951Sbde	opterr = 0;
731845Swollman	while (optind < argc &&
7430113Sjkh	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
7516663Sjkh	    (ch = getopt(argc, argv, "aef:g")) != -1)
7614986Swosch		switch(ch) {
7734181Sbde		case 'a':		/* undocumented: POSIX compatibility */
7834181Sbde			fmt = POSIX;
7914986Swosch			break;
8014986Swosch		case 'e':
8134181Sbde			fmt = BSD;
8234181Sbde			break;
8334181Sbde		case 'f':
8434181Sbde			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
8534181Sbde				err(1, "%s", optarg);
8614986Swosch			break;
8734181Sbde		case 'g':
8834181Sbde			fmt = GFLAG;
8934181Sbde			break;
9034181Sbde		case '?':
9114986Swosch		default:
9234181Sbde			goto args;
9314986Swosch		}
9434181Sbde
9534181Sbdeargs:	argc -= optind;
9614986Swosch	argv += optind;
9724750Sbde
9824750Sbde	if (tcgetattr(i.fd, &i.t) < 0)
9924750Sbde		errx(1, "stdin isn't a terminal");
10014986Swosch	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
10130113Sjkh		err(1, "TIOCGETD");
1021845Swollman	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
10330113Sjkh		warn("TIOCGWINSZ: %s\n", strerror(errno));
1041845Swollman
1051845Swollman	checkredirect();			/* conversion aid */
1061845Swollman
10730113Sjkh	switch(fmt) {
10830113Sjkh	case NOTSET:
10930113Sjkh		if (*argv)
1101845Swollman			break;
1111845Swollman		/* FALLTHROUGH */
1121845Swollman	case BSD:
1131845Swollman	case POSIX:
1141845Swollman		print(&i.t, &i.win, i.ldisc, fmt);
1151845Swollman		break;
11624861Sjkh	case GFLAG:
11724861Sjkh		gprint(&i.t, &i.win, i.ldisc);
11824861Sjkh		break;
11924861Sjkh	}
1201845Swollman
12116663Sjkh	for (i.set = i.wset = 0; *argv; ++argv) {
12234829Seivind		if (ksearch(&argv, &i))
12324861Sjkh			continue;
12434829Seivind
1251845Swollman		if (csearch(&argv, &i))
1261845Swollman			continue;
1271845Swollman
12816663Sjkh		if (msearch(&argv, &i))
12916663Sjkh			continue;
13028806Sbde
13134829Seivind		if (isdigit(**argv)) {
13224861Sjkh			int speed;
13334829Seivind
13416663Sjkh			speed = atoi(*argv);
13516663Sjkh			cfsetospeed(&i.t, speed);
13624861Sjkh			cfsetispeed(&i.t, speed);
137			i.set = 1;
138			continue;
139		}
140
141		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
142			gread(&i.t, *argv + sizeof("gfmt1") - 1);
143			i.set = 1;
144			continue;
145		}
146
147		warnx("illegal option -- %s", *argv);
148		usage();
149	}
150
151	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
152		err(1, "tcsetattr");
153	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
154		warn("TIOCSWINSZ");
155	exit(0);
156}
157
158void
159usage()
160{
161
162	(void)fprintf(stderr, "usage: stty: [-a|-e|-g] [-f file] [options]\n");
163	exit (1);
164}
165