-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
286 lines (229 loc) · 6.93 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <stdio.h>
#include "util.h"
#include "parser.h"
#include "interpreter.h"
#include "bmp.h"
typedef struct yy_buffer_state * YY_BUFFER_STATE;
YY_BUFFER_STATE yy_create_buffer(FILE * file, int size);
void yy_switch_to_buffer(YY_BUFFER_STATE new_buffer);
YY_BUFFER_STATE yy_scan_string(const char * str);
void yy_delete_buffer(YY_BUFFER_STATE buffer);
struct args {
const char * script; /* script filename */
const char * input; /* input BMP filename */
const char * output; /* output BMP filename */
bool code; /* assume that script is code instead of filename */
char * modules_prefix; /* optional modules prefix */
bool help; /* print help and exit */
};
struct args args_create() {
struct args args = { NULL, "-", "-", false, NULL, false };
return args;
}
void args_discard(struct args args) {
free(args.modules_prefix);
}
bool parse_args(struct args * args, int argc, char ** argv) {
static const char * const usage = ""
"Usage: %s [-c] [-p <modules_prefix>] <script> [<input>] [<output>]\n"
"Arguments:\n"
" - script - script filename\n"
" - input - input BMP filename or stdin if is - (default is -)\n"
" - output - output BMP filename or stdout if is - (default is -)\n"
"Options:\n"
" - -c - assume that script is code instead of filename\n"
" - -p <modules_prefix> - set prefix for module files lookup "
"(for example: if is ./, then all modules will be searching only in the working directory)\n";
uint32_t i;
int opt;
while ((opt = getopt(argc, argv, "cp:h")) != -1) {
switch (opt) {
case 'c':
args->code = true;
break;
case 'p':
args->modules_prefix = strdup(optarg);
break;
case 'h':
args->help = true;
break;
default:
fprintf(stderr, usage, argv[0]);
return false;
}
}
if (args->help) {
printf(usage, argv[0]);
return true;
}
for (i = optind; i < argc; ++i) {
switch (i - optind) {
case 0:
args->script = argv[i];
continue;
case 1:
args->input = argv[i];
continue;
case 2:
args->output = argv[i];
continue;
}
fputs("There are some extra arguments on the tail, skipping.\n", stderr);
break;
}
if (i - optind < 1) {
fputs("Script is not specified.\n", stderr);
fprintf(stderr, usage, argv[0]);
return false;
}
return true;
}
bool parse_script(struct ast_script ** result, const char * script, bool code) {
YY_BUFFER_STATE buffer_state;
FILE * script_file = NULL;
char * error = NULL;
if (code) {
buffer_state = yy_scan_string(script);
} else {
if (!(script_file = fopen(script, "r"))) {
perror("Script file opening failed");
return false;
}
buffer_state = yy_create_buffer(script_file, 16384);
yy_switch_to_buffer(buffer_state);
}
if (yyparse(result, &error)) {
fprintf(stderr, "Parsing failed: %s.\n", error);
return false;
}
yy_delete_buffer(buffer_state);
if (script_file) {
if (fclose(script_file)) {
perror("Script file closing failed");
return false;
}
}
return true;
}
bool init_interpreter(struct interpreter * interpreter, const struct ast_script * script, const char * modules_prefix) {
const char * error;
*interpreter = interpreter_create(script);
if (modules_prefix) {
interpreter->modules_prefix = modules_prefix;
}
if ((error = interpreter_process_script(interpreter))) {
fprintf(stderr, "Script preprocessing failed: %s.\n", error);
interpreter_discard(*interpreter);
return false;
}
return true;
}
bool load_image(struct bmp_image * image, const char * filename) {
bool stdinFilename = filename[0] == '-' && filename[1] == '\0';
const char * error;
FILE * file;
if (stdinFilename) {
file = stdin;
} else {
if (!(file = fopen(filename, "rb"))) {
perror("Input file opening failed");
return false;
}
}
if ((error = bmp_image_read(image, file))) {
fprintf(stderr, "Input file reading failed: %s.\n", error);
return false;
}
if (stdinFilename) {
if (fclose(file)) {
perror("Input file closing failed");
return false;
}
}
return true;
}
bool run_interpreter(struct interpreter interpreter, struct image * image) {
const char * error;
if ((error = interpreter_run(interpreter, image))) {
fprintf(stderr, "Interpretation failed: %s.\n", error);
return false;
}
return true;
}
bool save_image(struct bmp_image image, const char * filename) {
bool stdoutFilename = filename[0] == '-' && filename[1] == '\0';
const char * error;
FILE * file;
if (stdoutFilename) {
file = stdout;
} else {
if (!(file = fopen(filename, "wb"))) {
perror("Output file opening failed");
return false;
}
}
if ((error = bmp_image_write(image, file))) {
fprintf(stderr, "Output file writing failed: %s.\n", error);
return false;
}
if (stdoutFilename) {
if (fclose(file)) {
perror("Output file closing failed");
return false;
}
}
return true;
}
int main(int argc, char ** argv) {
struct args args = args_create();
struct ast_script * script;
struct interpreter interpreter;
struct bmp_image bmp_image;
struct image image;
if (!parse_args(&args, argc, argv)) {
return 1;
}
if (args.help) {
return 0;
}
if (!parse_script(&script, args.script, args.code)) {
args_discard(args);
return 2;
}
if (!init_interpreter(&interpreter, script, args.modules_prefix)) {
ast_script_delete(script);
args_discard(args);
return 3;
}
if (!load_image(&bmp_image, args.input)) {
interpreter_discard(interpreter);
ast_script_delete(script);
args_discard(args);
return 4;
}
image = bmp_image_to_image(bmp_image);
if (!run_interpreter(interpreter, &image)) {
image_discard(image);
bmp_image_discard(bmp_image);
interpreter_discard(interpreter);
ast_script_delete(script);
args_discard(args);
return 5;
}
interpreter_discard(interpreter);
ast_script_delete(script);
bmp_image_replace(&bmp_image, image);
image_discard(image);
if (!save_image(bmp_image, args.output)) {
bmp_image_discard(bmp_image);
args_discard(args);
return 6;
}
bmp_image_discard(bmp_image);
args_discard(args);
return 0;
}