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