home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Linux / PCI / gen-devlist.c < prev    next >
C/C++ Source or Header  |  2001-11-04  |  3KB  |  126 lines

  1. /*
  2.  *    Generate devlist.h and classlist.h from the PCI ID file.
  3.  *
  4.  *    (c) 1999--2000 Martin Mares <mj@ucw.cz>
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <string.h>
  9.  
  10. #define MAX_NAME_SIZE 79
  11.  
  12. static void
  13. pq(FILE *f, const char *c)
  14. {
  15.     while (*c) {
  16.         if (*c == '"')
  17.             fprintf(f, "\\\"");
  18.         else
  19.             fputc(*c, f);
  20.         c++;
  21.     }
  22. }
  23.  
  24. int
  25. main(void)
  26. {
  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;
  33.  
  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;
  39.     }
  40.  
  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;
  54.         }
  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.                             fprintf(stderr, "%s\n", c);
  72.                             return 1;
  73.                         }
  74.                     }
  75.                     fprintf(devf, "\tDEVICE(%s,%s,\"", vend, line+1);
  76.                     pq(devf, c);
  77.                     fputs("\")\n", devf);
  78.                 } else goto err;
  79.                 break;
  80.             case 2:
  81.                 if (strlen(line) > 3 && line[3] == ' ') {
  82.                     c = line + 3;
  83.                     while (*c == ' ')
  84.                         *c++ = 0;
  85.                     fprintf(clsf, "CLASS(%s%s, \"%s\")\n", vend, line+1, c);
  86.                 } else goto err;
  87.                 break;
  88.             default:
  89.                 goto err;
  90.             }
  91.         } else if (strlen(line) > 4 && line[4] == ' ') {
  92.             c = line + 4;
  93.             while (*c == ' ')
  94.                 *c++ = 0;
  95.             if (vendors)
  96.                 fputs("ENDVENDOR()\n\n", devf);
  97.             vendors++;
  98.             strcpy(vend, line);
  99.             vendor_len = strlen(c);
  100.             if (vendor_len + 24 > MAX_NAME_SIZE) {
  101.                 fprintf(stderr, "Line %d: Vendor name too long\n", lino);
  102.                 return 1;
  103.             }
  104.             fprintf(devf, "VENDOR(%s,\"", vend);
  105.             pq(devf, c);
  106.             fputs("\")\n", devf);
  107.             mode = 1;
  108.         } else {
  109.         err:
  110.             fprintf(stderr, "Line %d: Syntax error in mode %d: %s\n", lino, mode, line);
  111.             return 1;
  112.         }
  113.     }
  114.     fputs("ENDVENDOR()\n\
  115. \n\
  116. #undef VENDOR\n\
  117. #undef DEVICE\n\
  118. #undef ENDVENDOR\n", devf);
  119.     fputs("\n#undef CLASS\n", clsf);
  120.  
  121.     fclose(devf);
  122.     fclose(clsf);
  123.  
  124.     return 0;
  125. }
  126.