]> git.ozlabs.org Git - ccan/blob - tools/doc_extract.c
Proper recursive dependencies (came from libantithread work)
[ccan] / tools / doc_extract.c
1 /* This merely extracts, doesn't do XML or anything. */
2 #include <err.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <stdbool.h>
11 #include "talloc/talloc.h"
12
13 /* Is A == B ? */
14 #define streq(a,b) (strcmp((a),(b)) == 0)
15
16 /* Does A start with B ? */
17 #define strstarts(a,b) (strncmp((a),(b),strlen(b)) == 0)
18
19 /* This version adds one byte (for nul term) */
20 static void *grab_file(void *ctx, const char *filename)
21 {
22         unsigned int max = 16384, size = 0;
23         int ret, fd;
24         char *buffer;
25
26         if (streq(filename, "-"))
27                 fd = dup(STDIN_FILENO);
28         else
29                 fd = open(filename, O_RDONLY, 0);
30
31         if (fd < 0)
32                 return NULL;
33
34         buffer = talloc_array(ctx, char, max+1);
35         while ((ret = read(fd, buffer + size, max - size)) > 0) {
36                 size += ret;
37                 if (size == max)
38                         buffer = talloc_realloc(ctx, buffer, char, max*=2 + 1);
39         }
40         if (ret < 0) {
41                 talloc_free(buffer);
42                 buffer = NULL;
43         } else
44                 buffer[size] = '\0';
45         close(fd);
46         return buffer;
47 }
48
49 /* This is a dumb one which copies.  We could mangle instead. */
50 static char **split(const char *text)
51 {
52         char **lines = NULL;
53         unsigned int max = 64, num = 0;
54
55         lines = talloc_array(text, char *, max+1);
56
57         while (*text != '\0') {
58                 unsigned int len = strcspn(text, "\n");
59                 lines[num] = talloc_array(lines, char, len + 1);
60                 memcpy(lines[num], text, len);
61                 lines[num][len] = '\0';
62                 text += len + 1;
63                 if (++num == max)
64                         lines = talloc_realloc(text, lines, char *, max*=2 + 1);
65         }
66         lines[num] = NULL;
67         return lines;
68 }
69
70 int main(int argc, char *argv[])
71 {
72         unsigned int i, j;
73
74         for (i = 1; i < argc; i++) {
75                 char *file;
76                 char **lines;
77                 bool printing = false, printed = false;
78
79                 file = grab_file(NULL, argv[i]);
80                 if (!file)
81                         err(1, "Reading file %s", argv[i]);
82                 lines = split(file);
83
84                 for (j = 0; lines[j]; j++) {
85                         if (streq(lines[j], "/**")) {
86                                 printing = true;
87                                 if (printed++)
88                                         puts("\n");
89                         } else if (streq(lines[j], " */"))
90                                 printing = false;
91                         else if (printing) {
92                                 if (strstarts(lines[j], " * "))
93                                         puts(lines[j] + 3);
94                                 else if (strstarts(lines[j], " *"))
95                                         puts(lines[j] + 2);
96                                 else
97                                         errx(1, "Malformed line %s:%u",
98                                              argv[i], j);
99                         }
100                 }
101                 talloc_free(file);
102         }
103         return 0;
104 }
105
106                 
107