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 extern speed_t Ospeed;
48 speed_t tset_baudrate(char *);
50 /* Baud rate conditionals for mapping. */
59 struct map *next; /* Linked list of maps. */
60 char *porttype; /* Port type, or "" for any. */
61 char *type; /* Terminal type to select. */
62 int conditional; /* Baud rate conditionals bitmask. */
63 speed_t speed; /* Baud rate to compare against. */
70 * [port-type][test baudrate]:terminal-type
71 * The baud rate tests are: >, <, @, =, !
74 add_mapping(const char *port, char *arg)
77 char *copy, *p, *termp;
80 mapp = malloc(sizeof(MAP));
81 if (copy == NULL || mapp == NULL)
92 mapp->conditional = 0;
94 arg = strpbrk(arg, "><@=!:");
96 if (arg == NULL) { /* [?]term */
97 mapp->type = mapp->porttype;
98 mapp->porttype = NULL;
102 if (arg == mapp->porttype) /* [><@=! baud]:term */
103 termp = mapp->porttype = NULL;
107 for (;; ++arg) /* Optional conditionals. */
110 if (mapp->conditional & GT)
112 mapp->conditional |= LT;
115 if (mapp->conditional & LT)
117 mapp->conditional |= GT;
120 case '=': /* Not documented. */
121 mapp->conditional |= EQ;
124 mapp->conditional |= NOT;
130 next: if (*arg == ':') {
131 if (mapp->conditional)
134 } else { /* Optional baudrate. */
135 arg = index(p = arg, ':');
139 mapp->speed = tset_baudrate(p);
142 if (*arg == '\0') /* Non-optional type. */
147 /* Terminate porttype, if specified. */
151 /* If a NOT conditional, reverse the test. */
152 if (mapp->conditional & NOT)
153 mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
155 /* If user specified a port with an option flag, set it. */
158 badmopt: errx(1, "illegal -m option format: %s", copy);
159 mapp->porttype = strdup(port);
163 (void)printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
164 (void)printf("type: %s\n", mapp->type);
165 (void)printf("conditional: ");
167 if (mapp->conditional & GT) {
171 if (mapp->conditional & EQ) {
172 (void)printf("%sEQ", p);
175 if (mapp->conditional & LT)
176 (void)printf("%sLT", p);
177 (void)printf("\nspeed: %d\n", mapp->speed);
182 * Return the type of terminal to use for a port of type 'type', as specified
183 * by the first applicable mapping in 'map'. If no mappings apply, return
187 mapped(const char *type)
193 for (mapp = maplist; mapp; mapp = mapp->next)
194 if (mapp->porttype == NULL || !strcmp(mapp->porttype, type)) {
195 switch (mapp->conditional) {
196 case 0: /* No test specified. */
200 match = (Ospeed == mapp->speed);
203 match = (Ospeed >= mapp->speed);
206 match = (Ospeed > mapp->speed);
209 match = (Ospeed <= mapp->speed);
212 match = (Ospeed < mapp->speed);
218 /* No match found; return given type. */
222 typedef struct speeds {
236 tset_baudrate(char *rate)
241 /* The baudrate number can be preceded by a 'B', which is ignored. */
245 for (sp = speeds; sp->string; ++sp)
246 if (!strcasecmp(rate, sp->string))
250 errx(1, "unknown baud rate %s", rate);