]>
Commit | Line | Data |
---|---|---|
b4aa79af JF |
1 | /* Cycript - Remove Execution Server and Disassembler |
2 | * Copyright (C) 2009 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
5 | /* Modified BSD License {{{ */ | |
6 | /* | |
7 | * Redistribution and use in source and binary | |
8 | * forms, with or without modification, are permitted | |
9 | * provided that the following conditions are met: | |
10 | * | |
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 | |
18 | * distribution. | |
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. | |
22 | * | |
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. | |
37 | */ | |
38 | /* }}} */ | |
39 | ||
057f943f JF |
40 | #define _GNU_SOURCE |
41 | ||
693d501b | 42 | #include <substrate.h> |
30ddc20c | 43 | #include "cycript.hpp" |
057f943f JF |
44 | |
45 | #include <cstdio> | |
46 | #include <sstream> | |
47 | ||
48 | #include <setjmp.h> | |
49 | ||
50 | #include <readline/readline.h> | |
51 | #include <readline/history.h> | |
52 | ||
b09da87b JF |
53 | #include <sys/mman.h> |
54 | ||
55 | #include <errno.h> | |
56 | #include <unistd.h> | |
57 | ||
58 | #include <sys/types.h> | |
59 | #include <sys/stat.h> | |
60 | #include <fcntl.h> | |
61 | ||
057f943f JF |
62 | #include "Cycript.tab.hh" |
63 | ||
967067aa JF |
64 | #include <sys/types.h> |
65 | #include <sys/socket.h> | |
66 | #include <netinet/in.h> | |
67 | #include <sys/un.h> | |
68 | ||
057f943f JF |
69 | static jmp_buf ctrlc_; |
70 | ||
579ed526 | 71 | static void sigint(int) { |
057f943f JF |
72 | longjmp(ctrlc_, 1); |
73 | } | |
74 | ||
48e3be8a | 75 | void Run(int socket, const char *data, size_t size, FILE *fout = NULL, bool expand = false) { |
967067aa | 76 | CYPool pool; |
b09da87b | 77 | |
967067aa | 78 | const char *json; |
6ec85c5b | 79 | if (socket == -1) { |
fcc64bb5 | 80 | json = CYExecute(pool, data); |
6ec85c5b JF |
81 | if (json != NULL) |
82 | size = strlen(json); | |
83 | } else { | |
967067aa JF |
84 | CYSendAll(socket, &size, sizeof(size)); |
85 | CYSendAll(socket, data, size); | |
86 | CYRecvAll(socket, &size, sizeof(size)); | |
87 | if (size == _not(size_t)) | |
88 | json = NULL; | |
89 | else { | |
90 | char *temp(new(pool) char[size + 1]); | |
91 | CYRecvAll(socket, temp, size); | |
92 | temp[size] = '\0'; | |
93 | json = temp; | |
94 | } | |
4cf49641 | 95 | } |
b09da87b | 96 | |
967067aa | 97 | if (json != NULL && fout != NULL) { |
6ec85c5b JF |
98 | if (!expand || json[0] != '"' && json[0] != '\'') |
99 | fputs(json, fout); | |
100 | else for (size_t i(0); i != size; ++i) | |
101 | if (json[i] != '\\') | |
102 | fputc(json[i], fout); | |
103 | else switch(json[++i]) { | |
104 | case '\0': goto done; | |
105 | case '\\': fputc('\\', fout); break; | |
106 | case '\'': fputc('\'', fout); break; | |
107 | case '"': fputc('"', fout); break; | |
108 | case 'b': fputc('\b', fout); break; | |
109 | case 'f': fputc('\f', fout); break; | |
110 | case 'n': fputc('\n', fout); break; | |
111 | case 'r': fputc('\r', fout); break; | |
112 | case 't': fputc('\t', fout); break; | |
113 | case 'v': fputc('\v', fout); break; | |
114 | default: fputc('\\', fout); --i; break; | |
115 | } | |
116 | ||
117 | done: | |
967067aa JF |
118 | fputs("\n", fout); |
119 | fflush(fout); | |
b09da87b JF |
120 | } |
121 | } | |
122 | ||
48e3be8a | 123 | void Run(int socket, std::string &code, FILE *fout = NULL, bool expand = false) { |
6ec85c5b | 124 | Run(socket, code.c_str(), code.size(), fout, expand); |
fcc64bb5 JF |
125 | } |
126 | ||
967067aa | 127 | static void Console(int socket) { |
1f8eae40 JF |
128 | bool bypass(false); |
129 | bool debug(false); | |
6ec85c5b | 130 | bool expand(false); |
1f8eae40 | 131 | |
057f943f JF |
132 | FILE *fout(stdout); |
133 | ||
134 | rl_bind_key('\t', rl_insert); | |
135 | ||
136 | struct sigaction action; | |
137 | sigemptyset(&action.sa_mask); | |
138 | action.sa_handler = &sigint; | |
139 | action.sa_flags = 0; | |
140 | sigaction(SIGINT, &action, NULL); | |
141 | ||
142 | restart: for (;;) { | |
143 | std::string command; | |
144 | std::vector<std::string> lines; | |
145 | ||
931b816a JF |
146 | bool extra(false); |
147 | const char *prompt("cy# "); | |
148 | ||
057f943f JF |
149 | if (setjmp(ctrlc_) != 0) { |
150 | fputs("\n", fout); | |
151 | fflush(fout); | |
152 | goto restart; | |
153 | } | |
154 | ||
057f943f JF |
155 | read: |
156 | char *line(readline(prompt)); | |
157 | if (line == NULL) | |
158 | break; | |
931b816a JF |
159 | |
160 | if (!extra) { | |
161 | extra = true; | |
6ec85c5b | 162 | if (line[0] == '?') { |
1f8eae40 JF |
163 | std::string data(line + 1); |
164 | if (data == "bypass") { | |
165 | bypass = !bypass; | |
166 | fprintf(fout, "bypass == %s\n", bypass ? "true" : "false"); | |
167 | fflush(fout); | |
168 | } else if (data == "debug") { | |
169 | debug = !debug; | |
170 | fprintf(fout, "debug == %s\n", debug ? "true" : "false"); | |
171 | fflush(fout); | |
6ec85c5b JF |
172 | } else if (data == "expand") { |
173 | expand = !expand; | |
174 | fprintf(fout, "expand == %s\n", expand ? "true" : "false"); | |
175 | fflush(fout); | |
1f8eae40 | 176 | } |
d35a3b07 | 177 | add_history(line); |
931b816a JF |
178 | goto restart; |
179 | } | |
180 | } | |
181 | ||
057f943f JF |
182 | lines.push_back(line); |
183 | command += line; | |
184 | free(line); | |
185 | ||
1f8eae40 JF |
186 | std::string code; |
187 | ||
188 | if (bypass) | |
189 | code = command; | |
190 | else { | |
191 | CYDriver driver(""); | |
192 | cy::parser parser(driver); | |
193 | ||
194 | driver.data_ = command.c_str(); | |
195 | driver.size_ = command.size(); | |
196 | ||
197 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
f0360d51 JF |
198 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { |
199 | cy::position begin(error->location_.begin); | |
1f8eae40 | 200 | if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) { |
f0360d51 | 201 | cy::position end(error->location_.end); |
48e3be8a | 202 | |
f0360d51 JF |
203 | if (begin.line != lines.size()) { |
204 | std::cerr << " | "; | |
205 | std::cerr << lines[begin.line - 1] << std::endl; | |
206 | } | |
48e3be8a | 207 | |
f0360d51 JF |
208 | std::cerr << " | "; |
209 | for (size_t i(0); i != begin.column - 1; ++i) | |
210 | std::cerr << '.'; | |
48e3be8a | 211 | if (begin.line != end.line || begin.column == end.column) |
f0360d51 JF |
212 | std::cerr << '^'; |
213 | else for (size_t i(0), e(end.column - begin.column); i != e; ++i) | |
214 | std::cerr << '^'; | |
215 | std::cerr << std::endl; | |
48e3be8a | 216 | |
f0360d51 JF |
217 | std::cerr << " | "; |
218 | std::cerr << error->message_ << std::endl; | |
48e3be8a | 219 | |
1f8eae40 JF |
220 | add_history(command.c_str()); |
221 | goto restart; | |
222 | } | |
223 | } | |
057f943f | 224 | |
1f8eae40 | 225 | driver.errors_.clear(); |
057f943f | 226 | |
1f8eae40 JF |
227 | command += '\n'; |
228 | prompt = "cy> "; | |
229 | goto read; | |
057f943f JF |
230 | } |
231 | ||
1f8eae40 JF |
232 | if (driver.source_ == NULL) |
233 | goto restart; | |
057f943f | 234 | |
967067aa JF |
235 | if (socket != -1) |
236 | code = command; | |
237 | else { | |
238 | std::ostringstream str; | |
239 | driver.source_->Show(str); | |
240 | code = str.str(); | |
241 | } | |
057f943f JF |
242 | } |
243 | ||
057f943f JF |
244 | add_history(command.c_str()); |
245 | ||
1f8eae40 JF |
246 | if (debug) |
247 | std::cout << code << std::endl; | |
057f943f | 248 | |
6ec85c5b | 249 | Run(socket, code, fout, expand); |
b09da87b | 250 | } |
057f943f | 251 | |
b09da87b JF |
252 | fputs("\n", fout); |
253 | fflush(fout); | |
254 | } | |
057f943f | 255 | |
579ed526 | 256 | static void *Map(const char *path, size_t *psize) { |
b09da87b JF |
257 | int fd; |
258 | _syscall(fd = open(path, O_RDONLY)); | |
057f943f | 259 | |
b09da87b JF |
260 | struct stat stat; |
261 | _syscall(fstat(fd, &stat)); | |
262 | size_t size(stat.st_size); | |
057f943f | 263 | |
b09da87b | 264 | *psize = size; |
057f943f | 265 | |
b09da87b JF |
266 | void *base; |
267 | _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); | |
057f943f | 268 | |
b09da87b JF |
269 | _syscall(close(fd)); |
270 | return base; | |
271 | } | |
057f943f | 272 | |
967067aa | 273 | int main(int argc, char *argv[]) { |
48e3be8a | 274 | bool tty(isatty(STDIN_FILENO)); |
967067aa JF |
275 | pid_t pid(_not(pid_t)); |
276 | ||
277 | for (;;) switch (getopt(argc, argv, "p:")) { | |
278 | case -1: | |
279 | goto getopt; | |
280 | case '?': | |
fcc64bb5 | 281 | fprintf(stderr, "usage: cycript [-p <pid>] [<script> [<arg>...]]\n"); |
967067aa JF |
282 | return 1; |
283 | ||
284 | case 'p': { | |
285 | size_t size(strlen(optarg)); | |
286 | char *end; | |
287 | pid = strtoul(optarg, &end, 0); | |
288 | if (optarg + size != end) { | |
289 | fprintf(stderr, "invalid pid for -p\n"); | |
290 | return 1; | |
291 | } | |
292 | } break; | |
293 | } getopt:; | |
294 | ||
b09da87b JF |
295 | const char *script; |
296 | ||
fcc64bb5 JF |
297 | if (optind < argc - 1 && pid != _not(pid_t)) { |
298 | fprintf(stderr, "-p cannot set argv\n"); | |
299 | return 1; | |
300 | } | |
301 | ||
967067aa | 302 | if (optind == argc) |
b09da87b JF |
303 | script = NULL; |
304 | else { | |
967067aa JF |
305 | // XXX: const_cast?! wtf gcc :( |
306 | CYSetArgs(argc - optind - 1, const_cast<const char **>(argv + optind + 1)); | |
307 | script = argv[optind]; | |
308 | if (strcmp(script, "-") == 0) | |
309 | script = NULL; | |
b09da87b JF |
310 | } |
311 | ||
48e3be8a JF |
312 | if (script == NULL && !tty && pid != _not(pid_t)) { |
313 | fprintf(stderr, "non-terminal attaching to remove console\n"); | |
314 | return 1; | |
315 | } | |
316 | ||
967067aa JF |
317 | int socket; |
318 | ||
319 | if (pid == _not(pid_t)) | |
320 | socket = -1; | |
321 | else { | |
322 | socket = _syscall(::socket(PF_UNIX, SOCK_STREAM, 0)); | |
323 | ||
324 | struct sockaddr_un address; | |
325 | memset(&address, 0, sizeof(address)); | |
326 | address.sun_family = AF_UNIX; | |
327 | sprintf(address.sun_path, "/tmp/.s.cy.%u", pid); | |
328 | ||
329 | _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
330 | } | |
579ed526 | 331 | |
48e3be8a | 332 | if (script == NULL && tty) |
967067aa | 333 | Console(socket); |
b09da87b | 334 | else { |
48e3be8a | 335 | CYDriver driver(script ?: "<stdin>"); |
b09da87b JF |
336 | cy::parser parser(driver); |
337 | ||
48e3be8a | 338 | char *start, *end; |
b09da87b | 339 | |
48e3be8a JF |
340 | if (script == NULL) { |
341 | start = NULL; | |
342 | end = NULL; | |
2b1245e4 | 343 | |
48e3be8a JF |
344 | driver.file_ = stdin; |
345 | } else { | |
346 | size_t size; | |
347 | start = reinterpret_cast<char *>(Map(script, &size)); | |
348 | end = start + size; | |
349 | ||
350 | if (size >= 2 && start[0] == '#' && start[1] == '!') { | |
351 | start += 2; | |
057f943f | 352 | |
48e3be8a JF |
353 | if (void *line = memchr(start, '\n', end - start)) |
354 | start = reinterpret_cast<char *>(line); | |
355 | else | |
356 | start = end; | |
357 | } | |
358 | ||
359 | driver.data_ = start; | |
360 | driver.size_ = end - start; | |
361 | } | |
62ca2b82 | 362 | |
b09da87b JF |
363 | if (parser.parse() != 0 || !driver.errors_.empty()) { |
364 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) | |
365 | std::cerr << i->location_.begin << ": " << i->message_ << std::endl; | |
fcc64bb5 JF |
366 | } else if (driver.source_ != NULL) |
367 | if (socket != -1) | |
368 | Run(socket, start, end - start, stdout); | |
369 | else { | |
370 | std::ostringstream str; | |
371 | driver.source_->Show(str); | |
372 | std::string code(str.str()); | |
48e3be8a | 373 | Run(socket, code); |
fcc64bb5 | 374 | } |
057f943f | 375 | } |
62ca2b82 | 376 | |
057f943f | 377 | return 0; |
62ca2b82 | 378 | } |