doc_extract-core.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 struct doc_section *new_section(struct list_head *list,
  71. const char *function,
  72. const char *type)
  73. {
  74. struct doc_section *d = talloc(list, struct doc_section);
  75. d->function = function;
  76. d->type = type;
  77. d->lines = NULL;
  78. d->num_lines = 0;
  79. list_add_tail(list, &d->list);
  80. return d;
  81. }
  82. static void add_line(struct doc_section *curr, const char *line)
  83. {
  84. curr->lines = talloc_realloc(curr, curr->lines, char *,
  85. curr->num_lines+1);
  86. curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
  87. }
  88. struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
  89. {
  90. char **lines = grab_doc(rawlines, num);
  91. const char *function = NULL;
  92. struct doc_section *curr = NULL;
  93. unsigned int i;
  94. struct list_head *list;
  95. list = talloc(NULL, struct list_head);
  96. list_head_init(list);
  97. for (i = 0; lines[i]; i++) {
  98. if (is_summary_line(lines[i])) {
  99. function = talloc_strndup(list, lines[i],
  100. strcspn(lines[i], " "));
  101. curr = new_section(list, function, "summary");
  102. add_line(curr, strstr(lines[i], " - ") + 3);
  103. curr = new_section(list, function, "description");
  104. } else if (is_section(lines[i], false)) {
  105. char *type = talloc_strndup(curr, lines[i],
  106. strcspn(lines[i], ":"));
  107. curr = new_section(list, function, type);
  108. } else if (is_section(lines[i], true)) {
  109. unsigned int sectlen = strcspn(lines[i], ":");
  110. char *type = talloc_strndup(curr, lines[i], sectlen);
  111. curr = new_section(list, function, type);
  112. add_line(curr, lines[i] + sectlen + 1
  113. + strspn(lines[i] + sectlen + 1, " \t"));
  114. } else {
  115. if (!curr)
  116. continue;
  117. add_line(curr, lines[i]);
  118. }
  119. }
  120. talloc_free(lines);
  121. return list;
  122. }