-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmml2mdx.c
79 lines (69 loc) · 1.56 KB
/
mml2mdx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef __linux__
#include <linux/limits.h>
#endif
#ifdef __APPLE__
#include <sys/syslimits.h>
#endif
#include "mdx_compiler.h"
#include "cmdline.h"
#include "tools.h"
int opt_stats = 0;
char *opt_output = 0;
struct mdx_compiler compiler;
int main(int argc, char **argv) {
int optind = cmdline_parse_args(argc, argv, (struct cmdline_option[]){
{
's', "stats",
"Dump statistics about compilation",
0,
TYPE_SWITCH,
TYPE_INT, &opt_stats
},
{
'o', "output",
"Choose output file",
"file",
TYPE_REQUIRED,
TYPE_STRING, &opt_output
},
CMDLINE_ARG_TERMINATOR
}, 1, 1, "<file.mml>");
if(optind < 0) {
return -optind;
}
if(opt_output && argc - optind > 1) {
fprintf(stderr, "When specifying output file, please specify only one input file.\n");
return 1;
}
for(int i = optind; i < argc; i++) {
FILE *f = fopen(argv[i], "r");
if(!f) {
fprintf(stderr, "Could not open %s: %s\n", argv[i], strerror(errno));
continue;
}
mdx_compiler_init(&compiler);
int y = mdx_compiler_parse(&compiler, f);
if(y == 1) {
fprintf(stderr, "parsing failed: invalid input\n");
return 1;
}
if(y == 2) {
fprintf(stderr, "parsing failed: memory exhausted\n");
return 2;
}
if(opt_stats)
mdx_compiler_dump(&compiler);
if(!opt_output || !opt_output[0]) {
char mdxbuf[PATH_MAX];
replace_ext(mdxbuf, sizeof(mdxbuf), argv[i], "mdx");
opt_output = mdxbuf;
}
mdx_compiler_save(&compiler, opt_output);
mdx_compiler_destroy(&compiler);
}
return 0;
}