doc_extract.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. unsigned int num;
  27. struct list_head *list;
  28. struct doc_section *d;
  29. file = grab_file(NULL, argv[i], NULL);
  30. if (!file)
  31. err(1, "Reading file %s", argv[i]);
  32. lines = strsplit(file, file, "\n", &num);
  33. list = extract_doc_sections(lines, num);
  34. if (list_empty(list))
  35. errx(1, "No documentation in file %s", argv[i]);
  36. talloc_free(file);
  37. if (streq(type, "functions")) {
  38. const char *last = NULL;
  39. list_for_each(list, d, list) {
  40. if (d->function) {
  41. if (!last || !streq(d->function, last))
  42. printf("%s\n", d->function);
  43. last = d->function;
  44. }
  45. }
  46. } else {
  47. unsigned int j;
  48. list_for_each(list, d, list) {
  49. if (function) {
  50. if (!d->function)
  51. continue;
  52. if (!streq(d->function, function))
  53. continue;
  54. }
  55. if (streq(type, "all"))
  56. printf("%s:\n", d->type);
  57. else if (!streq(d->type, type))
  58. continue;
  59. for (j = 0; j < d->num_lines; j++)
  60. printf("%s\n", d->lines[j]);
  61. }
  62. }
  63. talloc_free(list);
  64. }
  65. return 0;
  66. }