- Linus: drop support for old-style Makefiles entirely. Big.
[davej-history.git] / drivers / pci / gen-devlist.c
blob586ef3d2d9df39339746b2546465a49aca402340
1 /*
2 * Generate devlist.h and classlist.h from the PCI ID file.
4 * (c) 1999--2000 Martin Mares <mj@suse.cz>
5 */
7 #include <stdio.h>
8 #include <string.h>
10 #define MAX_NAME_SIZE 79
12 static void
13 pq(FILE *f, const char *c)
15 while (*c) {
16 if (*c == '"')
17 fprintf(f, "\\\"");
18 else
19 fputc(*c, f);
20 c++;
24 int
25 main(void)
27 char line[1024], *c, *bra, vend[8];
28 int vendors = 0;
29 int mode = 0;
30 int lino = 0;
31 int vendor_len = 0;
32 FILE *devf, *clsf;
34 devf = fopen("devlist.h", "w");
35 clsf = fopen("classlist.h", "w");
36 if (!devf || !clsf) {
37 fprintf(stderr, "Cannot create output file!\n");
38 return 1;
41 while (fgets(line, sizeof(line)-1, stdin)) {
42 lino++;
43 if ((c = strchr(line, '\n')))
44 *c = 0;
45 if (!line[0] || line[0] == '#')
46 continue;
47 if (line[1] == ' ') {
48 if (line[0] == 'C' && strlen(line) > 4 && line[4] == ' ') {
49 vend[0] = line[2];
50 vend[1] = line[3];
51 vend[2] = 0;
52 mode = 2;
53 } else goto err;
55 else if (line[0] == '\t') {
56 if (line[1] == '\t')
57 continue;
58 switch (mode) {
59 case 1:
60 if (strlen(line) > 5 && line[5] == ' ') {
61 c = line + 5;
62 while (*c == ' ')
63 *c++ = 0;
64 if (vendor_len + strlen(c) + 1 > MAX_NAME_SIZE) {
65 /* Too long, try cutting off long description */
66 bra = strchr(c, '[');
67 if (bra && bra > c && bra[-1] == ' ')
68 bra[-1] = 0;
69 if (vendor_len + strlen(c) + 1 > MAX_NAME_SIZE) {
70 fprintf(stderr, "Line %d: Device name too long\n", lino);
71 return 1;
74 fprintf(devf, "\tDEVICE(%s,%s,\"", vend, line+1);
75 pq(devf, c);
76 fputs("\")\n", devf);
77 } else goto err;
78 break;
79 case 2:
80 if (strlen(line) > 3 && line[3] == ' ') {
81 c = line + 3;
82 while (*c == ' ')
83 *c++ = 0;
84 fprintf(clsf, "CLASS(%s%s, \"%s\")\n", vend, line+1, c);
85 } else goto err;
86 break;
87 default:
88 goto err;
90 } else if (strlen(line) > 4 && line[4] == ' ') {
91 c = line + 4;
92 while (*c == ' ')
93 *c++ = 0;
94 if (vendors)
95 fputs("ENDVENDOR()\n\n", devf);
96 vendors++;
97 strcpy(vend, line);
98 vendor_len = strlen(c);
99 if (vendor_len + 24 > MAX_NAME_SIZE) {
100 fprintf(stderr, "Line %d: Vendor name too long\n", lino);
101 return 1;
103 fprintf(devf, "VENDOR(%s,\"", vend);
104 pq(devf, c);
105 fputs("\")\n", devf);
106 mode = 1;
107 } else {
108 err:
109 fprintf(stderr, "Line %d: Syntax error in mode %d: %s\n", lino, mode, line);
110 return 1;
113 fputs("ENDVENDOR()\n\
115 #undef VENDOR\n\
116 #undef DEVICE\n\
117 #undef ENDVENDOR\n", devf);
118 fputs("\n#undef CLASS\n", clsf);
120 fclose(devf);
121 fclose(clsf);
123 return 0;