]> git.saurik.com Git - cycript.git/blame - Console.cpp
Fleshed out support for &, reworked nil internally, setup new Instance(), and impleme...
[cycript.git] / Console.cpp
CommitLineData
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
69static jmp_buf ctrlc_;
70
579ed526 71static void sigint(int) {
057f943f
JF
72 longjmp(ctrlc_, 1);
73}
74
6ec85c5b 75void Run(int socket, const char *data, size_t size, FILE *fout, 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
6ec85c5b
JF
123void Run(int socket, std::string &code, FILE *fout, bool expand = false) {
124 Run(socket, code.c_str(), code.size(), fout, expand);
fcc64bb5
JF
125}
126
967067aa 127static 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
JF
201 cy::position end(error->location_.end);
202 if (begin.line != lines.size()) {
203 std::cerr << " | ";
204 std::cerr << lines[begin.line - 1] << std::endl;
205 }
206 std::cerr << " | ";
207 for (size_t i(0); i != begin.column - 1; ++i)
208 std::cerr << '.';
209 if (begin.line != end.line)
210 std::cerr << '^';
211 else for (size_t i(0), e(end.column - begin.column); i != e; ++i)
212 std::cerr << '^';
213 std::cerr << std::endl;
214 std::cerr << " | ";
215 std::cerr << error->message_ << std::endl;
1f8eae40
JF
216 add_history(command.c_str());
217 goto restart;
218 }
219 }
057f943f 220
1f8eae40 221 driver.errors_.clear();
057f943f 222
1f8eae40
JF
223 command += '\n';
224 prompt = "cy> ";
225 goto read;
057f943f
JF
226 }
227
1f8eae40
JF
228 if (driver.source_ == NULL)
229 goto restart;
057f943f 230
967067aa
JF
231 if (socket != -1)
232 code = command;
233 else {
234 std::ostringstream str;
235 driver.source_->Show(str);
236 code = str.str();
237 }
057f943f
JF
238 }
239
057f943f
JF
240 add_history(command.c_str());
241
1f8eae40
JF
242 if (debug)
243 std::cout << code << std::endl;
057f943f 244
6ec85c5b 245 Run(socket, code, fout, expand);
b09da87b 246 }
057f943f 247
b09da87b
JF
248 fputs("\n", fout);
249 fflush(fout);
250}
057f943f 251
579ed526 252static void *Map(const char *path, size_t *psize) {
b09da87b
JF
253 int fd;
254 _syscall(fd = open(path, O_RDONLY));
057f943f 255
b09da87b
JF
256 struct stat stat;
257 _syscall(fstat(fd, &stat));
258 size_t size(stat.st_size);
057f943f 259
b09da87b 260 *psize = size;
057f943f 261
b09da87b
JF
262 void *base;
263 _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
057f943f 264
b09da87b
JF
265 _syscall(close(fd));
266 return base;
267}
057f943f 268
967067aa
JF
269int main(int argc, char *argv[]) {
270 pid_t pid(_not(pid_t));
271
272 for (;;) switch (getopt(argc, argv, "p:")) {
273 case -1:
274 goto getopt;
275 case '?':
fcc64bb5 276 fprintf(stderr, "usage: cycript [-p <pid>] [<script> [<arg>...]]\n");
967067aa
JF
277 return 1;
278
279 case 'p': {
280 size_t size(strlen(optarg));
281 char *end;
282 pid = strtoul(optarg, &end, 0);
283 if (optarg + size != end) {
284 fprintf(stderr, "invalid pid for -p\n");
285 return 1;
286 }
287 } break;
288 } getopt:;
289
b09da87b
JF
290 const char *script;
291
fcc64bb5
JF
292 if (optind < argc - 1 && pid != _not(pid_t)) {
293 fprintf(stderr, "-p cannot set argv\n");
294 return 1;
295 }
296
967067aa 297 if (optind == argc)
b09da87b
JF
298 script = NULL;
299 else {
967067aa
JF
300 // XXX: const_cast?! wtf gcc :(
301 CYSetArgs(argc - optind - 1, const_cast<const char **>(argv + optind + 1));
302 script = argv[optind];
303 if (strcmp(script, "-") == 0)
304 script = NULL;
b09da87b
JF
305 }
306
967067aa
JF
307 int socket;
308
309 if (pid == _not(pid_t))
310 socket = -1;
311 else {
312 socket = _syscall(::socket(PF_UNIX, SOCK_STREAM, 0));
313
314 struct sockaddr_un address;
315 memset(&address, 0, sizeof(address));
316 address.sun_family = AF_UNIX;
317 sprintf(address.sun_path, "/tmp/.s.cy.%u", pid);
318
319 _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
320 }
579ed526 321
967067aa
JF
322 if (script == NULL)
323 Console(socket);
b09da87b
JF
324 else {
325 CYDriver driver(script);
326 cy::parser parser(driver);
327
328 size_t size;
329 char *start(reinterpret_cast<char *>(Map(script, &size)));
330 char *end(start + size);
331
332 if (size >= 2 && start[0] == '#' && start[1] == '!') {
333 start += 2;
2b1245e4
JF
334
335 if (void *line = memchr(start, '\n', end - start))
336 start = reinterpret_cast<char *>(line);
337 else
338 start = end;
057f943f
JF
339 }
340
b09da87b
JF
341 driver.data_ = start;
342 driver.size_ = end - start;
62ca2b82 343
b09da87b
JF
344 if (parser.parse() != 0 || !driver.errors_.empty()) {
345 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
346 std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
fcc64bb5
JF
347 } else if (driver.source_ != NULL)
348 if (socket != -1)
349 Run(socket, start, end - start, stdout);
350 else {
351 std::ostringstream str;
352 driver.source_->Show(str);
353 std::string code(str.str());
354 Run(socket, code, stdout);
355 }
057f943f 356 }
62ca2b82 357
057f943f 358 return 0;
62ca2b82 359}