doc_extract.c 1.9 KB

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