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