1 /* $NetBSD: key.c,v 1.4 2001/10/09 02:15:37 mjl 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 39 static char sccsid[] = "@(#)key.c 8.3 (Berkeley) 4/2/94"; 40 #else 41 __RCSID("$NetBSD: key.c,v 1.4 2001/10/09 02:15:37 mjl 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 60 static int 61 c_key __P((const void *, const void *)); 62 void f_cbreak __P((struct info *)); 63 void f_columns __P((struct info *)); 64 void f_dec __P((struct info *)); 65 void f_extproc __P((struct info *)); 66 void f_ispeed __P((struct info *)); 67 void f_nl __P((struct info *)); 68 void f_ospeed __P((struct info *)); 69 void f_raw __P((struct info *)); 70 void f_rows __P((struct info *)); 71 void f_sane __P((struct info *)); 72 void f_tty __P((struct info *)); 73 __END_DECLS 74 75 static 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 } const 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 99 static int 100 c_key(const void *a, const void *b) 101 { 102 103 return (strcmp(((struct key *)a)->name, ((struct key *)b)->name)); 104 } 105 106 int 107 ksearch(char ***argvp, struct info *ip) 108 { 109 char *name; 110 struct key *kp, tmp; 111 112 name = **argvp; 113 if (*name == '-') { 114 ip->off = 1; 115 ++name; 116 } else 117 ip->off = 0; 118 119 tmp.name = name; 120 if (!(kp = (struct key *)bsearch(&tmp, keys, 121 sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key))) 122 return (0); 123 if (!(kp->flags & F_OFFOK) && ip->off) { 124 syslog(LOG_INFO, "%s: illegal option: %s", printer, name); 125 return (1); 126 } 127 if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) { 128 syslog(LOG_INFO, "%s: option requires an argument: %s", 129 printer, name); 130 return (1); 131 } 132 kp->f(ip); 133 return (1); 134 } 135 136 void 137 f_cbreak(struct info *ip) 138 { 139 140 if (ip->off) 141 f_sane(ip); 142 else { 143 ip->t.c_iflag |= BRKINT|IXON|IMAXBEL; 144 ip->t.c_oflag |= OPOST; 145 ip->t.c_lflag |= ISIG|IEXTEN; 146 ip->t.c_lflag &= ~ICANON; 147 ip->set = 1; 148 } 149 } 150 151 void 152 f_columns(struct info *ip) 153 { 154 155 ip->win.ws_col = atoi(ip->arg); 156 ip->wset = 1; 157 } 158 159 void 160 f_dec(struct info *ip) 161 { 162 163 ip->t.c_cc[VERASE] = (u_char)0177; 164 ip->t.c_cc[VKILL] = CTRL('u'); 165 ip->t.c_cc[VINTR] = CTRL('c'); 166 ip->t.c_lflag &= ~ECHOPRT; 167 ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL; 168 ip->t.c_iflag &= ~IXANY; 169 ip->set = 1; 170 } 171 172 void 173 f_extproc(struct info *ip) 174 { 175 176 if (ip->set) { 177 int tmp = 1; 178 (void)ioctl(ip->fd, TIOCEXT, &tmp); 179 } else { 180 int tmp = 0; 181 (void)ioctl(ip->fd, TIOCEXT, &tmp); 182 } 183 } 184 185 void 186 f_ispeed(struct info *ip) 187 { 188 189 cfsetispeed(&ip->t, atoi(ip->arg)); 190 ip->set = 1; 191 } 192 193 void 194 f_nl(struct info *ip) 195 { 196 197 if (ip->off) { 198 ip->t.c_iflag |= ICRNL; 199 ip->t.c_oflag |= ONLCR; 200 } else { 201 ip->t.c_iflag &= ~ICRNL; 202 ip->t.c_oflag &= ~ONLCR; 203 } 204 ip->set = 1; 205 } 206 207 void 208 f_ospeed(struct info *ip) 209 { 210 211 cfsetospeed(&ip->t, atoi(ip->arg)); 212 ip->set = 1; 213 } 214 215 void 216 f_raw(struct info *ip) 217 { 218 219 if (ip->off) 220 f_sane(ip); 221 else { 222 cfmakeraw(&ip->t); 223 ip->t.c_cflag &= ~(CSIZE|PARENB); 224 ip->t.c_cflag |= CS8; 225 ip->set = 1; 226 } 227 } 228 229 void 230 f_rows(struct info *ip) 231 { 232 233 ip->win.ws_row = atoi(ip->arg); 234 ip->wset = 1; 235 } 236 237 void 238 f_sane(struct info *ip) 239 { 240 241 ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & (CLOCAL|CRTSCTS|CDTRCTS)); 242 ip->t.c_iflag = TTYDEF_IFLAG; 243 ip->t.c_iflag |= ICRNL; 244 /* preserve user-preference flags in lflag */ 245 #define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH) 246 ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP); 247 ip->t.c_oflag = TTYDEF_OFLAG; 248 ip->set = 1; 249 } 250 251 void 252 f_tty(struct info *ip) 253 { 254 int tmp; 255 256 tmp = TTYDISC; 257 if (ioctl(0, TIOCSETD, &tmp) < 0) 258 syslog(LOG_ERR, "%s: ioctl(TIOCSETD): %m", printer); 259 } 260