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