gen-devlist.c 2.1 KB

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