doc_extract.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. #include "tools.h"
  16. static char **grab_doc(const char *fname)
  17. {
  18. char *file;
  19. char **lines, **ret;
  20. unsigned int i, num;
  21. bool printing = false;
  22. file = grab_file(NULL, fname, NULL);
  23. if (!file)
  24. err(1, "Reading file %s", fname);
  25. lines = strsplit(file, file, "\n", &num);
  26. ret = talloc_array(NULL, char *, num+1);
  27. num = 0;
  28. for (i = 0; lines[i]; i++) {
  29. if (streq(lines[i], "/**")) {
  30. printing = true;
  31. if (num != 0)
  32. talloc_append_string(ret[num-1], "\n");
  33. } else if (streq(lines[i], " */"))
  34. printing = false;
  35. else if (printing) {
  36. if (strstarts(lines[i], " * "))
  37. ret[num++] = talloc_strdup(ret, lines[i]+3);
  38. else if (strstarts(lines[i], " *"))
  39. ret[num++] = talloc_strdup(ret, lines[i]+2);
  40. else
  41. errx(1, "Malformed line %s:%u", fname, i);
  42. }
  43. }
  44. ret[num] = NULL;
  45. talloc_free(file);
  46. return ret;
  47. }
  48. static bool is_blank(const char *line)
  49. {
  50. return line && line[strspn(line, " \t\n")] == '\0';
  51. }
  52. static bool is_section(const char *line, bool maybe_one_liner)
  53. {
  54. unsigned int len;
  55. len = strcspn(line, " \t\n:");
  56. if (len == 0)
  57. return false;
  58. if (line[len] != ':')
  59. return false;
  60. /* If it can be a one-liner, a space is sufficient.*/
  61. if (maybe_one_liner && (line[len+1] == ' ' || line[len+1] == '\t'))
  62. return true;
  63. return line[len] == ':' && is_blank(line+len+1);
  64. }
  65. /* Summary line is form '<identifier> - ' */
  66. static bool is_summary_line(const char *line)
  67. {
  68. unsigned int id_len;
  69. id_len = strspn(line, IDENT_CHARS);
  70. if (id_len == 0)
  71. return false;
  72. if (!strstarts(line + id_len, " - "))
  73. return false;
  74. return true;
  75. }
  76. static bool end_section(const char *line)
  77. {
  78. return !line || is_section(line, true) || is_summary_line(line);
  79. }
  80. static unsigned int find_section(char **lines, const char *name,
  81. bool maybe_one_liner)
  82. {
  83. unsigned int i;
  84. for (i = 0; lines[i]; i++) {
  85. if (!is_section(lines[i], maybe_one_liner))
  86. continue;
  87. if (strncasecmp(lines[i], name, strlen(name)) != 0)
  88. continue;
  89. if (lines[i][strlen(name)] == ':')
  90. break;
  91. }
  92. return i;
  93. }
  94. /* function is NULL if we don't care. */
  95. static unsigned int find_summary(char **lines, const char *function)
  96. {
  97. unsigned int i;
  98. for (i = 0; lines[i]; i++) {
  99. if (!is_summary_line(lines[i]))
  100. continue;
  101. if (function) {
  102. if (!strstarts(lines[i], function))
  103. continue;
  104. if (!strstarts(lines[i] + strlen(function), " - "))
  105. continue;
  106. }
  107. break;
  108. }
  109. return i;
  110. }
  111. int main(int argc, char *argv[])
  112. {
  113. unsigned int i;
  114. const char *type;
  115. const char *function = NULL;
  116. if (argc < 3)
  117. errx(1, "Usage: doc_extract [--function=<funcname>] TYPE <file>...\n"
  118. "Where TYPE is functions|author|licence|maintainer|summary|description|example|all");
  119. if (strstarts(argv[1], "--function=")) {
  120. function = argv[1] + strlen("--function=");
  121. argv++;
  122. argc--;
  123. }
  124. type = argv[1];
  125. for (i = 2; i < argc; i++) {
  126. unsigned int line;
  127. char **lines = grab_doc(argv[i]);
  128. if (!lines[0])
  129. errx(1, "No documentation in file %s", argv[i]);
  130. if (function) {
  131. /* Allow us to trawl multiple files for a function */
  132. line = find_summary(lines, function);
  133. if (!lines[line])
  134. continue;
  135. /* Trim to just this function then. */
  136. lines += line;
  137. lines[find_summary(lines+1, NULL)] = NULL;
  138. }
  139. /* Simple one-line fields. */
  140. if (streq(type, "author")
  141. || streq(type, "maintainer")
  142. || streq(type, "licence")) {
  143. line = find_section(lines, type, true);
  144. if (lines[line]) {
  145. const char *p = strchr(lines[line], ':') + 1;
  146. p += strspn(p, " \t\n");
  147. if (p[0] == '\0') {
  148. /* Must be on next line. */
  149. if (end_section(lines[line+1]))
  150. errx(1, "Malformed %s", type);
  151. puts(lines[line+1]);
  152. } else
  153. puts(p);
  154. }
  155. } else if (streq(type, "summary")) {
  156. /* Summary comes after - on first line. */
  157. char *dash;
  158. dash = strchr(lines[0], '-');
  159. if (!dash)
  160. errx(1, "Malformed first line: no -");
  161. dash += strspn(dash, "- ");
  162. puts(dash);
  163. } else if (streq(type, "description")) {
  164. line = 1;
  165. while (is_blank(lines[line]))
  166. line++;
  167. while (!end_section(lines[line]))
  168. puts(lines[line++]);
  169. } else if (streq(type, "example")) {
  170. line = find_section(lines, type, false);
  171. if (lines[line]) {
  172. unsigned int strip;
  173. line++;
  174. while (is_blank(lines[line]))
  175. line++;
  176. /* Examples can be indented. Take cue
  177. * from first non-blank line. */
  178. if (lines[line])
  179. strip = strspn(lines[line], " \t");
  180. while (!end_section(lines[line])) {
  181. if (strspn(lines[line], " \t") >= strip)
  182. puts(lines[line] + strip);
  183. else
  184. puts(lines[line]);
  185. line++;
  186. }
  187. }
  188. } else if (streq(type, "functions")) {
  189. while (lines[line = find_summary(lines, NULL)]) {
  190. const char *dash = strstr(lines[line], " - ");
  191. printf("%.*s\n",
  192. dash - lines[line], lines[line]);
  193. lines += line+1;
  194. }
  195. } else if (streq(type, "all")) {
  196. for (line = 0; lines[line]; line++)
  197. puts(lines[line]);
  198. } else
  199. errx(1, "Unknown type '%s'", type);
  200. }
  201. return 0;
  202. }