forked from ccxvii/mujs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pp.c
115 lines (96 loc) · 2.09 KB
/
pp.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* Pretty-print input source by emitting parse tree back as syntax.
* with no flags: pretty-printed source
* with -m: minified source with line breaks
* with -mm: minified source without line breaks
* with -s: s-expression syntax tree
*/
#include <stdio.h>
#include "jsi.h"
#include "jsparse.h"
static void js_ppstring(js_State *J, const char *filename, const char *source, int minify)
{
js_Ast *P;
if (js_try(J)) {
jsP_freeparse(J);
js_throw(J);
}
P = jsP_parse(J, filename, source);
if (minify > 2)
jsP_dumplist(J, P);
else
jsP_dumpsyntax(J, P, minify);
jsP_freeparse(J);
js_endtry(J);
}
void js_ppfile(js_State *J, const char *filename, int minify)
{
FILE *f;
char *s;
int n, t;
f = fopen(filename, "rb");
if (!f) {
js_error(J, "cannot open file: '%s'", filename);
}
if (fseek(f, 0, SEEK_END) < 0) {
fclose(f);
js_error(J, "cannot seek in file: '%s'", filename);
}
n = ftell(f);
if (n < 0) {
fclose(f);
js_error(J, "cannot tell in file: '%s'", filename);
}
if (fseek(f, 0, SEEK_SET) < 0) {
fclose(f);
js_error(J, "cannot seek in file: '%s'", filename);
}
s = js_malloc(J, n + 1); /* add space for string terminator */
if (!s) {
fclose(f);
js_error(J, "cannot allocate storage for file contents: '%s'", filename);
}
t = fread(s, 1, (size_t)n, f);
if (t != n) {
js_free(J, s);
fclose(f);
js_error(J, "cannot read data from file: '%s'", filename);
}
s[n] = 0; /* zero-terminate string containing file data */
if (js_try(J)) {
js_free(J, s);
fclose(f);
js_throw(J);
}
js_ppstring(J, filename, s, minify);
js_free(J, s);
fclose(f);
js_endtry(J);
}
int
main(int argc, char **argv)
{
js_State *J;
int minify = 0;
int i;
J = js_newstate(NULL, NULL, 0);
for (i = 1; i < argc; ++i) {
if (!strcmp(argv[i], "-m"))
minify = 1;
else if (!strcmp(argv[i], "-mm"))
minify = 2;
else if (!strcmp(argv[i], "-s"))
minify = 3;
else {
if (js_try(J)) {
js_report(J, js_trystring(J, -1, "Error"));
js_pop(J, 1);
continue;
}
js_ppfile(J, argv[i], minify);
js_endtry(J);
}
}
js_gc(J, 0);
js_freestate(J);
return 0;
}