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