doc_extract.c 1.9 KB

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