1 /*
2  *	Generate devlist.h from the Zorro ID file.
3  *
4  *	(c) 2000 Geert Uytterhoeven <geert@linux-m68k.org>
5  *
6  *	Based on the PCI version:
7  *
8  *	(c) 1999--2000 Martin Mares <mj@ucw.cz>
9  */
10 
11 #include <stdio.h>
12 #include <string.h>
13 
14 #define MAX_NAME_SIZE 63
15 
16 static void
pq(FILE * f,const char * c)17 pq(FILE *f, const char *c)
18 {
19 	while (*c) {
20 		if (*c == '"')
21 			fprintf(f, "\\\"");
22 		else
23 			fputc(*c, f);
24 		c++;
25 	}
26 }
27 
28 int
main(void)29 main(void)
30 {
31 	char line[1024], *c, *bra, manuf[8];
32 	int manufs = 0;
33 	int mode = 0;
34 	int lino = 0;
35 	int manuf_len = 0;
36 	FILE *devf;
37 
38 	devf = fopen("devlist.h", "w");
39 	if (!devf) {
40 		fprintf(stderr, "Cannot create output file!\n");
41 		return 1;
42 	}
43 
44 	while (fgets(line, sizeof(line)-1, stdin)) {
45 		lino++;
46 		if ((c = strchr(line, '\n')))
47 			*c = 0;
48 		if (!line[0] || line[0] == '#')
49 			continue;
50 		if (line[0] == '\t') {
51 			switch (mode) {
52 			case 1:
53 				if (strlen(line) > 5 && line[5] == ' ') {
54 					c = line + 5;
55 					while (*c == ' ')
56 						*c++ = 0;
57 					if (manuf_len + strlen(c) + 1 > MAX_NAME_SIZE) {
58 						/* Too long, try cutting off long description */
59 						bra = strchr(c, '[');
60 						if (bra && bra > c && bra[-1] == ' ')
61 							bra[-1] = 0;
62 						if (manuf_len + strlen(c) + 1 > MAX_NAME_SIZE) {
63 							fprintf(stderr, "Line %d: Product name too long\n", lino);
64 							return 1;
65 						}
66 					}
67 					fprintf(devf, "\tPRODUCT(%s,%s,\"", manuf, line+1);
68 					pq(devf, c);
69 					fputs("\")\n", devf);
70 				} else goto err;
71 				break;
72 			default:
73 				goto err;
74 			}
75 		} else if (strlen(line) > 4 && line[4] == ' ') {
76 			c = line + 4;
77 			while (*c == ' ')
78 				*c++ = 0;
79 			if (manufs)
80 				fputs("ENDMANUF()\n\n", devf);
81 			manufs++;
82 			strcpy(manuf, line);
83 			manuf_len = strlen(c);
84 			if (manuf_len + 24 > MAX_NAME_SIZE) {
85 				fprintf(stderr, "Line %d: manufacturer name too long\n", lino);
86 				return 1;
87 			}
88 			fprintf(devf, "MANUF(%s,\"", manuf);
89 			pq(devf, c);
90 			fputs("\")\n", devf);
91 			mode = 1;
92 		} else {
93 		err:
94 			fprintf(stderr, "Line %d: Syntax error in mode %d: %s\n", lino, mode, line);
95 			return 1;
96 		}
97 	}
98 	fputs("ENDMANUF()\n\
99 \n\
100 #undef MANUF\n\
101 #undef PRODUCT\n\
102 #undef ENDMANUF\n", devf);
103 
104 	fclose(devf);
105 
106 	return 0;
107 }
108