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