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 * 4. Neither the name of the University nor the names of its contributors
141556Srgrimes *    may be used to endorse or promote products derived from this software
151556Srgrimes *    without specific prior written permission.
161556Srgrimes *
171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271556Srgrimes * SUCH DAMAGE.
281556Srgrimes */
291556Srgrimes
30114433Sobrien#if 0
311556Srgrimes#ifndef lint
3220424Sstevestatic char const copyright[] =
331556Srgrimes"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
341556Srgrimes	The Regents of the University of California.  All rights reserved.\n";
351556Srgrimes#endif /* not lint */
361556Srgrimes
371556Srgrimes#ifndef lint
3836152Scharnierstatic char sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
39114433Sobrien#endif /* not lint */
4036152Scharnier#endif
4199110Sobrien#include <sys/cdefs.h>
4299110Sobrien__FBSDID("$FreeBSD$");
431556Srgrimes
441556Srgrimes#include <sys/types.h>
451556Srgrimes
461556Srgrimes#include <ctype.h>
471556Srgrimes#include <err.h>
481556Srgrimes#include <errno.h>
491556Srgrimes#include <fcntl.h>
501556Srgrimes#include <stdio.h>
511556Srgrimes#include <stdlib.h>
521556Srgrimes#include <string.h>
531556Srgrimes#include <unistd.h>
541556Srgrimes
551556Srgrimes#include "stty.h"
561556Srgrimes#include "extern.h"
571556Srgrimes
581556Srgrimesint
5990111Simpmain(int argc, char *argv[])
601556Srgrimes{
611556Srgrimes	struct info i;
621556Srgrimes	enum FMT fmt;
631556Srgrimes	int ch;
64221372Sru	const char *file;
651556Srgrimes
661556Srgrimes	fmt = NOTSET;
671556Srgrimes	i.fd = STDIN_FILENO;
68221372Sru	file = "stdin";
691556Srgrimes
701556Srgrimes	opterr = 0;
711556Srgrimes	while (optind < argc &&
721556Srgrimes	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
7324348Simp	    (ch = getopt(argc, argv, "aef:g")) != -1)
741556Srgrimes		switch(ch) {
751556Srgrimes		case 'a':		/* undocumented: POSIX compatibility */
761556Srgrimes			fmt = POSIX;
771556Srgrimes			break;
781556Srgrimes		case 'e':
791556Srgrimes			fmt = BSD;
801556Srgrimes			break;
811556Srgrimes		case 'f':
821556Srgrimes			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
831556Srgrimes				err(1, "%s", optarg);
84221372Sru			file = optarg;
851556Srgrimes			break;
861556Srgrimes		case 'g':
871556Srgrimes			fmt = GFLAG;
881556Srgrimes			break;
891556Srgrimes		case '?':
901556Srgrimes		default:
911556Srgrimes			goto args;
921556Srgrimes		}
931556Srgrimes
941556Srgrimesargs:	argc -= optind;
951556Srgrimes	argv += optind;
961556Srgrimes
979393Sbde	if (tcgetattr(i.fd, &i.t) < 0)
98221372Sru		errx(1, "%s isn't a terminal", file);
991556Srgrimes	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
1001556Srgrimes		err(1, "TIOCGETD");
1011556Srgrimes	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
10259790Sache		warn("TIOCGWINSZ");
1031556Srgrimes
1041556Srgrimes	checkredirect();			/* conversion aid */
1051556Srgrimes
1061556Srgrimes	switch(fmt) {
1071556Srgrimes	case NOTSET:
1081556Srgrimes		if (*argv)
1091556Srgrimes			break;
1101556Srgrimes		/* FALLTHROUGH */
1111556Srgrimes	case BSD:
1121556Srgrimes	case POSIX:
11359788Sache		print(&i.t, &i.win, i.ldisc, fmt);
1141556Srgrimes		break;
1151556Srgrimes	case GFLAG:
11659788Sache		gprint(&i.t, &i.win, i.ldisc);
1171556Srgrimes		break;
1181556Srgrimes	}
1198855Srgrimes
12059788Sache	for (i.set = i.wset = 0; *argv; ++argv) {
1211556Srgrimes		if (ksearch(&argv, &i))
1221556Srgrimes			continue;
1231556Srgrimes
1241556Srgrimes		if (csearch(&argv, &i))
1251556Srgrimes			continue;
1261556Srgrimes
1271556Srgrimes		if (msearch(&argv, &i))
1281556Srgrimes			continue;
1291556Srgrimes
1301556Srgrimes		if (isdigit(**argv)) {
13176810Skris			speed_t speed;
1321556Srgrimes
1331556Srgrimes			speed = atoi(*argv);
1341556Srgrimes			cfsetospeed(&i.t, speed);
1351556Srgrimes			cfsetispeed(&i.t, speed);
1361556Srgrimes			i.set = 1;
1371556Srgrimes			continue;
1381556Srgrimes		}
1391556Srgrimes
1401556Srgrimes		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
14159788Sache			gread(&i.t, *argv + sizeof("gfmt1") - 1);
14259788Sache			i.set = 1;
1431556Srgrimes			continue;
1441556Srgrimes		}
1451556Srgrimes
1461556Srgrimes		warnx("illegal option -- %s", *argv);
1471556Srgrimes		usage();
1481556Srgrimes	}
1491556Srgrimes
1501556Srgrimes	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
1511556Srgrimes		err(1, "tcsetattr");
1521556Srgrimes	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
1531556Srgrimes		warn("TIOCSWINSZ");
1541556Srgrimes	exit(0);
1551556Srgrimes}
1561556Srgrimes
1571556Srgrimesvoid
15890111Simpusage(void)
1591556Srgrimes{
1601556Srgrimes
161141578Sru	(void)fprintf(stderr,
162141578Sru	    "usage: stty [-a | -e | -g] [-f file] [arguments]\n");
1631556Srgrimes	exit (1);
1641556Srgrimes}
165