util.c revision 1.1 1 /* $NetBSD: util.c,v 1.1 1998/12/28 14:01:17 hannken Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juergen Hannken-Illjes.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/time.h>
40 #include <dev/wscons/wsconsio.h>
41 #include <dev/wscons/wsksymdef.h>
42 #include <err.h>
43 #include <string.h>
44 #include <stdio.h>
45 #include <unistd.h>
46 #include "wsconsctl.h"
47
48 #define TABLEN(t) (sizeof(t)/sizeof(t[0]))
49
50 extern struct wskbd_map_data kbmap; /* from keyboard.c */
51 extern struct wskbd_map_data newkbmap; /* from map_parse.y */
52
53 struct nameint {
54 int value;
55 char *name;
56 };
57
58 static struct nameint kbtype_tab[] = {
59 { WSKBD_TYPE_LK201, "lk201" },
60 { WSKBD_TYPE_LK401, "lk401" },
61 { WSKBD_TYPE_PC_XT, "pc-xt" },
62 { WSKBD_TYPE_PC_AT, "pc-at" },
63 };
64
65 static struct nameint mstype_tab[] = {
66 { WSMOUSE_TYPE_VSXXX, "dec-tc" },
67 { WSMOUSE_TYPE_PS2, "ps2" },
68 };
69
70 static struct nameint dpytype_tab[] = {
71 { WSDISPLAY_TYPE_PM_MONO, "dec-?mono" },
72 { WSDISPLAY_TYPE_PM_COLOR, "dec-?color" },
73 { WSDISPLAY_TYPE_CFB, "dec-cfb" },
74 { WSDISPLAY_TYPE_XCFB, "dec-?xcfb" },
75 { WSDISPLAY_TYPE_MFB, "dec-mfb" },
76 { WSDISPLAY_TYPE_SFB, "dec-sfb" },
77 { WSDISPLAY_TYPE_ISAVGA, "vga-isa" },
78 { WSDISPLAY_TYPE_PCIVGA, "vga-pci" },
79 { WSDISPLAY_TYPE_TGA, "dec-tga-pci" },
80 { WSDISPLAY_TYPE_SFBP, "dec-sfb+" },
81 { WSDISPLAY_TYPE_PCIMISC, "generic-pci" },
82 };
83
84 static struct nameint kbdenc_tab[] = {
85 KB_NAMETAB
86 };
87
88 static struct field *field_tab;
89 static int field_tab_len;
90
91 static char *int2name __P((int, int, struct nameint *, int));
92 static int name2int __P((char *, struct nameint *, int));
93 static void print_kmap __P((struct wskbd_map_data *));
94
95 void
96 field_setup(ftab, len)
97 struct field *ftab;
98 int len;
99 {
100 field_tab = ftab;
101 field_tab_len = len;
102 }
103
104 struct field *
105 field_by_name(name)
106 char *name;
107 {
108 int i;
109
110 for (i = 0; i < field_tab_len; i++)
111 if (strcmp(field_tab[i].name, name) == 0)
112 return(field_tab + i);
113
114 errx(1, "%s: not found", name);
115 }
116
117 struct field *
118 field_by_value(addr)
119 void *addr;
120 {
121 int i;
122
123 for (i = 0; i < field_tab_len; i++)
124 if (field_tab[i].valp == addr)
125 return(field_tab + i);
126
127 errx(1, "internal error: field_by_value: not found");
128 }
129
130 static char *
131 int2name(val, uflag, tab, len)
132 int val;
133 int uflag;
134 struct nameint *tab;
135 int len;
136 {
137 static char tmp[20];
138 int i;
139
140 for (i = 0; i < len; i++)
141 if (tab[i].value == val)
142 return(tab[i].name);
143
144 if (uflag) {
145 snprintf(tmp, sizeof(tmp), "unknown_%d", val);
146 return(tmp);
147 } else
148 return(NULL);
149 }
150
151 static int
152 name2int(val, tab, len)
153 char *val;
154 struct nameint *tab;
155 int len;
156 {
157 int i;
158
159 for (i = 0; i < len; i++)
160 if (strcmp(tab[i].name, val) == 0)
161 return(tab[i].value);
162 return(-1);
163 }
164
165 void
166 pr_field(f, sep)
167 struct field *f;
168 char *sep;
169 {
170 char *p;
171
172 if (sep)
173 printf("%s%s", f->name, sep);
174
175 switch (f->format) {
176 case FMT_UINT:
177 printf("%u", *((u_int *) f->valp));
178 break;
179 case FMT_KBDTYPE:
180 p = int2name(*((u_int *) f->valp), 1,
181 kbtype_tab, TABLEN(kbtype_tab));
182 printf("%s", p);
183 break;
184 case FMT_MSTYPE:
185 p = int2name(*((u_int *) f->valp), 1,
186 mstype_tab, TABLEN(mstype_tab));
187 printf("%s", p);
188 break;
189 case FMT_DPYTYPE:
190 p = int2name(*((u_int *) f->valp), 1,
191 dpytype_tab, TABLEN(dpytype_tab));
192 printf("%s", p);
193 break;
194 case FMT_KBDENC:
195 p = int2name(KB_ENCODING(*((u_int *) f->valp)), 1,
196 kbdenc_tab, TABLEN(kbdenc_tab));
197 printf("%s", p);
198
199 p = int2name(KB_VARIANT(*((u_int *) f->valp)), 0,
200 kbdenc_tab, TABLEN(kbdenc_tab));
201 if (p)
202 printf(".%s", p);
203 break;
204 case FMT_KBMAP:
205 print_kmap((struct wskbd_map_data *) f->valp);
206 break;
207 default:
208 errx(1, "internal error: pr_field: no format %d", f->format);
209 break;
210 }
211
212 printf("\n");
213 }
214
215 void
216 rd_field(f, val, merge)
217 struct field *f;
218 char *val;
219 int merge;
220 {
221 int i;
222 u_int u;
223 char *p;
224 struct wscons_keymap *mp;
225
226 switch (f->format) {
227 case FMT_UINT:
228 if (sscanf(val, "%u", &u) != 1)
229 errx(1, "%s: not a number", val);
230 if (merge)
231 *((u_int *) f->valp) += u;
232 else
233 *((u_int *) f->valp) = u;
234 break;
235 case FMT_KBDENC:
236 p = strchr(val, '.');
237 if (p != NULL)
238 *p++ = '\0';
239
240 i = name2int(val, kbdenc_tab, TABLEN(kbdenc_tab));
241 if (i == -1)
242 errx(1, "%s: not a valid encoding", val);
243 *((u_int *) f->valp) = i;
244
245 if (p == NULL)
246 break;
247
248 i = name2int(p, kbdenc_tab, TABLEN(kbdenc_tab));
249 if (i == -1)
250 errx(1, "%s: not a valid variant", val);
251 *((u_int *) f->valp) |= i;
252 break;
253 case FMT_KBMAP:
254 if (! merge)
255 kbmap.maplen = 0;
256 map_scan_setinput(val);
257 yyparse();
258 if (merge) {
259 if (newkbmap.maplen < kbmap.maplen)
260 newkbmap.maplen = kbmap.maplen;
261 for (i = 0; i < kbmap.maplen; i++) {
262 mp = newkbmap.map + i;
263 if (mp->command == KS_voidSymbol &&
264 mp->group1[0] == KS_voidSymbol &&
265 mp->group1[1] == KS_voidSymbol &&
266 mp->group2[0] == KS_voidSymbol &&
267 mp->group2[1] == KS_voidSymbol)
268 *mp = kbmap.map[i];
269 }
270 }
271 kbmap.maplen = newkbmap.maplen;
272 bcopy(newkbmap.map, kbmap.map,
273 kbmap.maplen*sizeof(struct wscons_keymap));
274 break;
275 default:
276 errx(1, "internal error: rd_field: no format %d", f->format);
277 break;
278 }
279 }
280
281 static void
282 print_kmap(map)
283 struct wskbd_map_data *map;
284 {
285 int i;
286 struct wscons_keymap *mp;
287
288 for (i = 0; i < map->maplen; i++) {
289 mp = map->map + i;
290
291 if (mp->command == KS_voidSymbol &&
292 mp->group1[0] == KS_voidSymbol &&
293 mp->group1[1] == KS_voidSymbol &&
294 mp->group2[0] == KS_voidSymbol &&
295 mp->group2[1] == KS_voidSymbol)
296 continue;
297 printf("\n");
298 printf("keycode %u =", i);
299 if (mp->command != KS_voidSymbol)
300 printf(" %s", ksym2name(mp->command));
301 printf(" %s", ksym2name(mp->group1[0]));
302 if (mp->group1[0] != mp->group1[1] ||
303 mp->group1[0] != mp->group2[0] ||
304 mp->group1[0] != mp->group2[1]) {
305 printf(" %s", ksym2name(mp->group1[1]));
306 if (mp->group1[0] != mp->group2[0] ||
307 mp->group1[1] != mp->group2[1]) {
308 printf(" %s", ksym2name(mp->group2[0]));
309 printf(" %s", ksym2name(mp->group2[1]));
310 }
311 }
312 }
313 }
314