ccanlint: -k should not pollute module directory.
[ccan] / tools / doc_extract-core.c
1 /* This merely extracts, doesn't do XML or anything. */
2 #include <err.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <stdbool.h>
11 #include <ctype.h>
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"
16 #include "tools.h"
17
18 static char **grab_doc(char **lines, unsigned int num, unsigned int **linemap)
19 {
20         char **ret;
21         unsigned int i;
22         bool printing = false;
23
24         ret = talloc_array(NULL, char *, num+1);
25         *linemap = talloc_array(ret, unsigned int, num);
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 struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
133 {
134         unsigned int *linemap;
135         char **lines = grab_doc(rawlines, num, &linemap);
136         const char *function = NULL;
137         struct doc_section *curr = NULL;
138         unsigned int i;
139         struct list_head *list;
140
141         list = talloc(NULL, struct list_head);
142         list_head_init(list);
143
144         for (i = 0; lines[i]; i++) {
145                 unsigned funclen;
146                 char *type, *extra;
147
148                 funclen = is_summary_line(lines[i]);
149                 if (funclen) {
150                         function = talloc_strndup(list, lines[i], funclen);
151                         curr = new_section(list, function, "summary",
152                                            linemap[i]);
153                         add_line(curr, lines[i] + funclen + 3);
154                         curr = new_section(list, function, "description",
155                                            linemap[i]);
156                 } else if ((type = is_section(list, lines[i], &extra)) != NULL){
157                         curr = new_section(list, function, type, linemap[i]);
158                         if (!streq(extra, "")) {
159                                 add_line(curr, extra);
160                                 curr = NULL;
161                         }
162                 } else {
163                         if (curr)
164                                 add_line(curr, lines[i]);
165                 }
166         }
167         talloc_free(lines);
168         return list;
169 }