Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  Generate devlist.h from the Zorro ID file.
0004  *
0005  *  (c) 2000 Geert Uytterhoeven <geert@linux-m68k.org>
0006  *
0007  *  Based on the PCI version:
0008  *
0009  *  (c) 1999--2000 Martin Mares <mj@ucw.cz>
0010  */
0011 
0012 #include <stdio.h>
0013 #include <string.h>
0014 
0015 #define MAX_NAME_SIZE 63
0016 
0017 static void
0018 pq(FILE *f, const char *c)
0019 {
0020     while (*c) {
0021         if (*c == '"')
0022             fprintf(f, "\\\"");
0023         else
0024             fputc(*c, f);
0025         c++;
0026     }
0027 }
0028 
0029 int
0030 main(void)
0031 {
0032     char line[1024], *c, *bra, manuf[8];
0033     int manufs = 0;
0034     int mode = 0;
0035     int lino = 0;
0036     int manuf_len = 0;
0037     FILE *devf;
0038 
0039     devf = fopen("devlist.h", "w");
0040     if (!devf) {
0041         fprintf(stderr, "Cannot create output file!\n");
0042         return 1;
0043     }
0044 
0045     while (fgets(line, sizeof(line)-1, stdin)) {
0046         lino++;
0047         if ((c = strchr(line, '\n')))
0048             *c = 0;
0049         if (!line[0] || line[0] == '#')
0050             continue;
0051         if (line[0] == '\t') {
0052             switch (mode) {
0053             case 1:
0054                 if (strlen(line) > 5 && line[5] == ' ') {
0055                     c = line + 5;
0056                     while (*c == ' ')
0057                         *c++ = 0;
0058                     if (manuf_len + strlen(c) + 1 > MAX_NAME_SIZE) {
0059                         /* Too long, try cutting off long description */
0060                         bra = strchr(c, '[');
0061                         if (bra && bra > c && bra[-1] == ' ')
0062                             bra[-1] = 0;
0063                         if (manuf_len + strlen(c) + 1 > MAX_NAME_SIZE) {
0064                             fprintf(stderr, "Line %d: Product name too long\n", lino);
0065                             return 1;
0066                         }
0067                     }
0068                     fprintf(devf, "\tPRODUCT(%s,%s,\"", manuf, line+1);
0069                     pq(devf, c);
0070                     fputs("\")\n", devf);
0071                 } else goto err;
0072                 break;
0073             default:
0074                 goto err;
0075             }
0076         } else if (strlen(line) > 4 && line[4] == ' ') {
0077             c = line + 4;
0078             while (*c == ' ')
0079                 *c++ = 0;
0080             if (manufs)
0081                 fputs("ENDMANUF()\n\n", devf);
0082             manufs++;
0083             strcpy(manuf, line);
0084             manuf_len = strlen(c);
0085             if (manuf_len + 24 > MAX_NAME_SIZE) {
0086                 fprintf(stderr, "Line %d: manufacturer name too long\n", lino);
0087                 return 1;
0088             }
0089             fprintf(devf, "MANUF(%s,\"", manuf);
0090             pq(devf, c);
0091             fputs("\")\n", devf);
0092             mode = 1;
0093         } else {
0094         err:
0095             fprintf(stderr, "Line %d: Syntax error in mode %d: %s\n", lino, mode, line);
0096             return 1;
0097         }
0098     }
0099     fputs("ENDMANUF()\n\
0100 \n\
0101 #undef MANUF\n\
0102 #undef PRODUCT\n\
0103 #undef ENDMANUF\n", devf);
0104 
0105     fclose(devf);
0106 
0107     return 0;
0108 }