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