stty.c revision 20424
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1989, 1991, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
323044Sdg *
3320424Ssteve *	$Id: stty.c,v 1.6 1995/07/04 08:16:30 bde Exp $
341556Srgrimes */
351556Srgrimes
361556Srgrimes#ifndef lint
3720424Sstevestatic char const copyright[] =
381556Srgrimes"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
391556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
401556Srgrimes#endif /* not lint */
411556Srgrimes
421556Srgrimes#ifndef lint
4320424Sstevestatic char const sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
441556Srgrimes#endif /* not lint */
451556Srgrimes
461556Srgrimes#include <sys/types.h>
471556Srgrimes
481556Srgrimes#include <ctype.h>
491556Srgrimes#include <err.h>
501556Srgrimes#include <errno.h>
511556Srgrimes#include <fcntl.h>
521556Srgrimes#include <stdio.h>
531556Srgrimes#include <stdlib.h>
541556Srgrimes#include <string.h>
551556Srgrimes#include <unistd.h>
561556Srgrimes
571556Srgrimes#include "stty.h"
581556Srgrimes#include "extern.h"
591556Srgrimes
601556Srgrimesint
618855Srgrimesmain(argc, argv)
621556Srgrimes	int argc;
631556Srgrimes	char *argv[];
641556Srgrimes{
651556Srgrimes	struct info i;
661556Srgrimes	enum FMT fmt;
671556Srgrimes	int ch;
681556Srgrimes
691556Srgrimes	fmt = NOTSET;
701556Srgrimes	i.fd = STDIN_FILENO;
711556Srgrimes
721556Srgrimes	opterr = 0;
731556Srgrimes	while (optind < argc &&
741556Srgrimes	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
751556Srgrimes	    (ch = getopt(argc, argv, "aef:g")) != EOF)
761556Srgrimes		switch(ch) {
771556Srgrimes		case 'a':		/* undocumented: POSIX compatibility */
781556Srgrimes			fmt = POSIX;
791556Srgrimes			break;
801556Srgrimes		case 'e':
811556Srgrimes			fmt = BSD;
821556Srgrimes			break;
831556Srgrimes		case 'f':
841556Srgrimes			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
851556Srgrimes				err(1, "%s", optarg);
861556Srgrimes			break;
871556Srgrimes		case 'g':
881556Srgrimes			fmt = GFLAG;
891556Srgrimes			break;
901556Srgrimes		case '?':
911556Srgrimes		default:
921556Srgrimes			goto args;
931556Srgrimes		}
941556Srgrimes
951556Srgrimesargs:	argc -= optind;
961556Srgrimes	argv += optind;
971556Srgrimes
989393Sbde	if (tcgetattr(i.fd, &i.t) < 0)
999393Sbde		errx(1, "stdin isn't a terminal");
1001556Srgrimes	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
1011556Srgrimes		err(1, "TIOCGETD");
1021556Srgrimes	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
1031556Srgrimes		warn("TIOCGWINSZ: %s\n", strerror(errno));
1041556Srgrimes
1051556Srgrimes	checkredirect();			/* conversion aid */
1061556Srgrimes
1071556Srgrimes	switch(fmt) {
1081556Srgrimes	case NOTSET:
1091556Srgrimes		if (*argv)
1101556Srgrimes			break;
1111556Srgrimes		/* FALLTHROUGH */
1121556Srgrimes	case BSD:
1131556Srgrimes	case POSIX:
1141556Srgrimes		print(&i.t, &i.win, i.ldisc, fmt);
1151556Srgrimes		break;
1161556Srgrimes	case GFLAG:
1171556Srgrimes		gprint(&i.t, &i.win, i.ldisc);
1181556Srgrimes		break;
1191556Srgrimes	}
1208855Srgrimes
1211556Srgrimes	for (i.set = i.wset = 0; *argv; ++argv) {
1221556Srgrimes		if (ksearch(&argv, &i))
1231556Srgrimes			continue;
1241556Srgrimes
1251556Srgrimes		if (csearch(&argv, &i))
1261556Srgrimes			continue;
1271556Srgrimes
1281556Srgrimes		if (msearch(&argv, &i))
1291556Srgrimes			continue;
1301556Srgrimes
1311556Srgrimes		if (isdigit(**argv)) {
1321556Srgrimes			int speed;
1331556Srgrimes
1341556Srgrimes			speed = atoi(*argv);
1351556Srgrimes			cfsetospeed(&i.t, speed);
1361556Srgrimes			cfsetispeed(&i.t, speed);
1371556Srgrimes			i.set = 1;
1381556Srgrimes			continue;
1391556Srgrimes		}
1401556Srgrimes
1411556Srgrimes		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
1421556Srgrimes			gread(&i.t, *argv + sizeof("gfmt1") - 1);
1438168Sbde			i.set = 1;
1441556Srgrimes			continue;
1451556Srgrimes		}
1461556Srgrimes
1471556Srgrimes		warnx("illegal option -- %s", *argv);
1481556Srgrimes		usage();
1491556Srgrimes	}
1501556Srgrimes
1511556Srgrimes	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
1521556Srgrimes		err(1, "tcsetattr");
1531556Srgrimes	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
1541556Srgrimes		warn("TIOCSWINSZ");
1551556Srgrimes	exit(0);
1561556Srgrimes}
1571556Srgrimes
1581556Srgrimesvoid
1591556Srgrimesusage()
1601556Srgrimes{
1611556Srgrimes
1621556Srgrimes	(void)fprintf(stderr, "usage: stty: [-a|-e|-g] [-f file] [options]\n");
1631556Srgrimes	exit (1);
1641556Srgrimes}
165