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