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