]>
Commit | Line | Data |
---|---|---|
b4aa79af JF |
1 | /* Cycript - Remove Execution Server and Disassembler |
2 | * Copyright (C) 2009 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
5 | /* Modified BSD License {{{ */ | |
6 | /* | |
7 | * Redistribution and use in source and binary | |
8 | * forms, with or without modification, are permitted | |
9 | * provided that the following conditions are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the | |
12 | * above copyright notice, this list of conditions | |
13 | * and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the | |
15 | * above copyright notice, this list of conditions | |
16 | * and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the | |
18 | * distribution. | |
19 | * 3. The name of the author may not be used to endorse | |
20 | * or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' | |
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
25 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
35 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
36 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | */ | |
38 | /* }}} */ | |
39 | ||
30ddc20c | 40 | #include "cycript.hpp" |
057f943f JF |
41 | |
42 | #include <cstdio> | |
43 | #include <sstream> | |
44 | ||
45 | #include <setjmp.h> | |
46 | ||
47 | #include <readline/readline.h> | |
48 | #include <readline/history.h> | |
49 | ||
b09da87b JF |
50 | #include <sys/mman.h> |
51 | ||
52 | #include <errno.h> | |
53 | #include <unistd.h> | |
54 | ||
55 | #include <sys/types.h> | |
56 | #include <sys/stat.h> | |
57 | #include <fcntl.h> | |
58 | ||
057f943f JF |
59 | #include "Cycript.tab.hh" |
60 | ||
967067aa JF |
61 | #include <sys/types.h> |
62 | #include <sys/socket.h> | |
63 | #include <netinet/in.h> | |
64 | #include <sys/un.h> | |
b1589845 | 65 | #include <pwd.h> |
967067aa | 66 | |
06edab7d JF |
67 | #include <apr_getopt.h> |
68 | ||
4e39dc0b JF |
69 | static volatile enum { |
70 | Working, | |
71 | Parsing, | |
72 | Running, | |
73 | Sending, | |
74 | Waiting, | |
75 | } mode_; | |
76 | ||
057f943f JF |
77 | static jmp_buf ctrlc_; |
78 | ||
579ed526 | 79 | static void sigint(int) { |
4e39dc0b JF |
80 | switch (mode_) { |
81 | case Working: | |
82 | return; | |
83 | case Parsing: | |
84 | longjmp(ctrlc_, 1); | |
85 | case Running: | |
86 | throw "*** Ctrl-C"; | |
87 | case Sending: | |
88 | return; | |
89 | case Waiting: | |
90 | return; | |
91 | } | |
057f943f JF |
92 | } |
93 | ||
cac61857 JF |
94 | #if YYDEBUG |
95 | static bool bison_; | |
96 | #endif | |
b10bd496 | 97 | static bool strict_; |
11c1cc16 | 98 | static bool pretty_; |
cac61857 | 99 | |
b10bd496 | 100 | void Setup(CYDriver &driver, cy::parser &parser) { |
cac61857 JF |
101 | #if YYDEBUG |
102 | if (bison_) | |
103 | parser.set_debug_level(1); | |
104 | #endif | |
b10bd496 JF |
105 | if (strict_) |
106 | driver.strict_ = true; | |
cac61857 JF |
107 | } |
108 | ||
3b52fd1a | 109 | void Setup(CYOutput &out, CYDriver &driver) { |
11c1cc16 | 110 | out.pretty_ = pretty_; |
3b52fd1a JF |
111 | |
112 | CYContext context(driver.pool_); | |
113 | driver.program_->Replace(context); | |
11c1cc16 JF |
114 | } |
115 | ||
48e3be8a | 116 | void Run(int socket, const char *data, size_t size, FILE *fout = NULL, bool expand = false) { |
967067aa | 117 | CYPool pool; |
b09da87b | 118 | |
967067aa | 119 | const char *json; |
6ec85c5b | 120 | if (socket == -1) { |
4e39dc0b | 121 | mode_ = Running; |
2aa77fd8 | 122 | #ifdef CY_EXECUTE |
fcc64bb5 | 123 | json = CYExecute(pool, data); |
2aa77fd8 JF |
124 | #else |
125 | json = NULL; | |
126 | #endif | |
4e39dc0b | 127 | mode_ = Working; |
6ec85c5b JF |
128 | if (json != NULL) |
129 | size = strlen(json); | |
130 | } else { | |
4e39dc0b | 131 | mode_ = Sending; |
967067aa JF |
132 | CYSendAll(socket, &size, sizeof(size)); |
133 | CYSendAll(socket, data, size); | |
4e39dc0b | 134 | mode_ = Waiting; |
967067aa JF |
135 | CYRecvAll(socket, &size, sizeof(size)); |
136 | if (size == _not(size_t)) | |
137 | json = NULL; | |
138 | else { | |
139 | char *temp(new(pool) char[size + 1]); | |
140 | CYRecvAll(socket, temp, size); | |
141 | temp[size] = '\0'; | |
142 | json = temp; | |
143 | } | |
4e39dc0b | 144 | mode_ = Working; |
4cf49641 | 145 | } |
b09da87b | 146 | |
967067aa | 147 | if (json != NULL && fout != NULL) { |
6ec85c5b JF |
148 | if (!expand || json[0] != '"' && json[0] != '\'') |
149 | fputs(json, fout); | |
150 | else for (size_t i(0); i != size; ++i) | |
151 | if (json[i] != '\\') | |
152 | fputc(json[i], fout); | |
153 | else switch(json[++i]) { | |
154 | case '\0': goto done; | |
155 | case '\\': fputc('\\', fout); break; | |
156 | case '\'': fputc('\'', fout); break; | |
157 | case '"': fputc('"', fout); break; | |
158 | case 'b': fputc('\b', fout); break; | |
159 | case 'f': fputc('\f', fout); break; | |
160 | case 'n': fputc('\n', fout); break; | |
161 | case 'r': fputc('\r', fout); break; | |
162 | case 't': fputc('\t', fout); break; | |
163 | case 'v': fputc('\v', fout); break; | |
164 | default: fputc('\\', fout); --i; break; | |
165 | } | |
166 | ||
167 | done: | |
967067aa JF |
168 | fputs("\n", fout); |
169 | fflush(fout); | |
b09da87b JF |
170 | } |
171 | } | |
172 | ||
48e3be8a | 173 | void Run(int socket, std::string &code, FILE *fout = NULL, bool expand = false) { |
6ec85c5b | 174 | Run(socket, code.c_str(), code.size(), fout, expand); |
fcc64bb5 JF |
175 | } |
176 | ||
b1589845 JF |
177 | static void Console(apr_pool_t *pool, int socket) { |
178 | passwd *passwd; | |
179 | if (const char *username = getenv("LOGNAME")) | |
180 | passwd = getpwnam(username); | |
181 | else | |
182 | passwd = getpwuid(getuid()); | |
183 | ||
184 | const char *basedir(apr_psprintf(pool, "%s/.cycript", passwd->pw_dir)); | |
185 | const char *histfile(apr_psprintf(pool, "%s/history", basedir)); | |
186 | size_t histlines(0); | |
187 | ||
188 | mkdir(basedir, 0700); | |
189 | read_history(histfile); | |
190 | ||
1f8eae40 JF |
191 | bool bypass(false); |
192 | bool debug(false); | |
6ec85c5b | 193 | bool expand(false); |
1f8eae40 | 194 | |
057f943f JF |
195 | FILE *fout(stdout); |
196 | ||
197 | rl_bind_key('\t', rl_insert); | |
198 | ||
199 | struct sigaction action; | |
200 | sigemptyset(&action.sa_mask); | |
201 | action.sa_handler = &sigint; | |
202 | action.sa_flags = 0; | |
203 | sigaction(SIGINT, &action, NULL); | |
204 | ||
205 | restart: for (;;) { | |
206 | std::string command; | |
207 | std::vector<std::string> lines; | |
208 | ||
931b816a JF |
209 | bool extra(false); |
210 | const char *prompt("cy# "); | |
211 | ||
057f943f | 212 | if (setjmp(ctrlc_) != 0) { |
4e39dc0b | 213 | mode_ = Working; |
057f943f JF |
214 | fputs("\n", fout); |
215 | fflush(fout); | |
216 | goto restart; | |
217 | } | |
218 | ||
057f943f | 219 | read: |
4e39dc0b | 220 | mode_ = Parsing; |
057f943f | 221 | char *line(readline(prompt)); |
4e39dc0b | 222 | mode_ = Working; |
057f943f JF |
223 | if (line == NULL) |
224 | break; | |
931b816a JF |
225 | |
226 | if (!extra) { | |
227 | extra = true; | |
6ec85c5b | 228 | if (line[0] == '?') { |
1f8eae40 JF |
229 | std::string data(line + 1); |
230 | if (data == "bypass") { | |
231 | bypass = !bypass; | |
232 | fprintf(fout, "bypass == %s\n", bypass ? "true" : "false"); | |
233 | fflush(fout); | |
234 | } else if (data == "debug") { | |
235 | debug = !debug; | |
236 | fprintf(fout, "debug == %s\n", debug ? "true" : "false"); | |
237 | fflush(fout); | |
6ec85c5b JF |
238 | } else if (data == "expand") { |
239 | expand = !expand; | |
240 | fprintf(fout, "expand == %s\n", expand ? "true" : "false"); | |
241 | fflush(fout); | |
1f8eae40 | 242 | } |
d35a3b07 | 243 | add_history(line); |
b1589845 | 244 | ++histlines; |
931b816a JF |
245 | goto restart; |
246 | } | |
247 | } | |
248 | ||
057f943f | 249 | command += line; |
e818f0e0 JF |
250 | |
251 | char *begin(line), *end(line + strlen(line)); | |
252 | while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) { | |
253 | *nl = '\0'; | |
254 | lines.push_back(begin); | |
255 | begin = nl + 1; | |
256 | } | |
257 | ||
258 | lines.push_back(begin); | |
259 | ||
057f943f JF |
260 | free(line); |
261 | ||
1f8eae40 JF |
262 | std::string code; |
263 | ||
264 | if (bypass) | |
265 | code = command; | |
266 | else { | |
267 | CYDriver driver(""); | |
268 | cy::parser parser(driver); | |
b10bd496 | 269 | Setup(driver, parser); |
1f8eae40 JF |
270 | |
271 | driver.data_ = command.c_str(); | |
272 | driver.size_ = command.size(); | |
273 | ||
274 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
f0360d51 JF |
275 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { |
276 | cy::position begin(error->location_.begin); | |
b10bd496 | 277 | if (begin.line != lines.size() || begin.column - 1 != lines.back().size() || error->warning_) { |
f0360d51 | 278 | cy::position end(error->location_.end); |
48e3be8a | 279 | |
f0360d51 JF |
280 | if (begin.line != lines.size()) { |
281 | std::cerr << " | "; | |
282 | std::cerr << lines[begin.line - 1] << std::endl; | |
283 | } | |
48e3be8a | 284 | |
c52a09b8 | 285 | std::cerr << "...."; |
f0360d51 JF |
286 | for (size_t i(0); i != begin.column - 1; ++i) |
287 | std::cerr << '.'; | |
48e3be8a | 288 | if (begin.line != end.line || begin.column == end.column) |
f0360d51 JF |
289 | std::cerr << '^'; |
290 | else for (size_t i(0), e(end.column - begin.column); i != e; ++i) | |
291 | std::cerr << '^'; | |
292 | std::cerr << std::endl; | |
48e3be8a | 293 | |
f0360d51 JF |
294 | std::cerr << " | "; |
295 | std::cerr << error->message_ << std::endl; | |
48e3be8a | 296 | |
1f8eae40 | 297 | add_history(command.c_str()); |
b1589845 | 298 | ++histlines; |
1f8eae40 JF |
299 | goto restart; |
300 | } | |
301 | } | |
057f943f | 302 | |
1f8eae40 | 303 | driver.errors_.clear(); |
057f943f | 304 | |
1f8eae40 JF |
305 | command += '\n'; |
306 | prompt = "cy> "; | |
307 | goto read; | |
057f943f JF |
308 | } |
309 | ||
b10bd496 | 310 | if (driver.program_ == NULL) |
1f8eae40 | 311 | goto restart; |
057f943f | 312 | |
967067aa JF |
313 | if (socket != -1) |
314 | code = command; | |
315 | else { | |
316 | std::ostringstream str; | |
652ec1ba | 317 | CYOutput out(str); |
3b52fd1a JF |
318 | Setup(out, driver); |
319 | out << *driver.program_; | |
967067aa JF |
320 | code = str.str(); |
321 | } | |
057f943f JF |
322 | } |
323 | ||
057f943f | 324 | add_history(command.c_str()); |
b1589845 | 325 | ++histlines; |
057f943f | 326 | |
1f8eae40 JF |
327 | if (debug) |
328 | std::cout << code << std::endl; | |
057f943f | 329 | |
6ec85c5b | 330 | Run(socket, code, fout, expand); |
b09da87b | 331 | } |
057f943f | 332 | |
b1589845 JF |
333 | _syscall(close(_syscall(open(histfile, O_CREAT | O_WRONLY, 0600)))); |
334 | append_history(histlines, histfile); | |
335 | ||
b09da87b JF |
336 | fputs("\n", fout); |
337 | fflush(fout); | |
338 | } | |
057f943f | 339 | |
579ed526 | 340 | static void *Map(const char *path, size_t *psize) { |
b09da87b JF |
341 | int fd; |
342 | _syscall(fd = open(path, O_RDONLY)); | |
057f943f | 343 | |
b09da87b JF |
344 | struct stat stat; |
345 | _syscall(fstat(fd, &stat)); | |
346 | size_t size(stat.st_size); | |
057f943f | 347 | |
b09da87b | 348 | *psize = size; |
057f943f | 349 | |
b09da87b JF |
350 | void *base; |
351 | _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); | |
057f943f | 352 | |
b09da87b JF |
353 | _syscall(close(fd)); |
354 | return base; | |
355 | } | |
057f943f | 356 | |
b6961e53 | 357 | void InjectLibrary(pid_t pid); |
06edab7d | 358 | |
b6961e53 | 359 | int Main(int argc, char const * const argv[], char const * const envp[]) { |
48e3be8a | 360 | bool tty(isatty(STDIN_FILENO)); |
75b0a457 | 361 | bool compile(false); |
967067aa | 362 | |
2aa77fd8 JF |
363 | #ifdef CY_ATTACH |
364 | pid_t pid(_not(pid_t)); | |
365 | #endif | |
366 | ||
06edab7d JF |
367 | CYPool pool; |
368 | apr_getopt_t *state; | |
369 | _aprcall(apr_getopt_init(&state, pool, argc, argv)); | |
370 | ||
371 | for (;;) { | |
372 | char opt; | |
373 | const char *arg; | |
374 | ||
375 | apr_status_t status(apr_getopt(state, | |
376 | "cg:n:" | |
2aa77fd8 | 377 | #ifdef CY_ATTACH |
06edab7d | 378 | "p:" |
2aa77fd8 | 379 | #endif |
06edab7d JF |
380 | "s" |
381 | , &opt, &arg)); | |
382 | ||
383 | switch (status) { | |
384 | case APR_EOF: | |
385 | goto getopt; | |
386 | case APR_BADCH: | |
387 | case APR_BADARG: | |
388 | fprintf(stderr, | |
389 | "usage: cycript [-c]" | |
2aa77fd8 | 390 | #ifdef CY_ATTACH |
69caa5be | 391 | " [-p <pid|name>]" |
2aa77fd8 | 392 | #endif |
06edab7d JF |
393 | " [<script> [<arg>...]]\n" |
394 | ); | |
395 | return 1; | |
396 | default: | |
397 | _aprcall(status); | |
398 | } | |
967067aa | 399 | |
06edab7d JF |
400 | switch (opt) { |
401 | case 'c': | |
402 | compile = true; | |
403 | break; | |
75b0a457 | 404 | |
06edab7d JF |
405 | case 'g': |
406 | if (false); | |
cac61857 | 407 | #if YYDEBUG |
5ef46cc0 | 408 | else if (strcmp(arg, "bison") == 0) |
06edab7d | 409 | bison_ = true; |
cac61857 | 410 | #endif |
06edab7d JF |
411 | else { |
412 | fprintf(stderr, "invalid name for -g\n"); | |
413 | return 1; | |
414 | } | |
415 | break; | |
cac61857 | 416 | |
06edab7d JF |
417 | case 'n': |
418 | if (false); | |
5ef46cc0 | 419 | else if (strcmp(arg, "minify") == 0) |
06edab7d JF |
420 | pretty_ = true; |
421 | else { | |
422 | fprintf(stderr, "invalid name for -n\n"); | |
423 | return 1; | |
424 | } | |
425 | break; | |
11c1cc16 | 426 | |
2aa77fd8 | 427 | #ifdef CY_ATTACH |
06edab7d | 428 | case 'p': { |
5ef46cc0 | 429 | size_t size(strlen(arg)); |
06edab7d | 430 | char *end; |
69caa5be | 431 | |
5ef46cc0 JF |
432 | pid = strtoul(arg, &end, 0); |
433 | if (arg + size != end) { | |
69caa5be JF |
434 | // XXX: arg needs to be escaped in some horrendous way of doom |
435 | const char *command(apr_psprintf(pool, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", arg)); | |
436 | ||
437 | if (FILE *pids = popen(command, "r")) { | |
438 | char value[32]; | |
439 | size = 0; | |
440 | ||
441 | for (;;) { | |
442 | size_t read(fread(value + size, 1, sizeof(value) - size, pids)); | |
443 | if (read == 0) | |
444 | break; | |
445 | else { | |
446 | size += read; | |
447 | if (size == sizeof(value)) { | |
448 | pid = _not(pid_t); | |
449 | goto pclose; | |
450 | } | |
451 | } | |
452 | } | |
453 | ||
454 | size: | |
455 | if (size == 0) | |
456 | goto pclose; | |
457 | if (value[size - 1] == '\n') { | |
458 | --size; | |
459 | goto size; | |
460 | } | |
461 | ||
462 | value[size] = '\0'; | |
463 | size = strlen(value); | |
464 | pid = strtoul(value, &end, 0); | |
465 | if (value + size != end) | |
466 | pid = _not(pid_t); | |
467 | ||
468 | pclose: | |
469 | _syscall(pclose(pids)); | |
470 | } | |
471 | ||
472 | if (pid == _not(pid_t)) { | |
473 | fprintf(stderr, "invalid pid for -p\n"); | |
474 | return 1; | |
475 | } | |
06edab7d JF |
476 | } |
477 | } break; | |
2aa77fd8 | 478 | #endif |
b10bd496 | 479 | |
06edab7d JF |
480 | case 's': |
481 | strict_ = true; | |
482 | break; | |
483 | } | |
967067aa JF |
484 | } getopt:; |
485 | ||
b09da87b | 486 | const char *script; |
5ef46cc0 | 487 | int ind(state->ind); |
b09da87b | 488 | |
2aa77fd8 | 489 | #ifdef CY_ATTACH |
5ef46cc0 | 490 | if (pid != _not(pid_t) && ind < argc - 1) { |
fcc64bb5 JF |
491 | fprintf(stderr, "-p cannot set argv\n"); |
492 | return 1; | |
493 | } | |
494 | ||
75b0a457 JF |
495 | if (pid != _not(pid_t) && compile) { |
496 | fprintf(stderr, "-p conflicts with -c\n"); | |
497 | return 1; | |
498 | } | |
2aa77fd8 | 499 | #endif |
75b0a457 | 500 | |
5ef46cc0 | 501 | if (ind == argc) |
b09da87b JF |
502 | script = NULL; |
503 | else { | |
9185d5ef | 504 | #ifdef CY_EXECUTE |
967067aa | 505 | // XXX: const_cast?! wtf gcc :( |
5ef46cc0 | 506 | CYSetArgs(argc - ind - 1, const_cast<const char **>(argv + ind + 1)); |
9185d5ef | 507 | #endif |
5ef46cc0 | 508 | script = argv[ind]; |
967067aa JF |
509 | if (strcmp(script, "-") == 0) |
510 | script = NULL; | |
b09da87b JF |
511 | } |
512 | ||
2aa77fd8 JF |
513 | #ifdef CY_ATTACH |
514 | if (pid != _not(pid_t) && script == NULL && !tty) { | |
515 | fprintf(stderr, "non-terminal attaching to remote console\n"); | |
48e3be8a JF |
516 | return 1; |
517 | } | |
2aa77fd8 | 518 | #endif |
48e3be8a | 519 | |
967067aa JF |
520 | int socket; |
521 | ||
2aa77fd8 | 522 | #ifdef CY_ATTACH |
967067aa JF |
523 | if (pid == _not(pid_t)) |
524 | socket = -1; | |
525 | else { | |
b6961e53 JF |
526 | InjectLibrary(pid); |
527 | ||
967067aa JF |
528 | socket = _syscall(::socket(PF_UNIX, SOCK_STREAM, 0)); |
529 | ||
530 | struct sockaddr_un address; | |
531 | memset(&address, 0, sizeof(address)); | |
532 | address.sun_family = AF_UNIX; | |
533 | sprintf(address.sun_path, "/tmp/.s.cy.%u", pid); | |
534 | ||
535 | _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
536 | } | |
2aa77fd8 JF |
537 | #else |
538 | socket = -1; | |
539 | #endif | |
579ed526 | 540 | |
48e3be8a | 541 | if (script == NULL && tty) |
b1589845 | 542 | Console(pool, socket); |
b09da87b | 543 | else { |
48e3be8a | 544 | CYDriver driver(script ?: "<stdin>"); |
b09da87b | 545 | cy::parser parser(driver); |
b10bd496 | 546 | Setup(driver, parser); |
b09da87b | 547 | |
48e3be8a | 548 | char *start, *end; |
b09da87b | 549 | |
48e3be8a JF |
550 | if (script == NULL) { |
551 | start = NULL; | |
552 | end = NULL; | |
2b1245e4 | 553 | |
48e3be8a JF |
554 | driver.file_ = stdin; |
555 | } else { | |
556 | size_t size; | |
557 | start = reinterpret_cast<char *>(Map(script, &size)); | |
558 | end = start + size; | |
559 | ||
560 | if (size >= 2 && start[0] == '#' && start[1] == '!') { | |
561 | start += 2; | |
057f943f | 562 | |
48e3be8a JF |
563 | if (void *line = memchr(start, '\n', end - start)) |
564 | start = reinterpret_cast<char *>(line); | |
565 | else | |
566 | start = end; | |
567 | } | |
568 | ||
569 | driver.data_ = start; | |
570 | driver.size_ = end - start; | |
571 | } | |
62ca2b82 | 572 | |
b09da87b JF |
573 | if (parser.parse() != 0 || !driver.errors_.empty()) { |
574 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) | |
575 | std::cerr << i->location_.begin << ": " << i->message_ << std::endl; | |
b10bd496 | 576 | } else if (driver.program_ != NULL) |
fcc64bb5 JF |
577 | if (socket != -1) |
578 | Run(socket, start, end - start, stdout); | |
579 | else { | |
580 | std::ostringstream str; | |
652ec1ba | 581 | CYOutput out(str); |
3b52fd1a JF |
582 | Setup(out, driver); |
583 | out << *driver.program_; | |
fcc64bb5 | 584 | std::string code(str.str()); |
75b0a457 JF |
585 | if (compile) |
586 | std::cout << code; | |
587 | else | |
c0bc320e | 588 | Run(socket, code, stdout); |
fcc64bb5 | 589 | } |
057f943f | 590 | } |
62ca2b82 | 591 | |
057f943f | 592 | return 0; |
62ca2b82 | 593 | } |
b6961e53 JF |
594 | |
595 | int main(int argc, char const * const argv[], char const * const envp[]) { | |
596 | apr_status_t status(apr_app_initialize(&argc, &argv, &envp)); | |
597 | if (status != APR_SUCCESS) { | |
598 | fprintf(stderr, "apr_app_initialize() != APR_SUCCESS\n"); | |
599 | return 1; | |
600 | } else try { | |
601 | return Main(argc, argv, envp); | |
602 | } catch (const CYException &error) { | |
603 | CYPool pool; | |
604 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
605 | return 1; | |
606 | } | |
607 | } |