1 /* This merely extracts, doesn't do XML or anything. */
11 #include "talloc/talloc.h"
12 #include "string/string.h"
14 static char **grab_doc(const char *fname)
19 bool printing = false, printed = false;
21 file = grab_file(NULL, fname, NULL);
23 err(1, "Reading file %s", fname);
24 lines = strsplit(file, file, "\n", &num);
25 ret = talloc_array(NULL, char *, num+1);
28 for (i = 0; lines[i]; i++) {
29 if (streq(lines[i], "/**")) {
32 talloc_append_string(ret[num], "\n");
33 } else if (streq(lines[i], " */"))
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);
41 errx(1, "Malformed line %s:%u", fname, i);
49 static bool is_blank(const char *line)
51 return line && line[strspn(line, " \t\n")] == '\0';
54 static bool is_section(const char *line)
58 len = strcspn(line, " \t\n:");
62 return line[len] == ':' && is_blank(line+len+1);
66 static bool end_section(const char *line)
68 return !line || is_section(line);
71 static unsigned int find_section(char **lines, const char *name)
75 for (i = 0; lines[i]; i++) {
76 if (!is_section(lines[i]))
78 if (strncasecmp(lines[i], name, strlen(name)) != 0)
80 if (lines[i][strlen(name)] == ':')
86 int main(int argc, char *argv[])
92 errx(1, "Usage: doc_extract TYPE <file>...\n"
93 "Where TYPE is author|licence|maintainer|summary|description|example|all");
96 for (i = 2; i < argc; i++) {
98 char **lines = grab_doc(argv[i]);
101 errx(1, "No documentation in file");
103 /* Simple one-line fields. */
104 if (streq(type, "author")
105 || streq(type, "maintainer")
106 || streq(type, "licence")) {
107 line = find_section(lines, type);
110 errx(1, "Malformed %s, end of file",
114 } else if (streq(type, "summary")) {
115 /* Summary comes after - on first line. */
118 dash = strchr(lines[0], '-');
120 errx(1, "Malformed first line: no -");
121 dash += strspn(dash, "- ");
123 } else if (streq(type, "description")) {
125 while (is_blank(lines[line]))
128 while (!end_section(lines[line]))
130 } else if (streq(type, "example")) {
131 line = find_section(lines, type);
136 while (is_blank(lines[line]))
139 /* Examples can be indented. Take cue
140 * from first non-blank line. */
142 strip = strspn(lines[line], " \t");
144 while (!end_section(lines[line])) {
145 if (strspn(lines[line], " \t") >= strip)
146 puts(lines[line] + strip);
152 } else if (streq(type, "all")) {
153 for (line = 0; lines[line]; line++)
156 errx(1, "Unknown type '%s'", type);