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 ret[num-1] = talloc_append_string(ret[num-1],
33 } else if (streq(lines[i], " */"))
36 if (strstarts(lines[i], " * "))
37 ret[num++] = talloc_strdup(ret, lines[i]+3);
38 else if (strstarts(lines[i], " *"))
39 ret[num++] = talloc_strdup(ret, lines[i]+2);
41 errx(1, "Malformed line %u", i);
48 static bool is_blank(const char *line)
50 return line && line[strspn(line, " \t\n")] == '\0';
53 static bool is_section(const char *line, bool one_liner)
57 /* Any number of upper case words separated by spaces, ending in : */
59 if (!isupper(line[len]))
61 len += strspn(line+len, IDENT_CHARS);
70 /* If it can be a one-liner, a space is sufficient.*/
72 return (line[len+1] == ' ' || line[len+1] == '\t');
74 return line[len] == ':' && is_blank(line+len+1);
77 /* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */
78 static unsigned int is_summary_line(const char *line)
82 id_len = strspn(line, IDENT_CHARS" ");
85 if (!strstarts(line + id_len-1, " - "))
91 static bool empty_section(struct doc_section *d)
95 for (i = 0; i < d->num_lines; i++)
96 if (!is_blank(d->lines[i]))
101 static struct doc_section *new_section(struct list_head *list,
102 const char *function,
105 struct doc_section *d;
109 /* If previous section was empty, delete it. */
110 d = list_tail(list, struct doc_section, list);
111 if (d && empty_section(d)) {
116 d = talloc(list, struct doc_section);
117 d->function = function;
118 lowertype = talloc_size(d, strlen(type) + 1);
119 /* Canonicalize type to lower case. */
120 for (i = 0; i < strlen(type)+1; i++)
121 lowertype[i] = tolower(type[i]);
126 list_add_tail(list, &d->list);
130 static void add_line(struct doc_section *curr, const char *line)
132 curr->lines = talloc_realloc(curr, curr->lines, char *,
134 curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
137 struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
139 char **lines = grab_doc(rawlines, num);
140 const char *function = NULL;
141 struct doc_section *curr = NULL;
143 struct list_head *list;
145 list = talloc(NULL, struct list_head);
146 list_head_init(list);
148 for (i = 0; lines[i]; i++) {
151 funclen = is_summary_line(lines[i]);
153 function = talloc_strndup(list, lines[i], funclen);
154 curr = new_section(list, function, "summary");
155 add_line(curr, lines[i] + funclen + 3);
156 curr = new_section(list, function, "description");
157 } else if (is_section(lines[i], false)) {
158 char *type = talloc_strndup(curr, lines[i],
159 strcspn(lines[i], ":"));
160 curr = new_section(list, function, type);
161 } else if (is_section(lines[i], true)) {
162 unsigned int sectlen = strcspn(lines[i], ":");
163 char *type = talloc_strndup(curr, lines[i], sectlen);
164 curr = new_section(list, function, type);
165 add_line(curr, lines[i] + sectlen + 1
166 + strspn(lines[i] + sectlen + 1, " \t"));
170 add_line(curr, lines[i]);