key.c revision 1.6
1/* $NetBSD: key.c,v 1.6 2003/08/07 11:25:27 agc Exp $ */ 2 3/*- 4 * Copyright (c) 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32#include <sys/cdefs.h> 33#ifndef lint 34#if 0 35static char sccsid[] = "@(#)key.c 8.3 (Berkeley) 4/2/94"; 36#else 37__RCSID("$NetBSD: key.c,v 1.6 2003/08/07 11:25:27 agc Exp $"); 38#endif 39#endif /* not lint */ 40 41#include <sys/param.h> 42#include <sys/types.h> 43 44#include <errno.h> 45#include <stdlib.h> 46#include <stdio.h> 47#include <string.h> 48#include <syslog.h> 49#include <dirent.h> 50#include <termios.h> 51 52#include "lp.h" 53#include "extern.h" 54 55__BEGIN_DECLS 56static int 57 c_key(const void *, const void *); 58void f_cbreak(struct info *); 59void f_columns(struct info *); 60void f_dec(struct info *); 61void f_extproc(struct info *); 62void f_ispeed(struct info *); 63void f_nl(struct info *); 64void f_ospeed(struct info *); 65void f_raw(struct info *); 66void f_rows(struct info *); 67void f_sane(struct info *); 68void f_tty(struct info *); 69__END_DECLS 70 71static struct key { 72 char *name; /* name */ 73 void (*f)(struct info *); /* function */ 74#define F_NEEDARG 0x01 /* needs an argument */ 75#define F_OFFOK 0x02 /* can turn off */ 76 int flags; 77} const keys[] = { 78 { "cbreak", f_cbreak, F_OFFOK }, 79 { "cols", f_columns, F_NEEDARG }, 80 { "columns", f_columns, F_NEEDARG }, 81 { "cooked", f_sane, 0 }, 82 { "dec", f_dec, 0 }, 83 { "extproc", f_extproc, F_OFFOK }, 84 { "ispeed", f_ispeed, F_NEEDARG }, 85 { "new", f_tty, 0 }, 86 { "nl", f_nl, F_OFFOK }, 87 { "old", f_tty, 0 }, 88 { "ospeed", f_ospeed, F_NEEDARG }, 89 { "raw", f_raw, F_OFFOK }, 90 { "rows", f_rows, F_NEEDARG }, 91 { "sane", f_sane, 0 }, 92 { "tty", f_tty, 0 }, 93}; 94 95static int 96c_key(const void *a, const void *b) 97{ 98 99 return (strcmp(((struct key *)a)->name, ((struct key *)b)->name)); 100} 101 102int 103ksearch(char ***argvp, struct info *ip) 104{ 105 char *name; 106 struct key *kp, tmp; 107 108 name = **argvp; 109 if (*name == '-') { 110 ip->off = 1; 111 ++name; 112 } else 113 ip->off = 0; 114 115 tmp.name = name; 116 if (!(kp = (struct key *)bsearch(&tmp, keys, 117 sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key))) 118 return (0); 119 if (!(kp->flags & F_OFFOK) && ip->off) { 120 syslog(LOG_INFO, "%s: illegal option: %s", printer, name); 121 return (1); 122 } 123 if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) { 124 syslog(LOG_INFO, "%s: option requires an argument: %s", 125 printer, name); 126 return (1); 127 } 128 kp->f(ip); 129 return (1); 130} 131 132void 133f_cbreak(struct info *ip) 134{ 135 136 if (ip->off) 137 f_sane(ip); 138 else { 139 ip->t.c_iflag |= BRKINT|IXON|IMAXBEL; 140 ip->t.c_oflag |= OPOST; 141 ip->t.c_lflag |= ISIG|IEXTEN; 142 ip->t.c_lflag &= ~ICANON; 143 ip->set = 1; 144 } 145} 146 147void 148f_columns(struct info *ip) 149{ 150 151 ip->win.ws_col = atoi(ip->arg); 152 ip->wset = 1; 153} 154 155void 156f_dec(struct info *ip) 157{ 158 159 ip->t.c_cc[VERASE] = (u_char)0177; 160 ip->t.c_cc[VKILL] = CTRL('u'); 161 ip->t.c_cc[VINTR] = CTRL('c'); 162 ip->t.c_lflag &= ~ECHOPRT; 163 ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL; 164 ip->t.c_iflag &= ~IXANY; 165 ip->set = 1; 166} 167 168void 169f_extproc(struct info *ip) 170{ 171 172 if (ip->set) { 173 int tmp = 1; 174 (void)ioctl(ip->fd, TIOCEXT, &tmp); 175 } else { 176 int tmp = 0; 177 (void)ioctl(ip->fd, TIOCEXT, &tmp); 178 } 179} 180 181void 182f_ispeed(struct info *ip) 183{ 184 185 cfsetispeed(&ip->t, atoi(ip->arg)); 186 ip->set = 1; 187} 188 189void 190f_nl(struct info *ip) 191{ 192 193 if (ip->off) { 194 ip->t.c_iflag |= ICRNL; 195 ip->t.c_oflag |= ONLCR; 196 } else { 197 ip->t.c_iflag &= ~ICRNL; 198 ip->t.c_oflag &= ~ONLCR; 199 } 200 ip->set = 1; 201} 202 203void 204f_ospeed(struct info *ip) 205{ 206 207 cfsetospeed(&ip->t, atoi(ip->arg)); 208 ip->set = 1; 209} 210 211void 212f_raw(struct info *ip) 213{ 214 215 if (ip->off) 216 f_sane(ip); 217 else { 218 cfmakeraw(&ip->t); 219 ip->t.c_cflag &= ~(CSIZE|PARENB); 220 ip->t.c_cflag |= CS8; 221 ip->set = 1; 222 } 223} 224 225void 226f_rows(struct info *ip) 227{ 228 229 ip->win.ws_row = atoi(ip->arg); 230 ip->wset = 1; 231} 232 233void 234f_sane(struct info *ip) 235{ 236 237 ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & (CLOCAL|CRTSCTS|CDTRCTS)); 238 ip->t.c_iflag = TTYDEF_IFLAG; 239 ip->t.c_iflag |= ICRNL; 240 /* preserve user-preference flags in lflag */ 241#define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH) 242 ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP); 243 ip->t.c_oflag = TTYDEF_OFLAG; 244 ip->set = 1; 245} 246 247void 248f_tty(struct info *ip) 249{ 250 int tmp; 251 252 tmp = TTYDISC; 253 if (ioctl(0, TIOCSETD, &tmp) < 0) 254 syslog(LOG_ERR, "%s: ioctl(TIOCSETD): %m", printer); 255} 256