]>
Commit | Line | Data |
---|---|---|
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 <sstream> | |
30 | ||
31 | #include <setjmp.h> | |
32 | ||
33 | #ifdef HAVE_READLINE_H | |
34 | #include <readline.h> | |
35 | #else | |
36 | #include <readline/readline.h> | |
37 | #endif | |
38 | ||
39 | #ifdef HAVE_HISTORY_H | |
40 | #include <history.h> | |
41 | #else | |
42 | #include <readline/history.h> | |
43 | #endif | |
44 | ||
45 | #include <sys/mman.h> | |
46 | ||
47 | #include <errno.h> | |
48 | #include <unistd.h> | |
49 | ||
50 | #include <sys/types.h> | |
51 | #include <sys/stat.h> | |
52 | #include <fcntl.h> | |
53 | ||
54 | #include <sys/types.h> | |
55 | #include <sys/socket.h> | |
56 | #include <netinet/in.h> | |
57 | #include <sys/un.h> | |
58 | #include <pwd.h> | |
59 | ||
60 | #include <apr_getopt.h> | |
61 | #include <apr_pools.h> | |
62 | #include <apr_strings.h> | |
63 | ||
64 | #include <dlfcn.h> | |
65 | ||
66 | #include "Display.hpp" | |
67 | #include "Replace.hpp" | |
68 | ||
69 | #include "Cycript.tab.hh" | |
70 | #include "Driver.hpp" | |
71 | ||
72 | static volatile enum { | |
73 | Working, | |
74 | Parsing, | |
75 | Running, | |
76 | Sending, | |
77 | Waiting, | |
78 | } mode_; | |
79 | ||
80 | static jmp_buf ctrlc_; | |
81 | ||
82 | static void sigint(int) { | |
83 | switch (mode_) { | |
84 | case Working: | |
85 | return; | |
86 | case Parsing: | |
87 | longjmp(ctrlc_, 1); | |
88 | case Running: | |
89 | throw "*** Ctrl-C"; | |
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) { | |
113 | out.pretty_ = pretty_; | |
114 | CYContext context(options); | |
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(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 | CYSendAll(client, &size, sizeof(size)); | |
138 | CYSendAll(client, code.data, code.size); | |
139 | mode_ = Waiting; | |
140 | 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 | 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 | // XXX: this could be replaced with a CYStatement::Primitive() | |
229 | if (CYExpress *express = dynamic_cast<CYExpress *>(driver.program_->statements_)) | |
230 | return express->expression_->Primitive(context); | |
231 | ||
232 | return NULL; | |
233 | } | |
234 | ||
235 | static int client_; | |
236 | ||
237 | static char **Complete(const char *word, int start, int end) { | |
238 | rl_attempted_completion_over = TRUE; | |
239 | ||
240 | CYLocalPool pool; | |
241 | ||
242 | std::string line(rl_line_buffer, start); | |
243 | std::istringstream stream(command_ + line); | |
244 | CYDriver driver(stream); | |
245 | ||
246 | driver.auto_ = true; | |
247 | ||
248 | cy::parser parser(driver); | |
249 | Setup(driver, parser); | |
250 | ||
251 | if (parser.parse() != 0 || !driver.errors_.empty()) | |
252 | return NULL; | |
253 | ||
254 | if (driver.mode_ == CYDriver::AutoNone) | |
255 | return NULL; | |
256 | ||
257 | CYExpression *expression; | |
258 | ||
259 | CYOptions options; | |
260 | CYContext context(options); | |
261 | ||
262 | std::ostringstream prefix; | |
263 | ||
264 | switch (driver.mode_) { | |
265 | case CYDriver::AutoPrimary: | |
266 | expression = $ CYThis(); | |
267 | break; | |
268 | ||
269 | case CYDriver::AutoDirect: | |
270 | expression = driver.context_; | |
271 | break; | |
272 | ||
273 | case CYDriver::AutoIndirect: | |
274 | expression = $ CYIndirect(driver.context_); | |
275 | break; | |
276 | ||
277 | case CYDriver::AutoMessage: { | |
278 | CYDriver::Context &thing(driver.contexts_.back()); | |
279 | expression = $M($C1($V("object_getClass"), thing.context_), $S("messages")); | |
280 | for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part) | |
281 | prefix << (*part)->word_ << ':'; | |
282 | } break; | |
283 | ||
284 | default: | |
285 | _assert(false); | |
286 | } | |
287 | ||
288 | std::string begin(prefix.str()); | |
289 | ||
290 | driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression( | |
291 | " function(object, prefix, word) {\n" | |
292 | " var names = [];\n" | |
293 | " var before = prefix.length;\n" | |
294 | " prefix += word;\n" | |
295 | " var entire = prefix.length;\n" | |
296 | " for (name in object)\n" | |
297 | " if (name.substring(0, entire) == prefix)\n" | |
298 | " names.push(name.substr(before));\n" | |
299 | " return names;\n" | |
300 | " }\n" | |
301 | ), expression, $S(begin.c_str()), $S(word)))); | |
302 | ||
303 | driver.program_->Replace(context); | |
304 | ||
305 | std::ostringstream str; | |
306 | CYOutput out(str, options); | |
307 | out << *driver.program_; | |
308 | ||
309 | std::string code(str.str()); | |
310 | CYUTF8String json(Run(pool, client_, code)); | |
311 | // XXX: if this fails we should not try to parse it | |
312 | ||
313 | CYExpression *result(ParseExpression(json)); | |
314 | if (result == NULL) | |
315 | return NULL; | |
316 | ||
317 | CYArray *array(dynamic_cast<CYArray *>(result)); | |
318 | ||
319 | if (array == NULL) { | |
320 | *out_ << '\n'; | |
321 | Output(false, json, out_); | |
322 | rl_forced_update_display(); | |
323 | return NULL; | |
324 | } | |
325 | ||
326 | // XXX: use an std::set? | |
327 | typedef std::vector<std::string> Completions; | |
328 | Completions completions; | |
329 | ||
330 | std::string common; | |
331 | bool rest(false); | |
332 | ||
333 | CYForEach (element, array->elements_) { | |
334 | CYString *string(dynamic_cast<CYString *>(element->value_)); | |
335 | _assert(string != NULL); | |
336 | ||
337 | std::string completion; | |
338 | if (string->size_ != 0) | |
339 | completion.assign(string->value_, string->size_); | |
340 | else if (driver.mode_ == CYDriver::AutoMessage) | |
341 | completion = "]"; | |
342 | else | |
343 | continue; | |
344 | ||
345 | completions.push_back(completion); | |
346 | ||
347 | if (!rest) { | |
348 | common = completion; | |
349 | rest = true; | |
350 | } else { | |
351 | size_t limit(completion.size()), size(common.size()); | |
352 | if (size > limit) | |
353 | common = common.substr(0, limit); | |
354 | else | |
355 | limit = size; | |
356 | for (limit = 0; limit != size; ++limit) | |
357 | if (common[limit] != completion[limit]) | |
358 | break; | |
359 | if (limit != size) | |
360 | common = common.substr(0, limit); | |
361 | } | |
362 | } | |
363 | ||
364 | size_t count(completions.size()); | |
365 | if (count == 0) | |
366 | return NULL; | |
367 | ||
368 | size_t colon(common.find(':')); | |
369 | if (colon != std::string::npos) | |
370 | common = common.substr(0, colon + 1); | |
371 | if (completions.size() == 1) | |
372 | common += ' '; | |
373 | ||
374 | char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count + 2)))); | |
375 | ||
376 | results[0] = strdup(common.c_str()); | |
377 | size_t index(0); | |
378 | for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i) | |
379 | results[++index] = strdup(i->c_str()); | |
380 | results[count + 1] = NULL; | |
381 | ||
382 | return results; | |
383 | } | |
384 | ||
385 | // need char *, not const char * | |
386 | static char name_[] = "cycript"; | |
387 | static char break_[] = " \t\n\"\\'`@$><=;|&{(" ")}" ".:[]"; | |
388 | ||
389 | static void Console(CYOptions &options) { | |
390 | CYPool pool; | |
391 | ||
392 | passwd *passwd; | |
393 | if (const char *username = getenv("LOGNAME")) | |
394 | passwd = getpwnam(username); | |
395 | else | |
396 | passwd = getpwuid(getuid()); | |
397 | ||
398 | const char *basedir(pool.strcat(passwd->pw_dir, "/.cycript", NULL)); | |
399 | const char *histfile(pool.strcat(basedir, "/history", NULL)); | |
400 | size_t histlines(0); | |
401 | ||
402 | rl_initialize(); | |
403 | rl_readline_name = name_; | |
404 | ||
405 | mkdir(basedir, 0700); | |
406 | read_history(histfile); | |
407 | ||
408 | bool bypass(false); | |
409 | bool debug(false); | |
410 | bool expand(false); | |
411 | bool syntax(true); | |
412 | ||
413 | out_ = &std::cout; | |
414 | ||
415 | // rl_completer_word_break_characters is broken in libedit | |
416 | rl_basic_word_break_characters = break_; | |
417 | ||
418 | rl_completer_word_break_characters = break_; | |
419 | rl_attempted_completion_function = &Complete; | |
420 | rl_bind_key('\t', rl_complete); | |
421 | ||
422 | struct sigaction action; | |
423 | sigemptyset(&action.sa_mask); | |
424 | action.sa_handler = &sigint; | |
425 | action.sa_flags = 0; | |
426 | sigaction(SIGINT, &action, NULL); | |
427 | ||
428 | restart: for (;;) { | |
429 | command_.clear(); | |
430 | std::vector<std::string> lines; | |
431 | ||
432 | bool extra(false); | |
433 | const char *prompt("cy# "); | |
434 | ||
435 | if (setjmp(ctrlc_) != 0) { | |
436 | mode_ = Working; | |
437 | *out_ << std::endl; | |
438 | goto restart; | |
439 | } | |
440 | ||
441 | read: | |
442 | ||
443 | #if RL_READLINE_VERSION >= 0x0600 | |
444 | if (syntax) { | |
445 | rl_prep_term_function = CYDisplayStart; | |
446 | rl_redisplay_function = CYDisplayUpdate; | |
447 | rl_deprep_term_function = CYDisplayFinish; | |
448 | } else { | |
449 | rl_prep_term_function = rl_prep_terminal; | |
450 | rl_redisplay_function = rl_redisplay; | |
451 | rl_deprep_term_function = rl_deprep_terminal; | |
452 | } | |
453 | #endif | |
454 | ||
455 | mode_ = Parsing; | |
456 | char *line(readline(prompt)); | |
457 | mode_ = Working; | |
458 | ||
459 | if (line == NULL) | |
460 | break; | |
461 | if (line[0] == '\0') | |
462 | goto read; | |
463 | ||
464 | if (!extra) { | |
465 | extra = true; | |
466 | if (line[0] == '?') { | |
467 | std::string data(line + 1); | |
468 | if (data == "bypass") { | |
469 | bypass = !bypass; | |
470 | *out_ << "bypass == " << (bypass ? "true" : "false") << std::endl; | |
471 | } else if (data == "debug") { | |
472 | debug = !debug; | |
473 | *out_ << "debug == " << (debug ? "true" : "false") << std::endl; | |
474 | } else if (data == "expand") { | |
475 | expand = !expand; | |
476 | *out_ << "expand == " << (expand ? "true" : "false") << std::endl; | |
477 | } else if (data == "syntax") { | |
478 | syntax = !syntax; | |
479 | *out_ << "syntax == " << (syntax ? "true" : "false") << std::endl; | |
480 | } | |
481 | add_history(line); | |
482 | ++histlines; | |
483 | goto restart; | |
484 | } | |
485 | } | |
486 | ||
487 | command_ += line; | |
488 | ||
489 | char *begin(line), *end(line + strlen(line)); | |
490 | while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) { | |
491 | *nl = '\0'; | |
492 | lines.push_back(begin); | |
493 | begin = nl + 1; | |
494 | } | |
495 | ||
496 | lines.push_back(begin); | |
497 | ||
498 | free(line); | |
499 | ||
500 | std::string code; | |
501 | ||
502 | if (bypass) | |
503 | code = command_; | |
504 | else { | |
505 | CYLocalPool pool; | |
506 | ||
507 | std::istringstream stream(command_); | |
508 | CYDriver driver(stream); | |
509 | ||
510 | cy::parser parser(driver); | |
511 | Setup(driver, parser); | |
512 | ||
513 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
514 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { | |
515 | cy::position begin(error->location_.begin); | |
516 | if (begin.line != lines.size() || begin.column < lines.back().size() || error->warning_) { | |
517 | cy::position end(error->location_.end); | |
518 | ||
519 | if (begin.line != lines.size()) { | |
520 | std::cerr << " | "; | |
521 | std::cerr << lines[begin.line - 1] << std::endl; | |
522 | } | |
523 | ||
524 | std::cerr << "...."; | |
525 | for (size_t i(0); i != begin.column; ++i) | |
526 | std::cerr << '.'; | |
527 | if (begin.line != end.line || begin.column == end.column) | |
528 | std::cerr << '^'; | |
529 | else for (size_t i(0), e(end.column - begin.column); i != e; ++i) | |
530 | std::cerr << '^'; | |
531 | std::cerr << std::endl; | |
532 | ||
533 | std::cerr << " | "; | |
534 | std::cerr << error->message_ << std::endl; | |
535 | ||
536 | add_history(command_.c_str()); | |
537 | ++histlines; | |
538 | goto restart; | |
539 | } | |
540 | } | |
541 | ||
542 | driver.errors_.clear(); | |
543 | ||
544 | command_ += '\n'; | |
545 | prompt = "cy> "; | |
546 | goto read; | |
547 | } | |
548 | ||
549 | if (driver.program_ == NULL) | |
550 | goto restart; | |
551 | ||
552 | if (client_ != -1) | |
553 | code = command_; | |
554 | else { | |
555 | std::ostringstream str; | |
556 | CYOutput out(str, options); | |
557 | Setup(out, driver, options); | |
558 | out << *driver.program_; | |
559 | code = str.str(); | |
560 | } | |
561 | } | |
562 | ||
563 | add_history(command_.c_str()); | |
564 | ++histlines; | |
565 | ||
566 | if (debug) { | |
567 | Write(syntax, code.c_str(), code.size(), std::cout); | |
568 | std::cout << std::endl; | |
569 | } | |
570 | ||
571 | Run(client_, syntax, code, out_, expand); | |
572 | } | |
573 | ||
574 | if (append_history$ != NULL) { | |
575 | _syscall(close(_syscall(open(histfile, O_CREAT | O_WRONLY, 0600)))); | |
576 | (*append_history$)(histlines, histfile); | |
577 | } else { | |
578 | write_history(histfile); | |
579 | } | |
580 | ||
581 | *out_ << std::endl; | |
582 | } | |
583 | ||
584 | static void *Map(const char *path, size_t *psize) { | |
585 | int fd; | |
586 | _syscall(fd = open(path, O_RDONLY)); | |
587 | ||
588 | struct stat stat; | |
589 | _syscall(fstat(fd, &stat)); | |
590 | size_t size(stat.st_size); | |
591 | ||
592 | *psize = size; | |
593 | ||
594 | void *base; | |
595 | _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); | |
596 | ||
597 | _syscall(close(fd)); | |
598 | return base; | |
599 | } | |
600 | ||
601 | void InjectLibrary(pid_t pid); | |
602 | ||
603 | int Main(int argc, char const * const argv[], char const * const envp[]) { | |
604 | _aprcall(apr_initialize()); | |
605 | ||
606 | apr_pool_t *pool; | |
607 | apr_pool_create(&pool, NULL); | |
608 | ||
609 | bool tty(isatty(STDIN_FILENO)); | |
610 | bool compile(false); | |
611 | CYOptions options; | |
612 | ||
613 | append_history$ = (int (*)(int, const char *)) (dlsym(RTLD_DEFAULT, "append_history")); | |
614 | ||
615 | #ifdef CY_ATTACH | |
616 | pid_t pid(_not(pid_t)); | |
617 | #endif | |
618 | ||
619 | apr_getopt_t *state; | |
620 | _aprcall(apr_getopt_init(&state, pool, argc, argv)); | |
621 | ||
622 | for (;;) { | |
623 | char opt; | |
624 | const char *arg; | |
625 | ||
626 | apr_status_t status(apr_getopt(state, | |
627 | "cg:n:" | |
628 | #ifdef CY_ATTACH | |
629 | "p:" | |
630 | #endif | |
631 | "s" | |
632 | , &opt, &arg)); | |
633 | ||
634 | switch (status) { | |
635 | case APR_EOF: | |
636 | goto getopt; | |
637 | case APR_BADCH: | |
638 | case APR_BADARG: | |
639 | fprintf(stderr, | |
640 | "usage: cycript [-c]" | |
641 | #ifdef CY_ATTACH | |
642 | " [-p <pid|name>]" | |
643 | #endif | |
644 | " [<script> [<arg>...]]\n" | |
645 | ); | |
646 | return 1; | |
647 | default: | |
648 | _aprcall(status); | |
649 | } | |
650 | ||
651 | switch (opt) { | |
652 | case 'c': | |
653 | compile = true; | |
654 | break; | |
655 | ||
656 | case 'g': | |
657 | if (false); | |
658 | else if (strcmp(arg, "rename") == 0) | |
659 | options.verbose_ = true; | |
660 | #if YYDEBUG | |
661 | else if (strcmp(arg, "bison") == 0) | |
662 | bison_ = true; | |
663 | #endif | |
664 | else { | |
665 | fprintf(stderr, "invalid name for -g\n"); | |
666 | return 1; | |
667 | } | |
668 | break; | |
669 | ||
670 | case 'n': | |
671 | if (false); | |
672 | else if (strcmp(arg, "minify") == 0) | |
673 | pretty_ = true; | |
674 | else { | |
675 | fprintf(stderr, "invalid name for -n\n"); | |
676 | return 1; | |
677 | } | |
678 | break; | |
679 | ||
680 | #ifdef CY_ATTACH | |
681 | case 'p': { | |
682 | size_t size(strlen(arg)); | |
683 | char *end; | |
684 | ||
685 | pid = strtoul(arg, &end, 0); | |
686 | if (arg + size != end) { | |
687 | // XXX: arg needs to be escaped in some horrendous way of doom | |
688 | const char *command(apr_pstrcat(pool, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^", arg, " /{s/^[^ ]* //;q;};};d'", NULL)); | |
689 | ||
690 | if (FILE *pids = popen(command, "r")) { | |
691 | char value[32]; | |
692 | size = 0; | |
693 | ||
694 | for (;;) { | |
695 | size_t read(fread(value + size, 1, sizeof(value) - size, pids)); | |
696 | if (read == 0) | |
697 | break; | |
698 | else { | |
699 | size += read; | |
700 | if (size == sizeof(value)) { | |
701 | pid = _not(pid_t); | |
702 | goto fail; | |
703 | } | |
704 | } | |
705 | } | |
706 | ||
707 | size: | |
708 | if (size == 0) | |
709 | goto fail; | |
710 | if (value[size - 1] == '\n') { | |
711 | --size; | |
712 | goto size; | |
713 | } | |
714 | ||
715 | value[size] = '\0'; | |
716 | size = strlen(value); | |
717 | pid = strtoul(value, &end, 0); | |
718 | if (value + size != end) fail: | |
719 | pid = _not(pid_t); | |
720 | _syscall(pclose(pids)); | |
721 | } | |
722 | ||
723 | if (pid == _not(pid_t)) { | |
724 | fprintf(stderr, "invalid pid for -p\n"); | |
725 | return 1; | |
726 | } | |
727 | } | |
728 | } break; | |
729 | #endif | |
730 | ||
731 | case 's': | |
732 | strict_ = true; | |
733 | break; | |
734 | } | |
735 | } getopt:; | |
736 | ||
737 | const char *script; | |
738 | int ind(state->ind); | |
739 | ||
740 | #ifdef CY_ATTACH | |
741 | if (pid != _not(pid_t) && ind < argc - 1) { | |
742 | fprintf(stderr, "-p cannot set argv\n"); | |
743 | return 1; | |
744 | } | |
745 | ||
746 | if (pid != _not(pid_t) && compile) { | |
747 | fprintf(stderr, "-p conflicts with -c\n"); | |
748 | return 1; | |
749 | } | |
750 | #endif | |
751 | ||
752 | if (ind == argc) | |
753 | script = NULL; | |
754 | else { | |
755 | #ifdef CY_EXECUTE | |
756 | // XXX: const_cast?! wtf gcc :( | |
757 | CYSetArgs(argc - ind - 1, const_cast<const char **>(argv + ind + 1)); | |
758 | #endif | |
759 | script = argv[ind]; | |
760 | if (strcmp(script, "-") == 0) | |
761 | script = NULL; | |
762 | } | |
763 | ||
764 | #ifdef CY_ATTACH | |
765 | if (pid != _not(pid_t) && script == NULL && !tty) { | |
766 | fprintf(stderr, "non-terminal attaching to remote console\n"); | |
767 | return 1; | |
768 | } | |
769 | #endif | |
770 | ||
771 | #ifdef CY_ATTACH | |
772 | if (pid == _not(pid_t)) | |
773 | client_ = -1; | |
774 | else { | |
775 | int server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0))); try { | |
776 | struct sockaddr_un address; | |
777 | memset(&address, 0, sizeof(address)); | |
778 | address.sun_family = AF_UNIX; | |
779 | ||
780 | sprintf(address.sun_path, "/tmp/.s.cy.%u", getpid()); | |
781 | ||
782 | _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
783 | _syscall(chmod(address.sun_path, 0777)); | |
784 | ||
785 | try { | |
786 | _syscall(listen(server, 1)); | |
787 | InjectLibrary(pid); | |
788 | client_ = _syscall(accept(server, NULL, NULL)); | |
789 | } catch (...) { | |
790 | // XXX: exception? | |
791 | unlink(address.sun_path); | |
792 | throw; | |
793 | } | |
794 | } catch (...) { | |
795 | _syscall(close(server)); | |
796 | throw; | |
797 | } | |
798 | } | |
799 | #else | |
800 | client_ = -1; | |
801 | #endif | |
802 | ||
803 | if (script == NULL && tty) | |
804 | Console(options); | |
805 | else { | |
806 | CYLocalPool pool; | |
807 | ||
808 | char *start, *end; | |
809 | std::istream *indirect; | |
810 | ||
811 | if (script == NULL) { | |
812 | start = NULL; | |
813 | end = NULL; | |
814 | indirect = &std::cin; | |
815 | } else { | |
816 | size_t size; | |
817 | start = reinterpret_cast<char *>(Map(script, &size)); | |
818 | end = start + size; | |
819 | ||
820 | if (size >= 2 && start[0] == '#' && start[1] == '!') { | |
821 | start += 2; | |
822 | ||
823 | if (void *line = memchr(start, '\n', end - start)) | |
824 | start = reinterpret_cast<char *>(line); | |
825 | else | |
826 | start = end; | |
827 | } | |
828 | ||
829 | indirect = NULL; | |
830 | } | |
831 | ||
832 | CYStream direct(start, end); | |
833 | std::istream &stream(indirect == NULL ? direct : *indirect); | |
834 | CYDriver driver(stream, script ?: "<stdin>"); | |
835 | ||
836 | cy::parser parser(driver); | |
837 | Setup(driver, parser); | |
838 | ||
839 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
840 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) | |
841 | std::cerr << i->location_.begin << ": " << i->message_ << std::endl; | |
842 | } else if (driver.program_ != NULL) | |
843 | if (client_ != -1) { | |
844 | // XXX: this code means that you can't pipe to another process | |
845 | std::string code(start, end-start); | |
846 | Run(client_, false, code, &std::cout); | |
847 | } else { | |
848 | std::ostringstream str; | |
849 | CYOutput out(str, options); | |
850 | Setup(out, driver, options); | |
851 | out << *driver.program_; | |
852 | std::string code(str.str()); | |
853 | if (compile) | |
854 | std::cout << code; | |
855 | else | |
856 | Run(client_, false, code, &std::cout); | |
857 | } | |
858 | } | |
859 | ||
860 | apr_pool_destroy(pool); | |
861 | ||
862 | return 0; | |
863 | } | |
864 | ||
865 | int main(int argc, char const * const argv[], char const * const envp[]) { | |
866 | apr_status_t status(apr_app_initialize(&argc, &argv, &envp)); | |
867 | ||
868 | if (status != APR_SUCCESS) { | |
869 | fprintf(stderr, "apr_app_initialize() != APR_SUCCESS\n"); | |
870 | return 1; | |
871 | } else try { | |
872 | return Main(argc, argv, envp); | |
873 | } catch (const CYException &error) { | |
874 | CYPool pool; | |
875 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
876 | return 1; | |
877 | } | |
878 | } |