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