]>
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 | ||
7e5391fd JF |
84 | #include "Replace.hpp" |
85 | ||
4e39dc0b JF |
86 | static volatile enum { |
87 | Working, | |
88 | Parsing, | |
89 | Running, | |
90 | Sending, | |
91 | Waiting, | |
92 | } mode_; | |
93 | ||
057f943f JF |
94 | static jmp_buf ctrlc_; |
95 | ||
579ed526 | 96 | static void sigint(int) { |
4e39dc0b JF |
97 | switch (mode_) { |
98 | case Working: | |
99 | return; | |
100 | case Parsing: | |
101 | longjmp(ctrlc_, 1); | |
102 | case Running: | |
103 | throw "*** Ctrl-C"; | |
104 | case Sending: | |
105 | return; | |
106 | case Waiting: | |
107 | return; | |
108 | } | |
057f943f JF |
109 | } |
110 | ||
cac61857 JF |
111 | #if YYDEBUG |
112 | static bool bison_; | |
113 | #endif | |
b10bd496 | 114 | static bool strict_; |
11c1cc16 | 115 | static bool pretty_; |
cac61857 | 116 | |
b10bd496 | 117 | void Setup(CYDriver &driver, cy::parser &parser) { |
cac61857 JF |
118 | #if YYDEBUG |
119 | if (bison_) | |
120 | parser.set_debug_level(1); | |
121 | #endif | |
b10bd496 JF |
122 | if (strict_) |
123 | driver.strict_ = true; | |
cac61857 JF |
124 | } |
125 | ||
de9fc71b | 126 | void Setup(CYOutput &out, CYDriver &driver, CYOptions &options) { |
11c1cc16 | 127 | out.pretty_ = pretty_; |
029bc65b | 128 | CYContext context(driver.pool_, options); |
3b52fd1a | 129 | driver.program_->Replace(context); |
11c1cc16 JF |
130 | } |
131 | ||
7e5391fd | 132 | static CYUTF8String Run(CYPool &pool, int client, CYUTF8String code) { |
967067aa | 133 | const char *json; |
7e5391fd JF |
134 | size_t size; |
135 | ||
b166b11b | 136 | if (client == -1) { |
4e39dc0b | 137 | mode_ = Running; |
2aa77fd8 | 138 | #ifdef CY_EXECUTE |
0ced2e47 | 139 | json = CYExecute(pool, code); |
2aa77fd8 JF |
140 | #else |
141 | json = NULL; | |
142 | #endif | |
4e39dc0b | 143 | mode_ = Working; |
6ec85c5b JF |
144 | if (json != NULL) |
145 | size = strlen(json); | |
146 | } else { | |
4e39dc0b | 147 | mode_ = Sending; |
0ced2e47 | 148 | size = code.size; |
b166b11b | 149 | CYSendAll(client, &size, sizeof(size)); |
0ced2e47 | 150 | CYSendAll(client, code.data, code.size); |
4e39dc0b | 151 | mode_ = Waiting; |
b166b11b | 152 | CYRecvAll(client, &size, sizeof(size)); |
967067aa JF |
153 | if (size == _not(size_t)) |
154 | json = NULL; | |
155 | else { | |
156 | char *temp(new(pool) char[size + 1]); | |
b166b11b | 157 | CYRecvAll(client, temp, size); |
967067aa JF |
158 | temp[size] = '\0'; |
159 | json = temp; | |
160 | } | |
4e39dc0b | 161 | mode_ = Working; |
4cf49641 | 162 | } |
b09da87b | 163 | |
7e5391fd JF |
164 | return CYUTF8String(json, size); |
165 | } | |
166 | ||
167 | static CYUTF8String Run(CYPool &pool, int client, const std::string &code) { | |
168 | return Run(pool, client, CYUTF8String(code.c_str(), code.size())); | |
169 | } | |
170 | ||
a3f4a8e1 JF |
171 | FILE *fout_; |
172 | ||
173 | static void Output(CYUTF8String json, FILE *fout, bool expand = false) { | |
174 | const char *data(json.data); | |
175 | size_t size(json.size); | |
176 | ||
177 | if (data == NULL || fout == NULL) | |
178 | return; | |
179 | ||
180 | if (!expand || data[0] != '"' && data[0] != '\'') | |
181 | fputs(data, fout); | |
182 | else for (size_t i(0); i != size; ++i) | |
183 | if (data[i] != '\\') | |
184 | fputc(data[i], fout); | |
185 | else switch(data[++i]) { | |
186 | case '\0': goto done; | |
187 | case '\\': fputc('\\', fout); break; | |
188 | case '\'': fputc('\'', fout); break; | |
189 | case '"': fputc('"', fout); break; | |
190 | case 'b': fputc('\b', fout); break; | |
191 | case 'f': fputc('\f', fout); break; | |
192 | case 'n': fputc('\n', fout); break; | |
193 | case 'r': fputc('\r', fout); break; | |
194 | case 't': fputc('\t', fout); break; | |
195 | case 'v': fputc('\v', fout); break; | |
196 | default: fputc('\\', fout); --i; break; | |
197 | } | |
6ec85c5b | 198 | |
a3f4a8e1 JF |
199 | done: |
200 | fputs("\n", fout); | |
201 | fflush(fout); | |
202 | } | |
203 | ||
204 | static void Run(int client, const char *data, size_t size, FILE *fout = NULL, bool expand = false) { | |
205 | CYPool pool; | |
206 | Output(Run(pool, client, CYUTF8String(data, size)), fout, expand); | |
b09da87b JF |
207 | } |
208 | ||
7e5391fd | 209 | static void Run(int client, std::string &code, FILE *fout = NULL, bool expand = false) { |
b166b11b | 210 | Run(client, code.c_str(), code.size(), fout, expand); |
fcc64bb5 JF |
211 | } |
212 | ||
da858962 JF |
213 | int (*append_history$)(int, const char *); |
214 | ||
7e5391fd JF |
215 | static std::string command_; |
216 | ||
217 | static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) { | |
218 | std::ostringstream str; | |
219 | str << '(' << code << ')'; | |
220 | std::string string(str.str()); | |
221 | ||
222 | CYDriver driver(pool); | |
223 | driver.data_ = string.c_str(); | |
224 | driver.size_ = string.size(); | |
225 | ||
226 | cy::parser parser(driver); | |
227 | Setup(driver, parser); | |
228 | ||
229 | if (parser.parse() != 0 || !driver.errors_.empty()) | |
230 | _assert(false); | |
231 | ||
232 | CYExpress *express(dynamic_cast<CYExpress *>(driver.program_->statements_)); | |
233 | _assert(express != NULL); | |
234 | return express->expression_; | |
235 | } | |
236 | ||
237 | static int client_; | |
238 | ||
239 | static char **Complete(const char *word, int start, int end) { | |
240 | rl_attempted_completion_over = TRUE; | |
241 | ||
242 | CYPool pool; | |
243 | ||
244 | CYDriver driver(pool); | |
245 | cy::parser parser(driver); | |
246 | Setup(driver, parser); | |
247 | ||
248 | std::string line(rl_line_buffer, start); | |
249 | std::string command(command_ + line); | |
250 | ||
251 | driver.data_ = command.c_str(); | |
252 | driver.size_ = command.size(); | |
253 | ||
254 | driver.auto_ = true; | |
255 | ||
256 | if (parser.parse() != 0 || !driver.errors_.empty()) | |
257 | return NULL; | |
258 | ||
259 | if (driver.mode_ == CYDriver::AutoNone) | |
260 | return NULL; | |
261 | ||
262 | CYExpression *expression; | |
263 | ||
264 | CYOptions options; | |
265 | CYContext context(driver.pool_, options); | |
266 | ||
267 | std::ostringstream prefix; | |
268 | ||
269 | switch (driver.mode_) { | |
270 | case CYDriver::AutoPrimary: | |
271 | expression = $ CYThis(); | |
272 | break; | |
273 | ||
274 | case CYDriver::AutoDirect: | |
275 | expression = driver.context_; | |
276 | break; | |
277 | ||
278 | case CYDriver::AutoIndirect: | |
279 | expression = $ CYIndirect(driver.context_); | |
280 | break; | |
281 | ||
282 | case CYDriver::AutoMessage: { | |
283 | CYDriver::Context &thing(driver.contexts_.back()); | |
284 | expression = $M($M($ CYIndirect(thing.context_), $S("isa")), $S("messages")); | |
285 | for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part) | |
286 | prefix << (*part)->word_ << ':'; | |
287 | } break; | |
288 | ||
289 | default: | |
290 | _assert(false); | |
291 | } | |
292 | ||
2a18313c | 293 | std::string begin(prefix.str()); |
7e5391fd | 294 | |
2a18313c JF |
295 | driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression(pool, |
296 | " function(object, prefix, word) {\n" | |
7e5391fd | 297 | " var names = [];\n" |
2a18313c JF |
298 | " var pattern = '^' + prefix + word;\n" |
299 | " var length = prefix.length;\n" | |
7e5391fd JF |
300 | " for (name in object)\n" |
301 | " if (name.match(pattern) != null)\n" | |
2a18313c | 302 | " names.push(name.substr(length));\n" |
7e5391fd JF |
303 | " return names;\n" |
304 | " }\n" | |
2a18313c | 305 | ), expression, $S(begin.c_str()), $S(word)))); |
7e5391fd JF |
306 | |
307 | driver.program_->Replace(context); | |
308 | ||
309 | std::ostringstream str; | |
310 | CYOutput out(str, options); | |
311 | out << *driver.program_; | |
312 | ||
313 | std::string code(str.str()); | |
314 | CYUTF8String json(Run(pool, client_, code)); | |
315 | ||
316 | CYExpression *result(ParseExpression(pool, json)); | |
317 | CYArray *array(dynamic_cast<CYArray *>(result)); | |
a3f4a8e1 JF |
318 | |
319 | if (array == NULL) { | |
320 | fprintf(fout_, "\n"); | |
321 | Output(json, fout_); | |
322 | rl_forced_update_display(); | |
323 | return NULL; | |
324 | } | |
7e5391fd JF |
325 | |
326 | // XXX: use an std::set? | |
327 | typedef std::vector<std::string> Completions; | |
328 | Completions completions; | |
329 | ||
330 | std::string common; | |
331 | bool rest(false); | |
332 | ||
333 | for (CYElement *element(array->elements_); element != NULL; element = element->next_) { | |
334 | CYString *string(dynamic_cast<CYString *>(element->value_)); | |
335 | _assert(string != NULL); | |
336 | ||
337 | std::string completion(string->value_, string->size_); | |
338 | completions.push_back(completion); | |
339 | ||
340 | if (!rest) { | |
341 | common = completion; | |
342 | rest = true; | |
343 | } else { | |
344 | size_t limit(completion.size()), size(common.size()); | |
345 | if (size > limit) | |
346 | common = common.substr(0, limit); | |
347 | else | |
348 | limit = size; | |
349 | for (limit = 0; limit != size; ++limit) | |
350 | if (common[limit] != completion[limit]) | |
351 | break; | |
352 | if (limit != size) | |
353 | common = common.substr(0, limit); | |
354 | } | |
355 | } | |
356 | ||
357 | size_t count(completions.size()); | |
358 | if (count == 0) | |
359 | return NULL; | |
360 | ||
c41590b2 JF |
361 | size_t colon(common.find(':')); |
362 | if (colon != std::string::npos) | |
363 | common = common.substr(0, colon + 1); | |
6dccefa9 JF |
364 | if (completions.size() == 1) |
365 | common += ' '; | |
c41590b2 | 366 | |
7e5391fd JF |
367 | char **results(reinterpret_cast<char **>(malloc(sizeof(char *) * (count + 2)))); |
368 | ||
369 | results[0] = strdup(common.c_str()); | |
370 | size_t index(0); | |
371 | for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i) | |
372 | results[++index] = strdup(i->c_str()); | |
373 | results[count + 1] = NULL; | |
374 | ||
375 | return results; | |
376 | } | |
377 | ||
378 | // need char *, not const char * | |
379 | static char name_[] = "cycript"; | |
108c5fc8 | 380 | static char break_[] = " \t\n\"\\'`@$><=;|&{(" ")}" ".:[]"; |
7e5391fd JF |
381 | |
382 | static void Console(apr_pool_t *pool, CYOptions &options) { | |
b1589845 JF |
383 | passwd *passwd; |
384 | if (const char *username = getenv("LOGNAME")) | |
385 | passwd = getpwnam(username); | |
386 | else | |
387 | passwd = getpwuid(getuid()); | |
388 | ||
389 | const char *basedir(apr_psprintf(pool, "%s/.cycript", passwd->pw_dir)); | |
390 | const char *histfile(apr_psprintf(pool, "%s/history", basedir)); | |
391 | size_t histlines(0); | |
392 | ||
7e5391fd JF |
393 | rl_initialize(); |
394 | rl_readline_name = name_; | |
395 | ||
b1589845 JF |
396 | mkdir(basedir, 0700); |
397 | read_history(histfile); | |
398 | ||
1f8eae40 JF |
399 | bool bypass(false); |
400 | bool debug(false); | |
6ec85c5b | 401 | bool expand(false); |
1f8eae40 | 402 | |
a3f4a8e1 | 403 | fout_ = stdout; |
057f943f | 404 | |
7e5391fd JF |
405 | // rl_completer_word_break_characters is broken in libedit |
406 | rl_basic_word_break_characters = break_; | |
bc1e87aa JF |
407 | |
408 | rl_completer_word_break_characters = break_; | |
7e5391fd JF |
409 | rl_attempted_completion_function = &Complete; |
410 | rl_bind_key('\t', rl_complete); | |
057f943f JF |
411 | |
412 | struct sigaction action; | |
413 | sigemptyset(&action.sa_mask); | |
414 | action.sa_handler = &sigint; | |
415 | action.sa_flags = 0; | |
416 | sigaction(SIGINT, &action, NULL); | |
417 | ||
418 | restart: for (;;) { | |
7e5391fd | 419 | command_.clear(); |
057f943f JF |
420 | std::vector<std::string> lines; |
421 | ||
931b816a JF |
422 | bool extra(false); |
423 | const char *prompt("cy# "); | |
424 | ||
057f943f | 425 | if (setjmp(ctrlc_) != 0) { |
4e39dc0b | 426 | mode_ = Working; |
a3f4a8e1 JF |
427 | fputs("\n", fout_); |
428 | fflush(fout_); | |
057f943f JF |
429 | goto restart; |
430 | } | |
431 | ||
057f943f | 432 | read: |
4e39dc0b | 433 | mode_ = Parsing; |
057f943f | 434 | char *line(readline(prompt)); |
4e39dc0b | 435 | mode_ = Working; |
057f943f JF |
436 | if (line == NULL) |
437 | break; | |
67f1fc6b JF |
438 | if (line[0] == '\0') |
439 | goto read; | |
931b816a JF |
440 | |
441 | if (!extra) { | |
442 | extra = true; | |
6ec85c5b | 443 | if (line[0] == '?') { |
1f8eae40 JF |
444 | std::string data(line + 1); |
445 | if (data == "bypass") { | |
446 | bypass = !bypass; | |
a3f4a8e1 JF |
447 | fprintf(fout_, "bypass == %s\n", bypass ? "true" : "false"); |
448 | fflush(fout_); | |
1f8eae40 JF |
449 | } else if (data == "debug") { |
450 | debug = !debug; | |
a3f4a8e1 JF |
451 | fprintf(fout_, "debug == %s\n", debug ? "true" : "false"); |
452 | fflush(fout_); | |
6ec85c5b JF |
453 | } else if (data == "expand") { |
454 | expand = !expand; | |
a3f4a8e1 JF |
455 | fprintf(fout_, "expand == %s\n", expand ? "true" : "false"); |
456 | fflush(fout_); | |
1f8eae40 | 457 | } |
d35a3b07 | 458 | add_history(line); |
b1589845 | 459 | ++histlines; |
931b816a JF |
460 | goto restart; |
461 | } | |
462 | } | |
463 | ||
7e5391fd | 464 | command_ += line; |
e818f0e0 JF |
465 | |
466 | char *begin(line), *end(line + strlen(line)); | |
467 | while (char *nl = reinterpret_cast<char *>(memchr(begin, '\n', end - begin))) { | |
468 | *nl = '\0'; | |
469 | lines.push_back(begin); | |
470 | begin = nl + 1; | |
471 | } | |
472 | ||
473 | lines.push_back(begin); | |
474 | ||
057f943f JF |
475 | free(line); |
476 | ||
1f8eae40 JF |
477 | std::string code; |
478 | ||
479 | if (bypass) | |
7e5391fd | 480 | code = command_; |
1f8eae40 | 481 | else { |
7e5391fd | 482 | CYDriver driver; |
1f8eae40 | 483 | cy::parser parser(driver); |
b10bd496 | 484 | Setup(driver, parser); |
1f8eae40 | 485 | |
7e5391fd JF |
486 | driver.data_ = command_.c_str(); |
487 | driver.size_ = command_.size(); | |
1f8eae40 JF |
488 | |
489 | if (parser.parse() != 0 || !driver.errors_.empty()) { | |
f0360d51 JF |
490 | for (CYDriver::Errors::const_iterator error(driver.errors_.begin()); error != driver.errors_.end(); ++error) { |
491 | cy::position begin(error->location_.begin); | |
b10bd496 | 492 | if (begin.line != lines.size() || begin.column - 1 != lines.back().size() || error->warning_) { |
f0360d51 | 493 | cy::position end(error->location_.end); |
48e3be8a | 494 | |
f0360d51 JF |
495 | if (begin.line != lines.size()) { |
496 | std::cerr << " | "; | |
497 | std::cerr << lines[begin.line - 1] << std::endl; | |
498 | } | |
48e3be8a | 499 | |
c52a09b8 | 500 | std::cerr << "...."; |
f0360d51 JF |
501 | for (size_t i(0); i != begin.column - 1; ++i) |
502 | std::cerr << '.'; | |
48e3be8a | 503 | if (begin.line != end.line || begin.column == end.column) |
f0360d51 JF |
504 | std::cerr << '^'; |
505 | else for (size_t i(0), e(end.column - begin.column); i != e; ++i) | |
506 | std::cerr << '^'; | |
507 | std::cerr << std::endl; | |
48e3be8a | 508 | |
f0360d51 JF |
509 | std::cerr << " | "; |
510 | std::cerr << error->message_ << std::endl; | |
48e3be8a | 511 | |
7e5391fd | 512 | add_history(command_.c_str()); |
b1589845 | 513 | ++histlines; |
1f8eae40 JF |
514 | goto restart; |
515 | } | |
516 | } | |
057f943f | 517 | |
1f8eae40 | 518 | driver.errors_.clear(); |
057f943f | 519 | |
7e5391fd | 520 | command_ += '\n'; |
1f8eae40 JF |
521 | prompt = "cy> "; |
522 | goto read; | |
057f943f JF |
523 | } |
524 | ||
b10bd496 | 525 | if (driver.program_ == NULL) |
1f8eae40 | 526 | goto restart; |
057f943f | 527 | |
7e5391fd JF |
528 | if (client_ != -1) |
529 | code = command_; | |
967067aa JF |
530 | else { |
531 | std::ostringstream str; | |
029bc65b | 532 | CYOutput out(str, options); |
de9fc71b | 533 | Setup(out, driver, options); |
3b52fd1a | 534 | out << *driver.program_; |
967067aa JF |
535 | code = str.str(); |
536 | } | |
057f943f JF |
537 | } |
538 | ||
7e5391fd | 539 | add_history(command_.c_str()); |
b1589845 | 540 | ++histlines; |
057f943f | 541 | |
1f8eae40 JF |
542 | if (debug) |
543 | std::cout << code << std::endl; | |
057f943f | 544 | |
a3f4a8e1 | 545 | Run(client_, code, fout_, expand); |
b09da87b | 546 | } |
057f943f | 547 | |
da858962 JF |
548 | if (append_history$ != NULL) { |
549 | _syscall(close(_syscall(open(histfile, O_CREAT | O_WRONLY, 0600)))); | |
550 | (*append_history$)(histlines, histfile); | |
551 | } else { | |
552 | write_history(histfile); | |
553 | } | |
b1589845 | 554 | |
a3f4a8e1 JF |
555 | fputs("\n", fout_); |
556 | fflush(fout_); | |
b09da87b | 557 | } |
057f943f | 558 | |
579ed526 | 559 | static void *Map(const char *path, size_t *psize) { |
b09da87b JF |
560 | int fd; |
561 | _syscall(fd = open(path, O_RDONLY)); | |
057f943f | 562 | |
b09da87b JF |
563 | struct stat stat; |
564 | _syscall(fstat(fd, &stat)); | |
565 | size_t size(stat.st_size); | |
057f943f | 566 | |
b09da87b | 567 | *psize = size; |
057f943f | 568 | |
b09da87b JF |
569 | void *base; |
570 | _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); | |
057f943f | 571 | |
b09da87b JF |
572 | _syscall(close(fd)); |
573 | return base; | |
574 | } | |
057f943f | 575 | |
b6961e53 | 576 | void InjectLibrary(pid_t pid); |
06edab7d | 577 | |
b6961e53 | 578 | int Main(int argc, char const * const argv[], char const * const envp[]) { |
48e3be8a | 579 | bool tty(isatty(STDIN_FILENO)); |
75b0a457 | 580 | bool compile(false); |
de9fc71b | 581 | CYOptions options; |
967067aa | 582 | |
da858962 JF |
583 | append_history$ = reinterpret_cast<int (*)(int, const char *)>(dlsym(RTLD_DEFAULT, "append_history")); |
584 | ||
2aa77fd8 JF |
585 | #ifdef CY_ATTACH |
586 | pid_t pid(_not(pid_t)); | |
587 | #endif | |
588 | ||
06edab7d JF |
589 | CYPool pool; |
590 | apr_getopt_t *state; | |
591 | _aprcall(apr_getopt_init(&state, pool, argc, argv)); | |
592 | ||
593 | for (;;) { | |
594 | char opt; | |
595 | const char *arg; | |
596 | ||
597 | apr_status_t status(apr_getopt(state, | |
598 | "cg:n:" | |
2aa77fd8 | 599 | #ifdef CY_ATTACH |
06edab7d | 600 | "p:" |
2aa77fd8 | 601 | #endif |
06edab7d JF |
602 | "s" |
603 | , &opt, &arg)); | |
604 | ||
605 | switch (status) { | |
606 | case APR_EOF: | |
607 | goto getopt; | |
608 | case APR_BADCH: | |
609 | case APR_BADARG: | |
610 | fprintf(stderr, | |
611 | "usage: cycript [-c]" | |
2aa77fd8 | 612 | #ifdef CY_ATTACH |
69caa5be | 613 | " [-p <pid|name>]" |
2aa77fd8 | 614 | #endif |
06edab7d JF |
615 | " [<script> [<arg>...]]\n" |
616 | ); | |
617 | return 1; | |
618 | default: | |
619 | _aprcall(status); | |
620 | } | |
967067aa | 621 | |
06edab7d JF |
622 | switch (opt) { |
623 | case 'c': | |
624 | compile = true; | |
625 | break; | |
75b0a457 | 626 | |
06edab7d JF |
627 | case 'g': |
628 | if (false); | |
de9fc71b JF |
629 | else if (strcmp(arg, "rename") == 0) |
630 | options.verbose_ = true; | |
cac61857 | 631 | #if YYDEBUG |
5ef46cc0 | 632 | else if (strcmp(arg, "bison") == 0) |
06edab7d | 633 | bison_ = true; |
cac61857 | 634 | #endif |
06edab7d JF |
635 | else { |
636 | fprintf(stderr, "invalid name for -g\n"); | |
637 | return 1; | |
638 | } | |
639 | break; | |
cac61857 | 640 | |
06edab7d JF |
641 | case 'n': |
642 | if (false); | |
5ef46cc0 | 643 | else if (strcmp(arg, "minify") == 0) |
06edab7d JF |
644 | pretty_ = true; |
645 | else { | |
646 | fprintf(stderr, "invalid name for -n\n"); | |
647 | return 1; | |
648 | } | |
649 | break; | |
11c1cc16 | 650 | |
2aa77fd8 | 651 | #ifdef CY_ATTACH |
06edab7d | 652 | case 'p': { |
5ef46cc0 | 653 | size_t size(strlen(arg)); |
06edab7d | 654 | char *end; |
69caa5be | 655 | |
5ef46cc0 JF |
656 | pid = strtoul(arg, &end, 0); |
657 | if (arg + size != end) { | |
69caa5be JF |
658 | // XXX: arg needs to be escaped in some horrendous way of doom |
659 | const char *command(apr_psprintf(pool, "ps axc|sed -e '/^ *[0-9]/{s/^ *\\([0-9]*\\)\\( *[^ ]*\\)\\{3\\} *-*\\([^ ]*\\)/\\3 \\1/;/^%s /{s/^[^ ]* //;q;};};d'", arg)); | |
660 | ||
661 | if (FILE *pids = popen(command, "r")) { | |
662 | char value[32]; | |
663 | size = 0; | |
664 | ||
665 | for (;;) { | |
666 | size_t read(fread(value + size, 1, sizeof(value) - size, pids)); | |
667 | if (read == 0) | |
668 | break; | |
669 | else { | |
670 | size += read; | |
671 | if (size == sizeof(value)) { | |
672 | pid = _not(pid_t); | |
b166b11b | 673 | goto fail; |
69caa5be JF |
674 | } |
675 | } | |
676 | } | |
677 | ||
678 | size: | |
679 | if (size == 0) | |
b166b11b | 680 | goto fail; |
69caa5be JF |
681 | if (value[size - 1] == '\n') { |
682 | --size; | |
683 | goto size; | |
684 | } | |
685 | ||
686 | value[size] = '\0'; | |
687 | size = strlen(value); | |
688 | pid = strtoul(value, &end, 0); | |
b166b11b | 689 | if (value + size != end) fail: |
69caa5be | 690 | pid = _not(pid_t); |
69caa5be JF |
691 | _syscall(pclose(pids)); |
692 | } | |
693 | ||
694 | if (pid == _not(pid_t)) { | |
695 | fprintf(stderr, "invalid pid for -p\n"); | |
696 | return 1; | |
697 | } | |
06edab7d JF |
698 | } |
699 | } break; | |
2aa77fd8 | 700 | #endif |
b10bd496 | 701 | |
06edab7d JF |
702 | case 's': |
703 | strict_ = true; | |
704 | break; | |
705 | } | |
967067aa JF |
706 | } getopt:; |
707 | ||
b09da87b | 708 | const char *script; |
5ef46cc0 | 709 | int ind(state->ind); |
b09da87b | 710 | |
2aa77fd8 | 711 | #ifdef CY_ATTACH |
5ef46cc0 | 712 | if (pid != _not(pid_t) && ind < argc - 1) { |
fcc64bb5 JF |
713 | fprintf(stderr, "-p cannot set argv\n"); |
714 | return 1; | |
715 | } | |
716 | ||
75b0a457 JF |
717 | if (pid != _not(pid_t) && compile) { |
718 | fprintf(stderr, "-p conflicts with -c\n"); | |
719 | return 1; | |
720 | } | |
2aa77fd8 | 721 | #endif |
75b0a457 | 722 | |
5ef46cc0 | 723 | if (ind == argc) |
b09da87b JF |
724 | script = NULL; |
725 | else { | |
9185d5ef | 726 | #ifdef CY_EXECUTE |
967067aa | 727 | // XXX: const_cast?! wtf gcc :( |
5ef46cc0 | 728 | CYSetArgs(argc - ind - 1, const_cast<const char **>(argv + ind + 1)); |
9185d5ef | 729 | #endif |
5ef46cc0 | 730 | script = argv[ind]; |
967067aa JF |
731 | if (strcmp(script, "-") == 0) |
732 | script = NULL; | |
b09da87b JF |
733 | } |
734 | ||
2aa77fd8 JF |
735 | #ifdef CY_ATTACH |
736 | if (pid != _not(pid_t) && script == NULL && !tty) { | |
737 | fprintf(stderr, "non-terminal attaching to remote console\n"); | |
48e3be8a JF |
738 | return 1; |
739 | } | |
2aa77fd8 | 740 | #endif |
48e3be8a | 741 | |
2aa77fd8 | 742 | #ifdef CY_ATTACH |
967067aa | 743 | if (pid == _not(pid_t)) |
7e5391fd | 744 | client_ = -1; |
967067aa | 745 | else { |
b166b11b JF |
746 | int server(_syscall(socket(PF_UNIX, SOCK_STREAM, 0))); try { |
747 | struct sockaddr_un address; | |
748 | memset(&address, 0, sizeof(address)); | |
749 | address.sun_family = AF_UNIX; | |
750 | ||
751 | sprintf(address.sun_path, "/tmp/.s.cy.%u", getpid()); | |
752 | ||
753 | _syscall(bind(server, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address))); | |
754 | _syscall(chmod(address.sun_path, 0777)); | |
755 | ||
756 | try { | |
757 | _syscall(listen(server, 1)); | |
758 | InjectLibrary(pid); | |
7e5391fd | 759 | client_ = _syscall(accept(server, NULL, NULL)); |
b166b11b JF |
760 | } catch (...) { |
761 | // XXX: exception? | |
762 | unlink(address.sun_path); | |
763 | throw; | |
764 | } | |
765 | } catch (...) { | |
766 | _syscall(close(server)); | |
95678376 | 767 | throw; |
b166b11b | 768 | } |
967067aa | 769 | } |
2aa77fd8 | 770 | #else |
7e5391fd | 771 | client_ = -1; |
2aa77fd8 | 772 | #endif |
579ed526 | 773 | |
48e3be8a | 774 | if (script == NULL && tty) |
7e5391fd | 775 | Console(pool, options); |
b09da87b | 776 | else { |
7e5391fd | 777 | CYDriver driver(pool, script ?: "<stdin>"); |
b09da87b | 778 | cy::parser parser(driver); |
b10bd496 | 779 | Setup(driver, parser); |
b09da87b | 780 | |
48e3be8a | 781 | char *start, *end; |
b09da87b | 782 | |
48e3be8a JF |
783 | if (script == NULL) { |
784 | start = NULL; | |
785 | end = NULL; | |
2b1245e4 | 786 | |
48e3be8a JF |
787 | driver.file_ = stdin; |
788 | } else { | |
789 | size_t size; | |
790 | start = reinterpret_cast<char *>(Map(script, &size)); | |
791 | end = start + size; | |
792 | ||
793 | if (size >= 2 && start[0] == '#' && start[1] == '!') { | |
794 | start += 2; | |
057f943f | 795 | |
48e3be8a JF |
796 | if (void *line = memchr(start, '\n', end - start)) |
797 | start = reinterpret_cast<char *>(line); | |
798 | else | |
799 | start = end; | |
800 | } | |
801 | ||
802 | driver.data_ = start; | |
803 | driver.size_ = end - start; | |
804 | } | |
62ca2b82 | 805 | |
b09da87b JF |
806 | if (parser.parse() != 0 || !driver.errors_.empty()) { |
807 | for (CYDriver::Errors::const_iterator i(driver.errors_.begin()); i != driver.errors_.end(); ++i) | |
808 | std::cerr << i->location_.begin << ": " << i->message_ << std::endl; | |
b10bd496 | 809 | } else if (driver.program_ != NULL) |
7e5391fd | 810 | if (client_ != -1) { |
e934f827 | 811 | std::string code(start, end-start); |
7e5391fd | 812 | Run(client_, code, stdout); |
e934f827 | 813 | } else { |
fcc64bb5 | 814 | std::ostringstream str; |
029bc65b | 815 | CYOutput out(str, options); |
de9fc71b | 816 | Setup(out, driver, options); |
3b52fd1a | 817 | out << *driver.program_; |
fcc64bb5 | 818 | std::string code(str.str()); |
75b0a457 JF |
819 | if (compile) |
820 | std::cout << code; | |
98ea05a3 | 821 | else |
7e5391fd | 822 | Run(client_, code, stdout); |
fcc64bb5 | 823 | } |
057f943f | 824 | } |
62ca2b82 | 825 | |
057f943f | 826 | return 0; |
62ca2b82 | 827 | } |
b6961e53 JF |
828 | |
829 | int main(int argc, char const * const argv[], char const * const envp[]) { | |
830 | apr_status_t status(apr_app_initialize(&argc, &argv, &envp)); | |
da858962 | 831 | |
b6961e53 JF |
832 | if (status != APR_SUCCESS) { |
833 | fprintf(stderr, "apr_app_initialize() != APR_SUCCESS\n"); | |
834 | return 1; | |
835 | } else try { | |
836 | return Main(argc, argv, envp); | |
837 | } catch (const CYException &error) { | |
838 | CYPool pool; | |
839 | fprintf(stderr, "%s\n", error.PoolCString(pool)); | |
840 | return 1; | |
841 | } | |
842 | } |