gen-devlist.c 2.1 KB

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