]>
Commit | Line | Data |
---|---|---|
1 | /* Cycript - Inlining/Optimizing JavaScript Compiler | |
2 | * Copyright (C) 2009 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
5 | /* Modified BSD License {{{ */ | |
6 | /* | |
7 | * Redistribution and use in source and binary | |
8 | * forms, with or without modification, are permitted | |
9 | * provided that the following conditions are met: | |
10 | * | |
11 | * 1. Redistributions of source code must retain the | |
12 | * above copyright notice, this list of conditions | |
13 | * and the following disclaimer. | |
14 | * 2. Redistributions in binary form must reproduce the | |
15 | * above copyright notice, this list of conditions | |
16 | * and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the | |
18 | * distribution. | |
19 | * 3. The name of the author may not be used to endorse | |
20 | * or promote products derived from this software | |
21 | * without specific prior written permission. | |
22 | * | |
23 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' | |
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
25 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
26 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
27 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
29 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
33 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
34 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
35 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
36 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
37 | */ | |
38 | /* }}} */ | |
39 | ||
40 | #include "cycript.hpp" | |
41 | ||
42 | #ifdef CY_EXECUTE | |
43 | #include "JavaScript.hpp" | |
44 | #endif | |
45 | ||
46 | #include <cstdio> | |
47 | #include <sstream> | |
48 | ||
49 | #include <setjmp.h> | |
50 | ||
51 | #ifdef HAVE_READLINE_H | |
52 | #include <readline.h> | |
53 | #else | |
54 | #include <readline/readline.h> | |
55 | #endif | |
56 | ||
57 | #ifdef HAVE_HISTORY_H | |
58 | #include <history.h> | |
59 | #else | |
60 | #include <readline/history.h> | |
61 | #endif | |
62 | ||
63 | #include <sys/mman.h> | |
64 | ||
65 | #include <errno.h> | |
66 | #include <unistd.h> | |
67 | ||
68 | #include <sys/types.h> | |
69 | #include <sys/stat.h> | |
70 | #include <fcntl.h> | |
71 | ||
72 | #include "Cycript.tab.hh" | |
73 | ||
74 | #include <sys/types.h> | |
75 | #include <sys/socket.h> | |
76 | #include <netinet/in.h> | |
77 | #include <sys/un.h> | |
78 | #include <pwd.h> | |
79 | ||
80 | #include <apr_getopt.h> | |
81 | ||
82 | #include <dlfcn.h> | |
83 | ||
84 | static volatile enum { | |
85 | Working, | |
86 | Parsing, | |
87 | Running, | |
88 | Sending, | |
89 | Waiting, | |
90 | } mode_; | |
91 | ||
92 | static jmp_buf ctrlc_; | |
93 | ||
94 | static void sigint(int) { | |
95 | switch (mode_) { | |
96 | case Working: | |
97 | return; | |
98 | case Parsing: | |
99 | longjmp(ctrlc_, 1); | |
100 | case Running: | |
101 | throw "*** Ctrl-C"; | |
102 | case Sending: | |
103 | return; | |
104 | case Waiting: | |
105 | return; | |
106 | } | |
107 | } | |
108 | ||
109 | #if YYDEBUG | |
110 | static bool bison_; | |
111 | #endif | |
112 | static bool strict_; | |
113 | static bool pretty_; | |
114 | ||
115 | void Setup(CYDriver &driver, cy::parser &parser) { | |
116 | #if YYDEBUG | |
117 | if (bison_) | |
118 | parser.set_debug_level(1); | |
119 | #endif | |
120 | if (strict_) | |
121 | driver.strict_ = true; | |
122 | } | |
123 | ||
124 | void Setup(CYOutput &out, CYDriver &driver, CYOptions &options) { | |
125 | out.pretty_ = pretty_; | |
126 | CYContext context(driver.pool_, options); | |
127 | driver.program_->Replace(context); | |
128 | } | |
129 | ||
130 | void Run(int client, const char *data, size_t size, FILE *fout = NULL, bool expand = false) { | |
131 | CYPool pool; | |
132 | ||
133 | const char *json; | |
134 | if (client == -1) { | |
135 | mode_ = Running; | |
136 | #ifdef CY_EXECUTE | |
137 | json = CYExecute(pool, data); | |
138 | #else | |
139 | json = NULL; | |
140 | #endif | |
141 | mode_ = Working; | |
142 | if (json != NULL) | |
143 | size = strlen(json); | |
144 | } else { | |
145 | mode_ = Sending; | |
146 | CYSendAll(client, &size, sizeof(size)); | |
147 | CYSendAll(client, data, size); | |
148 | mode_ = Waiting; | |
149 | CYRecvAll(client, &size, sizeof(size)); | |
150 | if (size == _not(size_t)) | |
151 | json = NULL; | |
152 | else { | |
153 | char *temp(new(pool) char[size + 1]); | |
154 | CYRecvAll(client, temp, size); | |
155 | temp[size] = '\0'; | |
156 | json = temp; | |
157 | } | |
158 | mode_ = Working; | |
159 | } | |
160 | ||
161 | if (json != NULL && fout != NULL) { | |
162 | if (!expand || json[0] != '"' && json[0] != '\'') | |
163 | fputs(json, fout); | |
164 | else for (size_t i(0); i != size; ++i) | |
165 | if (json[i] != '\\') | |
166 | fputc(json[i], fout); | |
167 | else switch(json[++i]) { | |
168 | case '\0': goto done; | |
169 | case '\\': fputc('\\', fout); break; | |
170 | case '\'': fputc('\'', fout); break; | |
171 | case '"': fputc('"', fout); break; | |
172 | case 'b': fputc('\b', fout); break; | |
173 | case 'f': fputc('\f', fout); break; | |
174 | case 'n': fputc('\n', fout); break; | |
175 | case 'r': fputc('\r', fout); break; | |
176 | case 't': fputc('\t', fout); break; | |
177 | case 'v': fputc('\v', fout); break; | |
178 | default: fputc('\\', fout); --i; break; | |
179 | } | |
180 | ||
181 | done: | |
182 | fputs("\n", fout); | |
183 | fflush(fout); | |
184 | } | |
185 | } | |
186 | ||
187 | void Run(int client, std::string &code, FILE *fout = NULL, bool expand = false) { | |
188 | Run(client, code.c_str(), code.size(), fout, expand); | |
189 | } | |
190 | ||
191 | int (*append_history$)(int, const char *); | |
192 | ||
193 | static void Console(apr_pool_t *pool, int client, CYOptions &options) { | |
194 | passwd *passwd; | |
195 | if (const char *username = getenv("LOGNAME")) | |
196 | passwd = getpwnam(username); | |
197 | else | |
198 | passwd = getpwuid(getuid()); | |
199 | ||
200 | const char *basedir(apr_psprintf(pool, "%s/.cycript", passwd->pw_dir)); | |
201 | const char *histfile(apr_psprintf(pool, "%s/history", basedir)); | |
202 | size_t histlines(0); | |
203 | ||
204 | mkdir(basedir, 0700); | |
205 | read_history(histfile); | |
206 | ||
207 | bool bypass(false); | |
208 | bool debug(false); | |
209 | bool expand(false); | |
210 | ||
211 | FILE *fout(stdout); | |
212 | ||
213 | rl_bind_key('\t', rl_insert); | |
214 | ||
215 | struct sigaction action; | |
216 | sigemptyset(&action.sa_mask); | |
217 | action.sa_handler = &sigint; | |
218 | action.sa_flags = 0; | |
219 | sigaction(SIGINT, &action, NULL); | |
220 | ||
221 | restart: for (;;) { | |
222 | std::string command; | |
223 | std::vector<std::string> lines; | |
224 | ||
225 | bool extra(false); | |
226 | const char *prompt("cy# "); | |
227 | ||
228 | if (setjmp(ctrlc_) != 0) { | |
229 | mode_ = Working; | |
230 | fputs("\n", fout); | |
231 | fflush(fout); | |
232 | goto restart; | |
233 | } | |
234 | ||
235 | read: | |
236 | mode_ = Parsing; | |
237 | char *line(readline(prompt)); | |
238 | mode_ = Working; | |
239 | if (line == NULL) | |
240 | break; | |
241 | if (line[0] == '\0') | |
242 | goto read; | |
243 | ||
244 | if (!extra) { | |
245 | extra = true; | |
246 | if (line[0] == '?') { | |
247 | std::string data(line + 1); | |
248 | if (data == "bypass") { | |
249 | bypass = !bypass; | |
250 | fprintf(fout, "bypass == %s\n", bypass ? "true" : "false"); | |
251 | fflush(fout); | |
252 | } else if (data == "debug") { | |
253 | debug = !debug; | |
254 | fprintf(fout, "debug == %s\n", debug ? "true" : "false"); | |
255 | fflush(fout); | |
256 | } else if (data == "expand") { | |
257 | expand = !expand; | |
258 | fprintf(fout, "expand == %s\n", expand ? "true" : "false"); | |
259 | fflush(fout); | |
260 | } | |
261 | add_history(line); | |
262 | ++histlines; | |
263 | goto restart; | |
264 | } | |
265 | } | |
266 | ||
267 | command += line; | |
268 | ||
269 | char *begin(line), *end(line + strlen(line)); | |
270 | while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) { | |
271 | *nl = '\0'; | |
272 | lines.push_back(begin); | |
273 | begin = nl + 1; | |
274 | } | |
275 | ||
276 | lines.push_back(begin); | |
277 | ||
278 | free(line); | |
279 | ||
280 | std::string code; | |
281 | ||
282 | if (bypass) | |
283 | code = command; | |
284 | else { | |
285 | CYDriver driver(""); | |
286 | cy::parser parser(driver); | |
287 | Setup(driver, parser); | |
288 | ||
289 | driver.data_ = command.c_str(); | |
290 | driver.size_ = command.size(); | |
291 | ||
292 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
293 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { | |
294 | cy::position begin(error->location_.begin); | |
295 | if (begin.line != lines.size() || begin.column - 1 != lines.back().size() || error->warning_) { | |
296 | cy::position end(error->location_.end); | |
297 | ||
298 | if (begin.line != lines.size()) { | |
299 | std::cerr << " | "; | |
300 | std::cerr << lines[begin.line - 1] << std::endl; | |
301 | } | |
302 | ||
303 | std::cerr << "...."; | |
304 | for (size_t i(0); i != begin.column - 1; ++i) | |
305 | std::cerr << '.'; | |
306 | if (begin.line != end.line || begin.column == end.column) | |
307 | std::cerr << '^'; | |
308 | else for (size_t i(0), e(end.column - begin.column); i != e; ++i) | |
309 | std::cerr << '^'; | |
310 | std::cerr << std::endl; | |
311 | ||
312 | std::cerr << " | "; | |
313 | std::cerr << error->message_ << std::endl; | |
314 | ||
315 | add_history(command.c_str()); | |
316 | ++histlines; | |
317 | goto restart; | |
318 | } | |
319 | } | |
320 | ||
321 | driver.errors_.clear(); | |
322 | ||
323 | command += '\n'; | |
324 | prompt = "cy> "; | |
325 | goto read; | |
326 | } | |
327 | ||
328 | if (driver.program_ == NULL) | |
329 | goto restart; | |
330 | ||
331 | if (client != -1) | |
332 | code = command; | |
333 | else { | |
334 | std::ostringstream str; | |
335 | CYOutput out(str, options); | |
336 | Setup(out, driver, options); | |
337 | out << *driver.program_; | |
338 | code = str.str(); | |
339 | } | |
340 | } | |
341 | ||
342 | add_history(command.c_str()); | |
343 | ++histlines; | |
344 | ||
345 | if (debug) | |
346 | std::cout << code << std::endl; | |
347 | ||
348 | Run(client, code, fout, expand); | |
349 | } | |
350 | ||
351 | if (append_history$ != NULL) { | |
352 | _syscall(close(_syscall(open(histfile, O_CREAT | O_WRONLY, 0600)))); | |
353 | (*append_history$)(histlines, histfile); | |
354 | } else { | |
355 | write_history(histfile); | |
356 | } | |
357 | ||
358 | fputs("\n", fout); | |
359 | fflush(fout); | |
360 | } | |
361 | ||
362 | static void *Map(const char *path, size_t *psize) { | |
363 | int fd; | |
364 | _syscall(fd = open(path, O_RDONLY)); | |
365 | ||
366 | struct stat stat; | |
367 | _syscall(fstat(fd, &stat)); | |
368 | size_t size(stat.st_size); | |
369 | ||
370 | *psize = size; | |
371 | ||
372 | void *base; | |
373 | _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); | |
374 | ||
375 | _syscall(close(fd)); | |
376 | return base; | |
377 | } | |
378 | ||
379 | void InjectLibrary(pid_t pid); | |
380 | ||
381 | int Main(int argc, char const * const argv[], char const * const envp[]) { | |
382 | bool tty(isatty(STDIN_FILENO)); | |
383 | bool compile(false); | |
384 | CYOptions options; | |
385 | ||
386 | append_history$ = reinterpret_cast<int (*)(int, const char *)>(dlsym(RTLD_DEFAULT, "append_history")); | |
387 | ||
388 | #ifdef CY_ATTACH | |
389 | pid_t pid(_not(pid_t)); | |
390 | #endif | |
391 | ||
392 | CYPool pool; | |
393 | apr_getopt_t *state; | |
394 | _aprcall(apr_getopt_init(&state, pool, argc, argv)); | |
395 | ||
396 | for (;;) { | |
397 | char opt; | |
398 | const char *arg; | |
399 | ||
400 | apr_status_t status(apr_getopt(state, | |
401 | "cg:n:" | |
402 | #ifdef CY_ATTACH | |
403 | "p:" | |
404 | #endif | |
405 | "s" | |
406 | , &opt, &arg)); | |
407 | ||
408 | switch (status) { | |
409 | case APR_EOF: | |
410 | goto getopt; | |
411 | case APR_BADCH: | |
412 | case APR_BADARG: | |
413 | fprintf(stderr, | |
414 | "usage: cycript [-c]" | |
415 | #ifdef CY_ATTACH | |
416 | " [-p <pid|name>]" | |
417 | #endif | |
418 | " [<script> [<arg>...]]\n" | |
419 | ); | |
420 | return 1; | |
421 | default: | |
422 | _aprcall(status); | |
423 | } | |
424 | ||
425 | switch (opt) { | |
426 | case 'c': | |
427 | compile = true; | |
428 | break; | |
429 | ||
430 | case 'g': | |
431 | if (false); | |
432 | else if (strcmp(arg, "rename") == 0) | |
433 | options.verbose_ = true; | |
434 | #if YYDEBUG | |
435 | else if (strcmp(arg, "bison") == 0) | |
436 | bison_ = true; | |
437 | #endif | |
438 | else { | |
439 | fprintf(stderr, "invalid name for -g\n"); | |
440 | return 1; | |
441 | } | |
442 | break; | |
443 | ||
444 | case 'n': | |
445 | if (false); | |
446 | else if (strcmp(arg, "minify") == 0) | |
447 | pretty_ = true; | |
448 | else { | |
449 | fprintf(stderr, "invalid name for -n\n"); | |
450 | return 1; | |
451 | } | |
452 | break; | |
453 | ||
454 | #ifdef CY_ATTACH | |
455 | case 'p': { | |
456 | size_t size(strlen(arg)); | |
457 | char *end; | |
458 | ||
459 | pid = strtoul(arg, &end, 0); | |
460 | if (arg + size != end) { | |
461 | // XXX: arg needs to be escaped in some horrendous way of doom | |
462 | const char *command(apr_psprintf(pool, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", arg)); | |
463 | ||
464 | if (FILE *pids = popen(command, "r")) { | |
465 | char value[32]; | |
466 | size = 0; | |
467 | ||
468 | for (;;) { | |
469 | size_t read(fread(value + size, 1, sizeof(value) - size, pids)); | |
470 | if (read == 0) | |
471 | break; | |
472 | else { | |
473 | size += read; | |
474 | if (size == sizeof(value)) { | |
475 | pid = _not(pid_t); | |
476 | goto fail; | |
477 | } | |
478 | } | |
479 | } | |
480 | ||
481 | size: | |
482 | if (size == 0) | |
483 | goto fail; | |
484 | if (value[size - 1] == '\n') { | |
485 | --size; | |
486 | goto size; | |
487 | } | |
488 | ||
489 | value[size] = '\0'; | |
490 | size = strlen(value); | |
491 | pid = strtoul(value, &end, 0); | |
492 | if (value + size != end) fail: | |
493 | pid = _not(pid_t); | |
494 | _syscall(pclose(pids)); | |
495 | } | |
496 | ||
497 | if (pid == _not(pid_t)) { | |
498 | fprintf(stderr, "invalid pid for -p\n"); | |
499 | return 1; | |
500 | } | |
501 | } | |
502 | } break; | |
503 | #endif | |
504 | ||
505 | case 's': | |
506 | strict_ = true; | |
507 | break; | |
508 | } | |
509 | } getopt:; | |
510 | ||
511 | const char *script; | |
512 | int ind(state->ind); | |
513 | ||
514 | #ifdef CY_ATTACH | |
515 | if (pid != _not(pid_t) && ind < argc - 1) { | |
516 | fprintf(stderr, "-p cannot set argv\n"); | |
517 | return 1; | |
518 | } | |
519 | ||
520 | if (pid != _not(pid_t) && compile) { | |
521 | fprintf(stderr, "-p conflicts with -c\n"); | |
522 | return 1; | |
523 | } | |
524 | #endif | |
525 | ||
526 | if (ind == argc) | |
527 | script = NULL; | |
528 | else { | |
529 | #ifdef CY_EXECUTE | |
530 | // XXX: const_cast?! wtf gcc :( | |
531 | CYSetArgs(argc - ind - 1, const_cast<const char **>(argv + ind + 1)); | |
532 | #endif | |
533 | script = argv[ind]; | |
534 | if (strcmp(script, "-") == 0) | |
535 | script = NULL; | |
536 | } | |
537 | ||
538 | #ifdef CY_ATTACH | |
539 | if (pid != _not(pid_t) && script == NULL && !tty) { | |
540 | fprintf(stderr, "non-terminal attaching to remote console\n"); | |
541 | return 1; | |
542 | } | |
543 | #endif | |
544 | ||
545 | int client; | |
546 | ||
547 | #ifdef CY_ATTACH | |
548 | if (pid == _not(pid_t)) | |
549 | client = -1; | |
550 | else { | |
551 | int server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0))); try { | |
552 | struct sockaddr_un address; | |
553 | memset(&address, 0, sizeof(address)); | |
554 | address.sun_family = AF_UNIX; | |
555 | ||
556 | sprintf(address.sun_path, "/tmp/.s.cy.%u", getpid()); | |
557 | ||
558 | _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
559 | _syscall(chmod(address.sun_path, 0777)); | |
560 | ||
561 | try { | |
562 | _syscall(listen(server, 1)); | |
563 | InjectLibrary(pid); | |
564 | client = _syscall(accept(server, NULL, NULL)); | |
565 | } catch (...) { | |
566 | // XXX: exception? | |
567 | unlink(address.sun_path); | |
568 | throw; | |
569 | } | |
570 | } catch (...) { | |
571 | _syscall(close(server)); | |
572 | throw; | |
573 | } | |
574 | } | |
575 | #else | |
576 | client = -1; | |
577 | #endif | |
578 | ||
579 | if (script == NULL && tty) | |
580 | Console(pool, client, options); | |
581 | else { | |
582 | CYDriver driver(script ?: "<stdin>"); | |
583 | cy::parser parser(driver); | |
584 | Setup(driver, parser); | |
585 | ||
586 | char *start, *end; | |
587 | ||
588 | if (script == NULL) { | |
589 | start = NULL; | |
590 | end = NULL; | |
591 | ||
592 | driver.file_ = stdin; | |
593 | } else { | |
594 | size_t size; | |
595 | start = reinterpret_cast<char *>(Map(script, &size)); | |
596 | end = start + size; | |
597 | ||
598 | if (size >= 2 && start[0] == '#' && start[1] == '!') { | |
599 | start += 2; | |
600 | ||
601 | if (void *line = memchr(start, '\n', end - start)) | |
602 | start = reinterpret_cast<char *>(line); | |
603 | else | |
604 | start = end; | |
605 | } | |
606 | ||
607 | driver.data_ = start; | |
608 | driver.size_ = end - start; | |
609 | } | |
610 | ||
611 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
612 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) | |
613 | std::cerr << i->location_.begin << ": " << i->message_ << std::endl; | |
614 | } else if (driver.program_ != NULL) | |
615 | if (client != -1) { | |
616 | std::string code(start, end-start); | |
617 | Run(client, code, stdout); | |
618 | } else { | |
619 | std::ostringstream str; | |
620 | CYOutput out(str, options); | |
621 | Setup(out, driver, options); | |
622 | out << *driver.program_; | |
623 | std::string code(str.str()); | |
624 | if (compile) | |
625 | std::cout << code; | |
626 | else | |
627 | Run(client, code, stdout); | |
628 | } | |
629 | } | |
630 | ||
631 | return 0; | |
632 | } | |
633 | ||
634 | int main(int argc, char const * const argv[], char const * const envp[]) { | |
635 | apr_status_t status(apr_app_initialize(&argc, &argv, &envp)); | |
636 | ||
637 | if (status != APR_SUCCESS) { | |
638 | fprintf(stderr, "apr_app_initialize() != APR_SUCCESS\n"); | |
639 | return 1; | |
640 | } else try { | |
641 | return Main(argc, argv, envp); | |
642 | } catch (const CYException &error) { | |
643 | CYPool pool; | |
644 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
645 | return 1; | |
646 | } | |
647 | } |