]> git.saurik.com Git - cycript.git/blob - Application.cpp
ae638a6c5d833ff9c3c6092d2ec2194626225328
[cycript.git] / Application.cpp
1 #define _GNU_SOURCE
2
3 #include <substrate.h>
4 #include "cycript.hpp"
5
6 #include <cstdio>
7 #include <sstream>
8
9 #include <setjmp.h>
10
11 #include <readline/readline.h>
12 #include <readline/history.h>
13
14 #include <sys/mman.h>
15
16 #include <errno.h>
17 #include <unistd.h>
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <fcntl.h>
22
23 #include "Cycript.tab.hh"
24
25 static jmp_buf ctrlc_;
26
27 void sigint(int) {
28 longjmp(ctrlc_, 1);
29 }
30
31 void Run(const char *code, FILE *fout) {
32 JSStringRef script(JSStringCreateWithUTF8CString(code));
33
34 JSContextRef context(CYGetJSContext());
35
36 JSValueRef exception(NULL);
37 JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception));
38 JSStringRelease(script);
39
40 if (exception != NULL)
41 result = exception;
42
43 if (!JSValueIsUndefined(context, result)) {
44 CYPool pool;
45 const char *json;
46
47 json = CYPoolJSONString(pool, context, result);
48
49 if (fout != NULL) {
50 fputs(json, fout);
51 fputs("\n", fout);
52 fflush(fout);
53 }
54 }
55 }
56
57 void Console() {
58 bool bypass(false);
59 bool debug(false);
60
61 FILE *fout(stdout);
62
63 rl_bind_key('\t', rl_insert);
64
65 struct sigaction action;
66 sigemptyset(&action.sa_mask);
67 action.sa_handler = &sigint;
68 action.sa_flags = 0;
69 sigaction(SIGINT, &action, NULL);
70
71 restart: for (;;) {
72 std::string command;
73 std::vector<std::string> lines;
74
75 bool extra(false);
76 const char *prompt("cy# ");
77
78 if (setjmp(ctrlc_) != 0) {
79 fputs("\n", fout);
80 fflush(fout);
81 goto restart;
82 }
83
84 read:
85 char *line(readline(prompt));
86 if (line == NULL)
87 break;
88
89 if (!extra) {
90 extra = true;
91 if (line[0] == '\\') {
92 std::string data(line + 1);
93 if (data == "bypass") {
94 bypass = !bypass;
95 fprintf(fout, "bypass == %s\n", bypass ? "true" : "false");
96 fflush(fout);
97 } else if (data == "debug") {
98 debug = !debug;
99 fprintf(fout, "debug == %s\n", debug ? "true" : "false");
100 fflush(fout);
101 }
102 add_history(line);
103 goto restart;
104 }
105 }
106
107 lines.push_back(line);
108 command += line;
109 free(line);
110
111 std::string code;
112
113 if (bypass)
114 code = command;
115 else {
116 CYDriver driver("");
117 cy::parser parser(driver);
118
119 driver.data_ = command.c_str();
120 driver.size_ = command.size();
121
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());
128 goto restart;
129 }
130 }
131
132 driver.errors_.clear();
133
134 command += '\n';
135 prompt = "cy> ";
136 goto read;
137 }
138
139 if (driver.source_ == NULL)
140 goto restart;
141
142 std::ostringstream str;
143 driver.source_->Show(str);
144 code = str.str();
145 }
146
147 add_history(command.c_str());
148
149 if (debug)
150 std::cout << code << std::endl;
151
152 Run(code.c_str(), fout);
153 }
154
155 fputs("\n", fout);
156 fflush(fout);
157 }
158
159 void *Map(const char *path, size_t *psize) {
160 int fd;
161 _syscall(fd = open(path, O_RDONLY));
162
163 struct stat stat;
164 _syscall(fstat(fd, &stat));
165 size_t size(stat.st_size);
166
167 *psize = size;
168
169 void *base;
170 _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
171
172 _syscall(close(fd));
173 return base;
174 }
175
176 int main(int argc, const char *argv[]) {
177 const char *script;
178
179 if (argc == 1)
180 script = NULL;
181 else {
182 CYSetArgs(argc - 1, argv + 1);
183 script = argv[1];
184 }
185
186 if (script == NULL || strcmp(script, "-") == 0)
187 Console();
188 else {
189 CYDriver driver(script);
190 cy::parser parser(driver);
191
192 size_t size;
193 char *start(reinterpret_cast<char *>(Map(script, &size)));
194 char *end(start + size);
195
196 if (size >= 2 && start[0] == '#' && start[1] == '!') {
197 start += 2;
198 while (start != end && *start++ != '\n');
199 }
200
201 driver.data_ = start;
202 driver.size_ = end - start;
203
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);
213 }
214 }
215
216 return 0;
217 }