]>
git.saurik.com Git - cycript.git/blob - Console.cpp
1 /* Cycript - Inlining/Optimizing JavaScript Compiler
2 * Copyright (C) 2009 Jay Freeman (saurik)
5 /* Modified BSD License {{{ */
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #include "cycript.hpp"
43 #include "JavaScript.hpp"
51 #ifdef HAVE_READLINE_H
54 #include <readline/readline.h>
60 #include <readline/history.h>
68 #include <sys/types.h>
72 #include "Cycript.tab.hh"
74 #include <sys/types.h>
75 #include <sys/socket.h>
76 #include <netinet/in.h>
80 #include <apr_getopt.h>
84 static volatile enum {
92 static jmp_buf ctrlc_
;
94 static void sigint(int) {
115 void Setup(CYDriver
&driver
, cy::parser
&parser
) {
118 parser
.set_debug_level(1);
121 driver
.strict_
= true;
124 void Setup(CYOutput
&out
, CYDriver
&driver
, CYOptions
&options
) {
125 out
.pretty_
= pretty_
;
126 CYContext
context(driver
.pool_
, options
);
127 driver
.program_
->Replace(context
);
130 void Run(int client
, const char *data
, size_t size
, FILE *fout
= NULL
, bool expand
= false) {
137 json
= CYExecute(pool
, data
);
146 CYSendAll(client
, &size
, sizeof(size
));
147 CYSendAll(client
, data
, size
);
149 CYRecvAll(client
, &size
, sizeof(size
));
150 if (size
== _not(size_t))
153 char *temp(new(pool
) char[size
+ 1]);
154 CYRecvAll(client
, temp
, size
);
161 if (json
!= NULL
&& fout
!= NULL
) {
162 if (!expand
|| json
[0] != '"' && json
[0] != '\'')
164 else for (size_t i(0); i
!= size
; ++i
)
166 fputc(json
[i
], fout
);
167 else switch(json
[++i
]) {
168 case '\0': goto done
;
169 case '\\': fputc('\\', fout
); break;
170 case '\'': fputc('\'', fout
); break;
171 case '"': fputc('"', fout
); break;
172 case 'b': fputc('\b', fout
); break;
173 case 'f': fputc('\f', fout
); break;
174 case 'n': fputc('\n', fout
); break;
175 case 'r': fputc('\r', fout
); break;
176 case 't': fputc('\t', fout
); break;
177 case 'v': fputc('\v', fout
); break;
178 default: fputc('\\', fout
); --i
; break;
187 void Run(int client
, std::string
&code
, FILE *fout
= NULL
, bool expand
= false) {
188 Run(client
, code
.c_str(), code
.size(), fout
, expand
);
191 int (*append_history$
)(int, const char *);
193 static void Console(apr_pool_t
*pool
, int client
, CYOptions
&options
) {
195 if (const char *username
= getenv("LOGNAME"))
196 passwd
= getpwnam(username
);
198 passwd
= getpwuid(getuid());
200 const char *basedir(apr_psprintf(pool
, "%s/.cycript", passwd
->pw_dir
));
201 const char *histfile(apr_psprintf(pool
, "%s/history", basedir
));
204 mkdir(basedir
, 0700);
205 read_history(histfile
);
213 rl_bind_key('\t', rl_insert
);
215 struct sigaction action
;
216 sigemptyset(&action
.sa_mask
);
217 action
.sa_handler
= &sigint
;
219 sigaction(SIGINT
, &action
, NULL
);
223 std::vector
<std::string
> lines
;
226 const char *prompt("cy# ");
228 if (setjmp(ctrlc_
) != 0) {
237 char *line(readline(prompt
));
246 if (line
[0] == '?') {
247 std::string
data(line
+ 1);
248 if (data
== "bypass") {
250 fprintf(fout
, "bypass == %s\n", bypass
? "true" : "false");
252 } else if (data
== "debug") {
254 fprintf(fout
, "debug == %s\n", debug
? "true" : "false");
256 } else if (data
== "expand") {
258 fprintf(fout
, "expand == %s\n", expand
? "true" : "false");
269 char *begin(line
), *end(line
+ strlen(line
));
270 while (char *nl
= reinterpret_cast<char *>(memchr(begin
, '\n', end
- begin
))) {
272 lines
.push_back(begin
);
276 lines
.push_back(begin
);
286 cy::parser
parser(driver
);
287 Setup(driver
, parser
);
289 driver
.data_
= command
.c_str();
290 driver
.size_
= command
.size();
292 if (parser
.parse() != 0 || !driver
.errors_
.empty()) {
293 for (CYDriver::Errors::const_iterator
error(driver
.errors_
.begin()); error
!= driver
.errors_
.end(); ++error
) {
294 cy::position
begin(error
->location_
.begin
);
295 if (begin
.line
!= lines
.size() || begin
.column
- 1 != lines
.back().size() || error
->warning_
) {
296 cy::position
end(error
->location_
.end
);
298 if (begin
.line
!= lines
.size()) {
300 std::cerr
<< lines
[begin
.line
- 1] << std::endl
;
304 for (size_t i(0); i
!= begin
.column
- 1; ++i
)
306 if (begin
.line
!= end
.line
|| begin
.column
== end
.column
)
308 else for (size_t i(0), e(end
.column
- begin
.column
); i
!= e
; ++i
)
310 std::cerr
<< std::endl
;
313 std::cerr
<< error
->message_
<< std::endl
;
315 add_history(command
.c_str());
321 driver
.errors_
.clear();
328 if (driver
.program_
== NULL
)
334 std::ostringstream str
;
335 CYOutput
out(str
, options
);
336 Setup(out
, driver
, options
);
337 out
<< *driver
.program_
;
342 add_history(command
.c_str());
346 std::cout
<< code
<< std::endl
;
348 Run(client
, code
, fout
, expand
);
351 if (append_history$
!= NULL
) {
352 _syscall(close(_syscall(open(histfile
, O_CREAT
| O_WRONLY
, 0600))));
353 (*append_history$
)(histlines
, histfile
);
355 write_history(histfile
);
362 static void *Map(const char *path
, size_t *psize
) {
364 _syscall(fd
= open(path
, O_RDONLY
));
367 _syscall(fstat(fd
, &stat
));
368 size_t size(stat
.st_size
);
373 _syscall(base
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0));
379 void InjectLibrary(pid_t pid
);
381 int Main(int argc
, char const * const argv
[], char const * const envp
[]) {
382 bool tty(isatty(STDIN_FILENO
));
386 append_history$
= reinterpret_cast<int (*)(int, const char *)>(dlsym(RTLD_DEFAULT
, "append_history"));
389 pid_t
pid(_not(pid_t
));
394 _aprcall(apr_getopt_init(&state
, pool
, argc
, argv
));
400 apr_status_t
status(apr_getopt(state
,
414 "usage: cycript [-c]"
418 " [<script> [<arg>...]]\n"
432 else if (strcmp(arg
, "rename") == 0)
433 options
.verbose_
= true;
435 else if (strcmp(arg
, "bison") == 0)
439 fprintf(stderr
, "invalid name for -g\n");
446 else if (strcmp(arg
, "minify") == 0)
449 fprintf(stderr
, "invalid name for -n\n");
456 size_t size(strlen(arg
));
459 pid
= strtoul(arg
, &end
, 0);
460 if (arg
+ size
!= end
) {
461 // XXX: arg needs to be escaped in some horrendous way of doom
462 const char *command(apr_psprintf(pool
, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", arg
));
464 if (FILE *pids
= popen(command
, "r")) {
469 size_t read(fread(value
+ size
, 1, sizeof(value
) - size
, pids
));
474 if (size
== sizeof(value
)) {
484 if (value
[size
- 1] == '\n') {
490 size
= strlen(value
);
491 pid
= strtoul(value
, &end
, 0);
492 if (value
+ size
!= end
) fail
:
494 _syscall(pclose(pids
));
497 if (pid
== _not(pid_t
)) {
498 fprintf(stderr
, "invalid pid for -p\n");
515 if (pid
!= _not(pid_t
) && ind
< argc
- 1) {
516 fprintf(stderr
, "-p cannot set argv\n");
520 if (pid
!= _not(pid_t
) && compile
) {
521 fprintf(stderr
, "-p conflicts with -c\n");
530 // XXX: const_cast?! wtf gcc :(
531 CYSetArgs(argc
- ind
- 1, const_cast<const char **>(argv
+ ind
+ 1));
534 if (strcmp(script
, "-") == 0)
539 if (pid
!= _not(pid_t
) && script
== NULL
&& !tty
) {
540 fprintf(stderr
, "non-terminal attaching to remote console\n");
548 if (pid
== _not(pid_t
))
551 int server(_syscall(socket(PF_UNIX
, SOCK_STREAM
, 0))); try {
552 struct sockaddr_un address
;
553 memset(&address
, 0, sizeof(address
));
554 address
.sun_family
= AF_UNIX
;
556 sprintf(address
.sun_path
, "/tmp/.s.cy.%u", getpid());
558 _syscall(bind(server
, reinterpret_cast<sockaddr
*>(&address
), SUN_LEN(&address
)));
559 _syscall(chmod(address
.sun_path
, 0777));
562 _syscall(listen(server
, 1));
564 client
= _syscall(accept(server
, NULL
, NULL
));
567 unlink(address
.sun_path
);
571 _syscall(close(server
));
579 if (script
== NULL
&& tty
)
580 Console(pool
, client
, options
);
582 CYDriver
driver(script
?: "<stdin>");
583 cy::parser
parser(driver
);
584 Setup(driver
, parser
);
588 if (script
== NULL
) {
592 driver
.file_
= stdin
;
595 start
= reinterpret_cast<char *>(Map(script
, &size
));
598 if (size
>= 2 && start
[0] == '#' && start
[1] == '!') {
601 if (void *line
= memchr(start
, '\n', end
- start
))
602 start
= reinterpret_cast<char *>(line
);
607 driver
.data_
= start
;
608 driver
.size_
= end
- start
;
611 if (parser
.parse() != 0 || !driver
.errors_
.empty()) {
612 for (CYDriver::Errors::const_iterator
i(driver
.errors_
.begin()); i
!= driver
.errors_
.end(); ++i
)
613 std::cerr
<< i
->location_
.begin
<< ": " << i
->message_
<< std::endl
;
614 } else if (driver
.program_
!= NULL
)
616 std::string
code(start
, end
-start
);
617 Run(client
, code
, stdout
);
619 std::ostringstream str
;
620 CYOutput
out(str
, options
);
621 Setup(out
, driver
, options
);
622 out
<< *driver
.program_
;
623 std::string
code(str
.str());
627 Run(client
, code
, stdout
);
634 int main(int argc
, char const * const argv
[], char const * const envp
[]) {
635 apr_status_t
status(apr_app_initialize(&argc
, &argv
, &envp
));
637 if (status
!= APR_SUCCESS
) {
638 fprintf(stderr
, "apr_app_initialize() != APR_SUCCESS\n");
641 return Main(argc
, argv
, envp
);
642 } catch (const CYException
&error
) {
644 fprintf(stderr
, "%s\n", error
.PoolCString(pool
));