map.c revision 1.9 1 /* $NetBSD: map.c,v 1.9 1998/07/26 23:09:50 mycroft Exp $ */
2
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)map.c 8.1 (Berkeley) 6/9/93";
40 #endif
41 __RCSID("$NetBSD: map.c,v 1.9 1998/07/26 23:09:50 mycroft Exp $");
42 #endif /* not lint */
43
44 #include <sys/types.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <termios.h>
50 #include "extern.h"
51
52 int baudrate __P((char *));
53
54 /* Baud rate conditionals for mapping. */
55 #define GT 0x01
56 #define EQ 0x02
57 #define LT 0x04
58 #define NOT 0x08
59 #define GE (GT | EQ)
60 #define LE (LT | EQ)
61
62 typedef struct map {
63 struct map *next; /* Linked list of maps. */
64 const char *porttype; /* Port type, or "" for any. */
65 const char *type; /* Terminal type to select. */
66 int conditional; /* Baud rate conditionals bitmask. */
67 int speed; /* Baud rate to compare against. */
68 } MAP;
69
70 MAP *cur, *maplist;
71
72 /*
73 * Syntax for -m:
74 * [port-type][test baudrate]:terminal-type
75 * The baud rate tests are: >, <, @, =, !
76 */
77 void
78 add_mapping(port, arg)
79 const char *port;
80 char *arg;
81 {
82 MAP *mapp;
83 char *copy, *p, *termp;
84
85 copy = strdup(arg);
86 mapp = malloc((u_int)sizeof(MAP));
87 if (copy == NULL || mapp == NULL)
88 err(1, "malloc");
89 mapp->next = NULL;
90 if (maplist == NULL)
91 cur = maplist = mapp;
92 else {
93 cur->next = mapp;
94 cur = mapp;
95 }
96
97 mapp->porttype = arg;
98 mapp->conditional = 0;
99
100 arg = strpbrk(arg, "><@=!:");
101
102 if (arg == NULL) { /* [?]term */
103 mapp->type = mapp->porttype;
104 mapp->porttype = NULL;
105 goto done;
106 }
107
108 if (arg == mapp->porttype) /* [><@=! baud]:term */
109 mapp->porttype = termp = NULL;
110 else
111 termp = arg;
112
113 for (;; ++arg) /* Optional conditionals. */
114 switch(*arg) {
115 case '<':
116 if (mapp->conditional & GT)
117 goto badmopt;
118 mapp->conditional |= LT;
119 break;
120 case '>':
121 if (mapp->conditional & LT)
122 goto badmopt;
123 mapp->conditional |= GT;
124 break;
125 case '@':
126 case '=': /* Not documented. */
127 mapp->conditional |= EQ;
128 break;
129 case '!':
130 mapp->conditional |= NOT;
131 break;
132 default:
133 goto next;
134 }
135
136 next: if (*arg == ':') {
137 if (mapp->conditional)
138 goto badmopt;
139 ++arg;
140 } else { /* Optional baudrate. */
141 arg = strchr(p = arg, ':');
142 if (arg == NULL)
143 goto badmopt;
144 *arg++ = '\0';
145 mapp->speed = baudrate(p);
146 }
147
148 if (*arg == '\0') /* Non-optional type. */
149 goto badmopt;
150
151 mapp->type = arg;
152
153 /* Terminate porttype, if specified. */
154 if (termp != NULL)
155 *termp = '\0';
156
157 /* If a NOT conditional, reverse the test. */
158 if (mapp->conditional & NOT)
159 mapp->conditional = ~mapp->conditional & (EQ | GT | LT);
160
161 /* If user specified a port with an option flag, set it. */
162 done: if (port) {
163 if (mapp->porttype)
164 badmopt: errx(1, "illegal -m option format: %s", copy);
165 mapp->porttype = port;
166 }
167
168 #ifdef MAPDEBUG
169 (void)printf("port: %s\n", mapp->porttype ? mapp->porttype : "ANY");
170 (void)printf("type: %s\n", mapp->type);
171 (void)printf("conditional: ");
172 p = "";
173 if (mapp->conditional & GT) {
174 (void)printf("GT");
175 p = "/";
176 }
177 if (mapp->conditional & EQ) {
178 (void)printf("%sEQ", p);
179 p = "/";
180 }
181 if (mapp->conditional & LT)
182 (void)printf("%sLT", p);
183 (void)printf("\nspeed: %d\n", mapp->speed);
184 #endif
185 }
186
187 /*
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
190 * 'type'.
191 */
192 const char *
193 mapped(type)
194 const char *type;
195 {
196 MAP *mapp;
197 int match;
198
199 match = 0;
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. */
204 match = 1;
205 break;
206 case EQ:
207 match = (ospeed == mapp->speed);
208 break;
209 case GE:
210 match = (ospeed >= mapp->speed);
211 break;
212 case GT:
213 match = (ospeed > mapp->speed);
214 break;
215 case LE:
216 match = (ospeed <= mapp->speed);
217 break;
218 case LT:
219 match = (ospeed < mapp->speed);
220 break;
221 }
222 if (match)
223 return (mapp->type);
224 }
225 /* No match found; return given type. */
226 return (type);
227 }
228
229 int
230 baudrate(rate)
231 char *rate;
232 {
233
234 /* The baudrate number can be preceded by a 'B', which is ignored. */
235 if (*rate == 'B')
236 ++rate;
237
238 return (atoi(rate));
239 }
240