]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Optimizing JavaScript Compiler/Runtime | |
2 | * Copyright (C) 2009-2015 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
5 | /* GNU Affero General Public License, Version 3 {{{ */ | |
6 | /* | |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU Affero General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | ||
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU Affero General Public License for more details. | |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
18 | * along with this program. 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 | CYCancel(); | |
89 | return; | |
90 | case Sending: | |
91 | return; | |
92 | case Waiting: | |
93 | return; | |
94 | } | |
95 | } | |
96 | ||
97 | #if YYDEBUG | |
98 | static bool bison_; | |
99 | #endif | |
100 | static bool strict_; | |
101 | static bool pretty_; | |
102 | ||
103 | void Setup(CYDriver &driver, cy::parser &parser) { | |
104 | #if YYDEBUG | |
105 | if (bison_) | |
106 | parser.set_debug_level(1); | |
107 | #endif | |
108 | if (strict_) | |
109 | driver.strict_ = true; | |
110 | } | |
111 | ||
112 | void Setup(CYOutput &out, CYDriver &driver, CYOptions &options, bool lower) { | |
113 | out.pretty_ = pretty_; | |
114 | CYContext context(options); | |
115 | if (lower) | |
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 | CYStatement *statement(driver.program_->statements_); | |
230 | _assert(statement != NULL); | |
231 | _assert(statement->next_ == NULL); | |
232 | ||
233 | CYExpress *express(dynamic_cast<CYExpress *>(driver.program_->statements_)); | |
234 | _assert(express != NULL); | |
235 | ||
236 | return express->expression_; | |
237 | } | |
238 | ||
239 | static int client_; | |
240 | ||
241 | static char **Complete(const char *word, int start, int end) { | |
242 | rl_attempted_completion_over = ~0; | |
243 | ||
244 | CYLocalPool pool; | |
245 | ||
246 | std::string line(rl_line_buffer, start); | |
247 | std::istringstream stream(command_ + line); | |
248 | CYDriver driver(stream); | |
249 | ||
250 | driver.auto_ = true; | |
251 | ||
252 | cy::parser parser(driver); | |
253 | Setup(driver, parser); | |
254 | ||
255 | if (parser.parse() != 0 || !driver.errors_.empty()) | |
256 | return NULL; | |
257 | ||
258 | if (driver.mode_ == CYDriver::AutoNone) | |
259 | return NULL; | |
260 | ||
261 | CYExpression *expression; | |
262 | ||
263 | CYOptions options; | |
264 | CYContext context(options); | |
265 | ||
266 | std::ostringstream prefix; | |
267 | ||
268 | switch (driver.mode_) { | |
269 | case CYDriver::AutoPrimary: | |
270 | expression = $ CYThis(); | |
271 | break; | |
272 | ||
273 | case CYDriver::AutoDirect: | |
274 | expression = driver.context_; | |
275 | break; | |
276 | ||
277 | case CYDriver::AutoIndirect: | |
278 | expression = $ CYIndirect(driver.context_); | |
279 | break; | |
280 | ||
281 | case CYDriver::AutoMessage: { | |
282 | CYDriver::Context &thing(driver.contexts_.back()); | |
283 | expression = $M($C1($V("object_getClass"), thing.context_), $S("messages")); | |
284 | for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part) | |
285 | prefix << (*part)->word_ << ':'; | |
286 | } break; | |
287 | ||
288 | default: | |
289 | _assert(false); | |
290 | } | |
291 | ||
292 | std::string begin(prefix.str()); | |
293 | ||
294 | driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression( | |
295 | " function(object, prefix, word) {\n" | |
296 | " var names = [];\n" | |
297 | " var before = prefix.length;\n" | |
298 | " prefix += word;\n" | |
299 | " var entire = prefix.length;\n" | |
300 | " for (var name in object)\n" | |
301 | " if (name.substring(0, entire) == prefix)\n" | |
302 | " names.push(name.substr(before));\n" | |
303 | " return names;\n" | |
304 | " }\n" | |
305 | ), expression, $S(begin.c_str()), $S(word)))); | |
306 | ||
307 | driver.program_->Replace(context); | |
308 | ||
309 | std::ostringstream str; | |
310 | CYOutput out(str, options); | |
311 | out << *driver.program_; | |
312 | ||
313 | std::string code(str.str()); | |
314 | CYUTF8String json(Run(pool, client_, code)); | |
315 | // XXX: if this fails we should not try to parse it | |
316 | ||
317 | CYExpression *result(ParseExpression(json)); | |
318 | if (result == NULL) | |
319 | return NULL; | |
320 | ||
321 | CYArray *array(dynamic_cast<CYArray *>(result->Primitive(context))); | |
322 | if (array == NULL) { | |
323 | *out_ << '\n'; | |
324 | Output(false, json, out_); | |
325 | rl_forced_update_display(); | |
326 | return NULL; | |
327 | } | |
328 | ||
329 | // XXX: use an std::set? | |
330 | typedef std::vector<std::string> Completions; | |
331 | Completions completions; | |
332 | ||
333 | std::string common; | |
334 | bool rest(false); | |
335 | ||
336 | CYForEach (element, array->elements_) { | |
337 | CYString *string(dynamic_cast<CYString *>(element->value_)); | |
338 | _assert(string != NULL); | |
339 | ||
340 | std::string completion; | |
341 | if (string->size_ != 0) | |
342 | completion.assign(string->value_, string->size_); | |
343 | else if (driver.mode_ == CYDriver::AutoMessage) | |
344 | completion = "]"; | |
345 | else | |
346 | continue; | |
347 | ||
348 | completions.push_back(completion); | |
349 | ||
350 | if (!rest) { | |
351 | common = completion; | |
352 | rest = true; | |
353 | } else { | |
354 | size_t limit(completion.size()), size(common.size()); | |
355 | if (size > limit) | |
356 | common = common.substr(0, limit); | |
357 | else | |
358 | limit = size; | |
359 | for (limit = 0; limit != size; ++limit) | |
360 | if (common[limit] != completion[limit]) | |
361 | break; | |
362 | if (limit != size) | |
363 | common = common.substr(0, limit); | |
364 | } | |
365 | } | |
366 | ||
367 | size_t count(completions.size()); | |
368 | if (count == 0) | |
369 | return NULL; | |
370 | ||
371 | size_t colon(common.find(':')); | |
372 | if (colon != std::string::npos) | |
373 | common = common.substr(0, colon + 1); | |
374 | if (completions.size() == 1) | |
375 | common += ' '; | |
376 | ||
377 | char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count + 2)))); | |
378 | ||
379 | results[0] = strdup(common.c_str()); | |
380 | size_t index(0); | |
381 | for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i) | |
382 | results[++index] = strdup(i->c_str()); | |
383 | results[count + 1] = NULL; | |
384 | ||
385 | return results; | |
386 | } | |
387 | ||
388 | // need char *, not const char * | |
389 | static char name_[] = "cycript"; | |
390 | static char break_[] = " \t\n\"\\'`@$><=;|&{(" ")}" ".:[]"; | |
391 | ||
392 | class History { | |
393 | private: | |
394 | std::string histfile_; | |
395 | size_t histlines_; | |
396 | ||
397 | public: | |
398 | History(std::string histfile) : | |
399 | histfile_(histfile), | |
400 | histlines_(0) | |
401 | { | |
402 | read_history(histfile_.c_str()); | |
403 | } | |
404 | ||
405 | ~History() { | |
406 | if (append_history$ != NULL) { | |
407 | int fd(_syscall(open(histfile_.c_str(), O_CREAT | O_WRONLY, 0600))); | |
408 | _syscall(close(fd)); | |
409 | _assert((*append_history$)(histlines_, histfile_.c_str()) == 0); | |
410 | } else { | |
411 | _assert(write_history(histfile_.c_str()) == 0); | |
412 | } | |
413 | } | |
414 | ||
415 | void operator +=(const std::string &command) { | |
416 | add_history(command_.c_str()); | |
417 | ++histlines_; | |
418 | } | |
419 | }; | |
420 | ||
421 | static void Console(CYOptions &options) { | |
422 | std::string basedir; | |
423 | if (const char *home = getenv("HOME")) | |
424 | basedir = home; | |
425 | else { | |
426 | passwd *passwd; | |
427 | if (const char *username = getenv("LOGNAME")) | |
428 | passwd = getpwnam(username); | |
429 | else | |
430 | passwd = getpwuid(getuid()); | |
431 | basedir = passwd->pw_dir; | |
432 | } | |
433 | ||
434 | basedir += "/.cycript"; | |
435 | mkdir(basedir.c_str(), 0700); | |
436 | ||
437 | rl_initialize(); | |
438 | rl_readline_name = name_; | |
439 | ||
440 | History history(basedir + "/history"); | |
441 | ||
442 | bool bypass(false); | |
443 | bool debug(false); | |
444 | bool expand(false); | |
445 | bool lower(true); | |
446 | bool syntax(true); | |
447 | ||
448 | out_ = &std::cout; | |
449 | ||
450 | // rl_completer_word_break_characters is broken in libedit | |
451 | rl_basic_word_break_characters = break_; | |
452 | ||
453 | rl_completer_word_break_characters = break_; | |
454 | rl_attempted_completion_function = &Complete; | |
455 | rl_bind_key('\t', rl_complete); | |
456 | ||
457 | struct sigaction action; | |
458 | sigemptyset(&action.sa_mask); | |
459 | action.sa_handler = &sigint; | |
460 | action.sa_flags = 0; | |
461 | sigaction(SIGINT, &action, NULL); | |
462 | ||
463 | restart: for (;;) { | |
464 | command_.clear(); | |
465 | std::vector<std::string> lines; | |
466 | ||
467 | bool extra(false); | |
468 | const char *prompt("cy# "); | |
469 | ||
470 | if (setjmp(ctrlc_) != 0) { | |
471 | mode_ = Working; | |
472 | *out_ << std::endl; | |
473 | goto restart; | |
474 | } | |
475 | ||
476 | read: | |
477 | ||
478 | #if RL_READLINE_VERSION >= 0x0600 | |
479 | if (syntax) | |
480 | rl_redisplay_function = CYDisplayUpdate; | |
481 | else | |
482 | rl_redisplay_function = rl_redisplay; | |
483 | #endif | |
484 | ||
485 | mode_ = Parsing; | |
486 | char *line(readline(prompt)); | |
487 | mode_ = Working; | |
488 | ||
489 | if (line == NULL) { | |
490 | *out_ << std::endl; | |
491 | break; | |
492 | } else if (line[0] == '\0') | |
493 | goto read; | |
494 | ||
495 | if (!extra) { | |
496 | extra = true; | |
497 | if (line[0] == '?') { | |
498 | std::string data(line + 1); | |
499 | if (data == "bypass") { | |
500 | bypass = !bypass; | |
501 | *out_ << "bypass == " << (bypass ? "true" : "false") << std::endl; | |
502 | } else if (data == "debug") { | |
503 | debug = !debug; | |
504 | *out_ << "debug == " << (debug ? "true" : "false") << std::endl; | |
505 | } else if (data == "destroy") { | |
506 | CYDestroyContext(); | |
507 | } else if (data == "gc") { | |
508 | *out_ << "collecting... " << std::flush; | |
509 | CYGarbageCollect(CYGetJSContext()); | |
510 | *out_ << "done." << std::endl; | |
511 | } else if (data == "exit") { | |
512 | return; | |
513 | } else if (data == "expand") { | |
514 | expand = !expand; | |
515 | *out_ << "expand == " << (expand ? "true" : "false") << std::endl; | |
516 | } else if (data == "lower") { | |
517 | lower = !lower; | |
518 | *out_ << "lower == " << (lower ? "true" : "false") << std::endl; | |
519 | } else if (data == "syntax") { | |
520 | syntax = !syntax; | |
521 | *out_ << "syntax == " << (syntax ? "true" : "false") << std::endl; | |
522 | } | |
523 | command_ = line; | |
524 | history += command_; | |
525 | goto restart; | |
526 | } | |
527 | } | |
528 | ||
529 | command_ += line; | |
530 | ||
531 | char *begin(line), *end(line + strlen(line)); | |
532 | while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) { | |
533 | *nl = '\0'; | |
534 | lines.push_back(begin); | |
535 | begin = nl + 1; | |
536 | } | |
537 | ||
538 | lines.push_back(begin); | |
539 | ||
540 | free(line); | |
541 | ||
542 | std::string code; | |
543 | ||
544 | if (bypass) | |
545 | code = command_; | |
546 | else { | |
547 | CYLocalPool pool; | |
548 | ||
549 | std::istringstream stream(command_); | |
550 | CYDriver driver(stream); | |
551 | ||
552 | cy::parser parser(driver); | |
553 | Setup(driver, parser); | |
554 | ||
555 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
556 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { | |
557 | CYPosition begin(error->location_.begin); | |
558 | if (begin.line != lines.size() || begin.column < lines.back().size() || error->warning_) { | |
559 | CYPosition end(error->location_.end); | |
560 | ||
561 | if (begin.line != lines.size()) { | |
562 | std::cerr << " | "; | |
563 | std::cerr << lines[begin.line - 1] << std::endl; | |
564 | } | |
565 | ||
566 | std::cerr << "...."; | |
567 | for (size_t i(0); i != begin.column; ++i) | |
568 | std::cerr << '.'; | |
569 | if (begin.line != end.line || begin.column == end.column) | |
570 | std::cerr << '^'; | |
571 | else for (size_t i(0), e(end.column - begin.column); i != e; ++i) | |
572 | std::cerr << '^'; | |
573 | std::cerr << std::endl; | |
574 | ||
575 | std::cerr << " | "; | |
576 | std::cerr << error->message_ << std::endl; | |
577 | ||
578 | history += command_; | |
579 | goto restart; | |
580 | } | |
581 | } | |
582 | ||
583 | driver.errors_.clear(); | |
584 | ||
585 | command_ += '\n'; | |
586 | prompt = "cy> "; | |
587 | goto read; | |
588 | } | |
589 | ||
590 | if (driver.program_ == NULL) | |
591 | goto restart; | |
592 | ||
593 | std::ostringstream str; | |
594 | CYOutput out(str, options); | |
595 | Setup(out, driver, options, lower); | |
596 | out << *driver.program_; | |
597 | code = str.str(); | |
598 | } | |
599 | ||
600 | history += command_; | |
601 | ||
602 | if (debug) { | |
603 | std::cout << "cy= "; | |
604 | Write(syntax, code.c_str(), code.size(), std::cout); | |
605 | std::cout << std::endl; | |
606 | } | |
607 | ||
608 | Run(client_, syntax, code, out_, expand); | |
609 | } | |
610 | } | |
611 | ||
612 | void InjectLibrary(pid_t, int, const char *[]); | |
613 | ||
614 | int Main(int argc, char * const argv[], char const * const envp[]) { | |
615 | bool tty(isatty(STDIN_FILENO)); | |
616 | bool compile(false); | |
617 | bool target(false); | |
618 | CYOptions options; | |
619 | ||
620 | append_history$ = (int (*)(int, const char *)) (dlsym(RTLD_DEFAULT, "append_history")); | |
621 | ||
622 | #ifdef CY_ATTACH | |
623 | pid_t pid(_not(pid_t)); | |
624 | #endif | |
625 | ||
626 | const char *host(NULL); | |
627 | const char *port(NULL); | |
628 | ||
629 | optind = 1; | |
630 | ||
631 | for (;;) { | |
632 | int option(getopt_long(argc, argv, | |
633 | "c" | |
634 | "g:" | |
635 | "n:" | |
636 | #ifdef CY_ATTACH | |
637 | "p:" | |
638 | #endif | |
639 | "r:" | |
640 | "s" | |
641 | , (const struct option[]) { | |
642 | {NULL, no_argument, NULL, 'c'}, | |
643 | {NULL, required_argument, NULL, 'g'}, | |
644 | {NULL, required_argument, NULL, 'n'}, | |
645 | #ifdef CY_ATTACH | |
646 | {NULL, required_argument, NULL, 'p'}, | |
647 | #endif | |
648 | {NULL, required_argument, NULL, 'r'}, | |
649 | {NULL, no_argument, NULL, 's'}, | |
650 | {0, 0, 0, 0}}, NULL)); | |
651 | ||
652 | switch (option) { | |
653 | case -1: | |
654 | goto getopt; | |
655 | ||
656 | case ':': | |
657 | case '?': | |
658 | fprintf(stderr, | |
659 | "usage: cycript [-c]" | |
660 | #ifdef CY_ATTACH | |
661 | " [-p <pid|name>]" | |
662 | #endif | |
663 | " [-r <host:port>]" | |
664 | " [<script> [<arg>...]]\n" | |
665 | ); | |
666 | return 1; | |
667 | ||
668 | target: | |
669 | if (!target) | |
670 | target = true; | |
671 | else { | |
672 | fprintf(stderr, "only one of -[c" | |
673 | #ifdef CY_ATTACH | |
674 | "p" | |
675 | #endif | |
676 | "r] may be used at a time\n"); | |
677 | return 1; | |
678 | } | |
679 | break; | |
680 | ||
681 | case 'c': | |
682 | compile = true; | |
683 | goto target; | |
684 | ||
685 | case 'g': | |
686 | if (false); | |
687 | else if (strcmp(optarg, "rename") == 0) | |
688 | options.verbose_ = true; | |
689 | #if YYDEBUG | |
690 | else if (strcmp(optarg, "bison") == 0) | |
691 | bison_ = true; | |
692 | #endif | |
693 | else { | |
694 | fprintf(stderr, "invalid name for -g\n"); | |
695 | return 1; | |
696 | } | |
697 | break; | |
698 | ||
699 | case 'n': | |
700 | if (false); | |
701 | else if (strcmp(optarg, "minify") == 0) | |
702 | pretty_ = true; | |
703 | else { | |
704 | fprintf(stderr, "invalid name for -n\n"); | |
705 | return 1; | |
706 | } | |
707 | break; | |
708 | ||
709 | #ifdef CY_ATTACH | |
710 | case 'p': { | |
711 | size_t size(strlen(optarg)); | |
712 | char *end; | |
713 | ||
714 | pid = strtoul(optarg, &end, 0); | |
715 | if (optarg + size != end) { | |
716 | // XXX: arg needs to be escaped in some horrendous way of doom | |
717 | // XXX: this is a memory leak now because I just don't care enough | |
718 | char *command; | |
719 | asprintf(&command, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", optarg); | |
720 | ||
721 | if (FILE *pids = popen(command, "r")) { | |
722 | char value[32]; | |
723 | size = 0; | |
724 | ||
725 | for (;;) { | |
726 | size_t read(fread(value + size, 1, sizeof(value) - size, pids)); | |
727 | if (read == 0) | |
728 | break; | |
729 | else { | |
730 | size += read; | |
731 | if (size == sizeof(value)) | |
732 | goto fail; | |
733 | } | |
734 | } | |
735 | ||
736 | size: | |
737 | if (size == 0) | |
738 | goto fail; | |
739 | if (value[size - 1] == '\n') { | |
740 | --size; | |
741 | goto size; | |
742 | } | |
743 | ||
744 | value[size] = '\0'; | |
745 | size = strlen(value); | |
746 | pid = strtoul(value, &end, 0); | |
747 | if (value + size != end) fail: | |
748 | pid = _not(pid_t); | |
749 | _syscall(pclose(pids)); | |
750 | } | |
751 | ||
752 | if (pid == _not(pid_t)) { | |
753 | fprintf(stderr, "unable to find process `%s' using ps\n", optarg); | |
754 | return 1; | |
755 | } | |
756 | } | |
757 | } goto target; | |
758 | #endif | |
759 | ||
760 | case 'r': { | |
761 | //size_t size(strlen(optarg)); | |
762 | ||
763 | char *colon(strrchr(optarg, ':')); | |
764 | if (colon == NULL) { | |
765 | fprintf(stderr, "missing colon in hostspec\n"); | |
766 | return 1; | |
767 | } | |
768 | ||
769 | /*char *end; | |
770 | port = strtoul(colon + 1, &end, 10); | |
771 | if (end != optarg + size) { | |
772 | fprintf(stderr, "invalid port in hostspec\n"); | |
773 | return 1; | |
774 | }*/ | |
775 | ||
776 | host = optarg; | |
777 | *colon = '\0'; | |
778 | port = colon + 1; | |
779 | } goto target; | |
780 | ||
781 | case 's': | |
782 | strict_ = true; | |
783 | break; | |
784 | ||
785 | default: | |
786 | _assert(false); | |
787 | } | |
788 | } | |
789 | ||
790 | getopt: | |
791 | argc -= optind; | |
792 | argv += optind; | |
793 | ||
794 | const char *script; | |
795 | ||
796 | #ifdef CY_ATTACH | |
797 | if (pid != _not(pid_t) && argc > 1) { | |
798 | fprintf(stderr, "-p cannot set argv\n"); | |
799 | return 1; | |
800 | } | |
801 | #endif | |
802 | ||
803 | if (argc == 0) | |
804 | script = NULL; | |
805 | else { | |
806 | #ifdef CY_EXECUTE | |
807 | // XXX: const_cast?! wtf gcc :( | |
808 | CYSetArgs(argc - 1, const_cast<const char **>(argv + 1)); | |
809 | #endif | |
810 | script = argv[0]; | |
811 | if (strcmp(script, "-") == 0) | |
812 | script = NULL; | |
813 | } | |
814 | ||
815 | #ifdef CY_ATTACH | |
816 | if (pid == _not(pid_t)) | |
817 | client_ = -1; | |
818 | else { | |
819 | struct Socket { | |
820 | int fd_; | |
821 | ||
822 | Socket(int fd) : | |
823 | fd_(fd) | |
824 | { | |
825 | } | |
826 | ||
827 | ~Socket() { | |
828 | close(fd_); | |
829 | } | |
830 | ||
831 | operator int() { | |
832 | return fd_; | |
833 | } | |
834 | } server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0))); | |
835 | ||
836 | struct sockaddr_un address; | |
837 | memset(&address, 0, sizeof(address)); | |
838 | address.sun_family = AF_UNIX; | |
839 | ||
840 | const char *tmp; | |
841 | #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) | |
842 | tmp = "/Library/Caches"; | |
843 | #else | |
844 | tmp = "/tmp"; | |
845 | #endif | |
846 | ||
847 | sprintf(address.sun_path, "%s/.s.cy.%u", tmp, getpid()); | |
848 | unlink(address.sun_path); | |
849 | ||
850 | struct File { | |
851 | const char *path_; | |
852 | ||
853 | File(const char *path) : | |
854 | path_(path) | |
855 | { | |
856 | } | |
857 | ||
858 | ~File() { | |
859 | unlink(path_); | |
860 | } | |
861 | } file(address.sun_path); | |
862 | ||
863 | _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
864 | _syscall(chmod(address.sun_path, 0777)); | |
865 | ||
866 | _syscall(listen(server, 1)); | |
867 | InjectLibrary(pid, 1, (const char *[]) {address.sun_path, NULL}); | |
868 | client_ = _syscall(accept(server, NULL, NULL)); | |
869 | } | |
870 | #else | |
871 | client_ = -1; | |
872 | #endif | |
873 | ||
874 | if (client_ == -1 && host != NULL && port != NULL) { | |
875 | struct addrinfo hints; | |
876 | memset(&hints, 0, sizeof(hints)); | |
877 | hints.ai_family = AF_UNSPEC; | |
878 | hints.ai_socktype = SOCK_STREAM; | |
879 | hints.ai_protocol = 0; | |
880 | hints.ai_flags = 0; | |
881 | ||
882 | struct addrinfo *infos; | |
883 | _syscall(getaddrinfo(host, port, &hints, &infos)); | |
884 | ||
885 | _assert(infos != NULL); try { | |
886 | for (struct addrinfo *info(infos); info != NULL; info = info->ai_next) { | |
887 | int client(_syscall(socket(info->ai_family, info->ai_socktype, info->ai_protocol))); try { | |
888 | _syscall(connect(client, info->ai_addr, info->ai_addrlen)); | |
889 | client_ = client; | |
890 | break; | |
891 | } catch (...) { | |
892 | _syscall(close(client)); | |
893 | throw; | |
894 | } | |
895 | } | |
896 | } catch (...) { | |
897 | freeaddrinfo(infos); | |
898 | throw; | |
899 | } | |
900 | } | |
901 | ||
902 | if (script == NULL && tty) | |
903 | Console(options); | |
904 | else { | |
905 | CYLocalPool pool; | |
906 | ||
907 | std::istream *stream; | |
908 | if (script == NULL) { | |
909 | stream = &std::cin; | |
910 | script = "<stdin>"; | |
911 | } else { | |
912 | stream = new std::fstream(script, std::ios::in | std::ios::binary); | |
913 | _assert(!stream->fail()); | |
914 | } | |
915 | ||
916 | CYDriver driver(*stream, script); | |
917 | cy::parser parser(driver); | |
918 | Setup(driver, parser); | |
919 | ||
920 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
921 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) | |
922 | std::cerr << i->location_.begin << ": " << i->message_ << std::endl; | |
923 | } else if (driver.program_ != NULL) { | |
924 | std::ostringstream str; | |
925 | CYOutput out(str, options); | |
926 | Setup(out, driver, options, true); | |
927 | out << *driver.program_; | |
928 | std::string code(str.str()); | |
929 | if (compile) | |
930 | std::cout << code; | |
931 | else | |
932 | Run(client_, false, code, &std::cout); | |
933 | } | |
934 | } | |
935 | ||
936 | return 0; | |
937 | } | |
938 | ||
939 | int main(int argc, char * const argv[], char const * const envp[]) { | |
940 | try { | |
941 | return Main(argc, argv, envp); | |
942 | } catch (const CYException &error) { | |
943 | CYPool pool; | |
944 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
945 | return 1; | |
946 | } | |
947 | } |