1 /* This merely extracts, doesn't do XML or anything. */
12 #include <ccan/talloc/talloc.h>
13 #include <ccan/str/str.h>
14 #include "doc_extract.h"
17 static char **grab_doc(char **lines, unsigned int num)
21 bool printing = false;
23 ret = talloc_array(NULL, char *, num+1);
26 for (i = 0; lines[i]; i++) {
27 if (streq(lines[i], "/**")) {
30 talloc_append_string(ret[num-1], "\n");
31 } else if (streq(lines[i], " */"))
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);
39 errx(1, "Malformed line %u", i);
46 static bool is_blank(const char *line)
48 return line && line[strspn(line, " \t\n")] == '\0';
51 static bool is_section(const char *line, bool one_liner)
55 if (!isupper(line[0]))
57 len = strspn(line, IDENT_CHARS);
61 /* If it can be a one-liner, a space is sufficient.*/
63 return (line[len+1] == ' ' || line[len+1] == '\t');
65 return line[len] == ':' && is_blank(line+len+1);
68 /* Summary line is form '<identifier> - ' */
69 static bool is_summary_line(const char *line)
73 id_len = strspn(line, IDENT_CHARS);
76 if (!strstarts(line + id_len, " - "))
82 static struct doc_section *new_section(struct list_head *list,
86 struct doc_section *d = talloc(list, struct doc_section);
87 d->function = function;
91 list_add_tail(list, &d->list);
95 static void add_line(struct doc_section *curr, const char *line)
97 curr->lines = talloc_realloc(curr, curr->lines, char *,
99 curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
102 struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
104 char **lines = grab_doc(rawlines, num);
105 const char *function = NULL;
106 struct doc_section *curr = NULL;
108 struct list_head *list;
110 list = talloc(NULL, struct list_head);
111 list_head_init(list);
113 for (i = 0; lines[i]; i++) {
114 if (is_summary_line(lines[i])) {
115 function = talloc_strndup(list, lines[i],
116 strcspn(lines[i], " "));
117 curr = new_section(list, function, "summary");
118 add_line(curr, strstr(lines[i], " - ") + 3);
119 curr = new_section(list, function, "description");
120 } else if (is_section(lines[i], false)) {
121 char *type = talloc_strndup(curr, lines[i],
122 strcspn(lines[i], ":"));
123 curr = new_section(list, function, type);
124 } else if (is_section(lines[i], true)) {
125 unsigned int sectlen = strcspn(lines[i], ":");
126 char *type = talloc_strndup(curr, lines[i], sectlen);
127 curr = new_section(list, function, type);
128 add_line(curr, lines[i] + sectlen + 1
129 + strspn(lines[i] + sectlen + 1, " \t"));
133 add_line(curr, lines[i]);