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