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