if (client == -1) {
mode_ = Running;
#ifdef CY_EXECUTE
- json = CYExecute(pool, code.data);
+ json = CYExecute(pool, code);
#else
json = NULL;
#endif
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))
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) {
_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);
CYExpression *result(ParseExpression(pool, json));
CYArray *array(dynamic_cast<CYArray *>(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<std::string> Completions;
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<char **>(malloc(sizeof(char *) * (count + 2))));
// 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;
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_;
if (setjmp(ctrlc_) != 0) {
mode_ = Working;
- fputs("\n", fout);
- fflush(fout);
+ fputs("\n", fout_);
+ fflush(fout_);
goto restart;
}
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;
if (debug)
std::cout << code << std::endl;
- Run(client_, code, fout, expand);
+ Run(client_, code, fout_, expand);
}
if (append_history$ != NULL) {
write_history(histfile);
}
- fputs("\n", fout);
- fflush(fout);
+ fputs("\n", fout_);
+ fflush(fout_);
}
static void *Map(const char *path, size_t *psize) {