key.c revision 1.4
11.4Smjl/* $NetBSD: key.c,v 1.4 2001/10/09 02:15:37 mjl Exp $ */ 21.1Shpeyerl 31.1Shpeyerl/*- 41.1Shpeyerl * Copyright (c) 1991, 1993, 1994 51.1Shpeyerl * The Regents of the University of California. All rights reserved. 61.1Shpeyerl * 71.1Shpeyerl * Redistribution and use in source and binary forms, with or without 81.1Shpeyerl * modification, are permitted provided that the following conditions 91.1Shpeyerl * are met: 101.1Shpeyerl * 1. Redistributions of source code must retain the above copyright 111.1Shpeyerl * notice, this list of conditions and the following disclaimer. 121.1Shpeyerl * 2. Redistributions in binary form must reproduce the above copyright 131.1Shpeyerl * notice, this list of conditions and the following disclaimer in the 141.1Shpeyerl * documentation and/or other materials provided with the distribution. 151.1Shpeyerl * 3. All advertising materials mentioning features or use of this software 161.1Shpeyerl * must display the following acknowledgement: 171.1Shpeyerl * This product includes software developed by the University of 181.1Shpeyerl * California, Berkeley and its contributors. 191.1Shpeyerl * 4. Neither the name of the University nor the names of its contributors 201.1Shpeyerl * may be used to endorse or promote products derived from this software 211.1Shpeyerl * without specific prior written permission. 221.1Shpeyerl * 231.1Shpeyerl * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 241.1Shpeyerl * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 251.1Shpeyerl * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 261.1Shpeyerl * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 271.1Shpeyerl * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 281.1Shpeyerl * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 291.1Shpeyerl * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 301.1Shpeyerl * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 311.1Shpeyerl * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 321.1Shpeyerl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 331.1Shpeyerl * SUCH DAMAGE. 341.1Shpeyerl */ 351.1Shpeyerl 361.2Smikel#include <sys/cdefs.h> 371.1Shpeyerl#ifndef lint 381.1Shpeyerl#if 0 391.1Shpeyerlstatic char sccsid[] = "@(#)key.c 8.3 (Berkeley) 4/2/94"; 401.1Shpeyerl#else 411.4Smjl__RCSID("$NetBSD: key.c,v 1.4 2001/10/09 02:15:37 mjl Exp $"); 421.1Shpeyerl#endif 431.1Shpeyerl#endif /* not lint */ 441.1Shpeyerl 451.1Shpeyerl#include <sys/param.h> 461.1Shpeyerl#include <sys/types.h> 471.1Shpeyerl 481.1Shpeyerl#include <errno.h> 491.1Shpeyerl#include <stdlib.h> 501.1Shpeyerl#include <stdio.h> 511.1Shpeyerl#include <string.h> 521.1Shpeyerl#include <syslog.h> 531.1Shpeyerl#include <dirent.h> 541.1Shpeyerl#include <termios.h> 551.1Shpeyerl 561.1Shpeyerl#include "lp.h" 571.1Shpeyerl#include "extern.h" 581.1Shpeyerl 591.1Shpeyerl__BEGIN_DECLS 601.2Smikelstatic int 611.2Smikel c_key __P((const void *, const void *)); 621.1Shpeyerlvoid f_cbreak __P((struct info *)); 631.1Shpeyerlvoid f_columns __P((struct info *)); 641.1Shpeyerlvoid f_dec __P((struct info *)); 651.1Shpeyerlvoid f_extproc __P((struct info *)); 661.1Shpeyerlvoid f_ispeed __P((struct info *)); 671.1Shpeyerlvoid f_nl __P((struct info *)); 681.1Shpeyerlvoid f_ospeed __P((struct info *)); 691.1Shpeyerlvoid f_raw __P((struct info *)); 701.1Shpeyerlvoid f_rows __P((struct info *)); 711.1Shpeyerlvoid f_sane __P((struct info *)); 721.1Shpeyerlvoid f_tty __P((struct info *)); 731.1Shpeyerl__END_DECLS 741.1Shpeyerl 751.1Shpeyerlstatic struct key { 761.1Shpeyerl char *name; /* name */ 771.1Shpeyerl void (*f) __P((struct info *)); /* function */ 781.1Shpeyerl#define F_NEEDARG 0x01 /* needs an argument */ 791.1Shpeyerl#define F_OFFOK 0x02 /* can turn off */ 801.1Shpeyerl int flags; 811.4Smjl} const keys[] = { 821.1Shpeyerl { "cbreak", f_cbreak, F_OFFOK }, 831.1Shpeyerl { "cols", f_columns, F_NEEDARG }, 841.1Shpeyerl { "columns", f_columns, F_NEEDARG }, 851.1Shpeyerl { "cooked", f_sane, 0 }, 861.1Shpeyerl { "dec", f_dec, 0 }, 871.1Shpeyerl { "extproc", f_extproc, F_OFFOK }, 881.1Shpeyerl { "ispeed", f_ispeed, F_NEEDARG }, 891.1Shpeyerl { "new", f_tty, 0 }, 901.1Shpeyerl { "nl", f_nl, F_OFFOK }, 911.1Shpeyerl { "old", f_tty, 0 }, 921.1Shpeyerl { "ospeed", f_ospeed, F_NEEDARG }, 931.1Shpeyerl { "raw", f_raw, F_OFFOK }, 941.1Shpeyerl { "rows", f_rows, F_NEEDARG }, 951.1Shpeyerl { "sane", f_sane, 0 }, 961.1Shpeyerl { "tty", f_tty, 0 }, 971.1Shpeyerl}; 981.1Shpeyerl 991.1Shpeyerlstatic int 1001.4Smjlc_key(const void *a, const void *b) 1011.1Shpeyerl{ 1021.1Shpeyerl 1031.1Shpeyerl return (strcmp(((struct key *)a)->name, ((struct key *)b)->name)); 1041.1Shpeyerl} 1051.1Shpeyerl 1061.1Shpeyerlint 1071.4Smjlksearch(char ***argvp, struct info *ip) 1081.1Shpeyerl{ 1091.1Shpeyerl char *name; 1101.1Shpeyerl struct key *kp, tmp; 1111.1Shpeyerl 1121.1Shpeyerl name = **argvp; 1131.1Shpeyerl if (*name == '-') { 1141.1Shpeyerl ip->off = 1; 1151.1Shpeyerl ++name; 1161.1Shpeyerl } else 1171.1Shpeyerl ip->off = 0; 1181.1Shpeyerl 1191.1Shpeyerl tmp.name = name; 1201.1Shpeyerl if (!(kp = (struct key *)bsearch(&tmp, keys, 1211.1Shpeyerl sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key))) 1221.1Shpeyerl return (0); 1231.1Shpeyerl if (!(kp->flags & F_OFFOK) && ip->off) { 1241.1Shpeyerl syslog(LOG_INFO, "%s: illegal option: %s", printer, name); 1251.1Shpeyerl return (1); 1261.1Shpeyerl } 1271.1Shpeyerl if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) { 1281.1Shpeyerl syslog(LOG_INFO, "%s: option requires an argument: %s", 1291.1Shpeyerl printer, name); 1301.1Shpeyerl return (1); 1311.1Shpeyerl } 1321.1Shpeyerl kp->f(ip); 1331.1Shpeyerl return (1); 1341.1Shpeyerl} 1351.1Shpeyerl 1361.1Shpeyerlvoid 1371.4Smjlf_cbreak(struct info *ip) 1381.1Shpeyerl{ 1391.1Shpeyerl 1401.1Shpeyerl if (ip->off) 1411.1Shpeyerl f_sane(ip); 1421.1Shpeyerl else { 1431.1Shpeyerl ip->t.c_iflag |= BRKINT|IXON|IMAXBEL; 1441.1Shpeyerl ip->t.c_oflag |= OPOST; 1451.1Shpeyerl ip->t.c_lflag |= ISIG|IEXTEN; 1461.1Shpeyerl ip->t.c_lflag &= ~ICANON; 1471.1Shpeyerl ip->set = 1; 1481.1Shpeyerl } 1491.1Shpeyerl} 1501.1Shpeyerl 1511.1Shpeyerlvoid 1521.4Smjlf_columns(struct info *ip) 1531.1Shpeyerl{ 1541.1Shpeyerl 1551.1Shpeyerl ip->win.ws_col = atoi(ip->arg); 1561.1Shpeyerl ip->wset = 1; 1571.1Shpeyerl} 1581.1Shpeyerl 1591.1Shpeyerlvoid 1601.4Smjlf_dec(struct info *ip) 1611.1Shpeyerl{ 1621.1Shpeyerl 1631.1Shpeyerl ip->t.c_cc[VERASE] = (u_char)0177; 1641.1Shpeyerl ip->t.c_cc[VKILL] = CTRL('u'); 1651.1Shpeyerl ip->t.c_cc[VINTR] = CTRL('c'); 1661.1Shpeyerl ip->t.c_lflag &= ~ECHOPRT; 1671.1Shpeyerl ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL; 1681.1Shpeyerl ip->t.c_iflag &= ~IXANY; 1691.1Shpeyerl ip->set = 1; 1701.1Shpeyerl} 1711.1Shpeyerl 1721.1Shpeyerlvoid 1731.4Smjlf_extproc(struct info *ip) 1741.1Shpeyerl{ 1751.1Shpeyerl 1761.1Shpeyerl if (ip->set) { 1771.1Shpeyerl int tmp = 1; 1781.1Shpeyerl (void)ioctl(ip->fd, TIOCEXT, &tmp); 1791.1Shpeyerl } else { 1801.1Shpeyerl int tmp = 0; 1811.1Shpeyerl (void)ioctl(ip->fd, TIOCEXT, &tmp); 1821.1Shpeyerl } 1831.1Shpeyerl} 1841.1Shpeyerl 1851.1Shpeyerlvoid 1861.4Smjlf_ispeed(struct info *ip) 1871.1Shpeyerl{ 1881.1Shpeyerl 1891.1Shpeyerl cfsetispeed(&ip->t, atoi(ip->arg)); 1901.1Shpeyerl ip->set = 1; 1911.1Shpeyerl} 1921.1Shpeyerl 1931.1Shpeyerlvoid 1941.4Smjlf_nl(struct info *ip) 1951.1Shpeyerl{ 1961.1Shpeyerl 1971.1Shpeyerl if (ip->off) { 1981.1Shpeyerl ip->t.c_iflag |= ICRNL; 1991.1Shpeyerl ip->t.c_oflag |= ONLCR; 2001.1Shpeyerl } else { 2011.1Shpeyerl ip->t.c_iflag &= ~ICRNL; 2021.1Shpeyerl ip->t.c_oflag &= ~ONLCR; 2031.1Shpeyerl } 2041.1Shpeyerl ip->set = 1; 2051.1Shpeyerl} 2061.1Shpeyerl 2071.1Shpeyerlvoid 2081.4Smjlf_ospeed(struct info *ip) 2091.1Shpeyerl{ 2101.1Shpeyerl 2111.1Shpeyerl cfsetospeed(&ip->t, atoi(ip->arg)); 2121.1Shpeyerl ip->set = 1; 2131.1Shpeyerl} 2141.1Shpeyerl 2151.1Shpeyerlvoid 2161.4Smjlf_raw(struct info *ip) 2171.1Shpeyerl{ 2181.1Shpeyerl 2191.1Shpeyerl if (ip->off) 2201.1Shpeyerl f_sane(ip); 2211.1Shpeyerl else { 2221.1Shpeyerl cfmakeraw(&ip->t); 2231.1Shpeyerl ip->t.c_cflag &= ~(CSIZE|PARENB); 2241.1Shpeyerl ip->t.c_cflag |= CS8; 2251.1Shpeyerl ip->set = 1; 2261.1Shpeyerl } 2271.1Shpeyerl} 2281.1Shpeyerl 2291.1Shpeyerlvoid 2301.4Smjlf_rows(struct info *ip) 2311.1Shpeyerl{ 2321.1Shpeyerl 2331.1Shpeyerl ip->win.ws_row = atoi(ip->arg); 2341.1Shpeyerl ip->wset = 1; 2351.1Shpeyerl} 2361.1Shpeyerl 2371.1Shpeyerlvoid 2381.4Smjlf_sane(struct info *ip) 2391.1Shpeyerl{ 2401.1Shpeyerl 2411.3Sscottr ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & (CLOCAL|CRTSCTS|CDTRCTS)); 2421.1Shpeyerl ip->t.c_iflag = TTYDEF_IFLAG; 2431.1Shpeyerl ip->t.c_iflag |= ICRNL; 2441.1Shpeyerl /* preserve user-preference flags in lflag */ 2451.1Shpeyerl#define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH) 2461.1Shpeyerl ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP); 2471.1Shpeyerl ip->t.c_oflag = TTYDEF_OFLAG; 2481.1Shpeyerl ip->set = 1; 2491.1Shpeyerl} 2501.1Shpeyerl 2511.1Shpeyerlvoid 2521.4Smjlf_tty(struct info *ip) 2531.1Shpeyerl{ 2541.1Shpeyerl int tmp; 2551.1Shpeyerl 2561.1Shpeyerl tmp = TTYDISC; 2571.1Shpeyerl if (ioctl(0, TIOCSETD, &tmp) < 0) 2581.1Shpeyerl syslog(LOG_ERR, "%s: ioctl(TIOCSETD): %m", printer); 2591.1Shpeyerl} 260