]>
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> |
51b2dc6b | 29 | #include <complex> |
0df6a201 | 30 | #include <fstream> |
057f943f JF |
31 | #include <sstream> |
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 | 45 | #include <errno.h> |
10d0e915 | 46 | #include <getopt.h> |
51b2dc6b | 47 | #include <setjmp.h> |
10d0e915 | 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 | |
51b2dc6b | 57 | #include <sys/ioctl.h> |
967067aa JF |
58 | #include <sys/types.h> |
59 | #include <sys/socket.h> | |
60 | #include <netinet/in.h> | |
61 | #include <sys/un.h> | |
62 | ||
da858962 | 63 | #include <dlfcn.h> |
51b2dc6b JF |
64 | #include <pwd.h> |
65 | #include <term.h> | |
da858962 | 66 | |
341d1456 JF |
67 | #ifdef __APPLE__ |
68 | #include <mach/mach_time.h> | |
69 | #endif | |
70 | ||
b12a9965 | 71 | #include "Driver.hpp" |
e66ced89 | 72 | #include "Error.hpp" |
d9c91152 | 73 | #include "Highlight.hpp" |
20052ff7 | 74 | #include "Syntax.hpp" |
7e5391fd | 75 | |
51b2dc6b JF |
76 | extern "C" int rl_display_fixed; |
77 | extern "C" int _rl_vis_botlin; | |
78 | extern "C" int _rl_last_c_pos; | |
79 | extern "C" int _rl_last_v_pos; | |
80 | ||
81 | typedef std::complex<int> CYCursor; | |
82 | ||
83 | static CYCursor current_; | |
84 | static int width_; | |
85 | static size_t point_; | |
86 | ||
87 | unsigned CYDisplayWidth() { | |
88 | struct winsize info; | |
89 | if (ioctl(1, TIOCGWINSZ, &info) != -1) | |
90 | return info.ws_col; | |
91 | return tgetnum(const_cast<char *>("co")); | |
92 | } | |
93 | ||
94 | void CYDisplayOutput_(bool display, const char *&data) { | |
95 | for (;; ++data) { | |
96 | char next(*data); | |
97 | if (next == '\0' || next == CYIgnoreEnd) | |
98 | return; | |
99 | if (display) | |
100 | putchar(next); | |
101 | } | |
102 | } | |
103 | ||
104 | CYCursor CYDisplayOutput(bool display, int width, const char *data, ssize_t offset = 0) { | |
105 | CYCursor point(current_); | |
106 | ||
107 | for (;;) { | |
108 | if (offset-- == 0) | |
109 | point = current_; | |
110 | switch (char next = *data++) { | |
111 | case '\0': | |
112 | return point; | |
113 | break; | |
114 | ||
115 | case CYIgnoreStart: | |
116 | CYDisplayOutput_(display, data); | |
117 | case CYIgnoreEnd: | |
118 | ++offset; | |
119 | break; | |
120 | ||
121 | default: | |
122 | if (display) | |
123 | putchar(next); | |
124 | current_ += CYCursor(0, 1); | |
125 | if (current_.imag() != width) | |
126 | break; | |
127 | current_ = CYCursor(current_.real() + 1, 0); | |
128 | if (display) | |
129 | putp(clr_eos); | |
130 | break; | |
131 | ||
132 | case '\n': | |
133 | current_ = CYCursor(current_.real() + 1, 4); | |
134 | if (display) { | |
135 | putp(clr_eol); | |
136 | putchar('\n'); | |
137 | putchar(' '); | |
138 | putchar(' '); | |
139 | putchar(' '); | |
140 | putchar(' '); | |
141 | } | |
142 | break; | |
143 | ||
144 | } | |
145 | } | |
146 | } | |
147 | ||
148 | void CYDisplayMove_(char *negative, char *positive, int offset) { | |
149 | if (offset < 0) | |
150 | putp(tparm(negative, -offset)); | |
151 | else if (offset > 0) | |
152 | putp(tparm(positive, offset)); | |
153 | } | |
154 | ||
155 | void CYDisplayMove(CYCursor target) { | |
156 | CYCursor offset(target - current_); | |
157 | ||
158 | CYDisplayMove_(parm_up_cursor, parm_down_cursor, offset.real()); | |
159 | ||
160 | if (char *parm = tparm(column_address, target.imag())) | |
161 | putp(parm); | |
162 | else | |
163 | CYDisplayMove_(parm_left_cursor, parm_right_cursor, offset.imag()); | |
164 | ||
165 | current_ = target; | |
166 | } | |
167 | ||
168 | void CYDisplayUpdate() { | |
169 | current_ = CYCursor(_rl_last_v_pos, _rl_last_c_pos); | |
170 | ||
171 | const char *prompt(rl_display_prompt); | |
172 | ||
173 | std::ostringstream stream; | |
174 | CYLexerHighlight(rl_line_buffer, rl_end, stream, true); | |
175 | std::string string(stream.str()); | |
176 | const char *buffer(string.c_str()); | |
177 | ||
178 | int width(CYDisplayWidth()); | |
179 | if (width_ != width) { | |
180 | current_ = CYCursor(); | |
181 | CYDisplayOutput(false, width, prompt); | |
182 | current_ = CYDisplayOutput(false, width, buffer, point_); | |
183 | } | |
184 | ||
185 | CYDisplayMove(CYCursor()); | |
186 | CYDisplayOutput(true, width, prompt); | |
187 | CYCursor target(CYDisplayOutput(true, width, stream.str().c_str(), rl_point)); | |
188 | ||
189 | _rl_vis_botlin = current_.real(); | |
190 | ||
191 | if (current_.imag() == 0) | |
192 | CYDisplayOutput(true, width, " "); | |
193 | putp(clr_eos); | |
194 | ||
195 | CYDisplayMove(target); | |
196 | fflush(stdout); | |
197 | ||
198 | _rl_last_v_pos = current_.real(); | |
199 | _rl_last_c_pos = current_.imag(); | |
200 | ||
201 | width_ = width; | |
202 | point_ = rl_point; | |
203 | } | |
204 | ||
4e39dc0b JF |
205 | static volatile enum { |
206 | Working, | |
207 | Parsing, | |
208 | Running, | |
209 | Sending, | |
210 | Waiting, | |
211 | } mode_; | |
212 | ||
057f943f JF |
213 | static jmp_buf ctrlc_; |
214 | ||
579ed526 | 215 | static void sigint(int) { |
4e39dc0b JF |
216 | switch (mode_) { |
217 | case Working: | |
218 | return; | |
219 | case Parsing: | |
220 | longjmp(ctrlc_, 1); | |
221 | case Running: | |
b0ba908c JF |
222 | CYCancel(); |
223 | return; | |
4e39dc0b JF |
224 | case Sending: |
225 | return; | |
226 | case Waiting: | |
227 | return; | |
228 | } | |
057f943f JF |
229 | } |
230 | ||
cac61857 | 231 | static bool bison_; |
341d1456 | 232 | static bool timing_; |
b10bd496 | 233 | static bool strict_; |
11c1cc16 | 234 | static bool pretty_; |
cac61857 | 235 | |
d9c91152 | 236 | void Setup(CYDriver &driver) { |
cac61857 | 237 | if (bison_) |
d9c91152 | 238 | driver.debug_ = 1; |
b10bd496 JF |
239 | if (strict_) |
240 | driver.strict_ = true; | |
cac61857 JF |
241 | } |
242 | ||
2c1d569a | 243 | void Setup(CYOutput &out, CYDriver &driver, CYOptions &options, bool lower) { |
11c1cc16 | 244 | out.pretty_ = pretty_; |
36bf49c4 | 245 | if (lower) |
2c1d569a | 246 | driver.Replace(options); |
11c1cc16 JF |
247 | } |
248 | ||
7e5391fd | 249 | static CYUTF8String Run(CYPool &pool, int client, CYUTF8String code) { |
967067aa | 250 | const char *json; |
9d79cefc | 251 | uint32_t size; |
7e5391fd | 252 | |
b166b11b | 253 | if (client == -1) { |
4e39dc0b | 254 | mode_ = Running; |
2aa77fd8 | 255 | #ifdef CY_EXECUTE |
89d16b11 | 256 | json = CYExecute(CYGetJSContext(), pool, code); |
2aa77fd8 JF |
257 | #else |
258 | json = NULL; | |
259 | #endif | |
4e39dc0b | 260 | mode_ = Working; |
dd221c6b JF |
261 | if (json == NULL) |
262 | size = 0; | |
263 | else | |
6ec85c5b JF |
264 | size = strlen(json); |
265 | } else { | |
4e39dc0b | 266 | mode_ = Sending; |
0ced2e47 | 267 | size = code.size; |
a1f3555e JF |
268 | _assert(CYSendAll(client, &size, sizeof(size))); |
269 | _assert(CYSendAll(client, code.data, code.size)); | |
4e39dc0b | 270 | mode_ = Waiting; |
a1f3555e | 271 | _assert(CYRecvAll(client, &size, sizeof(size))); |
9d79cefc | 272 | if (size == _not(uint32_t)) |
967067aa JF |
273 | json = NULL; |
274 | else { | |
275 | char *temp(new(pool) char[size + 1]); | |
a1f3555e | 276 | _assert(CYRecvAll(client, temp, size)); |
967067aa JF |
277 | temp[size] = '\0'; |
278 | json = temp; | |
279 | } | |
4e39dc0b | 280 | mode_ = Working; |
4cf49641 | 281 | } |
b09da87b | 282 | |
7e5391fd JF |
283 | return CYUTF8String(json, size); |
284 | } | |
285 | ||
286 | static CYUTF8String Run(CYPool &pool, int client, const std::string &code) { | |
287 | return Run(pool, client, CYUTF8String(code.c_str(), code.size())); | |
288 | } | |
289 | ||
2e2ed904 | 290 | static std::ostream *out_; |
a3f4a8e1 | 291 | |
e66ced89 | 292 | static void Output(CYUTF8String json, std::ostream *out, bool expand = false) { |
a3f4a8e1 JF |
293 | const char *data(json.data); |
294 | size_t size(json.size); | |
295 | ||
2e2ed904 | 296 | if (data == NULL || out == NULL) |
a3f4a8e1 JF |
297 | return; |
298 | ||
0881deb5 JF |
299 | if (!expand || |
300 | data[0] != '@' && data[0] != '"' && data[0] != '\'' || | |
301 | data[0] == '@' && data[1] != '"' && data[1] != '\'' | |
302 | ) | |
e66ced89 | 303 | CYLexerHighlight(data, size, *out); |
a3f4a8e1 JF |
304 | else for (size_t i(0); i != size; ++i) |
305 | if (data[i] != '\\') | |
2e2ed904 | 306 | *out << data[i]; |
a3f4a8e1 JF |
307 | else switch(data[++i]) { |
308 | case '\0': goto done; | |
2e2ed904 JF |
309 | case '\\': *out << '\\'; break; |
310 | case '\'': *out << '\''; break; | |
311 | case '"': *out << '"'; break; | |
312 | case 'b': *out << '\b'; break; | |
313 | case 'f': *out << '\f'; break; | |
314 | case 'n': *out << '\n'; break; | |
315 | case 'r': *out << '\r'; break; | |
316 | case 't': *out << '\t'; break; | |
317 | case 'v': *out << '\v'; break; | |
318 | default: *out << '\\'; --i; break; | |
a3f4a8e1 | 319 | } |
6ec85c5b | 320 | |
a3f4a8e1 | 321 | done: |
2e2ed904 | 322 | *out << std::endl; |
a3f4a8e1 JF |
323 | } |
324 | ||
da858962 JF |
325 | int (*append_history$)(int, const char *); |
326 | ||
7e5391fd JF |
327 | static std::string command_; |
328 | ||
d9c91152 | 329 | static int client_; |
b0385401 | 330 | |
d9c91152 JF |
331 | static CYUTF8String Run(CYPool &pool, const std::string &code) { |
332 | return Run(pool, client_, code); | |
7e5391fd JF |
333 | } |
334 | ||
7e5391fd | 335 | static char **Complete(const char *word, int start, int end) { |
10d0e915 | 336 | rl_attempted_completion_over = ~0; |
7e5391fd | 337 | std::string line(rl_line_buffer, start); |
51b2dc6b | 338 | char **values(CYComplete(word, command_ + line, &Run)); |
5569e998 | 339 | mode_ = Parsing; |
51b2dc6b | 340 | return values; |
7e5391fd JF |
341 | } |
342 | ||
343 | // need char *, not const char * | |
344 | static char name_[] = "cycript"; | |
87450281 | 345 | static char break_[] = " \t\n\"\\'`@><=;|&{(" ")}" ".:[]"; |
7e5391fd | 346 | |
da136f88 JF |
347 | class History { |
348 | private: | |
349 | std::string histfile_; | |
350 | size_t histlines_; | |
351 | ||
352 | public: | |
353 | History(std::string histfile) : | |
354 | histfile_(histfile), | |
355 | histlines_(0) | |
356 | { | |
357 | read_history(histfile_.c_str()); | |
51b2dc6b JF |
358 | |
359 | for (HIST_ENTRY *history((history_set_pos(0), current_history())); history; history = next_history()) | |
360 | for (char *character(history->line); *character; ++character) | |
361 | if (*character == '\x01') *character = '\n'; | |
da136f88 JF |
362 | } |
363 | ||
364 | ~History() { | |
51b2dc6b JF |
365 | for (HIST_ENTRY *history((history_set_pos(0), current_history())); history; history = next_history()) |
366 | for (char *character(history->line); *character; ++character) | |
367 | if (*character == '\n') *character = '\x01'; | |
368 | ||
da136f88 | 369 | if (append_history$ != NULL) { |
f61fec22 JF |
370 | int fd(_syscall(open(histfile_.c_str(), O_CREAT | O_WRONLY, 0600))); |
371 | _syscall(close(fd)); | |
da136f88 JF |
372 | _assert((*append_history$)(histlines_, histfile_.c_str()) == 0); |
373 | } else { | |
374 | _assert(write_history(histfile_.c_str()) == 0); | |
375 | } | |
376 | } | |
377 | ||
51b2dc6b | 378 | void operator +=(std::string command) { |
6265f0de | 379 | add_history(command.c_str()); |
da136f88 JF |
380 | ++histlines_; |
381 | } | |
382 | }; | |
383 | ||
61c71121 JF |
384 | template <typename Type_> |
385 | static Type_ *CYmemrchr(Type_ *data, Type_ value, size_t size) { | |
386 | while (size != 0) | |
387 | if (data[--size] == value) | |
388 | return data + size; | |
389 | return NULL; | |
390 | } | |
391 | ||
51b2dc6b | 392 | static int CYConsoleKeyReturn(int count, int key) { |
f66ec41a JF |
393 | if (rl_point != rl_end) { |
394 | if (memchr(rl_line_buffer, '\n', rl_end) == NULL) | |
66c347f0 | 395 | return rl_newline(count, key); |
61c71121 | 396 | |
14bb86e8 | 397 | insert: |
61c71121 JF |
398 | char *before(CYmemrchr(rl_line_buffer, '\n', rl_point)); |
399 | if (before == NULL) | |
400 | before = rl_line_buffer; | |
401 | ||
402 | int space(before + 1 - rl_line_buffer); | |
403 | while (space != rl_point && rl_line_buffer[space] == ' ') | |
404 | ++space; | |
405 | ||
406 | int adjust(rl_line_buffer + space - 1 - before); | |
407 | if (space == rl_point && adjust != 0) | |
408 | rl_rubout(adjust, '\b'); | |
409 | ||
f66ec41a | 410 | rl_insert(count, '\n'); |
61c71121 JF |
411 | if (adjust != 0) |
412 | rl_insert(adjust, ' '); | |
413 | ||
f66ec41a JF |
414 | return 0; |
415 | } | |
416 | ||
a664a58a | 417 | bool done(false); |
f66ec41a | 418 | if (rl_line_buffer[0] == '?') |
a664a58a | 419 | done = true; |
f66ec41a | 420 | else { |
51b2dc6b | 421 | std::string command(rl_line_buffer, rl_end); |
66c347f0 | 422 | command += '\n'; |
c3d9dbc7 | 423 | std::stringbuf stream(command); |
51b2dc6b JF |
424 | |
425 | size_t last(std::string::npos); | |
426 | for (size_t i(0); i != std::string::npos; i = command.find('\n', i + 1)) | |
427 | ++last; | |
428 | ||
429 | CYPool pool; | |
430 | CYDriver driver(pool, stream); | |
431 | if (driver.Parse() || !driver.errors_.empty()) | |
432 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { | |
433 | if (error->location_.begin.line != last + 1) | |
a664a58a | 434 | done = true; |
51b2dc6b JF |
435 | break; |
436 | } | |
437 | else | |
a664a58a | 438 | done = true; |
51b2dc6b JF |
439 | } |
440 | ||
a664a58a JF |
441 | if (done) |
442 | return rl_newline(count, key); | |
66c347f0 | 443 | |
14bb86e8 JF |
444 | // XXX: this was the most obvious fix, but is seriously dumb |
445 | goto insert; | |
51b2dc6b JF |
446 | } |
447 | ||
61c71121 JF |
448 | static int CYConsoleKeyUp(int count, int key) { |
449 | while (count-- != 0) { | |
450 | char *after(CYmemrchr(rl_line_buffer, '\n', rl_point)); | |
451 | if (after == NULL) { | |
452 | if (int value = rl_get_previous_history(1, key)) | |
453 | return value; | |
454 | continue; | |
455 | } | |
456 | ||
457 | char *before(CYmemrchr(rl_line_buffer, '\n', after - rl_line_buffer)); | |
458 | if (before == NULL) | |
459 | before = rl_line_buffer - 1; | |
460 | ||
461 | ptrdiff_t offset(rl_line_buffer + rl_point - after); | |
462 | if (offset > after - before) | |
463 | rl_point = after - rl_line_buffer; | |
464 | else | |
465 | rl_point = before + offset - rl_line_buffer; | |
466 | } | |
467 | ||
468 | return 0; | |
b1b144d1 JF |
469 | } |
470 | ||
61c71121 JF |
471 | static int CYConsoleKeyDown(int count, int key) { |
472 | while (count-- != 0) { | |
473 | char *after(static_cast<char *>(memchr(rl_line_buffer + rl_point, '\n', rl_end - rl_point))); | |
474 | if (after == NULL) { | |
475 | int where(where_history()); | |
476 | if (int value = rl_get_next_history(1, key)) | |
477 | return value; | |
478 | if (where != where_history()) { | |
479 | char *first(static_cast<char *>(memchr(rl_line_buffer, '\n', rl_end))); | |
480 | if (first != NULL) | |
481 | rl_point = first - 1 - rl_line_buffer; | |
482 | } | |
483 | continue; | |
484 | } | |
485 | ||
486 | char *before(CYmemrchr(rl_line_buffer, '\n', rl_point)); | |
487 | if (before == NULL) | |
488 | before = rl_line_buffer - 1; | |
489 | ||
490 | char *next(static_cast<char *>(memchr(after + 1, '\n', rl_line_buffer + rl_end - after - 1))); | |
491 | if (next == NULL) | |
492 | next = rl_line_buffer + rl_end; | |
493 | ||
494 | ptrdiff_t offset(rl_line_buffer + rl_point - before); | |
495 | if (offset > next - after) | |
496 | rl_point = next - rl_line_buffer; | |
497 | else | |
498 | rl_point = after + offset - rl_line_buffer; | |
b1b144d1 JF |
499 | } |
500 | ||
61c71121 JF |
501 | return 0; |
502 | } | |
b1b144d1 | 503 | |
61c71121 JF |
504 | static int CYConsoleLineBegin(int count, int key) { |
505 | while (rl_point != 0 && rl_line_buffer[rl_point - 1] != '\n') | |
506 | --rl_point; | |
507 | return 0; | |
508 | } | |
b1b144d1 | 509 | |
61c71121 JF |
510 | static int CYConsoleLineEnd(int count, int key) { |
511 | while (rl_point != rl_end && rl_line_buffer[rl_point] != '\n') | |
512 | ++rl_point; | |
2447e531 JF |
513 | if (rl_point != rl_end && rl_editing_mode == 0) |
514 | --rl_point; | |
b1b144d1 JF |
515 | return 0; |
516 | } | |
517 | ||
61c71121 JF |
518 | static int CYConsoleKeyBack(int count, int key) { |
519 | while (count-- != 0) { | |
520 | char *before(CYmemrchr(rl_line_buffer, '\n', rl_point)); | |
521 | if (before == NULL) | |
522 | return rl_rubout(count, key); | |
523 | ||
524 | int start(before + 1 - rl_line_buffer); | |
525 | if (start == rl_point) rubout: { | |
526 | rl_rubout(1, key); | |
527 | continue; | |
528 | } | |
529 | ||
530 | for (int i(start); i != rl_point; ++i) | |
531 | if (rl_line_buffer[i] != ' ') | |
532 | goto rubout; | |
533 | rl_rubout((rl_point - start) % 4 ?: 4, key); | |
b1b144d1 JF |
534 | } |
535 | ||
61c71121 JF |
536 | return 0; |
537 | } | |
538 | ||
539 | static int CYConsoleKeyTab(int count, int key) { | |
b1b144d1 | 540 | char *before(CYmemrchr(rl_line_buffer, '\n', rl_point)); |
61c71121 JF |
541 | if (before == NULL) complete: |
542 | return rl_complete_internal(rl_completion_mode(&CYConsoleKeyTab)); | |
543 | int start(before + 1 - rl_line_buffer); | |
544 | for (int i(start); i != rl_point; ++i) | |
545 | if (rl_line_buffer[i] != ' ') | |
546 | goto complete; | |
547 | return rl_insert(4 - (rl_point - start) % 4, ' '); | |
548 | } | |
b1b144d1 | 549 | |
2447e531 JF |
550 | static void CYConsoleRemapBind(Keymap map, rl_command_func_t *from, rl_command_func_t *to) { |
551 | char **keyseqs(rl_invoking_keyseqs_in_map(from, map)); | |
61c71121 JF |
552 | if (keyseqs == NULL) |
553 | return; | |
554 | for (char **keyseq(keyseqs); *keyseq != NULL; ++keyseq) { | |
2447e531 | 555 | rl_bind_keyseq_in_map(*keyseq, to, map); |
61c71121 JF |
556 | free(*keyseq); |
557 | } | |
558 | free(keyseqs); | |
559 | } | |
b1b144d1 | 560 | |
2447e531 JF |
561 | static void CYConsoleRemapKeys(Keymap map) { |
562 | CYConsoleRemapBind(map, &rl_beg_of_line, &CYConsoleLineBegin); | |
563 | CYConsoleRemapBind(map, &rl_end_of_line, &CYConsoleLineEnd); | |
564 | ||
565 | CYConsoleRemapBind(map, &rl_get_previous_history, &CYConsoleKeyUp); | |
566 | CYConsoleRemapBind(map, &rl_get_next_history, &CYConsoleKeyDown); | |
567 | ||
568 | CYConsoleRemapBind(map, &rl_rubout, &CYConsoleKeyBack); | |
569 | CYConsoleRemapBind(map, &rl_complete, &CYConsoleKeyTab); | |
570 | } | |
571 | ||
61c71121 JF |
572 | static void CYConsolePrepTerm(int meta) { |
573 | rl_prep_terminal(meta); | |
574 | ||
2447e531 JF |
575 | CYConsoleRemapKeys(emacs_standard_keymap); |
576 | CYConsoleRemapKeys(emacs_meta_keymap); | |
577 | CYConsoleRemapKeys(emacs_ctlx_keymap); | |
578 | CYConsoleRemapKeys(vi_insertion_keymap); | |
579 | CYConsoleRemapKeys(vi_movement_keymap); | |
b1b144d1 JF |
580 | } |
581 | ||
2eb8215d | 582 | static void Console(CYOptions &options) { |
1210260f JF |
583 | std::string basedir; |
584 | if (const char *home = getenv("HOME")) | |
585 | basedir = home; | |
586 | else { | |
587 | passwd *passwd; | |
588 | if (const char *username = getenv("LOGNAME")) | |
589 | passwd = getpwnam(username); | |
590 | else | |
591 | passwd = getpwuid(getuid()); | |
592 | basedir = passwd->pw_dir; | |
593 | } | |
b1589845 | 594 | |
da136f88 JF |
595 | basedir += "/.cycript"; |
596 | mkdir(basedir.c_str(), 0700); | |
b1589845 | 597 | |
7e5391fd JF |
598 | rl_initialize(); |
599 | rl_readline_name = name_; | |
600 | ||
da136f88 | 601 | History history(basedir + "/history"); |
b1589845 | 602 | |
1f8eae40 JF |
603 | bool bypass(false); |
604 | bool debug(false); | |
6ec85c5b | 605 | bool expand(false); |
36bf49c4 | 606 | bool lower(true); |
1f8eae40 | 607 | |
2e2ed904 | 608 | out_ = &std::cout; |
057f943f | 609 | |
bc1e87aa | 610 | rl_completer_word_break_characters = break_; |
7e5391fd | 611 | rl_attempted_completion_function = &Complete; |
61c71121 | 612 | |
e66ced89 | 613 | rl_redisplay_function = CYDisplayUpdate; |
61c71121 | 614 | rl_prep_term_function = CYConsolePrepTerm; |
e66ced89 | 615 | |
057f943f JF |
616 | struct sigaction action; |
617 | sigemptyset(&action.sa_mask); | |
618 | action.sa_handler = &sigint; | |
619 | action.sa_flags = 0; | |
620 | sigaction(SIGINT, &action, NULL); | |
621 | ||
51b2dc6b | 622 | for (;;) { |
057f943f | 623 | if (setjmp(ctrlc_) != 0) { |
4e39dc0b | 624 | mode_ = Working; |
2e2ed904 | 625 | *out_ << std::endl; |
51b2dc6b | 626 | continue; |
057f943f JF |
627 | } |
628 | ||
f66ec41a | 629 | if (bypass) { |
66c347f0 JF |
630 | rl_bind_key('\r', &rl_newline); |
631 | rl_bind_key('\n', &rl_newline); | |
f66ec41a | 632 | } else { |
51b2dc6b | 633 | rl_bind_key('\r', &CYConsoleKeyReturn); |
f66ec41a JF |
634 | rl_bind_key('\n', &CYConsoleKeyReturn); |
635 | } | |
51b2dc6b | 636 | |
4e39dc0b | 637 | mode_ = Parsing; |
51b2dc6b | 638 | char *line(readline("cy# ")); |
4e39dc0b | 639 | mode_ = Working; |
cd98d280 | 640 | |
79357996 JF |
641 | if (line == NULL) { |
642 | *out_ << std::endl; | |
057f943f | 643 | break; |
931b816a JF |
644 | } |
645 | ||
51b2dc6b JF |
646 | std::string command(line); |
647 | free(line); | |
51b2dc6b JF |
648 | if (command.empty()) |
649 | continue; | |
311fe5f3 | 650 | history += command; |
51b2dc6b JF |
651 | |
652 | if (command[0] == '?') { | |
653 | std::string data(command.substr(1)); | |
654 | if (data == "bypass") { | |
655 | bypass = !bypass; | |
656 | *out_ << "bypass == " << (bypass ? "true" : "false") << std::endl; | |
657 | } else if (data == "debug") { | |
658 | debug = !debug; | |
659 | *out_ << "debug == " << (debug ? "true" : "false") << std::endl; | |
660 | } else if (data == "destroy") { | |
661 | CYDestroyContext(); | |
662 | } else if (data == "gc") { | |
663 | *out_ << "collecting... " << std::flush; | |
664 | CYGarbageCollect(CYGetJSContext()); | |
665 | *out_ << "done." << std::endl; | |
666 | } else if (data == "exit") { | |
667 | return; | |
668 | } else if (data == "expand") { | |
669 | expand = !expand; | |
670 | *out_ << "expand == " << (expand ? "true" : "false") << std::endl; | |
671 | } else if (data == "lower") { | |
672 | lower = !lower; | |
673 | *out_ << "lower == " << (lower ? "true" : "false") << std::endl; | |
674 | } | |
e818f0e0 | 675 | |
51b2dc6b | 676 | continue; |
e818f0e0 JF |
677 | } |
678 | ||
1f8eae40 | 679 | std::string code; |
1f8eae40 | 680 | if (bypass) |
51b2dc6b | 681 | code = command; |
311fe5f3 | 682 | else try { |
c3d9dbc7 | 683 | std::stringbuf stream(command); |
d3b63265 | 684 | |
d9c91152 | 685 | CYPool pool; |
2c1d569a JF |
686 | CYDriver driver(pool, stream); |
687 | Setup(driver); | |
688 | ||
51b2dc6b | 689 | if (driver.Parse() || !driver.errors_.empty()) { |
f0360d51 | 690 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { |
58afc6aa | 691 | CYPosition begin(error->location_.begin); |
51b2dc6b | 692 | CYPosition end(error->location_.end); |
48e3be8a | 693 | |
51b2dc6b | 694 | /*if (begin.line != lines2.size()) { |
f0360d51 | 695 | std::cerr << " | "; |
51b2dc6b JF |
696 | std::cerr << lines2[begin.line - 1] << std::endl; |
697 | }*/ | |
698 | ||
699 | std::cerr << "...."; | |
700 | for (size_t i(0); i != begin.column; ++i) | |
701 | std::cerr << '.'; | |
702 | if (begin.line != end.line || begin.column == end.column) | |
703 | std::cerr << '^'; | |
704 | else for (size_t i(0), e(end.column - begin.column); i != e; ++i) | |
705 | std::cerr << '^'; | |
706 | std::cerr << std::endl; | |
707 | ||
708 | std::cerr << " | "; | |
709 | std::cerr << error->message_ << std::endl; | |
710 | ||
51b2dc6b | 711 | break; |
1f8eae40 | 712 | } |
057f943f | 713 | |
51b2dc6b | 714 | continue; |
057f943f JF |
715 | } |
716 | ||
a7d8b413 | 717 | if (driver.script_ == NULL) |
51b2dc6b | 718 | continue; |
057f943f | 719 | |
efd689d8 | 720 | std::stringbuf str; |
0df6a201 | 721 | CYOutput out(str, options); |
2c1d569a | 722 | Setup(out, driver, options, lower); |
a7d8b413 | 723 | out << *driver.script_; |
0df6a201 | 724 | code = str.str(); |
311fe5f3 JF |
725 | } catch (const CYException &error) { |
726 | CYPool pool; | |
727 | std::cout << error.PoolCString(pool) << std::endl; | |
728 | continue; | |
057f943f JF |
729 | } |
730 | ||
82a02ede | 731 | if (debug) { |
f43fcb85 | 732 | std::cout << "cy= "; |
e66ced89 | 733 | CYLexerHighlight(code.c_str(), code.size(), std::cout); |
82a02ede JF |
734 | std::cout << std::endl; |
735 | } | |
057f943f | 736 | |
e66ced89 JF |
737 | CYPool pool; |
738 | Output(Run(pool, client_, code), &std::cout, expand); | |
b09da87b | 739 | } |
b09da87b | 740 | } |
057f943f | 741 | |
ccb4e34c | 742 | void InjectLibrary(pid_t, int, const char *const []); |
06edab7d | 743 | |
341d1456 JF |
744 | static uint64_t CYGetTime() { |
745 | #ifdef __APPLE__ | |
746 | return mach_absolute_time(); | |
747 | #else | |
748 | struct timespec spec; | |
749 | clock_gettime(CLOCK_MONOTONIC, &spec); | |
750 | return spec.tv_sec * UINT64_C(1000000000) + spec.tv_nsec; | |
751 | #endif | |
752 | } | |
753 | ||
10d0e915 | 754 | int Main(int argc, char * const argv[], char const * const envp[]) { |
48e3be8a | 755 | bool tty(isatty(STDIN_FILENO)); |
75b0a457 | 756 | bool compile(false); |
96746747 | 757 | bool target(false); |
de9fc71b | 758 | CYOptions options; |
967067aa | 759 | |
e4676127 | 760 | append_history$ = (int (*)(int, const char *)) (dlsym(RTLD_DEFAULT, "append_history")); |
da858962 | 761 | |
2aa77fd8 JF |
762 | #ifdef CY_ATTACH |
763 | pid_t pid(_not(pid_t)); | |
764 | #endif | |
765 | ||
666eedb9 JF |
766 | const char *host(NULL); |
767 | const char *port(NULL); | |
768 | ||
10d0e915 | 769 | optind = 1; |
06edab7d JF |
770 | |
771 | for (;;) { | |
10d0e915 JF |
772 | int option(getopt_long(argc, argv, |
773 | "c" | |
774 | "g:" | |
775 | "n:" | |
2aa77fd8 | 776 | #ifdef CY_ATTACH |
10d0e915 | 777 | "p:" |
2aa77fd8 | 778 | #endif |
10d0e915 JF |
779 | "r:" |
780 | "s" | |
f61fec22 | 781 | , (const struct option[]) { |
10d0e915 JF |
782 | {NULL, no_argument, NULL, 'c'}, |
783 | {NULL, required_argument, NULL, 'g'}, | |
784 | {NULL, required_argument, NULL, 'n'}, | |
785 | #ifdef CY_ATTACH | |
786 | {NULL, required_argument, NULL, 'p'}, | |
787 | #endif | |
788 | {NULL, required_argument, NULL, 'r'}, | |
789 | {NULL, no_argument, NULL, 's'}, | |
790 | {0, 0, 0, 0}}, NULL)); | |
06edab7d | 791 | |
10d0e915 JF |
792 | switch (option) { |
793 | case -1: | |
06edab7d | 794 | goto getopt; |
10d0e915 JF |
795 | |
796 | case ':': | |
797 | case '?': | |
06edab7d JF |
798 | fprintf(stderr, |
799 | "usage: cycript [-c]" | |
2aa77fd8 | 800 | #ifdef CY_ATTACH |
69caa5be | 801 | " [-p <pid|name>]" |
2aa77fd8 | 802 | #endif |
666eedb9 | 803 | " [-r <host:port>]" |
06edab7d JF |
804 | " [<script> [<arg>...]]\n" |
805 | ); | |
806 | return 1; | |
967067aa | 807 | |
96746747 JF |
808 | target: |
809 | if (!target) | |
810 | target = true; | |
811 | else { | |
812 | fprintf(stderr, "only one of -[c" | |
813 | #ifdef CY_ATTACH | |
814 | "p" | |
815 | #endif | |
816 | "r] may be used at a time\n"); | |
817 | return 1; | |
818 | } | |
819 | break; | |
820 | ||
06edab7d JF |
821 | case 'c': |
822 | compile = true; | |
96746747 | 823 | goto target; |
75b0a457 | 824 | |
06edab7d JF |
825 | case 'g': |
826 | if (false); | |
10d0e915 | 827 | else if (strcmp(optarg, "rename") == 0) |
de9fc71b | 828 | options.verbose_ = true; |
10d0e915 | 829 | else if (strcmp(optarg, "bison") == 0) |
06edab7d | 830 | bison_ = true; |
341d1456 JF |
831 | else if (strcmp(optarg, "timing") == 0) |
832 | timing_ = true; | |
06edab7d JF |
833 | else { |
834 | fprintf(stderr, "invalid name for -g\n"); | |
835 | return 1; | |
836 | } | |
837 | break; | |
cac61857 | 838 | |
06edab7d JF |
839 | case 'n': |
840 | if (false); | |
10d0e915 | 841 | else if (strcmp(optarg, "minify") == 0) |
06edab7d JF |
842 | pretty_ = true; |
843 | else { | |
844 | fprintf(stderr, "invalid name for -n\n"); | |
845 | return 1; | |
846 | } | |
847 | break; | |
11c1cc16 | 848 | |
2aa77fd8 | 849 | #ifdef CY_ATTACH |
06edab7d | 850 | case 'p': { |
10d0e915 | 851 | size_t size(strlen(optarg)); |
06edab7d | 852 | char *end; |
69caa5be | 853 | |
10d0e915 JF |
854 | pid = strtoul(optarg, &end, 0); |
855 | if (optarg + size != end) { | |
69caa5be | 856 | // XXX: arg needs to be escaped in some horrendous way of doom |
10d0e915 JF |
857 | // XXX: this is a memory leak now because I just don't care enough |
858 | char *command; | |
ccb4e34c JF |
859 | int writ(asprintf(&command, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", optarg)); |
860 | _assert(writ != -1); | |
69caa5be JF |
861 | |
862 | if (FILE *pids = popen(command, "r")) { | |
863 | char value[32]; | |
864 | size = 0; | |
865 | ||
866 | for (;;) { | |
867 | size_t read(fread(value + size, 1, sizeof(value) - size, pids)); | |
868 | if (read == 0) | |
869 | break; | |
870 | else { | |
871 | size += read; | |
04aa6240 | 872 | if (size == sizeof(value)) |
b166b11b | 873 | goto fail; |
69caa5be JF |
874 | } |
875 | } | |
876 | ||
877 | size: | |
878 | if (size == 0) | |
b166b11b | 879 | goto fail; |
69caa5be JF |
880 | if (value[size - 1] == '\n') { |
881 | --size; | |
882 | goto size; | |
883 | } | |
884 | ||
885 | value[size] = '\0'; | |
886 | size = strlen(value); | |
887 | pid = strtoul(value, &end, 0); | |
b166b11b | 888 | if (value + size != end) fail: |
69caa5be | 889 | pid = _not(pid_t); |
69caa5be JF |
890 | _syscall(pclose(pids)); |
891 | } | |
892 | ||
893 | if (pid == _not(pid_t)) { | |
10d0e915 | 894 | fprintf(stderr, "unable to find process `%s' using ps\n", optarg); |
69caa5be JF |
895 | return 1; |
896 | } | |
06edab7d | 897 | } |
96746747 | 898 | } goto target; |
2aa77fd8 | 899 | #endif |
b10bd496 | 900 | |
666eedb9 | 901 | case 'r': { |
10d0e915 | 902 | //size_t size(strlen(optarg)); |
666eedb9 | 903 | |
10d0e915 | 904 | char *colon(strrchr(optarg, ':')); |
666eedb9 JF |
905 | if (colon == NULL) { |
906 | fprintf(stderr, "missing colon in hostspec\n"); | |
907 | return 1; | |
908 | } | |
909 | ||
910 | /*char *end; | |
911 | port = strtoul(colon + 1, &end, 10); | |
10d0e915 | 912 | if (end != optarg + size) { |
666eedb9 JF |
913 | fprintf(stderr, "invalid port in hostspec\n"); |
914 | return 1; | |
915 | }*/ | |
916 | ||
10d0e915 | 917 | host = optarg; |
666eedb9 JF |
918 | *colon = '\0'; |
919 | port = colon + 1; | |
96746747 | 920 | } goto target; |
666eedb9 | 921 | |
06edab7d JF |
922 | case 's': |
923 | strict_ = true; | |
924 | break; | |
10d0e915 JF |
925 | |
926 | default: | |
927 | _assert(false); | |
06edab7d | 928 | } |
10d0e915 JF |
929 | } |
930 | ||
931 | getopt: | |
932 | argc -= optind; | |
933 | argv += optind; | |
967067aa | 934 | |
b09da87b JF |
935 | const char *script; |
936 | ||
2aa77fd8 | 937 | #ifdef CY_ATTACH |
10d0e915 | 938 | if (pid != _not(pid_t) && argc > 1) { |
fcc64bb5 JF |
939 | fprintf(stderr, "-p cannot set argv\n"); |
940 | return 1; | |
941 | } | |
2aa77fd8 | 942 | #endif |
75b0a457 | 943 | |
10d0e915 | 944 | if (argc == 0) |
b09da87b JF |
945 | script = NULL; |
946 | else { | |
9185d5ef | 947 | #ifdef CY_EXECUTE |
967067aa | 948 | // XXX: const_cast?! wtf gcc :( |
10d0e915 | 949 | CYSetArgs(argc - 1, const_cast<const char **>(argv + 1)); |
9185d5ef | 950 | #endif |
10d0e915 | 951 | script = argv[0]; |
967067aa JF |
952 | if (strcmp(script, "-") == 0) |
953 | script = NULL; | |
b09da87b JF |
954 | } |
955 | ||
2aa77fd8 | 956 | #ifdef CY_ATTACH |
967067aa | 957 | if (pid == _not(pid_t)) |
7e5391fd | 958 | client_ = -1; |
967067aa | 959 | else { |
46d13f4c JF |
960 | struct Socket { |
961 | int fd_; | |
b166b11b | 962 | |
46d13f4c JF |
963 | Socket(int fd) : |
964 | fd_(fd) | |
965 | { | |
966 | } | |
96cc7967 | 967 | |
46d13f4c JF |
968 | ~Socket() { |
969 | close(fd_); | |
970 | } | |
96cc7967 | 971 | |
46d13f4c JF |
972 | operator int() { |
973 | return fd_; | |
974 | } | |
975 | } server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0))); | |
96cc7967 | 976 | |
46d13f4c JF |
977 | struct sockaddr_un address; |
978 | memset(&address, 0, sizeof(address)); | |
979 | address.sun_family = AF_UNIX; | |
b166b11b | 980 | |
f8d45a20 JF |
981 | const char *tmp; |
982 | #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__)) | |
983 | tmp = "/Library/Caches"; | |
984 | #else | |
985 | tmp = "/tmp"; | |
986 | #endif | |
987 | ||
988 | sprintf(address.sun_path, "%s/.s.cy.%u", tmp, getpid()); | |
46d13f4c | 989 | unlink(address.sun_path); |
b166b11b | 990 | |
46d13f4c JF |
991 | struct File { |
992 | const char *path_; | |
993 | ||
994 | File(const char *path) : | |
995 | path_(path) | |
996 | { | |
997 | } | |
998 | ||
999 | ~File() { | |
1000 | unlink(path_); | |
1001 | } | |
1002 | } file(address.sun_path); | |
1003 | ||
1004 | _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
1005 | _syscall(chmod(address.sun_path, 0777)); | |
1006 | ||
1007 | _syscall(listen(server, 1)); | |
ccb4e34c JF |
1008 | const char *const argv[] = {address.sun_path, NULL}; |
1009 | InjectLibrary(pid, 1, argv); | |
46d13f4c | 1010 | client_ = _syscall(accept(server, NULL, NULL)); |
967067aa | 1011 | } |
2aa77fd8 | 1012 | #else |
7e5391fd | 1013 | client_ = -1; |
2aa77fd8 | 1014 | #endif |
579ed526 | 1015 | |
666eedb9 JF |
1016 | if (client_ == -1 && host != NULL && port != NULL) { |
1017 | struct addrinfo hints; | |
1018 | memset(&hints, 0, sizeof(hints)); | |
1019 | hints.ai_family = AF_UNSPEC; | |
1020 | hints.ai_socktype = SOCK_STREAM; | |
1021 | hints.ai_protocol = 0; | |
1022 | hints.ai_flags = 0; | |
1023 | ||
1024 | struct addrinfo *infos; | |
1025 | _syscall(getaddrinfo(host, port, &hints, &infos)); | |
1026 | ||
1027 | _assert(infos != NULL); try { | |
1028 | for (struct addrinfo *info(infos); info != NULL; info = info->ai_next) { | |
1029 | int client(_syscall(socket(info->ai_family, info->ai_socktype, info->ai_protocol))); try { | |
1030 | _syscall(connect(client, info->ai_addr, info->ai_addrlen)); | |
1031 | client_ = client; | |
1032 | break; | |
1033 | } catch (...) { | |
1034 | _syscall(close(client)); | |
1035 | throw; | |
1036 | } | |
1037 | } | |
1038 | } catch (...) { | |
1039 | freeaddrinfo(infos); | |
1040 | throw; | |
1041 | } | |
1042 | } | |
1043 | ||
48e3be8a | 1044 | if (script == NULL && tty) |
2eb8215d | 1045 | Console(options); |
b09da87b | 1046 | else { |
0df6a201 | 1047 | std::istream *stream; |
48e3be8a | 1048 | if (script == NULL) { |
0df6a201 JF |
1049 | stream = &std::cin; |
1050 | script = "<stdin>"; | |
48e3be8a | 1051 | } else { |
0df6a201 JF |
1052 | stream = new std::fstream(script, std::ios::in | std::ios::binary); |
1053 | _assert(!stream->fail()); | |
48e3be8a | 1054 | } |
62ca2b82 | 1055 | |
341d1456 JF |
1056 | if (timing_) { |
1057 | std::stringbuf buffer; | |
1058 | stream->get(buffer, '\0'); | |
1059 | _assert(!stream->fail()); | |
1060 | ||
1061 | double average(0); | |
1062 | int samples(-50); | |
1063 | uint64_t start(CYGetTime()); | |
1064 | ||
1065 | for (;;) { | |
1066 | stream = new std::istringstream(buffer.str()); | |
1067 | ||
1068 | CYPool pool; | |
c3d9dbc7 | 1069 | CYDriver driver(pool, *stream->rdbuf(), script); |
341d1456 JF |
1070 | Setup(driver); |
1071 | ||
1072 | uint64_t begin(CYGetTime()); | |
1073 | driver.Parse(); | |
1074 | uint64_t end(CYGetTime()); | |
1075 | ||
1076 | delete stream; | |
1077 | ||
1078 | average += (end - begin - average) / ++samples; | |
1079 | ||
1080 | uint64_t now(CYGetTime()); | |
1081 | if (samples == 0) | |
1082 | average = 0; | |
1083 | else if ((now - start) / 1000000000 >= 1) | |
1084 | std::cout << std::fixed << average << '\t' << (end - begin) << '\t' << samples << std::endl; | |
1085 | else continue; | |
1086 | ||
1087 | start = now; | |
1088 | } | |
1089 | ||
1090 | stream = new std::istringstream(buffer.str()); | |
1091 | std::cin.get(); | |
1092 | } | |
1093 | ||
2c1d569a | 1094 | CYPool pool; |
c3d9dbc7 | 1095 | CYDriver driver(pool, *stream->rdbuf(), script); |
d9c91152 JF |
1096 | Setup(driver); |
1097 | ||
2c1d569a | 1098 | bool failed(driver.Parse()); |
d3b63265 | 1099 | |
d9c91152 | 1100 | if (failed || !driver.errors_.empty()) { |
b09da87b JF |
1101 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) |
1102 | std::cerr << i->location_.begin << ": " << i->message_ << std::endl; | |
311fe5f3 | 1103 | return 1; |
a7d8b413 | 1104 | } else if (driver.script_ != NULL) { |
efd689d8 | 1105 | std::stringbuf str; |
0df6a201 | 1106 | CYOutput out(str, options); |
2c1d569a | 1107 | Setup(out, driver, options, true); |
a7d8b413 | 1108 | out << *driver.script_; |
0df6a201 JF |
1109 | std::string code(str.str()); |
1110 | if (compile) | |
1111 | std::cout << code; | |
e66ced89 JF |
1112 | else { |
1113 | CYUTF8String json(Run(pool, client_, code)); | |
1114 | if (CYStartsWith(json, "throw ")) { | |
1115 | CYLexerHighlight(json.data, json.size, std::cerr); | |
1116 | std::cerr << std::endl; | |
1117 | return 1; | |
1118 | } | |
1119 | } | |
0df6a201 | 1120 | } |
057f943f | 1121 | } |
62ca2b82 | 1122 | |
057f943f | 1123 | return 0; |
62ca2b82 | 1124 | } |
b6961e53 | 1125 | |
10d0e915 JF |
1126 | int main(int argc, char * const argv[], char const * const envp[]) { |
1127 | try { | |
b6961e53 JF |
1128 | return Main(argc, argv, envp); |
1129 | } catch (const CYException &error) { | |
1130 | CYPool pool; | |
1131 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
1132 | return 1; | |
1133 | } | |
1134 | } |