2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 static const char sccsid[] = "@(#)map.c 8.1 (Berkeley) 6/9/93";
40 #include <sys/types.h>
49 static speed_t tset_baudrate(char *);
51 /* Baud rate conditionals for mapping. */
60 struct map *next; /* Linked list of maps. */
61 char *porttype; /* Port type, or "" for any. */
62 char *type; /* Terminal type to select. */
63 int conditional; /* Baud rate conditionals bitmask. */
64 speed_t speed; /* Baud rate to compare against. */
67 static MAP *cur, *maplist;
71 * [port-type][test baudrate]:terminal-type
72 * The baud rate tests are: >, <, @, =, !
75 add_mapping(const char *port, char *arg)
78 char *copy, *p, *termp;
81 mapp = malloc(sizeof(MAP));
82 if (copy == NULL || mapp == NULL)
93 mapp->conditional = 0;
95 arg = strpbrk(arg, "><@=!:");
97 if (arg == NULL) { /* [?]term */
98 mapp->type = mapp->porttype;
99 mapp->porttype = NULL;
103 if (arg == mapp->porttype) /* [><@=! baud]:term */
104 termp = mapp->porttype = NULL;
108 for (;; ++arg) /* Optional conditionals. */
111 if (mapp->conditional & GT)
113 mapp->conditional |= LT;
116 if (mapp->conditional & LT)
118 mapp->conditional |= GT;
121 case '=': /* Not documented. */
122 mapp->conditional |= EQ;
125 mapp->conditional |= NOT;
131 next: if (*arg == ':') {
132 if (mapp->conditional)
135 } else { /* Optional baudrate. */
136 arg = strchr(p = arg, ':');
140 mapp->speed = tset_baudrate(p);
143 if (*arg == '\0') /* Non-optional type. */
148 /* Terminate porttype, if specified. */
152 /* If a NOT conditional, reverse the test. */
153 if (mapp->conditional & NOT)
154 mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
156 /* If user specified a port with an option flag, set it. */
159 badmopt: errx(1, "illegal -m option format: %s", copy);
160 mapp->porttype = strdup(port);
165 (void)printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
166 (void)printf("type: %s\n", mapp->type);
167 (void)printf("conditional: ");
169 if (mapp->conditional & GT) {
173 if (mapp->conditional & EQ) {
174 (void)printf("%sEQ", p);
177 if (mapp->conditional & LT)
178 (void)printf("%sLT", p);
179 (void)printf("\nspeed: %d\n", mapp->speed);
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
189 mapped(const char *type)
195 for (mapp = maplist; mapp; mapp = mapp->next)
196 if (mapp->porttype == NULL || !strcmp(mapp->porttype, type)) {
197 switch (mapp->conditional) {
198 case 0: /* No test specified. */
202 match = (Ospeed == mapp->speed);
205 match = (Ospeed >= mapp->speed);
208 match = (Ospeed > mapp->speed);
211 match = (Ospeed <= mapp->speed);
214 match = (Ospeed < mapp->speed);
220 /* No match found; return given type. */
224 typedef struct speeds {
229 static SPEEDS speeds[] = {
238 tset_baudrate(char *rate)
243 /* The baudrate number can be preceded by a 'B', which is ignored. */
247 for (sp = speeds; sp->string; ++sp)
248 if (!strcasecmp(rate, sp->string))
252 errx(1, "unknown baud rate %s", rate);