]> git.saurik.com Git - cycript.git/blob - Console.cpp
Correct "error: taking address of temporary array".
[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 <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
71 static volatile enum {
72 Working,
73 Parsing,
74 Running,
75 Sending,
76 Waiting,
77 } mode_;
78
79 static jmp_buf ctrlc_;
80
81 static 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
97 static bool bison_;
98 #endif
99 static bool strict_;
100 static bool pretty_;
101
102 void 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
111 void 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
118 static 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
155 static CYUTF8String Run(CYPool &pool, int client, const std::string &code) {
156 return Run(pool, client, CYUTF8String(code.c_str(), code.size()));
157 }
158
159 static std::ostream *out_;
160
161 static 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
168 static 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
201 static 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
206 static 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
210 int (*append_history$)(int, const char *);
211
212 static std::string command_;
213
214 static 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
238 static int client_;
239
240 static 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 *
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 int fd(_syscall(open(histfile_.c_str(), O_CREAT | O_WRONLY, 0600)));
407 _syscall(close(fd));
408 _assert((*append_history$)(histlines_, histfile_.c_str()) == 0);
409 } else {
410 _assert(write_history(histfile_.c_str()) == 0);
411 }
412 }
413
414 void operator +=(const std::string &command) {
415 add_history(command_.c_str());
416 ++histlines_;
417 }
418 };
419
420 static void Console(CYOptions &options) {
421 CYPool pool;
422
423 passwd *passwd;
424 if (const char *username = getenv("LOGNAME"))
425 passwd = getpwnam(username);
426 else
427 passwd = getpwuid(getuid());
428
429 std::string basedir(passwd->pw_dir);
430 basedir += "/.cycript";
431 mkdir(basedir.c_str(), 0700);
432
433 rl_initialize();
434 rl_readline_name = name_;
435
436 History history(basedir + "/history");
437
438 bool bypass(false);
439 bool debug(false);
440 bool expand(false);
441 bool lower(true);
442 bool syntax(true);
443
444 out_ = &std::cout;
445
446 // rl_completer_word_break_characters is broken in libedit
447 rl_basic_word_break_characters = break_;
448
449 rl_completer_word_break_characters = break_;
450 rl_attempted_completion_function = &Complete;
451 rl_bind_key('\t', rl_complete);
452
453 struct sigaction action;
454 sigemptyset(&action.sa_mask);
455 action.sa_handler = &sigint;
456 action.sa_flags = 0;
457 sigaction(SIGINT, &action, NULL);
458
459 restart: for (;;) {
460 command_.clear();
461 std::vector<std::string> lines;
462
463 bool extra(false);
464 const char *prompt("cy# ");
465
466 if (setjmp(ctrlc_) != 0) {
467 mode_ = Working;
468 *out_ << std::endl;
469 goto restart;
470 }
471
472 read:
473
474 #if RL_READLINE_VERSION >= 0x0600
475 if (syntax)
476 rl_redisplay_function = CYDisplayUpdate;
477 else
478 rl_redisplay_function = rl_redisplay;
479 #endif
480
481 mode_ = Parsing;
482 char *line(readline(prompt));
483 mode_ = Working;
484
485 if (line == NULL) {
486 *out_ << std::endl;
487 break;
488 } else if (line[0] == '\0')
489 goto read;
490
491 if (!extra) {
492 extra = true;
493 if (line[0] == '?') {
494 std::string data(line + 1);
495 if (data == "bypass") {
496 bypass = !bypass;
497 *out_ << "bypass == " << (bypass ? "true" : "false") << std::endl;
498 } else if (data == "debug") {
499 debug = !debug;
500 *out_ << "debug == " << (debug ? "true" : "false") << std::endl;
501 } else if (data == "destroy") {
502 CYDestroyContext();
503 } else if (data == "gc") {
504 *out_ << "collecting... " << std::flush;
505 CYGarbageCollect(CYGetJSContext());
506 *out_ << "done." << std::endl;
507 } else if (data == "exit") {
508 return;
509 } else if (data == "expand") {
510 expand = !expand;
511 *out_ << "expand == " << (expand ? "true" : "false") << std::endl;
512 } else if (data == "lower") {
513 lower = !lower;
514 *out_ << "lower == " << (lower ? "true" : "false") << std::endl;
515 } else if (data == "syntax") {
516 syntax = !syntax;
517 *out_ << "syntax == " << (syntax ? "true" : "false") << std::endl;
518 }
519 command_ = line;
520 history += command_;
521 goto restart;
522 }
523 }
524
525 command_ += line;
526
527 char *begin(line), *end(line + strlen(line));
528 while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) {
529 *nl = '\0';
530 lines.push_back(begin);
531 begin = nl + 1;
532 }
533
534 lines.push_back(begin);
535
536 free(line);
537
538 std::string code;
539
540 if (bypass)
541 code = command_;
542 else {
543 CYLocalPool pool;
544
545 std::istringstream stream(command_);
546 CYDriver driver(stream);
547
548 cy::parser parser(driver);
549 Setup(driver, parser);
550
551 if (parser.parse() != 0 || !driver.errors_.empty()) {
552 for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) {
553 cy::position begin(error->location_.begin);
554 if (begin.line != lines.size() || begin.column < lines.back().size() || error->warning_) {
555 cy::position end(error->location_.end);
556
557 if (begin.line != lines.size()) {
558 std::cerr << " | ";
559 std::cerr << lines[begin.line - 1] << std::endl;
560 }
561
562 std::cerr << "....";
563 for (size_t i(0); i != begin.column; ++i)
564 std::cerr << '.';
565 if (begin.line != end.line || begin.column == end.column)
566 std::cerr << '^';
567 else for (size_t i(0), e(end.column - begin.column); i != e; ++i)
568 std::cerr << '^';
569 std::cerr << std::endl;
570
571 std::cerr << " | ";
572 std::cerr << error->message_ << std::endl;
573
574 history += command_;
575 goto restart;
576 }
577 }
578
579 driver.errors_.clear();
580
581 command_ += '\n';
582 prompt = "cy> ";
583 goto read;
584 }
585
586 if (driver.program_ == NULL)
587 goto restart;
588
589 std::ostringstream str;
590 CYOutput out(str, options);
591 Setup(out, driver, options, lower);
592 out << *driver.program_;
593 code = str.str();
594 }
595
596 history += command_;
597
598 if (debug) {
599 std::cout << "cy= ";
600 Write(syntax, code.c_str(), code.size(), std::cout);
601 std::cout << std::endl;
602 }
603
604 Run(client_, syntax, code, out_, expand);
605 }
606 }
607
608 void InjectLibrary(pid_t pid);
609
610 int Main(int argc, char * const argv[], char const * const envp[]) {
611 bool tty(isatty(STDIN_FILENO));
612 bool compile(false);
613 bool target(false);
614 CYOptions options;
615
616 append_history$ = (int (*)(int, const char *)) (dlsym(RTLD_DEFAULT, "append_history"));
617
618 #ifdef CY_ATTACH
619 pid_t pid(_not(pid_t));
620 #endif
621
622 const char *host(NULL);
623 const char *port(NULL);
624
625 optind = 1;
626
627 for (;;) {
628 int option(getopt_long(argc, argv,
629 "c"
630 "g:"
631 "n:"
632 #ifdef CY_ATTACH
633 "p:"
634 #endif
635 "r:"
636 "s"
637 , (const struct option[]) {
638 {NULL, no_argument, NULL, 'c'},
639 {NULL, required_argument, NULL, 'g'},
640 {NULL, required_argument, NULL, 'n'},
641 #ifdef CY_ATTACH
642 {NULL, required_argument, NULL, 'p'},
643 #endif
644 {NULL, required_argument, NULL, 'r'},
645 {NULL, no_argument, NULL, 's'},
646 {0, 0, 0, 0}}, NULL));
647
648 switch (option) {
649 case -1:
650 goto getopt;
651
652 case ':':
653 case '?':
654 fprintf(stderr,
655 "usage: cycript [-c]"
656 #ifdef CY_ATTACH
657 " [-p <pid|name>]"
658 #endif
659 " [-r <host:port>]"
660 " [<script> [<arg>...]]\n"
661 );
662 return 1;
663
664 target:
665 if (!target)
666 target = true;
667 else {
668 fprintf(stderr, "only one of -[c"
669 #ifdef CY_ATTACH
670 "p"
671 #endif
672 "r] may be used at a time\n");
673 return 1;
674 }
675 break;
676
677 case 'c':
678 compile = true;
679 goto target;
680
681 case 'g':
682 if (false);
683 else if (strcmp(optarg, "rename") == 0)
684 options.verbose_ = true;
685 #if YYDEBUG
686 else if (strcmp(optarg, "bison") == 0)
687 bison_ = true;
688 #endif
689 else {
690 fprintf(stderr, "invalid name for -g\n");
691 return 1;
692 }
693 break;
694
695 case 'n':
696 if (false);
697 else if (strcmp(optarg, "minify") == 0)
698 pretty_ = true;
699 else {
700 fprintf(stderr, "invalid name for -n\n");
701 return 1;
702 }
703 break;
704
705 #ifdef CY_ATTACH
706 case 'p': {
707 size_t size(strlen(optarg));
708 char *end;
709
710 pid = strtoul(optarg, &end, 0);
711 if (optarg + size != end) {
712 // XXX: arg needs to be escaped in some horrendous way of doom
713 // XXX: this is a memory leak now because I just don't care enough
714 char *command;
715 asprintf(&command, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", optarg);
716
717 if (FILE *pids = popen(command, "r")) {
718 char value[32];
719 size = 0;
720
721 for (;;) {
722 size_t read(fread(value + size, 1, sizeof(value) - size, pids));
723 if (read == 0)
724 break;
725 else {
726 size += read;
727 if (size == sizeof(value))
728 goto fail;
729 }
730 }
731
732 size:
733 if (size == 0)
734 goto fail;
735 if (value[size - 1] == '\n') {
736 --size;
737 goto size;
738 }
739
740 value[size] = '\0';
741 size = strlen(value);
742 pid = strtoul(value, &end, 0);
743 if (value + size != end) fail:
744 pid = _not(pid_t);
745 _syscall(pclose(pids));
746 }
747
748 if (pid == _not(pid_t)) {
749 fprintf(stderr, "unable to find process `%s' using ps\n", optarg);
750 return 1;
751 }
752 }
753 } goto target;
754 #endif
755
756 case 'r': {
757 //size_t size(strlen(optarg));
758
759 char *colon(strrchr(optarg, ':'));
760 if (colon == NULL) {
761 fprintf(stderr, "missing colon in hostspec\n");
762 return 1;
763 }
764
765 /*char *end;
766 port = strtoul(colon + 1, &end, 10);
767 if (end != optarg + size) {
768 fprintf(stderr, "invalid port in hostspec\n");
769 return 1;
770 }*/
771
772 host = optarg;
773 *colon = '\0';
774 port = colon + 1;
775 } goto target;
776
777 case 's':
778 strict_ = true;
779 break;
780
781 default:
782 _assert(false);
783 }
784 }
785
786 getopt:
787 argc -= optind;
788 argv += optind;
789
790 const char *script;
791
792 #ifdef CY_ATTACH
793 if (pid != _not(pid_t) && argc > 1) {
794 fprintf(stderr, "-p cannot set argv\n");
795 return 1;
796 }
797 #endif
798
799 if (argc == 0)
800 script = NULL;
801 else {
802 #ifdef CY_EXECUTE
803 // XXX: const_cast?! wtf gcc :(
804 CYSetArgs(argc - 1, const_cast<const char **>(argv + 1));
805 #endif
806 script = argv[0];
807 if (strcmp(script, "-") == 0)
808 script = NULL;
809 }
810
811 #ifdef CY_ATTACH
812 if (pid == _not(pid_t))
813 client_ = -1;
814 else {
815 struct Socket {
816 int fd_;
817
818 Socket(int fd) :
819 fd_(fd)
820 {
821 }
822
823 ~Socket() {
824 close(fd_);
825 }
826
827 operator int() {
828 return fd_;
829 }
830 } server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0)));
831
832 struct sockaddr_un address;
833 memset(&address, 0, sizeof(address));
834 address.sun_family = AF_UNIX;
835
836 sprintf(address.sun_path, "/tmp/.s.cy.%u", getpid());
837 unlink(address.sun_path);
838
839 struct File {
840 const char *path_;
841
842 File(const char *path) :
843 path_(path)
844 {
845 }
846
847 ~File() {
848 unlink(path_);
849 }
850 } file(address.sun_path);
851
852 _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
853 _syscall(chmod(address.sun_path, 0777));
854
855 _syscall(listen(server, 1));
856 InjectLibrary(pid);
857 client_ = _syscall(accept(server, NULL, NULL));
858 }
859 #else
860 client_ = -1;
861 #endif
862
863 if (client_ == -1 && host != NULL && port != NULL) {
864 struct addrinfo hints;
865 memset(&hints, 0, sizeof(hints));
866 hints.ai_family = AF_UNSPEC;
867 hints.ai_socktype = SOCK_STREAM;
868 hints.ai_protocol = 0;
869 hints.ai_flags = 0;
870
871 struct addrinfo *infos;
872 _syscall(getaddrinfo(host, port, &hints, &infos));
873
874 _assert(infos != NULL); try {
875 for (struct addrinfo *info(infos); info != NULL; info = info->ai_next) {
876 int client(_syscall(socket(info->ai_family, info->ai_socktype, info->ai_protocol))); try {
877 _syscall(connect(client, info->ai_addr, info->ai_addrlen));
878 client_ = client;
879 break;
880 } catch (...) {
881 _syscall(close(client));
882 throw;
883 }
884 }
885 } catch (...) {
886 freeaddrinfo(infos);
887 throw;
888 }
889 }
890
891 if (script == NULL && tty)
892 Console(options);
893 else {
894 CYLocalPool pool;
895
896 std::istream *stream;
897 if (script == NULL) {
898 stream = &std::cin;
899 script = "<stdin>";
900 } else {
901 stream = new std::fstream(script, std::ios::in | std::ios::binary);
902 _assert(!stream->fail());
903 }
904
905 CYDriver driver(*stream, script);
906 cy::parser parser(driver);
907 Setup(driver, parser);
908
909 if (parser.parse() != 0 || !driver.errors_.empty()) {
910 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
911 std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
912 } else if (driver.program_ != NULL) {
913 std::ostringstream str;
914 CYOutput out(str, options);
915 Setup(out, driver, options, true);
916 out << *driver.program_;
917 std::string code(str.str());
918 if (compile)
919 std::cout << code;
920 else
921 Run(client_, false, code, &std::cout);
922 }
923 }
924
925 return 0;
926 }
927
928 int main(int argc, char * const argv[], char const * const envp[]) {
929 try {
930 return Main(argc, argv, envp);
931 } catch (const CYException &error) {
932 CYPool pool;
933 fprintf(stderr, "%s\n", error.PoolCString(pool));
934 return 1;
935 }
936 }