Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / hotplug2 / linux24_compat / generate_alias.c
blobf534bd66a6390a2911ea4bd09d6056d9e02a8427
1 /*****************************************************************************\
2 * _ _ _ _ ___ *
3 * | || | ___ | |_ _ __ | | _ _ __ _ |_ ) *
4 * | __ |/ _ \| _|| '_ \| || || |/ _` | / / *
5 * |_||_|\___/ \__|| .__/|_| \_,_|\__, |/___| *
6 * |_| |___/ *
7 \*****************************************************************************/
9 #define _GNU_SOURCE
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <dirent.h>
18 #include <ctype.h>
19 #include <sys/types.h>
20 #include <sys/wait.h>
21 #include <sys/stat.h>
22 #include <sys/mman.h>
23 #include <linux/types.h>
24 #include <linux/input.h>
26 #include "../mem_utils.h"
27 #include "../parser_utils.h"
28 #include "../filemap_utils.h"
30 #define JUMP_TO_NEXT { free(line); free(module); continue; }
32 #define PRINT_WILDCARD(prefix, format, variable, any) \
33 if (variable == any)\
34 fprintf(fp, prefix "*"); \
35 else \
36 fprintf(fp, prefix format, variable);
38 #define PRINT_WILDCARD_COND(prefix, format, variable, condition) \
39 if (condition)\
40 fprintf(fp, prefix format, variable); \
41 else \
42 fprintf(fp, prefix "*");
44 #define SET_OFFSET(offset, token, prefix, prefix_len) \
45 offset = strncmp(token, prefix, prefix_len) ? 0 : prefix_len;
47 #ifndef KEY_MIN_INTERESTING
48 #define KEY_MIN_INTERESTING KEY_MUTE
49 #endif
51 /* Some kernel headers appear to define it even without __KERNEL__ */
52 #ifndef BITS_PER_LONG
53 #define BITS_PER_LONG (sizeof(long) * 8)
54 #endif
55 #ifndef NBITS
56 #define NBITS(x) ((x / BITS_PER_LONG) + 1)
57 #endif
59 #define TEST_INPUT_BIT(i,bm) (bm[i / BITS_PER_LONG] & (((unsigned long)1) << (i%BITS_PER_LONG)))
61 void module_tr(char *modname) {
62 char *ptr;
64 for (ptr = modname; *ptr != '\0'; ptr++) {
65 if (*ptr == '-')
66 *ptr = '_';
70 int hexchar_to_int(char c) {
71 c = tolower(c);
72 if (c >= 'a' && c <= 'f') {
73 return c - 'a' + 10;
74 } else if (c >= '0' && c <= '9') {
75 return c - '0';
78 return 0;
81 void alias_from_pcimap(FILE *fp, char *prefix) {
82 struct filemap_t pcimap;
83 char *line, *nline, *nptr;
84 char *token;
85 char *module;
86 char *filename;
88 unsigned long vendor, device, sub_vendor, sub_device, class_type, class_mask;
90 filename = xmalloc(strlen(prefix) + strlen("modules.pcimap") + 2);
91 strcpy(filename, prefix);
92 strcat(filename, "/modules.pcimap");
94 if (map_file(filename, &pcimap)) {
95 free(filename);
96 return;
99 nptr = pcimap.map;
101 while ((line = dup_line(nptr, &nptr)) != NULL) {
102 nline = line;
104 module = dup_token(nline, &nline, isspace);
105 if (!module || module[0] == '#')
106 JUMP_TO_NEXT;
108 token = dup_token(nline, &nline, isspace);
109 if (!token)
110 JUMP_TO_NEXT;
111 vendor = strtoul(token, NULL, 0);
112 free(token);
114 token = dup_token(nline, &nline, isspace);
115 if (!token)
116 JUMP_TO_NEXT;
117 device = strtoul(token, NULL, 0);
118 free(token);
120 token = dup_token(nline, &nline, isspace);
121 if (!token)
122 JUMP_TO_NEXT;
123 sub_vendor = strtoul(token, NULL, 0);
124 free(token);
126 token = dup_token(nline, &nline, isspace);
127 if (!token)
128 JUMP_TO_NEXT;
129 sub_device = strtoul(token, NULL, 0);
130 free(token);
132 token = dup_token(nline, &nline, isspace);
133 if (!token)
134 JUMP_TO_NEXT;
135 class_type = strtoul(token, NULL, 0);
136 free(token);
138 token = dup_token(nline, &nline, isspace);
139 if (!token)
140 JUMP_TO_NEXT;
141 class_mask = strtoul(token, NULL, 0);
142 free(token);
144 fprintf(fp, "alias pci:");
146 PRINT_WILDCARD("v", "%08lX", vendor, 0xffffffff);
147 PRINT_WILDCARD("d", "%08lX", device, 0xffffffff);
148 PRINT_WILDCARD("sv", "%08lX", sub_vendor, 0xffffffff);
149 PRINT_WILDCARD("sd", "%08lX", sub_device, 0xffffffff);
151 /* FIXME ! USE PRINT_WIL*/
152 PRINT_WILDCARD_COND("bc", "%02X",
153 (unsigned int)((class_type & 0x00ff0000 & class_mask) >> 16),
154 class_mask & 0x00ff0000);
156 PRINT_WILDCARD_COND("sc", "%02X",
157 (unsigned int)((class_type & 0x0000ff00 & class_mask) >> 8),
158 class_mask & 0x00ff0000);
160 PRINT_WILDCARD_COND("i", "%02X",
161 (unsigned int)((class_type & 0x000000ff & class_mask)),
162 class_mask & 0x00ff0000);
164 module_tr(module);
165 fprintf(fp, " %s\n", module);
167 free(module);
168 free(line);
171 unmap_file(&pcimap);
172 free(filename);
175 /* Yes, this -is- ugly. */
176 void alias_from_usbmap_print_pattern(FILE *fp,
177 unsigned long mflags, unsigned long ven,
178 unsigned long pro, unsigned long bcdl, unsigned long bcdh,
179 unsigned long bdc, unsigned long bdsc, unsigned long bdp,
180 unsigned long bic, unsigned long bisc, unsigned long bip,
181 int len, unsigned long left, int ndigits,
182 char *module) {
184 fprintf(fp, "alias usb:");
186 PRINT_WILDCARD_COND("v", "%04lX", ven, mflags & 0x0001);
187 PRINT_WILDCARD_COND("p", "%04lX", pro, mflags & 0x0002);
189 fprintf(fp, "d");
191 if (mflags & 0x000C) {
192 if (ndigits > 0)
193 fprintf(fp, "%.*lX", ndigits - 1, left);
195 if (bcdl == 0 && bcdh == 0xf)
196 fprintf(fp, "*"), ndigits = len;
197 else if (bcdl == bcdh)
198 fprintf(fp, "%lX", bcdl);
199 else if (bcdl / 10 != bcdh / 10)
200 fprintf(fp, "[%lX-%X%X-%lX]", bcdl, 9, 10, bcdh);
201 else
202 fprintf(fp, "[%lX-%lX]", bcdl, bcdh);
204 if (ndigits < len)
205 fprintf(fp, "*");
206 } else {
207 fprintf(fp, "*");
210 PRINT_WILDCARD_COND("dc", "%02lX", bdc, mflags & 0x0010);
211 PRINT_WILDCARD_COND("dsc", "%02lX", bdsc, mflags & 0x0020);
212 PRINT_WILDCARD_COND("dp", "%02lX", bdp, mflags & 0x0040);
214 PRINT_WILDCARD_COND("ic", "%02lX", bic, mflags & 0x0080);
215 PRINT_WILDCARD_COND("isc", "%02lX", bisc, mflags & 0x0100);
216 PRINT_WILDCARD_COND("ip", "%02lX", bip, mflags & 0x0200);
218 fprintf(fp, " %s\n", module);
221 void alias_from_usbmap(FILE *fp, char *prefix) {
222 struct filemap_t usbmap;
223 char *line, *nline, *nptr;
224 char *token;
225 char *module;
226 char *filename;
228 unsigned long match_flags;
229 unsigned long vendor, product;
230 unsigned long bcddev_lo, bcddev_hi; /* some devices violate bcd */
231 unsigned long bdevclass, bdevsubclass, bdevproto;
232 unsigned long bifclass, bifsubclass, bifproto;
234 unsigned long cmin, cmax;
235 int len, ndigits;
237 filename = xmalloc(strlen(prefix) + strlen("modules.usbmap") + 2);
238 strcpy(filename, prefix);
239 strcat(filename, "/modules.usbmap");
241 if (map_file(filename, &usbmap)) {
242 free(filename);
243 return;
246 nptr = usbmap.map;
248 while ((line = dup_line(nptr, &nptr)) != NULL) {
249 nline = line;
251 module = dup_token(nline, &nline, isspace);
252 if (!module || module[0] == '#')
253 JUMP_TO_NEXT;
255 token = dup_token(nline, &nline, isspace);
256 if (!token)
257 JUMP_TO_NEXT;
258 match_flags = strtoul(token, NULL, 0);
259 free(token);
261 token = dup_token(nline, &nline, isspace);
262 if (!token)
263 JUMP_TO_NEXT;
264 vendor = strtoul(token, NULL, 0);
265 free(token);
267 token = dup_token(nline, &nline, isspace);
268 if (!token)
269 JUMP_TO_NEXT;
270 product = strtoul(token, NULL, 0);
271 free(token);
273 token = dup_token(nline, &nline, isspace);
274 if (!token)
275 JUMP_TO_NEXT;
276 bcddev_lo = strtoul(token, NULL, 0);
277 free(token);
279 token = dup_token(nline, &nline, isspace);
280 if (!token)
281 JUMP_TO_NEXT;
282 bcddev_hi = strtoul(token, NULL, 0);
283 free(token);
285 token = dup_token(nline, &nline, isspace);
286 if (!token)
287 JUMP_TO_NEXT;
288 bdevclass = strtoul(token, NULL, 0);
289 free(token);
291 token = dup_token(nline, &nline, isspace);
292 if (!token)
293 JUMP_TO_NEXT;
294 bdevsubclass = strtoul(token, NULL, 0);
295 free(token);
297 token = dup_token(nline, &nline, isspace);
298 if (!token)
299 JUMP_TO_NEXT;
300 bdevproto = strtoul(token, NULL, 0);
301 free(token);
303 token = dup_token(nline, &nline, isspace);
304 if (!token)
305 JUMP_TO_NEXT;
306 bifclass = strtoul(token, NULL, 0);
307 free(token);
309 token = dup_token(nline, &nline, isspace);
310 if (!token)
311 JUMP_TO_NEXT;
312 bifsubclass = strtoul(token, NULL, 0);
313 free(token);
315 token = dup_token(nline, &nline, isspace);
316 if (!token)
317 JUMP_TO_NEXT;
318 bifproto = strtoul(token, NULL, 0);
319 free(token);
321 module_tr(module);
323 for (len = ndigits = 4; bcddev_lo <= bcddev_hi; ndigits--) {
324 cmin = bcddev_lo & 0xf;
325 cmax = bcddev_hi & 0xf;
327 bcddev_lo >>= 4;
328 bcddev_hi >>= 4;
330 if (bcddev_lo == bcddev_hi || ndigits == 0) {
331 alias_from_usbmap_print_pattern(fp,
332 match_flags, vendor, product,
333 cmin, cmax,
334 bdevclass, bdevsubclass, bdevproto,
335 bifclass, bifsubclass, bifproto,
336 len, bcddev_lo, ndigits,
337 module);
338 break;
341 if (cmin > 0) {
342 alias_from_usbmap_print_pattern(fp,
343 match_flags, vendor, product,
344 cmin, 0xf,
345 bdevclass, bdevsubclass, bdevproto,
346 bifclass, bifsubclass, bifproto,
347 len, bcddev_lo++, ndigits,
348 module);
351 if (cmin < 0xf) {
352 alias_from_usbmap_print_pattern(fp,
353 match_flags, vendor, product,
354 0x0, cmax,
355 bdevclass, bdevsubclass, bdevproto,
356 bifclass, bifsubclass, bifproto,
357 len, bcddev_hi--, ndigits,
358 module);
362 free(module);
363 free(line);
366 unmap_file(&usbmap);
367 free(filename);
370 void alias_from_ieee1394map(FILE *fp, char *prefix) {
371 struct filemap_t ieee1394map;
372 char *line, *nline, *nptr;
373 char *token;
374 char *module;
375 char *filename;
377 unsigned long match_flags, vendor, product, specifier, version;
379 filename = xmalloc(strlen(prefix) + strlen("modules.ieee1394map") + 2);
380 strcpy(filename, prefix);
381 strcat(filename, "/modules.ieee1394map");
383 if (map_file(filename, &ieee1394map)) {
384 free(filename);
385 return;
388 nptr = ieee1394map.map;
390 while ((line = dup_line(nptr, &nptr)) != NULL) {
391 nline = line;
393 module = dup_token(nline, &nline, isspace);
394 if (!module || module[0] == '#')
395 JUMP_TO_NEXT;
397 token = dup_token(nline, &nline, isspace);
398 if (!token)
399 JUMP_TO_NEXT;
400 match_flags = strtoul(token, NULL, 0);
401 free(token);
403 token = dup_token(nline, &nline, isspace);
404 if (!token)
405 JUMP_TO_NEXT;
406 vendor = strtoul(token, NULL, 0);
407 free(token);
409 token = dup_token(nline, &nline, isspace);
410 if (!token)
411 JUMP_TO_NEXT;
412 product = strtoul(token, NULL, 0);
413 free(token);
415 token = dup_token(nline, &nline, isspace);
416 if (!token)
417 JUMP_TO_NEXT;
418 specifier = strtoul(token, NULL, 0);
419 free(token);
421 token = dup_token(nline, &nline, isspace);
422 if (!token)
423 JUMP_TO_NEXT;
424 version = strtoul(token, NULL, 0);
425 free(token);
427 fprintf(fp, "alias ieee1394:");
429 PRINT_WILDCARD_COND("ven", "%08lX", vendor, match_flags & 0x01);
430 PRINT_WILDCARD_COND("mo", "%08lX", product, match_flags & 0x02);
431 PRINT_WILDCARD_COND("sp", "%08lX", specifier, match_flags & 0x04);
432 PRINT_WILDCARD_COND("ver", "%08lX", version, match_flags & 0x08);
433 if (match_flags & 0x08)
434 fprintf(fp, "*");
436 module_tr(module);
437 fprintf(fp, " %s\n", module);
439 free(module);
440 free(line);
443 unmap_file(&ieee1394map);
444 free(filename);
447 void alias_from_isapnpmap(FILE *fp, char *prefix) {
448 struct filemap_t isapnpmap;
449 char *line, *nline, *nptr;
450 char *module;
451 char *filename;
452 char *token;
454 int offset;
456 unsigned char vendor_sig[3], vendor[2], function[2];
457 unsigned char card_vendor_sig[3], card_vendor[2], card_device[2];
459 filename = xmalloc(strlen(prefix) + strlen("modules.isapnpmap") + 2);
460 strcpy(filename, prefix);
461 strcat(filename, "/modules.isapnpmap");
463 if (map_file(filename, &isapnpmap)) {
464 free(filename);
465 return;
468 nptr = isapnpmap.map;
470 while ((line = dup_line(nptr, &nptr)) != NULL) {
471 nline = line;
473 module = dup_token(nline, &nline, isspace);
474 if (!module || module[0] == '#')
475 JUMP_TO_NEXT;
477 token = dup_token(nline, &nline, isspace);
478 if (!token)
479 JUMP_TO_NEXT;
481 SET_OFFSET(offset, token, "0x", 2);
482 card_vendor[1] = hexchar_to_int(token[offset]) * 16 + hexchar_to_int(token[offset + 1]);
483 card_vendor[0] = hexchar_to_int(token[offset + 2]) * 16 + hexchar_to_int(token[offset + 3]);
484 free(token);
486 token = dup_token(nline, &nline, isspace);
487 if (!token)
488 JUMP_TO_NEXT;
490 SET_OFFSET(offset, token, "0x", 2);
491 card_device[1] = hexchar_to_int(token[offset]) * 16 + hexchar_to_int(token[offset + 1]);
492 card_device[0] = hexchar_to_int(token[offset + 2]) * 16 + hexchar_to_int(token[offset + 3]);
493 free(token);
495 /* jump driverdata */
496 token = dup_token(nline, &nline, isspace);
497 if (!token)
498 JUMP_TO_NEXT;
499 free(token);
501 token = dup_token(nline, &nline, isspace);
502 if (!token)
503 JUMP_TO_NEXT;
505 SET_OFFSET(offset, token, "0x", 2);
506 vendor[1] = hexchar_to_int(token[offset]) * 16 + hexchar_to_int(token[offset + 1]);
507 vendor[0] = hexchar_to_int(token[offset + 2]) * 16 + hexchar_to_int(token[offset+ 3]);
509 vendor_sig[0] = ((vendor[0] >> 2) & 0x1f) + ('A' - 1);
510 vendor_sig[1] = (((vendor[0] & 3) << 3) | (vendor[1] >> 5)) + ('A' - 1);
511 vendor_sig[2] = (vendor[1] & 0x1f) + ('A' - 1);
513 free(token);
515 token = dup_token(nline, &nline, isspace);
516 if (!token)
517 JUMP_TO_NEXT;
519 SET_OFFSET(offset, token, "0x", 2);
520 function[1] = hexchar_to_int(token[offset]) * 16 + hexchar_to_int(token[offset + 1]);
521 function[0] = hexchar_to_int(token[offset + 2]) * 16 + hexchar_to_int(token[offset + 3]);
523 free(token);
525 fprintf(fp, "alias pnp:");
526 if (memcmp("\xff\xff", card_vendor, 2) && memcmp("\xff\xff", card_device, 2)) {
527 card_vendor_sig[0] = ((card_vendor[0] >> 2) & 0x1f) + ('A' - 1);
528 card_vendor_sig[1] = (((card_vendor[0] & 3) << 3) | (card_vendor[1] >> 5)) + ('A' - 1);
529 card_vendor_sig[2] = (card_vendor[1] & 0x1f) + ('A' - 1);
531 fprintf(fp, "c%c%c%c%02x%02x", card_vendor_sig[0], card_vendor_sig[1], card_vendor_sig[2], card_device[0], card_device[1]);
534 fprintf(fp, "d%c%c%c%02x%02x", vendor_sig[0], vendor_sig[1], vendor_sig[2], function[0], function[1]);
535 while ((token = dup_token(nline, &nline, isspace)) != NULL) {
536 if (!token)
537 break;
539 SET_OFFSET(offset, token, "0x", 2);
540 vendor[1] = hexchar_to_int(token[offset]) * 16 + hexchar_to_int(token[offset + 1]);
541 vendor[0] = hexchar_to_int(token[offset + 2]) * 16 + hexchar_to_int(token[offset+ 3]);
543 vendor_sig[0] = ((vendor[0] >> 2) & 0x1f) + ('A' - 1);
544 vendor_sig[1] = (((vendor[0] & 3) << 3) | (vendor[1] >> 5)) + ('A' - 1);
545 vendor_sig[2] = (vendor[1] & 0x1f) + ('A' - 1);
547 free(token);
549 token = dup_token(nline, &nline, isspace);
550 if (!token)
551 break;
553 SET_OFFSET(offset, token, "0x", 2);
554 function[1] = hexchar_to_int(token[offset]) * 16 + hexchar_to_int(token[offset + 1]);
555 function[0] = hexchar_to_int(token[offset + 2]) * 16 + hexchar_to_int(token[offset + 3]);
557 free(token);
559 fprintf(fp, "d%c%c%c%02x%02x", vendor_sig[0], vendor_sig[1], vendor_sig[2], function[0], function[1]);
562 module_tr(module);
563 fprintf(fp, "* %s\n", module);
565 free(module);
566 free(line);
569 unmap_file(&isapnpmap);
570 free(filename);
573 void alias_from_seriomap(FILE *fp, char *prefix) {
574 struct filemap_t seriomap;
575 char *line, *nline, *nptr;
576 char *token;
577 char *module;
578 char *filename;
580 unsigned char type, extra, id, proto;
582 filename = xmalloc(strlen(prefix) + strlen("modules.seriomap") + 2);
583 strcpy(filename, prefix);
584 strcat(filename, "/modules.seriomap");
586 if (map_file(filename, &seriomap)) {
587 free(filename);
588 return;
591 nptr = seriomap.map;
593 while ((line = dup_line(nptr, &nptr)) != NULL) {
594 nline = line;
596 module = dup_token(nline, &nline, isspace);
597 if (!module || module[0] == '#')
598 JUMP_TO_NEXT;
600 token = dup_token(nline, &nline, isspace);
601 if (!token)
602 JUMP_TO_NEXT;
603 type = strtoul(token, NULL, 16);
604 free(token);
606 token = dup_token(nline, &nline, isspace);
607 if (!token)
608 JUMP_TO_NEXT;
609 extra = strtoul(token, NULL, 16);
610 free(token);
612 token = dup_token(nline, &nline, isspace);
613 if (!token)
614 JUMP_TO_NEXT;
615 id = strtoul(token, NULL, 16);
616 free(token);
618 token = dup_token(nline, &nline, isspace);
619 if (!token)
620 JUMP_TO_NEXT;
621 proto = strtoul(token, NULL, 16);
622 free(token);
624 fprintf(fp, "alias serio:");
625 PRINT_WILDCARD("ty", "%02X", type, 0xff);
626 PRINT_WILDCARD("pr", "%02X", proto, 0xff);
627 PRINT_WILDCARD("id", "%02X", id, 0xff);
628 PRINT_WILDCARD("ex", "%02X", extra, 0xff);
629 fprintf(fp, " %s\n", module);
631 free(module);
632 free(line);
635 unmap_file(&seriomap);
636 free(filename);
639 inline int iscolon(int c) {
640 return (c == ':');
643 char *bitmap_to_bitstring(char name, unsigned long *bm, unsigned int min_bit, unsigned int max_bit)
645 char *rv;
646 unsigned int i, len = 0, size = 16, srv;
648 rv = xmalloc(size);
650 len += snprintf(rv + len, size - len, "%c*", name);
652 for (i = min_bit; i < max_bit; i++) {
653 if (TEST_INPUT_BIT(i, bm)) {
654 while ((srv = snprintf(rv + len, size - len, "%X,", i)) >= (size - len)) {
655 size = size * 2;
656 rv = xrealloc(rv, size);
658 len += srv;
662 /* read: if (we had any match) { */
663 if (len > 2) {
664 while ((srv = snprintf(rv + len, size - len, "*")) >= (size - len)) {
665 size = size * 2;
666 rv = xrealloc(rv, size);
670 return rv;
673 void string_to_bitmap(char *input, unsigned long *bitmap, int bm_len) {
674 char *token, *ptr;
675 int i = 0;
677 ptr = input + strlen(input);
679 while ((token = dup_token_r(ptr, input, &ptr, iscolon)) != NULL) {
680 bitmap[i] = strtoul(token, NULL, 16);
681 free(token);
682 i++;
685 while (i < bm_len)
686 bitmap[i++] = 0;
689 #define GET_BITMAP(mapkey, bitmap, name, min) \
690 token = dup_token(nline, &nline, isspace); \
691 if (!token) \
692 JUMP_TO_NEXT; \
693 if (TEST_INPUT_BIT(EV_ ## mapkey, ev_bits)) { \
694 if (token == NULL) \
695 JUMP_TO_NEXT; \
697 string_to_bitmap(token, bitmap ## _bits, NBITS(mapkey ## _MAX)); \
699 bitmap = bitmap_to_bitstring(name, bitmap ## _bits, min, mapkey ## _MAX); \
700 free(token);
702 void alias_from_inputmap(FILE *fp, char *prefix) {
703 struct filemap_t inputmap;
704 char *line, *nline, *nptr;
705 char *token, *sw_token;
706 char *filename;
707 char *module;
709 int new_format;
711 char *ev, *key, *rel, *abs, *sw, *msc, *led, *snd, *ff;
713 unsigned long bustype, vendor, product, version;
714 unsigned long ev_bits[NBITS(EV_MAX)];
715 unsigned long key_bits[NBITS(KEY_MAX)];
716 unsigned long rel_bits[NBITS(REL_MAX)];
717 unsigned long abs_bits[NBITS(ABS_MAX)];
718 unsigned long msc_bits[NBITS(MSC_MAX)];
719 unsigned long led_bits[NBITS(LED_MAX)];
720 unsigned long snd_bits[NBITS(SND_MAX)];
721 unsigned long ff_bits[NBITS(FF_MAX)];
722 #if defined(SW_MAX) && defined(EV_SW)
723 unsigned long sw_bits[NBITS(SW_MAX)];
724 #endif
726 memset(ev_bits, 0, NBITS(EV_MAX) * sizeof(long));
727 memset(key_bits, 0, NBITS(KEY_MAX) * sizeof(long));
728 memset(rel_bits, 0, NBITS(REL_MAX) * sizeof(long));
729 memset(abs_bits, 0, NBITS(ABS_MAX) * sizeof(long));
730 memset(msc_bits, 0, NBITS(MSC_MAX) * sizeof(long));
731 memset(led_bits, 0, NBITS(LED_MAX) * sizeof(long));
732 memset(snd_bits, 0, NBITS(SND_MAX) * sizeof(long));
733 memset(ff_bits, 0, NBITS(FF_MAX) * sizeof(long));
734 #if defined(SW_MAX) && defined(EV_SW)
735 memset(sw_bits, 0, NBITS(SW_MAX) * sizeof(long));
736 #endif
738 filename = xmalloc(strlen(prefix) + strlen("modules.inputmap") + 2);
739 strcpy(filename, prefix);
740 strcat(filename, "/modules.inputmap");
742 if (map_file(filename, &inputmap)) {
743 free(filename);
744 return;
747 nptr = inputmap.map;
749 while ((line = dup_line(nptr, &nptr)) != NULL) {
750 nline = line;
752 module = dup_token(nline, &nline, isspace);
753 if (!module || module[0] == '#')
754 JUMP_TO_NEXT;
756 /* jump matchbits */
757 token = dup_token(nline, &nline, isspace);
758 if (!token)
759 JUMP_TO_NEXT;
760 free(token);
762 token = dup_token(nline, &nline, isspace);
763 if (!token)
764 JUMP_TO_NEXT;
765 bustype = strtoul(token, NULL, 0);
766 free(token);
768 token = dup_token(nline, &nline, isspace);
769 if (!token)
770 JUMP_TO_NEXT;
771 vendor = strtoul(token, NULL, 0);
772 free(token);
774 token = dup_token(nline, &nline, isspace);
775 if (!token)
776 JUMP_TO_NEXT;
777 product = strtoul(token, NULL, 0);
778 free(token);
780 token = dup_token(nline, &nline, isspace);
781 if (!token)
782 JUMP_TO_NEXT;
783 version = strtoul(token, NULL, 0);
784 free(token);
786 token = dup_token(nline, &nline, isspace);
787 if (!token)
788 JUMP_TO_NEXT;
790 string_to_bitmap(token, ev_bits, NBITS(EV_MAX));
791 ev = bitmap_to_bitstring('e', ev_bits, 0, EV_MAX);
792 free(token);
794 GET_BITMAP(KEY, key, 'k', KEY_MIN_INTERESTING);
795 GET_BITMAP(REL, rel, 'r', 0);
796 GET_BITMAP(ABS, abs, 'a', 0);
797 GET_BITMAP(MSC, msc, 'm', 0);
798 GET_BITMAP(LED, led, 'l', 0);
799 GET_BITMAP(SND, snd, 's', 0);
800 GET_BITMAP(FF, ff, 'f', 0);
802 sw_token = nline;
804 token = dup_token(nline, &nline, isspace);
805 if (!token)
806 JUMP_TO_NEXT;
807 free(token);
809 token = dup_token(nline, &nline, isspace);
810 new_format = token ? 1 : 0;
811 free(token);
813 #if defined(SW_MAX) && defined(EV_SW)
814 if (new_format) {
815 nline = sw_token;
816 GET_BITMAP(SW, sw, 'w', 0);
817 } else {
818 sw = strdup("");
820 #else
821 sw = strdup("");
822 #endif
824 fprintf(fp, "alias input:");
826 PRINT_WILDCARD("b", "%04lX", bustype, 0x0);
827 PRINT_WILDCARD("v", "%04lX", vendor, 0x0);
828 PRINT_WILDCARD("p", "%04lX", product, 0x0);
829 PRINT_WILDCARD("e", "%04lX", version, 0x0);
830 fprintf(fp, "-");
832 fprintf(fp, "%s%s%s%s%s%s%s%s%s",
833 ev, key, rel, abs, msc, led, snd, ff, sw);
835 fprintf(fp, " %s\n", module);
837 free(ev);
838 free(key);
839 free(rel);
840 free(abs);
841 free(msc);
842 free(led);
843 free(snd);
844 free(ff);
845 free(sw);
847 free(module);
848 free(line);
851 unmap_file(&inputmap);
852 free(filename);
855 void alias_from_ccwmap(FILE *fp, char *prefix) {
856 struct filemap_t ccwmap;
857 char *line, *nline, *nptr;
858 char *token;
859 char *module;
860 char *filename;
862 unsigned long match_flags, cu_type, cu_model, dev_type, dev_model;
864 filename = xmalloc(strlen(prefix) + strlen("modules.ccwmap") + 2);
865 strcpy(filename, prefix);
866 strcat(filename, "/modules.ccwmap");
868 if (map_file(filename, &ccwmap)) {
869 free(filename);
870 return;
873 nptr = ccwmap.map;
875 while ((line = dup_line(nptr, &nptr)) != NULL) {
876 nline = line;
878 module = dup_token(nline, &nline, isspace);
879 if (!module || module[0] == '#')
880 JUMP_TO_NEXT;
882 token = dup_token(nline, &nline, isspace);
883 if (!token)
884 JUMP_TO_NEXT;
885 match_flags = strtoul(token, NULL, 0);
886 free(token);
888 token = dup_token(nline, &nline, isspace);
889 if (!token)
890 JUMP_TO_NEXT;
891 cu_type = strtoul(token, NULL, 0);
892 free(token);
894 token = dup_token(nline, &nline, isspace);
895 if (!token)
896 JUMP_TO_NEXT;
897 cu_model = strtoul(token, NULL, 0);
898 free(token);
900 token = dup_token(nline, &nline, isspace);
901 if (!token)
902 JUMP_TO_NEXT;
903 dev_type = strtoul(token, NULL, 0);
904 free(token);
906 token = dup_token(nline, &nline, isspace);
907 if (!token)
908 JUMP_TO_NEXT;
909 dev_model = strtoul(token, NULL, 0);
910 free(token);
912 fprintf(fp, "alias ccw:");
913 PRINT_WILDCARD_COND("t", "%04lX", cu_type, match_flags & 0x01);
914 PRINT_WILDCARD_COND("m", "%02lX", cu_model, match_flags & 0x02);
915 PRINT_WILDCARD_COND("dt", "%04lX", dev_type, match_flags & 0x04);
916 PRINT_WILDCARD_COND("dm", "%02lX", dev_model, match_flags & 0x08);
917 if (match_flags & 0x08)
918 fprintf(fp, "*");
919 fprintf(fp, " %s\n", module);
921 free(module);
922 free(line);
925 unmap_file(&ccwmap);
926 free(filename);
929 void alias_from_ofmap(FILE *fp, char *prefix) {
930 struct filemap_t ofmap;
931 char *line, *nline, *nptr;
932 char *token;
933 char *module;
934 char *filename;
936 char *name, *type, *compatible;
938 filename = xmalloc(strlen(prefix) + strlen("modules.ofmap") + 2);
939 strcpy(filename, prefix);
940 strcat(filename, "/modules.ofmap");
942 if (map_file(filename, &ofmap)) {
943 free(filename);
944 return;
947 nptr = ofmap.map;
949 while ((line = dup_line(nptr, &nptr)) != NULL) {
950 nline = line;
952 module = dup_token(nline, &nline, isspace);
953 if (!module || module[0] == '#')
954 JUMP_TO_NEXT;
956 token = dup_token(nline, &nline, isspace);
957 if (!token)
958 JUMP_TO_NEXT;
959 name = token;
961 token = dup_token(nline, &nline, isspace);
962 if (!token) {
963 free(name);
964 JUMP_TO_NEXT;
966 type = token;
968 token = dup_token(nline, &nline, isspace);
969 if (!token) {
970 free(name);
971 free(type);
972 JUMP_TO_NEXT;
974 compatible = token;
976 fprintf(fp, "alias of:");
977 fprintf(fp, "N%sT%sC%s", name, type, compatible);
978 fprintf(fp, " %s\n", module);
980 free(name);
981 free(type);
982 free(compatible);
984 free(module);
985 free(line);
988 unmap_file(&ofmap);
989 free(filename);
992 int main(int argc, char *argv[]) {
993 FILE *fp = NULL;
994 char *prefix = NULL;
995 int i;
997 for (i = 1; i < argc; i++) {
998 if (!strcmp(argv[i], "--output") && i < argc - 1 && !fp) {
999 i++;
1000 if (!strcmp(argv[i], "-")) {
1001 fp = stdout;
1002 } else {
1003 fp = fopen(argv[i], "wb");
1004 if (!fp) {
1005 fprintf(stderr, "Unable to open output file.\n");
1006 return 1;
1009 } else if (!strcmp(argv[i], "--prefix") && i < argc - 1 && !prefix) {
1010 i++;
1011 prefix = argv[i];
1015 if (!fp)
1016 fp = stdout;
1017 if (!prefix)
1018 prefix = ".";
1020 alias_from_ofmap(fp, prefix);
1021 alias_from_ieee1394map(fp, prefix);
1022 alias_from_ccwmap(fp, prefix);
1023 alias_from_seriomap(fp, prefix);
1024 alias_from_ieee1394map(fp, prefix);
1025 alias_from_pcimap(fp, prefix);
1026 alias_from_isapnpmap(fp, prefix);
1027 alias_from_usbmap(fp, prefix);
1028 alias_from_inputmap(fp, prefix);
1030 if (fp != stdout)
1031 fclose(fp);
1033 return 0;