1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
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/>.
22 #include "cycript.hpp"
25 #include "JavaScript.hpp"
33 #ifdef HAVE_READLINE_H
36 #include <readline/readline.h>
42 #include <readline/history.h>
51 #include <sys/socket.h>
52 #include <sys/types.h>
57 #include <sys/ioctl.h>
58 #include <sys/types.h>
59 #include <sys/socket.h>
60 #include <netinet/in.h>
68 #include <mach/mach_time.h>
74 #include "Highlight.hpp"
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
;
82 typedef std::complex<int> CYCursor
;
84 static CYCursor current_
;
88 unsigned CYDisplayWidth() {
90 if (ioctl(1, TIOCGWINSZ
, &info
) != -1)
92 return tgetnum(const_cast<char *>("co"));
95 void CYDisplayOutput_(bool display
, const char *&data
) {
98 if (next
== '\0' || next
== CYIgnoreEnd
)
105 CYCursor
CYDisplayOutput(bool display
, int width
, const char *data
, ssize_t offset
= 0) {
106 CYCursor
point(current_
);
111 switch (char next
= *data
++) {
117 CYDisplayOutput_(display
, data
);
125 current_
+= CYCursor(0, 1);
126 if (current_
.imag() != width
)
128 current_
= CYCursor(current_
.real() + 1, 0);
134 current_
= CYCursor(current_
.real() + 1, 4);
149 void CYDisplayMove_(char *negative
, char *positive
, int offset
) {
151 putp(tparm(negative
, -offset
));
153 putp(tparm(positive
, offset
));
156 void CYDisplayMove(CYCursor target
) {
157 CYCursor
offset(target
- current_
);
159 CYDisplayMove_(parm_up_cursor
, parm_down_cursor
, offset
.real());
161 if (char *parm
= tparm(column_address
, target
.imag()))
164 CYDisplayMove_(parm_left_cursor
, parm_right_cursor
, offset
.imag());
169 void CYDisplayUpdate() {
170 current_
= CYCursor(_rl_last_v_pos
, _rl_last_c_pos
);
172 const char *prompt(rl_display_prompt
);
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());
179 int width(CYDisplayWidth());
180 if (width_
!= width
) {
181 current_
= CYCursor();
182 CYDisplayOutput(false, width
, prompt
);
183 current_
= CYDisplayOutput(false, width
, buffer
, point_
);
186 CYDisplayMove(CYCursor());
187 CYDisplayOutput(true, width
, prompt
);
188 CYCursor
target(CYDisplayOutput(true, width
, stream
.str().c_str(), rl_point
));
190 _rl_vis_botlin
= current_
.real();
192 if (current_
.imag() == 0)
193 CYDisplayOutput(true, width
, " ");
196 CYDisplayMove(target
);
199 _rl_last_v_pos
= current_
.real();
200 _rl_last_c_pos
= current_
.imag();
206 static volatile enum {
214 static jmp_buf ctrlc_
;
216 static void sigint(int) {
239 void Setup(CYDriver
&driver
) {
243 driver
.strict_
= true;
246 void Setup(CYOutput
&out
, CYDriver
&driver
, CYOptions
&options
, bool lower
) {
247 out
.pretty_
= pretty_
;
249 driver
.Replace(options
);
252 static CYUTF8String
Run(CYPool
&pool
, int client
, CYUTF8String code
) {
259 json
= CYExecute(CYGetJSContext(), pool
, code
);
271 _assert(CYSendAll(client
, &size
, sizeof(size
)));
272 _assert(CYSendAll(client
, code
.data
, code
.size
));
274 _assert(CYRecvAll(client
, &size
, sizeof(size
)));
275 if (size
== _not(uint32_t))
278 char *temp(new(pool
) char[size
+ 1]);
279 _assert(CYRecvAll(client
, temp
, size
));
286 return CYUTF8String(json
, size
);
289 static CYUTF8String
Run(CYPool
&pool
, int client
, const std::string
&code
) {
290 return Run(pool
, client
, CYUTF8String(code
.c_str(), code
.size()));
293 static std::ostream
*out_
;
295 static void Output(CYUTF8String json
, std::ostream
*out
, bool expand
= false, bool reparse
= false) {
299 CYStream
stream(json
.data
, json
.data
+ json
.size
);
300 CYDriver
driver(pool
, stream
);
301 if (driver
.Parse(CYMarkExpression
))
305 CYOutput
out(str
, options
);
307 out
<< *driver
.context_
;
308 std::string
data(str
.str());
309 json
= CYPoolUTF8String(pool
, data
);
314 const char *data(json
.data
);
315 size_t size(json
.size
);
317 if (data
== NULL
|| out
== NULL
)
321 data
[0] != '@' && data
[0] != '"' && data
[0] != '\'' ||
322 data
[0] == '@' && data
[1] != '"' && data
[1] != '\''
324 CYLexerHighlight(data
, size
, *out
);
325 else for (size_t i(0); i
!= size
; ++i
)
328 else switch(data
[++i
]) {
329 case '\0': goto done
;
330 case '\\': *out
<< '\\'; break;
331 case '\'': *out
<< '\''; break;
332 case '"': *out
<< '"'; break;
333 case 'b': *out
<< '\b'; break;
334 case 'f': *out
<< '\f'; break;
335 case 'n': *out
<< '\n'; break;
336 case 'r': *out
<< '\r'; break;
337 case 't': *out
<< '\t'; break;
338 case 'v': *out
<< '\v'; break;
339 default: *out
<< '\\'; --i
; break;
346 int (*append_history$
)(int, const char *);
348 static std::string command_
;
352 static CYUTF8String
Run(CYPool
&pool
, const std::string
&code
) {
353 return Run(pool
, client_
, code
);
356 static char **Complete(const char *word
, int start
, int end
) {
357 rl_attempted_completion_over
= ~0;
358 std::string
line(rl_line_buffer
, start
);
359 char **values(CYComplete(word
, command_
+ line
, &Run
));
364 // need char *, not const char *
365 static char name_
[] = "cycript";
366 static char break_
[] = " \t\n\"\\'`@><=;|&{(" ")}" ".:[]";
370 std::string histfile_
;
374 History(std::string histfile
) :
378 read_history(histfile_
.c_str());
380 for (HIST_ENTRY
*history((history_set_pos(0), current_history())); history
; history
= next_history())
381 for (char *character(history
->line
); *character
; ++character
)
382 if (*character
== '\x01') *character
= '\n';
386 for (HIST_ENTRY
*history((history_set_pos(0), current_history())); history
; history
= next_history())
387 for (char *character(history
->line
); *character
; ++character
)
388 if (*character
== '\n') *character
= '\x01';
390 if (append_history$
!= NULL
) {
391 int fd(_syscall(open(histfile_
.c_str(), O_CREAT
| O_WRONLY
, 0600)));
393 _assert((*append_history$
)(histlines_
, histfile_
.c_str()) == 0);
395 _assert(write_history(histfile_
.c_str()) == 0);
397 } catch (const CYException
&error
) {
399 std::cout
<< error
.PoolCString(pool
) << std::endl
;
402 void operator +=(std::string command
) {
403 if (HIST_ENTRY
*entry
= history_get(where_history()))
404 if (command
== entry
->line
)
406 add_history(command
.c_str());
411 template <typename Type_
>
412 static Type_
*CYmemrchr(Type_
*data
, Type_ value
, size_t size
) {
414 if (data
[--size
] == value
)
419 static void _lblcall(int (*command
)(int, int), int count
, int key
) {
420 int last(_rl_last_c_pos
);
421 // rl_rubout crashes in _rl_erase_at_end_of_line if _rl_last_c_pos != 0
422 if (command
== &rl_rubout
)
424 for (int i(0); i
!= count
; ++i
)
425 if (command(1, key
) != 0)
427 _rl_last_c_pos
= last
;
430 static int CYConsoleKeyReturn(int count
, int key
) {
431 if (rl_point
!= rl_end
) {
432 if (memchr(rl_line_buffer
, '\n', rl_end
) == NULL
) {
433 _lblcall(&rl_newline
, count
, key
);
438 char *before(CYmemrchr(rl_line_buffer
, '\n', rl_point
));
440 before
= rl_line_buffer
;
442 int space(before
+ 1 - rl_line_buffer
);
443 while (space
!= rl_point
&& rl_line_buffer
[space
] == ' ')
446 int adjust(rl_line_buffer
+ space
- 1 - before
);
447 if (space
== rl_point
&& adjust
!= 0)
448 _lblcall(&rl_rubout
, adjust
, '\b');
450 _lblcall(&rl_insert
, count
, '\n');
452 _lblcall(&rl_insert
, adjust
, ' ');
458 if (rl_line_buffer
[0] == '?')
461 std::string
command(rl_line_buffer
, rl_end
);
463 std::stringbuf
stream(command
);
465 size_t last(std::string::npos
);
466 for (size_t i(0); i
!= std::string::npos
; i
= command
.find('\n', i
+ 1))
470 CYDriver
driver(pool
, stream
);
471 if (driver
.Parse() || !driver
.errors_
.empty())
472 for (CYDriver::Errors::const_iterator
error(driver
.errors_
.begin()); error
!= driver
.errors_
.end(); ++error
) {
473 if (error
->location_
.begin
.line
!= last
+ 1)
482 _lblcall(&rl_newline
, count
, key
);
486 // XXX: this was the most obvious fix, but is seriously dumb
490 static int CYConsoleKeyUp(int count
, int key
) {
491 for (; count
!= 0; --count
) {
492 char *after(CYmemrchr(rl_line_buffer
, '\n', rl_point
));
494 if (int value
= rl_get_previous_history(1, key
))
499 char *before(CYmemrchr(rl_line_buffer
, '\n', after
- rl_line_buffer
));
501 before
= rl_line_buffer
- 1;
503 ptrdiff_t offset(rl_line_buffer
+ rl_point
- after
);
504 if (offset
> after
- before
)
505 rl_point
= after
- rl_line_buffer
;
507 rl_point
= before
+ offset
- rl_line_buffer
;
513 static int CYConsoleKeyDown(int count
, int key
) {
514 for (; count
!= 0; --count
) {
515 char *after(static_cast<char *>(memchr(rl_line_buffer
+ rl_point
, '\n', rl_end
- rl_point
)));
517 int where(where_history());
518 if (int value
= rl_get_next_history(1, key
))
520 if (where
!= where_history()) {
521 char *first(static_cast<char *>(memchr(rl_line_buffer
, '\n', rl_end
)));
523 rl_point
= first
- 1 - rl_line_buffer
;
528 char *before(CYmemrchr(rl_line_buffer
, '\n', rl_point
));
530 before
= rl_line_buffer
- 1;
532 char *next(static_cast<char *>(memchr(after
+ 1, '\n', rl_line_buffer
+ rl_end
- after
- 1)));
534 next
= rl_line_buffer
+ rl_end
;
536 ptrdiff_t offset(rl_line_buffer
+ rl_point
- before
);
537 if (offset
> next
- after
)
538 rl_point
= next
- rl_line_buffer
;
540 rl_point
= after
+ offset
- rl_line_buffer
;
546 static int CYConsoleLineBegin(int count
, int key
) {
547 while (rl_point
!= 0 && rl_line_buffer
[rl_point
- 1] != '\n')
552 static int CYConsoleLineEnd(int count
, int key
) {
553 while (rl_point
!= rl_end
&& rl_line_buffer
[rl_point
] != '\n')
555 if (rl_point
!= rl_end
&& rl_editing_mode
== 0)
560 static int CYConsoleKeyBack(int count
, int key
) {
561 for (; count
!= 0; --count
) {
565 char *before(CYmemrchr(rl_line_buffer
, '\n', rl_point
));
566 if (before
== NULL
) {
567 int adjust(std::min(count
, rl_point
));
568 _lblcall(&rl_rubout
, adjust
, key
);
573 int start(before
+ 1 - rl_line_buffer
);
574 if (start
== rl_point
) rubout
: {
575 _lblcall(&rl_rubout
, 1, key
);
579 for (int i(start
); i
!= rl_point
; ++i
)
580 if (rl_line_buffer
[i
] != ' ')
582 _lblcall(&rl_rubout
, (rl_point
- start
) % 4 ?: 4, key
);
588 static int CYConsoleKeyTab(int count
, int key
) {
589 char *before(CYmemrchr(rl_line_buffer
, '\n', rl_point
));
590 if (before
== NULL
) complete
:
591 return rl_complete_internal(rl_completion_mode(&CYConsoleKeyTab
));
592 int start(before
+ 1 - rl_line_buffer
);
593 for (int i(start
); i
!= rl_point
; ++i
)
594 if (rl_line_buffer
[i
] != ' ')
596 _lblcall(&rl_insert
, 4 - (rl_point
- start
) % 4, ' ');
600 static void CYConsoleRemapBind(Keymap map
, rl_command_func_t
*from
, rl_command_func_t
*to
) {
601 char **keyseqs(rl_invoking_keyseqs_in_map(from
, map
));
604 for (char **keyseq(keyseqs
); *keyseq
!= NULL
; ++keyseq
) {
605 rl_bind_keyseq_in_map(*keyseq
, to
, map
);
611 static void CYConsoleRemapKeys(Keymap map
) {
612 CYConsoleRemapBind(map
, &rl_beg_of_line
, &CYConsoleLineBegin
);
613 CYConsoleRemapBind(map
, &rl_end_of_line
, &CYConsoleLineEnd
);
615 CYConsoleRemapBind(map
, &rl_get_previous_history
, &CYConsoleKeyUp
);
616 CYConsoleRemapBind(map
, &rl_get_next_history
, &CYConsoleKeyDown
);
618 CYConsoleRemapBind(map
, &rl_rubout
, &CYConsoleKeyBack
);
619 CYConsoleRemapBind(map
, &rl_complete
, &CYConsoleKeyTab
);
622 static void CYConsolePrepTerm(int meta
) {
623 rl_prep_terminal(meta
);
625 CYConsoleRemapKeys(emacs_standard_keymap
);
626 CYConsoleRemapKeys(emacs_meta_keymap
);
627 CYConsoleRemapKeys(emacs_ctlx_keymap
);
628 CYConsoleRemapKeys(vi_insertion_keymap
);
629 CYConsoleRemapKeys(vi_movement_keymap
);
632 static void CYOutputRun(const std::string
&code
, bool expand
= false, bool reparse
= false) {
634 Output(Run(pool
, client_
, code
), &std::cout
, expand
, reparse
);
637 static void Console(CYOptions
&options
) {
640 basedir
= "/data/local/tmp";
642 if (const char *home
= getenv("HOME"))
646 if (const char *username
= getenv("LOGNAME"))
647 passwd
= getpwnam(username
);
649 passwd
= getpwuid(getuid());
650 basedir
= passwd
->pw_dir
;
654 basedir
+= "/.cycript";
655 mkdir(basedir
.c_str(), 0700);
658 rl_readline_name
= name_
;
660 History
history(basedir
+ "/history");
670 rl_completer_word_break_characters
= break_
;
671 rl_attempted_completion_function
= &Complete
;
673 if (cur_term
!= NULL
) {
674 rl_redisplay_function
= CYDisplayUpdate
;
675 rl_prep_term_function
= CYConsolePrepTerm
;
681 struct sigaction action
;
682 sigemptyset(&action
.sa_mask
);
683 action
.sa_handler
= &sigint
;
685 sigaction(SIGINT
, &action
, NULL
);
687 if (setjmp(ctrlc_
) != 0) {
694 rl_bind_key('\r', &rl_newline
);
695 rl_bind_key('\n', &rl_newline
);
697 rl_bind_key('\r', &CYConsoleKeyReturn
);
698 rl_bind_key('\n', &CYConsoleKeyReturn
);
702 char *line(readline("cy# "));
710 std::string
command(line
);
716 if (command
[0] == '?') {
717 std::string
data(command
.substr(1));
718 if (data
== "bypass") {
720 *out_
<< "bypass == " << (bypass
? "true" : "false") << std::endl
;
721 } else if (data
== "debug") {
723 *out_
<< "debug == " << (debug
? "true" : "false") << std::endl
;
724 } else if (data
== "destroy") {
726 } else if (data
== "gc") {
727 *out_
<< "collecting... " << std::flush
;
728 CYGarbageCollect(CYGetJSContext());
729 *out_
<< "done." << std::endl
;
730 } else if (data
== "exit") {
732 } else if (data
== "expand") {
734 *out_
<< "expand == " << (expand
? "true" : "false") << std::endl
;
735 } else if (data
== "lower") {
737 *out_
<< "lower == " << (lower
? "true" : "false") << std::endl
;
738 } else if (data
== "reparse") {
740 *out_
<< "reparse == " << (reparse
? "true" : "false") << std::endl
;
750 std::stringbuf
stream(command
);
753 CYDriver
driver(pool
, stream
);
756 if (driver
.Parse() || !driver
.errors_
.empty()) {
757 for (CYDriver::Errors::const_iterator
error(driver
.errors_
.begin()); error
!= driver
.errors_
.end(); ++error
) {
758 CYPosition
begin(error
->location_
.begin
);
759 CYPosition
end(error
->location_
.end
);
761 /*if (begin.line != lines2.size()) {
763 std::cerr << lines2[begin.line - 1] << std::endl;
767 for (size_t i(0); i
!= begin
.column
; ++i
)
769 if (begin
.line
!= end
.line
|| begin
.column
== end
.column
)
771 else for (size_t i(0), e(end
.column
- begin
.column
); i
!= e
; ++i
)
773 std::cerr
<< std::endl
;
776 std::cerr
<< error
->message_
<< std::endl
;
784 if (driver
.script_
== NULL
)
788 CYOutput
out(str
, options
);
789 Setup(out
, driver
, options
, lower
);
790 out
<< *driver
.script_
;
792 } catch (const CYException
&error
) {
794 std::cout
<< error
.PoolCString(pool
) << std::endl
;
800 CYLexerHighlight(code
.c_str(), code
.size(), std::cout
);
801 std::cout
<< std::endl
;
804 CYOutputRun(code
, expand
, reparse
);
808 void InjectLibrary(pid_t
, int, const char *const []);
810 static uint64_t CYGetTime() {
812 return mach_absolute_time();
814 struct timespec spec
;
815 clock_gettime(CLOCK_MONOTONIC
, &spec
);
816 return spec
.tv_sec
* UINT64_C(1000000000) + spec
.tv_nsec
;
820 int Main(int argc
, char * const argv
[], char const * const envp
[]) {
821 bool tty(isatty(STDIN_FILENO
));
826 append_history$
= (int (*)(int, const char *)) (dlsym(RTLD_DEFAULT
, "append_history"));
829 pid_t
pid(_not(pid_t
));
832 const char *host(NULL
);
833 const char *port(NULL
);
835 const char *argv0(argv
[0]);
840 int option(getopt_long(argc
, argv
,
849 , (const struct option
[]) {
850 {NULL
, no_argument
, NULL
, 'c'},
851 {NULL
, required_argument
, NULL
, 'g'},
852 {NULL
, required_argument
, NULL
, 'n'},
854 {NULL
, required_argument
, NULL
, 'p'},
856 {NULL
, required_argument
, NULL
, 'r'},
857 {NULL
, no_argument
, NULL
, 's'},
858 {0, 0, 0, 0}}, NULL
));
867 "usage: cycript [-c]"
872 " [<script> [<arg>...]]\n"
880 fprintf(stderr
, "only one of -[c"
884 "r] may be used at a time\n");
895 else if (strcmp(optarg
, "rename") == 0)
896 options
.verbose_
= true;
897 else if (strcmp(optarg
, "bison") == 0)
899 else if (strcmp(optarg
, "timing") == 0)
902 fprintf(stderr
, "invalid name for -g\n");
909 else if (strcmp(optarg
, "minify") == 0)
912 fprintf(stderr
, "invalid name for -n\n");
919 size_t size(strlen(optarg
));
922 pid
= strtoul(optarg
, &end
, 0);
923 if (optarg
+ size
!= end
) {
924 // XXX: arg needs to be escaped in some horrendous way of doom
925 // XXX: this is a memory leak now because I just don't care enough
927 int writ(asprintf(&command
, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", optarg
));
930 if (FILE *pids
= popen(command
, "r")) {
935 size_t read(fread(value
+ size
, 1, sizeof(value
) - size
, pids
));
940 if (size
== sizeof(value
))
948 if (value
[size
- 1] == '\n') {
954 size
= strlen(value
);
955 pid
= strtoul(value
, &end
, 0);
956 if (value
+ size
!= end
) fail
:
958 _syscall(pclose(pids
));
961 if (pid
== _not(pid_t
)) {
962 fprintf(stderr
, "unable to find process `%s' using ps\n", optarg
);
970 //size_t size(strlen(optarg));
972 char *colon(strrchr(optarg
, ':'));
974 fprintf(stderr
, "missing colon in hostspec\n");
979 port = strtoul(colon + 1, &end, 10);
980 if (end != optarg + size) {
981 fprintf(stderr, "invalid port in hostspec\n");
1006 if (pid
!= _not(pid_t
) && argc
> 1) {
1007 fprintf(stderr
, "-p cannot set argv\n");
1016 if (strcmp(script
, "-") == 0)
1023 // XXX: const_cast?! wtf gcc :(
1024 CYSetArgs(argv0
, script
, argc
, const_cast<const char **>(argv
));
1028 if (pid
== _not(pid_t
))
1046 } server(_syscall(socket(PF_UNIX
, SOCK_STREAM
, 0)));
1048 struct sockaddr_un address
;
1049 memset(&address
, 0, sizeof(address
));
1050 address
.sun_family
= AF_UNIX
;
1053 #if defined(__APPLE__) && (defined(__arm__) || defined(__arm64__))
1054 tmp
= "/Library/Caches";
1059 sprintf(address
.sun_path
, "%s/.s.cy.%u", tmp
, getpid());
1060 unlink(address
.sun_path
);
1065 File(const char *path
) :
1073 } file(address
.sun_path
);
1075 _syscall(bind(server
, reinterpret_cast<sockaddr
*>(&address
), sizeof(address
)));
1076 _syscall(chmod(address
.sun_path
, 0777));
1078 _syscall(listen(server
, 1));
1079 const char *const argv
[] = {address
.sun_path
, NULL
};
1080 InjectLibrary(pid
, 1, argv
);
1081 client_
= _syscall(accept(server
, NULL
, NULL
));
1087 if (client_
== -1 && host
!= NULL
&& port
!= NULL
) {
1088 struct addrinfo hints
;
1089 memset(&hints
, 0, sizeof(hints
));
1090 hints
.ai_family
= AF_UNSPEC
;
1091 hints
.ai_socktype
= SOCK_STREAM
;
1092 hints
.ai_protocol
= 0;
1095 struct addrinfo
*infos
;
1096 _syscall(getaddrinfo(host
, port
, &hints
, &infos
));
1098 _assert(infos
!= NULL
); try {
1099 for (struct addrinfo
*info(infos
); info
!= NULL
; info
= info
->ai_next
) {
1100 int client(_syscall(socket(info
->ai_family
, info
->ai_socktype
, info
->ai_protocol
))); try {
1101 _syscall(connect(client
, info
->ai_addr
, info
->ai_addrlen
));
1105 _syscall(close(client
));
1110 freeaddrinfo(infos
);
1115 if (script
== NULL
&& tty
)
1118 std::istream
*stream
;
1119 if (script
== NULL
) {
1123 stream
= new std::fstream(script
, std::ios::in
| std::ios::binary
);
1124 _assert(!stream
->fail());
1128 std::stringbuf buffer
;
1129 stream
->get(buffer
, '\0');
1130 _assert(!stream
->fail());
1134 uint64_t start(CYGetTime());
1137 stream
= new std::istringstream(buffer
.str());
1140 CYDriver
driver(pool
, *stream
->rdbuf(), script
);
1143 uint64_t begin(CYGetTime());
1145 uint64_t end(CYGetTime());
1149 average
+= (end
- begin
- average
) / ++samples
;
1151 uint64_t now(CYGetTime());
1154 else if ((now
- start
) / 1000000000 >= 1)
1155 std::cout
<< std::fixed
<< average
<< '\t' << (end
- begin
) << '\t' << samples
<< std::endl
;
1161 stream
= new std::istringstream(buffer
.str());
1166 CYDriver
driver(pool
, *stream
->rdbuf(), script
);
1169 bool failed(driver
.Parse());
1171 if (failed
|| !driver
.errors_
.empty()) {
1172 for (CYDriver::Errors::const_iterator
i(driver
.errors_
.begin()); i
!= driver
.errors_
.end(); ++i
)
1173 std::cerr
<< i
->location_
.begin
<< ": " << i
->message_
<< std::endl
;
1175 } else if (driver
.script_
!= NULL
) {
1177 CYOutput
out(str
, options
);
1178 Setup(out
, driver
, options
, true);
1179 out
<< *driver
.script_
;
1180 std::string
code(str
.str());
1184 CYUTF8String
json(Run(pool
, client_
, code
));
1185 if (CYStartsWith(json
, "throw ")) {
1186 CYLexerHighlight(json
.data
, json
.size
, std::cerr
);
1187 std::cerr
<< std::endl
;
1197 _visible
int main(int argc
, char * const argv
[], char const * const envp
[]) {
1199 return Main(argc
, argv
, envp
);
1200 } catch (const CYException
&error
) {
1202 fprintf(stderr
, "%s\n", error
.PoolCString(pool
));