From 8d9b5eed2cbd4dd74437558400833a9931ad3078 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 1 Sep 2009 09:06:08 +0000 Subject: [PATCH] Refactored console mechanism. --- Application.mm | 62 ++---------------------------------------- Tweak.mm => Library.mm | 52 +++++++++++++++++++++++++++++++++++ makefile | 6 ++-- 3 files changed, 58 insertions(+), 62 deletions(-) rename Tweak.mm => Library.mm (96%) diff --git a/Application.mm b/Application.mm index 7e3afdb..106c7dd 100644 --- a/Application.mm +++ b/Application.mm @@ -1,63 +1,7 @@ -#include -#include +#include -#include -#include - -#include - -#include -#include -#include -#include -#include -#include - -#define _trace() do { \ - CFLog(kCFLogLevelNotice, CFSTR("_trace(%u)"), __LINE__); \ -} while (false) - -JSContextRef JSGetContext(void); -void CYThrow(JSContextRef context, id error, JSValueRef *exception); -CFStringRef JSValueToJSONCopy(JSContextRef context, JSValueRef value); +int CYConsole(FILE *in, FILE *out, FILE *err); int main() { - for (;;) { - NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); - - std::cout << ">>> " << std::flush; - - std::string line; - if (!std::getline(std::cin, line)) - break; - - JSStringRef script(JSStringCreateWithUTF8CString(line.c_str())); - - JSContextRef context(JSGetContext()); - - JSValueRef exception(NULL); - JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception)); - JSStringRelease(script); - - if (exception != NULL) - result = exception; - - if (!JSValueIsUndefined(context, result)) { - CFStringRef json; - - @try { json: - json = JSValueToJSONCopy(context, result); - } @catch (id error) { - CYThrow(context, error, &result); - goto json; - } - - std::cout << [reinterpret_cast(json) UTF8String] << std::endl; - CFRelease(json); - } - - [pool release]; - } - - return 0; + return CYConsole(stdin, stdout, stderr); } diff --git a/Tweak.mm b/Library.mm similarity index 96% rename from Tweak.mm rename to Library.mm index 2a8bf97..d7c0aec 100644 --- a/Tweak.mm +++ b/Library.mm @@ -37,6 +37,8 @@ */ /* }}} */ +#define _GNU_SOURCE + #include #include "Struct.hpp" @@ -67,6 +69,9 @@ #include #include +#include +#include + #undef _assert #undef _trace @@ -984,6 +989,53 @@ static JSStaticValue Pointer_staticValues[2] = { {NULL, NULL, NULL, 0} }; +void CYConsole(FILE *fin, FILE *fout, FILE *ferr) { + std::string line; + + __gnu_cxx::stdio_filebuf bin(fin, std::ios::in); + std::istream sin(&bin); + + for (;;) { + NSAutoreleasePool *pool([[NSAutoreleasePool alloc] init]); + + fputs(">>> ", fout); + fflush(fout); + + if (!std::getline(sin, line)) + break; + + JSStringRef script(JSStringCreateWithUTF8CString(line.c_str())); + + JSContextRef context(JSGetContext()); + + JSValueRef exception(NULL); + JSValueRef result(JSEvaluateScript(context, script, NULL, NULL, 0, &exception)); + JSStringRelease(script); + + if (exception != NULL) + result = exception; + + if (!JSValueIsUndefined(context, result)) { + CFStringRef json; + + @try { json: + json = JSValueToJSONCopy(context, result); + } @catch (id error) { + CYThrow(context, error, &result); + goto json; + } + + fputs([reinterpret_cast(json) UTF8String], fout); + CFRelease(json); + + fputs("\n", fout); + fflush(fout); + } + + [pool release]; + } +} + MSInitialize { _pooled apr_initialize(); diff --git a/makefile b/makefile index a45ae3c..bbeb938 100644 --- a/makefile +++ b/makefile @@ -31,12 +31,12 @@ libcycript.plist: Bridge.def makefile Struct.hpp: $$($(target)gcc -print-prog-name=cc1obj) -print-objc-runtime-info $@ -libcycript.dylib: Tweak.mm makefile $(menes)/mobilesubstrate/substrate.h sig/*.[ch]pp Struct.hpp - $(target)g++ -save-temps -dynamiclib -mthumb -g0 -O2 -Wall -Werror -o $@ $(filter %.cpp,$^) $(filter %.mm,$^) -lobjc -I$(menes)/mobilesubstrate $(link) $(flags) +libcycript.dylib: Library.mm makefile $(menes)/mobilesubstrate/substrate.h sig/*.[ch]pp Struct.hpp + $(target)g++ -dynamiclib -mthumb -g0 -O2 -Wall -Werror -o $@ $(filter %.cpp,$^) $(filter %.mm,$^) -lobjc -I$(menes)/mobilesubstrate $(link) $(flags) ldid -S $@ cycript: Application.mm libcycript.dylib - $(target)g++ -g0 -O2 -Wall -Werror -o $@ $(filter %.mm,$^) -framework UIKit -framework Foundation -framework CoreFoundation -lobjc libcycript.dylib -framework JavaScriptCore -F${PKG_ROOT}/System/Library/PrivateFrameworks + $(target)g++ -g0 -O2 -Wall -Werror -o $@ $(filter %.mm,$^) -framework UIKit -framework Foundation -framework CoreFoundation -lobjc libcycript.dylib ldid -S cycript package: all -- 2.47.2