doc_extract-core.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 = 0;
  51. /* Any number of upper case words separated by spaces, ending in : */
  52. for (;;) {
  53. if (!isupper(line[len]))
  54. return false;
  55. len += strspn(line+len, IDENT_CHARS);
  56. if (line[len] == ':')
  57. break;
  58. if (line[len] != ' ')
  59. return false;
  60. len++;
  61. }
  62. /* If it can be a one-liner, a space is sufficient.*/
  63. if (one_liner)
  64. return (line[len+1] == ' ' || line[len+1] == '\t');
  65. return line[len] == ':' && is_blank(line+len+1);
  66. }
  67. /* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */
  68. static unsigned int is_summary_line(const char *line)
  69. {
  70. unsigned int id_len;
  71. id_len = strspn(line, IDENT_CHARS" ");
  72. if (id_len == 0)
  73. return 0;
  74. if (!strstarts(line + id_len-1, " - "))
  75. return 0;
  76. return id_len - 1;
  77. }
  78. static bool empty_section(struct doc_section *d)
  79. {
  80. unsigned int i;
  81. for (i = 0; i < d->num_lines; i++)
  82. if (!is_blank(d->lines[i]))
  83. return false;
  84. return true;
  85. }
  86. static struct doc_section *new_section(struct list_head *list,
  87. const char *function,
  88. const char *type)
  89. {
  90. struct doc_section *d;
  91. char *lowertype;
  92. unsigned int i;
  93. /* If previous section was empty, delete it. */
  94. d = list_tail(list, struct doc_section, list);
  95. if (d && empty_section(d)) {
  96. list_del(&d->list);
  97. talloc_free(d);
  98. }
  99. d = talloc(list, struct doc_section);
  100. d->function = function;
  101. lowertype = talloc_size(d, strlen(type) + 1);
  102. /* Canonicalize type to lower case. */
  103. for (i = 0; i < strlen(type)+1; i++)
  104. lowertype[i] = tolower(type[i]);
  105. d->type = lowertype;
  106. d->lines = NULL;
  107. d->num_lines = 0;
  108. list_add_tail(list, &d->list);
  109. return d;
  110. }
  111. static void add_line(struct doc_section *curr, const char *line)
  112. {
  113. curr->lines = talloc_realloc(curr, curr->lines, char *,
  114. curr->num_lines+1);
  115. curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
  116. }
  117. struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
  118. {
  119. char **lines = grab_doc(rawlines, num);
  120. const char *function = NULL;
  121. struct doc_section *curr = NULL;
  122. unsigned int i;
  123. struct list_head *list;
  124. list = talloc(NULL, struct list_head);
  125. list_head_init(list);
  126. for (i = 0; lines[i]; i++) {
  127. unsigned funclen;
  128. funclen = is_summary_line(lines[i]);
  129. if (funclen) {
  130. function = talloc_strndup(list, lines[i], funclen);
  131. curr = new_section(list, function, "summary");
  132. add_line(curr, lines[i] + funclen + 3);
  133. curr = new_section(list, function, "description");
  134. } else if (is_section(lines[i], false)) {
  135. char *type = talloc_strndup(curr, lines[i],
  136. strcspn(lines[i], ":"));
  137. curr = new_section(list, function, type);
  138. } else if (is_section(lines[i], true)) {
  139. unsigned int sectlen = strcspn(lines[i], ":");
  140. char *type = talloc_strndup(curr, lines[i], sectlen);
  141. curr = new_section(list, function, type);
  142. add_line(curr, lines[i] + sectlen + 1
  143. + strspn(lines[i] + sectlen + 1, " \t"));
  144. } else {
  145. if (!curr)
  146. continue;
  147. add_line(curr, lines[i]);
  148. }
  149. }
  150. talloc_free(lines);
  151. return list;
  152. }