1 /* $NetBSD: map.c,v 1.10 2003/08/07 11:16:48 agc Exp $ */ 2 3 /*- 4 * Copyright (c) 1991, 1993 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 35 static char sccsid[] = "@(#)map.c 8.1 (Berkeley) 6/9/93"; 36 #endif 37 __RCSID("$NetBSD: map.c,v 1.10 2003/08/07 11:16:48 agc Exp $"); 38 #endif /* not lint */ 39 40 #include <sys/types.h> 41 #include <err.h> 42 #include <errno.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <termios.h> 46 #include "extern.h" 47 48 int baudrate __P((char *)); 49 50 /* Baud rate conditionals for mapping. */ 51 #define GT 0x01 52 #define EQ 0x02 53 #define LT 0x04 54 #define NOT 0x08 55 #define GE (GT | EQ) 56 #define LE (LT | EQ) 57 58 typedef struct map { 59 struct map *next; /* Linked list of maps. */ 60 const char *porttype; /* Port type, or "" for any. */ 61 const char *type; /* Terminal type to select. */ 62 int conditional; /* Baud rate conditionals bitmask. */ 63 int speed; /* Baud rate to compare against. */ 64 } MAP; 65 66 MAP *cur, *maplist; 67 68 /* 69 * Syntax for -m: 70 * [port-type][test baudrate]:terminal-type 71 * The baud rate tests are: >, <, @, =, ! 72 */ 73 void 74 add_mapping(port, arg) 75 const char *port; 76 char *arg; 77 { 78 MAP *mapp; 79 char *copy, *p, *termp; 80 81 copy = strdup(arg); 82 mapp = malloc((u_int)sizeof(MAP)); 83 if (copy == NULL || mapp == NULL) 84 err(1, "malloc"); 85 mapp->next = NULL; 86 if (maplist == NULL) 87 cur = maplist = mapp; 88 else { 89 cur->next = mapp; 90 cur = mapp; 91 } 92 93 mapp->porttype = arg; 94 mapp->conditional = 0; 95 96 arg = strpbrk(arg, "><@=!:"); 97 98 if (arg == NULL) { /* [?]term */ 99 mapp->type = mapp->porttype; 100 mapp->porttype = NULL; 101 goto done; 102 } 103 104 if (arg == mapp->porttype) /* [><@=! baud]:term */ 105 mapp->porttype = termp = NULL; 106 else 107 termp = arg; 108 109 for (;; ++arg) /* Optional conditionals. */ 110 switch(*arg) { 111 case '<': 112 if (mapp->conditional & GT) 113 goto badmopt; 114 mapp->conditional |= LT; 115 break; 116 case '>': 117 if (mapp->conditional & LT) 118 goto badmopt; 119 mapp->conditional |= GT; 120 break; 121 case '@': 122 case '=': /* Not documented. */ 123 mapp->conditional |= EQ; 124 break; 125 case '!': 126 mapp->conditional |= NOT; 127 break; 128 default: 129 goto next; 130 } 131 132 next: if (*arg == ':') { 133 if (mapp->conditional) 134 goto badmopt; 135 ++arg; 136 } else { /* Optional baudrate. */ 137 arg = strchr(p = arg, ':'); 138 if (arg == NULL) 139 goto badmopt; 140 *arg++ = '\0'; 141 mapp->speed = baudrate(p); 142 } 143 144 if (*arg == '\0') /* Non-optional type. */ 145 goto badmopt; 146 147 mapp->type = arg; 148 149 /* Terminate porttype, if specified. */ 150 if (termp != NULL) 151 *termp = '\0'; 152 153 /* If a NOT conditional, reverse the test. */ 154 if (mapp->conditional & NOT) 155 mapp->conditional = ~mapp->conditional & (EQ | GT | LT); 156 157 /* If user specified a port with an option flag, set it. */ 158 done: if (port) { 159 if (mapp->porttype) 160 badmopt: errx(1, "illegal -m option format: %s", copy); 161 mapp->porttype = port; 162 } 163 164 #ifdef MAPDEBUG 165 (void)printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY"); 166 (void)printf("type: %s\n", mapp->type); 167 (void)printf("conditional: "); 168 p = ""; 169 if (mapp->conditional & GT) { 170 (void)printf("GT"); 171 p = "/"; 172 } 173 if (mapp->conditional & EQ) { 174 (void)printf("%sEQ", p); 175 p = "/"; 176 } 177 if (mapp->conditional & LT) 178 (void)printf("%sLT", p); 179 (void)printf("\nspeed: %d\n", mapp->speed); 180 #endif 181 } 182 183 /* 184 * Return the type of terminal to use for a port of type 'type', as specified 185 * by the first applicable mapping in 'map'. If no mappings apply, return 186 * 'type'. 187 */ 188 const char * 189 mapped(type) 190 const char *type; 191 { 192 MAP *mapp; 193 int match; 194 195 match = 0; 196 for (mapp = maplist; mapp; mapp = mapp->next) 197 if (mapp->porttype == NULL || !strcmp(mapp->porttype, type)) { 198 switch (mapp->conditional) { 199 case 0: /* No test specified. */ 200 match = 1; 201 break; 202 case EQ: 203 match = (ospeed == mapp->speed); 204 break; 205 case GE: 206 match = (ospeed >= mapp->speed); 207 break; 208 case GT: 209 match = (ospeed > mapp->speed); 210 break; 211 case LE: 212 match = (ospeed <= mapp->speed); 213 break; 214 case LT: 215 match = (ospeed < mapp->speed); 216 break; 217 } 218 if (match) 219 return (mapp->type); 220 } 221 /* No match found; return given type. */ 222 return (type); 223 } 224 225 int 226 baudrate(rate) 227 char *rate; 228 { 229 230 /* The baudrate number can be preceded by a 'B', which is ignored. */ 231 if (*rate == 'B') 232 ++rate; 233 234 return (atoi(rate)); 235 } 236