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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 static const char sccsid[] = "@(#)map.c 8.1 (Berkeley) 6/9/93";
42 #include <sys/types.h>
51 extern speed_t Ospeed;
52 speed_t tset_baudrate(char *);
54 /* Baud rate conditionals for mapping. */
63 struct map *next; /* Linked list of maps. */
64 char *porttype; /* Port type, or "" for any. */
65 char *type; /* Terminal type to select. */
66 int conditional; /* Baud rate conditionals bitmask. */
67 speed_t speed; /* Baud rate to compare against. */
74 * [port-type][test baudrate]:terminal-type
75 * The baud rate tests are: >, <, @, =, !
78 add_mapping(port, arg)
83 char *copy, *p, *termp;
86 mapp = malloc(sizeof(MAP));
87 if (copy == NULL || mapp == NULL)
98 mapp->conditional = 0;
100 arg = strpbrk(arg, "><@=!:");
102 if (arg == NULL) { /* [?]term */
103 mapp->type = mapp->porttype;
104 mapp->porttype = NULL;
108 if (arg == mapp->porttype) /* [><@=! baud]:term */
109 termp = mapp->porttype = NULL;
113 for (;; ++arg) /* Optional conditionals. */
116 if (mapp->conditional & GT)
118 mapp->conditional |= LT;
121 if (mapp->conditional & LT)
123 mapp->conditional |= GT;
126 case '=': /* Not documented. */
127 mapp->conditional |= EQ;
130 mapp->conditional |= NOT;
136 next: if (*arg == ':') {
137 if (mapp->conditional)
140 } else { /* Optional baudrate. */
141 arg = index(p = arg, ':');
145 mapp->speed = tset_baudrate(p);
148 if (*arg == '\0') /* Non-optional type. */
153 /* Terminate porttype, if specified. */
157 /* If a NOT conditional, reverse the test. */
158 if (mapp->conditional & NOT)
159 mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
161 /* If user specified a port with an option flag, set it. */
164 badmopt: errx(1, "illegal -m option format: %s", copy);
165 mapp->porttype = strdup(port);
169 (void)printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
170 (void)printf("type: %s\n", mapp->type);
171 (void)printf("conditional: ");
173 if (mapp->conditional & GT) {
177 if (mapp->conditional & EQ) {
178 (void)printf("%sEQ", p);
181 if (mapp->conditional & LT)
182 (void)printf("%sLT", p);
183 (void)printf("\nspeed: %d\n", mapp->speed);
188 * Return the type of terminal to use for a port of type 'type', as specified
189 * by the first applicable mapping in 'map'. If no mappings apply, return
200 for (mapp = maplist; mapp; mapp = mapp->next)
201 if (mapp->porttype == NULL || !strcmp(mapp->porttype, type)) {
202 switch (mapp->conditional) {
203 case 0: /* No test specified. */
207 match = (Ospeed == mapp->speed);
210 match = (Ospeed >= mapp->speed);
213 match = (Ospeed > mapp->speed);
216 match = (Ospeed <= mapp->speed);
219 match = (Ospeed < mapp->speed);
225 /* No match found; return given type. */
229 typedef struct speeds {
249 /* The baudrate number can be preceded by a 'B', which is ignored. */
253 for (sp = speeds; sp->string; ++sp)
254 if (!strcasecmp(rate, sp->string))
258 errx(1, "unknown baud rate %s", rate);