-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.c
121 lines (92 loc) · 2.59 KB
/
script.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
116
117
118
119
120
121
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <err.h>
#include <getopt.h>
#include <stdint.h>
#include <string.h>
#include <sysexits.h>
#include "script.h"
#include "script-vm.h"
static int Script_printHelp;
static int Script_printVersion;
static const char *Script_format = "binary";
static int Script_pointerSize = 32;
static struct option Script_longOptions[] =
{
{ "format", required_argument, 0, 'f' },
{ "pointer-size", required_argument, 0, 'p' },
{ "help", no_argument, &Script_printHelp, 1 },
{ "version", no_argument, &Script_printVersion, 1 },
{ 0, 0, 0, 0 }
};
int
main (int argc, char **argv)
{
struct script_parse_context context;
int i;
while(-1 != (i = getopt_long(argc, argv, "", Script_longOptions, NULL)))
{
switch(i)
{
case 0:
break;
case 'f':
Script_format = optarg;
break;
case 'p':
Script_pointerSize = atoi (optarg);
break;
case '?':
fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
return EXIT_FAILURE;
}
}
if (Script_printHelp)
{
fprintf(stdout,
"Usage: %s [OPTION]... SCRIPT\n"
"\n"
" -f, --format=FORMAT set output format\n"
" -p, --pointer-size=BITS set size of pointers on target platform\n"
" --help display this help and exit\n"
" --version display version information\n"
"\n"
"Report bugs to <[email protected]>\n", argv[0]);
return EXIT_SUCCESS;
}
if (Script_printVersion)
{
puts (PACKAGE_STRING);
return EXIT_SUCCESS;
}
if (optind + 1 < argc)
errx (EX_USAGE, "Usage: %s [OPTION]... [SCRIPT]", argv[0]);
else if (optind == argc)
{
if (-1 == script_parse_file (&context, stdin))
return EXIT_FAILURE;
}
else
{
FILE *input;
if(!(input = fopen(argv[optind], "r")))
err (EXIT_FAILURE, "%s: failed to open `%s' for reading", argv[0], argv[optind]);
if (-1 == script_parse_file (&context, input))
return EXIT_FAILURE;
fclose (input);
}
SCRIPT_Optimize (&context);
if (!strcmp (Script_format, "binary"))
script_dump_binary (&context, Script_pointerSize);
else if (!strcmp (Script_format, "html"))
script_dump_html (&context);
else
{
fprintf (stderr, "Unknown format: %s\n", Script_format);
return EXIT_FAILURE;
}
arena_free(&context.statement_arena);
return EXIT_SUCCESS;
}