]>
git.saurik.com Git - cycript.git/blob - Application.cpp
ae638a6c5d833ff9c3c6092d2ec2194626225328
11 #include <readline/readline.h>
12 #include <readline/history.h>
19 #include <sys/types.h>
23 #include "Cycript.tab.hh"
25 static jmp_buf ctrlc_
;
31 void Run(const char *code
, FILE *fout
) {
32 JSStringRef
script(JSStringCreateWithUTF8CString(code
));
34 JSContextRef
context(CYGetJSContext());
36 JSValueRef
exception(NULL
);
37 JSValueRef
result(JSEvaluateScript(context
, script
, NULL
, NULL
, 0, &exception
));
38 JSStringRelease(script
);
40 if (exception
!= NULL
)
43 if (!JSValueIsUndefined(context
, result
)) {
47 json
= CYPoolJSONString(pool
, context
, result
);
63 rl_bind_key('\t', rl_insert
);
65 struct sigaction action
;
66 sigemptyset(&action
.sa_mask
);
67 action
.sa_handler
= &sigint
;
69 sigaction(SIGINT
, &action
, NULL
);
73 std::vector
<std::string
> lines
;
76 const char *prompt("cy# ");
78 if (setjmp(ctrlc_
) != 0) {
85 char *line(readline(prompt
));
91 if (line
[0] == '\\') {
92 std::string
data(line
+ 1);
93 if (data
== "bypass") {
95 fprintf(fout
, "bypass == %s\n", bypass
? "true" : "false");
97 } else if (data
== "debug") {
99 fprintf(fout
, "debug == %s\n", debug
? "true" : "false");
107 lines
.push_back(line
);
117 cy::parser
parser(driver
);
119 driver
.data_
= command
.c_str();
120 driver
.size_
= command
.size();
122 if (parser
.parse() != 0 || !driver
.errors_
.empty()) {
123 for (CYDriver::Errors::const_iterator
i(driver
.errors_
.begin()); i
!= driver
.errors_
.end(); ++i
) {
124 cy::position
begin(i
->location_
.begin
);
125 if (begin
.line
!= lines
.size() || begin
.column
- 1 != lines
.back().size()) {
126 std::cerr
<< i
->message_
<< std::endl
;
127 add_history(command
.c_str());
132 driver
.errors_
.clear();
139 if (driver
.source_
== NULL
)
142 std::ostringstream str
;
143 driver
.source_
->Show(str
);
147 add_history(command
.c_str());
150 std::cout
<< code
<< std::endl
;
152 Run(code
.c_str(), fout
);
159 void *Map(const char *path
, size_t *psize
) {
161 _syscall(fd
= open(path
, O_RDONLY
));
164 _syscall(fstat(fd
, &stat
));
165 size_t size(stat
.st_size
);
170 _syscall(base
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, 0));
176 int main(int argc
, const char *argv
[]) {
182 CYSetArgs(argc
- 1, argv
+ 1);
186 if (script
== NULL
|| strcmp(script
, "-") == 0)
189 CYDriver
driver(script
);
190 cy::parser
parser(driver
);
193 char *start(reinterpret_cast<char *>(Map(script
, &size
)));
194 char *end(start
+ size
);
196 if (size
>= 2 && start
[0] == '#' && start
[1] == '!') {
198 while (start
!= end
&& *start
++ != '\n');
201 driver
.data_
= start
;
202 driver
.size_
= end
- start
;
204 if (parser
.parse() != 0 || !driver
.errors_
.empty()) {
205 for (CYDriver::Errors::const_iterator
i(driver
.errors_
.begin()); i
!= driver
.errors_
.end(); ++i
)
206 std::cerr
<< i
->location_
.begin
<< ": " << i
->message_
<< std::endl
;
207 } else if (driver
.source_
!= NULL
) {
208 std::ostringstream str
;
209 driver
.source_
->Show(str
);
210 std::string
code(str
.str());
211 std::cout
<< code
<< std::endl
;
212 Run(code
.c_str(), stdout
);