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