doc_extract.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /* This merely extracts, doesn't do XML or anything. */
  2. #include <err.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <stdbool.h>
  11. #include <ccan/talloc/talloc.h>
  12. #include <ccan/str/str.h>
  13. #include <ccan/str_talloc/str_talloc.h>
  14. #include <ccan/grab_file/grab_file.h>
  15. static char **grab_doc(const char *fname)
  16. {
  17. char *file;
  18. char **lines, **ret;
  19. unsigned int i, num;
  20. bool printing = false, printed = false;
  21. file = grab_file(NULL, fname, NULL);
  22. if (!file)
  23. err(1, "Reading file %s", fname);
  24. lines = strsplit(file, file, "\n", &num);
  25. ret = talloc_array(NULL, char *, num+1);
  26. num = 0;
  27. for (i = 0; lines[i]; i++) {
  28. if (streq(lines[i], "/**")) {
  29. printing = true;
  30. if (printed++)
  31. talloc_append_string(ret[num], "\n");
  32. } else if (streq(lines[i], " */"))
  33. printing = false;
  34. else if (printing) {
  35. if (strstarts(lines[i], " * "))
  36. ret[num++] = talloc_strdup(ret, lines[i]+3);
  37. else if (strstarts(lines[i], " *"))
  38. ret[num++] = talloc_strdup(ret, lines[i]+2);
  39. else
  40. errx(1, "Malformed line %s:%u", fname, i);
  41. }
  42. }
  43. ret[num] = NULL;
  44. talloc_free(file);
  45. return ret;
  46. }
  47. static bool is_blank(const char *line)
  48. {
  49. return line && line[strspn(line, " \t\n")] == '\0';
  50. }
  51. static bool is_section(const char *line, bool maybe_one_liner)
  52. {
  53. unsigned int len;
  54. len = strcspn(line, " \t\n:");
  55. if (len == 0)
  56. return false;
  57. if (line[len] != ':')
  58. return false;
  59. /* If it can be a one-liner, a space is sufficient.*/
  60. if (maybe_one_liner && (line[len+1] == ' ' || line[len+1] == '\t'))
  61. return true;
  62. return line[len] == ':' && is_blank(line+len+1);
  63. }
  64. static bool end_section(const char *line)
  65. {
  66. return !line || is_section(line, true);
  67. }
  68. static unsigned int find_section(char **lines, const char *name,
  69. bool maybe_one_liner)
  70. {
  71. unsigned int i;
  72. for (i = 0; lines[i]; i++) {
  73. if (!is_section(lines[i], maybe_one_liner))
  74. continue;
  75. if (strncasecmp(lines[i], name, strlen(name)) != 0)
  76. continue;
  77. if (lines[i][strlen(name)] == ':')
  78. break;
  79. }
  80. return i;
  81. }
  82. int main(int argc, char *argv[])
  83. {
  84. unsigned int i;
  85. const char *type;
  86. if (argc < 3)
  87. errx(1, "Usage: doc_extract TYPE <file>...\n"
  88. "Where TYPE is author|licence|maintainer|summary|description|example|all");
  89. type = argv[1];
  90. for (i = 2; i < argc; i++) {
  91. unsigned int line;
  92. char **lines = grab_doc(argv[i]);
  93. if (!lines[0])
  94. errx(1, "No documentation in file");
  95. /* Simple one-line fields. */
  96. if (streq(type, "author")
  97. || streq(type, "maintainer")
  98. || streq(type, "licence")) {
  99. line = find_section(lines, type, true);
  100. if (lines[line]) {
  101. const char *p = strchr(lines[line], ':') + 1;
  102. p += strspn(p, " \t\n");
  103. if (p[0] == '\0') {
  104. /* Must be on next line. */
  105. if (end_section(lines[line+1]))
  106. errx(1, "Malformed %s", type);
  107. puts(lines[line+1]);
  108. } else
  109. puts(p);
  110. }
  111. } else if (streq(type, "summary")) {
  112. /* Summary comes after - on first line. */
  113. char *dash;
  114. dash = strchr(lines[0], '-');
  115. if (!dash)
  116. errx(1, "Malformed first line: no -");
  117. dash += strspn(dash, "- ");
  118. puts(dash);
  119. } else if (streq(type, "description")) {
  120. line = 1;
  121. while (is_blank(lines[line]))
  122. line++;
  123. while (!end_section(lines[line]))
  124. puts(lines[line++]);
  125. } else if (streq(type, "example")) {
  126. line = find_section(lines, type, false);
  127. if (lines[line]) {
  128. unsigned int strip;
  129. line++;
  130. while (is_blank(lines[line]))
  131. line++;
  132. /* Examples can be indented. Take cue
  133. * from first non-blank line. */
  134. if (lines[line])
  135. strip = strspn(lines[line], " \t");
  136. while (!end_section(lines[line])) {
  137. if (strspn(lines[line], " \t") >= strip)
  138. puts(lines[line] + strip);
  139. else
  140. puts(lines[line]);
  141. line++;
  142. }
  143. }
  144. } else if (streq(type, "all")) {
  145. for (line = 0; lines[line]; line++)
  146. puts(lines[line]);
  147. } else
  148. errx(1, "Unknown type '%s'", type);
  149. talloc_free(lines);
  150. }
  151. return 0;
  152. }