doc_extract-core.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. talloc_append_string(ret[num-1], "\n");
  28. } else if (streq(lines[i], " */"))
  29. printing = false;
  30. else if (printing) {
  31. if (strstarts(lines[i], " * "))
  32. ret[num++] = talloc_strdup(ret, lines[i]+3);
  33. else if (strstarts(lines[i], " *"))
  34. ret[num++] = talloc_strdup(ret, lines[i]+2);
  35. else
  36. errx(1, "Malformed line %u", i);
  37. }
  38. }
  39. ret[num] = NULL;
  40. return ret;
  41. }
  42. static bool is_blank(const char *line)
  43. {
  44. return line && line[strspn(line, " \t\n")] == '\0';
  45. }
  46. static bool is_section(const char *line, bool one_liner)
  47. {
  48. unsigned int len;
  49. if (!isupper(line[0]))
  50. return false;
  51. len = strspn(line, IDENT_CHARS);
  52. if (line[len] != ':')
  53. return false;
  54. /* If it can be a one-liner, a space is sufficient.*/
  55. if (one_liner)
  56. return (line[len+1] == ' ' || line[len+1] == '\t');
  57. return line[len] == ':' && is_blank(line+len+1);
  58. }
  59. /* Summary line is form '<identifier> - ' */
  60. static bool 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 false;
  66. if (!strstarts(line + id_len, " - "))
  67. return false;
  68. return true;
  69. }
  70. static bool empty_section(struct doc_section *d)
  71. {
  72. unsigned int i;
  73. for (i = 0; i < d->num_lines; i++)
  74. if (!is_blank(d->lines[i]))
  75. return false;
  76. return true;
  77. }
  78. static struct doc_section *new_section(struct list_head *list,
  79. const char *function,
  80. const char *type)
  81. {
  82. struct doc_section *d;
  83. char *lowertype;
  84. unsigned int i;
  85. /* If previous section was empty, delete it. */
  86. d = list_tail(list, struct doc_section, list);
  87. if (d && empty_section(d)) {
  88. list_del(&d->list);
  89. talloc_free(d);
  90. }
  91. d = talloc(list, struct doc_section);
  92. d->function = function;
  93. lowertype = talloc_size(d, strlen(type) + 1);
  94. /* Canonicalize type to lower case. */
  95. for (i = 0; i < strlen(type)+1; i++)
  96. lowertype[i] = tolower(type[i]);
  97. d->type = lowertype;
  98. d->lines = NULL;
  99. d->num_lines = 0;
  100. list_add_tail(list, &d->list);
  101. return d;
  102. }
  103. static void add_line(struct doc_section *curr, const char *line)
  104. {
  105. curr->lines = talloc_realloc(curr, curr->lines, char *,
  106. curr->num_lines+1);
  107. curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
  108. }
  109. struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
  110. {
  111. char **lines = grab_doc(rawlines, num);
  112. const char *function = NULL;
  113. struct doc_section *curr = NULL;
  114. unsigned int i;
  115. struct list_head *list;
  116. list = talloc(NULL, struct list_head);
  117. list_head_init(list);
  118. for (i = 0; lines[i]; i++) {
  119. if (is_summary_line(lines[i])) {
  120. function = talloc_strndup(list, lines[i],
  121. strcspn(lines[i], " "));
  122. curr = new_section(list, function, "summary");
  123. add_line(curr, strstr(lines[i], " - ") + 3);
  124. curr = new_section(list, function, "description");
  125. } else if (is_section(lines[i], false)) {
  126. char *type = talloc_strndup(curr, lines[i],
  127. strcspn(lines[i], ":"));
  128. curr = new_section(list, function, type);
  129. } else if (is_section(lines[i], true)) {
  130. unsigned int sectlen = strcspn(lines[i], ":");
  131. char *type = talloc_strndup(curr, lines[i], sectlen);
  132. curr = new_section(list, function, type);
  133. add_line(curr, lines[i] + sectlen + 1
  134. + strspn(lines[i] + sectlen + 1, " \t"));
  135. } else {
  136. if (!curr)
  137. continue;
  138. add_line(curr, lines[i]);
  139. }
  140. }
  141. talloc_free(lines);
  142. return list;
  143. }