stty.c revision 114433
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 34114433Sobrien#if 0 351556Srgrimes#ifndef lint 3620424Sstevestatic char const copyright[] = 371556Srgrimes"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\ 381556Srgrimes The Regents of the University of California. All rights reserved.\n"; 391556Srgrimes#endif /* not lint */ 401556Srgrimes 411556Srgrimes#ifndef lint 4236152Scharnierstatic char sccsid[] = "@(#)stty.c 8.3 (Berkeley) 4/2/94"; 43114433Sobrien#endif /* not lint */ 4436152Scharnier#endif 4599110Sobrien#include <sys/cdefs.h> 4699110Sobrien__FBSDID("$FreeBSD: head/bin/stty/stty.c 114433 2003-05-01 16:58:57Z obrien $"); 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 6390111Simpmain(int argc, 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]) && 7524348Simp (ch = getopt(argc, argv, "aef:g")) != -1) 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) 10359790Sache warn("TIOCGWINSZ"); 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: 11459788Sache print(&i.t, &i.win, i.ldisc, fmt); 1151556Srgrimes break; 1161556Srgrimes case GFLAG: 11759788Sache gprint(&i.t, &i.win, i.ldisc); 1181556Srgrimes break; 1191556Srgrimes } 1208855Srgrimes 12159788Sache 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)) { 13276810Skris speed_t 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)) { 14259788Sache gread(&i.t, *argv + sizeof("gfmt1") - 1); 14359788Sache 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 15990111Simpusage(void) 1601556Srgrimes{ 1611556Srgrimes 16226468Scharnier (void)fprintf(stderr, "usage: stty [-a|-e|-g] [-f file] [options]\n"); 1631556Srgrimes exit (1); 1641556Srgrimes} 165