1 /*- 2 * Copyright (c) 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)map.c 8.1 (Berkeley) 6/9/93 30 * $FreeBSD: src/usr.bin/tset/map.c,v 1.7 1999/08/30 08:27:30 peter Exp $ 31 * $DragonFly: src/usr.bin/tset/map.c,v 1.5 2004/08/30 18:06:50 eirikn Exp $ 32 */ 33 34 #include <sys/types.h> 35 #include <err.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <termios.h> 39 #include "extern.h" 40 41 extern speed_t Ospeed; 42 speed_t tset_baudrate(char *); 43 44 /* Baud rate conditionals for mapping. */ 45 #define GT 0x01 46 #define EQ 0x02 47 #define LT 0x04 48 #define NOT 0x08 49 #define GE (GT | EQ) 50 #define LE (LT | EQ) 51 52 typedef struct map { 53 struct map *next; /* Linked list of maps. */ 54 char *porttype; /* Port type, or "" for any. */ 55 char *type; /* Terminal type to select. */ 56 int conditional; /* Baud rate conditionals bitmask. */ 57 speed_t speed; /* Baud rate to compare against. */ 58 } MAP; 59 60 MAP *cur, *maplist; 61 62 /* 63 * Syntax for -m: 64 * [port-type][test baudrate]:terminal-type 65 * The baud rate tests are: >, <, @, =, ! 66 */ 67 void 68 add_mapping(const char *port, char *arg) 69 { 70 MAP *mapp; 71 char *copy, *p, *termp; 72 73 copy = strdup(arg); 74 mapp = malloc((u_int)sizeof(MAP)); 75 if (copy == NULL || mapp == NULL) 76 errx(1, "malloc"); 77 mapp->next = NULL; 78 if (maplist == NULL) 79 cur = maplist = mapp; 80 else { 81 cur->next = mapp; 82 cur = mapp; 83 } 84 85 mapp->porttype = arg; 86 mapp->conditional = 0; 87 88 arg = strpbrk(arg, "><@=!:"); 89 90 if (arg == NULL) { /* [?]term */ 91 mapp->type = mapp->porttype; 92 mapp->porttype = NULL; 93 goto done; 94 } 95 96 if (arg == mapp->porttype) /* [><@=! baud]:term */ 97 termp = mapp->porttype = NULL; 98 else 99 termp = arg; 100 101 for (;; ++arg) /* Optional conditionals. */ 102 switch(*arg) { 103 case '<': 104 if (mapp->conditional & GT) 105 goto badmopt; 106 mapp->conditional |= LT; 107 break; 108 case '>': 109 if (mapp->conditional & LT) 110 goto badmopt; 111 mapp->conditional |= GT; 112 break; 113 case '@': 114 case '=': /* Not documented. */ 115 mapp->conditional |= EQ; 116 break; 117 case '!': 118 mapp->conditional |= NOT; 119 break; 120 default: 121 goto next; 122 } 123 124 next: if (*arg == ':') { 125 if (mapp->conditional) 126 goto badmopt; 127 ++arg; 128 } else { /* Optional baudrate. */ 129 arg = strchr(p = arg, ':'); 130 if (arg == NULL) 131 goto badmopt; 132 *arg++ = '\0'; 133 mapp->speed = tset_baudrate(p); 134 } 135 136 if (*arg == '\0') /* Non-optional type. */ 137 goto badmopt; 138 139 mapp->type = arg; 140 141 /* Terminate porttype, if specified. */ 142 if (termp != NULL) 143 *termp = '\0'; 144 145 /* If a NOT conditional, reverse the test. */ 146 if (mapp->conditional & NOT) 147 mapp->conditional = ~mapp->conditional & (EQ | GT | LT); 148 149 /* If user specified a port with an option flag, set it. */ 150 done: if (port) { 151 if (mapp->porttype) 152 badmopt: errx(1, "illegal -m option format: %s", copy); 153 mapp->porttype = strdup(port); 154 } 155 156 #ifdef MAPDEBUG 157 (void)printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY"); 158 (void)printf("type: %s\n", mapp->type); 159 (void)printf("conditional: "); 160 p = ""; 161 if (mapp->conditional & GT) { 162 (void)printf("GT"); 163 p = "/"; 164 } 165 if (mapp->conditional & EQ) { 166 (void)printf("%sEQ", p); 167 p = "/"; 168 } 169 if (mapp->conditional & LT) 170 (void)printf("%sLT", p); 171 (void)printf("\nspeed: %d\n", mapp->speed); 172 #endif 173 } 174 175 /* 176 * Return the type of terminal to use for a port of type 'type', as specified 177 * by the first applicable mapping in 'map'. If no mappings apply, return 178 * 'type'. 179 */ 180 const char * 181 mapped(const char *type) 182 { 183 MAP *mapp; 184 int match; 185 186 match = 0; 187 for (mapp = maplist; mapp; mapp = mapp->next) 188 if (mapp->porttype == NULL || !strcmp(mapp->porttype, type)) { 189 switch (mapp->conditional) { 190 case 0: /* No test specified. */ 191 match = 1; 192 break; 193 case EQ: 194 match = (Ospeed == mapp->speed); 195 break; 196 case GE: 197 match = (Ospeed >= mapp->speed); 198 break; 199 case GT: 200 match = (Ospeed > mapp->speed); 201 break; 202 case LE: 203 match = (Ospeed <= mapp->speed); 204 break; 205 case LT: 206 match = (Ospeed < mapp->speed); 207 break; 208 } 209 if (match) 210 return (mapp->type); 211 } 212 /* No match found; return given type. */ 213 return (type); 214 } 215 216 typedef struct speeds { 217 const char *string; 218 speed_t speed; 219 } SPEEDS; 220 221 SPEEDS speeds[] = { 222 { "0", B0 }, 223 { "134.5", B134 }, 224 { "exta", B19200 }, 225 { "extb", B38400 }, 226 { NULL, 0 } 227 }; 228 229 speed_t 230 tset_baudrate(char *rate) 231 { 232 SPEEDS *sp; 233 speed_t speed; 234 235 /* The baudrate number can be preceded by a 'B', which is ignored. */ 236 if (*rate == 'B') 237 ++rate; 238 239 for (sp = speeds; sp->string; ++sp) 240 if (!strcasecmp(rate, sp->string)) 241 return (sp->speed); 242 speed = atol(rate); 243 if (speed == 0) 244 errx(1, "unknown baud rate %s", rate); 245 return speed; 246 } 247