]> git.saurik.com Git - cycript.git/blob - Console.cpp
CYExecute() should run code in a specific context.
[cycript.git] / Console.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
3 */
4
5 /* GNU General Public License, Version 3 {{{ */
6 /*
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
19 **/
20 /* }}} */
21
22 #include "cycript.hpp"
23
24 #ifdef CY_EXECUTE
25 #include "JavaScript.hpp"
26 #endif
27
28 #include <cstdio>
29 #include <fstream>
30 #include <sstream>
31
32 #include <setjmp.h>
33
34 #ifdef HAVE_READLINE_H
35 #include <readline.h>
36 #else
37 #include <readline/readline.h>
38 #endif
39
40 #ifdef HAVE_HISTORY_H
41 #include <history.h>
42 #else
43 #include <readline/history.h>
44 #endif
45
46 #include <errno.h>
47 #include <unistd.h>
48
49 #include <sys/socket.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <fcntl.h>
53 #include <netdb.h>
54
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <netinet/in.h>
58 #include <sys/un.h>
59 #include <pwd.h>
60
61 #include <apr_getopt.h>
62 #include <apr_pools.h>
63 #include <apr_strings.h>
64
65 #include <dlfcn.h>
66
67 #include "Display.hpp"
68 #include "Replace.hpp"
69
70 #include "Cycript.tab.hh"
71 #include "Driver.hpp"
72
73 static volatile enum {
74 Working,
75 Parsing,
76 Running,
77 Sending,
78 Waiting,
79 } mode_;
80
81 static jmp_buf ctrlc_;
82
83 static void sigint(int) {
84 switch (mode_) {
85 case Working:
86 return;
87 case Parsing:
88 longjmp(ctrlc_, 1);
89 case Running:
90 throw "*** Ctrl-C";
91 case Sending:
92 return;
93 case Waiting:
94 return;
95 }
96 }
97
98 #if YYDEBUG
99 static bool bison_;
100 #endif
101 static bool strict_;
102 static bool pretty_;
103
104 void Setup(CYDriver &driver, cy::parser &parser) {
105 #if YYDEBUG
106 if (bison_)
107 parser.set_debug_level(1);
108 #endif
109 if (strict_)
110 driver.strict_ = true;
111 }
112
113 void Setup(CYOutput &out, CYDriver &driver, CYOptions &options) {
114 out.pretty_ = pretty_;
115 CYContext context(options);
116 driver.program_->Replace(context);
117 }
118
119 static CYUTF8String Run(CYPool &pool, int client, CYUTF8String code) {
120 const char *json;
121 uint32_t size;
122
123 if (client == -1) {
124 mode_ = Running;
125 #ifdef CY_EXECUTE
126 json = CYExecute(CYGetJSContext(), pool, code);
127 #else
128 json = NULL;
129 #endif
130 mode_ = Working;
131 if (json == NULL)
132 size = 0;
133 else
134 size = strlen(json);
135 } else {
136 mode_ = Sending;
137 size = code.size;
138 _assert(CYSendAll(client, &size, sizeof(size)));
139 _assert(CYSendAll(client, code.data, code.size));
140 mode_ = Waiting;
141 _assert(CYRecvAll(client, &size, sizeof(size)));
142 if (size == _not(uint32_t))
143 json = NULL;
144 else {
145 char *temp(new(pool) char[size + 1]);
146 _assert(CYRecvAll(client, temp, size));
147 temp[size] = '\0';
148 json = temp;
149 }
150 mode_ = Working;
151 }
152
153 return CYUTF8String(json, size);
154 }
155
156 static CYUTF8String Run(CYPool &pool, int client, const std::string &code) {
157 return Run(pool, client, CYUTF8String(code.c_str(), code.size()));
158 }
159
160 static std::ostream *out_;
161
162 static void Write(bool syntax, const char *data, size_t size, std::ostream &out) {
163 if (syntax)
164 CYLexerHighlight(data, size, out);
165 else
166 out.write(data, size);
167 }
168
169 static void Output(bool syntax, CYUTF8String json, std::ostream *out, bool expand = false) {
170 const char *data(json.data);
171 size_t size(json.size);
172
173 if (data == NULL || out == NULL)
174 return;
175
176 if (!expand ||
177 data[0] != '@' && data[0] != '"' && data[0] != '\'' ||
178 data[0] == '@' && data[1] != '"' && data[1] != '\''
179 )
180 Write(syntax, data, size, *out);
181 else for (size_t i(0); i != size; ++i)
182 if (data[i] != '\\')
183 *out << data[i];
184 else switch(data[++i]) {
185 case '\0': goto done;
186 case '\\': *out << '\\'; break;
187 case '\'': *out << '\''; break;
188 case '"': *out << '"'; break;
189 case 'b': *out << '\b'; break;
190 case 'f': *out << '\f'; break;
191 case 'n': *out << '\n'; break;
192 case 'r': *out << '\r'; break;
193 case 't': *out << '\t'; break;
194 case 'v': *out << '\v'; break;
195 default: *out << '\\'; --i; break;
196 }
197
198 done:
199 *out << std::endl;
200 }
201
202 static void Run(int client, bool syntax, const char *data, size_t size, std::ostream *out = NULL, bool expand = false) {
203 CYPool pool;
204 Output(syntax, Run(pool, client, CYUTF8String(data, size)), out, expand);
205 }
206
207 static void Run(int client, bool syntax, std::string &code, std::ostream *out = NULL, bool expand = false) {
208 Run(client, syntax, code.c_str(), code.size(), out, expand);
209 }
210
211 int (*append_history$)(int, const char *);
212
213 static std::string command_;
214
215 static CYExpression *ParseExpression(CYUTF8String code) {
216 std::stringstream stream;
217 stream << '(' << code << ')';
218 CYDriver driver(stream);
219
220 cy::parser parser(driver);
221 Setup(driver, parser);
222
223 if (parser.parse() != 0 || !driver.errors_.empty())
224 return NULL;
225
226 CYOptions options;
227 CYContext context(options);
228
229 // XXX: this could be replaced with a CYStatement::Primitive()
230 if (CYExpress *express = dynamic_cast<CYExpress *>(driver.program_->statements_))
231 return express->expression_->Primitive(context);
232
233 return NULL;
234 }
235
236 static int client_;
237
238 static char **Complete(const char *word, int start, int end) {
239 rl_attempted_completion_over = TRUE;
240
241 CYLocalPool pool;
242
243 std::string line(rl_line_buffer, start);
244 std::istringstream stream(command_ + line);
245 CYDriver driver(stream);
246
247 driver.auto_ = true;
248
249 cy::parser parser(driver);
250 Setup(driver, parser);
251
252 if (parser.parse() != 0 || !driver.errors_.empty())
253 return NULL;
254
255 if (driver.mode_ == CYDriver::AutoNone)
256 return NULL;
257
258 CYExpression *expression;
259
260 CYOptions options;
261 CYContext context(options);
262
263 std::ostringstream prefix;
264
265 switch (driver.mode_) {
266 case CYDriver::AutoPrimary:
267 expression = $ CYThis();
268 break;
269
270 case CYDriver::AutoDirect:
271 expression = driver.context_;
272 break;
273
274 case CYDriver::AutoIndirect:
275 expression = $ CYIndirect(driver.context_);
276 break;
277
278 case CYDriver::AutoMessage: {
279 CYDriver::Context &thing(driver.contexts_.back());
280 expression = $M($C1($V("object_getClass"), thing.context_), $S("messages"));
281 for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part)
282 prefix << (*part)->word_ << ':';
283 } break;
284
285 default:
286 _assert(false);
287 }
288
289 std::string begin(prefix.str());
290
291 driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression(
292 " function(object, prefix, word) {\n"
293 " var names = [];\n"
294 " var before = prefix.length;\n"
295 " prefix += word;\n"
296 " var entire = prefix.length;\n"
297 " for (name in object)\n"
298 " if (name.substring(0, entire) == prefix)\n"
299 " names.push(name.substr(before));\n"
300 " return names;\n"
301 " }\n"
302 ), expression, $S(begin.c_str()), $S(word))));
303
304 driver.program_->Replace(context);
305
306 std::ostringstream str;
307 CYOutput out(str, options);
308 out << *driver.program_;
309
310 std::string code(str.str());
311 CYUTF8String json(Run(pool, client_, code));
312 // XXX: if this fails we should not try to parse it
313
314 CYExpression *result(ParseExpression(json));
315 if (result == NULL)
316 return NULL;
317
318 CYArray *array(dynamic_cast<CYArray *>(result));
319
320 if (array == NULL) {
321 *out_ << '\n';
322 Output(false, json, out_);
323 rl_forced_update_display();
324 return NULL;
325 }
326
327 // XXX: use an std::set?
328 typedef std::vector<std::string> Completions;
329 Completions completions;
330
331 std::string common;
332 bool rest(false);
333
334 CYForEach (element, array->elements_) {
335 CYString *string(dynamic_cast<CYString *>(element->value_));
336 _assert(string != NULL);
337
338 std::string completion;
339 if (string->size_ != 0)
340 completion.assign(string->value_, string->size_);
341 else if (driver.mode_ == CYDriver::AutoMessage)
342 completion = "]";
343 else
344 continue;
345
346 completions.push_back(completion);
347
348 if (!rest) {
349 common = completion;
350 rest = true;
351 } else {
352 size_t limit(completion.size()), size(common.size());
353 if (size > limit)
354 common = common.substr(0, limit);
355 else
356 limit = size;
357 for (limit = 0; limit != size; ++limit)
358 if (common[limit] != completion[limit])
359 break;
360 if (limit != size)
361 common = common.substr(0, limit);
362 }
363 }
364
365 size_t count(completions.size());
366 if (count == 0)
367 return NULL;
368
369 size_t colon(common.find(':'));
370 if (colon != std::string::npos)
371 common = common.substr(0, colon + 1);
372 if (completions.size() == 1)
373 common += ' ';
374
375 char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count + 2))));
376
377 results[0] = strdup(common.c_str());
378 size_t index(0);
379 for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i)
380 results[++index] = strdup(i->c_str());
381 results[count + 1] = NULL;
382
383 return results;
384 }
385
386 // need char *, not const char *
387 static char name_[] = "cycript";
388 static char break_[] = " \t\n\"\\'`@$><=;|&{(" ")}" ".:[]";
389
390 class History {
391 private:
392 std::string histfile_;
393 size_t histlines_;
394
395 public:
396 History(std::string histfile) :
397 histfile_(histfile),
398 histlines_(0)
399 {
400 read_history(histfile_.c_str());
401 }
402
403 ~History() {
404 if (append_history$ != NULL) {
405 _syscall(close(_syscall(open(histfile_.c_str(), O_CREAT | O_WRONLY, 0600))));
406 _assert((*append_history$)(histlines_, histfile_.c_str()) == 0);
407 } else {
408 _assert(write_history(histfile_.c_str()) == 0);
409 }
410 }
411
412 void operator +=(const std::string &command) {
413 add_history(command_.c_str());
414 ++histlines_;
415 }
416 };
417
418 static void Console(CYOptions &options) {
419 CYPool pool;
420
421 passwd *passwd;
422 if (const char *username = getenv("LOGNAME"))
423 passwd = getpwnam(username);
424 else
425 passwd = getpwuid(getuid());
426
427 std::string basedir(passwd->pw_dir);
428 basedir += "/.cycript";
429 mkdir(basedir.c_str(), 0700);
430
431 rl_initialize();
432 rl_readline_name = name_;
433
434 History history(basedir + "/history");
435
436 bool bypass(false);
437 bool debug(false);
438 bool expand(false);
439 bool syntax(true);
440
441 out_ = &std::cout;
442
443 // rl_completer_word_break_characters is broken in libedit
444 rl_basic_word_break_characters = break_;
445
446 rl_completer_word_break_characters = break_;
447 rl_attempted_completion_function = &Complete;
448 rl_bind_key('\t', rl_complete);
449
450 struct sigaction action;
451 sigemptyset(&action.sa_mask);
452 action.sa_handler = &sigint;
453 action.sa_flags = 0;
454 sigaction(SIGINT, &action, NULL);
455
456 restart: for (;;) {
457 command_.clear();
458 std::vector<std::string> lines;
459
460 bool extra(false);
461 const char *prompt("cy# ");
462
463 if (setjmp(ctrlc_) != 0) {
464 mode_ = Working;
465 *out_ << std::endl;
466 goto restart;
467 }
468
469 read:
470
471 #if RL_READLINE_VERSION >= 0x0600
472 if (syntax) {
473 rl_prep_term_function = CYDisplayStart;
474 rl_redisplay_function = CYDisplayUpdate;
475 rl_deprep_term_function = CYDisplayFinish;
476 } else {
477 rl_prep_term_function = rl_prep_terminal;
478 rl_redisplay_function = rl_redisplay;
479 rl_deprep_term_function = rl_deprep_terminal;
480 }
481 #endif
482
483 mode_ = Parsing;
484 char *line(readline(prompt));
485 mode_ = Working;
486
487 if (line == NULL) {
488 *out_ << std::endl;
489 break;
490 } else if (line[0] == '\0')
491 goto read;
492
493 if (!extra) {
494 extra = true;
495 if (line[0] == '?') {
496 std::string data(line + 1);
497 if (data == "bypass") {
498 bypass = !bypass;
499 *out_ << "bypass == " << (bypass ? "true" : "false") << std::endl;
500 } else if (data == "debug") {
501 debug = !debug;
502 *out_ << "debug == " << (debug ? "true" : "false") << std::endl;
503 } else if (data == "expand") {
504 expand = !expand;
505 *out_ << "expand == " << (expand ? "true" : "false") << std::endl;
506 } else if (data == "syntax") {
507 syntax = !syntax;
508 *out_ << "syntax == " << (syntax ? "true" : "false") << std::endl;
509 }
510 command_ = line;
511 history += command_;
512 goto restart;
513 }
514 }
515
516 command_ += line;
517
518 char *begin(line), *end(line + strlen(line));
519 while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) {
520 *nl = '\0';
521 lines.push_back(begin);
522 begin = nl + 1;
523 }
524
525 lines.push_back(begin);
526
527 free(line);
528
529 std::string code;
530
531 if (bypass)
532 code = command_;
533 else {
534 CYLocalPool pool;
535
536 std::istringstream stream(command_);
537 CYDriver driver(stream);
538
539 cy::parser parser(driver);
540 Setup(driver, parser);
541
542 if (parser.parse() != 0 || !driver.errors_.empty()) {
543 for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) {
544 cy::position begin(error->location_.begin);
545 if (begin.line != lines.size() || begin.column < lines.back().size() || error->warning_) {
546 cy::position end(error->location_.end);
547
548 if (begin.line != lines.size()) {
549 std::cerr << " | ";
550 std::cerr << lines[begin.line - 1] << std::endl;
551 }
552
553 std::cerr << "....";
554 for (size_t i(0); i != begin.column; ++i)
555 std::cerr << '.';
556 if (begin.line != end.line || begin.column == end.column)
557 std::cerr << '^';
558 else for (size_t i(0), e(end.column - begin.column); i != e; ++i)
559 std::cerr << '^';
560 std::cerr << std::endl;
561
562 std::cerr << " | ";
563 std::cerr << error->message_ << std::endl;
564
565 history += command_;
566 goto restart;
567 }
568 }
569
570 driver.errors_.clear();
571
572 command_ += '\n';
573 prompt = "cy> ";
574 goto read;
575 }
576
577 if (driver.program_ == NULL)
578 goto restart;
579
580 std::ostringstream str;
581 CYOutput out(str, options);
582 Setup(out, driver, options);
583 out << *driver.program_;
584 code = str.str();
585 }
586
587 history += command_;
588
589 if (debug) {
590 Write(syntax, code.c_str(), code.size(), std::cout);
591 std::cout << std::endl;
592 }
593
594 Run(client_, syntax, code, out_, expand);
595 }
596 }
597
598 void InjectLibrary(pid_t pid);
599
600 int Main(int argc, char const * const argv[], char const * const envp[]) {
601 _aprcall(apr_initialize());
602
603 apr_pool_t *pool;
604 apr_pool_create(&pool, NULL);
605
606 bool tty(isatty(STDIN_FILENO));
607 bool compile(false);
608 CYOptions options;
609
610 append_history$ = (int (*)(int, const char *)) (dlsym(RTLD_DEFAULT, "append_history"));
611
612 #ifdef CY_ATTACH
613 pid_t pid(_not(pid_t));
614 #endif
615
616 const char *host(NULL);
617 const char *port(NULL);
618
619 apr_getopt_t *state;
620 _aprcall(apr_getopt_init(&state, pool, argc, argv));
621
622 for (;;) {
623 char opt;
624 const char *arg;
625
626 apr_status_t status(apr_getopt(state,
627 "cg:n:"
628 #ifdef CY_ATTACH
629 "p:"
630 #endif
631 "r:"
632 "s"
633 , &opt, &arg));
634
635 switch (status) {
636 case APR_EOF:
637 goto getopt;
638 case APR_BADCH:
639 case APR_BADARG:
640 fprintf(stderr,
641 "usage: cycript [-c]"
642 #ifdef CY_ATTACH
643 " [-p <pid|name>]"
644 #endif
645 " [-r <host:port>]"
646 " [<script> [<arg>...]]\n"
647 );
648 return 1;
649 default:
650 _aprcall(status);
651 }
652
653 switch (opt) {
654 case 'c':
655 compile = true;
656 break;
657
658 case 'g':
659 if (false);
660 else if (strcmp(arg, "rename") == 0)
661 options.verbose_ = true;
662 #if YYDEBUG
663 else if (strcmp(arg, "bison") == 0)
664 bison_ = true;
665 #endif
666 else {
667 fprintf(stderr, "invalid name for -g\n");
668 return 1;
669 }
670 break;
671
672 case 'n':
673 if (false);
674 else if (strcmp(arg, "minify") == 0)
675 pretty_ = true;
676 else {
677 fprintf(stderr, "invalid name for -n\n");
678 return 1;
679 }
680 break;
681
682 #ifdef CY_ATTACH
683 case 'p': {
684 size_t size(strlen(arg));
685 char *end;
686
687 pid = strtoul(arg, &end, 0);
688 if (arg + size != end) {
689 // XXX: arg needs to be escaped in some horrendous way of doom
690 const char *command(apr_pstrcat(pool, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^", arg, " /{s/^[^ ]* //;q;};};d'", NULL));
691
692 if (FILE *pids = popen(command, "r")) {
693 char value[32];
694 size = 0;
695
696 for (;;) {
697 size_t read(fread(value + size, 1, sizeof(value) - size, pids));
698 if (read == 0)
699 break;
700 else {
701 size += read;
702 if (size == sizeof(value)) {
703 pid = _not(pid_t);
704 goto fail;
705 }
706 }
707 }
708
709 size:
710 if (size == 0)
711 goto fail;
712 if (value[size - 1] == '\n') {
713 --size;
714 goto size;
715 }
716
717 value[size] = '\0';
718 size = strlen(value);
719 pid = strtoul(value, &end, 0);
720 if (value + size != end) fail:
721 pid = _not(pid_t);
722 _syscall(pclose(pids));
723 }
724
725 if (pid == _not(pid_t)) {
726 fprintf(stderr, "invalid pid for -p\n");
727 return 1;
728 }
729 }
730 } break;
731 #endif
732
733 case 'r': {
734 //size_t size(strlen(arg));
735
736 char *colon(strrchr(arg, ':'));
737 if (colon == NULL) {
738 fprintf(stderr, "missing colon in hostspec\n");
739 return 1;
740 }
741
742 /*char *end;
743 port = strtoul(colon + 1, &end, 10);
744 if (end != arg + size) {
745 fprintf(stderr, "invalid port in hostspec\n");
746 return 1;
747 }*/
748
749 host = arg;
750 *colon = '\0';
751 port = colon + 1;
752 } break;
753
754 case 's':
755 strict_ = true;
756 break;
757 }
758 } getopt:;
759
760 const char *script;
761 int ind(state->ind);
762
763 #ifdef CY_ATTACH
764 if (pid != _not(pid_t) && ind < argc - 1) {
765 fprintf(stderr, "-p cannot set argv\n");
766 return 1;
767 }
768
769 if (pid != _not(pid_t) && compile) {
770 fprintf(stderr, "-p conflicts with -c\n");
771 return 1;
772 }
773 #endif
774
775 if (ind == argc)
776 script = NULL;
777 else {
778 #ifdef CY_EXECUTE
779 // XXX: const_cast?! wtf gcc :(
780 CYSetArgs(argc - ind - 1, const_cast<const char **>(argv + ind + 1));
781 #endif
782 script = argv[ind];
783 if (strcmp(script, "-") == 0)
784 script = NULL;
785 }
786
787 #ifdef CY_ATTACH
788 if (pid != _not(pid_t) && script == NULL && !tty) {
789 fprintf(stderr, "non-terminal attaching to remote console\n");
790 return 1;
791 }
792 #endif
793
794 #ifdef CY_ATTACH
795 if (pid == _not(pid_t))
796 client_ = -1;
797 else {
798 int server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0))); try {
799 struct sockaddr_un address;
800 memset(&address, 0, sizeof(address));
801 address.sun_family = AF_UNIX;
802
803 sprintf(address.sun_path, "/tmp/.s.cy.%u", getpid());
804
805 _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
806 _syscall(chmod(address.sun_path, 0777));
807
808 try {
809 _syscall(listen(server, 1));
810 InjectLibrary(pid);
811 client_ = _syscall(accept(server, NULL, NULL));
812 } catch (...) {
813 // XXX: exception?
814 unlink(address.sun_path);
815 throw;
816 }
817 } catch (...) {
818 _syscall(close(server));
819 throw;
820 }
821 }
822 #else
823 client_ = -1;
824 #endif
825
826 if (client_ == -1 && host != NULL && port != NULL) {
827 struct addrinfo hints;
828 memset(&hints, 0, sizeof(hints));
829 hints.ai_family = AF_UNSPEC;
830 hints.ai_socktype = SOCK_STREAM;
831 hints.ai_protocol = 0;
832 hints.ai_flags = 0;
833
834 struct addrinfo *infos;
835 _syscall(getaddrinfo(host, port, &hints, &infos));
836
837 _assert(infos != NULL); try {
838 for (struct addrinfo *info(infos); info != NULL; info = info->ai_next) {
839 int client(_syscall(socket(info->ai_family, info->ai_socktype, info->ai_protocol))); try {
840 _syscall(connect(client, info->ai_addr, info->ai_addrlen));
841 client_ = client;
842 break;
843 } catch (...) {
844 _syscall(close(client));
845 throw;
846 }
847 }
848 } catch (...) {
849 freeaddrinfo(infos);
850 throw;
851 }
852 }
853
854 if (script == NULL && tty)
855 Console(options);
856 else {
857 CYLocalPool pool;
858
859 std::istream *stream;
860 if (script == NULL) {
861 stream = &std::cin;
862 script = "<stdin>";
863 } else {
864 stream = new std::fstream(script, std::ios::in | std::ios::binary);
865 _assert(!stream->fail());
866 }
867
868 CYDriver driver(*stream, script);
869 cy::parser parser(driver);
870 Setup(driver, parser);
871
872 if (parser.parse() != 0 || !driver.errors_.empty()) {
873 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
874 std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
875 } else if (driver.program_ != NULL) {
876 std::ostringstream str;
877 CYOutput out(str, options);
878 Setup(out, driver, options);
879 out << *driver.program_;
880 std::string code(str.str());
881 if (compile)
882 std::cout << code;
883 else
884 Run(client_, false, code, &std::cout);
885 }
886 }
887
888 apr_pool_destroy(pool);
889
890 return 0;
891 }
892
893 int main(int argc, char const * const argv[], char const * const envp[]) {
894 apr_status_t status(apr_app_initialize(&argc, &argv, &envp));
895
896 if (status != APR_SUCCESS) {
897 fprintf(stderr, "apr_app_initialize() != APR_SUCCESS\n");
898 return 1;
899 } else try {
900 return Main(argc, argv, envp);
901 } catch (const CYException &error) {
902 CYPool pool;
903 fprintf(stderr, "%s\n", error.PoolCString(pool));
904 return 1;
905 }
906 }