gen-devlist.c 2.8 KB

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