]>
Commit | Line | Data |
---|---|---|
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 | ||
40 | #define _GNU_SOURCE | |
41 | ||
42 | #include <substrate.h> | |
43 | #include "cycript.hpp" | |
44 | ||
45 | #include <cstdio> | |
46 | #include <sstream> | |
47 | ||
48 | #include <setjmp.h> | |
49 | ||
50 | #include <readline/readline.h> | |
51 | #include <readline/history.h> | |
52 | ||
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 | ||
62 | #include "Cycript.tab.hh" | |
63 | ||
64 | #include <sys/types.h> | |
65 | #include <sys/socket.h> | |
66 | #include <netinet/in.h> | |
67 | #include <sys/un.h> | |
68 | ||
69 | static jmp_buf ctrlc_; | |
70 | ||
71 | static void sigint(int) { | |
72 | longjmp(ctrlc_, 1); | |
73 | } | |
74 | ||
75 | void Run(int socket, const char *data, size_t size, FILE *fout = NULL, bool expand = false) { | |
76 | CYPool pool; | |
77 | ||
78 | const char *json; | |
79 | if (socket == -1) { | |
80 | json = CYExecute(pool, data); | |
81 | if (json != NULL) | |
82 | size = strlen(json); | |
83 | } else { | |
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 | } | |
95 | } | |
96 | ||
97 | if (json != NULL && fout != NULL) { | |
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: | |
118 | fputs("\n", fout); | |
119 | fflush(fout); | |
120 | } | |
121 | } | |
122 | ||
123 | void Run(int socket, std::string &code, FILE *fout = NULL, bool expand = false) { | |
124 | Run(socket, code.c_str(), code.size(), fout, expand); | |
125 | } | |
126 | ||
127 | static void Console(int socket) { | |
128 | bool bypass(false); | |
129 | bool debug(false); | |
130 | bool expand(false); | |
131 | ||
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 | ||
146 | bool extra(false); | |
147 | const char *prompt("cy# "); | |
148 | ||
149 | if (setjmp(ctrlc_) != 0) { | |
150 | fputs("\n", fout); | |
151 | fflush(fout); | |
152 | goto restart; | |
153 | } | |
154 | ||
155 | read: | |
156 | char *line(readline(prompt)); | |
157 | if (line == NULL) | |
158 | break; | |
159 | ||
160 | if (!extra) { | |
161 | extra = true; | |
162 | if (line[0] == '?') { | |
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); | |
172 | } else if (data == "expand") { | |
173 | expand = !expand; | |
174 | fprintf(fout, "expand == %s\n", expand ? "true" : "false"); | |
175 | fflush(fout); | |
176 | } | |
177 | add_history(line); | |
178 | goto restart; | |
179 | } | |
180 | } | |
181 | ||
182 | lines.push_back(line); | |
183 | command += line; | |
184 | free(line); | |
185 | ||
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()) { | |
198 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { | |
199 | cy::position begin(error->location_.begin); | |
200 | if (begin.line != lines.size() || begin.column - 1 != lines.back().size()) { | |
201 | cy::position end(error->location_.end); | |
202 | ||
203 | if (begin.line != lines.size()) { | |
204 | std::cerr << " | "; | |
205 | std::cerr << lines[begin.line - 1] << std::endl; | |
206 | } | |
207 | ||
208 | std::cerr << " | "; | |
209 | for (size_t i(0); i != begin.column - 1; ++i) | |
210 | std::cerr << '.'; | |
211 | if (begin.line != end.line || begin.column == end.column) | |
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; | |
216 | ||
217 | std::cerr << " | "; | |
218 | std::cerr << error->message_ << std::endl; | |
219 | ||
220 | add_history(command.c_str()); | |
221 | goto restart; | |
222 | } | |
223 | } | |
224 | ||
225 | driver.errors_.clear(); | |
226 | ||
227 | command += '\n'; | |
228 | prompt = "cy> "; | |
229 | goto read; | |
230 | } | |
231 | ||
232 | if (driver.source_ == NULL) | |
233 | goto restart; | |
234 | ||
235 | if (socket != -1) | |
236 | code = command; | |
237 | else { | |
238 | std::ostringstream str; | |
239 | driver.source_->Show(str); | |
240 | code = str.str(); | |
241 | } | |
242 | } | |
243 | ||
244 | add_history(command.c_str()); | |
245 | ||
246 | if (debug) | |
247 | std::cout << code << std::endl; | |
248 | ||
249 | Run(socket, code, fout, expand); | |
250 | } | |
251 | ||
252 | fputs("\n", fout); | |
253 | fflush(fout); | |
254 | } | |
255 | ||
256 | static void *Map(const char *path, size_t *psize) { | |
257 | int fd; | |
258 | _syscall(fd = open(path, O_RDONLY)); | |
259 | ||
260 | struct stat stat; | |
261 | _syscall(fstat(fd, &stat)); | |
262 | size_t size(stat.st_size); | |
263 | ||
264 | *psize = size; | |
265 | ||
266 | void *base; | |
267 | _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); | |
268 | ||
269 | _syscall(close(fd)); | |
270 | return base; | |
271 | } | |
272 | ||
273 | int main(int argc, char *argv[]) { | |
274 | bool tty(isatty(STDIN_FILENO)); | |
275 | pid_t pid(_not(pid_t)); | |
276 | ||
277 | for (;;) switch (getopt(argc, argv, "p:")) { | |
278 | case -1: | |
279 | goto getopt; | |
280 | case '?': | |
281 | fprintf(stderr, "usage: cycript [-p <pid>] [<script> [<arg>...]]\n"); | |
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 | ||
295 | const char *script; | |
296 | ||
297 | if (optind < argc - 1 && pid != _not(pid_t)) { | |
298 | fprintf(stderr, "-p cannot set argv\n"); | |
299 | return 1; | |
300 | } | |
301 | ||
302 | if (optind == argc) | |
303 | script = NULL; | |
304 | else { | |
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; | |
310 | } | |
311 | ||
312 | if (script == NULL && !tty && pid != _not(pid_t)) { | |
313 | fprintf(stderr, "non-terminal attaching to remove console\n"); | |
314 | return 1; | |
315 | } | |
316 | ||
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 | } | |
331 | ||
332 | if (script == NULL && tty) | |
333 | Console(socket); | |
334 | else { | |
335 | CYDriver driver(script ?: "<stdin>"); | |
336 | cy::parser parser(driver); | |
337 | ||
338 | char *start, *end; | |
339 | ||
340 | if (script == NULL) { | |
341 | start = NULL; | |
342 | end = NULL; | |
343 | ||
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; | |
352 | ||
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 | } | |
362 | ||
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; | |
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()); | |
373 | Run(socket, code); | |
374 | } | |
375 | } | |
376 | ||
377 | return 0; | |
378 | } |