doc_extract.c 2.0 KB

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