]> git.saurik.com Git - cycript.git/blame_incremental - Console.cpp
Give Xcode/Apple/iOS build scripts a common prefix.
[cycript.git] / Console.cpp
... / ...
CommitLineData
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 <getopt.h>
48#include <signal.h>
49#include <unistd.h>
50
51#include <sys/socket.h>
52#include <sys/types.h>
53#include <sys/stat.h>
54#include <fcntl.h>
55#include <netdb.h>
56
57#include <sys/types.h>
58#include <sys/socket.h>
59#include <netinet/in.h>
60#include <sys/un.h>
61#include <pwd.h>
62
63#include <dlfcn.h>
64
65#include "Display.hpp"
66#include "Replace.hpp"
67
68#include "Cycript.tab.hh"
69#include "Driver.hpp"
70
71static volatile enum {
72 Working,
73 Parsing,
74 Running,
75 Sending,
76 Waiting,
77} mode_;
78
79static jmp_buf ctrlc_;
80
81static void sigint(int) {
82 switch (mode_) {
83 case Working:
84 return;
85 case Parsing:
86 longjmp(ctrlc_, 1);
87 case Running:
88 throw "*** Ctrl-C";
89 case Sending:
90 return;
91 case Waiting:
92 return;
93 }
94}
95
96#if YYDEBUG
97static bool bison_;
98#endif
99static bool strict_;
100static bool pretty_;
101
102void Setup(CYDriver &driver, cy::parser &parser) {
103#if YYDEBUG
104 if (bison_)
105 parser.set_debug_level(1);
106#endif
107 if (strict_)
108 driver.strict_ = true;
109}
110
111void Setup(CYOutput &out, CYDriver &driver, CYOptions &options, bool lower) {
112 out.pretty_ = pretty_;
113 CYContext context(options);
114 if (lower)
115 driver.program_->Replace(context);
116}
117
118static CYUTF8String Run(CYPool &pool, int client, CYUTF8String code) {
119 const char *json;
120 uint32_t size;
121
122 if (client == -1) {
123 mode_ = Running;
124#ifdef CY_EXECUTE
125 json = CYExecute(CYGetJSContext(), pool, code);
126#else
127 json = NULL;
128#endif
129 mode_ = Working;
130 if (json == NULL)
131 size = 0;
132 else
133 size = strlen(json);
134 } else {
135 mode_ = Sending;
136 size = code.size;
137 _assert(CYSendAll(client, &size, sizeof(size)));
138 _assert(CYSendAll(client, code.data, code.size));
139 mode_ = Waiting;
140 _assert(CYRecvAll(client, &size, sizeof(size)));
141 if (size == _not(uint32_t))
142 json = NULL;
143 else {
144 char *temp(new(pool) char[size + 1]);
145 _assert(CYRecvAll(client, temp, size));
146 temp[size] = '\0';
147 json = temp;
148 }
149 mode_ = Working;
150 }
151
152 return CYUTF8String(json, size);
153}
154
155static CYUTF8String Run(CYPool &pool, int client, const std::string &code) {
156 return Run(pool, client, CYUTF8String(code.c_str(), code.size()));
157}
158
159static std::ostream *out_;
160
161static void Write(bool syntax, const char *data, size_t size, std::ostream &out) {
162 if (syntax)
163 CYLexerHighlight(data, size, out);
164 else
165 out.write(data, size);
166}
167
168static void Output(bool syntax, CYUTF8String json, std::ostream *out, bool expand = false) {
169 const char *data(json.data);
170 size_t size(json.size);
171
172 if (data == NULL || out == NULL)
173 return;
174
175 if (!expand ||
176 data[0] != '@' && data[0] != '"' && data[0] != '\'' ||
177 data[0] == '@' && data[1] != '"' && data[1] != '\''
178 )
179 Write(syntax, data, size, *out);
180 else for (size_t i(0); i != size; ++i)
181 if (data[i] != '\\')
182 *out << data[i];
183 else switch(data[++i]) {
184 case '\0': goto done;
185 case '\\': *out << '\\'; break;
186 case '\'': *out << '\''; break;
187 case '"': *out << '"'; break;
188 case 'b': *out << '\b'; break;
189 case 'f': *out << '\f'; break;
190 case 'n': *out << '\n'; break;
191 case 'r': *out << '\r'; break;
192 case 't': *out << '\t'; break;
193 case 'v': *out << '\v'; break;
194 default: *out << '\\'; --i; break;
195 }
196
197 done:
198 *out << std::endl;
199}
200
201static void Run(int client, bool syntax, const char *data, size_t size, std::ostream *out = NULL, bool expand = false) {
202 CYPool pool;
203 Output(syntax, Run(pool, client, CYUTF8String(data, size)), out, expand);
204}
205
206static void Run(int client, bool syntax, std::string &code, std::ostream *out = NULL, bool expand = false) {
207 Run(client, syntax, code.c_str(), code.size(), out, expand);
208}
209
210int (*append_history$)(int, const char *);
211
212static std::string command_;
213
214static CYExpression *ParseExpression(CYUTF8String code) {
215 std::stringstream stream;
216 stream << '(' << code << ')';
217 CYDriver driver(stream);
218
219 cy::parser parser(driver);
220 Setup(driver, parser);
221
222 if (parser.parse() != 0 || !driver.errors_.empty())
223 return NULL;
224
225 CYOptions options;
226 CYContext context(options);
227
228 CYStatement *statement(driver.program_->statements_);
229 _assert(statement != NULL);
230 _assert(statement->next_ == NULL);
231
232 CYExpress *express(dynamic_cast<CYExpress *>(driver.program_->statements_));
233 _assert(express != NULL);
234
235 return express->expression_;
236}
237
238static int client_;
239
240static char **Complete(const char *word, int start, int end) {
241 rl_attempted_completion_over = ~0;
242
243 CYLocalPool pool;
244
245 std::string line(rl_line_buffer, start);
246 std::istringstream stream(command_ + line);
247 CYDriver driver(stream);
248
249 driver.auto_ = true;
250
251 cy::parser parser(driver);
252 Setup(driver, parser);
253
254 if (parser.parse() != 0 || !driver.errors_.empty())
255 return NULL;
256
257 if (driver.mode_ == CYDriver::AutoNone)
258 return NULL;
259
260 CYExpression *expression;
261
262 CYOptions options;
263 CYContext context(options);
264
265 std::ostringstream prefix;
266
267 switch (driver.mode_) {
268 case CYDriver::AutoPrimary:
269 expression = $ CYThis();
270 break;
271
272 case CYDriver::AutoDirect:
273 expression = driver.context_;
274 break;
275
276 case CYDriver::AutoIndirect:
277 expression = $ CYIndirect(driver.context_);
278 break;
279
280 case CYDriver::AutoMessage: {
281 CYDriver::Context &thing(driver.contexts_.back());
282 expression = $M($C1($V("object_getClass"), thing.context_), $S("messages"));
283 for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part)
284 prefix << (*part)->word_ << ':';
285 } break;
286
287 default:
288 _assert(false);
289 }
290
291 std::string begin(prefix.str());
292
293 driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression(
294 " function(object, prefix, word) {\n"
295 " var names = [];\n"
296 " var before = prefix.length;\n"
297 " prefix += word;\n"
298 " var entire = prefix.length;\n"
299 " for (var name in object)\n"
300 " if (name.substring(0, entire) == prefix)\n"
301 " names.push(name.substr(before));\n"
302 " return names;\n"
303 " }\n"
304 ), expression, $S(begin.c_str()), $S(word))));
305
306 driver.program_->Replace(context);
307
308 std::ostringstream str;
309 CYOutput out(str, options);
310 out << *driver.program_;
311
312 std::string code(str.str());
313 CYUTF8String json(Run(pool, client_, code));
314 // XXX: if this fails we should not try to parse it
315
316 CYExpression *result(ParseExpression(json));
317 if (result == NULL)
318 return NULL;
319
320 CYArray *array(dynamic_cast<CYArray *>(result->Primitive(context)));
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 *
388static char name_[] = "cycript";
389static char break_[] = " \t\n\"\\'`@$><=;|&{(" ")}" ".:[]";
390
391class 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
419static 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
607void InjectLibrary(pid_t pid);
608
609int Main(int argc, char * const argv[], char const * const envp[]) {
610 bool tty(isatty(STDIN_FILENO));
611 bool compile(false);
612 bool target(false);
613 CYOptions options;
614
615 append_history$ = (int (*)(int, const char *)) (dlsym(RTLD_DEFAULT, "append_history"));
616
617#ifdef CY_ATTACH
618 pid_t pid(_not(pid_t));
619#endif
620
621 const char *host(NULL);
622 const char *port(NULL);
623
624 optind = 1;
625
626 for (;;) {
627 int option(getopt_long(argc, argv,
628 "c"
629 "g:"
630 "n:"
631#ifdef CY_ATTACH
632 "p:"
633#endif
634 "r:"
635 "s"
636 , (struct option[]) {
637 {NULL, no_argument, NULL, 'c'},
638 {NULL, required_argument, NULL, 'g'},
639 {NULL, required_argument, NULL, 'n'},
640#ifdef CY_ATTACH
641 {NULL, required_argument, NULL, 'p'},
642#endif
643 {NULL, required_argument, NULL, 'r'},
644 {NULL, no_argument, NULL, 's'},
645 {0, 0, 0, 0}}, NULL));
646
647 switch (option) {
648 case -1:
649 goto getopt;
650
651 case ':':
652 case '?':
653 fprintf(stderr,
654 "usage: cycript [-c]"
655#ifdef CY_ATTACH
656 " [-p <pid|name>]"
657#endif
658 " [-r <host:port>]"
659 " [<script> [<arg>...]]\n"
660 );
661 return 1;
662
663 target:
664 if (!target)
665 target = true;
666 else {
667 fprintf(stderr, "only one of -[c"
668#ifdef CY_ATTACH
669 "p"
670#endif
671 "r] may be used at a time\n");
672 return 1;
673 }
674 break;
675
676 case 'c':
677 compile = true;
678 goto target;
679
680 case 'g':
681 if (false);
682 else if (strcmp(optarg, "rename") == 0)
683 options.verbose_ = true;
684#if YYDEBUG
685 else if (strcmp(optarg, "bison") == 0)
686 bison_ = true;
687#endif
688 else {
689 fprintf(stderr, "invalid name for -g\n");
690 return 1;
691 }
692 break;
693
694 case 'n':
695 if (false);
696 else if (strcmp(optarg, "minify") == 0)
697 pretty_ = true;
698 else {
699 fprintf(stderr, "invalid name for -n\n");
700 return 1;
701 }
702 break;
703
704#ifdef CY_ATTACH
705 case 'p': {
706 size_t size(strlen(optarg));
707 char *end;
708
709 pid = strtoul(optarg, &end, 0);
710 if (optarg + size != end) {
711 // XXX: arg needs to be escaped in some horrendous way of doom
712 // XXX: this is a memory leak now because I just don't care enough
713 char *command;
714 asprintf(&command, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", optarg);
715
716 if (FILE *pids = popen(command, "r")) {
717 char value[32];
718 size = 0;
719
720 for (;;) {
721 size_t read(fread(value + size, 1, sizeof(value) - size, pids));
722 if (read == 0)
723 break;
724 else {
725 size += read;
726 if (size == sizeof(value))
727 goto fail;
728 }
729 }
730
731 size:
732 if (size == 0)
733 goto fail;
734 if (value[size - 1] == '\n') {
735 --size;
736 goto size;
737 }
738
739 value[size] = '\0';
740 size = strlen(value);
741 pid = strtoul(value, &end, 0);
742 if (value + size != end) fail:
743 pid = _not(pid_t);
744 _syscall(pclose(pids));
745 }
746
747 if (pid == _not(pid_t)) {
748 fprintf(stderr, "unable to find process `%s' using ps\n", optarg);
749 return 1;
750 }
751 }
752 } goto target;
753#endif
754
755 case 'r': {
756 //size_t size(strlen(optarg));
757
758 char *colon(strrchr(optarg, ':'));
759 if (colon == NULL) {
760 fprintf(stderr, "missing colon in hostspec\n");
761 return 1;
762 }
763
764 /*char *end;
765 port = strtoul(colon + 1, &end, 10);
766 if (end != optarg + size) {
767 fprintf(stderr, "invalid port in hostspec\n");
768 return 1;
769 }*/
770
771 host = optarg;
772 *colon = '\0';
773 port = colon + 1;
774 } goto target;
775
776 case 's':
777 strict_ = true;
778 break;
779
780 default:
781 _assert(false);
782 }
783 }
784
785 getopt:
786 argc -= optind;
787 argv += optind;
788
789 const char *script;
790
791#ifdef CY_ATTACH
792 if (pid != _not(pid_t) && argc > 1) {
793 fprintf(stderr, "-p cannot set argv\n");
794 return 1;
795 }
796#endif
797
798 if (argc == 0)
799 script = NULL;
800 else {
801#ifdef CY_EXECUTE
802 // XXX: const_cast?! wtf gcc :(
803 CYSetArgs(argc - 1, const_cast<const char **>(argv + 1));
804#endif
805 script = argv[0];
806 if (strcmp(script, "-") == 0)
807 script = NULL;
808 }
809
810#ifdef CY_ATTACH
811 if (pid == _not(pid_t))
812 client_ = -1;
813 else {
814 struct Socket {
815 int fd_;
816
817 Socket(int fd) :
818 fd_(fd)
819 {
820 }
821
822 ~Socket() {
823 close(fd_);
824 }
825
826 operator int() {
827 return fd_;
828 }
829 } server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0)));
830
831 struct sockaddr_un address;
832 memset(&address, 0, sizeof(address));
833 address.sun_family = AF_UNIX;
834
835 sprintf(address.sun_path, "/tmp/.s.cy.%u", getpid());
836 unlink(address.sun_path);
837
838 struct File {
839 const char *path_;
840
841 File(const char *path) :
842 path_(path)
843 {
844 }
845
846 ~File() {
847 unlink(path_);
848 }
849 } file(address.sun_path);
850
851 _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
852 _syscall(chmod(address.sun_path, 0777));
853
854 _syscall(listen(server, 1));
855 InjectLibrary(pid);
856 client_ = _syscall(accept(server, NULL, NULL));
857 }
858#else
859 client_ = -1;
860#endif
861
862 if (client_ == -1 && host != NULL && port != NULL) {
863 struct addrinfo hints;
864 memset(&hints, 0, sizeof(hints));
865 hints.ai_family = AF_UNSPEC;
866 hints.ai_socktype = SOCK_STREAM;
867 hints.ai_protocol = 0;
868 hints.ai_flags = 0;
869
870 struct addrinfo *infos;
871 _syscall(getaddrinfo(host, port, &hints, &infos));
872
873 _assert(infos != NULL); try {
874 for (struct addrinfo *info(infos); info != NULL; info = info->ai_next) {
875 int client(_syscall(socket(info->ai_family, info->ai_socktype, info->ai_protocol))); try {
876 _syscall(connect(client, info->ai_addr, info->ai_addrlen));
877 client_ = client;
878 break;
879 } catch (...) {
880 _syscall(close(client));
881 throw;
882 }
883 }
884 } catch (...) {
885 freeaddrinfo(infos);
886 throw;
887 }
888 }
889
890 if (script == NULL && tty)
891 Console(options);
892 else {
893 CYLocalPool pool;
894
895 std::istream *stream;
896 if (script == NULL) {
897 stream = &std::cin;
898 script = "<stdin>";
899 } else {
900 stream = new std::fstream(script, std::ios::in | std::ios::binary);
901 _assert(!stream->fail());
902 }
903
904 CYDriver driver(*stream, script);
905 cy::parser parser(driver);
906 Setup(driver, parser);
907
908 if (parser.parse() != 0 || !driver.errors_.empty()) {
909 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
910 std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
911 } else if (driver.program_ != NULL) {
912 std::ostringstream str;
913 CYOutput out(str, options);
914 Setup(out, driver, options, true);
915 out << *driver.program_;
916 std::string code(str.str());
917 if (compile)
918 std::cout << code;
919 else
920 Run(client_, false, code, &std::cout);
921 }
922 }
923
924 return 0;
925}
926
927int main(int argc, char * const argv[], char const * const envp[]) {
928 try {
929 return Main(argc, argv, envp);
930 } catch (const CYException &error) {
931 CYPool pool;
932 fprintf(stderr, "%s\n", error.PoolCString(pool));
933 return 1;
934 }
935}