doc_extract.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* This merely extracts, doesn't do XML or anything. */
  2. #include <err.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <ccan/str/str.h>
  6. #include <ccan/str_talloc/str_talloc.h>
  7. #include <ccan/talloc/talloc.h>
  8. #include <ccan/grab_file/grab_file.h>
  9. #include "doc_extract.h"
  10. int main(int argc, char *argv[])
  11. {
  12. unsigned int i;
  13. const char *type;
  14. const char *function = NULL;
  15. if (argc < 3)
  16. errx(1, "Usage: doc_extract [--function=<funcname>] TYPE <file>...\n"
  17. "Where TYPE is functions|author|license|maintainer|summary|description|example|all");
  18. if (strstarts(argv[1], "--function=")) {
  19. function = argv[1] + strlen("--function=");
  20. argv++;
  21. argc--;
  22. }
  23. type = argv[1];
  24. for (i = 2; i < argc; i++) {
  25. char *file, **lines;
  26. struct list_head *list;
  27. struct doc_section *d;
  28. file = grab_file(NULL, argv[i], NULL);
  29. if (!file)
  30. err(1, "Reading file %s", argv[i]);
  31. lines = strsplit(file, file, "\n");
  32. list = extract_doc_sections(lines);
  33. if (list_empty(list))
  34. errx(1, "No documentation in file %s", argv[i]);
  35. talloc_free(file);
  36. if (streq(type, "functions")) {
  37. const char *last = NULL;
  38. list_for_each(list, d, list) {
  39. if (d->function) {
  40. if (!last || !streq(d->function, last))
  41. printf("%s\n", d->function);
  42. last = d->function;
  43. }
  44. }
  45. } else {
  46. unsigned int j;
  47. list_for_each(list, d, list) {
  48. if (function) {
  49. if (!d->function)
  50. continue;
  51. if (!streq(d->function, function))
  52. continue;
  53. }
  54. if (streq(type, "all"))
  55. printf("%s:\n", d->type);
  56. else if (!streq(d->type, type))
  57. continue;
  58. for (j = 0; j < d->num_lines; j++)
  59. printf("%s\n", d->lines[j]);
  60. }
  61. }
  62. talloc_free(list);
  63. }
  64. return 0;
  65. }