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 num)
22 bool printing = false;
24 ret = talloc_array(NULL, char *, num+1);
27 for (i = 0; lines[i]; i++) {
28 if (streq(lines[i], "/**")) {
31 ret[num-1] = talloc_append_string(ret[num-1],
34 } else if (streq(lines[i], " */"))
37 if (strstarts(lines[i], " * "))
38 ret[num++] = talloc_strdup(ret, lines[i]+3);
39 else if (strstarts(lines[i], " *"))
40 ret[num++] = talloc_strdup(ret, lines[i]+2);
42 errx(1, "Malformed line %u", i);
49 static bool is_blank(const char *line)
51 return line && line[strspn(line, " \t\n")] == '\0';
54 static char *is_section(const void *ctx, const char *line, char **value)
58 /* Any number of upper case words separated by spaces, ending in : */
59 if (!strreg(ctx, line,
60 "^([A-Z][a-zA-Z0-9_]*( [A-Z][a-zA-Z0-9_]*)*):[ \t\n]*(.*)",
61 &secname, NULL, value))
67 /* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */
68 static unsigned int is_summary_line(const char *line)
72 id_len = strspn(line, IDENT_CHARS" ");
75 if (strspn(line, " ") == id_len)
77 if (!strstarts(line + id_len-1, " - "))
82 static bool empty_section(struct doc_section *d)
86 for (i = 0; i < d->num_lines; i++)
87 if (!is_blank(d->lines[i]))
92 static struct doc_section *new_section(struct list_head *list,
96 struct doc_section *d;
100 /* If previous section was empty, delete it. */
101 d = list_tail(list, struct doc_section, list);
102 if (d && empty_section(d)) {
107 d = talloc(list, struct doc_section);
108 d->function = function;
109 lowertype = talloc_size(d, strlen(type) + 1);
110 /* Canonicalize type to lower case. */
111 for (i = 0; i < strlen(type)+1; i++)
112 lowertype[i] = tolower(type[i]);
117 list_add_tail(list, &d->list);
121 static void add_line(struct doc_section *curr, const char *line)
123 curr->lines = talloc_realloc(curr, curr->lines, char *,
125 curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
128 struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
130 char **lines = grab_doc(rawlines, num);
131 const char *function = NULL;
132 struct doc_section *curr = NULL;
134 struct list_head *list;
136 list = talloc(NULL, struct list_head);
137 list_head_init(list);
139 for (i = 0; lines[i]; i++) {
143 funclen = is_summary_line(lines[i]);
145 function = talloc_strndup(list, lines[i], funclen);
146 curr = new_section(list, function, "summary");
147 add_line(curr, lines[i] + funclen + 3);
148 curr = new_section(list, function, "description");
149 } else if ((type = is_section(list, lines[i], &extra)) != NULL){
150 curr = new_section(list, function, type);
151 if (!streq(extra, "")) {
152 add_line(curr, extra);
157 add_line(curr, lines[i]);