tools: fix bug in doc-extract, handle spaces in summary line
[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 "doc_extract.h"
15 #include "tools.h"
16
17 static char **grab_doc(char **lines, unsigned int num)
18 {
19         char **ret;
20         unsigned int i;
21         bool printing = false;
22
23         ret = talloc_array(NULL, char *, num+1);
24
25         num = 0;
26         for (i = 0; lines[i]; i++) {
27                 if (streq(lines[i], "/**")) {
28                         printing = true;
29                         if (num != 0) {
30                                 ret[num-1] = talloc_append_string(ret[num-1],
31                                                                   "\n");
32                         }
33                 } else if (streq(lines[i], " */")) 
34                         printing = false;
35                 else if (printing) {
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);
40                         else
41                                 errx(1, "Malformed line %u", i);
42                 }
43         }
44         ret[num] = NULL;
45         return ret;
46 }
47
48 static bool is_blank(const char *line)
49 {
50         return line && line[strspn(line, " \t\n")] == '\0';
51 }
52
53 static bool is_section(const char *line, bool one_liner)
54 {
55         unsigned int len;
56
57         if (!isupper(line[0]))
58                 return false;
59         len = strspn(line, IDENT_CHARS" ");
60         if (line[len] != ':')
61                 return false;
62
63         /* If it can be a one-liner, a space is sufficient.*/
64         if (one_liner)
65                 return (line[len+1] == ' ' || line[len+1] == '\t');
66
67         return line[len] == ':' && is_blank(line+len+1);
68 }
69
70 /* Summary line is form '<identifier> - ' (spaces for 'struct foo -') */
71 static unsigned int is_summary_line(const char *line)
72 {
73         unsigned int id_len;
74
75         id_len = strspn(line, IDENT_CHARS" ");
76         if (id_len == 0)
77                 return 0;
78         if (!strstarts(line + id_len-1, " - "))
79                 return 0;
80
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 {
98         struct doc_section *d;
99         char *lowertype;
100         unsigned int i;
101
102         /* If previous section was empty, delete it. */
103         d = list_tail(list, struct doc_section, list);
104         if (d && empty_section(d)) {
105                 list_del(&d->list);
106                 talloc_free(d);
107         }
108
109         d = talloc(list, struct doc_section);
110         d->function = function;
111         lowertype = talloc_size(d, strlen(type) + 1);
112         /* Canonicalize type to lower case. */
113         for (i = 0; i < strlen(type)+1; i++)
114                 lowertype[i] = tolower(type[i]);
115         d->type = lowertype;
116         d->lines = NULL;
117         d->num_lines = 0;
118
119         list_add_tail(list, &d->list);
120         return d;
121 }
122
123 static void add_line(struct doc_section *curr, const char *line)
124 {
125         curr->lines = talloc_realloc(curr, curr->lines, char *,
126                                      curr->num_lines+1);
127         curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
128 }
129
130 struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
131 {
132         char **lines = grab_doc(rawlines, num);
133         const char *function = NULL;
134         struct doc_section *curr = NULL;
135         unsigned int i;
136         struct list_head *list;
137
138         list = talloc(NULL, struct list_head);
139         list_head_init(list);
140
141         for (i = 0; lines[i]; i++) {
142                 unsigned funclen;
143
144                 funclen = is_summary_line(lines[i]);
145                 if (funclen) {
146                         function = talloc_strndup(list, lines[i], funclen);
147                         curr = new_section(list, function, "summary");
148                         add_line(curr, lines[i] + funclen + 3);
149                         curr = new_section(list, function, "description");
150                 } else if (is_section(lines[i], false)) {
151                         char *type = talloc_strndup(curr, lines[i],
152                                                     strcspn(lines[i], ":"));
153                         curr = new_section(list, function, type);
154                 } else if (is_section(lines[i], true)) {
155                         unsigned int sectlen = strcspn(lines[i], ":");
156                         char *type = talloc_strndup(curr, lines[i], sectlen);
157                         curr = new_section(list, function, type);
158                         add_line(curr, lines[i] + sectlen + 1
159                                  + strspn(lines[i] + sectlen + 1, " \t"));
160                 } else {
161                         if (!curr)
162                                 continue;
163                         add_line(curr, lines[i]);
164                 }
165         }
166         talloc_free(lines);
167         return list;
168 }