]> ocean-lang.org Git - ocean/blob - csrc/boot-strap/md2c.c
oceani: more test coverage
[ocean] / csrc / boot-strap / md2c.c
1 #line 124 "../mdcode.mdc"
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5
6 #include "mdcode.h"
7
8 #line 894 "../mdcode.mdc"
9 #include <fcntl.h>
10 #include <errno.h>
11 #include <sys/mman.h>
12 #include <string.h>
13 #line 867 "../mdcode.mdc"
14 static void copy_fname(char *name, int space, struct text t)
15 {
16         char *sec = t.txt;
17         int len = t.len;
18         name[0] = 0;
19         if (len < 5 || strncmp(sec, "File:", 5) != 0)
20                 return;
21         sec += 5;
22         len -= 5;
23         while (len && sec[0] == ' ') {
24                 sec++;
25                 len--;
26         }
27         if (len >= space)
28                 len = space - 1;
29         strncpy(name, sec, len);
30         name[len] = 0;
31 }
32 #line 901 "../mdcode.mdc"
33 static int errs;
34 static void pr_err(char *msg)
35 {
36         errs++;
37         fprintf(stderr, "%s\n", msg);
38 }
39
40 static char *strnchr(char *haystack, int len, char needle)
41 {
42         while (len > 0 && *haystack && *haystack != needle) {
43                 haystack++;
44                 len--;
45         }
46         return len > 0 && *haystack == needle ? haystack : NULL;
47 }
48
49 int main(int argc, char *argv[])
50 {
51         int fd;
52         size_t len;
53         char *file;
54         struct text section = {NULL, 0};
55         struct section *table, *s, *prev;
56
57         errs = 0;
58         if (argc != 2 && argc != 3) {
59                 fprintf(stderr, "Usage: mdcode file.mdc [section]\n");
60                 exit(2);
61         }
62         if (argc == 3) {
63                 section.txt = argv[2];
64                 section.len = strlen(argv[2]);
65         }
66
67         fd = open(argv[1], O_RDONLY);
68         if (fd < 0) {
69                 fprintf(stderr, "mdcode: cannot open %s: %s\n",
70                         argv[1], strerror(errno));
71                 exit(1);
72         }
73         len = lseek(fd, 0, 2);
74         file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
75         table = code_extract(file, file+len, pr_err);
76
77         for (s = table; s;
78                 (code_free(s->code), prev = s, s = s->next, free(prev))) {
79                 FILE *fl;
80                 char fname[1024];
81                 char *spc = strnchr(s->section.txt, s->section.len, ' ');
82
83                 if (spc > s->section.txt && spc[-1] == ':') {
84                         if (strncmp(s->section.txt, "File: ", 6) != 0 &&
85                             (section.txt == NULL ||
86                              text_cmp(s->section, section) != 0))
87                                 /* Ignore this section */
88                                 continue;
89                 } else {
90                         fprintf(stderr, "Code in unreferenced section that is not ignored or a file name: %.*s\n",
91                                 s->section.len, s->section.txt);
92                         errs++;
93                         continue;
94                 }
95                 if (section.txt) {
96                         if (text_cmp(s->section, section) == 0)
97                                 code_node_print(stdout, s->code, argv[1]);
98                         break;
99                 }
100                 copy_fname(fname, sizeof(fname), s->section);
101                 if (fname[0] == 0) {
102                         fprintf(stderr, "Missing file name at:%.*s\n",
103                                 s->section.len, s->section.txt);
104                         errs++;
105                         continue;
106                 }
107                 fl = fopen(fname, "w");
108                 if (!fl) {
109                         fprintf(stderr, "Cannot create %s: %s\n",
110                                 fname, strerror(errno));
111                         errs++;
112                         continue;
113                 }
114                 code_node_print(fl, s->code, argv[1]);
115                 fclose(fl);
116         }
117         exit(!!errs);
118 }
119 #line 132 "../mdcode.mdc"
120