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