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