stty.c revision 1.22
1234353Sdim/* $NetBSD: stty.c,v 1.22 2012/06/20 10:09:43 wiz Exp $ */
2193323Sed
3193323Sed/*-
4193323Sed * Copyright (c) 1989, 1991, 1993, 1994
5193323Sed *	The Regents of the University of California.  All rights reserved.
6193323Sed *
7193323Sed * Redistribution and use in source and binary forms, with or without
8193323Sed * modification, are permitted provided that the following conditions
9193323Sed * are met:
10193323Sed * 1. Redistributions of source code must retain the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer.
12193323Sed * 2. Redistributions in binary form must reproduce the above copyright
13193323Sed *    notice, this list of conditions and the following disclaimer in the
14193323Sed *    documentation and/or other materials provided with the distribution.
15249423Sdim * 3. Neither the name of the University nor the names of its contributors
16276479Sdim *    may be used to endorse or promote products derived from this software
17193323Sed *    without specific prior written permission.
18280031Sdim *
19249423Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20249423Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21249423Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22249423Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23193323Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24193323Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25193323Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26193323Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27249423Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28249423Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29249423Sdim * SUCH DAMAGE.
30249423Sdim */
31276479Sdim
32249423Sdim#include <sys/cdefs.h>
33218893Sdim#ifndef lint
34193323Sed__COPYRIGHT("@(#) Copyright (c) 1989, 1991, 1993, 1994\
35193323Sed The Regents of the University of California.  All rights reserved.");
36193323Sed#endif /* not lint */
37276479Sdim
38276479Sdim#ifndef lint
39276479Sdim#if 0
40276479Sdimstatic char sccsid[] = "@(#)stty.c	8.3 (Berkeley) 4/2/94";
41224145Sdim#else
42224145Sdim__RCSID("$NetBSD: stty.c,v 1.22 2012/06/20 10:09:43 wiz Exp $");
43224145Sdim#endif
44261991Sdim#endif /* not lint */
45261991Sdim
46193323Sed#include <sys/types.h>
47193323Sed
48193323Sed#include <ctype.h>
49193323Sed#include <err.h>
50193323Sed#include <errno.h>
51193323Sed#include <fcntl.h>
52193323Sed#include <locale.h>
53193323Sed#include <stdio.h>
54193323Sed#include <stdlib.h>
55193323Sed#include <string.h>
56193323Sed#include <unistd.h>
57193323Sed
58193323Sed#include "stty.h"
59193323Sed#include "extern.h"
60193323Sed
61276479Sdimint main(int, char *[]);
62276479Sdim
63276479Sdimint
64276479Sdimmain(int argc, char *argv[])
65276479Sdim{
66276479Sdim	struct info i;
67276479Sdim	enum FMT fmt;
68276479Sdim	int ch;
69276479Sdim
70276479Sdim	setprogname(argv[0]);
71276479Sdim	(void)setlocale(LC_ALL, "");
72276479Sdim
73276479Sdim	fmt = STTY_NOTSET;
74276479Sdim	i.fd = STDIN_FILENO;
75276479Sdim
76276479Sdim	opterr = 0;
77276479Sdim	while (optind < argc &&
78276479Sdim	    strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
79276479Sdim	    (ch = getopt(argc, argv, "aef:g")) != -1)
80276479Sdim		switch(ch) {
81276479Sdim		case 'a':		/* undocumented: POSIX compatibility */
82276479Sdim			fmt = STTY_POSIX;
83276479Sdim			break;
84276479Sdim		case 'e':
85276479Sdim			fmt = STTY_BSD;
86276479Sdim			break;
87276479Sdim		case 'f':
88276479Sdim			if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
89276479Sdim				err(1, "%s", optarg);
90276479Sdim			break;
91276479Sdim		case 'g':
92276479Sdim			fmt = STTY_GFLAG;
93276479Sdim			break;
94276479Sdim		case '?':
95276479Sdim		default:
96276479Sdim			goto args;
97276479Sdim		}
98276479Sdim
99276479Sdimargs:	argc -= optind;
100276479Sdim	argv += optind;
101276479Sdim
102280031Sdim	if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
103276479Sdim		err(1, "TIOCGETD");
104276479Sdim	if (tcgetattr(i.fd, &i.t) < 0)
105276479Sdim		err(1, "tcgetattr");
106276479Sdim	if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
107276479Sdim		warn("TIOCGWINSZ");
108276479Sdim
109276479Sdim	switch(fmt) {
110276479Sdim	case STTY_NOTSET:
111276479Sdim		if (*argv)
112276479Sdim			break;
113276479Sdim		/* FALLTHROUGH */
114276479Sdim	case STTY_BSD:
115276479Sdim	case STTY_POSIX:
116276479Sdim		print(&i.t, &i.win, i.ldisc, fmt);
117276479Sdim		break;
118276479Sdim	case STTY_GFLAG:
119276479Sdim		gprint(&i.t);
120276479Sdim		break;
121276479Sdim	}
122276479Sdim
123276479Sdim	for (i.set = i.wset = 0; *argv; ++argv) {
124276479Sdim		if (ksearch(&argv, &i))
125276479Sdim			continue;
126276479Sdim
127276479Sdim		if (csearch(&argv, &i))
128276479Sdim			continue;
129276479Sdim
130276479Sdim		if (msearch(&argv, &i))
131276479Sdim			continue;
132276479Sdim
133276479Sdim		if (isdigit((unsigned char)**argv)) {
134276479Sdim			int speed;
135276479Sdim
136276479Sdim			speed = atoi(*argv);
137276479Sdim			cfsetospeed(&i.t, speed);
138276479Sdim			cfsetispeed(&i.t, speed);
139276479Sdim			i.set = 1;
140276479Sdim			continue;
141276479Sdim		}
142276479Sdim
143276479Sdim		if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
144276479Sdim			gread(&i.t, *argv + sizeof("gfmt1") - 1);
145276479Sdim			i.set = 1;
146276479Sdim			continue;
147276479Sdim		}
148276479Sdim
149276479Sdim		warnx("illegal option -- %s", *argv);
150276479Sdim		usage();
151276479Sdim	}
152276479Sdim
153276479Sdim	if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
154276479Sdim		err(1, "tcsetattr");
155276479Sdim	if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
156276479Sdim		warn("TIOCSWINSZ");
157276479Sdim	exit(0);
158276479Sdim	/* NOTREACHED */
159276479Sdim}
160276479Sdim
161276479Sdimvoid
162276479Sdimusage(void)
163276479Sdim{
164276479Sdim
165276479Sdim	(void)fprintf(stderr, "usage: %s [-a|-e|-g] [-f file] [operand ...]\n", getprogname());
166276479Sdim	exit(1);
167276479Sdim	/* NOTREACHED */
168276479Sdim}
169276479Sdim