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
36 static char sccsid[] = "@(#)map.c 8.1 (Berkeley) 6/9/93";
38 static const char rcsid[] =
39 "$FreeBSD: src/usr.bin/tset/map.c,v 1.7 1999/08/30 08:27:30 peter Exp $";
42 #include <sys/types.h>
49 extern speed_t Ospeed;
50 speed_t tset_baudrate __P((char *));
52 /* Baud rate conditionals for mapping. */
61 struct map *next; /* Linked list of maps. */
62 char *porttype; /* Port type, or "" for any. */
63 char *type; /* Terminal type to select. */
64 int conditional; /* Baud rate conditionals bitmask. */
65 speed_t speed; /* Baud rate to compare against. */
72 * [port-type][test baudrate]:terminal-type
73 * The baud rate tests are: >, <, @, =, !
76 add_mapping(port, arg)
80 char *copy, *p, *termp;
83 mapp = malloc((u_int)sizeof(MAP));
84 if (copy == NULL || mapp == NULL)
95 mapp->conditional = 0;
97 arg = strpbrk(arg, "><@=!:");
99 if (arg == NULL) { /* [?]term */
100 mapp->type = mapp->porttype;
101 mapp->porttype = NULL;
105 if (arg == mapp->porttype) /* [><@=! baud]:term */
106 termp = mapp->porttype = NULL;
110 for (;; ++arg) /* Optional conditionals. */
113 if (mapp->conditional & GT)
115 mapp->conditional |= LT;
118 if (mapp->conditional & LT)
120 mapp->conditional |= GT;
123 case '=': /* Not documented. */
124 mapp->conditional |= EQ;
127 mapp->conditional |= NOT;
133 next: if (*arg == ':') {
134 if (mapp->conditional)
137 } else { /* Optional baudrate. */
138 arg = index(p = arg, ':');
142 mapp->speed = tset_baudrate(p);
145 if (*arg == '\0') /* Non-optional type. */
150 /* Terminate porttype, if specified. */
154 /* If a NOT conditional, reverse the test. */
155 if (mapp->conditional & NOT)
156 mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
158 /* If user specified a port with an option flag, set it. */
161 badmopt: errx(1, "illegal -m option format: %s", copy);
162 mapp->porttype = port;
166 (void)printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
167 (void)printf("type: %s\n", mapp->type);
168 (void)printf("conditional: ");
170 if (mapp->conditional & GT) {
174 if (mapp->conditional & EQ) {
175 (void)printf("%sEQ", p);
178 if (mapp->conditional & LT)
179 (void)printf("%sLT", p);
180 (void)printf("\nspeed: %d\n", mapp->speed);
185 * Return the type of terminal to use for a port of type 'type', as specified
186 * by the first applicable mapping in 'map'. If no mappings apply, return
196 for (mapp = maplist; mapp; mapp = mapp->next)
197 if (mapp->porttype == NULL || !strcmp(mapp->porttype, type)) {
198 switch (mapp->conditional) {
199 case 0: /* No test specified. */
203 match = (Ospeed == mapp->speed);
206 match = (Ospeed >= mapp->speed);
209 match = (Ospeed > mapp->speed);
212 match = (Ospeed <= mapp->speed);
215 match = (Ospeed < mapp->speed);
221 /* No match found; return given type. */
225 typedef struct speeds {
245 /* The baudrate number can be preceded by a 'B', which is ignored. */
249 for (sp = speeds; sp->string; ++sp)
250 if (!strcasecmp(rate, sp->string))
254 errx(1, "unknown baud rate %s", rate);