1 /* This merely extracts, doesn't do XML or anything. */
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"
18 static char **grab_doc(char **lines, unsigned int **linemap)
22 bool printing = false;
24 ret = talloc_array(NULL, char *, talloc_array_length(lines));
25 *linemap = talloc_array(ret, unsigned int, talloc_array_length(lines));
28 for (i = 0; lines[i]; i++) {
29 if (streq(lines[i], "/**")) {
32 ret[num-1] = talloc_append_string(ret[num-1],
35 } else if (streq(lines[i], " */"))
38 if (strstarts(lines[i], " * "))
39 ret[num++] = talloc_strdup(ret, lines[i]+3);
40 else if (strstarts(lines[i], " *"))
41 ret[num++] = talloc_strdup(ret, lines[i]+2);
43 errx(1, "Malformed line %u", i);
44 (*linemap)[num-1] = i;
51 static bool is_blank(const char *line)
53 return line && line[strspn(line, " \t\n")] == '\0';
56 static char *is_section(const void *ctx, const char *line, char **value)
60 /* Any number of upper case words separated by spaces, ending in : */
61 if (!strreg(ctx, line,
62 "^([A-Z][a-zA-Z0-9_]*( [A-Z][a-zA-Z0-9_]*)*):[ \t\n]*(.*)",
63 &secname, NULL, value))
69 /* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */
70 static unsigned int is_summary_line(const char *line)
74 id_len = strspn(line, IDENT_CHARS" ");
77 if (strspn(line, " ") == id_len)
79 if (!strstarts(line + id_len-1, " - "))
84 static bool empty_section(struct doc_section *d)
88 for (i = 0; i < d->num_lines; i++)
89 if (!is_blank(d->lines[i]))
94 static struct doc_section *new_section(struct list_head *list,
99 struct doc_section *d;
103 /* If previous section was empty, delete it. */
104 d = list_tail(list, struct doc_section, list);
105 if (d && empty_section(d)) {
110 d = talloc(list, struct doc_section);
111 d->function = function;
112 lowertype = talloc_size(d, strlen(type) + 1);
113 /* Canonicalize type to lower case. */
114 for (i = 0; i < strlen(type)+1; i++)
115 lowertype[i] = tolower(type[i]);
119 d->srcline = srcline;
121 list_add_tail(list, &d->list);
125 static void add_line(struct doc_section *curr, const char *line)
127 curr->lines = talloc_realloc(curr, curr->lines, char *,
129 curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
132 struct list_head *extract_doc_sections(char **rawlines)
134 unsigned int *linemap;
135 char **lines = grab_doc(rawlines, &linemap);
136 const char *function = NULL;
137 struct doc_section *curr = NULL;
139 struct list_head *list;
141 list = talloc(NULL, struct list_head);
142 list_head_init(list);
144 for (i = 0; lines[i]; i++) {
148 funclen = is_summary_line(lines[i]);
150 function = talloc_strndup(list, lines[i], funclen);
151 curr = new_section(list, function, "summary",
153 add_line(curr, lines[i] + funclen + 3);
154 curr = new_section(list, function, "description",
156 } else if ((type = is_section(list, lines[i], &extra)) != NULL){
157 curr = new_section(list, function, type, linemap[i]);
158 if (!streq(extra, "")) {
159 add_line(curr, extra);
164 add_line(curr, lines[i]);