stty.c revision 59790
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.
321556Srgrimes */
331556Srgrimes
341556Srgrimes#ifndef lint
3520424Sstevestatic char const copyright[] =
361556Srgrimes"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
371556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381556Srgrimes#endif /* not lint */
391556Srgrimes
401556Srgrimes#ifndef lint
4136152Scharnier#if 0
4236152Scharnierstatic char sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
4336152Scharnier#endif
4436152Scharnierstatic const char rcsid[] =
4550471Speter  "$FreeBSD: head/bin/stty/stty.c 59790 2000-04-30 17:12:49Z ache $";
461556Srgrimes#endif /* not lint */
471556Srgrimes
481556Srgrimes#include <sys/types.h>
491556Srgrimes
501556Srgrimes#include <ctype.h>
511556Srgrimes#include <err.h>
521556Srgrimes#include <errno.h>
531556Srgrimes#include <fcntl.h>
541556Srgrimes#include <stdio.h>
551556Srgrimes#include <stdlib.h>
561556Srgrimes#include <string.h>
571556Srgrimes#include <unistd.h>
581556Srgrimes
591556Srgrimes#include "stty.h"
601556Srgrimes#include "extern.h"
611556Srgrimes
621556Srgrimesint
638855Srgrimesmain(argc, argv)
641556Srgrimes	int argc;
651556Srgrimes	char *argv[];
661556Srgrimes{
671556Srgrimes	struct info i;
681556Srgrimes	enum FMT fmt;
691556Srgrimes	int ch;
701556Srgrimes
711556Srgrimes	fmt = NOTSET;
721556Srgrimes	i.fd = STDIN_FILENO;
731556Srgrimes
741556Srgrimes	opterr = 0;
751556Srgrimes	while (optind < argc &&
761556Srgrimes	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
7724348Simp	    (ch = getopt(argc, argv, "aef:g")) != -1)
781556Srgrimes		switch(ch) {
791556Srgrimes		case 'a':		/* undocumented: POSIX compatibility */
801556Srgrimes			fmt = POSIX;
811556Srgrimes			break;
821556Srgrimes		case 'e':
831556Srgrimes			fmt = BSD;
841556Srgrimes			break;
851556Srgrimes		case 'f':
861556Srgrimes			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
871556Srgrimes				err(1, "%s", optarg);
881556Srgrimes			break;
891556Srgrimes		case 'g':
901556Srgrimes			fmt = GFLAG;
911556Srgrimes			break;
921556Srgrimes		case '?':
931556Srgrimes		default:
941556Srgrimes			goto args;
951556Srgrimes		}
961556Srgrimes
971556Srgrimesargs:	argc -= optind;
981556Srgrimes	argv += optind;
991556Srgrimes
1009393Sbde	if (tcgetattr(i.fd, &i.t) < 0)
1019393Sbde		errx(1, "stdin isn't a terminal");
1021556Srgrimes	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
1031556Srgrimes		err(1, "TIOCGETD");
1041556Srgrimes	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
10559790Sache		warn("TIOCGWINSZ");
1061556Srgrimes
1071556Srgrimes	checkredirect();			/* conversion aid */
1081556Srgrimes
1091556Srgrimes	switch(fmt) {
1101556Srgrimes	case NOTSET:
1111556Srgrimes		if (*argv)
1121556Srgrimes			break;
1131556Srgrimes		/* FALLTHROUGH */
1141556Srgrimes	case BSD:
1151556Srgrimes	case POSIX:
11659788Sache		print(&i.t, &i.win, i.ldisc, fmt);
1171556Srgrimes		break;
1181556Srgrimes	case GFLAG:
11959788Sache		gprint(&i.t, &i.win, i.ldisc);
1201556Srgrimes		break;
1211556Srgrimes	}
1228855Srgrimes
12359788Sache	for (i.set = i.wset = 0; *argv; ++argv) {
1241556Srgrimes		if (ksearch(&argv, &i))
1251556Srgrimes			continue;
1261556Srgrimes
1271556Srgrimes		if (csearch(&argv, &i))
1281556Srgrimes			continue;
1291556Srgrimes
1301556Srgrimes		if (msearch(&argv, &i))
1311556Srgrimes			continue;
1321556Srgrimes
1331556Srgrimes		if (isdigit(**argv)) {
1341556Srgrimes			int speed;
1351556Srgrimes
1361556Srgrimes			speed = atoi(*argv);
1371556Srgrimes			cfsetospeed(&i.t, speed);
1381556Srgrimes			cfsetispeed(&i.t, speed);
1391556Srgrimes			i.set = 1;
1401556Srgrimes			continue;
1411556Srgrimes		}
1421556Srgrimes
1431556Srgrimes		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
14459788Sache			gread(&i.t, *argv + sizeof("gfmt1") - 1);
14559788Sache			i.set = 1;
1461556Srgrimes			continue;
1471556Srgrimes		}
1481556Srgrimes
1491556Srgrimes		warnx("illegal option -- %s", *argv);
1501556Srgrimes		usage();
1511556Srgrimes	}
1521556Srgrimes
1531556Srgrimes	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
1541556Srgrimes		err(1, "tcsetattr");
1551556Srgrimes	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
1561556Srgrimes		warn("TIOCSWINSZ");
1571556Srgrimes	exit(0);
1581556Srgrimes}
1591556Srgrimes
1601556Srgrimesvoid
1611556Srgrimesusage()
1621556Srgrimes{
1631556Srgrimes
16426468Scharnier	(void)fprintf(stderr, "usage: stty [-a|-e|-g] [-f file] [options]\n");
1651556Srgrimes	exit (1);
1661556Srgrimes}
167