]> git.saurik.com Git - cycript.git/blobdiff - Console.cpp
Update injection handler for new local pool semantics.
[cycript.git] / Console.cpp
index bc353795e48633bac762dbec5a41183b29a56c22..c9bce69c63178ea577fd490f8ed1635b98d93f25 100644 (file)
@@ -125,7 +125,7 @@ void Setup(CYDriver &driver, cy::parser &parser) {
 
 void Setup(CYOutput &out, CYDriver &driver, CYOptions &options) {
     out.pretty_ = pretty_;
-    CYContext context(driver.pool_, options);
+    CYContext context(options);
     driver.program_->Replace(context);
 }
 
@@ -141,7 +141,9 @@ static CYUTF8String Run(CYPool &pool, int client, CYUTF8String code) {
         json = NULL;
 #endif
         mode_ = Working;
-        if (json != NULL)
+        if (json == NULL)
+            size = 0;
+        else
             size = strlen(json);
     } else {
         mode_ = Sending;
@@ -214,12 +216,12 @@ int (*append_history$)(int, const char *);
 
 static std::string command_;
 
-static CYExpression *ParseExpression(CYPool &pool, CYUTF8String code) {
+static CYExpression *ParseExpression(CYUTF8String code) {
     std::ostringstream str;
     str << '(' << code << ')';
     std::string string(str.str());
 
-    CYDriver driver(pool);
+    CYDriver driver;
     driver.data_ = string.c_str();
     driver.size_ = string.size();
 
@@ -239,9 +241,8 @@ static int client_;
 static char **Complete(const char *word, int start, int end) {
     rl_attempted_completion_over = TRUE;
 
-    CYPool pool;
-
-    CYDriver driver(pool);
+    CYLocalPool pool;
+    CYDriver driver;
     cy::parser parser(driver);
     Setup(driver, parser);
 
@@ -262,7 +263,7 @@ static char **Complete(const char *word, int start, int end) {
     CYExpression *expression;
 
     CYOptions options;
-    CYContext context(driver.pool_, options);
+    CYContext context(options);
 
     std::ostringstream prefix;
 
@@ -290,18 +291,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(
+    "   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);
 
@@ -312,7 +314,7 @@ static char **Complete(const char *word, int start, int end) {
     std::string code(str.str());
     CYUTF8String json(Run(pool, client_, code));
 
-    CYExpression *result(ParseExpression(pool, json));
+    CYExpression *result(ParseExpression(json));
     CYArray *array(dynamic_cast<CYArray *>(result));
 
     if (array == NULL) {
@@ -333,7 +335,14 @@ static char **Complete(const char *word, int start, int end) {
         CYString *string(dynamic_cast<CYString *>(element->value_));
         _assert(string != NULL);
 
-        std::string completion(string->value_, string->size_);
+        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) {
@@ -357,15 +366,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<char **>(malloc(sizeof(char *) * (count + 2))));
 
@@ -380,9 +385,11 @@ 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(CYOptions &options) {
+    CYPool pool;
 
-static void Console(apr_pool_t *pool, CYOptions &options) {
     passwd *passwd;
     if (const char *username = getenv("LOGNAME"))
         passwd = getpwnam(username);
@@ -482,6 +489,7 @@ static void Console(apr_pool_t *pool, CYOptions &options) {
         if (bypass)
             code = command_;
         else {
+            CYLocalPool pool;
             CYDriver driver;
             cy::parser parser(driver);
             Setup(driver, parser);
@@ -775,9 +783,10 @@ int Main(int argc, char const * const argv[], char const * const envp[]) {
 #endif
 
     if (script == NULL && tty)
-        Console(pool, options);
+        Console(options);
     else {
-        CYDriver driver(pool, script ?: "<stdin>");
+        CYLocalPool pool;
+        CYDriver driver(script ?: "<stdin>");
         cy::parser parser(driver);
         Setup(driver, parser);