tdb2: unify tdb1_transaction_start etc. into tdb_transaction_start.
[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
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')
174                         continue;
175                 if (prefix < trim)
176                         trim = prefix;
177         }
178
179         /* Now trim it. */
180         for (i = 0; i < curr->num_lines; i++) {
181                 unsigned int prefix = strspn(curr->lines[i], " ");
182                 if (prefix < trim)
183                         curr->lines[i] += prefix;
184                 else
185                         curr->lines[i] += trim;
186         }
187 }
188
189 struct list_head *extract_doc_sections(char **rawlines)
190 {
191         unsigned int *linemap;
192         char **lines = grab_doc(rawlines, &linemap);
193         const char *function = NULL;
194         struct doc_section *curr = NULL;
195         unsigned int i;
196         struct list_head *list;
197
198         list = talloc(NULL, struct list_head);
199         list_head_init(list);
200
201         for (i = 0; lines[i]; i++) {
202                 unsigned funclen;
203                 char *type, *extra;
204
205                 funclen = is_summary_line(lines[i]);
206                 if (funclen) {
207                         function = talloc_strndup(list, lines[i], funclen);
208                         curr = new_section(list, function, "summary",
209                                            linemap[i]);
210                         add_line(curr, lines[i] + funclen + 3);
211                         curr = new_section(list, function, "description",
212                                            linemap[i]);
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);
217                                 curr = NULL;
218                         }
219                 } else {
220                         if (curr)
221                                 add_detabbed_line(curr, rawlines[linemap[i]]);
222                 }
223         }
224
225         list_for_each(list, curr, list)
226                 trim_lines(curr);
227
228         talloc_free(lines);
229         return list;
230 }