]> git.ozlabs.org Git - ccan/blob - tools/doc_extract.c
99c3202ca0bf2ccd1034b113a02cc71a6a27fe32
[ccan] / tools / doc_extract.c
1 /* This merely extracts, doesn't do XML or anything. */
2 #include <ccan/str/str.h>
3 #include <ccan/err/err.h>
4 #include <ccan/tal/grab_file/grab_file.h>
5 #include "tools.h"
6 #include <string.h>
7 #include <stdio.h>
8 #include "doc_extract.h"
9
10 /* We regard non-alphanumerics as equiv. */
11 static bool typematch(const char *a, const char *b)
12 {
13         size_t i;
14
15         for (i = 0; a[i]; i++) {
16                 if (cisalnum(a[i])) {
17                         if (a[i] != b[i])
18                                 return false;
19                 } else {
20                         if (cisalnum(b[i]))
21                                 return false;
22                 }
23         }
24         return b[i] == '\0';
25 }
26
27 int main(int argc, char *argv[])
28 {
29         unsigned int i;
30         const char *type;
31         const char *function = NULL;
32
33         if (argc < 3)
34                 errx(1, "Usage: doc_extract [--function=<funcname>] TYPE <file>...\n"
35                      "Where TYPE is functions|author|license|maintainer|summary|description|example|see_also|all");
36
37         if (strstarts(argv[1], "--function=")) {
38                 function = argv[1] + strlen("--function=");
39                 argv++;
40                 argc--;
41         }
42
43         type = argv[1];
44         for (i = 2; i < argc; i++) {
45                 char *file, **lines;
46                 struct list_head *list;
47                 struct doc_section *d;
48
49                 file = grab_file(NULL, argv[i]);
50                 if (!file)
51                         err(1, "Reading file %s", argv[i]);
52                 lines = tal_strsplit(file, file, "\n", STR_EMPTY_OK);
53
54                 list = extract_doc_sections(lines, argv[i]);
55                 if (list_empty(list))
56                         errx(1, "No documentation in file %s", argv[i]);
57                 tal_free(file);
58
59                 if (streq(type, "functions")) {
60                         const char *last = NULL;
61                         list_for_each(list, d, list) {
62                                 if (d->function) {
63                                         if (!last || !streq(d->function, last))
64                                                 printf("%s\n", d->function);
65                                         last = d->function;
66                                 }
67                         }
68                 } else {
69                         unsigned int j;
70                         list_for_each(list, d, list) {
71                                 if (function) {
72                                         if (!d->function)
73                                                 continue;
74                                         if (!streq(d->function, function))
75                                                 continue;
76                                 }
77                                 if (streq(type, "all"))
78                                         printf("%s:\n", d->type);
79                                 else if (!typematch(d->type, type))
80                                         continue;
81
82                                 for (j = 0; j < d->num_lines; j++)
83                                         printf("%s\n", d->lines[j]);
84                         }
85                 }
86                 tal_free(list);
87         }
88         return 0;
89 }