X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/bc1e87aa9169654208f6ee87281108143792d981..996c7c0ce2f75b82b32c43d64c350fd71b05e0cd:/Console.cpp diff --git a/Console.cpp b/Console.cpp index 7f4f454..3fe2310 100644 --- a/Console.cpp +++ b/Console.cpp @@ -136,7 +136,7 @@ static CYUTF8String Run(CYPool &pool, int client, CYUTF8String code) { if (client == -1) { mode_ = Running; #ifdef CY_EXECUTE - json = CYExecute(pool, code.data); + json = CYExecute(pool, code); #else json = NULL; #endif @@ -145,8 +145,9 @@ static CYUTF8String Run(CYPool &pool, int client, CYUTF8String code) { size = strlen(json); } else { mode_ = Sending; + size = code.size; CYSendAll(client, &size, sizeof(size)); - CYSendAll(client, code.data, size); + CYSendAll(client, code.data, code.size); mode_ = Waiting; CYRecvAll(client, &size, sizeof(size)); if (size == _not(size_t)) @@ -167,37 +168,42 @@ static CYUTF8String Run(CYPool &pool, int client, const std::string &code) { return Run(pool, client, CYUTF8String(code.c_str(), code.size())); } -void Run(int client, const char *data, size_t size, FILE *fout = NULL, bool expand = false) { - CYPool pool; - CYUTF8String json(Run(pool, client, CYUTF8String(data, size))); - - data = json.data; - size = json.size; - - if (data != NULL && fout != NULL) { - if (!expand || data[0] != '"' && data[0] != '\'') - fputs(data, fout); - else for (size_t i(0); i != size; ++i) - if (data[i] != '\\') - fputc(data[i], fout); - else switch(data[++i]) { - case '\0': goto done; - case '\\': fputc('\\', fout); break; - case '\'': fputc('\'', fout); break; - case '"': fputc('"', fout); break; - case 'b': fputc('\b', fout); break; - case 'f': fputc('\f', fout); break; - case 'n': fputc('\n', fout); break; - case 'r': fputc('\r', fout); break; - case 't': fputc('\t', fout); break; - case 'v': fputc('\v', fout); break; - default: fputc('\\', fout); --i; break; - } +FILE *fout_; + +static void Output(CYUTF8String json, FILE *fout, bool expand = false) { + const char *data(json.data); + size_t size(json.size); + + if (data == NULL || fout == NULL) + return; + + if (!expand || data[0] != '"' && data[0] != '\'') + fputs(data, fout); + else for (size_t i(0); i != size; ++i) + if (data[i] != '\\') + fputc(data[i], fout); + else switch(data[++i]) { + case '\0': goto done; + case '\\': fputc('\\', fout); break; + case '\'': fputc('\'', fout); break; + case '"': fputc('"', fout); break; + case 'b': fputc('\b', fout); break; + case 'f': fputc('\f', fout); break; + case 'n': fputc('\n', fout); break; + case 'r': fputc('\r', fout); break; + case 't': fputc('\t', fout); break; + case 'v': fputc('\v', fout); break; + default: fputc('\\', fout); --i; break; + } - done: - fputs("\n", fout); - fflush(fout); - } + done: + fputs("\n", fout); + fflush(fout); +} + +static void Run(int client, const char *data, size_t size, FILE *fout = NULL, bool expand = false) { + CYPool pool; + Output(Run(pool, client, CYUTF8String(data, size)), fout, expand); } static void Run(int client, std::string &code, FILE *fout = NULL, bool expand = false) { @@ -284,18 +290,19 @@ static char **Complete(const char *word, int start, int end) { _assert(false); } - std::string begin(prefix.str() + word); + std::string begin(prefix.str()); - driver.program_ = $ CYProgram($ CYExpress($C2(ParseExpression(pool, - " function(object, prefix) {\n" + driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression(pool, + " function(object, prefix, word) {\n" " var names = [];\n" - " var pattern = '^' + prefix;\n" + " var pattern = '^' + prefix + word;\n" + " var length = prefix.length;\n" " for (name in object)\n" " if (name.match(pattern) != null)\n" - " names.push(name);\n" + " names.push(name.substr(length));\n" " return names;\n" " }\n" - ), expression, $S(begin.c_str())))); + ), expression, $S(begin.c_str()), $S(word)))); driver.program_->Replace(context); @@ -308,7 +315,13 @@ static char **Complete(const char *word, int start, int end) { CYExpression *result(ParseExpression(pool, json)); CYArray *array(dynamic_cast(result)); - _assert(array != NULL); + + if (array == NULL) { + fprintf(fout_, "\n"); + Output(json, fout_); + rl_forced_update_display(); + return NULL; + } // XXX: use an std::set? typedef std::vector Completions; @@ -345,15 +358,11 @@ static char **Complete(const char *word, int start, int end) { if (count == 0) return NULL; - if (!common.empty()) { - size_t size(prefix.str().size()); - _assert(common.size() >= size); - common = common.substr(size); - } - size_t colon(common.find(':')); if (colon != std::string::npos) common = common.substr(0, colon + 1); + if (completions.size() == 1) + common += ' '; char **results(reinterpret_cast(malloc(sizeof(char *) * (count + 2)))); @@ -368,7 +377,7 @@ static char **Complete(const char *word, int start, int end) { // need char *, not const char * static char name_[] = "cycript"; -static char break_[] = " \t\n\"\\'`@$><=;|&{(" ".:"; +static char break_[] = " \t\n\"\\'`@$><=;|&{(" ")}" ".:[]"; static void Console(apr_pool_t *pool, CYOptions &options) { passwd *passwd; @@ -391,7 +400,7 @@ static void Console(apr_pool_t *pool, CYOptions &options) { bool debug(false); bool expand(false); - FILE *fout(stdout); + fout_ = stdout; // rl_completer_word_break_characters is broken in libedit rl_basic_word_break_characters = break_; @@ -415,8 +424,8 @@ static void Console(apr_pool_t *pool, CYOptions &options) { if (setjmp(ctrlc_) != 0) { mode_ = Working; - fputs("\n", fout); - fflush(fout); + fputs("\n", fout_); + fflush(fout_); goto restart; } @@ -435,16 +444,16 @@ static void Console(apr_pool_t *pool, CYOptions &options) { std::string data(line + 1); if (data == "bypass") { bypass = !bypass; - fprintf(fout, "bypass == %s\n", bypass ? "true" : "false"); - fflush(fout); + fprintf(fout_, "bypass == %s\n", bypass ? "true" : "false"); + fflush(fout_); } else if (data == "debug") { debug = !debug; - fprintf(fout, "debug == %s\n", debug ? "true" : "false"); - fflush(fout); + fprintf(fout_, "debug == %s\n", debug ? "true" : "false"); + fflush(fout_); } else if (data == "expand") { expand = !expand; - fprintf(fout, "expand == %s\n", expand ? "true" : "false"); - fflush(fout); + fprintf(fout_, "expand == %s\n", expand ? "true" : "false"); + fflush(fout_); } add_history(line); ++histlines; @@ -533,7 +542,7 @@ static void Console(apr_pool_t *pool, CYOptions &options) { if (debug) std::cout << code << std::endl; - Run(client_, code, fout, expand); + Run(client_, code, fout_, expand); } if (append_history$ != NULL) { @@ -543,8 +552,8 @@ static void Console(apr_pool_t *pool, CYOptions &options) { write_history(histfile); } - fputs("\n", fout); - fflush(fout); + fputs("\n", fout_); + fflush(fout_); } static void *Map(const char *path, size_t *psize) {