1 #line 124 "../mdcode.mdc"
8 #line 894 "../mdcode.mdc"
13 #line 867 "../mdcode.mdc"
14 static void copy_fname(char *name, int space, struct text t)
19 if (len < 5 || strncmp(sec, "File:", 5) != 0)
23 while (len && sec[0] == ' ') {
29 strncpy(name, sec, len);
32 #line 901 "../mdcode.mdc"
34 static void pr_err(char *msg)
37 fprintf(stderr, "%s\n", msg);
40 static char *strnchr(char *haystack, int len, char needle)
42 while (len > 0 && *haystack && *haystack != needle) {
46 return len > 0 && *haystack == needle ? haystack : NULL;
49 int main(int argc, char *argv[])
54 struct text section = {NULL, 0};
55 struct section *table, *s, *prev;
58 if (argc != 2 && argc != 3) {
59 fprintf(stderr, "Usage: mdcode file.mdc [section]\n");
63 section.txt = argv[2];
64 section.len = strlen(argv[2]);
67 fd = open(argv[1], O_RDONLY);
69 fprintf(stderr, "mdcode: cannot open %s: %s\n",
70 argv[1], strerror(errno));
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);
78 (code_free(s->code), prev = s, s = s->next, free(prev))) {
81 char *spc = strnchr(s->section.txt, s->section.len, ' ');
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 */
90 fprintf(stderr, "Code in unreferenced section that is not ignored or a file name: %.*s\n",
91 s->section.len, s->section.txt);
96 if (text_cmp(s->section, section) == 0)
97 code_node_print(stdout, s->code, argv[1]);
100 copy_fname(fname, sizeof(fname), s->section);
102 fprintf(stderr, "Missing file name at:%.*s\n",
103 s->section.len, s->section.txt);
107 fl = fopen(fname, "w");
109 fprintf(stderr, "Cannot create %s: %s\n",
110 fname, strerror(errno));
114 code_node_print(fl, s->code, argv[1]);
119 #line 132 "../mdcode.mdc"