]> git.saurik.com Git - cycript.git/blobdiff - Handler.cpp
With -p on all platforms, we can't use asprintf().
[cycript.git] / Handler.cpp
index 2a17d61d0d9d4d9b399a71fefa757b03dcca8ae1..744344fc9c5709bf487e14dc846119cbd6ecdfcd 100644 (file)
@@ -1,5 +1,5 @@
-/* Cycript - Optimizing JavaScript Compiler/Runtime
- * Copyright (C) 2009-2015  Jay Freeman (saurik)
+/* Cycript - The Truly Universal Scripting Language
+ * Copyright (C) 2009-2016  Jay Freeman (saurik)
 */
 
 /* GNU Affero General Public License, Version 3 {{{ */
@@ -19,6 +19,8 @@
 **/
 /* }}} */
 
+#include "cycript.hpp"
+
 #include <dlfcn.h>
 #include <pthread.h>
 #include <unistd.h>
 #include <CoreFoundation/CoreFoundation.h>
 #endif
 
-#include "cycript.hpp"
-
 #include "Driver.hpp"
 #include "JavaScript.hpp"
-#include "Parser.hpp"
+#include "Syntax.hpp"
 #include "Pooling.hpp"
 
 struct CYExecute_ {
@@ -60,6 +60,54 @@ void CYPerform(void *arg) {
     pthread_mutex_unlock(&execute->mutex_);
 }
 
+const char *CYHandleCommand(CYPool &pool, const std::string &code) {
+    bool dispatch;
+#ifdef __APPLE__
+    CFRunLoopRef loop(CFRunLoopGetMain());
+    if (CFStringRef mode = CFRunLoopCopyCurrentMode(loop)) {
+        dispatch = true;
+        CFRelease(mode);
+    } else
+#endif
+        dispatch = false;
+
+    CYExecute_ execute = {pool, code.c_str()};
+
+    pthread_mutex_init(&execute.mutex_, NULL);
+    pthread_cond_init(&execute.condition_, NULL);
+
+    if (!dispatch)
+        CYPerform(&execute);
+#ifdef __APPLE__
+    else {
+        CFRunLoopSourceContext context;
+        memset(&context, 0, sizeof(context));
+        context.version = 0;
+        context.info = &execute;
+        context.perform = &CYPerform;
+
+        CFRunLoopSourceRef source(CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context));
+
+        pthread_mutex_lock(&execute.mutex_);
+
+        CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes);
+        CFRunLoopSourceSignal(source);
+
+        CFRunLoopWakeUp(loop);
+        pthread_cond_wait(&execute.condition_, &execute.mutex_);
+        pthread_mutex_unlock(&execute.mutex_);
+
+        CFRunLoopRemoveSource(loop, source, kCFRunLoopCommonModes);
+        CFRelease(source);
+    }
+#endif
+
+    pthread_cond_destroy(&execute.condition_);
+    pthread_mutex_destroy(&execute.mutex_);
+
+    return execute.data_;
+}
+
 struct CYClient :
     CYData
 {
@@ -76,16 +124,6 @@ struct CYClient :
     }
 
     void Handle() {
-        bool dispatch;
-#ifdef __APPLE__
-        CFRunLoopRef loop(CFRunLoopGetMain());
-        if (CFStringRef mode = CFRunLoopCopyCurrentMode(loop)) {
-            dispatch = true;
-            CFRelease(mode);
-        } else
-#endif
-            dispatch = false;
-
         for (;;) {
             uint32_t size;
             if (!CYRecvAll(socket_, &size, sizeof(size)))
@@ -98,41 +136,7 @@ struct CYClient :
             data[size] = '\0';
 
             std::string code(data, size);
-            CYExecute_ execute = {pool, code.c_str()};
-
-            pthread_mutex_init(&execute.mutex_, NULL);
-            pthread_cond_init(&execute.condition_, NULL);
-
-            if (!dispatch)
-                CYPerform(&execute);
-#ifdef __APPLE__
-            else {
-                CFRunLoopSourceContext context;
-                memset(&context, 0, sizeof(context));
-                context.version = 0;
-                context.info = &execute;
-                context.perform = &CYPerform;
-
-                CFRunLoopSourceRef source(CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context));
-
-                pthread_mutex_lock(&execute.mutex_);
-
-                CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes);
-                CFRunLoopSourceSignal(source);
-
-                CFRunLoopWakeUp(loop);
-                pthread_cond_wait(&execute.condition_, &execute.mutex_);
-                pthread_mutex_unlock(&execute.mutex_);
-
-                CFRunLoopRemoveSource(loop, source, kCFRunLoopCommonModes);
-                CFRelease(source);
-            }
-#endif
-
-            pthread_cond_destroy(&execute.condition_);
-            pthread_mutex_destroy(&execute.mutex_);
-
-            const char *json(execute.data_);
+            const char *json(CYHandleCommand(pool, code));
             size = json == NULL ? _not(uint32_t) : strlen(json);
 
             if (!CYSendAll(socket_, &size, sizeof(size)))
@@ -166,7 +170,7 @@ static void CYHandleSocket(const char *path) {
     address.sun_family = AF_UNIX;
     strcpy(address.sun_path, path);
 
-    _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), SUN_LEN(&address)));
+    _syscall(connect(socket, reinterpret_cast<sockaddr *>(&address), sizeof(address)));
 
     CYInitializeDynamic();
     CYHandleClient(socket);
@@ -182,17 +186,37 @@ _extern void CYHandleServer(pid_t pid) { try {
 } }
 
 _extern char *MSmain0(int argc, char *argv[]) { try {
-    _assert(argc == 2);
-    CYHandleSocket(argv[1]);
+    char *error(NULL);
+
+    switch (argc) {
+        case 2:
+            CYHandleSocket(argv[1]);
+        break;
+
+        case 3:
+            if (false);
+            else if (strcmp(argv[1], "-e") == 0) {
+                CYPool pool;
+                error = strdup(CYHandleCommand(pool, argv[2]) ?: "");
+            } else _assert(false);
+        break;
+
+        default:
+            _assert(false);
+    }
 
     static void *handle(NULL);
     if (handle == NULL) {
         Dl_info info;
         _assert(dladdr(reinterpret_cast<void *>(&MSmain0), &info) != 0);
+#ifdef __ANDROID__
+        handle = dlopen(info.dli_fname, 0);
+#else
         handle = dlopen(info.dli_fname, RTLD_NOLOAD);
+#endif
     }
 
-    return NULL;
+    return error;
 } catch (const CYException &error) {
     CYPool pool;
     return strdup(error.PoolCString(pool));