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