]> git.saurik.com Git - cycript.git/blobdiff - Console.cpp
Fix the implementation of cy$toJSON for primitives.
[cycript.git] / Console.cpp
index d407948fe4219b069cbb1fa4f865040d9e29e628..642cc20fcb9f79ad30c81ed46e7ce189f4cf16d2 100644 (file)
@@ -64,6 +64,7 @@
 #include <dlfcn.h>
 
 #include "Replace.hpp"
+#include "Display.hpp"
 
 static volatile enum {
     Working,
@@ -154,7 +155,14 @@ static CYUTF8String Run(CYPool &pool, int client, const std::string &code) {
 
 static std::ostream *out_;
 
-static void Output(CYUTF8String json, std::ostream *out, bool expand = false) {
+static void Write(bool syntax, const char *data, size_t size, std::ostream &out) {
+    if (syntax)
+        CYLexerHighlight(data, size, out);
+    else
+        out.write(data, size);
+}
+
+static void Output(bool syntax, CYUTF8String json, std::ostream *out, bool expand = false) {
     const char *data(json.data);
     size_t size(json.size);
 
@@ -165,7 +173,7 @@ static void Output(CYUTF8String json, std::ostream *out, bool expand = false) {
         data[0] != '@' && data[0] != '"' && data[0] != '\'' ||
         data[0] == '@' && data[1] != '"' && data[1] != '\''
     )
-        out->write(data, size);
+        Write(syntax, data, size, *out);
     else for (size_t i(0); i != size; ++i)
         if (data[i] != '\\')
             *out << data[i];
@@ -187,13 +195,13 @@ static void Output(CYUTF8String json, std::ostream *out, bool expand = false) {
     *out << std::endl;
 }
 
-static void Run(int client, const char *data, size_t size, std::ostream *out = NULL, bool expand = false) {
+static void Run(int client, bool syntax, const char *data, size_t size, std::ostream *out = NULL, bool expand = false) {
     CYPool pool;
-    Output(Run(pool, client, CYUTF8String(data, size)), out, expand);
+    Output(syntax, Run(pool, client, CYUTF8String(data, size)), out, expand);
 }
 
-static void Run(int client, std::string &code, std::ostream *out = NULL, bool expand = false) {
-    Run(client, code.c_str(), code.size(), out, expand);
+static void Run(int client, bool syntax, std::string &code, std::ostream *out = NULL, bool expand = false) {
+    Run(client, syntax, code.c_str(), code.size(), out, expand);
 }
 
 int (*append_history$)(int, const char *);
@@ -307,7 +315,7 @@ static char **Complete(const char *word, int start, int end) {
 
     if (array == NULL) {
         *out_ << '\n';
-        Output(json, out_);
+        Output(false, json, out_);
         rl_forced_update_display();
         return NULL;
     }
@@ -397,6 +405,7 @@ static void Console(CYOptions &options) {
     bool bypass(false);
     bool debug(false);
     bool expand(false);
+    bool syntax(true);
 
     out_ = &std::cout;
 
@@ -427,9 +436,23 @@ static void Console(CYOptions &options) {
         }
 
       read:
+
+#if RL_READLINE_VERSION >= 0x0600
+        if (syntax) {
+            rl_prep_term_function = CYDisplayStart;
+            rl_redisplay_function = CYDisplayUpdate;
+            rl_deprep_term_function = CYDisplayFinish;
+        } else {
+            rl_prep_term_function = rl_prep_terminal;
+            rl_redisplay_function = rl_redisplay;
+            rl_deprep_term_function = rl_deprep_terminal;
+        }
+#endif
+
         mode_ = Parsing;
         char *line(readline(prompt));
         mode_ = Working;
+
         if (line == NULL)
             break;
         if (line[0] == '\0')
@@ -448,6 +471,9 @@ static void Console(CYOptions &options) {
                 } else if (data == "expand") {
                     expand = !expand;
                     *out_ << "expand == " << (expand ? "true" : "false") << std::endl;
+                } else if (data == "syntax") {
+                    syntax = !syntax;
+                    *out_ << "syntax == " << (syntax ? "true" : "false") << std::endl;
                 }
                 add_history(line);
                 ++histlines;
@@ -534,10 +560,12 @@ static void Console(CYOptions &options) {
         add_history(command_.c_str());
         ++histlines;
 
-        if (debug)
-            std::cout << code << std::endl;
+        if (debug) {
+            Write(syntax, code.c_str(), code.size(), std::cout);
+            std::cout << std::endl;
+        }
 
-        Run(client_, code, out_, expand);
+        Run(client_, syntax, code, out_, expand);
     }
 
     if (append_history$ != NULL) {
@@ -808,7 +836,7 @@ int Main(int argc, char const * const argv[], char const * const envp[]) {
             if (client_ != -1) {
                 // XXX: this code means that you can't pipe to another process
                 std::string code(start, end-start);
-                Run(client_, code, &std::cout);
+                Run(client_, false, code, &std::cout);
             } else {
                 std::ostringstream str;
                 CYOutput out(str, options);
@@ -818,7 +846,7 @@ int Main(int argc, char const * const argv[], char const * const envp[]) {
                 if (compile)
                     std::cout << code;
                 else
-                    Run(client_, code, &std::cout);
+                    Run(client_, false, code, &std::cout);
             }
     }