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