doc_extract-core.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <ccan/str_talloc/str_talloc.h>
  15. #include "doc_extract.h"
  16. #include "tools.h"
  17. static char **grab_doc(char **lines, unsigned int num)
  18. {
  19. char **ret;
  20. unsigned int i;
  21. bool printing = false;
  22. ret = talloc_array(NULL, char *, num+1);
  23. num = 0;
  24. for (i = 0; lines[i]; i++) {
  25. if (streq(lines[i], "/**")) {
  26. printing = true;
  27. if (num != 0) {
  28. ret[num-1] = talloc_append_string(ret[num-1],
  29. "\n");
  30. }
  31. } else if (streq(lines[i], " */"))
  32. printing = false;
  33. else if (printing) {
  34. if (strstarts(lines[i], " * "))
  35. ret[num++] = talloc_strdup(ret, lines[i]+3);
  36. else if (strstarts(lines[i], " *"))
  37. ret[num++] = talloc_strdup(ret, lines[i]+2);
  38. else
  39. errx(1, "Malformed line %u", i);
  40. }
  41. }
  42. ret[num] = NULL;
  43. return ret;
  44. }
  45. static bool is_blank(const char *line)
  46. {
  47. return line && line[strspn(line, " \t\n")] == '\0';
  48. }
  49. static char *is_section(const void *ctx, const char *line, char **value)
  50. {
  51. char *secname;
  52. /* Any number of upper case words separated by spaces, ending in : */
  53. if (!strreg(ctx, line,
  54. "^([A-Z][a-zA-Z0-9_]*( [A-Z][a-zA-Z0-9_]*)*):[ \t\n]*(.*)",
  55. &secname, NULL, value))
  56. return NULL;
  57. return secname;
  58. }
  59. /* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */
  60. static unsigned int is_summary_line(const char *line)
  61. {
  62. unsigned int id_len;
  63. id_len = strspn(line, IDENT_CHARS" ");
  64. if (id_len == 0)
  65. return 0;
  66. if (strspn(line, " ") == id_len)
  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. char *type, *extra;
  123. funclen = is_summary_line(lines[i]);
  124. if (funclen) {
  125. function = talloc_strndup(list, lines[i], funclen);
  126. curr = new_section(list, function, "summary");
  127. add_line(curr, lines[i] + funclen + 3);
  128. curr = new_section(list, function, "description");
  129. } else if ((type = is_section(list, lines[i], &extra)) != NULL){
  130. curr = new_section(list, function, type);
  131. if (!streq(extra, "")) {
  132. add_line(curr, extra);
  133. curr = NULL;
  134. }
  135. } else {
  136. if (curr)
  137. add_line(curr, lines[i]);
  138. }
  139. }
  140. talloc_free(lines);
  141. return list;
  142. }