tools/doc_extract: trim trailing blank lines on sections.
[ccan] / tools / doc_extract-core.c
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>
5 #include <err.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <string.h>
10 #include <sys/types.h>
11 #include <sys/stat.h>
12 #include <fcntl.h>
13 #include <stdbool.h>
14 #include <ctype.h>
15 #include "doc_extract.h"
16 #include "tools.h"
17
18 static char **grab_doc(char **lines, unsigned int **linemap)
19 {
20         char **ret;
21         unsigned int i, num;
22         bool printing = false;
23
24         ret = talloc_array(NULL, char *, talloc_array_length(lines));
25         *linemap = talloc_array(ret, unsigned int, talloc_array_length(lines));
26
27         num = 0;
28         for (i = 0; lines[i]; i++) {
29                 if (streq(lines[i], "/**")) {
30                         printing = true;
31                         if (num != 0) {
32                                 ret[num-1] = talloc_append_string(ret[num-1],
33                                                                   "\n");
34                         }
35                 } else if (streq(lines[i], " */")) 
36                         printing = false;
37                 else if (printing) {
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);
42                         else
43                                 errx(1, "Malformed line %u", i);
44                         (*linemap)[num-1] = i;
45                 }
46         }
47         ret[num] = NULL;
48         return ret;
49 }
50
51 static bool is_blank(const char *line)
52 {
53         return line && line[strspn(line, " \t\n")] == '\0';
54 }
55
56 static char *is_section(const void *ctx, const char *line, char **value)
57 {
58         char *secname;
59
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))
64                 return NULL;
65
66         return secname;
67 }
68
69 /* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */
70 static unsigned int is_summary_line(const char *line)
71 {
72         unsigned int id_len;
73
74         id_len = strspn(line, IDENT_CHARS" ");
75         if (id_len == 0)
76                 return 0;
77         if (strspn(line, " ") == id_len)
78                 return 0;
79         if (!strstarts(line + id_len-1, " - "))
80                 return 0;
81         return id_len - 1;
82 }
83
84 static bool empty_section(struct doc_section *d)
85 {
86         unsigned int i;
87
88         for (i = 0; i < d->num_lines; i++)
89                 if (!is_blank(d->lines[i]))
90                         return false;
91         return true;
92 }
93
94 static struct doc_section *new_section(struct list_head *list,
95                                        const char *function,
96                                        const char *type,
97                                        unsigned int srcline)
98 {
99         struct doc_section *d;
100         char *lowertype;
101         unsigned int i;
102
103         /* If previous section was empty, delete it. */
104         d = list_tail(list, struct doc_section, list);
105         if (d && empty_section(d)) {
106                 list_del(&d->list);
107                 talloc_free(d);
108         }
109
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]);
116         d->type = lowertype;
117         d->lines = NULL;
118         d->num_lines = 0;
119         d->srcline = srcline;
120
121         list_add_tail(list, &d->list);
122         return d;
123 }
124
125 static void add_line(struct doc_section *curr, const char *line)
126 {
127         curr->lines = talloc_realloc(curr, curr->lines, char *,
128                                      curr->num_lines+1);
129         curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
130 }
131
132 /* We convert tabs to spaces here. */
133 static void add_detabbed_line(struct doc_section *curr, const char *rawline)
134 {
135         unsigned int i, eff_i, len, off = 0;
136         char *line;
137
138         /* Worst-case alloc: 8 spaces per tab. */
139         line = talloc_array(curr, char, strlen(rawline) +
140                             strcount(rawline, "\t") * 7 + 1);
141         len = 0;
142
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') {
146                         do {
147                                 line[len++] = ' ';
148                                 eff_i++;
149                         } while (eff_i % 8 != 0);
150                 } else {
151                         line[len++] = rawline[i];
152                         if (off == 0 && rawline[i] == '*')
153                                 off = i + 1;
154                         eff_i++;
155                 }
156         }
157         line[len] = '\0';
158
159         add_line(curr, line + off);
160         talloc_free(line);
161 }
162
163 /* Not very efficient: we could track prefix length while doing
164  * add_detabbed_line */
165 static void trim_lines(struct doc_section *curr)
166 {
167         unsigned int i, trim = -1;
168         int last_non_empty = -1;
169
170         /* Get minimum whitespace prefix. */
171         for (i = 0; i < curr->num_lines; i++) {
172                 unsigned int prefix = strspn(curr->lines[i], " ");
173                 /* Ignore blank lines */
174                 if (curr->lines[i][prefix] == '\0')
175                         continue;
176                 if (prefix < trim)
177                         trim = prefix;
178         }
179
180         /* Now trim it. */
181         for (i = 0; i < curr->num_lines; i++) {
182                 unsigned int prefix = strspn(curr->lines[i], " ");
183                 if (prefix < trim)
184                         curr->lines[i] += prefix;
185                 else
186                         curr->lines[i] += trim;
187
188                 /* All blank?  Potential to trim. */
189                 if (curr->lines[i][strspn(curr->lines[i], " \t")] != '\0')
190                         last_non_empty = i;
191         }
192
193         /* Remove trailing blank lines. */
194         curr->num_lines = last_non_empty + 1;
195 }
196
197 struct list_head *extract_doc_sections(char **rawlines)
198 {
199         unsigned int *linemap;
200         char **lines = grab_doc(rawlines, &linemap);
201         const char *function = NULL;
202         struct doc_section *curr = NULL;
203         unsigned int i;
204         struct list_head *list;
205
206         list = talloc(NULL, struct list_head);
207         list_head_init(list);
208
209         for (i = 0; lines[i]; i++) {
210                 unsigned funclen;
211                 char *type, *extra;
212
213                 funclen = is_summary_line(lines[i]);
214                 if (funclen) {
215                         function = talloc_strndup(list, lines[i], funclen);
216                         curr = new_section(list, function, "summary",
217                                            linemap[i]);
218                         add_line(curr, lines[i] + funclen + 3);
219                         curr = new_section(list, function, "description",
220                                            linemap[i]);
221                 } else if ((type = is_section(list, lines[i], &extra)) != NULL){
222                         curr = new_section(list, function, type, linemap[i]);
223                         if (!streq(extra, "")) {
224                                 add_line(curr, extra);
225                                 curr = NULL;
226                         }
227                 } else {
228                         if (curr)
229                                 add_detabbed_line(curr, rawlines[linemap[i]]);
230                 }
231         }
232
233         list_for_each(list, curr, list)
234                 trim_lines(curr);
235
236         talloc_free(lines);
237         return list;
238 }