+int (*append_history$)(int, const char *);
+
+static std::string command_;
+
+static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) {
+ std::ostringstream str;
+ str << '(' << code << ')';
+ std::string string(str.str());
+
+ CYDriver driver(pool);
+ driver.data_ = string.c_str();
+ driver.size_ = string.size();
+
+ cy::parser parser(driver);
+ Setup(driver, parser);
+
+ if (parser.parse() != 0 || !driver.errors_.empty())
+ _assert(false);
+
+ CYExpress *express(dynamic_cast<CYExpress *>(driver.program_->statements_));
+ _assert(express != NULL);
+ return express->expression_;
+}
+
+static int client_;
+
+static char **Complete(const char *word, int start, int end) {
+ rl_attempted_completion_over = TRUE;
+
+ CYPool pool;
+
+ CYDriver driver(pool);
+ 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(driver.pool_, 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() + word);
+
+ driver.program_ = $ CYProgram($ CYExpress($C2(ParseExpression(pool,
+ " function(object, prefix) {\n"
+ " var names = [];\n"
+ " var pattern = '^' + prefix;\n"
+ " for (name in object)\n"
+ " if (name.match(pattern) != null)\n"
+ " names.push(name);\n"
+ " return names;\n"
+ " }\n"
+ ), expression, $S(begin.c_str()))));
+
+ 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(pool, json));
+ CYArray *array(dynamic_cast<CYArray *>(result));
+ _assert(array != 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(string->value_, string->size_);
+ 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;
+
+ if (!common.empty()) {
+ size_t size(prefix.str().size());
+ _assert(common.size() >= size);
+ common = common.substr(size);
+ }
+
+ 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(apr_pool_t *pool, CYOptions &options) {