+static int client_;
+
+static char **Complete(const char *word, int start, int end) {
+ rl_attempted_completion_over = TRUE;
+
+ CYLocalPool pool;
+ CYDriver driver;
+ cy::parser parser(driver);
+ Setup(driver, parser);
+
+ std::string line(rl_line_buffer, start);
+ std::string command(command_ + line);
+
+ driver.data_ = command.c_str();
+ driver.size_ = command.size();
+
+ driver.auto_ = true;
+
+ if (parser.parse() != 0 || !driver.errors_.empty())
+ return NULL;
+
+ if (driver.mode_ == CYDriver::AutoNone)
+ return NULL;
+
+ CYExpression *expression;
+
+ CYOptions options;
+ CYContext context(options);
+
+ std::ostringstream prefix;
+
+ switch (driver.mode_) {
+ case CYDriver::AutoPrimary:
+ expression = $ CYThis();
+ break;
+
+ case CYDriver::AutoDirect:
+ expression = driver.context_;
+ break;
+
+ case CYDriver::AutoIndirect:
+ expression = $ CYIndirect(driver.context_);
+ break;
+
+ case CYDriver::AutoMessage: {
+ CYDriver::Context &thing(driver.contexts_.back());
+ expression = $M($M($ CYIndirect(thing.context_), $S("isa")), $S("messages"));
+ for (CYDriver::Context::Words::const_iterator part(thing.words_.begin()); part != thing.words_.end(); ++part)
+ prefix << (*part)->word_ << ':';
+ } break;
+
+ default:
+ _assert(false);
+ }
+
+ std::string begin(prefix.str());
+
+ driver.program_ = $ CYProgram($ CYExpress($C3(ParseExpression(
+ " function(object, prefix, word) {\n"
+ " var names = [];\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.substr(length));\n"
+ " return names;\n"
+ " }\n"
+ ), expression, $S(begin.c_str()), $S(word))));
+
+ driver.program_->Replace(context);
+
+ std::ostringstream str;
+ CYOutput out(str, options);
+ out << *driver.program_;
+
+ std::string code(str.str());
+ CYUTF8String json(Run(pool, client_, code));
+
+ CYExpression *result(ParseExpression(json));
+ CYArray *array(dynamic_cast<CYArray *>(result));
+
+ 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;
+ Completions completions;
+
+ std::string common;
+ bool rest(false);
+
+ for (CYElement *element(array->elements_); element != NULL; element = element->next_) {
+ CYString *string(dynamic_cast<CYString *>(element->value_));
+ _assert(string != NULL);
+
+ std::string completion;
+ if (string->size_ != 0)
+ completion.assign(string->value_, string->size_);
+ else if (driver.mode_ == CYDriver::AutoMessage)
+ completion = "]";
+ else
+ continue;
+
+ completions.push_back(completion);
+
+ if (!rest) {
+ common = completion;
+ rest = true;
+ } else {
+ size_t limit(completion.size()), size(common.size());
+ if (size > limit)
+ common = common.substr(0, limit);
+ else
+ limit = size;
+ for (limit = 0; limit != size; ++limit)
+ if (common[limit] != completion[limit])
+ break;
+ if (limit != size)
+ common = common.substr(0, limit);
+ }
+ }
+
+ size_t count(completions.size());
+ if (count == 0)
+ return NULL;
+
+ 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))));
+
+ results[0] = strdup(common.c_str());
+ size_t index(0);
+ for (Completions::const_iterator i(completions.begin()); i != completions.end(); ++i)
+ results[++index] = strdup(i->c_str());
+ results[count + 1] = NULL;
+
+ return results;
+}
+
+// need char *, not const char *
+static char name_[] = "cycript";
+static char break_[] = " \t\n\"\\'`@$><=;|&{(" ")}" ".:[]";
+
+static void Console(CYOptions &options) {
+ CYPool pool;
+
+ passwd *passwd;
+ if (const char *username = getenv("LOGNAME"))
+ passwd = getpwnam(username);
+ else
+ passwd = getpwuid(getuid());
+
+ const char *basedir(apr_psprintf(pool, "%s/.cycript", passwd->pw_dir));
+ const char *histfile(apr_psprintf(pool, "%s/history", basedir));
+ size_t histlines(0);
+
+ rl_initialize();
+ rl_readline_name = name_;
+
+ mkdir(basedir, 0700);
+ read_history(histfile);
+