ccanlint: fix spurious warning errors.
[ccan] / tools / doc_extract.c
1 /* This merely extracts, doesn't do XML or anything. */
2 #include <err.h>
3 #include <string.h>
4 #include <stdio.h>
5 #include <ccan/str/str.h>
6 #include <ccan/str_talloc/str_talloc.h>
7 #include <ccan/talloc/talloc.h>
8 #include <ccan/grab_file/grab_file.h>
9 #include "doc_extract.h"
10
11 int main(int argc, char *argv[])
12 {
13         unsigned int i;
14         const char *type;
15         const char *function = NULL;
16
17         if (argc < 3)
18                 errx(1, "Usage: doc_extract [--function=<funcname>] TYPE <file>...\n"
19                      "Where TYPE is functions|author|license|maintainer|summary|description|example|all");
20
21         if (strstarts(argv[1], "--function=")) {
22                 function = argv[1] + strlen("--function=");
23                 argv++;
24                 argc--;
25         }
26
27         type = argv[1];
28         for (i = 2; i < argc; i++) {
29                 char *file, **lines;
30                 struct list_head *list;
31                 struct doc_section *d;
32
33                 file = grab_file(NULL, argv[i], NULL);
34                 if (!file)
35                         err(1, "Reading file %s", argv[i]);
36                 lines = strsplit(file, file, "\n");
37
38                 list = extract_doc_sections(lines);
39                 if (list_empty(list))
40                         errx(1, "No documentation in file %s", argv[i]);
41                 talloc_free(file);
42
43                 if (streq(type, "functions")) {
44                         const char *last = NULL;
45                         list_for_each(list, d, list) {
46                                 if (d->function) {
47                                         if (!last || !streq(d->function, last))
48                                                 printf("%s\n", d->function);
49                                         last = d->function;
50                                 }
51                         }
52                 } else {
53                         unsigned int j;
54                         list_for_each(list, d, list) {
55                                 if (function) {
56                                         if (!d->function)
57                                                 continue;
58                                         if (!streq(d->function, function))
59                                                 continue;
60                                 }
61                                 if (streq(type, "all"))
62                                         printf("%s:\n", d->type);
63                                 else if (!streq(d->type, type))
64                                         continue;
65
66                                 for (j = 0; j < d->num_lines; j++)
67                                         printf("%s\n", d->lines[j]);
68                         }
69                 }
70                 talloc_free(list);
71         }
72         return 0;
73 }