2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char sccsid[] = "@(#)map.c 8.1 (Berkeley) 6/9/93";
38 #include <sys/types.h>
47 static speed_t tset_baudrate(char *);
49 /* Baud rate conditionals for mapping. */
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. */
65 static MAP *cur, *maplist;
69 * [port-type][test baudrate]:terminal-type
70 * The baud rate tests are: >, <, @, =, !
73 add_mapping(const char *port, char *arg)
76 char *copy, *p, *termp;
79 mapp = malloc(sizeof(MAP));
80 if (copy == NULL || mapp == NULL)
91 mapp->conditional = 0;
93 arg = strpbrk(arg, "><@=!:");
95 if (arg == NULL) { /* [?]term */
96 mapp->type = mapp->porttype;
97 mapp->porttype = NULL;
101 if (arg == mapp->porttype) /* [><@=! baud]:term */
102 termp = mapp->porttype = NULL;
106 for (;; ++arg) /* Optional conditionals. */
109 if (mapp->conditional & GT)
111 mapp->conditional |= LT;
114 if (mapp->conditional & LT)
116 mapp->conditional |= GT;
119 case '=': /* Not documented. */
120 mapp->conditional |= EQ;
123 mapp->conditional |= NOT;
129 next: if (*arg == ':') {
130 if (mapp->conditional)
133 } else { /* Optional baudrate. */
134 arg = strchr(p = arg, ':');
138 mapp->speed = tset_baudrate(p);
141 if (*arg == '\0') /* Non-optional type. */
146 /* Terminate porttype, if specified. */
150 /* If a NOT conditional, reverse the test. */
151 if (mapp->conditional & NOT)
152 mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
154 /* If user specified a port with an option flag, set it. */
157 badmopt: errx(1, "illegal -m option format: %s", copy);
158 mapp->porttype = strdup(port);
162 (void)printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
163 (void)printf("type: %s\n", mapp->type);
164 (void)printf("conditional: ");
166 if (mapp->conditional & GT) {
170 if (mapp->conditional & EQ) {
171 (void)printf("%sEQ", p);
174 if (mapp->conditional & LT)
175 (void)printf("%sLT", p);
176 (void)printf("\nspeed: %d\n", mapp->speed);
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
186 mapped(const char *type)
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. */
199 match = (Ospeed == mapp->speed);
202 match = (Ospeed >= mapp->speed);
205 match = (Ospeed > mapp->speed);
208 match = (Ospeed <= mapp->speed);
211 match = (Ospeed < mapp->speed);
217 /* No match found; return given type. */
221 typedef struct speeds {
226 static SPEEDS speeds[] = {
235 tset_baudrate(char *rate)
240 /* The baudrate number can be preceded by a 'B', which is ignored. */
244 for (sp = speeds; sp->string; ++sp)
245 if (!strcasecmp(rate, sp->string))
249 errx(1, "unknown baud rate %s", rate);