X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/2b1245e47c16c879fbe534d0007dbfe47eeda3e5..bce8339b629582dd04df0570468ac746adc0fcf7:/Application.mm?ds=sidebyside diff --git a/Application.mm b/Application.mm index e910f66..1455012 100644 --- a/Application.mm +++ b/Application.mm @@ -61,47 +61,47 @@ #include "Cycript.tab.hh" +#include +#include +#include +#include + static jmp_buf ctrlc_; static void sigint(int) { longjmp(ctrlc_, 1); } -static JSStringRef Result_; - -void Run(const char *code, FILE *fout) { _pooled - JSStringRef script(JSStringCreateWithUTF8CString(code)); - - JSContextRef context(CYGetJSContext()); +void Run(int socket, std::string &code, FILE *fout) { + CYPool pool; - JSValueRef exception(NULL); - JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception)); - JSStringRelease(script); - - if (exception != NULL) { error: - result = exception; - exception = NULL; + const char *json; + if (socket == -1) + json = CYExecute(pool, code.c_str()); + else { + const char *data(code.c_str()); + size_t size(code.size()); + CYSendAll(socket, &size, sizeof(size)); + CYSendAll(socket, data, size); + CYRecvAll(socket, &size, sizeof(size)); + if (size == _not(size_t)) + json = NULL; + else { + char *temp(new(pool) char[size + 1]); + CYRecvAll(socket, temp, size); + temp[size] = '\0'; + json = temp; + } } - if (!JSValueIsUndefined(context, result)) { - CYPool pool; - const char *json; - - json = CYPoolCYONString(pool, context, result, &exception); - if (exception != NULL) - goto error; - - CYSetProperty(context, CYGetGlobalObject(context), Result_, result); - - if (fout != NULL) { - fputs(json, fout); - fputs("\n", fout); - fflush(fout); - } + if (json != NULL && fout != NULL) { + fputs(json, fout); + fputs("\n", fout); + fflush(fout); } } -static void Console() { +static void Console(int socket) { bool bypass(false); bool debug(false); @@ -186,9 +186,13 @@ static void Console() { if (driver.source_ == NULL) goto restart; - std::ostringstream str; - driver.source_->Show(str); - code = str.str(); + if (socket != -1) + code = command; + else { + std::ostringstream str; + driver.source_->Show(str); + code = str.str(); + } } add_history(command.c_str()); @@ -196,7 +200,7 @@ static void Console() { if (debug) std::cout << code << std::endl; - Run(code.c_str(), fout); + Run(socket, code, fout); } fputs("\n", fout); @@ -220,20 +224,61 @@ static void *Map(const char *path, size_t *psize) { return base; } -int main(int argc, const char *argv[]) { +int main(int argc, char *argv[]) { + pid_t pid(_not(pid_t)); + + for (;;) switch (getopt(argc, argv, "p:")) { + case -1: + goto getopt; + case '?': + fprintf(stderr, "usage: cycript [-p ] [