1 #line 119 "../mdcode.mdc"
7 #line 849 "../mdcode.mdc"
14 #line 771 "../mdcode.mdc"
15 static void code_print(FILE *out, struct code_node *node,
18 for (; node; node = node->next) {
19 char *c = node->code.txt;
20 int len = node->code.len;
26 fprintf(out, "#line %d \"%s\"\n",
27 node->line_no, fname);
28 if (*c == ' ' || *c == '\t')
31 fprintf(out, "%*s", node->indent, "");
33 if (*c == '\t' && len > 1) {
36 } else if (strncmp(c, " ", 4) == 0 && len > 4) {
45 } while (len && c[-1] != '\n');
50 #line 821 "../mdcode.mdc"
51 static void copy_fname(char *name, int space, struct text t)
56 if (len < 5 || strncmp(sec, "File:", 5) != 0)
60 while (len && sec[0] == ' ') {
66 strncpy(name, sec, len);
70 #line 857 "../mdcode.mdc"
72 static void pr_err(char *msg)
75 fprintf(stderr, "%s\n", msg);
78 int main(int argc, char *argv[])
83 struct section *table, *s, *prev;
87 fprintf(stderr, "Usage: mdcode file.mdc\n");
90 fd = open(argv[1], O_RDONLY);
92 fprintf(stderr, "mdcode: cannot open %s: %s\n",
93 argv[1], strerror(errno));
96 len = lseek(fd, 0, 2);
97 file = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0);
98 table = code_extract(file, file+len, pr_err);
101 (code_free(s->code), prev = s, s = s->next, free(prev))) {
104 if (strncmp(s->section.txt, "Example:", 8) == 0)
106 if (strncmp(s->section.txt, "File:", 5) != 0) {
107 fprintf(stderr, "Unreferenced section is not a file name: %.*s\n",
108 s->section.len, s->section.txt);
112 copy_fname(fname, sizeof(fname), s->section);
114 fprintf(stderr, "Missing file name at:%.*s\n",
115 s->section.len, s->section.txt);
119 fl = fopen(fname, "w");
121 fprintf(stderr, "Cannot create %s: %s\n",
122 fname, strerror(errno));
126 code_print(fl, s->code, argv[1]);
132 #line 126 "../mdcode.mdc"