]>
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 | ||
36bf49c4 | 113 | void Setup(CYOutput &out, CYDriver &driver, CYOptions &options, bool lower) { |
11c1cc16 | 114 | out.pretty_ = pretty_; |
2eb8215d | 115 | CYContext context(options); |
36bf49c4 JF |
116 | if (lower) |
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 |
89d16b11 | 127 | json = CYExecute(CYGetJSContext(), 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 | ||
fd5cdf97 JF |
230 | CYStatement *statement(driver.program_->statements_); |
231 | _assert(statement != NULL); | |
232 | _assert(statement->next_ == NULL); | |
e06e5ee1 | 233 | |
fd5cdf97 JF |
234 | CYExpress *express(dynamic_cast<CYExpress *>(driver.program_->statements_)); |
235 | _assert(express != NULL); | |
236 | ||
237 | return express->expression_; | |
7e5391fd JF |
238 | } |
239 | ||
240 | static int client_; | |
241 | ||
242 | static char **Complete(const char *word, int start, int end) { | |
243 | rl_attempted_completion_over = TRUE; | |
244 | ||
2eb8215d | 245 | CYLocalPool pool; |
7e5391fd JF |
246 | |
247 | std::string line(rl_line_buffer, start); | |
d3b63265 JF |
248 | std::istringstream stream(command_ + line); |
249 | CYDriver driver(stream); | |
7e5391fd JF |
250 | |
251 | driver.auto_ = true; | |
252 | ||
d3b63265 JF |
253 | cy::parser parser(driver); |
254 | Setup(driver, parser); | |
255 | ||
7e5391fd JF |
256 | if (parser.parse() != 0 || !driver.errors_.empty()) |
257 | return NULL; | |
258 | ||
259 | if (driver.mode_ == CYDriver::AutoNone) | |
260 | return NULL; | |
261 | ||
262 | CYExpression *expression; | |
263 | ||
264 | CYOptions options; | |
2eb8215d | 265 | CYContext context(options); |
7e5391fd JF |
266 | |
267 | std::ostringstream prefix; | |
268 | ||
269 | switch (driver.mode_) { | |
270 | case CYDriver::AutoPrimary: | |
271 | expression = $ CYThis(); | |
272 | break; | |
273 | ||
274 | case CYDriver::AutoDirect: | |
275 | expression = driver.context_; | |
276 | break; | |
277 | ||
278 | case CYDriver::AutoIndirect: | |
279 | expression = $ CYIndirect(driver.context_); | |
280 | break; | |
281 | ||
282 | case CYDriver::AutoMessage: { | |
283 | CYDriver::Context &thing(driver.contexts_.back()); | |
aa926c8b | 284 | expression = $M($C1($V("object_getClass"), thing.context_), $S("messages")); |
7e5391fd JF |
285 | for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part) |
286 | prefix << (*part)->word_ << ':'; | |
287 | } break; | |
288 | ||
289 | default: | |
290 | _assert(false); | |
291 | } | |
292 | ||
2a18313c | 293 | std::string begin(prefix.str()); |
7e5391fd | 294 | |
2eb8215d | 295 | driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression( |
2a18313c | 296 | " function(object, prefix, word) {\n" |
7e5391fd | 297 | " var names = [];\n" |
5a3f7a01 JF |
298 | " var before = prefix.length;\n" |
299 | " prefix += word;\n" | |
300 | " var entire = prefix.length;\n" | |
3e6377db | 301 | " for (var name in object)\n" |
5a3f7a01 JF |
302 | " if (name.substring(0, entire) == prefix)\n" |
303 | " names.push(name.substr(before));\n" | |
7e5391fd JF |
304 | " return names;\n" |
305 | " }\n" | |
2a18313c | 306 | ), expression, $S(begin.c_str()), $S(word)))); |
7e5391fd JF |
307 | |
308 | driver.program_->Replace(context); | |
309 | ||
310 | std::ostringstream str; | |
311 | CYOutput out(str, options); | |
312 | out << *driver.program_; | |
313 | ||
314 | std::string code(str.str()); | |
315 | CYUTF8String json(Run(pool, client_, code)); | |
1b2613d3 | 316 | // XXX: if this fails we should not try to parse it |
7e5391fd | 317 | |
2eb8215d | 318 | CYExpression *result(ParseExpression(json)); |
df24434f JF |
319 | if (result == NULL) |
320 | return NULL; | |
321 | ||
fd5cdf97 | 322 | CYArray *array(dynamic_cast<CYArray *>(result->Primitive(context))); |
a3f4a8e1 | 323 | if (array == NULL) { |
2e2ed904 | 324 | *out_ << '\n'; |
82a02ede | 325 | Output(false, json, out_); |
a3f4a8e1 JF |
326 | rl_forced_update_display(); |
327 | return NULL; | |
328 | } | |
7e5391fd JF |
329 | |
330 | // XXX: use an std::set? | |
331 | typedef std::vector<std::string> Completions; | |
332 | Completions completions; | |
333 | ||
334 | std::string common; | |
335 | bool rest(false); | |
336 | ||
c2c9f509 | 337 | CYForEach (element, array->elements_) { |
7e5391fd JF |
338 | CYString *string(dynamic_cast<CYString *>(element->value_)); |
339 | _assert(string != NULL); | |
340 | ||
763aa499 JF |
341 | std::string completion; |
342 | if (string->size_ != 0) | |
343 | completion.assign(string->value_, string->size_); | |
344 | else if (driver.mode_ == CYDriver::AutoMessage) | |
345 | completion = "]"; | |
346 | else | |
347 | continue; | |
348 | ||
7e5391fd JF |
349 | completions.push_back(completion); |
350 | ||
351 | if (!rest) { | |
352 | common = completion; | |
353 | rest = true; | |
354 | } else { | |
355 | size_t limit(completion.size()), size(common.size()); | |
356 | if (size > limit) | |
357 | common = common.substr(0, limit); | |
358 | else | |
359 | limit = size; | |
360 | for (limit = 0; limit != size; ++limit) | |
361 | if (common[limit] != completion[limit]) | |
362 | break; | |
363 | if (limit != size) | |
364 | common = common.substr(0, limit); | |
365 | } | |
366 | } | |
367 | ||
368 | size_t count(completions.size()); | |
369 | if (count == 0) | |
370 | return NULL; | |
371 | ||
c41590b2 JF |
372 | size_t colon(common.find(':')); |
373 | if (colon != std::string::npos) | |
374 | common = common.substr(0, colon + 1); | |
6dccefa9 JF |
375 | if (completions.size() == 1) |
376 | common += ' '; | |
c41590b2 | 377 | |
7e5391fd JF |
378 | char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count + 2)))); |
379 | ||
380 | results[0] = strdup(common.c_str()); | |
381 | size_t index(0); | |
382 | for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i) | |
383 | results[++index] = strdup(i->c_str()); | |
384 | results[count + 1] = NULL; | |
385 | ||
386 | return results; | |
387 | } | |
388 | ||
389 | // need char *, not const char * | |
390 | static char name_[] = "cycript"; | |
108c5fc8 | 391 | static char break_[] = " \t\n\"\\'`@$><=;|&{(" ")}" ".:[]"; |
7e5391fd | 392 | |
da136f88 JF |
393 | class History { |
394 | private: | |
395 | std::string histfile_; | |
396 | size_t histlines_; | |
397 | ||
398 | public: | |
399 | History(std::string histfile) : | |
400 | histfile_(histfile), | |
401 | histlines_(0) | |
402 | { | |
403 | read_history(histfile_.c_str()); | |
404 | } | |
405 | ||
406 | ~History() { | |
407 | if (append_history$ != NULL) { | |
408 | _syscall(close(_syscall(open(histfile_.c_str(), O_CREAT | O_WRONLY, 0600)))); | |
409 | _assert((*append_history$)(histlines_, histfile_.c_str()) == 0); | |
410 | } else { | |
411 | _assert(write_history(histfile_.c_str()) == 0); | |
412 | } | |
413 | } | |
414 | ||
415 | void operator +=(const std::string &command) { | |
416 | add_history(command_.c_str()); | |
417 | ++histlines_; | |
418 | } | |
419 | }; | |
420 | ||
2eb8215d JF |
421 | static void Console(CYOptions &options) { |
422 | CYPool pool; | |
423 | ||
b1589845 JF |
424 | passwd *passwd; |
425 | if (const char *username = getenv("LOGNAME")) | |
426 | passwd = getpwnam(username); | |
427 | else | |
428 | passwd = getpwuid(getuid()); | |
429 | ||
da136f88 JF |
430 | std::string basedir(passwd->pw_dir); |
431 | basedir += "/.cycript"; | |
432 | mkdir(basedir.c_str(), 0700); | |
b1589845 | 433 | |
7e5391fd JF |
434 | rl_initialize(); |
435 | rl_readline_name = name_; | |
436 | ||
da136f88 | 437 | History history(basedir + "/history"); |
b1589845 | 438 | |
1f8eae40 JF |
439 | bool bypass(false); |
440 | bool debug(false); | |
6ec85c5b | 441 | bool expand(false); |
36bf49c4 | 442 | bool lower(true); |
cd98d280 | 443 | bool syntax(true); |
1f8eae40 | 444 | |
2e2ed904 | 445 | out_ = &std::cout; |
057f943f | 446 | |
7e5391fd JF |
447 | // rl_completer_word_break_characters is broken in libedit |
448 | rl_basic_word_break_characters = break_; | |
bc1e87aa JF |
449 | |
450 | rl_completer_word_break_characters = break_; | |
7e5391fd JF |
451 | rl_attempted_completion_function = &Complete; |
452 | rl_bind_key('\t', rl_complete); | |
057f943f JF |
453 | |
454 | struct sigaction action; | |
455 | sigemptyset(&action.sa_mask); | |
456 | action.sa_handler = &sigint; | |
457 | action.sa_flags = 0; | |
458 | sigaction(SIGINT, &action, NULL); | |
459 | ||
460 | restart: for (;;) { | |
7e5391fd | 461 | command_.clear(); |
057f943f JF |
462 | std::vector<std::string> lines; |
463 | ||
931b816a JF |
464 | bool extra(false); |
465 | const char *prompt("cy# "); | |
466 | ||
057f943f | 467 | if (setjmp(ctrlc_) != 0) { |
4e39dc0b | 468 | mode_ = Working; |
2e2ed904 | 469 | *out_ << std::endl; |
057f943f JF |
470 | goto restart; |
471 | } | |
472 | ||
057f943f | 473 | read: |
cd98d280 JF |
474 | |
475 | #if RL_READLINE_VERSION >= 0x0600 | |
8a81192b | 476 | if (syntax) |
cd98d280 | 477 | rl_redisplay_function = CYDisplayUpdate; |
8a81192b | 478 | else |
cd98d280 | 479 | rl_redisplay_function = rl_redisplay; |
cd98d280 JF |
480 | #endif |
481 | ||
4e39dc0b | 482 | mode_ = Parsing; |
057f943f | 483 | char *line(readline(prompt)); |
4e39dc0b | 484 | mode_ = Working; |
cd98d280 | 485 | |
79357996 JF |
486 | if (line == NULL) { |
487 | *out_ << std::endl; | |
057f943f | 488 | break; |
79357996 | 489 | } else if (line[0] == '\0') |
67f1fc6b | 490 | goto read; |
931b816a JF |
491 | |
492 | if (!extra) { | |
493 | extra = true; | |
6ec85c5b | 494 | if (line[0] == '?') { |
1f8eae40 JF |
495 | std::string data(line + 1); |
496 | if (data == "bypass") { | |
497 | bypass = !bypass; | |
2e2ed904 | 498 | *out_ << "bypass == " << (bypass ? "true" : "false") << std::endl; |
1f8eae40 JF |
499 | } else if (data == "debug") { |
500 | debug = !debug; | |
2e2ed904 | 501 | *out_ << "debug == " << (debug ? "true" : "false") << std::endl; |
8fab8594 JF |
502 | } else if (data == "destroy") { |
503 | CYDestroyContext(); | |
51b6165e | 504 | } else if (data == "gc") { |
e7ff0158 | 505 | *out_ << "collecting... " << std::flush; |
51b6165e | 506 | CYGarbageCollect(CYGetJSContext()); |
e7ff0158 | 507 | *out_ << "done." << std::endl; |
6a5fd0d9 JF |
508 | } else if (data == "exit") { |
509 | return; | |
6ec85c5b JF |
510 | } else if (data == "expand") { |
511 | expand = !expand; | |
2e2ed904 | 512 | *out_ << "expand == " << (expand ? "true" : "false") << std::endl; |
36bf49c4 JF |
513 | } else if (data == "lower") { |
514 | lower = !lower; | |
515 | *out_ << "lower == " << (lower ? "true" : "false") << std::endl; | |
82a02ede JF |
516 | } else if (data == "syntax") { |
517 | syntax = !syntax; | |
518 | *out_ << "syntax == " << (syntax ? "true" : "false") << std::endl; | |
1f8eae40 | 519 | } |
be8c2078 JF |
520 | command_ = line; |
521 | history += command_; | |
931b816a JF |
522 | goto restart; |
523 | } | |
524 | } | |
525 | ||
7e5391fd | 526 | command_ += line; |
e818f0e0 JF |
527 | |
528 | char *begin(line), *end(line + strlen(line)); | |
529 | while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) { | |
530 | *nl = '\0'; | |
531 | lines.push_back(begin); | |
532 | begin = nl + 1; | |
533 | } | |
534 | ||
535 | lines.push_back(begin); | |
536 | ||
057f943f JF |
537 | free(line); |
538 | ||
1f8eae40 JF |
539 | std::string code; |
540 | ||
541 | if (bypass) | |
7e5391fd | 542 | code = command_; |
1f8eae40 | 543 | else { |
2eb8215d | 544 | CYLocalPool pool; |
d3b63265 JF |
545 | |
546 | std::istringstream stream(command_); | |
547 | CYDriver driver(stream); | |
548 | ||
1f8eae40 | 549 | cy::parser parser(driver); |
b10bd496 | 550 | Setup(driver, parser); |
1f8eae40 | 551 | |
1f8eae40 | 552 | if (parser.parse() != 0 || !driver.errors_.empty()) { |
f0360d51 JF |
553 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { |
554 | cy::position begin(error->location_.begin); | |
c247172c | 555 | if (begin.line != lines.size() || begin.column < lines.back().size() || error->warning_) { |
f0360d51 | 556 | cy::position end(error->location_.end); |
48e3be8a | 557 | |
f0360d51 JF |
558 | if (begin.line != lines.size()) { |
559 | std::cerr << " | "; | |
560 | std::cerr << lines[begin.line - 1] << std::endl; | |
561 | } | |
48e3be8a | 562 | |
c52a09b8 | 563 | std::cerr << "...."; |
97c8a8fc | 564 | for (size_t i(0); i != begin.column; ++i) |
f0360d51 | 565 | std::cerr << '.'; |
48e3be8a | 566 | if (begin.line != end.line || begin.column == end.column) |
f0360d51 JF |
567 | std::cerr << '^'; |
568 | else for (size_t i(0), e(end.column - begin.column); i != e; ++i) | |
569 | std::cerr << '^'; | |
570 | std::cerr << std::endl; | |
48e3be8a | 571 | |
f0360d51 JF |
572 | std::cerr << " | "; |
573 | std::cerr << error->message_ << std::endl; | |
48e3be8a | 574 | |
da136f88 | 575 | history += command_; |
1f8eae40 JF |
576 | goto restart; |
577 | } | |
578 | } | |
057f943f | 579 | |
1f8eae40 | 580 | driver.errors_.clear(); |
057f943f | 581 | |
7e5391fd | 582 | command_ += '\n'; |
1f8eae40 JF |
583 | prompt = "cy> "; |
584 | goto read; | |
057f943f JF |
585 | } |
586 | ||
b10bd496 | 587 | if (driver.program_ == NULL) |
1f8eae40 | 588 | goto restart; |
057f943f | 589 | |
0df6a201 JF |
590 | std::ostringstream str; |
591 | CYOutput out(str, options); | |
36bf49c4 | 592 | Setup(out, driver, options, lower); |
0df6a201 JF |
593 | out << *driver.program_; |
594 | code = str.str(); | |
057f943f JF |
595 | } |
596 | ||
da136f88 | 597 | history += command_; |
057f943f | 598 | |
82a02ede | 599 | if (debug) { |
f43fcb85 | 600 | std::cout << "cy= "; |
82a02ede JF |
601 | Write(syntax, code.c_str(), code.size(), std::cout); |
602 | std::cout << std::endl; | |
603 | } | |
057f943f | 604 | |
82a02ede | 605 | Run(client_, syntax, code, out_, expand); |
b09da87b | 606 | } |
b09da87b | 607 | } |
057f943f | 608 | |
b6961e53 | 609 | void InjectLibrary(pid_t pid); |
06edab7d | 610 | |
b6961e53 | 611 | int Main(int argc, char const * const argv[], char const * const envp[]) { |
0cbeddf8 JF |
612 | _aprcall(apr_initialize()); |
613 | ||
614 | apr_pool_t *pool; | |
615 | apr_pool_create(&pool, NULL); | |
616 | ||
48e3be8a | 617 | bool tty(isatty(STDIN_FILENO)); |
75b0a457 | 618 | bool compile(false); |
de9fc71b | 619 | CYOptions options; |
967067aa | 620 | |
e4676127 | 621 | append_history$ = (int (*)(int, const char *)) (dlsym(RTLD_DEFAULT, "append_history")); |
da858962 | 622 | |
2aa77fd8 JF |
623 | #ifdef CY_ATTACH |
624 | pid_t pid(_not(pid_t)); | |
625 | #endif | |
626 | ||
666eedb9 JF |
627 | const char *host(NULL); |
628 | const char *port(NULL); | |
629 | ||
06edab7d JF |
630 | apr_getopt_t *state; |
631 | _aprcall(apr_getopt_init(&state, pool, argc, argv)); | |
632 | ||
633 | for (;;) { | |
634 | char opt; | |
635 | const char *arg; | |
636 | ||
637 | apr_status_t status(apr_getopt(state, | |
638 | "cg:n:" | |
2aa77fd8 | 639 | #ifdef CY_ATTACH |
06edab7d | 640 | "p:" |
2aa77fd8 | 641 | #endif |
666eedb9 | 642 | "r:" |
06edab7d JF |
643 | "s" |
644 | , &opt, &arg)); | |
645 | ||
646 | switch (status) { | |
647 | case APR_EOF: | |
648 | goto getopt; | |
649 | case APR_BADCH: | |
650 | case APR_BADARG: | |
651 | fprintf(stderr, | |
652 | "usage: cycript [-c]" | |
2aa77fd8 | 653 | #ifdef CY_ATTACH |
69caa5be | 654 | " [-p <pid|name>]" |
2aa77fd8 | 655 | #endif |
666eedb9 | 656 | " [-r <host:port>]" |
06edab7d JF |
657 | " [<script> [<arg>...]]\n" |
658 | ); | |
659 | return 1; | |
660 | default: | |
661 | _aprcall(status); | |
662 | } | |
967067aa | 663 | |
06edab7d JF |
664 | switch (opt) { |
665 | case 'c': | |
666 | compile = true; | |
667 | break; | |
75b0a457 | 668 | |
06edab7d JF |
669 | case 'g': |
670 | if (false); | |
de9fc71b JF |
671 | else if (strcmp(arg, "rename") == 0) |
672 | options.verbose_ = true; | |
cac61857 | 673 | #if YYDEBUG |
5ef46cc0 | 674 | else if (strcmp(arg, "bison") == 0) |
06edab7d | 675 | bison_ = true; |
cac61857 | 676 | #endif |
06edab7d JF |
677 | else { |
678 | fprintf(stderr, "invalid name for -g\n"); | |
679 | return 1; | |
680 | } | |
681 | break; | |
cac61857 | 682 | |
06edab7d JF |
683 | case 'n': |
684 | if (false); | |
5ef46cc0 | 685 | else if (strcmp(arg, "minify") == 0) |
06edab7d JF |
686 | pretty_ = true; |
687 | else { | |
688 | fprintf(stderr, "invalid name for -n\n"); | |
689 | return 1; | |
690 | } | |
691 | break; | |
11c1cc16 | 692 | |
2aa77fd8 | 693 | #ifdef CY_ATTACH |
06edab7d | 694 | case 'p': { |
5ef46cc0 | 695 | size_t size(strlen(arg)); |
06edab7d | 696 | char *end; |
69caa5be | 697 | |
5ef46cc0 JF |
698 | pid = strtoul(arg, &end, 0); |
699 | if (arg + size != end) { | |
69caa5be | 700 | // XXX: arg needs to be escaped in some horrendous way of doom |
0cbeddf8 | 701 | 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 |
702 | |
703 | if (FILE *pids = popen(command, "r")) { | |
704 | char value[32]; | |
705 | size = 0; | |
706 | ||
707 | for (;;) { | |
708 | size_t read(fread(value + size, 1, sizeof(value) - size, pids)); | |
709 | if (read == 0) | |
710 | break; | |
711 | else { | |
712 | size += read; | |
04aa6240 | 713 | if (size == sizeof(value)) |
b166b11b | 714 | goto fail; |
69caa5be JF |
715 | } |
716 | } | |
717 | ||
718 | size: | |
719 | if (size == 0) | |
b166b11b | 720 | goto fail; |
69caa5be JF |
721 | if (value[size - 1] == '\n') { |
722 | --size; | |
723 | goto size; | |
724 | } | |
725 | ||
726 | value[size] = '\0'; | |
727 | size = strlen(value); | |
728 | pid = strtoul(value, &end, 0); | |
b166b11b | 729 | if (value + size != end) fail: |
69caa5be | 730 | pid = _not(pid_t); |
69caa5be JF |
731 | _syscall(pclose(pids)); |
732 | } | |
733 | ||
734 | if (pid == _not(pid_t)) { | |
04aa6240 | 735 | fprintf(stderr, "unable to find process `%s' using ps\n", arg); |
69caa5be JF |
736 | return 1; |
737 | } | |
06edab7d JF |
738 | } |
739 | } break; | |
2aa77fd8 | 740 | #endif |
b10bd496 | 741 | |
666eedb9 JF |
742 | case 'r': { |
743 | //size_t size(strlen(arg)); | |
744 | ||
745 | char *colon(strrchr(arg, ':')); | |
746 | if (colon == NULL) { | |
747 | fprintf(stderr, "missing colon in hostspec\n"); | |
748 | return 1; | |
749 | } | |
750 | ||
751 | /*char *end; | |
752 | port = strtoul(colon + 1, &end, 10); | |
753 | if (end != arg + size) { | |
754 | fprintf(stderr, "invalid port in hostspec\n"); | |
755 | return 1; | |
756 | }*/ | |
757 | ||
758 | host = arg; | |
759 | *colon = '\0'; | |
760 | port = colon + 1; | |
761 | } break; | |
762 | ||
06edab7d JF |
763 | case 's': |
764 | strict_ = true; | |
765 | break; | |
766 | } | |
967067aa JF |
767 | } getopt:; |
768 | ||
b09da87b | 769 | const char *script; |
5ef46cc0 | 770 | int ind(state->ind); |
b09da87b | 771 | |
2aa77fd8 | 772 | #ifdef CY_ATTACH |
5ef46cc0 | 773 | if (pid != _not(pid_t) && ind < argc - 1) { |
fcc64bb5 JF |
774 | fprintf(stderr, "-p cannot set argv\n"); |
775 | return 1; | |
776 | } | |
777 | ||
75b0a457 JF |
778 | if (pid != _not(pid_t) && compile) { |
779 | fprintf(stderr, "-p conflicts with -c\n"); | |
780 | return 1; | |
781 | } | |
2aa77fd8 | 782 | #endif |
75b0a457 | 783 | |
5ef46cc0 | 784 | if (ind == argc) |
b09da87b JF |
785 | script = NULL; |
786 | else { | |
9185d5ef | 787 | #ifdef CY_EXECUTE |
967067aa | 788 | // XXX: const_cast?! wtf gcc :( |
5ef46cc0 | 789 | CYSetArgs(argc - ind - 1, const_cast<const char **>(argv + ind + 1)); |
9185d5ef | 790 | #endif |
5ef46cc0 | 791 | script = argv[ind]; |
967067aa JF |
792 | if (strcmp(script, "-") == 0) |
793 | script = NULL; | |
b09da87b JF |
794 | } |
795 | ||
2aa77fd8 JF |
796 | #ifdef CY_ATTACH |
797 | if (pid != _not(pid_t) && script == NULL && !tty) { | |
798 | fprintf(stderr, "non-terminal attaching to remote console\n"); | |
48e3be8a JF |
799 | return 1; |
800 | } | |
2aa77fd8 | 801 | #endif |
48e3be8a | 802 | |
2aa77fd8 | 803 | #ifdef CY_ATTACH |
967067aa | 804 | if (pid == _not(pid_t)) |
7e5391fd | 805 | client_ = -1; |
967067aa | 806 | else { |
b166b11b JF |
807 | int server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0))); try { |
808 | struct sockaddr_un address; | |
809 | memset(&address, 0, sizeof(address)); | |
810 | address.sun_family = AF_UNIX; | |
811 | ||
812 | sprintf(address.sun_path, "/tmp/.s.cy.%u", getpid()); | |
813 | ||
814 | _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
815 | _syscall(chmod(address.sun_path, 0777)); | |
816 | ||
817 | try { | |
818 | _syscall(listen(server, 1)); | |
819 | InjectLibrary(pid); | |
7e5391fd | 820 | client_ = _syscall(accept(server, NULL, NULL)); |
b166b11b JF |
821 | } catch (...) { |
822 | // XXX: exception? | |
823 | unlink(address.sun_path); | |
824 | throw; | |
825 | } | |
826 | } catch (...) { | |
827 | _syscall(close(server)); | |
95678376 | 828 | throw; |
b166b11b | 829 | } |
967067aa | 830 | } |
2aa77fd8 | 831 | #else |
7e5391fd | 832 | client_ = -1; |
2aa77fd8 | 833 | #endif |
579ed526 | 834 | |
666eedb9 JF |
835 | if (client_ == -1 && host != NULL && port != NULL) { |
836 | struct addrinfo hints; | |
837 | memset(&hints, 0, sizeof(hints)); | |
838 | hints.ai_family = AF_UNSPEC; | |
839 | hints.ai_socktype = SOCK_STREAM; | |
840 | hints.ai_protocol = 0; | |
841 | hints.ai_flags = 0; | |
842 | ||
843 | struct addrinfo *infos; | |
844 | _syscall(getaddrinfo(host, port, &hints, &infos)); | |
845 | ||
846 | _assert(infos != NULL); try { | |
847 | for (struct addrinfo *info(infos); info != NULL; info = info->ai_next) { | |
848 | int client(_syscall(socket(info->ai_family, info->ai_socktype, info->ai_protocol))); try { | |
849 | _syscall(connect(client, info->ai_addr, info->ai_addrlen)); | |
850 | client_ = client; | |
851 | break; | |
852 | } catch (...) { | |
853 | _syscall(close(client)); | |
854 | throw; | |
855 | } | |
856 | } | |
857 | } catch (...) { | |
858 | freeaddrinfo(infos); | |
859 | throw; | |
860 | } | |
861 | } | |
862 | ||
48e3be8a | 863 | if (script == NULL && tty) |
2eb8215d | 864 | Console(options); |
b09da87b | 865 | else { |
2eb8215d | 866 | CYLocalPool pool; |
b09da87b | 867 | |
0df6a201 | 868 | std::istream *stream; |
48e3be8a | 869 | if (script == NULL) { |
0df6a201 JF |
870 | stream = &std::cin; |
871 | script = "<stdin>"; | |
48e3be8a | 872 | } else { |
0df6a201 JF |
873 | stream = new std::fstream(script, std::ios::in | std::ios::binary); |
874 | _assert(!stream->fail()); | |
48e3be8a | 875 | } |
62ca2b82 | 876 | |
0df6a201 | 877 | CYDriver driver(*stream, script); |
d3b63265 JF |
878 | cy::parser parser(driver); |
879 | Setup(driver, parser); | |
880 | ||
b09da87b JF |
881 | if (parser.parse() != 0 || !driver.errors_.empty()) { |
882 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) | |
883 | std::cerr << i->location_.begin << ": " << i->message_ << std::endl; | |
0df6a201 JF |
884 | } else if (driver.program_ != NULL) { |
885 | std::ostringstream str; | |
886 | CYOutput out(str, options); | |
36bf49c4 | 887 | Setup(out, driver, options, true); |
0df6a201 JF |
888 | out << *driver.program_; |
889 | std::string code(str.str()); | |
890 | if (compile) | |
891 | std::cout << code; | |
892 | else | |
82a02ede | 893 | Run(client_, false, code, &std::cout); |
0df6a201 | 894 | } |
057f943f | 895 | } |
62ca2b82 | 896 | |
0cbeddf8 JF |
897 | apr_pool_destroy(pool); |
898 | ||
057f943f | 899 | return 0; |
62ca2b82 | 900 | } |
b6961e53 JF |
901 | |
902 | int main(int argc, char const * const argv[], char const * const envp[]) { | |
903 | apr_status_t status(apr_app_initialize(&argc, &argv, &envp)); | |
da858962 | 904 | |
b6961e53 JF |
905 | if (status != APR_SUCCESS) { |
906 | fprintf(stderr, "apr_app_initialize() != APR_SUCCESS\n"); | |
907 | return 1; | |
908 | } else try { | |
909 | return Main(argc, argv, envp); | |
910 | } catch (const CYException &error) { | |
911 | CYPool pool; | |
912 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
913 | return 1; | |
914 | } | |
915 | } |