]> git.saurik.com Git - cycript.git/blame - Console.cpp
Removed -mthumb, as it isn't working for me.
[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
4e39dc0b
JF
69static volatile enum {
70 Working,
71 Parsing,
72 Running,
73 Sending,
74 Waiting,
75} mode_;
76
057f943f
JF
77static jmp_buf ctrlc_;
78
579ed526 79static void sigint(int) {
4e39dc0b
JF
80 switch (mode_) {
81 case Working:
82 return;
83 case Parsing:
84 longjmp(ctrlc_, 1);
85 case Running:
86 throw "*** Ctrl-C";
87 case Sending:
88 return;
89 case Waiting:
90 return;
91 }
057f943f
JF
92}
93
cac61857
JF
94#if YYDEBUG
95static bool bison_;
96#endif
b10bd496 97static bool strict_;
cac61857 98
b10bd496 99void Setup(CYDriver &driver, cy::parser &parser) {
cac61857
JF
100#if YYDEBUG
101 if (bison_)
102 parser.set_debug_level(1);
103#endif
b10bd496
JF
104 if (strict_)
105 driver.strict_ = true;
cac61857
JF
106}
107
48e3be8a 108void Run(int socket, const char *data, size_t size, FILE *fout = NULL, bool expand = false) {
967067aa 109 CYPool pool;
b09da87b 110
967067aa 111 const char *json;
6ec85c5b 112 if (socket == -1) {
4e39dc0b 113 mode_ = Running;
fcc64bb5 114 json = CYExecute(pool, data);
4e39dc0b 115 mode_ = Working;
6ec85c5b
JF
116 if (json != NULL)
117 size = strlen(json);
118 } else {
4e39dc0b 119 mode_ = Sending;
967067aa
JF
120 CYSendAll(socket, &size, sizeof(size));
121 CYSendAll(socket, data, size);
4e39dc0b 122 mode_ = Waiting;
967067aa
JF
123 CYRecvAll(socket, &size, sizeof(size));
124 if (size == _not(size_t))
125 json = NULL;
126 else {
127 char *temp(new(pool) char[size + 1]);
128 CYRecvAll(socket, temp, size);
129 temp[size] = '\0';
130 json = temp;
131 }
4e39dc0b 132 mode_ = Working;
4cf49641 133 }
b09da87b 134
967067aa 135 if (json != NULL && fout != NULL) {
6ec85c5b
JF
136 if (!expand || json[0] != '"' && json[0] != '\'')
137 fputs(json, fout);
138 else for (size_t i(0); i != size; ++i)
139 if (json[i] != '\\')
140 fputc(json[i], fout);
141 else switch(json[++i]) {
142 case '\0': goto done;
143 case '\\': fputc('\\', fout); break;
144 case '\'': fputc('\'', fout); break;
145 case '"': fputc('"', fout); break;
146 case 'b': fputc('\b', fout); break;
147 case 'f': fputc('\f', fout); break;
148 case 'n': fputc('\n', fout); break;
149 case 'r': fputc('\r', fout); break;
150 case 't': fputc('\t', fout); break;
151 case 'v': fputc('\v', fout); break;
152 default: fputc('\\', fout); --i; break;
153 }
154
155 done:
967067aa
JF
156 fputs("\n", fout);
157 fflush(fout);
b09da87b
JF
158 }
159}
160
48e3be8a 161void Run(int socket, std::string &code, FILE *fout = NULL, bool expand = false) {
6ec85c5b 162 Run(socket, code.c_str(), code.size(), fout, expand);
fcc64bb5
JF
163}
164
967067aa 165static void Console(int socket) {
1f8eae40
JF
166 bool bypass(false);
167 bool debug(false);
6ec85c5b 168 bool expand(false);
1f8eae40 169
057f943f
JF
170 FILE *fout(stdout);
171
172 rl_bind_key('\t', rl_insert);
173
174 struct sigaction action;
175 sigemptyset(&action.sa_mask);
176 action.sa_handler = &sigint;
177 action.sa_flags = 0;
178 sigaction(SIGINT, &action, NULL);
179
180 restart: for (;;) {
181 std::string command;
182 std::vector<std::string> lines;
183
931b816a
JF
184 bool extra(false);
185 const char *prompt("cy# ");
186
057f943f 187 if (setjmp(ctrlc_) != 0) {
4e39dc0b 188 mode_ = Working;
057f943f
JF
189 fputs("\n", fout);
190 fflush(fout);
191 goto restart;
192 }
193
057f943f 194 read:
4e39dc0b 195 mode_ = Parsing;
057f943f 196 char *line(readline(prompt));
4e39dc0b 197 mode_ = Working;
057f943f
JF
198 if (line == NULL)
199 break;
931b816a
JF
200
201 if (!extra) {
202 extra = true;
6ec85c5b 203 if (line[0] == '?') {
1f8eae40
JF
204 std::string data(line + 1);
205 if (data == "bypass") {
206 bypass = !bypass;
207 fprintf(fout, "bypass == %s\n", bypass ? "true" : "false");
208 fflush(fout);
209 } else if (data == "debug") {
210 debug = !debug;
211 fprintf(fout, "debug == %s\n", debug ? "true" : "false");
212 fflush(fout);
6ec85c5b
JF
213 } else if (data == "expand") {
214 expand = !expand;
215 fprintf(fout, "expand == %s\n", expand ? "true" : "false");
216 fflush(fout);
1f8eae40 217 }
d35a3b07 218 add_history(line);
931b816a
JF
219 goto restart;
220 }
221 }
222
057f943f 223 command += line;
83b5afe2
JF
224 for (char *state, *token(apr_strtok(line, "\n", &state)); token != NULL; token = apr_strtok(NULL, "\n", &state))
225 lines.push_back(token);
057f943f
JF
226 free(line);
227
1f8eae40
JF
228 std::string code;
229
230 if (bypass)
231 code = command;
232 else {
233 CYDriver driver("");
234 cy::parser parser(driver);
b10bd496 235 Setup(driver, parser);
1f8eae40
JF
236
237 driver.data_ = command.c_str();
238 driver.size_ = command.size();
239
240 if (parser.parse() != 0 || !driver.errors_.empty()) {
f0360d51
JF
241 for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) {
242 cy::position begin(error->location_.begin);
b10bd496 243 if (begin.line != lines.size() || begin.column - 1 != lines.back().size() || error->warning_) {
f0360d51 244 cy::position end(error->location_.end);
48e3be8a 245
f0360d51
JF
246 if (begin.line != lines.size()) {
247 std::cerr << " | ";
248 std::cerr << lines[begin.line - 1] << std::endl;
249 }
48e3be8a 250
f0360d51
JF
251 std::cerr << " | ";
252 for (size_t i(0); i != begin.column - 1; ++i)
253 std::cerr << '.';
48e3be8a 254 if (begin.line != end.line || begin.column == end.column)
f0360d51
JF
255 std::cerr << '^';
256 else for (size_t i(0), e(end.column - begin.column); i != e; ++i)
257 std::cerr << '^';
258 std::cerr << std::endl;
48e3be8a 259
f0360d51
JF
260 std::cerr << " | ";
261 std::cerr << error->message_ << std::endl;
48e3be8a 262
1f8eae40
JF
263 add_history(command.c_str());
264 goto restart;
265 }
266 }
057f943f 267
1f8eae40 268 driver.errors_.clear();
057f943f 269
1f8eae40
JF
270 command += '\n';
271 prompt = "cy> ";
272 goto read;
057f943f
JF
273 }
274
b10bd496 275 if (driver.program_ == NULL)
1f8eae40 276 goto restart;
057f943f 277
967067aa
JF
278 if (socket != -1)
279 code = command;
280 else {
281 std::ostringstream str;
652ec1ba 282 CYOutput out(str);
fb98ac0c 283 driver.program_->Multiple(out);
967067aa
JF
284 code = str.str();
285 }
057f943f
JF
286 }
287
057f943f
JF
288 add_history(command.c_str());
289
1f8eae40
JF
290 if (debug)
291 std::cout << code << std::endl;
057f943f 292
6ec85c5b 293 Run(socket, code, fout, expand);
b09da87b 294 }
057f943f 295
b09da87b
JF
296 fputs("\n", fout);
297 fflush(fout);
298}
057f943f 299
579ed526 300static void *Map(const char *path, size_t *psize) {
b09da87b
JF
301 int fd;
302 _syscall(fd = open(path, O_RDONLY));
057f943f 303
b09da87b
JF
304 struct stat stat;
305 _syscall(fstat(fd, &stat));
306 size_t size(stat.st_size);
057f943f 307
b09da87b 308 *psize = size;
057f943f 309
b09da87b
JF
310 void *base;
311 _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0));
057f943f 312
b09da87b
JF
313 _syscall(close(fd));
314 return base;
315}
057f943f 316
967067aa 317int main(int argc, char *argv[]) {
48e3be8a 318 bool tty(isatty(STDIN_FILENO));
967067aa 319 pid_t pid(_not(pid_t));
75b0a457 320 bool compile(false);
967067aa 321
b10bd496 322 for (;;) switch (getopt(argc, argv, "cg:p:s")) {
967067aa
JF
323 case -1:
324 goto getopt;
325 case '?':
75b0a457 326 fprintf(stderr, "usage: cycript [-c] [-p <pid>] [<script> [<arg>...]]\n");
967067aa
JF
327 return 1;
328
75b0a457
JF
329 case 'c':
330 compile = true;
331 break;
332
cac61857
JF
333 case 'g':
334 if (false);
335#if YYDEBUG
336 else if (strcmp(optarg, "bison") == 0)
337 bison_ = true;
338#endif
339 else {
340 fprintf(stderr, "invalid name for -g\n");
341 return 1;
342 }
343 break;
344
967067aa
JF
345 case 'p': {
346 size_t size(strlen(optarg));
347 char *end;
348 pid = strtoul(optarg, &end, 0);
349 if (optarg + size != end) {
350 fprintf(stderr, "invalid pid for -p\n");
351 return 1;
352 }
353 } break;
b10bd496
JF
354
355 case 's':
356 strict_ = true;
357 break;
967067aa
JF
358 } getopt:;
359
b09da87b
JF
360 const char *script;
361
75b0a457 362 if (pid != _not(pid_t) && optind < argc - 1) {
fcc64bb5
JF
363 fprintf(stderr, "-p cannot set argv\n");
364 return 1;
365 }
366
75b0a457
JF
367 if (pid != _not(pid_t) && compile) {
368 fprintf(stderr, "-p conflicts with -c\n");
369 return 1;
370 }
371
967067aa 372 if (optind == argc)
b09da87b
JF
373 script = NULL;
374 else {
967067aa
JF
375 // XXX: const_cast?! wtf gcc :(
376 CYSetArgs(argc - optind - 1, const_cast<const char **>(argv + optind + 1));
377 script = argv[optind];
378 if (strcmp(script, "-") == 0)
379 script = NULL;
b09da87b
JF
380 }
381
48e3be8a
JF
382 if (script == NULL && !tty && pid != _not(pid_t)) {
383 fprintf(stderr, "non-terminal attaching to remove console\n");
384 return 1;
385 }
386
967067aa
JF
387 int socket;
388
389 if (pid == _not(pid_t))
390 socket = -1;
391 else {
392 socket = _syscall(::socket(PF_UNIX, SOCK_STREAM, 0));
393
394 struct sockaddr_un address;
395 memset(&address, 0, sizeof(address));
396 address.sun_family = AF_UNIX;
397 sprintf(address.sun_path, "/tmp/.s.cy.%u", pid);
398
399 _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
400 }
579ed526 401
48e3be8a 402 if (script == NULL && tty)
967067aa 403 Console(socket);
b09da87b 404 else {
48e3be8a 405 CYDriver driver(script ?: "<stdin>");
b09da87b 406 cy::parser parser(driver);
b10bd496 407 Setup(driver, parser);
b09da87b 408
48e3be8a 409 char *start, *end;
b09da87b 410
48e3be8a
JF
411 if (script == NULL) {
412 start = NULL;
413 end = NULL;
2b1245e4 414
48e3be8a
JF
415 driver.file_ = stdin;
416 } else {
417 size_t size;
418 start = reinterpret_cast<char *>(Map(script, &size));
419 end = start + size;
420
421 if (size >= 2 && start[0] == '#' && start[1] == '!') {
422 start += 2;
057f943f 423
48e3be8a
JF
424 if (void *line = memchr(start, '\n', end - start))
425 start = reinterpret_cast<char *>(line);
426 else
427 start = end;
428 }
429
430 driver.data_ = start;
431 driver.size_ = end - start;
432 }
62ca2b82 433
b09da87b
JF
434 if (parser.parse() != 0 || !driver.errors_.empty()) {
435 for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i)
436 std::cerr << i->location_.begin << ": " << i->message_ << std::endl;
b10bd496 437 } else if (driver.program_ != NULL)
fcc64bb5
JF
438 if (socket != -1)
439 Run(socket, start, end - start, stdout);
440 else {
441 std::ostringstream str;
652ec1ba 442 CYOutput out(str);
fb98ac0c 443 driver.program_->Multiple(out);
fcc64bb5 444 std::string code(str.str());
75b0a457
JF
445 if (compile)
446 std::cout << code;
447 else
448 Run(socket, code);
fcc64bb5 449 }
057f943f 450 }
62ca2b82 451
057f943f 452 return 0;
62ca2b82 453}