]> git.ozlabs.org Git - ccan/blob - tools/doc_extract-core.c
Small fix to read_cstring for reading \? .
[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                                 talloc_append_string(ret[num-1], "\n");
31                 } else if (streq(lines[i], " */")) 
32                         printing = false;
33                 else if (printing) {
34                         if (strstarts(lines[i], " * "))
35                                 ret[num++] = talloc_strdup(ret, lines[i]+3);
36                         else if (strstarts(lines[i], " *"))
37                                 ret[num++] = talloc_strdup(ret, lines[i]+2);
38                         else
39                                 errx(1, "Malformed line %u", i);
40                 }
41         }
42         ret[num] = NULL;
43         return ret;
44 }
45
46 static bool is_blank(const char *line)
47 {
48         return line && line[strspn(line, " \t\n")] == '\0';
49 }
50
51 static bool is_section(const char *line, bool one_liner)
52 {
53         unsigned int len;
54
55         if (!isupper(line[0]))
56                 return false;
57         len = strspn(line, IDENT_CHARS);
58         if (line[len] != ':')
59                 return false;
60
61         /* If it can be a one-liner, a space is sufficient.*/
62         if (one_liner)
63                 return (line[len+1] == ' ' || line[len+1] == '\t');
64
65         return line[len] == ':' && is_blank(line+len+1);
66 }
67
68 /* Summary line is form '<identifier> - ' */
69 static bool is_summary_line(const char *line)
70 {
71         unsigned int id_len;
72
73         id_len = strspn(line, IDENT_CHARS);
74         if (id_len == 0)
75                 return false;
76         if (!strstarts(line + id_len, " - "))
77                 return false;
78
79         return true;
80 }
81
82 static bool empty_section(struct doc_section *d)
83 {
84         unsigned int i;
85
86         for (i = 0; i < d->num_lines; i++)
87                 if (!is_blank(d->lines[i]))
88                         return false;
89         return true;
90 }
91
92 static struct doc_section *new_section(struct list_head *list,
93                                        const char *function,
94                                        const char *type)
95 {
96         struct doc_section *d;
97         char *lowertype;
98         unsigned int i;
99
100         /* If previous section was empty, delete it. */
101         d = list_tail(list, struct doc_section, list);
102         if (d && empty_section(d)) {
103                 list_del(&d->list);
104                 talloc_free(d);
105         }
106
107         d = talloc(list, struct doc_section);
108         d->function = function;
109         lowertype = talloc_size(d, strlen(type) + 1);
110         /* Canonicalize type to lower case. */
111         for (i = 0; i < strlen(type)+1; i++)
112                 lowertype[i] = tolower(type[i]);
113         d->type = lowertype;
114         d->lines = NULL;
115         d->num_lines = 0;
116
117         list_add_tail(list, &d->list);
118         return d;
119 }
120
121 static void add_line(struct doc_section *curr, const char *line)
122 {
123         curr->lines = talloc_realloc(curr, curr->lines, char *,
124                                      curr->num_lines+1);
125         curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line);
126 }
127
128 struct list_head *extract_doc_sections(char **rawlines, unsigned int num)
129 {
130         char **lines = grab_doc(rawlines, num);
131         const char *function = NULL;
132         struct doc_section *curr = NULL;
133         unsigned int i;
134         struct list_head *list;
135
136         list = talloc(NULL, struct list_head);
137         list_head_init(list);
138
139         for (i = 0; lines[i]; i++) {
140                 if (is_summary_line(lines[i])) {
141                         function = talloc_strndup(list, lines[i],
142                                                   strcspn(lines[i], " "));
143                         curr = new_section(list, function, "summary");
144                         add_line(curr, strstr(lines[i], " - ") + 3);
145                         curr = new_section(list, function, "description");
146                 } else if (is_section(lines[i], false)) {
147                         char *type = talloc_strndup(curr, lines[i],
148                                                     strcspn(lines[i], ":"));
149                         curr = new_section(list, function, type);
150                 } else if (is_section(lines[i], true)) {
151                         unsigned int sectlen = strcspn(lines[i], ":");
152                         char *type = talloc_strndup(curr, lines[i], sectlen);
153                         curr = new_section(list, function, type);
154                         add_line(curr, lines[i] + sectlen + 1
155                                  + strspn(lines[i] + sectlen + 1, " \t"));
156                 } else {
157                         if (!curr)
158                                 continue;
159                         add_line(curr, lines[i]);
160                 }
161         }
162         talloc_free(lines);
163         return list;
164 }