1 /* This merely extracts, doesn't do XML or anything. */
2 #include <ccan/talloc/talloc.h>
3 #include <ccan/str/str.h>
4 #include <ccan/str_talloc/str_talloc.h>
10 #include <sys/types.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 /* We convert tabs to spaces here. */
133 static void add_detabbed_line(struct doc_section *curr, const char *rawline)
135 unsigned int i, eff_i, len, off = 0;
138 /* Worst-case alloc: 8 spaces per tab. */
139 line = talloc_array(curr, char, strlen(rawline) +
140 strcount(rawline, "\t") * 7 + 1);
143 /* We keep track of the *effective* offset of i. */
144 for (i = eff_i = 0; i < strlen(rawline); i++) {
145 if (rawline[i] == '\t') {
149 } while (eff_i % 8 != 0);
151 line[len++] = rawline[i];
152 if (off == 0 && rawline[i] == '*')
159 add_line(curr, line + off);
163 /* Not very efficient: we could track prefix length while doing
164 * add_detabbed_line */
165 static void trim_lines(struct doc_section *curr)
167 unsigned int i, trim = -1;
169 /* Get minimum whitespace prefix. */
170 for (i = 0; i < curr->num_lines; i++) {
171 unsigned int prefix = strspn(curr->lines[i], " ");
172 /* Ignore blank lines */
173 if (curr->lines[i][prefix] == '\0')
180 for (i = 0; i < curr->num_lines; i++) {
181 unsigned int prefix = strspn(curr->lines[i], " ");
183 curr->lines[i] += prefix;
185 curr->lines[i] += trim;
189 struct list_head *extract_doc_sections(char **rawlines)
191 unsigned int *linemap;
192 char **lines = grab_doc(rawlines, &linemap);
193 const char *function = NULL;
194 struct doc_section *curr = NULL;
196 struct list_head *list;
198 list = talloc(NULL, struct list_head);
199 list_head_init(list);
201 for (i = 0; lines[i]; i++) {
205 funclen = is_summary_line(lines[i]);
207 function = talloc_strndup(list, lines[i], funclen);
208 curr = new_section(list, function, "summary",
210 add_line(curr, lines[i] + funclen + 3);
211 curr = new_section(list, function, "description",
213 } else if ((type = is_section(list, lines[i], &extra)) != NULL){
214 curr = new_section(list, function, type, linemap[i]);
215 if (!streq(extra, "")) {
216 add_line(curr, extra);
221 add_detabbed_line(curr, rawlines[linemap[i]]);
225 list_for_each(list, curr, list)