]>
Commit | Line | Data |
---|---|---|
e91fbe93 | 1 | /* Cycript - Inlining/Optimizing JavaScript Compiler |
b4aa79af JF |
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 | ||
30ddc20c | 40 | #include "cycript.hpp" |
057f943f | 41 | |
9cad30fa JF |
42 | #ifdef CY_EXECUTE |
43 | #include "JavaScript.hpp" | |
44 | #endif | |
45 | ||
057f943f JF |
46 | #include <cstdio> |
47 | #include <sstream> | |
48 | ||
49 | #include <setjmp.h> | |
50 | ||
aff87425 DWT |
51 | #ifdef HAVE_READLINE_H |
52 | #include <readline.h> | |
53 | #else | |
057f943f | 54 | #include <readline/readline.h> |
aff87425 DWT |
55 | #endif |
56 | ||
57 | #ifdef HAVE_HISTORY_H | |
58 | #include <history.h> | |
59 | #else | |
057f943f | 60 | #include <readline/history.h> |
aff87425 | 61 | #endif |
057f943f | 62 | |
b09da87b JF |
63 | #include <sys/mman.h> |
64 | ||
65 | #include <errno.h> | |
66 | #include <unistd.h> | |
67 | ||
68 | #include <sys/types.h> | |
69 | #include <sys/stat.h> | |
70 | #include <fcntl.h> | |
71 | ||
057f943f JF |
72 | #include "Cycript.tab.hh" |
73 | ||
967067aa JF |
74 | #include <sys/types.h> |
75 | #include <sys/socket.h> | |
76 | #include <netinet/in.h> | |
77 | #include <sys/un.h> | |
b1589845 | 78 | #include <pwd.h> |
967067aa | 79 | |
06edab7d JF |
80 | #include <apr_getopt.h> |
81 | ||
da858962 JF |
82 | #include <dlfcn.h> |
83 | ||
4e39dc0b JF |
84 | static volatile enum { |
85 | Working, | |
86 | Parsing, | |
87 | Running, | |
88 | Sending, | |
89 | Waiting, | |
90 | } mode_; | |
91 | ||
057f943f JF |
92 | static jmp_buf ctrlc_; |
93 | ||
579ed526 | 94 | static void sigint(int) { |
4e39dc0b JF |
95 | switch (mode_) { |
96 | case Working: | |
97 | return; | |
98 | case Parsing: | |
99 | longjmp(ctrlc_, 1); | |
100 | case Running: | |
101 | throw "*** Ctrl-C"; | |
102 | case Sending: | |
103 | return; | |
104 | case Waiting: | |
105 | return; | |
106 | } | |
057f943f JF |
107 | } |
108 | ||
cac61857 JF |
109 | #if YYDEBUG |
110 | static bool bison_; | |
111 | #endif | |
b10bd496 | 112 | static bool strict_; |
11c1cc16 | 113 | static bool pretty_; |
cac61857 | 114 | |
b10bd496 | 115 | void Setup(CYDriver &driver, cy::parser &parser) { |
cac61857 JF |
116 | #if YYDEBUG |
117 | if (bison_) | |
118 | parser.set_debug_level(1); | |
119 | #endif | |
b10bd496 JF |
120 | if (strict_) |
121 | driver.strict_ = true; | |
cac61857 JF |
122 | } |
123 | ||
de9fc71b | 124 | void Setup(CYOutput &out, CYDriver &driver, CYOptions &options) { |
11c1cc16 | 125 | out.pretty_ = pretty_; |
029bc65b | 126 | CYContext context(driver.pool_, options); |
3b52fd1a | 127 | driver.program_->Replace(context); |
11c1cc16 JF |
128 | } |
129 | ||
b166b11b | 130 | void Run(int client, const char *data, size_t size, FILE *fout = NULL, bool expand = false) { |
967067aa | 131 | CYPool pool; |
b09da87b | 132 | |
967067aa | 133 | const char *json; |
b166b11b | 134 | if (client == -1) { |
4e39dc0b | 135 | mode_ = Running; |
2aa77fd8 | 136 | #ifdef CY_EXECUTE |
fcc64bb5 | 137 | json = CYExecute(pool, data); |
2aa77fd8 JF |
138 | #else |
139 | json = NULL; | |
140 | #endif | |
4e39dc0b | 141 | mode_ = Working; |
6ec85c5b JF |
142 | if (json != NULL) |
143 | size = strlen(json); | |
144 | } else { | |
4e39dc0b | 145 | mode_ = Sending; |
b166b11b JF |
146 | CYSendAll(client, &size, sizeof(size)); |
147 | CYSendAll(client, data, size); | |
4e39dc0b | 148 | mode_ = Waiting; |
b166b11b | 149 | CYRecvAll(client, &size, sizeof(size)); |
967067aa JF |
150 | if (size == _not(size_t)) |
151 | json = NULL; | |
152 | else { | |
153 | char *temp(new(pool) char[size + 1]); | |
b166b11b | 154 | CYRecvAll(client, temp, size); |
967067aa JF |
155 | temp[size] = '\0'; |
156 | json = temp; | |
157 | } | |
4e39dc0b | 158 | mode_ = Working; |
4cf49641 | 159 | } |
b09da87b | 160 | |
967067aa | 161 | if (json != NULL && fout != NULL) { |
6ec85c5b JF |
162 | if (!expand || json[0] != '"' && json[0] != '\'') |
163 | fputs(json, fout); | |
164 | else for (size_t i(0); i != size; ++i) | |
165 | if (json[i] != '\\') | |
166 | fputc(json[i], fout); | |
167 | else switch(json[++i]) { | |
168 | case '\0': goto done; | |
169 | case '\\': fputc('\\', fout); break; | |
170 | case '\'': fputc('\'', fout); break; | |
171 | case '"': fputc('"', fout); break; | |
172 | case 'b': fputc('\b', fout); break; | |
173 | case 'f': fputc('\f', fout); break; | |
174 | case 'n': fputc('\n', fout); break; | |
175 | case 'r': fputc('\r', fout); break; | |
176 | case 't': fputc('\t', fout); break; | |
177 | case 'v': fputc('\v', fout); break; | |
178 | default: fputc('\\', fout); --i; break; | |
179 | } | |
180 | ||
181 | done: | |
967067aa JF |
182 | fputs("\n", fout); |
183 | fflush(fout); | |
b09da87b JF |
184 | } |
185 | } | |
186 | ||
b166b11b JF |
187 | void Run(int client, std::string &code, FILE *fout = NULL, bool expand = false) { |
188 | Run(client, code.c_str(), code.size(), fout, expand); | |
fcc64bb5 JF |
189 | } |
190 | ||
da858962 JF |
191 | int (*append_history$)(int, const char *); |
192 | ||
de9fc71b | 193 | static void Console(apr_pool_t *pool, int client, CYOptions &options) { |
b1589845 JF |
194 | passwd *passwd; |
195 | if (const char *username = getenv("LOGNAME")) | |
196 | passwd = getpwnam(username); | |
197 | else | |
198 | passwd = getpwuid(getuid()); | |
199 | ||
200 | const char *basedir(apr_psprintf(pool, "%s/.cycript", passwd->pw_dir)); | |
201 | const char *histfile(apr_psprintf(pool, "%s/history", basedir)); | |
202 | size_t histlines(0); | |
203 | ||
204 | mkdir(basedir, 0700); | |
205 | read_history(histfile); | |
206 | ||
1f8eae40 JF |
207 | bool bypass(false); |
208 | bool debug(false); | |
6ec85c5b | 209 | bool expand(false); |
1f8eae40 | 210 | |
057f943f JF |
211 | FILE *fout(stdout); |
212 | ||
213 | rl_bind_key('\t', rl_insert); | |
214 | ||
215 | struct sigaction action; | |
216 | sigemptyset(&action.sa_mask); | |
217 | action.sa_handler = &sigint; | |
218 | action.sa_flags = 0; | |
219 | sigaction(SIGINT, &action, NULL); | |
220 | ||
221 | restart: for (;;) { | |
222 | std::string command; | |
223 | std::vector<std::string> lines; | |
224 | ||
931b816a JF |
225 | bool extra(false); |
226 | const char *prompt("cy# "); | |
227 | ||
057f943f | 228 | if (setjmp(ctrlc_) != 0) { |
4e39dc0b | 229 | mode_ = Working; |
057f943f JF |
230 | fputs("\n", fout); |
231 | fflush(fout); | |
232 | goto restart; | |
233 | } | |
234 | ||
057f943f | 235 | read: |
4e39dc0b | 236 | mode_ = Parsing; |
057f943f | 237 | char *line(readline(prompt)); |
4e39dc0b | 238 | mode_ = Working; |
057f943f JF |
239 | if (line == NULL) |
240 | break; | |
67f1fc6b JF |
241 | if (line[0] == '\0') |
242 | goto read; | |
931b816a JF |
243 | |
244 | if (!extra) { | |
245 | extra = true; | |
6ec85c5b | 246 | if (line[0] == '?') { |
1f8eae40 JF |
247 | std::string data(line + 1); |
248 | if (data == "bypass") { | |
249 | bypass = !bypass; | |
250 | fprintf(fout, "bypass == %s\n", bypass ? "true" : "false"); | |
251 | fflush(fout); | |
252 | } else if (data == "debug") { | |
253 | debug = !debug; | |
254 | fprintf(fout, "debug == %s\n", debug ? "true" : "false"); | |
255 | fflush(fout); | |
6ec85c5b JF |
256 | } else if (data == "expand") { |
257 | expand = !expand; | |
258 | fprintf(fout, "expand == %s\n", expand ? "true" : "false"); | |
259 | fflush(fout); | |
1f8eae40 | 260 | } |
d35a3b07 | 261 | add_history(line); |
b1589845 | 262 | ++histlines; |
931b816a JF |
263 | goto restart; |
264 | } | |
265 | } | |
266 | ||
057f943f | 267 | command += line; |
e818f0e0 JF |
268 | |
269 | char *begin(line), *end(line + strlen(line)); | |
270 | while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) { | |
271 | *nl = '\0'; | |
272 | lines.push_back(begin); | |
273 | begin = nl + 1; | |
274 | } | |
275 | ||
276 | lines.push_back(begin); | |
277 | ||
057f943f JF |
278 | free(line); |
279 | ||
1f8eae40 JF |
280 | std::string code; |
281 | ||
282 | if (bypass) | |
283 | code = command; | |
284 | else { | |
285 | CYDriver driver(""); | |
286 | cy::parser parser(driver); | |
b10bd496 | 287 | Setup(driver, parser); |
1f8eae40 JF |
288 | |
289 | driver.data_ = command.c_str(); | |
290 | driver.size_ = command.size(); | |
291 | ||
292 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
f0360d51 JF |
293 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { |
294 | cy::position begin(error->location_.begin); | |
b10bd496 | 295 | if (begin.line != lines.size() || begin.column - 1 != lines.back().size() || error->warning_) { |
f0360d51 | 296 | cy::position end(error->location_.end); |
48e3be8a | 297 | |
f0360d51 JF |
298 | if (begin.line != lines.size()) { |
299 | std::cerr << " | "; | |
300 | std::cerr << lines[begin.line - 1] << std::endl; | |
301 | } | |
48e3be8a | 302 | |
c52a09b8 | 303 | std::cerr << "...."; |
f0360d51 JF |
304 | for (size_t i(0); i != begin.column - 1; ++i) |
305 | std::cerr << '.'; | |
48e3be8a | 306 | if (begin.line != end.line || begin.column == end.column) |
f0360d51 JF |
307 | std::cerr << '^'; |
308 | else for (size_t i(0), e(end.column - begin.column); i != e; ++i) | |
309 | std::cerr << '^'; | |
310 | std::cerr << std::endl; | |
48e3be8a | 311 | |
f0360d51 JF |
312 | std::cerr << " | "; |
313 | std::cerr << error->message_ << std::endl; | |
48e3be8a | 314 | |
1f8eae40 | 315 | add_history(command.c_str()); |
b1589845 | 316 | ++histlines; |
1f8eae40 JF |
317 | goto restart; |
318 | } | |
319 | } | |
057f943f | 320 | |
1f8eae40 | 321 | driver.errors_.clear(); |
057f943f | 322 | |
1f8eae40 JF |
323 | command += '\n'; |
324 | prompt = "cy> "; | |
325 | goto read; | |
057f943f JF |
326 | } |
327 | ||
b10bd496 | 328 | if (driver.program_ == NULL) |
1f8eae40 | 329 | goto restart; |
057f943f | 330 | |
b166b11b | 331 | if (client != -1) |
967067aa JF |
332 | code = command; |
333 | else { | |
334 | std::ostringstream str; | |
029bc65b | 335 | CYOutput out(str, options); |
de9fc71b | 336 | Setup(out, driver, options); |
3b52fd1a | 337 | out << *driver.program_; |
967067aa JF |
338 | code = str.str(); |
339 | } | |
057f943f JF |
340 | } |
341 | ||
057f943f | 342 | add_history(command.c_str()); |
b1589845 | 343 | ++histlines; |
057f943f | 344 | |
1f8eae40 JF |
345 | if (debug) |
346 | std::cout << code << std::endl; | |
057f943f | 347 | |
b166b11b | 348 | Run(client, code, fout, expand); |
b09da87b | 349 | } |
057f943f | 350 | |
da858962 JF |
351 | if (append_history$ != NULL) { |
352 | _syscall(close(_syscall(open(histfile, O_CREAT | O_WRONLY, 0600)))); | |
353 | (*append_history$)(histlines, histfile); | |
354 | } else { | |
355 | write_history(histfile); | |
356 | } | |
b1589845 | 357 | |
b09da87b JF |
358 | fputs("\n", fout); |
359 | fflush(fout); | |
360 | } | |
057f943f | 361 | |
579ed526 | 362 | static void *Map(const char *path, size_t *psize) { |
b09da87b JF |
363 | int fd; |
364 | _syscall(fd = open(path, O_RDONLY)); | |
057f943f | 365 | |
b09da87b JF |
366 | struct stat stat; |
367 | _syscall(fstat(fd, &stat)); | |
368 | size_t size(stat.st_size); | |
057f943f | 369 | |
b09da87b | 370 | *psize = size; |
057f943f | 371 | |
b09da87b JF |
372 | void *base; |
373 | _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); | |
057f943f | 374 | |
b09da87b JF |
375 | _syscall(close(fd)); |
376 | return base; | |
377 | } | |
057f943f | 378 | |
b6961e53 | 379 | void InjectLibrary(pid_t pid); |
06edab7d | 380 | |
b6961e53 | 381 | int Main(int argc, char const * const argv[], char const * const envp[]) { |
48e3be8a | 382 | bool tty(isatty(STDIN_FILENO)); |
75b0a457 | 383 | bool compile(false); |
de9fc71b | 384 | CYOptions options; |
967067aa | 385 | |
da858962 JF |
386 | append_history$ = reinterpret_cast<int (*)(int, const char *)>(dlsym(RTLD_DEFAULT, "append_history")); |
387 | ||
2aa77fd8 JF |
388 | #ifdef CY_ATTACH |
389 | pid_t pid(_not(pid_t)); | |
390 | #endif | |
391 | ||
06edab7d JF |
392 | CYPool pool; |
393 | apr_getopt_t *state; | |
394 | _aprcall(apr_getopt_init(&state, pool, argc, argv)); | |
395 | ||
396 | for (;;) { | |
397 | char opt; | |
398 | const char *arg; | |
399 | ||
400 | apr_status_t status(apr_getopt(state, | |
401 | "cg:n:" | |
2aa77fd8 | 402 | #ifdef CY_ATTACH |
06edab7d | 403 | "p:" |
2aa77fd8 | 404 | #endif |
06edab7d JF |
405 | "s" |
406 | , &opt, &arg)); | |
407 | ||
408 | switch (status) { | |
409 | case APR_EOF: | |
410 | goto getopt; | |
411 | case APR_BADCH: | |
412 | case APR_BADARG: | |
413 | fprintf(stderr, | |
414 | "usage: cycript [-c]" | |
2aa77fd8 | 415 | #ifdef CY_ATTACH |
69caa5be | 416 | " [-p <pid|name>]" |
2aa77fd8 | 417 | #endif |
06edab7d JF |
418 | " [<script> [<arg>...]]\n" |
419 | ); | |
420 | return 1; | |
421 | default: | |
422 | _aprcall(status); | |
423 | } | |
967067aa | 424 | |
06edab7d JF |
425 | switch (opt) { |
426 | case 'c': | |
427 | compile = true; | |
428 | break; | |
75b0a457 | 429 | |
06edab7d JF |
430 | case 'g': |
431 | if (false); | |
de9fc71b JF |
432 | else if (strcmp(arg, "rename") == 0) |
433 | options.verbose_ = true; | |
cac61857 | 434 | #if YYDEBUG |
5ef46cc0 | 435 | else if (strcmp(arg, "bison") == 0) |
06edab7d | 436 | bison_ = true; |
cac61857 | 437 | #endif |
06edab7d JF |
438 | else { |
439 | fprintf(stderr, "invalid name for -g\n"); | |
440 | return 1; | |
441 | } | |
442 | break; | |
cac61857 | 443 | |
06edab7d JF |
444 | case 'n': |
445 | if (false); | |
5ef46cc0 | 446 | else if (strcmp(arg, "minify") == 0) |
06edab7d JF |
447 | pretty_ = true; |
448 | else { | |
449 | fprintf(stderr, "invalid name for -n\n"); | |
450 | return 1; | |
451 | } | |
452 | break; | |
11c1cc16 | 453 | |
2aa77fd8 | 454 | #ifdef CY_ATTACH |
06edab7d | 455 | case 'p': { |
5ef46cc0 | 456 | size_t size(strlen(arg)); |
06edab7d | 457 | char *end; |
69caa5be | 458 | |
5ef46cc0 JF |
459 | pid = strtoul(arg, &end, 0); |
460 | if (arg + size != end) { | |
69caa5be JF |
461 | // XXX: arg needs to be escaped in some horrendous way of doom |
462 | const char *command(apr_psprintf(pool, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", arg)); | |
463 | ||
464 | if (FILE *pids = popen(command, "r")) { | |
465 | char value[32]; | |
466 | size = 0; | |
467 | ||
468 | for (;;) { | |
469 | size_t read(fread(value + size, 1, sizeof(value) - size, pids)); | |
470 | if (read == 0) | |
471 | break; | |
472 | else { | |
473 | size += read; | |
474 | if (size == sizeof(value)) { | |
475 | pid = _not(pid_t); | |
b166b11b | 476 | goto fail; |
69caa5be JF |
477 | } |
478 | } | |
479 | } | |
480 | ||
481 | size: | |
482 | if (size == 0) | |
b166b11b | 483 | goto fail; |
69caa5be JF |
484 | if (value[size - 1] == '\n') { |
485 | --size; | |
486 | goto size; | |
487 | } | |
488 | ||
489 | value[size] = '\0'; | |
490 | size = strlen(value); | |
491 | pid = strtoul(value, &end, 0); | |
b166b11b | 492 | if (value + size != end) fail: |
69caa5be | 493 | pid = _not(pid_t); |
69caa5be JF |
494 | _syscall(pclose(pids)); |
495 | } | |
496 | ||
497 | if (pid == _not(pid_t)) { | |
498 | fprintf(stderr, "invalid pid for -p\n"); | |
499 | return 1; | |
500 | } | |
06edab7d JF |
501 | } |
502 | } break; | |
2aa77fd8 | 503 | #endif |
b10bd496 | 504 | |
06edab7d JF |
505 | case 's': |
506 | strict_ = true; | |
507 | break; | |
508 | } | |
967067aa JF |
509 | } getopt:; |
510 | ||
b09da87b | 511 | const char *script; |
5ef46cc0 | 512 | int ind(state->ind); |
b09da87b | 513 | |
2aa77fd8 | 514 | #ifdef CY_ATTACH |
5ef46cc0 | 515 | if (pid != _not(pid_t) && ind < argc - 1) { |
fcc64bb5 JF |
516 | fprintf(stderr, "-p cannot set argv\n"); |
517 | return 1; | |
518 | } | |
519 | ||
75b0a457 JF |
520 | if (pid != _not(pid_t) && compile) { |
521 | fprintf(stderr, "-p conflicts with -c\n"); | |
522 | return 1; | |
523 | } | |
2aa77fd8 | 524 | #endif |
75b0a457 | 525 | |
5ef46cc0 | 526 | if (ind == argc) |
b09da87b JF |
527 | script = NULL; |
528 | else { | |
9185d5ef | 529 | #ifdef CY_EXECUTE |
967067aa | 530 | // XXX: const_cast?! wtf gcc :( |
5ef46cc0 | 531 | CYSetArgs(argc - ind - 1, const_cast<const char **>(argv + ind + 1)); |
9185d5ef | 532 | #endif |
5ef46cc0 | 533 | script = argv[ind]; |
967067aa JF |
534 | if (strcmp(script, "-") == 0) |
535 | script = NULL; | |
b09da87b JF |
536 | } |
537 | ||
2aa77fd8 JF |
538 | #ifdef CY_ATTACH |
539 | if (pid != _not(pid_t) && script == NULL && !tty) { | |
540 | fprintf(stderr, "non-terminal attaching to remote console\n"); | |
48e3be8a JF |
541 | return 1; |
542 | } | |
2aa77fd8 | 543 | #endif |
48e3be8a | 544 | |
b166b11b | 545 | int client; |
967067aa | 546 | |
2aa77fd8 | 547 | #ifdef CY_ATTACH |
967067aa | 548 | if (pid == _not(pid_t)) |
b166b11b | 549 | client = -1; |
967067aa | 550 | else { |
b166b11b JF |
551 | int server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0))); try { |
552 | struct sockaddr_un address; | |
553 | memset(&address, 0, sizeof(address)); | |
554 | address.sun_family = AF_UNIX; | |
555 | ||
556 | sprintf(address.sun_path, "/tmp/.s.cy.%u", getpid()); | |
557 | ||
558 | _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
559 | _syscall(chmod(address.sun_path, 0777)); | |
560 | ||
561 | try { | |
562 | _syscall(listen(server, 1)); | |
563 | InjectLibrary(pid); | |
564 | client = _syscall(accept(server, NULL, NULL)); | |
565 | } catch (...) { | |
566 | // XXX: exception? | |
567 | unlink(address.sun_path); | |
568 | throw; | |
569 | } | |
570 | } catch (...) { | |
571 | _syscall(close(server)); | |
95678376 | 572 | throw; |
b166b11b | 573 | } |
967067aa | 574 | } |
2aa77fd8 | 575 | #else |
b166b11b | 576 | client = -1; |
2aa77fd8 | 577 | #endif |
579ed526 | 578 | |
48e3be8a | 579 | if (script == NULL && tty) |
de9fc71b | 580 | Console(pool, client, options); |
b09da87b | 581 | else { |
48e3be8a | 582 | CYDriver driver(script ?: "<stdin>"); |
b09da87b | 583 | cy::parser parser(driver); |
b10bd496 | 584 | Setup(driver, parser); |
b09da87b | 585 | |
48e3be8a | 586 | char *start, *end; |
b09da87b | 587 | |
48e3be8a JF |
588 | if (script == NULL) { |
589 | start = NULL; | |
590 | end = NULL; | |
2b1245e4 | 591 | |
48e3be8a JF |
592 | driver.file_ = stdin; |
593 | } else { | |
594 | size_t size; | |
595 | start = reinterpret_cast<char *>(Map(script, &size)); | |
596 | end = start + size; | |
597 | ||
598 | if (size >= 2 && start[0] == '#' && start[1] == '!') { | |
599 | start += 2; | |
057f943f | 600 | |
48e3be8a JF |
601 | if (void *line = memchr(start, '\n', end - start)) |
602 | start = reinterpret_cast<char *>(line); | |
603 | else | |
604 | start = end; | |
605 | } | |
606 | ||
607 | driver.data_ = start; | |
608 | driver.size_ = end - start; | |
609 | } | |
62ca2b82 | 610 | |
b09da87b JF |
611 | if (parser.parse() != 0 || !driver.errors_.empty()) { |
612 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) | |
613 | std::cerr << i->location_.begin << ": " << i->message_ << std::endl; | |
b10bd496 | 614 | } else if (driver.program_ != NULL) |
e934f827 JF |
615 | if (client != -1) { |
616 | std::string code(start, end-start); | |
34fc459c | 617 | Run(client, code, stdout); |
e934f827 | 618 | } else { |
fcc64bb5 | 619 | std::ostringstream str; |
029bc65b | 620 | CYOutput out(str, options); |
de9fc71b | 621 | Setup(out, driver, options); |
3b52fd1a | 622 | out << *driver.program_; |
fcc64bb5 | 623 | std::string code(str.str()); |
75b0a457 JF |
624 | if (compile) |
625 | std::cout << code; | |
98ea05a3 | 626 | else |
b166b11b | 627 | Run(client, code, stdout); |
fcc64bb5 | 628 | } |
057f943f | 629 | } |
62ca2b82 | 630 | |
057f943f | 631 | return 0; |
62ca2b82 | 632 | } |
b6961e53 JF |
633 | |
634 | int main(int argc, char const * const argv[], char const * const envp[]) { | |
635 | apr_status_t status(apr_app_initialize(&argc, &argv, &envp)); | |
da858962 | 636 | |
b6961e53 JF |
637 | if (status != APR_SUCCESS) { |
638 | fprintf(stderr, "apr_app_initialize() != APR_SUCCESS\n"); | |
639 | return 1; | |
640 | } else try { | |
641 | return Main(argc, argv, envp); | |
642 | } catch (const CYException &error) { | |
643 | CYPool pool; | |
644 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
645 | return 1; | |
646 | } | |
647 | } |