gen-devlist.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:3k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 79
  9. static void
  10. pq(FILE *f, const char *c)
  11. {
  12. while (*c) {
  13. if (*c == '"')
  14. fprintf(f, "\"");
  15. else {
  16. fputc(*c, f);
  17. if (*c == '?' && c[1] == '?') {
  18. /* Avoid trigraphs */
  19. fprintf(f, "" "");
  20. }
  21. }
  22. c++;
  23. }
  24. }
  25. int
  26. main(void)
  27. {
  28. char line[1024], *c, *bra, vend[8];
  29. int vendors = 0;
  30. int mode = 0;
  31. int lino = 0;
  32. int vendor_len = 0;
  33. FILE *devf, *clsf;
  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. while (fgets(line, sizeof(line)-1, stdin)) {
  41. lino++;
  42. if ((c = strchr(line, 'n')))
  43. *c = 0;
  44. if (!line[0] || line[0] == '#')
  45. continue;
  46. if (line[1] == ' ') {
  47. if (line[0] == 'C' && strlen(line) > 4 && line[4] == ' ') {
  48. vend[0] = line[2];
  49. vend[1] = line[3];
  50. vend[2] = 0;
  51. mode = 2;
  52. } else goto err;
  53. }
  54. else if (line[0] == 't') {
  55. if (line[1] == 't')
  56. continue;
  57. switch (mode) {
  58. case 1:
  59. if (strlen(line) > 5 && line[5] == ' ') {
  60. c = line + 5;
  61. while (*c == ' ')
  62. *c++ = 0;
  63. if (vendor_len + strlen(c) + 1 > MAX_NAME_SIZE) {
  64. /* Too long, try cutting off long description */
  65. bra = strchr(c, '[');
  66. if (bra && bra > c && bra[-1] == ' ')
  67. bra[-1] = 0;
  68. if (vendor_len + strlen(c) + 1 > MAX_NAME_SIZE) {
  69. fprintf(stderr, "Line %d: Device name too longn", lino);
  70. fprintf(stderr, "%sn", c);
  71. return 1;
  72. }
  73. }
  74. fprintf(devf, "tDEVICE(%s,%s,"", vend, line+1);
  75. pq(devf, c);
  76. fputs("")n", devf);
  77. } else goto err;
  78. break;
  79. case 2:
  80. if (strlen(line) > 3 && line[3] == ' ') {
  81. c = line + 3;
  82. while (*c == ' ')
  83. *c++ = 0;
  84. fprintf(clsf, "CLASS(%s%s, "%s")n", vend, line+1, c);
  85. } else goto err;
  86. break;
  87. default:
  88. goto err;
  89. }
  90. } else if (strlen(line) > 4 && line[4] == ' ') {
  91. c = line + 4;
  92. while (*c == ' ')
  93. *c++ = 0;
  94. if (vendors)
  95. fputs("ENDVENDOR()nn", devf);
  96. vendors++;
  97. strcpy(vend, line);
  98. vendor_len = strlen(c);
  99. if (vendor_len + 24 > MAX_NAME_SIZE) {
  100. fprintf(stderr, "Line %d: Vendor name too longn", lino);
  101. return 1;
  102. }
  103. fprintf(devf, "VENDOR(%s,"", vend);
  104. pq(devf, c);
  105. fputs("")n", devf);
  106. mode = 1;
  107. } else {
  108. err:
  109. fprintf(stderr, "Line %d: Syntax error in mode %d: %sn", lino, mode, line);
  110. return 1;
  111. }
  112. }
  113. fputs("ENDVENDOR()n
  114. n
  115. #undef VENDORn
  116. #undef DEVICEn
  117. #undef ENDVENDORn", devf);
  118. fputs("n#undef CLASSn", clsf);
  119. fclose(devf);
  120. fclose(clsf);
  121. return 0;
  122. }