stty.c revision 141578
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: head/bin/stty/stty.c 141578 2005-02-09 17:37:39Z ru $"); 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; 641556Srgrimes 651556Srgrimes fmt = NOTSET; 661556Srgrimes i.fd = STDIN_FILENO; 671556Srgrimes 681556Srgrimes opterr = 0; 691556Srgrimes while (optind < argc && 701556Srgrimes strspn(argv[optind], "-aefg") == strlen(argv[optind]) && 7124348Simp (ch = getopt(argc, argv, "aef:g")) != -1) 721556Srgrimes switch(ch) { 731556Srgrimes case 'a': /* undocumented: POSIX compatibility */ 741556Srgrimes fmt = POSIX; 751556Srgrimes break; 761556Srgrimes case 'e': 771556Srgrimes fmt = BSD; 781556Srgrimes break; 791556Srgrimes case 'f': 801556Srgrimes if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0) 811556Srgrimes err(1, "%s", optarg); 821556Srgrimes break; 831556Srgrimes case 'g': 841556Srgrimes fmt = GFLAG; 851556Srgrimes break; 861556Srgrimes case '?': 871556Srgrimes default: 881556Srgrimes goto args; 891556Srgrimes } 901556Srgrimes 911556Srgrimesargs: argc -= optind; 921556Srgrimes argv += optind; 931556Srgrimes 949393Sbde if (tcgetattr(i.fd, &i.t) < 0) 959393Sbde errx(1, "stdin isn't a terminal"); 961556Srgrimes if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0) 971556Srgrimes err(1, "TIOCGETD"); 981556Srgrimes if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0) 9959790Sache warn("TIOCGWINSZ"); 1001556Srgrimes 1011556Srgrimes checkredirect(); /* conversion aid */ 1021556Srgrimes 1031556Srgrimes switch(fmt) { 1041556Srgrimes case NOTSET: 1051556Srgrimes if (*argv) 1061556Srgrimes break; 1071556Srgrimes /* FALLTHROUGH */ 1081556Srgrimes case BSD: 1091556Srgrimes case POSIX: 11059788Sache print(&i.t, &i.win, i.ldisc, fmt); 1111556Srgrimes break; 1121556Srgrimes case GFLAG: 11359788Sache gprint(&i.t, &i.win, i.ldisc); 1141556Srgrimes break; 1151556Srgrimes } 1168855Srgrimes 11759788Sache for (i.set = i.wset = 0; *argv; ++argv) { 1181556Srgrimes if (ksearch(&argv, &i)) 1191556Srgrimes continue; 1201556Srgrimes 1211556Srgrimes if (csearch(&argv, &i)) 1221556Srgrimes continue; 1231556Srgrimes 1241556Srgrimes if (msearch(&argv, &i)) 1251556Srgrimes continue; 1261556Srgrimes 1271556Srgrimes if (isdigit(**argv)) { 12876810Skris speed_t speed; 1291556Srgrimes 1301556Srgrimes speed = atoi(*argv); 1311556Srgrimes cfsetospeed(&i.t, speed); 1321556Srgrimes cfsetispeed(&i.t, speed); 1331556Srgrimes i.set = 1; 1341556Srgrimes continue; 1351556Srgrimes } 1361556Srgrimes 1371556Srgrimes if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) { 13859788Sache gread(&i.t, *argv + sizeof("gfmt1") - 1); 13959788Sache i.set = 1; 1401556Srgrimes continue; 1411556Srgrimes } 1421556Srgrimes 1431556Srgrimes warnx("illegal option -- %s", *argv); 1441556Srgrimes usage(); 1451556Srgrimes } 1461556Srgrimes 1471556Srgrimes if (i.set && tcsetattr(i.fd, 0, &i.t) < 0) 1481556Srgrimes err(1, "tcsetattr"); 1491556Srgrimes if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0) 1501556Srgrimes warn("TIOCSWINSZ"); 1511556Srgrimes exit(0); 1521556Srgrimes} 1531556Srgrimes 1541556Srgrimesvoid 15590111Simpusage(void) 1561556Srgrimes{ 1571556Srgrimes 158141578Sru (void)fprintf(stderr, 159141578Sru "usage: stty [-a | -e | -g] [-f file] [arguments]\n"); 1601556Srgrimes exit (1); 1611556Srgrimes} 162