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 /* We allow /, because it can be in (nested) module names. */
75 id_len = strspn(line, IDENT_CHARS" /");
78 if (strspn(line, " ") == id_len)
80 if (!strstarts(line + id_len-1, " - "))
85 static bool empty_section(struct doc_section *d)
89 for (i = 0; i < d->num_lines; i++)
90 if (!is_blank(d->lines[i]))
95 static struct doc_section *new_section(struct list_head *list,
100 struct doc_section *d;
104 /* If previous section was empty, delete it. */
105 d = list_tail(list, struct doc_section, list);
106 if (d && empty_section(d)) {
111 d = talloc(list, struct doc_section);
112 d->function = function;
113 lowertype = talloc_size(d, strlen(type) + 1);
114 /* Canonicalize type to lower case. */
115 for (i = 0; i < strlen(type)+1; i++)
116 lowertype[i] = tolower(type[i]);
120 d->srcline = srcline;
122 list_add_tail(list, &d->list);
126 static void add_line(struct doc_section *curr, const char *line)
128 curr->lines = talloc_realloc(curr, curr->lines, char *,
130 curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
133 /* We convert tabs to spaces here. */
134 static void add_detabbed_line(struct doc_section *curr, const char *rawline)
136 unsigned int i, eff_i, len, off = 0;
139 /* Worst-case alloc: 8 spaces per tab. */
140 line = talloc_array(curr, char, strlen(rawline) +
141 strcount(rawline, "\t") * 7 + 1);
144 /* We keep track of the *effective* offset of i. */
145 for (i = eff_i = 0; i < strlen(rawline); i++) {
146 if (rawline[i] == '\t') {
150 } while (eff_i % 8 != 0);
152 line[len++] = rawline[i];
153 if (off == 0 && rawline[i] == '*')
160 add_line(curr, line + off);
164 /* Not very efficient: we could track prefix length while doing
165 * add_detabbed_line */
166 static void trim_lines(struct doc_section *curr)
168 unsigned int i, trim = -1;
169 int last_non_empty = -1;
171 /* Get minimum whitespace prefix. */
172 for (i = 0; i < curr->num_lines; i++) {
173 unsigned int prefix = strspn(curr->lines[i], " ");
174 /* Ignore blank lines */
175 if (curr->lines[i][prefix] == '\0')
182 for (i = 0; i < curr->num_lines; i++) {
183 unsigned int prefix = strspn(curr->lines[i], " ");
185 curr->lines[i] += prefix;
187 curr->lines[i] += trim;
189 /* All blank? Potential to trim. */
190 if (curr->lines[i][strspn(curr->lines[i], " \t")] != '\0')
194 /* Remove trailing blank lines. */
195 curr->num_lines = last_non_empty + 1;
198 struct list_head *extract_doc_sections(char **rawlines)
200 unsigned int *linemap;
201 char **lines = grab_doc(rawlines, &linemap);
202 const char *function = NULL;
203 struct doc_section *curr = NULL;
205 struct list_head *list;
207 list = talloc(NULL, struct list_head);
208 list_head_init(list);
210 for (i = 0; lines[i]; i++) {
214 funclen = is_summary_line(lines[i]);
216 function = talloc_strndup(list, lines[i], funclen);
217 curr = new_section(list, function, "summary",
219 add_line(curr, lines[i] + funclen + 3);
220 curr = new_section(list, function, "description",
222 } else if ((type = is_section(list, lines[i], &extra)) != NULL){
223 curr = new_section(list, function, type, linemap[i]);
224 if (!streq(extra, "")) {
225 add_line(curr, extra);
230 add_detabbed_line(curr, rawlines[linemap[i]]);
234 list_for_each(list, curr, list)