doc_extract-core.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* This merely extracts, doesn't do XML or anything. */
  2. #include <err.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <stdbool.h>
  11. #include <ctype.h>
  12. #include <ccan/talloc/talloc.h>
  13. #include <ccan/str/str.h>
  14. #include "doc_extract.h"
  15. #include "tools.h"
  16. static char **grab_doc(char **lines, unsigned int num)
  17. {
  18. char **ret;
  19. unsigned int i;
  20. bool printing = false;
  21. ret = talloc_array(NULL, char *, num+1);
  22. num = 0;
  23. for (i = 0; lines[i]; i++) {
  24. if (streq(lines[i], "/**")) {
  25. printing = true;
  26. if (num != 0) {
  27. ret[num-1] = talloc_append_string(ret[num-1],
  28. "\n");
  29. }
  30. } else if (streq(lines[i], " */"))
  31. printing = false;
  32. else if (printing) {
  33. if (strstarts(lines[i], " * "))
  34. ret[num++] = talloc_strdup(ret, lines[i]+3);
  35. else if (strstarts(lines[i], " *"))
  36. ret[num++] = talloc_strdup(ret, lines[i]+2);
  37. else
  38. errx(1, "Malformed line %u", i);
  39. }
  40. }
  41. ret[num] = NULL;
  42. return ret;
  43. }
  44. static bool is_blank(const char *line)
  45. {
  46. return line && line[strspn(line, " \t\n")] == '\0';
  47. }
  48. static bool is_section(const char *line, bool one_liner)
  49. {
  50. unsigned int len;
  51. if (!isupper(line[0]))
  52. return false;
  53. len = strspn(line, IDENT_CHARS" ");
  54. if (line[len] != ':')
  55. return false;
  56. /* If it can be a one-liner, a space is sufficient.*/
  57. if (one_liner)
  58. return (line[len+1] == ' ' || line[len+1] == '\t');
  59. return line[len] == ':' && is_blank(line+len+1);
  60. }
  61. /* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */
  62. static unsigned int is_summary_line(const char *line)
  63. {
  64. unsigned int id_len;
  65. id_len = strspn(line, IDENT_CHARS" ");
  66. if (id_len == 0)
  67. return 0;
  68. if (!strstarts(line + id_len-1, " - "))
  69. return 0;
  70. return id_len - 1;
  71. }
  72. static bool empty_section(struct doc_section *d)
  73. {
  74. unsigned int i;
  75. for (i = 0; i < d->num_lines; i++)
  76. if (!is_blank(d->lines[i]))
  77. return false;
  78. return true;
  79. }
  80. static struct doc_section *new_section(struct list_head *list,
  81. const char *function,
  82. const char *type)
  83. {
  84. struct doc_section *d;
  85. char *lowertype;
  86. unsigned int i;
  87. /* If previous section was empty, delete it. */
  88. d = list_tail(list, struct doc_section, list);
  89. if (d && empty_section(d)) {
  90. list_del(&d->list);
  91. talloc_free(d);
  92. }
  93. d = talloc(list, struct doc_section);
  94. d->function = function;
  95. lowertype = talloc_size(d, strlen(type) + 1);
  96. /* Canonicalize type to lower case. */
  97. for (i = 0; i < strlen(type)+1; i++)
  98. lowertype[i] = tolower(type[i]);
  99. d->type = lowertype;
  100. d->lines = NULL;
  101. d->num_lines = 0;
  102. list_add_tail(list, &d->list);
  103. return d;
  104. }
  105. static void add_line(struct doc_section *curr, const char *line)
  106. {
  107. curr->lines = talloc_realloc(curr, curr->lines, char *,
  108. curr->num_lines+1);
  109. curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
  110. }
  111. struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
  112. {
  113. char **lines = grab_doc(rawlines, num);
  114. const char *function = NULL;
  115. struct doc_section *curr = NULL;
  116. unsigned int i;
  117. struct list_head *list;
  118. list = talloc(NULL, struct list_head);
  119. list_head_init(list);
  120. for (i = 0; lines[i]; i++) {
  121. unsigned funclen;
  122. funclen = is_summary_line(lines[i]);
  123. if (funclen) {
  124. function = talloc_strndup(list, lines[i], funclen);
  125. curr = new_section(list, function, "summary");
  126. add_line(curr, lines[i] + funclen + 3);
  127. curr = new_section(list, function, "description");
  128. } else if (is_section(lines[i], false)) {
  129. char *type = talloc_strndup(curr, lines[i],
  130. strcspn(lines[i], ":"));
  131. curr = new_section(list, function, type);
  132. } else if (is_section(lines[i], true)) {
  133. unsigned int sectlen = strcspn(lines[i], ":");
  134. char *type = talloc_strndup(curr, lines[i], sectlen);
  135. curr = new_section(list, function, type);
  136. add_line(curr, lines[i] + sectlen + 1
  137. + strspn(lines[i] + sectlen + 1, " \t"));
  138. } else {
  139. if (!curr)
  140. continue;
  141. add_line(curr, lines[i]);
  142. }
  143. }
  144. talloc_free(lines);
  145. return list;
  146. }