]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
c1d3e52e | 2 | * Copyright (C) 2009-2015 Jay Freeman (saurik) |
b4aa79af JF |
3 | */ |
4 | ||
f95d2598 | 5 | /* GNU Affero General Public License, Version 3 {{{ */ |
b4aa79af | 6 | /* |
f95d2598 JF |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU Affero General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | ||
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
c15969fd | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f95d2598 JF |
15 | * GNU Affero General Public License for more details. |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
b3378a02 | 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 | 46 | #include <errno.h> |
10d0e915 JF |
47 | #include <getopt.h> |
48 | #include <signal.h> | |
b09da87b JF |
49 | #include <unistd.h> |
50 | ||
666eedb9 | 51 | #include <sys/socket.h> |
b09da87b JF |
52 | #include <sys/types.h> |
53 | #include <sys/stat.h> | |
54 | #include <fcntl.h> | |
666eedb9 | 55 | #include <netdb.h> |
b09da87b | 56 | |
967067aa JF |
57 | #include <sys/types.h> |
58 | #include <sys/socket.h> | |
59 | #include <netinet/in.h> | |
60 | #include <sys/un.h> | |
b1589845 | 61 | #include <pwd.h> |
967067aa | 62 | |
da858962 JF |
63 | #include <dlfcn.h> |
64 | ||
74296173 | 65 | #include "Display.hpp" |
b12a9965 JF |
66 | #include "Replace.hpp" |
67 | ||
68 | #include "Cycript.tab.hh" | |
69 | #include "Driver.hpp" | |
7e5391fd | 70 | |
4e39dc0b JF |
71 | static volatile enum { |
72 | Working, | |
73 | Parsing, | |
74 | Running, | |
75 | Sending, | |
76 | Waiting, | |
77 | } mode_; | |
78 | ||
057f943f JF |
79 | static jmp_buf ctrlc_; |
80 | ||
579ed526 | 81 | static void sigint(int) { |
4e39dc0b JF |
82 | switch (mode_) { |
83 | case Working: | |
84 | return; | |
85 | case Parsing: | |
86 | longjmp(ctrlc_, 1); | |
87 | case Running: | |
b0ba908c JF |
88 | CYCancel(); |
89 | return; | |
4e39dc0b JF |
90 | case Sending: |
91 | return; | |
92 | case Waiting: | |
93 | return; | |
94 | } | |
057f943f JF |
95 | } |
96 | ||
cac61857 JF |
97 | #if YYDEBUG |
98 | static bool bison_; | |
99 | #endif | |
b10bd496 | 100 | static bool strict_; |
11c1cc16 | 101 | static bool pretty_; |
cac61857 | 102 | |
b10bd496 | 103 | void Setup(CYDriver &driver, cy::parser &parser) { |
cac61857 JF |
104 | #if YYDEBUG |
105 | if (bison_) | |
106 | parser.set_debug_level(1); | |
107 | #endif | |
b10bd496 JF |
108 | if (strict_) |
109 | driver.strict_ = true; | |
cac61857 JF |
110 | } |
111 | ||
36bf49c4 | 112 | void Setup(CYOutput &out, CYDriver &driver, CYOptions &options, bool lower) { |
11c1cc16 | 113 | out.pretty_ = pretty_; |
2eb8215d | 114 | CYContext context(options); |
36bf49c4 JF |
115 | if (lower) |
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 | ||
fd5cdf97 JF |
229 | CYStatement *statement(driver.program_->statements_); |
230 | _assert(statement != NULL); | |
231 | _assert(statement->next_ == NULL); | |
e06e5ee1 | 232 | |
fd5cdf97 JF |
233 | CYExpress *express(dynamic_cast<CYExpress *>(driver.program_->statements_)); |
234 | _assert(express != NULL); | |
235 | ||
236 | return express->expression_; | |
7e5391fd JF |
237 | } |
238 | ||
239 | static int client_; | |
240 | ||
241 | static char **Complete(const char *word, int start, int end) { | |
10d0e915 | 242 | rl_attempted_completion_over = ~0; |
7e5391fd | 243 | |
2eb8215d | 244 | CYLocalPool pool; |
7e5391fd JF |
245 | |
246 | std::string line(rl_line_buffer, start); | |
d3b63265 JF |
247 | std::istringstream stream(command_ + line); |
248 | CYDriver driver(stream); | |
7e5391fd JF |
249 | |
250 | driver.auto_ = true; | |
251 | ||
d3b63265 JF |
252 | cy::parser parser(driver); |
253 | Setup(driver, parser); | |
254 | ||
7e5391fd JF |
255 | if (parser.parse() != 0 || !driver.errors_.empty()) |
256 | return NULL; | |
257 | ||
258 | if (driver.mode_ == CYDriver::AutoNone) | |
259 | return NULL; | |
260 | ||
261 | CYExpression *expression; | |
262 | ||
263 | CYOptions options; | |
2eb8215d | 264 | CYContext context(options); |
7e5391fd JF |
265 | |
266 | std::ostringstream prefix; | |
267 | ||
268 | switch (driver.mode_) { | |
269 | case CYDriver::AutoPrimary: | |
270 | expression = $ CYThis(); | |
271 | break; | |
272 | ||
273 | case CYDriver::AutoDirect: | |
274 | expression = driver.context_; | |
275 | break; | |
276 | ||
277 | case CYDriver::AutoIndirect: | |
278 | expression = $ CYIndirect(driver.context_); | |
279 | break; | |
280 | ||
281 | case CYDriver::AutoMessage: { | |
282 | CYDriver::Context &thing(driver.contexts_.back()); | |
aa926c8b | 283 | expression = $M($C1($V("object_getClass"), thing.context_), $S("messages")); |
7e5391fd JF |
284 | for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part) |
285 | prefix << (*part)->word_ << ':'; | |
286 | } break; | |
287 | ||
288 | default: | |
289 | _assert(false); | |
290 | } | |
291 | ||
2a18313c | 292 | std::string begin(prefix.str()); |
7e5391fd | 293 | |
2eb8215d | 294 | driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression( |
2a18313c | 295 | " function(object, prefix, word) {\n" |
7e5391fd | 296 | " var names = [];\n" |
5a3f7a01 JF |
297 | " var before = prefix.length;\n" |
298 | " prefix += word;\n" | |
299 | " var entire = prefix.length;\n" | |
3e6377db | 300 | " for (var name in object)\n" |
5a3f7a01 JF |
301 | " if (name.substring(0, entire) == prefix)\n" |
302 | " names.push(name.substr(before));\n" | |
7e5391fd JF |
303 | " return names;\n" |
304 | " }\n" | |
2a18313c | 305 | ), expression, $S(begin.c_str()), $S(word)))); |
7e5391fd JF |
306 | |
307 | driver.program_->Replace(context); | |
308 | ||
309 | std::ostringstream str; | |
310 | CYOutput out(str, options); | |
311 | out << *driver.program_; | |
312 | ||
313 | std::string code(str.str()); | |
314 | CYUTF8String json(Run(pool, client_, code)); | |
1b2613d3 | 315 | // XXX: if this fails we should not try to parse it |
7e5391fd | 316 | |
2eb8215d | 317 | CYExpression *result(ParseExpression(json)); |
df24434f JF |
318 | if (result == NULL) |
319 | return NULL; | |
320 | ||
fd5cdf97 | 321 | CYArray *array(dynamic_cast<CYArray *>(result->Primitive(context))); |
a3f4a8e1 | 322 | if (array == NULL) { |
2e2ed904 | 323 | *out_ << '\n'; |
82a02ede | 324 | Output(false, json, out_); |
a3f4a8e1 JF |
325 | rl_forced_update_display(); |
326 | return NULL; | |
327 | } | |
7e5391fd JF |
328 | |
329 | // XXX: use an std::set? | |
330 | typedef std::vector<std::string> Completions; | |
331 | Completions completions; | |
332 | ||
333 | std::string common; | |
334 | bool rest(false); | |
335 | ||
c2c9f509 | 336 | CYForEach (element, array->elements_) { |
7e5391fd JF |
337 | CYString *string(dynamic_cast<CYString *>(element->value_)); |
338 | _assert(string != NULL); | |
339 | ||
763aa499 JF |
340 | std::string completion; |
341 | if (string->size_ != 0) | |
342 | completion.assign(string->value_, string->size_); | |
343 | else if (driver.mode_ == CYDriver::AutoMessage) | |
344 | completion = "]"; | |
345 | else | |
346 | continue; | |
347 | ||
7e5391fd JF |
348 | completions.push_back(completion); |
349 | ||
350 | if (!rest) { | |
351 | common = completion; | |
352 | rest = true; | |
353 | } else { | |
354 | size_t limit(completion.size()), size(common.size()); | |
355 | if (size > limit) | |
356 | common = common.substr(0, limit); | |
357 | else | |
358 | limit = size; | |
359 | for (limit = 0; limit != size; ++limit) | |
360 | if (common[limit] != completion[limit]) | |
361 | break; | |
362 | if (limit != size) | |
363 | common = common.substr(0, limit); | |
364 | } | |
365 | } | |
366 | ||
367 | size_t count(completions.size()); | |
368 | if (count == 0) | |
369 | return NULL; | |
370 | ||
c41590b2 JF |
371 | size_t colon(common.find(':')); |
372 | if (colon != std::string::npos) | |
373 | common = common.substr(0, colon + 1); | |
6dccefa9 JF |
374 | if (completions.size() == 1) |
375 | common += ' '; | |
c41590b2 | 376 | |
7e5391fd JF |
377 | char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count + 2)))); |
378 | ||
379 | results[0] = strdup(common.c_str()); | |
380 | size_t index(0); | |
381 | for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i) | |
382 | results[++index] = strdup(i->c_str()); | |
383 | results[count + 1] = NULL; | |
384 | ||
385 | return results; | |
386 | } | |
387 | ||
388 | // need char *, not const char * | |
389 | static char name_[] = "cycript"; | |
108c5fc8 | 390 | static char break_[] = " \t\n\"\\'`@$><=;|&{(" ")}" ".:[]"; |
7e5391fd | 391 | |
da136f88 JF |
392 | class History { |
393 | private: | |
394 | std::string histfile_; | |
395 | size_t histlines_; | |
396 | ||
397 | public: | |
398 | History(std::string histfile) : | |
399 | histfile_(histfile), | |
400 | histlines_(0) | |
401 | { | |
402 | read_history(histfile_.c_str()); | |
403 | } | |
404 | ||
405 | ~History() { | |
406 | if (append_history$ != NULL) { | |
f61fec22 JF |
407 | int fd(_syscall(open(histfile_.c_str(), O_CREAT | O_WRONLY, 0600))); |
408 | _syscall(close(fd)); | |
da136f88 JF |
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 | 421 | static void Console(CYOptions &options) { |
1210260f JF |
422 | std::string basedir; |
423 | if (const char *home = getenv("HOME")) | |
424 | basedir = home; | |
425 | else { | |
426 | passwd *passwd; | |
427 | if (const char *username = getenv("LOGNAME")) | |
428 | passwd = getpwnam(username); | |
429 | else | |
430 | passwd = getpwuid(getuid()); | |
431 | basedir = passwd->pw_dir; | |
432 | } | |
b1589845 | 433 | |
da136f88 JF |
434 | basedir += "/.cycript"; |
435 | mkdir(basedir.c_str(), 0700); | |
b1589845 | 436 | |
7e5391fd JF |
437 | rl_initialize(); |
438 | rl_readline_name = name_; | |
439 | ||
da136f88 | 440 | History history(basedir + "/history"); |
b1589845 | 441 | |
1f8eae40 JF |
442 | bool bypass(false); |
443 | bool debug(false); | |
6ec85c5b | 444 | bool expand(false); |
36bf49c4 | 445 | bool lower(true); |
cd98d280 | 446 | bool syntax(true); |
1f8eae40 | 447 | |
2e2ed904 | 448 | out_ = &std::cout; |
057f943f | 449 | |
7e5391fd JF |
450 | // rl_completer_word_break_characters is broken in libedit |
451 | rl_basic_word_break_characters = break_; | |
bc1e87aa JF |
452 | |
453 | rl_completer_word_break_characters = break_; | |
7e5391fd JF |
454 | rl_attempted_completion_function = &Complete; |
455 | rl_bind_key('\t', rl_complete); | |
057f943f JF |
456 | |
457 | struct sigaction action; | |
458 | sigemptyset(&action.sa_mask); | |
459 | action.sa_handler = &sigint; | |
460 | action.sa_flags = 0; | |
461 | sigaction(SIGINT, &action, NULL); | |
462 | ||
463 | restart: for (;;) { | |
7e5391fd | 464 | command_.clear(); |
057f943f JF |
465 | std::vector<std::string> lines; |
466 | ||
931b816a JF |
467 | bool extra(false); |
468 | const char *prompt("cy# "); | |
469 | ||
057f943f | 470 | if (setjmp(ctrlc_) != 0) { |
4e39dc0b | 471 | mode_ = Working; |
2e2ed904 | 472 | *out_ << std::endl; |
057f943f JF |
473 | goto restart; |
474 | } | |
475 | ||
057f943f | 476 | read: |
cd98d280 JF |
477 | |
478 | #if RL_READLINE_VERSION >= 0x0600 | |
8a81192b | 479 | if (syntax) |
cd98d280 | 480 | rl_redisplay_function = CYDisplayUpdate; |
8a81192b | 481 | else |
cd98d280 | 482 | rl_redisplay_function = rl_redisplay; |
cd98d280 JF |
483 | #endif |
484 | ||
4e39dc0b | 485 | mode_ = Parsing; |
057f943f | 486 | char *line(readline(prompt)); |
4e39dc0b | 487 | mode_ = Working; |
cd98d280 | 488 | |
79357996 JF |
489 | if (line == NULL) { |
490 | *out_ << std::endl; | |
057f943f | 491 | break; |
79357996 | 492 | } else if (line[0] == '\0') |
67f1fc6b | 493 | goto read; |
931b816a JF |
494 | |
495 | if (!extra) { | |
496 | extra = true; | |
6ec85c5b | 497 | if (line[0] == '?') { |
1f8eae40 JF |
498 | std::string data(line + 1); |
499 | if (data == "bypass") { | |
500 | bypass = !bypass; | |
2e2ed904 | 501 | *out_ << "bypass == " << (bypass ? "true" : "false") << std::endl; |
1f8eae40 JF |
502 | } else if (data == "debug") { |
503 | debug = !debug; | |
2e2ed904 | 504 | *out_ << "debug == " << (debug ? "true" : "false") << std::endl; |
8fab8594 JF |
505 | } else if (data == "destroy") { |
506 | CYDestroyContext(); | |
51b6165e | 507 | } else if (data == "gc") { |
e7ff0158 | 508 | *out_ << "collecting... " << std::flush; |
51b6165e | 509 | CYGarbageCollect(CYGetJSContext()); |
e7ff0158 | 510 | *out_ << "done." << std::endl; |
6a5fd0d9 JF |
511 | } else if (data == "exit") { |
512 | return; | |
6ec85c5b JF |
513 | } else if (data == "expand") { |
514 | expand = !expand; | |
2e2ed904 | 515 | *out_ << "expand == " << (expand ? "true" : "false") << std::endl; |
36bf49c4 JF |
516 | } else if (data == "lower") { |
517 | lower = !lower; | |
518 | *out_ << "lower == " << (lower ? "true" : "false") << std::endl; | |
82a02ede JF |
519 | } else if (data == "syntax") { |
520 | syntax = !syntax; | |
521 | *out_ << "syntax == " << (syntax ? "true" : "false") << std::endl; | |
1f8eae40 | 522 | } |
be8c2078 JF |
523 | command_ = line; |
524 | history += command_; | |
931b816a JF |
525 | goto restart; |
526 | } | |
527 | } | |
528 | ||
7e5391fd | 529 | command_ += line; |
e818f0e0 JF |
530 | |
531 | char *begin(line), *end(line + strlen(line)); | |
532 | while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) { | |
533 | *nl = '\0'; | |
534 | lines.push_back(begin); | |
535 | begin = nl + 1; | |
536 | } | |
537 | ||
538 | lines.push_back(begin); | |
539 | ||
057f943f JF |
540 | free(line); |
541 | ||
1f8eae40 JF |
542 | std::string code; |
543 | ||
544 | if (bypass) | |
7e5391fd | 545 | code = command_; |
1f8eae40 | 546 | else { |
2eb8215d | 547 | CYLocalPool pool; |
d3b63265 JF |
548 | |
549 | std::istringstream stream(command_); | |
550 | CYDriver driver(stream); | |
551 | ||
1f8eae40 | 552 | cy::parser parser(driver); |
b10bd496 | 553 | Setup(driver, parser); |
1f8eae40 | 554 | |
1f8eae40 | 555 | if (parser.parse() != 0 || !driver.errors_.empty()) { |
f0360d51 | 556 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { |
58afc6aa | 557 | CYPosition begin(error->location_.begin); |
c247172c | 558 | if (begin.line != lines.size() || begin.column < lines.back().size() || error->warning_) { |
58afc6aa | 559 | CYPosition end(error->location_.end); |
48e3be8a | 560 | |
f0360d51 JF |
561 | if (begin.line != lines.size()) { |
562 | std::cerr << " | "; | |
563 | std::cerr << lines[begin.line - 1] << std::endl; | |
564 | } | |
48e3be8a | 565 | |
c52a09b8 | 566 | std::cerr << "...."; |
97c8a8fc | 567 | for (size_t i(0); i != begin.column; ++i) |
f0360d51 | 568 | std::cerr << '.'; |
48e3be8a | 569 | if (begin.line != end.line || begin.column == end.column) |
f0360d51 JF |
570 | std::cerr << '^'; |
571 | else for (size_t i(0), e(end.column - begin.column); i != e; ++i) | |
572 | std::cerr << '^'; | |
573 | std::cerr << std::endl; | |
48e3be8a | 574 | |
f0360d51 JF |
575 | std::cerr << " | "; |
576 | std::cerr << error->message_ << std::endl; | |
48e3be8a | 577 | |
da136f88 | 578 | history += command_; |
1f8eae40 JF |
579 | goto restart; |
580 | } | |
581 | } | |
057f943f | 582 | |
1f8eae40 | 583 | driver.errors_.clear(); |
057f943f | 584 | |
7e5391fd | 585 | command_ += '\n'; |
1f8eae40 JF |
586 | prompt = "cy> "; |
587 | goto read; | |
057f943f JF |
588 | } |
589 | ||
b10bd496 | 590 | if (driver.program_ == NULL) |
1f8eae40 | 591 | goto restart; |
057f943f | 592 | |
0df6a201 JF |
593 | std::ostringstream str; |
594 | CYOutput out(str, options); | |
36bf49c4 | 595 | Setup(out, driver, options, lower); |
0df6a201 JF |
596 | out << *driver.program_; |
597 | code = str.str(); | |
057f943f JF |
598 | } |
599 | ||
da136f88 | 600 | history += command_; |
057f943f | 601 | |
82a02ede | 602 | if (debug) { |
f43fcb85 | 603 | std::cout << "cy= "; |
82a02ede JF |
604 | Write(syntax, code.c_str(), code.size(), std::cout); |
605 | std::cout << std::endl; | |
606 | } | |
057f943f | 607 | |
82a02ede | 608 | Run(client_, syntax, code, out_, expand); |
b09da87b | 609 | } |
b09da87b | 610 | } |
057f943f | 611 | |
f8d45a20 | 612 | void InjectLibrary(pid_t, int, const char *[]); |
06edab7d | 613 | |
10d0e915 | 614 | int Main(int argc, char * const argv[], char const * const envp[]) { |
48e3be8a | 615 | bool tty(isatty(STDIN_FILENO)); |
75b0a457 | 616 | bool compile(false); |
96746747 | 617 | bool target(false); |
de9fc71b | 618 | CYOptions options; |
967067aa | 619 | |
e4676127 | 620 | append_history$ = (int (*)(int, const char *)) (dlsym(RTLD_DEFAULT, "append_history")); |
da858962 | 621 | |
2aa77fd8 JF |
622 | #ifdef CY_ATTACH |
623 | pid_t pid(_not(pid_t)); | |
624 | #endif | |
625 | ||
666eedb9 JF |
626 | const char *host(NULL); |
627 | const char *port(NULL); | |
628 | ||
10d0e915 | 629 | optind = 1; |
06edab7d JF |
630 | |
631 | for (;;) { | |
10d0e915 JF |
632 | int option(getopt_long(argc, argv, |
633 | "c" | |
634 | "g:" | |
635 | "n:" | |
2aa77fd8 | 636 | #ifdef CY_ATTACH |
10d0e915 | 637 | "p:" |
2aa77fd8 | 638 | #endif |
10d0e915 JF |
639 | "r:" |
640 | "s" | |
f61fec22 | 641 | , (const struct option[]) { |
10d0e915 JF |
642 | {NULL, no_argument, NULL, 'c'}, |
643 | {NULL, required_argument, NULL, 'g'}, | |
644 | {NULL, required_argument, NULL, 'n'}, | |
645 | #ifdef CY_ATTACH | |
646 | {NULL, required_argument, NULL, 'p'}, | |
647 | #endif | |
648 | {NULL, required_argument, NULL, 'r'}, | |
649 | {NULL, no_argument, NULL, 's'}, | |
650 | {0, 0, 0, 0}}, NULL)); | |
06edab7d | 651 | |
10d0e915 JF |
652 | switch (option) { |
653 | case -1: | |
06edab7d | 654 | goto getopt; |
10d0e915 JF |
655 | |
656 | case ':': | |
657 | case '?': | |
06edab7d JF |
658 | fprintf(stderr, |
659 | "usage: cycript [-c]" | |
2aa77fd8 | 660 | #ifdef CY_ATTACH |
69caa5be | 661 | " [-p <pid|name>]" |
2aa77fd8 | 662 | #endif |
666eedb9 | 663 | " [-r <host:port>]" |
06edab7d JF |
664 | " [<script> [<arg>...]]\n" |
665 | ); | |
666 | return 1; | |
967067aa | 667 | |
96746747 JF |
668 | target: |
669 | if (!target) | |
670 | target = true; | |
671 | else { | |
672 | fprintf(stderr, "only one of -[c" | |
673 | #ifdef CY_ATTACH | |
674 | "p" | |
675 | #endif | |
676 | "r] may be used at a time\n"); | |
677 | return 1; | |
678 | } | |
679 | break; | |
680 | ||
06edab7d JF |
681 | case 'c': |
682 | compile = true; | |
96746747 | 683 | goto target; |
75b0a457 | 684 | |
06edab7d JF |
685 | case 'g': |
686 | if (false); | |
10d0e915 | 687 | else if (strcmp(optarg, "rename") == 0) |
de9fc71b | 688 | options.verbose_ = true; |
cac61857 | 689 | #if YYDEBUG |
10d0e915 | 690 | else if (strcmp(optarg, "bison") == 0) |
06edab7d | 691 | bison_ = true; |
cac61857 | 692 | #endif |
06edab7d JF |
693 | else { |
694 | fprintf(stderr, "invalid name for -g\n"); | |
695 | return 1; | |
696 | } | |
697 | break; | |
cac61857 | 698 | |
06edab7d JF |
699 | case 'n': |
700 | if (false); | |
10d0e915 | 701 | else if (strcmp(optarg, "minify") == 0) |
06edab7d JF |
702 | pretty_ = true; |
703 | else { | |
704 | fprintf(stderr, "invalid name for -n\n"); | |
705 | return 1; | |
706 | } | |
707 | break; | |
11c1cc16 | 708 | |
2aa77fd8 | 709 | #ifdef CY_ATTACH |
06edab7d | 710 | case 'p': { |
10d0e915 | 711 | size_t size(strlen(optarg)); |
06edab7d | 712 | char *end; |
69caa5be | 713 | |
10d0e915 JF |
714 | pid = strtoul(optarg, &end, 0); |
715 | if (optarg + size != end) { | |
69caa5be | 716 | // XXX: arg needs to be escaped in some horrendous way of doom |
10d0e915 JF |
717 | // XXX: this is a memory leak now because I just don't care enough |
718 | char *command; | |
719 | asprintf(&command, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", optarg); | |
69caa5be JF |
720 | |
721 | if (FILE *pids = popen(command, "r")) { | |
722 | char value[32]; | |
723 | size = 0; | |
724 | ||
725 | for (;;) { | |
726 | size_t read(fread(value + size, 1, sizeof(value) - size, pids)); | |
727 | if (read == 0) | |
728 | break; | |
729 | else { | |
730 | size += read; | |
04aa6240 | 731 | if (size == sizeof(value)) |
b166b11b | 732 | goto fail; |
69caa5be JF |
733 | } |
734 | } | |
735 | ||
736 | size: | |
737 | if (size == 0) | |
b166b11b | 738 | goto fail; |
69caa5be JF |
739 | if (value[size - 1] == '\n') { |
740 | --size; | |
741 | goto size; | |
742 | } | |
743 | ||
744 | value[size] = '\0'; | |
745 | size = strlen(value); | |
746 | pid = strtoul(value, &end, 0); | |
b166b11b | 747 | if (value + size != end) fail: |
69caa5be | 748 | pid = _not(pid_t); |
69caa5be JF |
749 | _syscall(pclose(pids)); |
750 | } | |
751 | ||
752 | if (pid == _not(pid_t)) { | |
10d0e915 | 753 | fprintf(stderr, "unable to find process `%s' using ps\n", optarg); |
69caa5be JF |
754 | return 1; |
755 | } | |
06edab7d | 756 | } |
96746747 | 757 | } goto target; |
2aa77fd8 | 758 | #endif |
b10bd496 | 759 | |
666eedb9 | 760 | case 'r': { |
10d0e915 | 761 | //size_t size(strlen(optarg)); |
666eedb9 | 762 | |
10d0e915 | 763 | char *colon(strrchr(optarg, ':')); |
666eedb9 JF |
764 | if (colon == NULL) { |
765 | fprintf(stderr, "missing colon in hostspec\n"); | |
766 | return 1; | |
767 | } | |
768 | ||
769 | /*char *end; | |
770 | port = strtoul(colon + 1, &end, 10); | |
10d0e915 | 771 | if (end != optarg + size) { |
666eedb9 JF |
772 | fprintf(stderr, "invalid port in hostspec\n"); |
773 | return 1; | |
774 | }*/ | |
775 | ||
10d0e915 | 776 | host = optarg; |
666eedb9 JF |
777 | *colon = '\0'; |
778 | port = colon + 1; | |
96746747 | 779 | } goto target; |
666eedb9 | 780 | |
06edab7d JF |
781 | case 's': |
782 | strict_ = true; | |
783 | break; | |
10d0e915 JF |
784 | |
785 | default: | |
786 | _assert(false); | |
06edab7d | 787 | } |
10d0e915 JF |
788 | } |
789 | ||
790 | getopt: | |
791 | argc -= optind; | |
792 | argv += optind; | |
967067aa | 793 | |
b09da87b JF |
794 | const char *script; |
795 | ||
2aa77fd8 | 796 | #ifdef CY_ATTACH |
10d0e915 | 797 | if (pid != _not(pid_t) && argc > 1) { |
fcc64bb5 JF |
798 | fprintf(stderr, "-p cannot set argv\n"); |
799 | return 1; | |
800 | } | |
2aa77fd8 | 801 | #endif |
75b0a457 | 802 | |
10d0e915 | 803 | if (argc == 0) |
b09da87b JF |
804 | script = NULL; |
805 | else { | |
9185d5ef | 806 | #ifdef CY_EXECUTE |
967067aa | 807 | // XXX: const_cast?! wtf gcc :( |
10d0e915 | 808 | CYSetArgs(argc - 1, const_cast<const char **>(argv + 1)); |
9185d5ef | 809 | #endif |
10d0e915 | 810 | script = argv[0]; |
967067aa JF |
811 | if (strcmp(script, "-") == 0) |
812 | script = NULL; | |
b09da87b JF |
813 | } |
814 | ||
2aa77fd8 | 815 | #ifdef CY_ATTACH |
967067aa | 816 | if (pid == _not(pid_t)) |
7e5391fd | 817 | client_ = -1; |
967067aa | 818 | else { |
46d13f4c JF |
819 | struct Socket { |
820 | int fd_; | |
b166b11b | 821 | |
46d13f4c JF |
822 | Socket(int fd) : |
823 | fd_(fd) | |
824 | { | |
825 | } | |
96cc7967 | 826 | |
46d13f4c JF |
827 | ~Socket() { |
828 | close(fd_); | |
829 | } | |
96cc7967 | 830 | |
46d13f4c JF |
831 | operator int() { |
832 | return fd_; | |
833 | } | |
834 | } server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0))); | |
96cc7967 | 835 | |
46d13f4c JF |
836 | struct sockaddr_un address; |
837 | memset(&address, 0, sizeof(address)); | |
838 | address.sun_family = AF_UNIX; | |
b166b11b | 839 | |
f8d45a20 JF |
840 | const char *tmp; |
841 | #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) | |
842 | tmp = "/Library/Caches"; | |
843 | #else | |
844 | tmp = "/tmp"; | |
845 | #endif | |
846 | ||
847 | sprintf(address.sun_path, "%s/.s.cy.%u", tmp, getpid()); | |
46d13f4c | 848 | unlink(address.sun_path); |
b166b11b | 849 | |
46d13f4c JF |
850 | struct File { |
851 | const char *path_; | |
852 | ||
853 | File(const char *path) : | |
854 | path_(path) | |
855 | { | |
856 | } | |
857 | ||
858 | ~File() { | |
859 | unlink(path_); | |
860 | } | |
861 | } file(address.sun_path); | |
862 | ||
863 | _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
864 | _syscall(chmod(address.sun_path, 0777)); | |
865 | ||
866 | _syscall(listen(server, 1)); | |
f8d45a20 | 867 | InjectLibrary(pid, 1, (const char *[]) {address.sun_path, NULL}); |
46d13f4c | 868 | client_ = _syscall(accept(server, NULL, NULL)); |
967067aa | 869 | } |
2aa77fd8 | 870 | #else |
7e5391fd | 871 | client_ = -1; |
2aa77fd8 | 872 | #endif |
579ed526 | 873 | |
666eedb9 JF |
874 | if (client_ == -1 && host != NULL && port != NULL) { |
875 | struct addrinfo hints; | |
876 | memset(&hints, 0, sizeof(hints)); | |
877 | hints.ai_family = AF_UNSPEC; | |
878 | hints.ai_socktype = SOCK_STREAM; | |
879 | hints.ai_protocol = 0; | |
880 | hints.ai_flags = 0; | |
881 | ||
882 | struct addrinfo *infos; | |
883 | _syscall(getaddrinfo(host, port, &hints, &infos)); | |
884 | ||
885 | _assert(infos != NULL); try { | |
886 | for (struct addrinfo *info(infos); info != NULL; info = info->ai_next) { | |
887 | int client(_syscall(socket(info->ai_family, info->ai_socktype, info->ai_protocol))); try { | |
888 | _syscall(connect(client, info->ai_addr, info->ai_addrlen)); | |
889 | client_ = client; | |
890 | break; | |
891 | } catch (...) { | |
892 | _syscall(close(client)); | |
893 | throw; | |
894 | } | |
895 | } | |
896 | } catch (...) { | |
897 | freeaddrinfo(infos); | |
898 | throw; | |
899 | } | |
900 | } | |
901 | ||
48e3be8a | 902 | if (script == NULL && tty) |
2eb8215d | 903 | Console(options); |
b09da87b | 904 | else { |
2eb8215d | 905 | CYLocalPool pool; |
b09da87b | 906 | |
0df6a201 | 907 | std::istream *stream; |
48e3be8a | 908 | if (script == NULL) { |
0df6a201 JF |
909 | stream = &std::cin; |
910 | script = "<stdin>"; | |
48e3be8a | 911 | } else { |
0df6a201 JF |
912 | stream = new std::fstream(script, std::ios::in | std::ios::binary); |
913 | _assert(!stream->fail()); | |
48e3be8a | 914 | } |
62ca2b82 | 915 | |
0df6a201 | 916 | CYDriver driver(*stream, script); |
d3b63265 JF |
917 | cy::parser parser(driver); |
918 | Setup(driver, parser); | |
919 | ||
b09da87b JF |
920 | if (parser.parse() != 0 || !driver.errors_.empty()) { |
921 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) | |
922 | std::cerr << i->location_.begin << ": " << i->message_ << std::endl; | |
0df6a201 JF |
923 | } else if (driver.program_ != NULL) { |
924 | std::ostringstream str; | |
925 | CYOutput out(str, options); | |
36bf49c4 | 926 | Setup(out, driver, options, true); |
0df6a201 JF |
927 | out << *driver.program_; |
928 | std::string code(str.str()); | |
929 | if (compile) | |
930 | std::cout << code; | |
931 | else | |
82a02ede | 932 | Run(client_, false, code, &std::cout); |
0df6a201 | 933 | } |
057f943f | 934 | } |
62ca2b82 | 935 | |
057f943f | 936 | return 0; |
62ca2b82 | 937 | } |
b6961e53 | 938 | |
10d0e915 JF |
939 | int main(int argc, char * const argv[], char const * const envp[]) { |
940 | try { | |
b6961e53 JF |
941 | return Main(argc, argv, envp); |
942 | } catch (const CYException &error) { | |
943 | CYPool pool; | |
944 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
945 | return 1; | |
946 | } | |
947 | } |