Change link naming to vastly prefer -with-deps download.
[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|licence|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                 unsigned int num;
31                 struct list_head *list;
32                 struct doc_section *d;
33
34                 file = grab_file(NULL, argv[i], NULL);
35                 if (!file)
36                         err(1, "Reading file %s", argv[i]);
37                 lines = strsplit(file, file, "\n", &num);
38
39                 list = extract_doc_sections(lines, num);
40                 if (list_empty(list))
41                         errx(1, "No documentation in file %s", argv[i]);
42                 talloc_free(file);
43
44                 if (streq(type, "functions")) {
45                         const char *last = NULL;
46                         list_for_each(list, d, list) {
47                                 if (d->function) {
48                                         if (!last || !streq(d->function, last))
49                                                 printf("%s\n", d->function);
50                                         last = d->function;
51                                 }
52                         }
53                 } else {
54                         unsigned int j;
55                         list_for_each(list, d, list) {
56                                 if (function) {
57                                         if (!d->function)
58                                                 continue;
59                                         if (!streq(d->function, function))
60                                                 continue;
61                                 }
62                                 if (streq(type, "all"))
63                                         printf("%s:\n", d->type);
64                                 else if (!streq(d->type, type))
65                                         continue;
66
67                                 for (j = 0; j < d->num_lines; j++)
68                                         printf("%s\n", d->lines[j]);
69                         }
70                 }
71                 talloc_free(list);
72         }
73         return 0;
74 }