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