Browse Source

first real commit

Kevin Bradley 6 years ago
parent
commit
3a04fc6eda
100 changed files with 8119 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 13 0
      AppEnabler/AppEnabler.plist
  3. 19 0
      AppEnabler/Makefile
  4. 30 0
      AppEnabler/Whitelist.xm
  5. 168 0
      AppEnabler/main.x
  6. 29 0
      Makefile
  7. 15 0
      PBReloadHelper.h
  8. 578 0
      PBReloadHelper.m
  9. 21 0
      Tweak.xm
  10. 7 0
      com.nito.whitelist.plist
  11. 10 0
      control
  12. 38 0
      main.m
  13. 4 0
      rmcache
  14. 1 0
      theos/Defines.h
  15. 609 0
      theos/LICENSE.md
  16. 55 0
      theos/Prefix.pch
  17. 6 0
      theos/README.md
  18. 31 0
      theos/bin/deb_build_num.sh
  19. 48 0
      theos/bin/denicify.pl
  20. 200 0
      theos/bin/dm.pl
  21. 1 0
      theos/bin/dm.pl.txt
  22. 46 0
      theos/bin/fakeroot.sh
  23. 6 0
      theos/bin/install.copyFile
  24. 19 0
      theos/bin/install.exec
  25. 4 0
      theos/bin/install.mergeDir
  26. 122 0
      theos/bin/lib/Logos/Class.pm
  27. 64 0
      theos/bin/lib/Logos/Function.pm
  28. 48 0
      theos/bin/lib/Logos/Generator.pm
  29. 55 0
      theos/bin/lib/Logos/Generator/Base/Class.pm
  30. 177 0
      theos/bin/lib/Logos/Generator/Base/Function.pm
  31. 49 0
      theos/bin/lib/Logos/Generator/Base/Generator.pm
  32. 28 0
      theos/bin/lib/Logos/Generator/Base/Group.pm
  33. 119 0
      theos/bin/lib/Logos/Generator/Base/Method.pm
  34. 271 0
      theos/bin/lib/Logos/Generator/Base/Property.pm
  35. 34 0
      theos/bin/lib/Logos/Generator/Base/StaticClassGroup.pm
  36. 12 0
      theos/bin/lib/Logos/Generator/Base/Subclass.pm
  37. 19 0
      theos/bin/lib/Logos/Generator/MobileSubstrate/Class.pm
  38. 18 0
      theos/bin/lib/Logos/Generator/MobileSubstrate/Function.pm
  39. 22 0
      theos/bin/lib/Logos/Generator/MobileSubstrate/Generator.pm
  40. 122 0
      theos/bin/lib/Logos/Generator/MobileSubstrate/Method.pm
  41. 32 0
      theos/bin/lib/Logos/Generator/MobileSubstrate/Subclass.pm
  42. 54 0
      theos/bin/lib/Logos/Generator/Thunk.pm
  43. 51 0
      theos/bin/lib/Logos/Generator/internal/Class.pm
  44. 14 0
      theos/bin/lib/Logos/Generator/internal/Function.pm
  45. 22 0
      theos/bin/lib/Logos/Generator/internal/Generator.pm
  46. 141 0
      theos/bin/lib/Logos/Generator/internal/Method.pm
  47. 33 0
      theos/bin/lib/Logos/Generator/internal/Subclass.pm
  48. 136 0
      theos/bin/lib/Logos/Group.pm
  49. 39 0
      theos/bin/lib/Logos/Ivar.pm
  50. 285 0
      theos/bin/lib/Logos/Method.pm
  51. 98 0
      theos/bin/lib/Logos/Patch.pm
  52. 27 0
      theos/bin/lib/Logos/Patch/Source/Generator.pm
  53. 69 0
      theos/bin/lib/Logos/Property.pm
  54. 51 0
      theos/bin/lib/Logos/StaticClassGroup.pm
  55. 67 0
      theos/bin/lib/Logos/Subclass.pm
  56. 113 0
      theos/bin/lib/Logos/Util.pm
  57. 120 0
      theos/bin/lib/NIC/Bridge/Context.pm
  58. 6 0
      theos/bin/lib/NIC/Bridge/Directory.pm
  59. 13 0
      theos/bin/lib/NIC/Bridge/File.pm
  60. 76 0
      theos/bin/lib/NIC/Bridge/NICBase.pm
  61. 29 0
      theos/bin/lib/NIC/Bridge/NICType.pm
  62. 13 0
      theos/bin/lib/NIC/Bridge/Symlink.pm
  63. 28 0
      theos/bin/lib/NIC/Bridge/Tie/WrappedMethod.pm
  64. 19 0
      theos/bin/lib/NIC/Bridge/_BridgedObject.pm
  65. 21 0
      theos/bin/lib/NIC/Bridge/_Undefined.pm
  66. 60 0
      theos/bin/lib/NIC/Formats/NIC1.pm
  67. 146 0
      theos/bin/lib/NIC/Formats/NICTar.pm
  68. 11 0
      theos/bin/lib/NIC/Formats/NICTar/Directory.pm
  69. 16 0
      theos/bin/lib/NIC/Formats/NICTar/File.pm
  70. 11 0
      theos/bin/lib/NIC/Formats/NICTar/Symlink.pm
  71. 20 0
      theos/bin/lib/NIC/Formats/NICTar/_TarMixin.pm
  72. 221 0
      theos/bin/lib/NIC/NICBase.pm
  73. 24 0
      theos/bin/lib/NIC/NICBase/Directory.pm
  74. 36 0
      theos/bin/lib/NIC/NICBase/File.pm
  75. 52 0
      theos/bin/lib/NIC/NICBase/Symlink.pm
  76. 79 0
      theos/bin/lib/NIC/NICType.pm
  77. 26 0
      theos/bin/lib/NIC/Tie/Method.pm
  78. 34 0
      theos/bin/lib/NIC/Tie/PrefixedHandleRedirect.pm
  79. 326 0
      theos/bin/lib/aliased.pm
  80. 136 0
      theos/bin/lib/parent.pm
  81. 63 0
      theos/bin/logify.pl
  82. 967 0
      theos/bin/logos.pl
  83. 324 0
      theos/bin/nic.pl
  84. 147 0
      theos/bin/nicify.pl
  85. 44 0
      theos/bin/package_version.sh
  86. 26 0
      theos/bin/target.pl
  87. 23 0
      theos/bin/vercmp.pl
  88. 7 0
      theos/documentation/Makefile
  89. 405 0
      theos/documentation/makefiles.docbook
  90. 3 0
      theos/extras/vim/README
  91. 11 0
      theos/extras/vim/ftplugin/logos.vim
  92. 6 0
      theos/extras/vim/indent/logos.vim
  93. 69 0
      theos/extras/vim/syntax/logos.vim
  94. 11 0
      theos/git-submodule-recur.sh
  95. 0 0
      theos/include/.keep
  96. 1 0
      theos/include/APDeviceController.h
  97. 32 0
      theos/include/AppSupport/CPDistributedMessagingCenter.h
  98. 65 0
      theos/include/ContextFilterLogFormatter.h
  99. 41 0
      theos/include/DDASLLogger.h
  100. 0 0
      theos/include/DDAbstractDatabaseLogger.h

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+debs*

+ 13 - 0
AppEnabler/AppEnabler.plist

@@ -0,0 +1,13 @@
+{
+	Filter = {
+		Bundles = (
+			"com.apple.PineBoard",
+			"com.apple.Security",
+			"com.apple.MobileCoreServices",
+			"com.apple.PineBoardServices",
+		);
+		Executables = (
+			installd,
+		);
+	};
+}

+ 19 - 0
AppEnabler/Makefile

@@ -0,0 +1,19 @@
+ARCHS = arm64
+TARGET = appletv
+export GO_EASY_ON_ME=1
+export SDKVERSION=10.2
+THEOS_DEVICE_IP=atv.local
+
+include $(THEOS)/makefiles/common.mk
+
+TWEAK_NAME = AppEnabler
+AppEnabler_FILES = main.x Whitelist.xm
+AppEnabler_LIBRARIES = substrate
+AppEnabler_FRAMEWORKS = Foundation UIKit CoreGraphics MobileCoreServices
+AppEnabler_LDFLAGS = -undefined dynamic_lookup
+#AppEnabler_LIBRARIES += rocketbootstrap
+
+include $(THEOS_MAKE_PATH)/tweak.mk
+
+after-install::
+	install.exec "killall -9 lsd"

+ 30 - 0
AppEnabler/Whitelist.xm

@@ -0,0 +1,30 @@
+%hook PBSMutableAppState
+
+-(BOOL)isEnabled
+{
+    //%log;
+    NSString *ourID = [self applicationIdentifier];
+    //don't interfere with stuff that is already enabled properly
+    if (%orig == YES){
+        //NSLog(@"already yes: %@",ourID );
+        return %orig;
+    }
+    NSArray *whitelistArray = [NSArray arrayWithContentsOfFile:@"/var/mobile/Library/Preferences/com.nito.whitelist.plist"];
+    NSString *recentlyDeleted = @"/var/mobile/Library/Preferences/com.nito.deleted.plist";
+    NSArray *deletedArray = nil;
+    if ([[NSFileManager defaultManager] fileExistsAtPath:recentlyDeleted])
+    {
+        deletedArray = [NSArray arrayWithContentsOfFile:recentlyDeleted];
+    }
+    if ([whitelistArray containsObject:ourID] && ![deletedArray containsObject:ourID])
+    {
+        NSLog(@"whitelisted: %@",ourID );
+        return YES;
+    } else {
+        return NO;
+    }
+   // return %orig;
+}
+
+
+%end

+ 168 - 0
AppEnabler/main.x

@@ -0,0 +1,168 @@
+/**
+ * AppEnabler for nitoTV
+ *
+ * Makes it possible to load Applications from /var/mobile/Applications
+ *
+ * By Luca Todesco and Kevin Bradley
+ *
+ */
+
+#import <Foundation/Foundation.h>
+#include <mach-o/dyld.h>
+#import <objc/runtime.h>
+//#import "rocketbootstrap.h"
+//#import "AppSupport/CPDistributedMessagingCenter.h"
+
+%class LSContext
+%class FSNode
+
+/*
+ 
+ Some fun magic happens in here. We hook _MobileInstallationEnumerateAllInstalledItemDictionaries
+ in tvOS 10 and _MobileInstallationCopyInstalledAppsForLaunchServices in tvOS 9 to inject
+ our "whitelist" of applications that is created during uicache process.
+ 
+ 
+ */
+
+
+//tvOS 10 function for _MobileInstallationEnumerateAllInstalledItemDictionaries
+id (*original_func)(uint64_t a, id b);
+
+//tvOS 9 function for _MobileInstallationCopyInstalledAppsForLaunchServices
+id (*original_func2)(uint64_t a);
+
+//hook for _MobileInstallationCopyInstalledAppsForLaunchServices
+
+id we_really_out_here_nine(uint64_t a)
+{
+    
+    /*
+     
+     in tvOS 9 this list is just one huge plist that we need to inject ourselves into the
+     System key of.
+     
+     */
+    
+    id retv = original_func2(a);
+    
+    //read the list created in uicache of apps to inject
+    
+    NSString* pth = @"/var/mobile/Library/Preferences/kjc.appenabler.state.plist";
+    NSArray* arr = [NSArray arrayWithContentsOfFile: pth];
+    
+    if(!arr) return retv;
+
+    NSLog(@"got apps to inject!!");
+    
+    [retv autorelease];
+    retv = [retv mutableCopy];
+    
+    NSLog(@"retv allKeys:%@", [(NSDictionary*)retv allKeys]);
+    
+    //get the System key of the installed plist
+    id sysdb = retv[@"System"];
+    id pkp = retv[@"PluginKitPlugin"];
+    
+    if(sysdb){
+        NSLog(@"injecting to system apps");
+        sysdb=[sysdb mutableCopy];
+        pkp=[pkp mutableCopy];
+        retv[@"System"] = sysdb;
+        retv[@"PluginKitPlugin"] = pkp;
+        for (NSDictionary* appb in arr)
+        {
+            NSString *appType = appb[@"ApplicationType"];
+            if (![appType isEqualToString:@"PluginKitPlugin"])
+            {
+                NSLog(@"injecting %@", appb[@"CFBundleIdentifier"]);
+                if (sysdb[appb[@"CFBundleIdentifier"]])
+                {
+                    NSLog(@"already present!! WTF??? overwriting..");
+                }
+                //add current CFBundleIdentifier to System bundle
+                sysdb[appb[@"CFBundleIdentifier"]] = appb;
+            } else { //its a plugin
+                NSLog(@"injecting %@", appb[@"CFBundleIdentifier"]);
+                if (pkp[appb[@"CFBundleIdentifier"]])
+                {
+                    NSLog(@"already present!! WTF??? overwriting..");
+                }
+                //add current CFBundleIdentifier to System bundle
+                pkp[appb[@"CFBundleIdentifier"]] = appb;
+            }
+            
+        }
+    }
+    
+    //NSLog(@"return: %@",retv);
+    
+    //at this point we've injected our bundle ids into the System list/
+    //from here, some other process adds the entry to PBAppDepot for us! :)
+    
+    return retv;
+}
+
+
+//hook for _MobileInstallationEnumerateAllInstalledItemDictionaries
+
+id we_really_out_here(uint64_t a, uint64_t (^block)(id b1, uint64_t b2, uint64_t b3))
+{
+	//NSLog(@"Stack trace : %@",[NSThread callStackSymbols]);
+
+    //define the return block
+	uint64_t (^blockz)(id b1, uint64_t b2, uint64_t b3) = ^(id b1, uint64_t b2, uint64_t b3){
+		//NSLog(@"called!!! %@ %llx %llx", b1, b2, b3);
+		return block(b1, b2, b3);
+	};
+
+    //load our whitelist that is created in uicache
+	NSString* pth = @"/var/mobile/Library/Preferences/kjc.appenabler.state.plist";
+	NSArray* arr = [NSArray arrayWithContentsOfFile: pth];
+
+    //get original return value
+	id retv = original_func(a, blockz);
+	//NSLog(@"crack out of coke");
+
+    //loop through our apps an call the block to add the application to the list,
+    //from here it will be added to PBAppDepot automatically, but in a disabled state.
+	for (NSDictionary* appb in arr)
+	{
+		//blockz(@{@"ApplicationType": @"System", @"CFBundleIdentifier": appb[0], @"CodeInfoIdentifier": appb[0], @"Path": appb[1]},0,0);
+		blockz(appb,0,0);
+	}
+
+
+	return retv;
+}
+
+%ctor
+{
+	dlopen("/System/Library/PrivateFrameworks/MobileInstallation.framework/MobileInstallation", RTLD_LAZY);
+	
+	MSImageRef gangshit = MSGetImageByName("/System/Library/PrivateFrameworks/MobileInstallation.framework/MobileInstallation");
+
+    //tvos 10 symbol
+	void* weouthere = MSFindSymbol(gangshit, "_MobileInstallationEnumerateAllInstalledItemDictionaries");
+	NSLog(@"tvOS 10 hooking %p", weouthere);
+
+	if (weouthere)
+	{
+		MSHookFunction((void*)weouthere, (void*)we_really_out_here, (void**)&original_func);
+		//void (*lolz)() = weouthere;
+		//lolz();
+	}
+
+    //tvos 9 symbol
+    void* weouthere2 = MSFindSymbol(gangshit, "_MobileInstallationCopyInstalledAppsForLaunchServices");
+    NSLog(@"ios 9 hooking %p", weouthere2);
+    
+    if (weouthere2)
+    {
+        MSHookFunction((void*)weouthere2, (void*)we_really_out_here_nine, (void**)&original_func2);
+        //void (*lolz)() = weouthere;
+        //lolz();
+    }
+
+    
+} 

+ 29 - 0
Makefile

@@ -0,0 +1,29 @@
+ARCHS=arm64
+TARGET=appletv
+export GO_EASY_ON_ME=1
+export SDKVERSION=10.2
+THEOS_DEVICE_IP=apple-tv.local
+
+include theos/makefiles/common.mk
+
+LIBRARY_NAME = librespring
+librespring_FILES = main.m PBReloadHelper.m
+librespring_LIBRARIES = substrate
+#librespring_LIBRARIES += rocketbootstrap
+librespring_FRAMEWORKS = Foundation UIKit CoreGraphics
+librespring_Frameworks += WebCore ImageIO Foundation CFNetwork AppSupport UIKit IOKit MobileCoreServices
+librespring_PRIVATE_FRAMEWORKS = GraphicsServices FMCore
+
+include $(THEOS_MAKE_PATH)/library.mk
+
+SUBPROJECTS += uicache
+SUBPROJECTS += AppEnabler
+include $(THEOS_MAKE_PATH)/aggregate.mk
+
+after-stage::
+	#cp $(FW_STAGING_DIR)/usr/bin/uicache ../layout/usr/bin/uicache
+	#cp $(FW_STAGING_DIR)/Library/MobileSubstrate/DynamicLibraries/AppEnabler.dylib ../layout/Library/MobileSubstrate/DynamicLibraries/AppEnabler.dylib
+	#cp $(FW_STAGING_DIR)/Library/MobileSubstrate/DynamicLibraries/AppEnabler.plist ../layout/Library/MobileSubstrate/DynamicLibraries/AppEnabler.plist
+	#cp $(FW_STAGING_DIR)/usr/lib/librespring.dylib ../layout/usr/lib/librespring.dylib
+	cp rmcache $(THEOS_STAGING_DIR)/usr/bin/
+	#cp rmcache ../layout/usr/bin/rmcache

+ 15 - 0
PBReloadHelper.h

@@ -0,0 +1,15 @@
+//
+//  PBReloadHelper.h
+//  ATVWebRemote
+//
+//  Created by Kevin Bradley on 3/21/17.
+//  Copyright © 2017 nito. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+@interface PBReloadHelper : NSObject
+
++ (void)reloadApplications;
+-(void)runAppEnabler;
+@end

+ 578 - 0
PBReloadHelper.m

@@ -0,0 +1,578 @@
+//
+//  PBReloadHelper.m
+//  ATVWebRemote
+//
+//  Created by Kevin Bradley on 3/21/17.
+//  Copyright © 2017 nito. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#import "NSTask.h"
+#import <objc/runtime.h>
+#import "rocketbootstrap.h"
+#import "AppSupport/CPDistributedMessagingCenter.h"
+
+@interface LSApplicationProxy : NSObject
+
+@property (nonatomic,readonly) NSURL * bundleURL;
+@property (nonatomic,readonly) NSString * bundleIdentifier;
+@property (nonatomic,readonly) NSString * bundleType;                                                                 //@synthesize bundleType=_bundleType - In the implementation block
+@property (nonatomic,readonly) NSString * localizedShortName;
+@end
+
+@interface NSDistributedNotificationCenter : NSNotificationCenter
+
++ (id)defaultCenter;
+
+- (void)addObserver:(id)arg1 selector:(SEL)arg2 name:(id)arg3 object:(id)arg4;
+- (void)postNotificationName:(id)arg1 object:(id)arg2 userInfo:(id)arg3;
+
+@end
+
+@interface PBSMutableAppState : NSObject
+-(void)setEnabled:(BOOL)arg1 ;
+-(id)initWithApplicationIdentifer:(id)arg1 ;
+-(BOOL)isEnabled;
+@end
+
+@interface LSApplicationWorkspace : NSObject
++ (id) defaultWorkspace;
+- (void)_LSClearSchemaCaches;
+- (_Bool)_LSPrivateRebuildApplicationDatabasesForSystemApps:(_Bool)arg1 internal:(_Bool)arg2 user:(_Bool)arg3;
+-(id)allInstalledApplications;
+-(BOOL)_LSPrivateSyncWithMobileInstallation;
+-(BOOL)registerApplicationDictionary:(id)arg1 withObserverNotification:(int)arg2;
+-(BOOL)registerApplicationDictionary:(id)arg1 ;
+-(BOOL)registerApplication:(id)arg1 ;
+-(BOOL)unregisterApplication:(id)arg1 ;
+-(BOOL)registerPlugin:(id)arg1 ;
+-(BOOL)unregisterPlugin:(id)arg1 ;
+-(id)installedPlugins;
+@end
+
+@interface PBAppDepot : NSObject
++ (id)sharedInstance;
+@property(retain, nonatomic) NSMutableDictionary *internalAppState;
+- (id)_addAppStateForIdentifier:(id)arg1;
+- (void)_save;
+- (void)_setNeedsNotifyAppStateDidChange;
+- (void)_removeAppStateForIdentifier:(id)arg1;
+@end
+
+
+#import "PBReloadHelper.h"
+
+
+
+
+@implementation PBReloadHelper
+
+
+
++ (void)captureScreenshot
+{
+    //CALayer *layer;
+    // UIWindow *win = [[UIApplication sharedApplication] keyWindow];
+    //layer = win.layer;
+    //UIGraphicsBeginImageContextWithOptions(win.bounds.size, NO, 1.0f);
+    //[layer.presentationLayer renderInContext:UIGraphicsGetCurrentContext()];
+    //UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
+    
+    
+    //return screenImage;
+}
+
+-(void)runAppEnabler
+{
+    //NSLog(@"#### for luck!");
+    [PBReloadHelper reloadApplications];
+}
+
+/**
+ 
+ On tvOS 10+ we need to manually remove the applications from PBAppDepot after moving / removing
+ them from the /var/mobile/Applications folder. This method cycles filters allInstalledApplications 
+ in the application workspace to only applications with the bundleURL string containing
+ /var/mobile/Applications/
+ 
+ loop through them and see which ones actually exist, if they dont, add the bundle ID to the 
+ deleted array that is returned.
+ 
+ */
+
++ (BOOL)containsNewApplication:(NSArray *)appIds
+{
+    Class WSCLASS = objc_getClass("LSApplicationWorkspace");
+    
+    id workspace = nil;
+    if (WSCLASS != nil)
+    {
+        workspace = [WSCLASS defaultWorkspace];
+    }
+    __block NSMutableArray *installedArray = [NSMutableArray new];
+    __block BOOL containsApplication = NO;
+    NSArray *installedApplications = [workspace allInstalledApplications];
+    
+    NSPredicate *pred = [NSPredicate predicateWithFormat:@"self.bundleURL.absoluteString contains[cd] %@ or self.applicationIdentifier contains[cd] %@ ", @"/var/mobile/Applications/", @"com.nito.nitoTV4"];
+    NSArray <LSApplicationProxy *> *mobileApplications = [installedApplications filteredArrayUsingPredicate:pred];
+    //NSLog(@"new array: %@", newArray);
+    
+    NSFileManager *man = [NSFileManager defaultManager];
+    
+    [mobileApplications enumerateObjectsUsingBlock:^(LSApplicationProxy *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+        
+        
+        NSString *appPath = [[obj bundleURL] path];
+        //NSLog(@"thepath: %@", appPath);
+        if ([man fileExistsAtPath:appPath])
+        {
+            //NSLog(@"app removed: %@", [obj bundleIdentifier]);
+            [installedArray addObject:[obj bundleIdentifier]];
+        }
+        
+        
+    }];
+    
+    [appIds enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+        
+        
+        if (![installedArray containsObject:obj])
+        {
+            *stop = YES;
+            containsApplication = YES;
+        }
+        
+    }];
+    
+    return containsApplication;
+}
+
+
++ (NSArray *)deletedApplications
+{
+    Class WSCLASS = objc_getClass("LSApplicationWorkspace");
+    
+    id workspace = nil;
+    if (WSCLASS != nil)
+    {
+        workspace = [WSCLASS defaultWorkspace];
+    }
+    __block NSMutableArray *deletedArray = [NSMutableArray new];
+    NSArray *installedApplications = [workspace allInstalledApplications];
+    
+    NSPredicate *pred = [NSPredicate predicateWithFormat:@"self.bundleURL.absoluteString contains[cd] %@ or self.applicationIdentifier contains[cd] %@ ", @"/var/mobile/Applications/", @"com.nito.nitoTV4"];
+    NSArray <LSApplicationProxy *> *mobileApplications = [installedApplications filteredArrayUsingPredicate:pred];
+    //NSLog(@"new array: %@", newArray);
+    
+    NSFileManager *man = [NSFileManager defaultManager];
+    
+    [mobileApplications enumerateObjectsUsingBlock:^(LSApplicationProxy *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+        
+        
+        NSString *appPath = [[obj bundleURL] path];
+        //NSLog(@"thepath: %@", appPath);
+        if (![man fileExistsAtPath:appPath])
+        {
+            NSLog(@"app removed: %@", [obj bundleIdentifier]);
+            [deletedArray addObject:[obj bundleIdentifier]];
+        }
+        
+        
+    }];
+    
+    return deletedArray;
+}
+
++ (void)reloadApplications
+{
+    //at the moment in tvOS 10 morpheus extracts the bintools to /tmp rather than root, so .. for now, it could be in /tmp
+    NSFileManager *manager = [NSFileManager defaultManager];
+    NSString *whichKill = @"/tmp/usr/bin/killall";
+    if (![manager fileExistsAtPath:whichKill])
+    {
+        whichKill = @"/usr/bin/killall";
+    }
+    
+    /*
+     
+     PBAppDepot is where PineBoard keeps track of all the applications that are installed
+     and whether or not they are new or enabled or whatever. PineBoardServices appears to communicate
+     with the Depot to some degree over an XPC service that i couldn't quite figure out, a more
+     /proper/ way to do this is probably through there.
+     
+     https://github.com/lechium/tvOS10Headers/blob/master/System/Library/PrivateFrameworks/PineBoardServices.framework/PBSAppDepotProxy.h
+     
+     the internalAppState is an NSDictionary where each key / value pair is the app id / PBMutableAppState
+     that is related to each installed application.
+     
+     i.e.
+     `{
+     ...
+     "com.Moballo.tvbrowser" = "PBSMutableAppState(identifier=com.Moballo.tvbrowser, badge=(null), badgeEnabled=1, recent=0, cacheDeleting=0, enabled=1, notifs=0)";
+     "com.apple.AdSheetPhone" = "PBSMutableAppState(identifier=com.apple.AdSheetPhone, badge=(null), badgeEnabled=1, recent=0, cacheDeleting=0, enabled=1, notifs=0)";
+     "com.apple.CloudKit.ShareBear" = "PBSMutableAppState(identifier=com.apple.CloudKit.ShareBear, badge=(null), badgeEnabled=1, recent=0, cacheDeleting=0, enabled=1, notifs=0)";
+     "com.apple.DiagnosticsService" = "PBSMutableAppState(identifier=com.apple.DiagnosticsService, badge=(null), badgeEnabled=1, recent=0, cacheDeleting=0, enabled=1, notifs=0)";
+     ...
+     }
+     
+     
+     */
+    id appDepot = [objc_getClass("PBAppDepot") sharedInstance];
+    NSMutableDictionary *installedAppStates = [appDepot internalAppState];
+    NSLog(@"installedAppStates: %@", installedAppStates);
+    id nitoTV = [installedAppStates objectForKey:@"com.nito.nitoTV4"];
+    NSError *error = nil;
+    NSString *whitelistFile = @"/var/mobile/Library/Preferences/com.nito.whitelist.plist";
+    NSString *recentlyDeleted = @"/var/mobile/Library/Preferences/com.nito.deleted.plist";
+    NSArray *whiteList = [NSArray arrayWithContentsOfFile:whitelistFile];
+    NSMutableArray *recentDeletions = [NSMutableArray new];
+    NSMutableArray *mutableWhiteList = [whiteList mutableCopy];
+    BOOL addNito = [manager fileExistsAtPath:@"/Applications/nitoTV.app/nitoTV"];
+    if (whiteList == nil){
+        
+        if (addNito)
+        {
+            whiteList = @[@"com.nito.nitoTV4"];
+            mutableWhiteList = [whiteList mutableCopy];
+            
+        } else {
+            mutableWhiteList = [NSMutableArray new];
+        }
+    } else {
+        if (addNito)
+        {
+            NSLog(@"mutableWhiteList: %@", mutableWhiteList);
+            if (![mutableWhiteList containsObject:@"com.nito.nitoTV4"]){
+                [mutableWhiteList addObject:@"com.nito.nitoTV4"];
+            }
+        }
+    }
+    
+    /*
+     
+     Loop through our /@"/var/mobile/Applications" folder and add them to a new plist
+     file that will be injected inside the MobileInstallation framework when loading
+     the list of applications that are installed.
+     
+     
+     */
+    
+    NSString *applicationPath = @"/var/mobile/Applications";
+    
+    
+    NSMutableArray *appArray = [NSMutableArray new];
+    NSMutableArray *identifierArray = [NSMutableArray new];
+    BOOL addedApps = FALSE;
+    NSArray *apps = [manager contentsOfDirectoryAtPath:applicationPath error:&error];
+    NSArray *defaultApps = [manager contentsOfDirectoryAtPath:@"/Applications" error:&error];
+    
+    for (NSString *app in apps)
+    {
+        if ([app hasSuffix:@".app"]) {
+            
+            NSString *path = [applicationPath stringByAppendingPathComponent:app];
+            NSString *newPl = [path stringByAppendingPathComponent:@"Info.plist"];
+            
+            NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:newPl];
+            //NSLog(@"info: %@", info);
+            if (info != nil) {
+                NSString *identifier = [info objectForKey:@"CFBundleIdentifier"];
+                if(identifier != nil)  {
+                    
+                    if (![mutableWhiteList containsObject:identifier])
+                    {
+                        [mutableWhiteList addObject:identifier];
+                    }
+                    
+                    
+                    NSDictionary *appDict = @{@"ApplicationType": @"System", @"CFBundleIdentifier": identifier, @"CodeInfoIdentifier": identifier, @"Path": path};
+                    
+                    [appArray addObject:appDict];
+                    [identifierArray addObject:identifier];
+                    addedApps = YES;
+                    NSString *appPluginPath = [path stringByAppendingPathComponent:@"PlugIns"];
+                    if ([manager fileExistsAtPath:appPluginPath])
+                    {
+                        NSLog(@"found plugin folder for id: %@", identifier );
+                        NSArray <NSString *> *plugins = [manager contentsOfDirectoryAtPath:appPluginPath error:&error];
+                        [plugins enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+                            
+                            if ([[obj pathExtension] isEqualToString:@"appex"])
+                            {
+                                
+                                NSString *pluginPath = [appPluginPath stringByAppendingPathComponent:obj];
+                                
+                                //NSLog(@"found appex: %@", pluginPath);
+                                
+                                NSString *pluginPl = [pluginPath stringByAppendingPathComponent:@"Info.plist"];
+                                
+                                NSMutableDictionary *pluginInfo = [NSMutableDictionary dictionaryWithContentsOfFile:pluginPl];
+                                //NSLog(@"pluginInfo: %@", pluginInfo);
+                                if (pluginInfo != nil) {
+                                    NSString *pluginID = [pluginInfo objectForKey:@"CFBundleIdentifier"];
+                                    
+                                    NSDictionary *pluginDict = @{@"ApplicationType": @"PluginKitPlugin", @"CFBundleIdentifier": pluginID, @"CodeInfoIdentifier": pluginID, @"Path": pluginPath, @"PluginOwnerBundleID": identifier};
+                                    [appArray addObject:pluginDict];
+                                    
+                                }
+                            }
+                            
+                        }];
+                    }
+                    
+                    
+                }
+            }
+        }
+    }
+    
+    if (nitoTV == nil && addNito)
+    {
+        NSLog(@"nitoTV has never been loaded before! force it to load");
+        NSString *path = @"/Applications/nitoTV.app";
+        NSString *newPl = [path stringByAppendingPathComponent:@"Info.plist"];
+        
+        NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:newPl];
+        //NSLog(@"info: %@", info);
+        if (info != nil) {
+            NSString *identifier = [info objectForKey:@"CFBundleIdentifier"];
+            if(identifier != nil)  {
+                
+                if (![mutableWhiteList containsObject:identifier])
+                {
+                    [mutableWhiteList addObject:identifier];
+                }
+                
+                
+                NSDictionary *appDict = @{@"ApplicationType": @"System", @"CFBundleIdentifier": identifier, @"CodeInfoIdentifier": identifier, @"Path": path};
+                
+                [appArray addObject:appDict];
+                [identifierArray addObject:identifier];
+                addedApps = YES;
+                NSString *appPluginPath = [path stringByAppendingPathComponent:@"PlugIns"];
+                if ([manager fileExistsAtPath:appPluginPath])
+                {
+                    NSLog(@"found plugin folder for id: %@", identifier );
+                    NSArray <NSString *> *plugins = [manager contentsOfDirectoryAtPath:appPluginPath error:&error];
+                    [plugins enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+                        
+                        if ([[obj pathExtension] isEqualToString:@"appex"])
+                        {
+                            
+                            NSString *pluginPath = [appPluginPath stringByAppendingPathComponent:obj];
+                            
+                            //NSLog(@"found appex: %@", pluginPath);
+                            
+                            NSString *pluginPl = [pluginPath stringByAppendingPathComponent:@"Info.plist"];
+                            
+                            NSMutableDictionary *pluginInfo = [NSMutableDictionary dictionaryWithContentsOfFile:pluginPl];
+                            //NSLog(@"pluginInfo: %@", pluginInfo);
+                            if (pluginInfo != nil) {
+                                NSString *pluginID = [pluginInfo objectForKey:@"CFBundleIdentifier"];
+                                
+                                NSDictionary *pluginDict = @{@"ApplicationType": @"PluginKitPlugin", @"CFBundleIdentifier": pluginID, @"CodeInfoIdentifier": pluginID, @"Path": pluginPath, @"PluginOwnerBundleID": identifier};
+                                [appArray addObject:pluginDict];
+                                
+                            }
+                        }
+                        
+                    }];
+                }
+                
+                
+            }
+        }
+    }
+            
+
+        
+    
+    //the file that we store the installed applications in
+    NSString* pth = @"/var/mobile/Library/Preferences/kjc.appenabler.state.plist";
+    [appArray writeToFile:pth atomically:YES];
+    
+    
+    Class WSCLASS = objc_getClass("LSApplicationWorkspace");
+    
+    id workspace = nil;
+    if (WSCLASS != nil)
+    {
+        workspace = [WSCLASS defaultWorkspace];
+    }
+    
+  
+    
+    int is9 = 0;
+    //tvOS 10+
+    
+
+    NSArray *deletedApps = [self deletedApplications];
+    BOOL removedApps = ([deletedApps count] > 0);
+    
+    if ([workspace respondsToSelector:@selector(_LSPrivateSyncWithMobileInstallation)]){
+        if (addedApps || removedApps){
+            
+            //rm /var/containers/Data/System/97D6E4BA-C0BF-408B-AF63-1836844381AE/Library/Caches/com.apple.LaunchServices-175-v2.csstore
+            
+            //delete the csstore file, its run through bash so the wildcards are completed properly
+            
+            //[NSTask launchedTaskWithLaunchPath:@"/bin/bash" arguments:@[@"/bin/rm", @"/var/containers/Data/System/*/Library/Caches/com.apple.LaunchServices*.csstore"]];
+            
+            //FIXME: i dont like calling this chmod +x shell script
+            //its probably a security risk of some type, but the line above
+            //didnt work properly and i couldnt figure out why
+            
+            [NSTask launchedTaskWithLaunchPath:@"/usr/bin/rmcache" arguments:@[]];
+            
+            if (removedApps)
+            {
+                
+                NSLog(@"app ids were deleted: %@", deletedApps);
+                [deletedApps enumerateObjectsUsingBlock:^(NSString *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+                    
+                    [mutableWhiteList removeObject:obj];
+                    [recentDeletions addObject:obj];
+                    NSLog(@"removing app state with id: %@", obj);
+                    [appDepot _removeAppStateForIdentifier:obj];
+                    
+                    
+                    
+                }];
+            } else {
+                //no removed apps, need to make sure they don't mess with our reload anymore
+                [[NSFileManager defaultManager] removeItemAtPath:recentlyDeleted error:nil];
+            }
+            
+           
+         
+            /*
+             
+             _LSPrivateSyncWithMobileInstallation will actually display the application and allow you to launch it, but the AppDepot (or something related to the installed app database) is potentially harshed or out of sync after we do this, which is why we always call the killall at the very bottom of this method, also if apps are 
+             deleted their state is not updated properly without a respring either
+             
+             */
+            if((addedApps || removedApps) && [self containsNewApplication:identifierArray]){
+                
+                NSLog(@"we can haz new app");
+                
+                [workspace _LSClearSchemaCaches]; //may or may not be necessary
+                [workspace _LSPrivateRebuildApplicationDatabasesForSystemApps:YES internal:YES user:YES];
+                [workspace _LSPrivateSyncWithMobileInstallation];
+            }
+        }
+    } else {
+        is9 = 1;
+        //tvOS 9
+        
+        /*
+         
+         lil bit of extra work for tvOS 9, we completely torch the lsd csstore file,
+        
+         from there we call a distributed notification that is relayed back to the uicache
+         binary
+         
+         as far as i can remember we need to call back to uicache binary to trigger the workspace
+         methods for recaching applications because of entitlement issues, our uicache
+         binary that triggers these calls inside of Pineboard has the proper entitlements to
+         make those calls, while Pineboard doesnt?
+         
+         
+         
+         */
+        
+        
+        //clear cache file
+        NSString *file = @"/var/mobile/Library/Caches/com.apple.LaunchServices-135.csstore";
+        NSError *removeError = nil;
+        [manager removeItemAtPath:file error:&removeError];
+        
+        [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"kjc.AppEnabler.rebuild_appdb" object:nil userInfo:nil];
+    }
+    
+    [mutableWhiteList writeToFile:whitelistFile atomically:YES];
+    
+    [recentDeletions writeToFile:recentlyDeleted atomically:YES];
+    
+    /* 
+     
+     this is a kludgy hack, we need to wait some time while our code is triggered in
+     the AppEnabler tweak that injects into MobileInstallation.
+     
+     the code is triggered by _LSPrivateSyncWithMobileInstallation in tvOS 10+
+     and _LSPrivateRebuildApplicationDatabasesForSystemApps:* in tvOS 9 respectively.
+     
+     after that tweak does its magic our applications should be added (or removed) from PBAppDepot
+     
+     however, at that point the applications are added but still need to be enabled in the depot
+     
+     below we loop through every application in appArray (all apps in /var/mobile/Applications)
+     and make sure all of their states are set to enabled in the PBAppDepot.
+     
+     At that point in tvOS 10+ we still need to 'respring' to get everything loaded and working
+     without any issues.
+     
+     in tvOS 9 we don't need to respring, things should appear / disappear without any 
+     extra work.
+     
+     */
+    
+    if(addedApps && [self containsNewApplication:identifierArray]){
+        
+        NSLog(@"we can haz new app");
+    
+    sleep(5);
+    
+    
+    for (NSDictionary* o in appArray)
+    {
+        char retry=0;
+        NSString* identifier = o[@"CFBundleIdentifier"];
+    retry_:
+        NSLog(@"identifier: %@", identifier);
+        
+        id existingObject = [installedAppStates objectForKey:identifier];
+        if (existingObject != nil)
+        {
+            [existingObject setValue:[NSNumber numberWithBool:YES] forKey:@"_enabled"];
+            
+            BOOL isEnabled = [existingObject isEnabled];
+            
+            
+            //[existingObject setEnabled:YES];
+            //[installedAppStates setObject: existingObject forKey: identifier];
+        } else {
+            //[appDepot _addAppStateForIdentifier:identifier];
+            NSLog(@"didnt find object for key: %@, error!!!!!!", identifier);
+            if(retry == 0)
+            {
+                NSLog(@"retry");
+                //NSLog(@"try to manually kick start substrate!");
+                //[NSTask launchedTaskWithLaunchPath:@"/usr/bin/nitoHelper" arguments:@[@"substrate", @"1", @"2"]];
+                //is9 = false;
+                
+                ///usr/bin/cynject 1 /Library/Frameworks/CydiaSubstrate.framework/Libraries/SubstrateLauncher.dylib
+                retry = 1;
+                goto retry_;
+            }
+        }
+    }
+    }
+    //this notification is received by uicache binary so its knows that its safe to exit the runloop it creates so it can relay notifications back and forth during this convoluted process.
+    
+    [[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"kjc.AppEnabler.done" object:nil userInfo:nil];
+    
+    //it beats killall -u mobile... right?
+    //we dont have system() call anymore and posix_spawn is a pain to configure.
+    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
+        if (!is9){
+            [NSTask launchedTaskWithLaunchPath:whichKill arguments:@[@"-9", @"PineBoard", @"HeadBoard", @"lsd"]];
+        }
+    });
+    
+    
+    
+    
+}
+
+@end

+ 21 - 0
Tweak.xm

@@ -0,0 +1,21 @@
+#import "PBReloadHelper.h"
+
+%class PBAppDelegate
+%ctor {
+	%init;
+	
+	//	[PBReloadHelper reloadApplications];
+    Class pbad = NSClassFromString(@"PBAppDelegate");
+    Class rth = NSClassFromString(@"RemoteTestHelper");
+    PBReloadHelper *rmh = [PBReloadHelper sharedInstance];
+    //[rmh setPbDelegateRef:self];
+    
+    id center = [objc_getClass("CPDistributedMessagingCenter") centerNamed:@"com.kjc.AppEnabler"];
+    rocketbootstrap_distributedmessagingcenter_apply(center);
+    [center runServerOnCurrentThread];
+    
+     [center registerForMessageName:@"com.kjc.AppEnabler.run" target:rmh selector:@selector(runAppEnabler)];
+    
+    
+    
+	}

+ 7 - 0
com.nito.whitelist.plist

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<array>
+	<string>com.nito.nitoTV4</string>
+</array>
+</plist>

+ 10 - 0
control

@@ -0,0 +1,10 @@
+Package: com.nito.uicache
+Name: uicache
+Pre-Depends: cy+model.appletv
+Depends: mobilesubstrate
+Version: 0.0.1
+Architecture: appletvos-arm64
+Description: Respring and reload any applications added to /Applications
+Maintainer: Kevin Bradley
+Author: Kevin Bradley
+Section: Library

+ 38 - 0
main.m

@@ -0,0 +1,38 @@
+#import "PBReloadHelper.h"
+#import <Foundation/Foundation.h>
+#import <objc/runtime.h>
+#import "rocketbootstrap.h"
+#import "AppSupport/CPDistributedMessagingCenter.h"
+
+@interface NSDistributedNotificationCenter : NSNotificationCenter
+
++ (id)defaultCenter;
+
+- (void)addObserver:(id)arg1 selector:(SEL)arg2 name:(id)arg3 object:(id)arg4;
+- (void)postNotificationName:(id)arg1 object:(id)arg2 userInfo:(id)arg3;
+
+@end
+
+@interface MessageHandler : NSObject
+-(void)runAppEnabler:(id)a;
+@end
+@implementation MessageHandler
+
+-(void)runAppEnabler:(id)a
+{
+    //NSLog(@"#### for luck!");
+    [PBReloadHelper reloadApplications];
+}
+
+@end
+__attribute__ ((__constructor__))
+void pre_func(void) {
+    //NSLog(@"pre_func\n");
+    
+    dispatch_async(dispatch_get_main_queue(), ^(void){
+        NSDistributedNotificationCenter* notificationCenter;
+        notificationCenter = [NSDistributedNotificationCenter defaultCenter];
+        [notificationCenter addObserver:[MessageHandler new]  selector:@selector(runAppEnabler:) name:@"kjc.AppEnabler.recache" object:nil];
+        
+    });
+}

+ 4 - 0
rmcache

@@ -0,0 +1,4 @@
+#!/bin/bash
+
+rm /var/containers/Data/System/*/Library/Caches/com.apple.LaunchServices*.csstore
+

+ 1 - 0
theos/Defines.h

@@ -0,0 +1 @@
+../Defines.h

+ 609 - 0
theos/LICENSE.md

@@ -0,0 +1,609 @@
+Theos (and by extension, Logos) is available under the provisions of the GNU
+General Public License, version 3 (or later), available here:
+http://www.gnu.org/licenses/gpl-3.0.html.
+
+Projects created using Theos and/or Logos are not considered derivative works
+(from a licensing standpoint, or, for that matter, any other standpoint) and
+are, as such, not required to be licensed under the GNU GPL.
+
+The included project templates are license-free. The use of a template does
+not confer a license to your project.
+
+----
+
+GNU GENERAL PUBLIC LICENSE
+==========================
+
+Version 3, 29 June 2007
+
+Copyright © 2007 Free Software Foundation, Inc. &lt;<http://fsf.org/>&gt;
+
+Everyone is permitted to copy and distribute verbatim copies of this license
+document, but changing it is not allowed.
+
+## Preamble
+
+The GNU General Public License is a free, copyleft license for software and other
+kinds of works.
+
+The licenses for most software and other practical works are designed to take away
+your freedom to share and change the works. By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change all versions of a
+program--to make sure it remains free software for all its users. We, the Free
+Software Foundation, use the GNU General Public License for most of our software; it
+applies also to any other work released this way by its authors. You can apply it to
+your programs, too.
+
+When we speak of free software, we are referring to freedom, not price. Our General
+Public Licenses are designed to make sure that you have the freedom to distribute
+copies of free software (and charge for them if you wish), that you receive source
+code or can get it if you want it, that you can change the software or use pieces of
+it in new free programs, and that you know you can do these things.
+
+To protect your rights, we need to prevent others from denying you these rights or
+asking you to surrender the rights. Therefore, you have certain responsibilities if
+you distribute copies of the software, or if you modify it: responsibilities to
+respect the freedom of others.
+
+For example, if you distribute copies of such a program, whether gratis or for a fee,
+you must pass on to the recipients the same freedoms that you received. You must make
+sure that they, too, receive or can get the source code. And you must show them these
+terms so they know their rights.
+
+Developers that use the GNU GPL protect your rights with two steps: (1) assert
+copyright on the software, and (2) offer you this License giving you legal permission
+to copy, distribute and/or modify it.
+
+For the developers' and authors' protection, the GPL clearly explains that there is
+no warranty for this free software. For both users' and authors' sake, the GPL
+requires that modified versions be marked as changed, so that their problems will not
+be attributed erroneously to authors of previous versions.
+
+Some devices are designed to deny users access to install or run modified versions of
+the software inside them, although the manufacturer can do so. This is fundamentally
+incompatible with the aim of protecting users' freedom to change the software. The
+systematic pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable. Therefore, we have designed
+this version of the GPL to prohibit the practice for those products. If such problems
+arise substantially in other domains, we stand ready to extend this provision to
+those domains in future versions of the GPL, as needed to protect the freedom of
+users.
+
+Finally, every program is threatened constantly by software patents. States should
+not allow patents to restrict development and use of software on general-purpose
+computers, but in those that do, we wish to avoid the special danger that patents
+applied to a free program could make it effectively proprietary. To prevent this, the
+GPL assures that patents cannot be used to render the program non-free.
+
+The precise terms and conditions for copying, distribution and modification follow.
+
+## TERMS AND CONDITIONS
+
+### 0. Definitions.
+
+“This License” refers to version 3 of the GNU General Public License.
+
+“Copyright” also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+“The Program” refers to any copyrightable work licensed under this
+License. Each licensee is addressed as “you”. “Licensees” and
+“recipients” may be individuals or organizations.
+
+To “modify” a work means to copy from or adapt all or part of the work in
+a fashion requiring copyright permission, other than the making of an exact copy. The
+resulting work is called a “modified version” of the earlier work or a
+work “based on” the earlier work.
+
+A “covered work” means either the unmodified Program or a work based on
+the Program.
+
+To “propagate” a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for infringement under
+applicable copyright law, except executing it on a computer or modifying a private
+copy. Propagation includes copying, distribution (with or without modification),
+making available to the public, and in some countries other activities as well.
+
+To “convey” a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through a computer
+network, with no transfer of a copy, is not conveying.
+
+An interactive user interface displays “Appropriate Legal Notices” to the
+extent that it includes a convenient and prominently visible feature that (1)
+displays an appropriate copyright notice, and (2) tells the user that there is no
+warranty for the work (except to the extent that warranties are provided), that
+licensees may convey the work under this License, and how to view a copy of this
+License. If the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+### 1. Source Code.
+
+The “source code” for a work means the preferred form of the work for
+making modifications to it. “Object code” means any non-source form of a
+work.
+
+A “Standard Interface” means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of interfaces
+specified for a particular programming language, one that is widely used among
+developers working in that language.
+
+The “System Libraries” of an executable work include anything, other than
+the work as a whole, that (a) is included in the normal form of packaging a Major
+Component, but which is not part of that Major Component, and (b) serves only to
+enable use of the work with that Major Component, or to implement a Standard
+Interface for which an implementation is available to the public in source code form.
+A “Major Component”, in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system (if any) on which
+the executable work runs, or a compiler used to produce the work, or an object code
+interpreter used to run it.
+
+The “Corresponding Source” for a work in object code form means all the
+source code needed to generate, install, and (for an executable work) run the object
+code and to modify the work, including scripts to control those activities. However,
+it does not include the work's System Libraries, or general-purpose tools or
+generally available free programs which are used unmodified in performing those
+activities but which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for the work, and
+the source code for shared libraries and dynamically linked subprograms that the work
+is specifically designed to require, such as by intimate data communication or
+control flow between those subprograms and other parts of the work.
+
+The Corresponding Source need not include anything that users can regenerate
+automatically from other parts of the Corresponding Source.
+
+The Corresponding Source for a work in source code form is that same work.
+
+### 2. Basic Permissions.
+
+All rights granted under this License are granted for the term of copyright on the
+Program, and are irrevocable provided the stated conditions are met. This License
+explicitly affirms your unlimited permission to run the unmodified Program. The
+output from running a covered work is covered by this License only if the output,
+given its content, constitutes a covered work. This License acknowledges your rights
+of fair use or other equivalent, as provided by copyright law.
+
+You may make, run and propagate covered works that you do not convey, without
+conditions so long as your license otherwise remains in force. You may convey covered
+works to others for the sole purpose of having them make modifications exclusively
+for you, or provide you with facilities for running those works, provided that you
+comply with the terms of this License in conveying all material for which you do not
+control copyright. Those thus making or running the covered works for you must do so
+exclusively on your behalf, under your direction and control, on terms that prohibit
+them from making any copies of your copyrighted material outside their relationship
+with you.
+
+Conveying under any other circumstances is permitted solely under the conditions
+stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
+
+### 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+No covered work shall be deemed part of an effective technological measure under any
+applicable law fulfilling obligations under article 11 of the WIPO copyright treaty
+adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention
+of such measures.
+
+When you convey a covered work, you waive any legal power to forbid circumvention of
+technological measures to the extent such circumvention is effected by exercising
+rights under this License with respect to the covered work, and you disclaim any
+intention to limit operation or modification of the work as a means of enforcing,
+against the work's users, your or third parties' legal rights to forbid circumvention
+of technological measures.
+
+### 4. Conveying Verbatim Copies.
+
+You may convey verbatim copies of the Program's source code as you receive it, in any
+medium, provided that you conspicuously and appropriately publish on each copy an
+appropriate copyright notice; keep intact all notices stating that this License and
+any non-permissive terms added in accord with section 7 apply to the code; keep
+intact all notices of the absence of any warranty; and give all recipients a copy of
+this License along with the Program.
+
+You may charge any price or no price for each copy that you convey, and you may offer
+support or warranty protection for a fee.
+
+### 5. Conveying Modified Source Versions.
+
+You may convey a work based on the Program, or the modifications to produce it from
+the Program, in the form of source code under the terms of section 4, provided that
+you also meet all of these conditions:
+
+* **a)** The work must carry prominent notices stating that you modified it, and giving a
+relevant date.
+* **b)** The work must carry prominent notices stating that it is released under this
+License and any conditions added under section 7. This requirement modifies the
+requirement in section 4 to “keep intact all notices”.
+* **c)** You must license the entire work, as a whole, under this License to anyone who
+comes into possession of a copy. This License will therefore apply, along with any
+applicable section 7 additional terms, to the whole of the work, and all its parts,
+regardless of how they are packaged. This License gives no permission to license the
+work in any other way, but it does not invalidate such permission if you have
+separately received it.
+* **d)** If the work has interactive user interfaces, each must display Appropriate Legal
+Notices; however, if the Program has interactive interfaces that do not display
+Appropriate Legal Notices, your work need not make them do so.
+
+A compilation of a covered work with other separate and independent works, which are
+not by their nature extensions of the covered work, and which are not combined with
+it such as to form a larger program, in or on a volume of a storage or distribution
+medium, is called an “aggregate” if the compilation and its resulting
+copyright are not used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work in an aggregate
+does not cause this License to apply to the other parts of the aggregate.
+
+### 6. Conveying Non-Source Forms.
+
+You may convey a covered work in object code form under the terms of sections 4 and
+5, provided that you also convey the machine-readable Corresponding Source under the
+terms of this License, in one of these ways:
+
+* **a)** Convey the object code in, or embodied in, a physical product (including a
+physical distribution medium), accompanied by the Corresponding Source fixed on a
+durable physical medium customarily used for software interchange.
+* **b)** Convey the object code in, or embodied in, a physical product (including a
+physical distribution medium), accompanied by a written offer, valid for at least
+three years and valid for as long as you offer spare parts or customer support for
+that product model, to give anyone who possesses the object code either (1) a copy of
+the Corresponding Source for all the software in the product that is covered by this
+License, on a durable physical medium customarily used for software interchange, for
+a price no more than your reasonable cost of physically performing this conveying of
+source, or (2) access to copy the Corresponding Source from a network server at no
+charge.
+* **c)** Convey individual copies of the object code with a copy of the written offer to
+provide the Corresponding Source. This alternative is allowed only occasionally and
+noncommercially, and only if you received the object code with such an offer, in
+accord with subsection 6b.
+* **d)** Convey the object code by offering access from a designated place (gratis or for
+a charge), and offer equivalent access to the Corresponding Source in the same way
+through the same place at no further charge. You need not require recipients to copy
+the Corresponding Source along with the object code. If the place to copy the object
+code is a network server, the Corresponding Source may be on a different server
+(operated by you or a third party) that supports equivalent copying facilities,
+provided you maintain clear directions next to the object code saying where to find
+the Corresponding Source. Regardless of what server hosts the Corresponding Source,
+you remain obligated to ensure that it is available for as long as needed to satisfy
+these requirements.
+* **e)** Convey the object code using peer-to-peer transmission, provided you inform
+other peers where the object code and Corresponding Source of the work are being
+offered to the general public at no charge under subsection 6d.
+
+A separable portion of the object code, whose source code is excluded from the
+Corresponding Source as a System Library, need not be included in conveying the
+object code work.
+
+A “User Product” is either (1) a “consumer product”, which
+means any tangible personal property which is normally used for personal, family, or
+household purposes, or (2) anything designed or sold for incorporation into a
+dwelling. In determining whether a product is a consumer product, doubtful cases
+shall be resolved in favor of coverage. For a particular product received by a
+particular user, “normally used” refers to a typical or common use of
+that class of product, regardless of the status of the particular user or of the way
+in which the particular user actually uses, or expects or is expected to use, the
+product. A product is a consumer product regardless of whether the product has
+substantial commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+“Installation Information” for a User Product means any methods,
+procedures, authorization keys, or other information required to install and execute
+modified versions of a covered work in that User Product from a modified version of
+its Corresponding Source. The information must suffice to ensure that the continued
+functioning of the modified object code is in no case prevented or interfered with
+solely because modification has been made.
+
+If you convey an object code work under this section in, or with, or specifically for
+use in, a User Product, and the conveying occurs as part of a transaction in which
+the right of possession and use of the User Product is transferred to the recipient
+in perpetuity or for a fixed term (regardless of how the transaction is
+characterized), the Corresponding Source conveyed under this section must be
+accompanied by the Installation Information. But this requirement does not apply if
+neither you nor any third party retains the ability to install modified object code
+on the User Product (for example, the work has been installed in ROM).
+
+The requirement to provide Installation Information does not include a requirement to
+continue to provide support service, warranty, or updates for a work that has been
+modified or installed by the recipient, or for the User Product in which it has been
+modified or installed. Access to a network may be denied when the modification itself
+materially and adversely affects the operation of the network or violates the rules
+and protocols for communication across the network.
+
+Corresponding Source conveyed, and Installation Information provided, in accord with
+this section must be in a format that is publicly documented (and with an
+implementation available to the public in source code form), and must require no
+special password or key for unpacking, reading or copying.
+
+### 7. Additional Terms.
+
+“Additional permissions” are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions. Additional
+permissions that are applicable to the entire Program shall be treated as though they
+were included in this License, to the extent that they are valid under applicable
+law. If additional permissions apply only to part of the Program, that part may be
+used separately under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+When you convey a copy of a covered work, you may at your option remove any
+additional permissions from that copy, or from any part of it. (Additional
+permissions may be written to require their own removal in certain cases when you
+modify the work.) You may place additional permissions on material, added by you to a
+covered work, for which you have or can give appropriate copyright permission.
+
+Notwithstanding any other provision of this License, for material you add to a
+covered work, you may (if authorized by the copyright holders of that material)
+supplement the terms of this License with terms:
+
+* **a)** Disclaiming warranty or limiting liability differently from the terms of
+sections 15 and 16 of this License; or
+* **b)** Requiring preservation of specified reasonable legal notices or author
+attributions in that material or in the Appropriate Legal Notices displayed by works
+containing it; or
+* **c)** Prohibiting misrepresentation of the origin of that material, or requiring that
+modified versions of such material be marked in reasonable ways as different from the
+original version; or
+* **d)** Limiting the use for publicity purposes of names of licensors or authors of the
+material; or
+* **e)** Declining to grant rights under trademark law for use of some trade names,
+trademarks, or service marks; or
+* **f)** Requiring indemnification of licensors and authors of that material by anyone
+who conveys the material (or modified versions of it) with contractual assumptions of
+liability to the recipient, for any liability that these contractual assumptions
+directly impose on those licensors and authors.
+
+All other non-permissive additional terms are considered “further
+restrictions” within the meaning of section 10. If the Program as you received
+it, or any part of it, contains a notice stating that it is governed by this License
+along with a term that is a further restriction, you may remove that term. If a
+license document contains a further restriction but permits relicensing or conveying
+under this License, you may add to a covered work material governed by the terms of
+that license document, provided that the further restriction does not survive such
+relicensing or conveying.
+
+If you add terms to a covered work in accord with this section, you must place, in
+the relevant source files, a statement of the additional terms that apply to those
+files, or a notice indicating where to find the applicable terms.
+
+Additional terms, permissive or non-permissive, may be stated in the form of a
+separately written license, or stated as exceptions; the above requirements apply
+either way.
+
+### 8. Termination.
+
+You may not propagate or modify a covered work except as expressly provided under
+this License. Any attempt otherwise to propagate or modify it is void, and will
+automatically terminate your rights under this License (including any patent licenses
+granted under the third paragraph of section 11).
+
+However, if you cease all violation of this License, then your license from a
+particular copyright holder is reinstated (a) provisionally, unless and until the
+copyright holder explicitly and finally terminates your license, and (b) permanently,
+if the copyright holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+Moreover, your license from a particular copyright holder is reinstated permanently
+if the copyright holder notifies you of the violation by some reasonable means, this
+is the first time you have received notice of violation of this License (for any
+work) from that copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+Termination of your rights under this section does not terminate the licenses of
+parties who have received copies or rights from you under this License. If your
+rights have been terminated and not permanently reinstated, you do not qualify to
+receive new licenses for the same material under section 10.
+
+### 9. Acceptance Not Required for Having Copies.
+
+You are not required to accept this License in order to receive or run a copy of the
+Program. Ancillary propagation of a covered work occurring solely as a consequence of
+using peer-to-peer transmission to receive a copy likewise does not require
+acceptance. However, nothing other than this License grants you permission to
+propagate or modify any covered work. These actions infringe copyright if you do not
+accept this License. Therefore, by modifying or propagating a covered work, you
+indicate your acceptance of this License to do so.
+
+### 10. Automatic Licensing of Downstream Recipients.
+
+Each time you convey a covered work, the recipient automatically receives a license
+from the original licensors, to run, modify and propagate that work, subject to this
+License. You are not responsible for enforcing compliance by third parties with this
+License.
+
+An “entity transaction” is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an organization, or
+merging organizations. If propagation of a covered work results from an entity
+transaction, each party to that transaction who receives a copy of the work also
+receives whatever licenses to the work the party's predecessor in interest had or
+could give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if the predecessor
+has it or can get it with reasonable efforts.
+
+You may not impose any further restrictions on the exercise of the rights granted or
+affirmed under this License. For example, you may not impose a license fee, royalty,
+or other charge for exercise of rights granted under this License, and you may not
+initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging
+that any patent claim is infringed by making, using, selling, offering for sale, or
+importing the Program or any portion of it.
+
+### 11. Patents.
+
+A “contributor” is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The work thus
+licensed is called the contributor's “contributor version”.
+
+A contributor's “essential patent claims” are all patent claims owned or
+controlled by the contributor, whether already acquired or hereafter acquired, that
+would be infringed by some manner, permitted by this License, of making, using, or
+selling its contributor version, but do not include claims that would be infringed
+only as a consequence of further modification of the contributor version. For
+purposes of this definition, “control” includes the right to grant patent
+sublicenses in a manner consistent with the requirements of this License.
+
+Each contributor grants you a non-exclusive, worldwide, royalty-free patent license
+under the contributor's essential patent claims, to make, use, sell, offer for sale,
+import and otherwise run, modify and propagate the contents of its contributor
+version.
+
+In the following three paragraphs, a “patent license” is any express
+agreement or commitment, however denominated, not to enforce a patent (such as an
+express permission to practice a patent or covenant not to sue for patent
+infringement). To “grant” such a patent license to a party means to make
+such an agreement or commitment not to enforce a patent against the party.
+
+If you convey a covered work, knowingly relying on a patent license, and the
+Corresponding Source of the work is not available for anyone to copy, free of charge
+and under the terms of this License, through a publicly available network server or
+other readily accessible means, then you must either (1) cause the Corresponding
+Source to be so available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner consistent with
+the requirements of this License, to extend the patent license to downstream
+recipients. “Knowingly relying” means you have actual knowledge that, but
+for the patent license, your conveying the covered work in a country, or your
+recipient's use of the covered work in a country, would infringe one or more
+identifiable patents in that country that you have reason to believe are valid.
+
+If, pursuant to or in connection with a single transaction or arrangement, you
+convey, or propagate by procuring conveyance of, a covered work, and grant a patent
+license to some of the parties receiving the covered work authorizing them to use,
+propagate, modify or convey a specific copy of the covered work, then the patent
+license you grant is automatically extended to all recipients of the covered work and
+works based on it.
+
+A patent license is “discriminatory” if it does not include within the
+scope of its coverage, prohibits the exercise of, or is conditioned on the
+non-exercise of one or more of the rights that are specifically granted under this
+License. You may not convey a covered work if you are a party to an arrangement with
+a third party that is in the business of distributing software, under which you make
+payment to the third party based on the extent of your activity of conveying the
+work, and under which the third party grants, to any of the parties who would receive
+the covered work from you, a discriminatory patent license (a) in connection with
+copies of the covered work conveyed by you (or copies made from those copies), or (b)
+primarily for and in connection with specific products or compilations that contain
+the covered work, unless you entered into that arrangement, or that patent license
+was granted, prior to 28 March 2007.
+
+Nothing in this License shall be construed as excluding or limiting any implied
+license or other defenses to infringement that may otherwise be available to you
+under applicable patent law.
+
+### 12. No Surrender of Others' Freedom.
+
+If conditions are imposed on you (whether by court order, agreement or otherwise)
+that contradict the conditions of this License, they do not excuse you from the
+conditions of this License. If you cannot convey a covered work so as to satisfy
+simultaneously your obligations under this License and any other pertinent
+obligations, then as a consequence you may not convey it at all. For example, if you
+agree to terms that obligate you to collect a royalty for further conveying from
+those to whom you convey the Program, the only way you could satisfy both those terms
+and this License would be to refrain entirely from conveying the Program.
+
+### 13. Use with the GNU Affero General Public License.
+
+Notwithstanding any other provision of this License, you have permission to link or
+combine any covered work with a work licensed under version 3 of the GNU Affero
+General Public License into a single combined work, and to convey the resulting work.
+The terms of this License will continue to apply to the part which is the covered
+work, but the special requirements of the GNU Affero General Public License, section
+13, concerning interaction through a network will apply to the combination as such.
+
+### 14. Revised Versions of this License.
+
+The Free Software Foundation may publish revised and/or new versions of the GNU
+General Public License from time to time. Such new versions will be similar in spirit
+to the present version, but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program specifies that
+a certain numbered version of the GNU General Public License “or any later
+version” applies to it, you have the option of following the terms and
+conditions either of that numbered version or of any later version published by the
+Free Software Foundation. If the Program does not specify a version number of the GNU
+General Public License, you may choose any version ever published by the Free
+Software Foundation.
+
+If the Program specifies that a proxy can decide which future versions of the GNU
+General Public License can be used, that proxy's public statement of acceptance of a
+version permanently authorizes you to choose that version for the Program.
+
+Later license versions may give you additional or different permissions. However, no
+additional obligations are imposed on any author or copyright holder as a result of
+your choosing to follow a later version.
+
+### 15. Disclaimer of Warranty.
+
+THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER
+EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE
+QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
+DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+### 16. Limitation of Liability.
+
+IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY
+COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS
+PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL,
+INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE
+OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE
+WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+### 17. Interpretation of Sections 15 and 16.
+
+If the disclaimer of warranty and limitation of liability provided above cannot be
+given local legal effect according to their terms, reviewing courts shall apply local
+law that most closely approximates an absolute waiver of all civil liability in
+connection with the Program, unless a warranty or assumption of liability accompanies
+a copy of the Program in return for a fee.
+
+END OF TERMS AND CONDITIONS
+
+## How to Apply These Terms to Your New Programs
+
+If you develop a new program, and you want it to be of the greatest possible use to
+the public, the best way to achieve this is to make it free software which everyone
+can redistribute and change under these terms.
+
+To do so, attach the following notices to the program. It is safest to attach them
+to the start of each source file to most effectively state the exclusion of warranty;
+and each file should have at least the “copyright” line and a pointer to
+where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program does terminal interaction, make it output a short notice like this
+when it starts in an interactive mode:
+
+    <program>  Copyright (C) <year>  <name of author>
+    This program comes with ABSOLUTELY NO WARRANTY; for details type 'show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type 'show c' for details.
+
+The hypothetical commands 'show w' and 'show c' should show the appropriate parts of
+the General Public License. Of course, your program's commands might be different;
+for a GUI interface, you would use an “about box”.
+
+You should also get your employer (if you work as a programmer) or school, if any, to
+sign a “copyright disclaimer” for the program, if necessary. For more
+information on this, and how to apply and follow the GNU GPL, see
+&lt;<http://www.gnu.org/licenses/>&gt;.
+
+The GNU General Public License does not permit incorporating your program into
+proprietary programs. If your program is a subroutine library, you may consider it
+more useful to permit linking proprietary applications with the library. If this is
+what you want to do, use the GNU Lesser General Public License instead of this
+License. But first, please read
+&lt;<http://www.gnu.org/philosophy/why-not-lgpl.html>&gt;.

+ 55 - 0
theos/Prefix.pch

@@ -0,0 +1,55 @@
+#ifdef DEBUG
+	#define __DEBUG__
+#endif
+
+#ifdef __OBJC__
+	#import <_Prefix/NullabilityCompat.h>
+
+	#if __has_feature(modules)
+		@import Foundation;
+	#else
+		#import <Foundation/Foundation.h>
+	#endif
+
+	#ifdef TARGET_OS_SIMULATOR
+		#define _THEOS_IS_SIMULATOR TARGET_OS_SIMULATOR
+	#else
+		#define _THEOS_IS_SIMULATOR TARGET_IPHONE_SIMULATOR
+	#endif
+
+	#if TARGET_IPHONE || _THEOS_IS_SIMULATOR
+		#if __has_feature(modules)
+			@import UIKit;
+		#else
+			#import <UIKit/UIKit.h>
+			
+		#endif
+
+		#import <_Prefix/IOSWebKitCompatHacks.h>
+	#endif
+
+	#if TARGET_MACOSX
+		#if __has_feature(modules)
+			@import Cocoa;
+			@import AppKit;
+		#else
+			#import <Cocoa/Cocoa.h>
+			#import <AppKit/AppKit.h>
+		#endif
+	#endif
+
+	#import <_Prefix/HBLog.h>
+	#import <_Prefix/IOSMacros.h>
+	#define NB [NSBundle mainBundle]
+	#define UD [NSUserDefaults standardUserDefaults]
+	#define FM [NSFileManager defaultManager]
+#endif
+
+#ifndef __clang__
+#import <Availability.h>
+#undef NS_AVAILABLE
+#define NS_AVAILABLE(osx, ios)
+#undef __OSX_AVAILABLE_STARTING
+#define __OSX_AVAILABLE_STARTING(osx, ios)
+#include <stdbool.h>
+#endif

+ 6 - 0
theos/README.md

@@ -0,0 +1,6 @@
+# Theos
+**Unified cross-platform Makefile system.**
+
+See [LICENSE.md](LICENSE.md) for licensing information.
+
+To learn more, including how to install or switch to this fork of Theos, visit [**the wiki**](https://github.com/kirb/theos/wiki).

+ 31 - 0
theos/bin/deb_build_num.sh

@@ -0,0 +1,31 @@
+#!/bin/bash
+GETONLY=0
+if [[ $1 == "-g" ]]; then
+	GETONLY=1
+	shift
+fi
+
+if [[ $# -lt 2 ]]; then
+	echo "Syntax: $0 [-g] packagename versionname" >&2
+	exit 1
+fi
+
+if [[ ! -d $TOP_DIR/.debmake ]]; then
+	mkdir $TOP_DIR/.debmake
+fi
+
+PACKAGE=$1
+VERSION=$2
+INFOFILE=$TOP_DIR/.debmake/$PACKAGE-$VERSION
+if [[ ! -e $INFOFILE ]]; then
+	echo -n 1 > $INFOFILE
+	echo -n 1
+	exit 0
+else
+	CURNUM=$(cat $INFOFILE)
+	if [[ $GETONLY -eq 0 ]]; then
+		let CURNUM++
+		echo -n $CURNUM > $INFOFILE
+	fi
+	echo $CURNUM
+fi

+ 48 - 0
theos/bin/denicify.pl

@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+use warnings;
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use NIC::NICBase;
+
+use Module::Load::Conditional 'can_load';
+
+$nicfile = $ARGV[0] if($ARGV[0]);
+$outputdir = $ARGV[1];
+if(!$nicfile || !$outputdir) {
+	exitWithError("Syntax: $0 nicfile outputdir");
+}
+
+### LOAD THE NICFILE! ###
+open(my $nichandle, "<", $nicfile);
+my $line = <$nichandle>;
+my $nicversion = 1;
+if($line =~ /^nic (\w+)$/) {
+	$nicversion = $1;
+} elsif($nicfile =~ /\.tar$/) {
+	$nicversion = "Tar";
+}
+seek($nichandle, 0, 0);
+
+my $NICPackage = "NIC$nicversion";
+exitWithError("I don't understand NIC version $nicversion!") if(!can_load(modules => {"NIC::Formats::$NICPackage" => undef}));
+
+{
+	no warnings 'redefine';
+	sub NIC::NICBase::_meetsConstraints { return 1; }
+}
+
+my $NIC = "NIC::Formats::$NICPackage"->new($nichandle);
+$NIC->addConstraint("package");
+close($nichandle);
+### YAY! ###
+
+$NIC->build($outputdir);
+$NIC->dumpPreamble("pre.NIC");
+
+sub exitWithError {
+	my $error = shift;
+	print STDERR "[error] ", $error, $/;
+	exit 1;
+}

+ 200 - 0
theos/bin/dm.pl

@@ -0,0 +1,200 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use File::Find;
+use File::Spec;
+use Cwd;
+use Getopt::Long;
+use Pod::Usage;
+use Archive::Tar;
+use IO::Compress::Gzip;
+use IO::Compress::Bzip2;
+
+package NIC::Archive::Tar::File;
+use parent "Archive::Tar::File";
+sub new {
+	my $class = shift;
+	my $self = Archive::Tar::File->new(@_);
+	bless($self, $class);
+	return $self;
+}
+
+sub full_path {
+	my $self = shift;
+	my $full_path = $self->SUPER::full_path(); $full_path = '' unless defined $full_path;
+	$full_path =~ s#^#./# if $full_path ne "" && $full_path ne "." && $full_path !~ m#^\./#;
+	return $full_path;
+}
+1;
+package main;
+
+our $VERSION = '2.0';
+
+our $_PROGNAME = "dm.pl";
+
+my $ADMINARCHIVENAME = "control.tar.gz";
+my $DATAARCHIVENAME = "data.tar";
+my $ARCHIVEVERSION = "2.0";
+
+$Archive::Tar::DO_NOT_USE_PREFIX = 1; # use GNU extensions (not POSIX prefix)
+
+our $compression = "gzip";
+Getopt::Long::Configure("bundling", "auto_version");
+GetOptions('compression|Z=s' => \$compression,
+	'build|b' => sub { },
+	'help|?' => sub { pod2usage(1); },
+	'man' => sub { pod2usage(-exitstatus => 0, -verbose => 2); })
+	or pod2usage(2);
+
+pod2usage(1) if(@ARGV < 2);
+
+my $pwd = Cwd::cwd();
+my $indir = File::Spec->rel2abs($ARGV[0]);
+my $outfile = $ARGV[1];
+
+die "ERROR: '$indir' is not a directory or does not exist.\n" unless -d $indir;
+
+my $controldir = File::Spec->catpath("", $indir, "DEBIAN");
+
+die "ERROR: control directory '$controldir' is not a directory or does not exist.\n" unless -d $controldir;
+my $mode = (lstat($controldir))[2];
+die sprintf("ERROR: control directory has bad permissions %03lo (must be >=0755 and <=0775)\n", $mode & 07777) if(($mode & 07757) != 0755);
+
+my $controlfile = File::Spec->catfile($controldir, "control");
+die "ERROR: control file '$controlfile' is not a plain file\n" unless -f $controlfile;
+my %control_data = read_control_file($controlfile);
+
+die "ERROR: package name has characters that aren't alphanumueric or '-+.'.\n" if($control_data{"package"} =~ m/[^a-zA-Z0-9+-.]/);
+die "ERROR: package version ".$control_data{"version"}." doesn't contain any digits.\n" if($control_data{"version"} !~ m/[0-9]/);
+
+foreach my $m ("preinst", "postinst", "prerm", "postrm", "extrainst_") {
+	$_ = File::Spec->catfile($controldir, $m);
+	next unless -e $_;
+	die "ERROR: maintainer script '$m' is not a plain file or symlink\n" unless(-f $_ || -l $_);
+	$mode = (lstat)[2];
+	die sprintf("ERROR: maintainer script '$m' has bad permissions %03lo (must be >=0555 and <=0775)\n", $mode & 07777) if(($mode & 07557) != 0555)
+}
+
+print "$_PROGNAME: building package `".$control_data{"package"}.":".$control_data{"architecture"}."' in `$outfile'\n";
+
+open(my $ar, '>', $outfile) or die $!;
+
+print $ar "!<arch>\n";
+print_ar_record($ar, "debian-binary", time, 0, 0, 0100644, 4);
+print_ar_file($ar, "$ARCHIVEVERSION\n", 4);
+
+{
+	my $tar = Archive::Tar->new();
+	$tar->add_files(tar_filelist($controldir));
+	my $comp;
+	my $zFd = IO::Compress::Gzip->new(\$comp, -Level => 9);
+	$tar->write($zFd);
+	$zFd->close();
+	print_ar_record($ar, $ADMINARCHIVENAME, time, 0, 0, 0100644, length($comp));
+	print_ar_file($ar, $comp, length($comp));
+} {
+	my $tar = Archive::Tar->new();
+	$tar->add_files(tar_filelist($indir));
+	my $comp;
+	my $zFd = compressed_fd(\$comp);
+	$tar->write($zFd);
+	$zFd->close();
+	print_ar_record($ar, compressed_filename($DATAARCHIVENAME), time, 0, 0, 0100644, length($comp));
+	print_ar_file($ar, $comp, length($comp));
+}
+
+close $ar;
+
+sub print_ar_record {
+	my ($fh, $filename, $timestamp, $uid, $gid, $mode, $size) = @_;
+	printf $fh "%-16s%-12lu%-6lu%-6lu%-8lo%-10ld`\n", $filename, $timestamp, $uid, $gid, $mode, $size;
+	$fh->flush();
+}
+
+sub print_ar_file {
+	my ($fh, $data, $size) = @_;
+	syswrite $fh, $data;
+	print $fh "\n" if($size % 2 == 1);
+	$fh->flush();
+}
+
+sub tar_filelist {
+	chdir(shift);
+	my @filelist;
+	my @symlinks;
+
+	find({wanted => sub {
+		return if m#^./DEBIAN#;
+		my $tf = NIC::Archive::Tar::File->new(file=>$_);
+		push @symlinks, $tf if -l;
+		push @filelist, $tf if ! -l;
+	}, no_chdir => 1}, ".");
+	return (@filelist, @symlinks);
+}
+
+sub read_control_file {
+	my $filename = shift;
+	open(my $fh, '<', $filename) or die "ERROR: can't open control file '$filename'\n";
+	my %data;
+	while(<$fh>) {
+		if(m/^(.*?): (.*)/) {
+			$data{lc($1)} = $2;
+		}
+	}
+	close $fh;
+	return %data;
+}
+
+sub compressed_fd {
+	my $sref = shift;
+	return IO::Compress::Gzip->new($sref, -Level => 9) if $::compression eq "gzip";
+	return IO::Compress::Bzip2->new($sref) if $::compression eq "bzip2";
+	open my $fh, ">", $sref;
+	return $fh;
+}
+
+sub compressed_filename {
+	my $fn = shift;
+	my $suffix = "";
+	$suffix = ".gz" if $::compression eq "gzip";
+	$suffix = ".bz2" if $::compression eq "bzip2";
+	return $fn.$suffix;
+}
+
+__END__
+
+=head1 NAME
+
+dm.pl
+
+=head1 SYNOPSIS
+
+dm.pl [options] <directory> <package>
+
+=head1 OPTIONS
+
+=over 8
+
+=item B<-b>
+
+This option exists solely for compatibility with dpkg-deb.
+
+=item B<-ZE<lt>compressionE<gt>>
+
+Specify the package compression type. Valid values are gzip (default), bzip2 and cat (no compression.)
+
+=item B<--help>, B<-?>
+
+Print a brief help message and exit.
+
+=item B<--man>
+
+Print a manual page and exit.
+
+=back
+
+=head1 DESCRIPTION
+
+B<This program> creates Debian software packages (.deb files) and is a drop-in replacement for dpkg-deb.
+
+=cut

+ 1 - 0
theos/bin/dm.pl.txt

@@ -0,0 +1 @@
+dm.pl is at revision ac95b88a5c84747b1c40c3456525efeb3a750eed

+ 46 - 0
theos/bin/fakeroot.sh

@@ -0,0 +1,46 @@
+#!/bin/bash
+required=0
+persistence=/tmp/dhbxxx
+
+while getopts ":p:rc" flag; do
+	case "$flag" in
+		:)	echo "$0: Option -$OPTARG requires an argument." 1>&2
+			exit 1
+			;;
+		\?)	echo "$0: Option -$OPTARG unrecognized." 1>&2
+			exit 1
+			;;
+		p)	persistence="$OPTARG" ;;
+		r)	required=1 ;;
+		c)	delpersistence=1 ;;
+	esac
+done
+shift $((OPTIND-1))
+cmd=$*
+
+mkdir -p $(dirname $persistence)
+touch $persistence
+
+if [[ $delpersistence -eq 1 ]]; then
+	rm -f $persistence
+	exit 0
+fi
+
+if [[ "$USER" == "root" ]]; then
+	fakeroot=""
+elif type fauxsu &> /dev/null; then
+	fakeroot="fauxsu -p $persistence -- "
+elif type fakeroot-ng &> /dev/null; then
+	fakeroot="fakeroot-ng -p $persistence -- "
+elif type fakeroot &> /dev/null; then
+	fakeroot="fakeroot -i $persistence -s $persistence -- "
+else
+	if [[ $required -eq 1 ]]; then
+		fakeroot=""
+	else
+		fakeroot=": "
+	fi
+fi
+
+#echo $fakeroot $cmd
+$fakeroot $cmd

+ 6 - 0
theos/bin/install.copyFile

@@ -0,0 +1,6 @@
+#!/bin/bash
+if [[ TARGET_INSTALL_REMOTE -eq 1 ]]; then
+	scp -P $THEOS_DEVICE_PORT "$1" $THEOS_DEVICE_USER@$THEOS_DEVICE_IP:$2
+else
+	cp "$1" "$2"
+fi

+ 19 - 0
theos/bin/install.exec

@@ -0,0 +1,19 @@
+#!/bin/bash
+if [[ TARGET_INSTALL_REMOTE -eq 1 ]]; then
+	args=(ssh)
+
+	if [[ ! -z "$THEOS_DEVICE_PORT" ]]; then
+		args+=(-p "$THEOS_DEVICE_PORT")
+	fi
+
+	if [[ ! -z "$THEOS_DEVICE_USER" ]]; then
+		args+=(-l "$THEOS_DEVICE_USER")
+	fi
+
+	args+=($THEOS_DEVICE_IP)
+	args+=("$@")
+
+	exec ${args[@]}
+else
+	exec su -c "$@"
+fi

+ 4 - 0
theos/bin/install.mergeDir

@@ -0,0 +1,4 @@
+#!/bin/bash
+cd "$1"
+# Use fakeroot.sh to ensure that permissions are preserved, and install.exec to ensure that we are running tar -x on the right system.
+fakeroot.sh -r tar -c . | install.exec "tar -x -C \"$2\""

+ 122 - 0
theos/bin/lib/Logos/Class.pm

@@ -0,0 +1,122 @@
+package Logos::Class;
+use strict;
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = {};
+	$self->{NAME} = undef;
+	$self->{EXPR} = undef;
+	$self->{METAEXPR} = undef;
+	$self->{TYPE} = undef;
+	$self->{META} = 0;
+	$self->{INST} = 0;
+	$self->{OVERRIDDEN} = 0;
+	$self->{REQUIRED} = 0;
+	$self->{METHODS} = [];
+	$self->{NUM_METHODS} = 0;
+	$self->{PROPERTIES} = [];
+	$self->{NUM_PROPERTIES} = 0;
+	$self->{GROUP} = undef;
+	bless($self, $class);
+	return $self;
+}
+
+##################### #
+# Setters and Getters #
+# #####################
+sub name {
+	my $self = shift;
+	if(@_) { $self->{NAME} = shift; }
+	return $self->{NAME};
+}
+
+sub expression {
+	my $self = shift;
+	if(@_) {
+		$self->{EXPR} = shift;
+		$self->type("id");
+		$self->{OVERRIDDEN} = 1;
+	}
+	return $self->{EXPR};
+}
+
+sub metaexpression {
+	my $self = shift;
+	if(@_) {
+		$self->{METAEXPR} = shift;
+		$self->{OVERRIDDEN} = 1;
+	}
+	return $self->{METAEXPR};
+}
+
+sub type {
+	my $self = shift;
+	if(@_) { $self->{TYPE} = shift; }
+	return $self->{TYPE} if $self->{TYPE};
+	return $self->{NAME}."*";
+}
+
+sub hasmetahooks {
+	my $self = shift;
+	if(@_) { $self->{META} = shift; }
+	return $self->{META};
+}
+
+sub hasinstancehooks {
+	my $self = shift;
+	if(@_) { $self->{INST} = shift; }
+	return $self->{INST};
+}
+
+sub group {
+	my $self = shift;
+	if(@_) { $self->{GROUP} = shift; }
+	return $self->{GROUP};
+}
+
+sub required {
+	my $self = shift;
+	if(@_) { $self->{REQUIRED} = shift; }
+	return $self->{REQUIRED};
+}
+
+sub overridden {
+	my $self = shift;
+	return $self->{OVERRIDDEN};
+}
+
+sub methods {
+	my $self = shift;
+	return $self->{METHODS};
+}
+
+sub properties {
+	my $self = shift;
+	return $self->{PROPERTIES};
+}
+
+sub initRequired {
+	my $self = shift;
+	return $self->required || scalar @{$self->{METHODS}} > 0;
+}
+
+##### #
+# END #
+# #####
+
+sub addMethod {
+	my $self = shift;
+	my $hook = shift;
+	push(@{$self->{METHODS}}, $hook);
+	$self->{NUM_METHODS}++;
+}
+
+sub addProperty {
+	my $self = shift;
+	my $property = shift;
+	push(@{$self->{PROPERTIES}}, $property);
+	$self->{NUM_PROPERTIES}++;
+}
+
+1;

+ 64 - 0
theos/bin/lib/Logos/Function.pm

@@ -0,0 +1,64 @@
+package Logos::Function;
+use strict;
+
+sub new {
+	my $proto = shift;
+	my $function = ref($proto) || $proto;
+	my $self = {};
+	$self->{NAME} = undef;
+	$self->{RETVAL} = undef;
+	$self->{ARGS} = [];
+	$self->{GROUP} = undef;
+	$self->{REQUIRED} = 0;
+	bless($self, $function);
+	return $self;
+}
+
+##################### #
+# Setters and Getters #
+# #####################
+sub name {
+	my $self = shift;
+	if(@_) { $self->{NAME} = shift; }
+	return $self->{NAME};
+}
+
+sub retval {
+	my $self = shift;
+	if(@_) { $self->{RETVAL} = shift; }
+	return $self->{RETVAL};
+}
+
+sub args {
+	my $self = shift;
+	return $self->{ARGS};
+}
+
+sub group {
+	my $self = shift;
+	if(@_) { $self->{GROUP} = shift; }
+	return $self->{GROUP};
+}
+
+sub required {
+	my $self = shift;
+	if(@_) { $self->{REQUIRED} = shift; }
+	return $self->{REQUIRED};
+}
+
+sub initRequired {
+	my $self = shift;
+	return 1;
+}
+
+##### #
+# END #
+# #####
+
+sub addArg {
+	my $self = shift;
+	my $arg = shift;
+	push(@{$self->{ARGS}}, $arg);
+}
+
+1;

+ 48 - 0
theos/bin/lib/Logos/Generator.pm

@@ -0,0 +1,48 @@
+package Logos::Generator;
+use strict;
+use Logos::Generator::Thunk;
+use Scalar::Util qw(blessed);
+use Module::Load::Conditional qw(can_load);
+$Module::Load::Conditional::VERBOSE = 1;
+our $GeneratorPackage = "";
+
+my %cache;
+
+sub for {
+	my $object = shift;
+	my $dequalified = undef;
+	my $cachekey;
+	if(defined $object) {
+		$cachekey = $object;
+		my $class = blessed($object);
+		($dequalified = $class) =~ s/.*::// if defined $class
+	}
+	$cachekey = "-" if !$cachekey;
+	$dequalified .= "Generator" if !defined $dequalified;
+	return $cache{$cachekey} if $cache{$cachekey};
+
+	my $qualified = $GeneratorPackage."::".$dequalified;
+	my $fallback = "Logos::Generator::Base::".$dequalified;
+
+	my $shouldFallBack = 0;
+	can_load(modules=>{$qualified=>undef},verbose=>0) || ($shouldFallBack = 1);
+	can_load(modules=>{$fallback=>undef},verbose=>1) if $shouldFallBack;
+
+	my $thunk = Logos::Generator::Thunk->for(($shouldFallBack ? $fallback : $qualified), $object);
+	$cache{$cachekey} = $thunk;
+	return $thunk;
+}
+
+sub use {
+	my $generatorName = shift;
+	if($generatorName =~ /^(\w+)@(.+)$/) {
+		$generatorName = $1;
+		unshift @INC, $2;
+	}
+	$GeneratorPackage = "Logos::Generator::".$generatorName;
+	::fileError(-1, "I can't find the $generatorName Generator!") if(!can_load(modules => {
+				$GeneratorPackage."::Generator" => undef
+			}));
+}
+
+1;

+ 55 - 0
theos/bin/lib/Logos/Generator/Base/Class.pm

@@ -0,0 +1,55 @@
+package Logos::Generator::Base::Class;
+use Logos::Generator;
+use strict;
+
+sub _initExpression {
+	my $self = shift;
+	my $class = shift;
+	return $class->expression if $class->expression;
+	return "objc_getClass(\"".$class->name."\")";
+}
+
+sub _metaInitExpression {
+	my $self = shift;
+	my $class = shift;
+	return $class->metaexpression if $class->metaexpression;
+	return "object_getClass(".$self->variable($class).")";
+}
+
+
+sub variable {
+	my $self = shift;
+	my $class = shift;
+	return Logos::sigil("class").$class->group->name."\$".$class->name;
+}
+
+sub metaVariable {
+	my $self = shift;
+	my $class = shift;
+	return Logos::sigil("metaclass").$class->group->name."\$".$class->name;
+}
+
+sub declarations {
+	my $self = shift;
+	my $class = shift;
+	my $return = "";
+	for(@{$class->methods}) {
+		$return .= Logos::Generator::for($_)->declarations;
+	}
+	return $return;
+}
+
+sub initializers {
+	my $self = shift;
+	my $class = shift;
+	my $return = "";
+	for(@{$class->methods}) {
+		$return .= Logos::Generator::for($_)->initializers;
+	}
+	for(@{$class->properties}) {
+		$return .= Logos::Generator::for($_)->initializers;
+	}
+	return $return;
+}
+
+1;

+ 177 - 0
theos/bin/lib/Logos/Generator/Base/Function.pm

@@ -0,0 +1,177 @@
+package Logos::Generator::Base::Function;
+use strict;
+use Logos::Generator;
+use Logos::Util;
+
+sub originalFunctionName {
+	my $self = shift;
+	my $function = shift;
+	return Logos::sigil("orig").$function->group->name."\$".$function->name;
+}
+
+sub newFunctionName {
+	my $self = shift;
+	my $function = shift;
+	return Logos::sigil("function").$function->group->name."\$".$function->name;
+}
+
+sub originalFunctionCall {
+	my $self = shift;
+	my $function = shift;
+	my $args = shift;
+
+	my $return = $self->originalFunctionName($function)."(";
+	if (!$args) {
+		$args = [];
+		for(@{$function->args}) {
+			push(@$args, $self->getArgumentName($_));
+		}
+		$return .= join(", ", @$args);
+	} else {
+		$return .= $args;
+	}
+	$return .= ")";
+
+	return $return;
+}
+
+sub declaration {
+	my $self = shift;
+	my $function = shift;
+	my $return = "";
+	$return .= "_disused static ".$function->retval." (*".$self->originalFunctionName($function).")(".join(", ", @{$function->args})."); ";
+	$return .= "static ".$function->retval." ".$self->newFunctionName($function)."(".join(", ", @{$function->args}).")";
+	return $return;
+}
+
+sub initializers {
+	::fileError(-1, "Base::Function does not implement initializers");
+}
+
+##########
+# extras #
+##########
+
+sub getArgumentName {
+	my $self = shift;
+	my $arg = shift;
+
+	# Split the argument string by spaces
+	my $argArray = $self->notSoSmartSplit($arg, " ");
+	# Try to get the name of the last element of the array if it is a function pointer
+	my $return = $self->escapeFunctionPointer(@$argArray[-1]);
+	# Separate the name from trailing vector size
+	$return =~ s/\[/ \[/g;
+	# Trimm spaces or asterisks from the start
+	while (substr($return, 0, 1) eq " " || substr($return, 0, 1) eq "*") {
+		$return = substr($return, 1, length($return));
+	}
+	# Split the resulting string by spaces
+	$argArray = $self->notSoSmartSplit($return, " ");
+	# Get the first element
+	my $return = @$argArray[0];
+
+	return $return;
+}
+
+sub escapeFunctionPointer {
+	my $self = shift;
+	my $arg = shift;
+	if($arg !~ /\(\s*[*^]/) {
+		return $arg;
+	}
+	my $substring = $arg;
+	my ($opening, $closing) = matchedParenthesisSet($substring, 0);
+	my $offset = 0;
+	while(1) {
+		# We want to put the parameter name right before the closing ) in the deepest nested set if we found a (^ or (*.
+		$substring = substr($substring, $opening, $closing - $opening - 1);
+		$offset += $opening;
+		my ($newopening, $newclosing) = matchedParenthesisSet($substring, 0);
+		last if !defined $newopening;
+		$opening = $newopening;
+		$closing = $newclosing;
+	}
+	return substr($arg, $offset, $closing-$opening-1);
+}
+
+#for a lack of Logos::Util::matchedDelimiterSet()
+sub notSoSmartSplit {
+	my $self = shift;
+	my $argumentString = shift;
+	my $delimiter = shift;
+
+	# Default to commas
+	if (!$delimiter) {
+		$delimiter = ",";
+	}
+	$argumentString .= $delimiter; #uber hax
+
+# curved  brackets or parens ()
+	my $parensLevel = 0;
+# squared brackets or crotchets []
+	my $crotchetsLevel = 0;
+# curly   brackets or braces {}
+	my $bracesLevel = 0;
+# angled  brackets or chevrons <>
+	my $chevronsLevel = 0;
+
+	my $token = "";
+	my $pc = "";
+
+	my $args = [];
+
+	foreach my $c (split //, $argumentString) {
+		if ($c eq $delimiter) {
+			# If at root level, end token, push to array and start again
+			if ($parensLevel == 0 && $crotchetsLevel == 0 && $bracesLevel == 0 && $chevronsLevel == 0) {
+				push(@$args, $token);
+				$pc = $c;
+				$token = "";
+				next;
+			}
+		}
+
+		if ($c eq "(") {
+			$parensLevel++;
+		}
+		if ($c eq ")") {
+			$parensLevel--;
+		}
+
+		if ($c eq "[") {
+			$crotchetsLevel++;
+		}
+		if ($c eq "]") {
+			$crotchetsLevel--;
+		}
+
+		if ($c eq "{") {
+			$bracesLevel++;
+		}
+		if ($c eq "}") {
+			$bracesLevel--;
+		}
+
+		if ($c eq "<") {
+			$chevronsLevel++;
+		}
+		if ($c eq ">") {
+			$chevronsLevel--;
+		}
+
+		#skip redundant empty spaces
+		if (($pc eq $delimiter && $c eq " ") || ($pc eq " " && $c eq " ")) {
+			next;
+		}
+
+		# Concatenate char to token
+		$token .= $c;
+		#save previous char
+		$pc = $c;
+	} # foreach
+
+	return $args;
+}
+
+1;

+ 49 - 0
theos/bin/lib/Logos/Generator/Base/Generator.pm

@@ -0,0 +1,49 @@
+package Logos::Generator::Base::Generator;
+use strict;
+
+sub findPreamble {
+	my $self = shift;
+	my $aref = shift;
+	my @matches = grep(/\s*#\s*(import|include)\s*[<"]logos\/logos\.h[">]/, @$aref);
+	return @matches > 0;
+}
+
+sub preamble {
+	return <<END;
+#if defined(__clang__)
+#if __has_feature(objc_arc)
+#define _LOGOS_SELF_TYPE_NORMAL __unsafe_unretained
+#define _LOGOS_SELF_TYPE_INIT __attribute__((ns_consumed))
+#define _LOGOS_SELF_CONST const
+#define _LOGOS_RETURN_RETAINED __attribute__((ns_returns_retained))
+#else
+#define _LOGOS_SELF_TYPE_NORMAL
+#define _LOGOS_SELF_TYPE_INIT
+#define _LOGOS_SELF_CONST
+#define _LOGOS_RETURN_RETAINED
+#endif
+#else
+#define _LOGOS_SELF_TYPE_NORMAL
+#define _LOGOS_SELF_TYPE_INIT
+#define _LOGOS_SELF_CONST
+#define _LOGOS_RETURN_RETAINED
+#endif
+END
+}
+
+sub generateClassList {
+	my $self = shift;
+	my $return = "";
+	map $return .= "\@class $_; ", @_;
+	return $return;
+}
+
+sub classReferenceWithScope {
+	my $self = shift;
+	my $classname = shift;
+	my $scope = shift;
+	my $prefix = Logos::sigil($scope eq "+" ? "static_metaclass_lookup" : "static_class_lookup");
+	return $prefix.$classname."()";
+}
+
+1;

+ 28 - 0
theos/bin/lib/Logos/Generator/Base/Group.pm

@@ -0,0 +1,28 @@
+package Logos::Generator::Base::Group;
+use strict;
+
+sub declarations {
+	my $self = shift;
+	my $group = shift;
+	my $return = "";
+	foreach(@{$group->classes}) {
+		$return .= Logos::Generator::for($_)->declarations if $_->initRequired;
+	}
+	return $return;
+}
+
+sub initializers {
+	my $self = shift;
+	my $group = shift;
+	my $return = "{";
+	foreach(@{$group->classes}) {
+		$return .= Logos::Generator::for($_)->initializers if $_->initRequired;
+	}
+	foreach(@{$group->functions}) {
+		$return .= Logos::Generator::for($_)->initializers if $_->initRequired;
+	}
+	$return .= "}";
+	return $return;
+}
+
+1;

+ 119 - 0
theos/bin/lib/Logos/Generator/Base/Method.pm

@@ -0,0 +1,119 @@
+package Logos::Generator::Base::Method;
+use strict;
+use Logos::Util;
+
+sub originalFunctionName {
+	my $self = shift;
+	my $method = shift;
+	return Logos::sigil(($method->scope eq "+" ? "meta_" : "")."orig").$method->groupIdentifier."\$".$method->class->name."\$".$method->_new_selector;
+}
+
+sub newFunctionName {
+	my $self = shift;
+	my $method = shift;
+	return Logos::sigil(($method->scope eq "+" ? "meta_" : "")."method").$method->groupIdentifier."\$".$method->class->name."\$".$method->_new_selector;
+}
+
+sub definition {
+	::fileError(-1, "generator does not implement Method::definition");
+}
+
+sub originalCall {
+	::fileError(-1, "generator does not implement Method::originalCall");
+}
+
+sub selectorRef {
+	my $self = shift;
+	my $selector = shift;
+	if ($selector eq "dealloc") {
+		return "sel_registerName(\"".$selector."\")";
+	}
+	return "\@selector(".$selector.")";
+}
+
+sub selfTypeForMethod {
+	my $self = shift;
+	my $method = shift;
+	if($method->scope eq "+") {
+		return "_LOGOS_SELF_TYPE_NORMAL Class _LOGOS_SELF_CONST";
+	}
+	if($method->methodFamily eq "init") {
+		return "_LOGOS_SELF_TYPE_INIT ".$method->class->type;
+	}
+	return "_LOGOS_SELF_TYPE_NORMAL ".$method->class->type." _LOGOS_SELF_CONST";
+}
+
+sub returnTypeForMethod {
+	my $self = shift;
+	my $method = shift;
+ 	if($method->methodFamily ne "") {
+		return $method->class->type;
+	}
+	my $result = $method->return;
+	if ($result eq "instancetype") {
+		return $method->class->type;
+	}
+	return $result;
+}
+
+sub functionAttributesForMethod {
+	my $self = shift;
+	my $method = shift;
+	if($method->methodFamily ne "") {
+		return " _LOGOS_RETURN_RETAINED";
+	}
+	return "";
+}
+
+sub buildLogCall {
+	my $self = shift;
+	my $method = shift;
+	my $args = shift;
+	# Log preamble
+	my $build = "HBLogDebug(\@\"".$method->scope."[<".$method->class->name.": %p>";
+	my $argnamelist = "";
+	if($method->numArgs > 0) {
+		# For each argument, add its keyword and a format char to the log string.
+		map $build .= " ".$method->selectorParts->[$_].":".Logos::Method::formatCharForArgType($method->argtypes->[$_]), (0..$method->numArgs - 1);
+		# This builds a list of args by making sure the format char isn't -- (or, what we're using for non-operational types)
+		# Map (in list context) "format char == -- ? nothing : arg name" over the indices of the arg list.
+		my @newarglist = map(Logos::Method::printArgForArgType($method->argtypes->[$_], $method->argnames->[$_]), (0..$method->numArgs - 1));
+		my @existingargs = grep(defined($_), @newarglist);
+		if(scalar(@existingargs) > 0) {
+			$argnamelist = ", ".join(", ", grep(defined($_), @existingargs));
+		}
+	} else {
+		# Space and then the only keyword in the selector.
+		$build .= " ".$method->selector;
+	}
+
+	my @extraFormatSpecifiers;
+	my @extraArguments;
+	for(Logos::Util::smartSplit(qr/\s*,\s*/, $args)) {
+		my ($popen, $pclose) = matchedParenthesisSet($_);
+		my $type = "id";
+		if(defined $popen) {
+			$type = substr($_, $popen, $pclose-$popen-1);
+		}
+		push(@extraFormatSpecifiers, Logos::Method::formatCharForArgType($type));
+		my $n = Logos::Method::printArgForArgType($type, "($_)");
+		push(@extraArguments, $n) if $n;
+	}
+
+	# Log postamble
+	$build .= "]";
+	$build .= ": ".join(", ", @extraFormatSpecifiers) if @extraFormatSpecifiers > 0;
+	$build .= "\", self".$argnamelist;
+	$build .= ", ".join(", ", @extraArguments) if @extraArguments > 0;
+	$build .= ")";
+}
+
+sub declarations {
+	::fileError(-1, "generator does not implement Method::declarations");
+}
+
+sub initializers {
+	::fileError(-1, "generator does not implement Method::initializers");
+}
+
+1;

+ 271 - 0
theos/bin/lib/Logos/Generator/Base/Property.pm

@@ -0,0 +1,271 @@
+package Logos::Generator::Base::Property;
+use strict;
+use Logos::Util;
+
+sub key {
+	my $self = shift;
+	my $property = shift;
+
+	my $build = "_logos_property_key\$" . $property->group . "\$" . $property->class . "\$" . $property->name;
+
+	return $build;
+}
+
+sub getter {
+	my $self = shift;
+	my $property = shift;
+	my $name = shift;
+	my $key = shift;
+	my $type = $property->type;
+
+	$type =~ s/\s+$//;
+
+	if(!$name){
+		# Use property name if no getter specified
+		$name = $property->name;
+	}
+
+	# Build function start
+	my $build = "static " . $type . " _logos_method\$" . $property->group . "\$" . $property->class . "\$" . $name . "\$" . "(" . $property->class . "* self, SEL _cmd){";
+
+	# Build function body
+
+	$build .= " return ";
+
+
+	if ($type =~ /^(BOOL|(unsigned )?char|double|float|CGFloat|(unsigned )?int|NSInteger|unsigned|NSUInteger|(unsigned )?long (long)?|(unsigned )?short|NSRange|CGPoint|CGVector|CGSize|CGRect|CGAffineTransform|UIEdgeInsets|UIOffset|CATransform3D|CMTime(Range|Mapping)?|MKCoordinate(Span)?|SCNVector[34]|SCNMatrix4)$/){
+		$build .= "[";
+	}
+
+	$build .= "objc_getAssociatedObject(self, &" . $key . ")";
+
+	if ($type eq "BOOL"){
+		$build .= " boolValue]";
+	} elsif ($type eq "char"){
+		$build .= " charValue]";
+	} elsif ($type eq "unsigned char"){
+		$build .= " unsignedCharValue]";
+	} elsif ($type eq "double"){
+		$build .= " doubleValue]";
+	} elsif ($type =~ /^(float|CGFloat)$/){
+		$build .= " floatValue]";
+	} elsif ($type eq "int"){
+		$build .= " intValue]";
+	} elsif ($type eq "NSInteger"){
+		$build .= " integerValue]";
+	} elsif ($type =~ /^unsigned( int)?$/){
+		$build .= " unsignedIntValue]";
+	} elsif ($type eq "NSUInteger"){
+		$build .= " unsignedIntegerValue]";
+	} elsif ($type eq "long"){
+		$build .= " longValue]";
+	} elsif ($type eq "unsigned long"){
+		$build .= " unsignedLongValue]";
+	} elsif ($type eq "long long"){
+		$build .= " longLongValue]";
+	} elsif ($type eq "unsigned long long"){
+		$build .= " unsignedLongLongValue]";
+	} elsif ($type eq "short"){
+		$build .= " shortValue]";
+	} elsif ($type eq "unsigned short"){
+		$build .= " unsignedShortValue]";
+	} elsif ($type =~ /^(NSRange|CGPoint|CGVector|CGSize|CGRect|CGAffineTransform|UIEdgeInsets|UIOffset|CATransform3D|CMTime(Range|Mapping)?|MKCoordinate(Span)?|SCNVector[34]|SCNMatrix4)$/){
+		$build .= " " . $type . "Value]";
+	}
+
+	$build .= "; }";
+
+	return $build;
+}
+
+sub setter {
+	my $self = shift;
+	my $property = shift;
+	my $name = shift;
+	my $policy = shift;
+	my $key = shift;
+	my $type = $property->type;
+
+	$type =~ s/\s+$//;
+
+	if(!$name){
+		# Capitalize first letter
+		$_ = $property->name;
+		$_ =~ s/^([a-z])/\u$1/;
+
+		$name = "set" . $_;
+	}
+
+	# Remove semicolon
+	$name =~ s/://;
+
+	my $build = "static void _logos_method\$" . $property->group . "\$" . $property->class . "\$" . $name . "\$" . "(" . $property->class . "* self, SEL _cmd, " . $type . " arg){ ";
+
+
+	$build .= "objc_setAssociatedObject(self, &" . $key . ", ";
+
+	my $hasOpening = 1;
+
+	if ($type eq "BOOL"){
+		$build .= "[NSNumber numberWithBool:";
+	} elsif ($type eq "char"){
+		$build .= "[NSNumber numberWithChar:";
+	} elsif ($type eq "unsigned char"){
+		$build .= "[NSNumber numberWithUnsignedChar:";
+	} elsif ($type eq "double"){
+		$build .= "[NSNumber numberWithDouble:";
+	} elsif ($type =~ /^(float|CGFloat)$/){
+		$build .= "[NSNumber numberWithFloat:";
+	} elsif ($type eq "int"){
+		$build .= "[NSNumber numberWithInt:";
+	} elsif ($type eq "NSInteger"){
+		$build .= "[NSNumber numberWithInteger:";
+	} elsif ($type =~ /^unsigned( int)?$/){
+		$build .= "[NSNumber numberWithUnsignedInt:";
+	} elsif ($type eq "NSUInteger"){
+		$build .= "[NSNumber numberWithUnsignedInteger:";
+	} elsif ($type eq "long"){
+		$build .= "[NSNumber numberWithLong:";
+	} elsif ($type eq "unsigned long"){
+		$build .= "[NSNumber numberWithUnsignedLong:";
+	} elsif ($type eq "long long"){
+		$build .= "[NSNumber numberWithLongLong:";
+	} elsif ($type eq "unsigned long long"){
+		$build .= "[NSNumber numberWithUnsignedLongLong:";
+	} elsif ($type eq "short"){
+		$build .= "[NSNumber numberWithShort:";
+	} elsif ($type eq "unsigned short"){
+		$build .= "[NSNumber numberWithUnsignedShort:";
+	} elsif ($type =~ /^(NSRange|CGPoint|CGVector|CGSize|CGRect|CGAffineTransform|UIEdgeInsets|UIOffset|CATransform3D|CMTime(Range|Mapping)?|MKCoordinate(Span)?|SCNVector[34]|SCNMatrix4)$/){
+		$build .= "[NSValue valueWith " . $type . ":";
+	} else {
+		$hasOpening = 0;
+	}
+
+	$build .= "arg";
+
+	if ($hasOpening){
+		$build .= "]";
+	}
+
+	$build .= ", ".$policy.")";
+
+	$build .= "; }";
+
+	return $build;
+}
+
+sub getters_setters {
+	my $self = shift;
+	my $property = shift;
+
+	my ($assign, $retain, $nonatomic, $copy, $getter, $setter);
+
+
+	for(my $i = 0; $i < $property->numattr; $i++){
+
+		my $attr = $property->attributes->[$i];
+
+		if($attr =~ /assign/){
+			$assign = 1;
+		}elsif($attr =~ /retain/){
+			$retain = 1;
+		}elsif($attr =~ /nonatomic/){
+			$nonatomic = 1;
+		}elsif($attr =~ /copy/){
+			$copy = 1;
+		}elsif($attr =~ /getter=(\w+)/){
+			$getter = $1;
+		}elsif($attr =~ /setter=(\w+:)/){
+			$setter = $1;
+		}
+	}
+
+	my $policy = "OBJC_ASSOCIATION_";
+
+	if($retain){
+		$policy .= "RETAIN";
+	}elsif($copy){
+		$policy .= "COPY";
+	}elsif($assign){
+		$policy .= "ASSIGN";
+	}else{
+		print "error: no 'assign', 'retain', or 'copy' attribu...wait, how did you manage to get here?\n";
+	}
+
+	if($nonatomic){
+		# The 'assign' attribute appears to be nonatomic by default.
+		if(!$assign){
+			$policy .= "_NONATOMIC";
+		}
+	}
+
+	$property->associationPolicy($policy);
+
+	my $build;
+
+	my $key = $self->key($property);
+	my $getter_func = $self->getter($property, $getter, $key);
+	my $setter_func = $self->setter($property, $setter, $policy, $key);
+
+	$build .= $build . "static char " . $key . ";";
+	$build .= $getter_func;
+	$build .= $setter_func;
+
+
+	return $build;
+}
+
+sub initializers {
+	my $self = shift;
+	my $property = shift;
+
+	my ($getter, $setter);
+
+	for(my $i = 0; $i < $property->numattr; $i++){ # This could be more efficient
+		my $attr = $property->attributes->[$i];
+
+		if($attr =~ /getter=(\w+)/){
+			$getter = $1;
+		}elsif($attr =~ /setter=(\w+:)/){
+			$setter = $1;
+			$setter =~ s/://;
+		}
+	}
+
+	if(!$setter){
+		# Capitalize first letter
+		$_ = $property->name;
+		$_ =~ s/^([a-z])/\u$1/;
+
+		$setter = "set" . $_;
+	}
+
+	if(!$getter){
+		# Use property name if no getter specified
+		$getter = $property->name;
+	}
+
+
+	my $build = "";
+
+	$build .= "{ ";
+
+	# Getter
+	$build .= "class_addMethod(";
+
+	$build .= "_logos_class\$" . $property->group . "\$" . $property->class . ", ";
+	$build .= "\@selector(" . $getter . "), " . "(IMP)&" . "_logos_method\$" . $property->group . "\$" . $property->class . "\$" . $getter . "\$, [[NSString stringWithFormat:\@\"%s\@:\", \@encode(".$property->type.")] UTF8String]);";
+
+	# Setter
+	$build .= "class_addMethod(";
+	$build .= "_logos_class\$" . $property->group . "\$" . $property->class . ", ";
+
+	$build .= "\@selector(" . $setter . ":), " . "(IMP)&" . "_logos_method\$" . $property->group . "\$" . $property->class . "\$" . $setter . "\$, [[NSString stringWithFormat:\@\"v\@:%s\", \@encode(".$property->type.")] UTF8String]);";
+
+	$build .= "} ";
+
+	return $build;
+}
+
+1;

+ 34 - 0
theos/bin/lib/Logos/Generator/Base/StaticClassGroup.pm

@@ -0,0 +1,34 @@
+package Logos::Generator::Base::StaticClassGroup;
+use strict;
+
+sub _methodForClassWithScope {
+	my $self = shift;
+	my $class = shift;
+	my $scope = shift;
+	my $return = "";
+	my $methodname = Logos::sigil($scope eq "+" ? "static_metaclass_lookup" : "static_class_lookup").$class;
+	my $lookupMethod = $scope eq "+" ? "objc_getMetaClass" : "objc_getClass";
+
+	# This is a dirty assumption - we believe that we will always be using a compiler that defines __GNUC__ and respects GNU C attributes.
+	return "static __inline__ __attribute__((always_inline)) __attribute__((unused)) Class ".$methodname."(void) { static Class _klass; if(!_klass) { _klass = ".$lookupMethod."(\"".$class."\"); } return _klass; }";
+}
+
+sub declarations {
+	my $self = shift;
+	my $group = shift;
+	my $return = "";
+	return "" if scalar(keys %{$group->usedMetaClasses}) + scalar(keys %{$group->usedClasses}) + scalar(keys %{$group->declaredOnlyClasses}) == 0;
+	foreach(keys %{$group->usedMetaClasses}) {
+		$return .= $self->_methodForClassWithScope($_, "+");
+	}
+	foreach(keys %{$group->usedClasses}) {
+		$return .= $self->_methodForClassWithScope($_, "-");
+	}
+	return $return;
+}
+
+sub initializers {
+	return "";
+}
+
+1;

+ 12 - 0
theos/bin/lib/Logos/Generator/Base/Subclass.pm

@@ -0,0 +1,12 @@
+package Logos::Generator::Base::Subclass;
+use strict;
+
+sub declarations {
+	::fileError(-1, "generator does not implement Subclass::declarations");
+}
+
+sub initializers {
+	::fileError(-1, "generator does not implement Subclass::initializers");
+}
+
+1;

+ 19 - 0
theos/bin/lib/Logos/Generator/MobileSubstrate/Class.pm

@@ -0,0 +1,19 @@
+package Logos::Generator::MobileSubstrate::Class;
+use strict;
+use parent qw(Logos::Generator::Base::Class);
+
+sub initializers {
+	my $self = shift;
+	my $class = shift;
+	my $return = "";
+	if($class->required || $class->overridden || $class->hasinstancehooks || $class->hasmetahooks) {
+		$return .= "Class ".$self->variable($class)." = ".$self->_initExpression($class)."; ";
+	}
+	if($class->hasmetahooks) {
+		$return .= "Class ".$self->metaVariable($class)." = ".$self->_metaInitExpression($class)."; ";
+	}
+	$return .= $self->SUPER::initializers($class);
+	return $return;
+}
+
+1;

+ 18 - 0
theos/bin/lib/Logos/Generator/MobileSubstrate/Function.pm

@@ -0,0 +1,18 @@
+package Logos::Generator::MobileSubstrate::Function;
+use strict;
+use parent qw(Logos::Generator::Base::Function);
+
+sub initializers {
+	my $self = shift;
+	my $function = shift;
+
+	my $return = "";
+	$return .= " MSHookFunction((void *)".$function->name;
+	$return .= ", (void *)&".$self->newFunctionName($function);
+	$return .= ", (void **)&".$self->originalFunctionName($function);
+	$return .= ");";
+
+	return $return;
+}
+
+1;

+ 22 - 0
theos/bin/lib/Logos/Generator/MobileSubstrate/Generator.pm

@@ -0,0 +1,22 @@
+package Logos::Generator::MobileSubstrate::Generator;
+use strict;
+use parent qw(Logos::Generator::Base::Generator);
+
+sub findPreamble {
+	my $self = shift;
+	my $aref = shift;
+	my @matches = grep(/\s*#\s*(import|include)\s*[<"]substrate\.h[">]/, @$aref);
+	return $self->SUPER::findPreamble($aref) && @matches > 0;
+}
+
+sub preamble {
+	my $self = shift;
+	my $skipIncludes = shift;
+	if ($skipIncludes) {
+		return $self->SUPER::preamble();
+	} else {
+		return join("\n", ($self->SUPER::preamble(), "#include <substrate.h>"));
+	}
+}
+
+1;

+ 122 - 0
theos/bin/lib/Logos/Generator/MobileSubstrate/Method.pm

@@ -0,0 +1,122 @@
+package Logos::Generator::MobileSubstrate::Method;
+use strict;
+use parent qw(Logos::Generator::Base::Method);
+
+sub _originalMethodPointerDeclaration {
+	my $self = shift;
+	my $method = shift;
+	if(!$method->isNew) {
+		my $build = "static ";
+		my $classargtype = $self->selfTypeForMethod($method);
+		my $name = "(*".$self->originalFunctionName($method).")(".$classargtype.", SEL";
+		my $argtypelist = join(", ", @{$method->argtypes});
+		$name .= ", ".$argtypelist if $argtypelist;
+
+		$name .= ")";
+		$build .= Logos::Method::declarationForTypeWithName($self->returnTypeForMethod($method), $name);
+		$build .= $self->functionAttributesForMethod($method);
+		return $build;
+	}
+	return undef;
+}
+
+sub _methodPrototype {
+	my $self = shift;
+	my $method = shift;
+	my $includeArgNames = 0 || shift;
+	my $build = "static ";
+	my $classargtype = $self->selfTypeForMethod($method);
+	my $arglist = "";
+	if($includeArgNames == 1) {
+		map $arglist .= ", ".Logos::Method::declarationForTypeWithName($method->argtypes->[$_], $method->argnames->[$_]), (0..$method->numArgs - 1);
+	} else {
+		my $typelist = join(", ", @{$method->argtypes});
+		$arglist = ", ".$typelist if $typelist;
+	}
+
+	my $name = $self->newFunctionName($method)."(".$classargtype.($includeArgNames?" self":"").", SEL".($includeArgNames?" _cmd":"").$arglist.")";
+	$build .= Logos::Method::declarationForTypeWithName($self->returnTypeForMethod($method), $name);
+	$build .= $self->functionAttributesForMethod($method);
+	return $build;
+}
+
+sub definition {
+	my $self = shift;
+	my $method = shift;
+	my $build = "";
+	$build .= $self->_methodPrototype($method, 1);
+	return $build;
+}
+
+sub originalCall {
+	my $self = shift;
+	my $method = shift;
+	my $customargs = shift;
+	return "" if $method->isNew;
+
+	my $build = $self->originalFunctionName($method)."(self, _cmd";
+	if(defined $customargs && $customargs ne "") {
+		$build .= ", ".$customargs;
+	} elsif($method->numArgs > 0) {
+		$build .= ", ".join(", ",@{$method->argnames});
+	}
+	$build .= ")";
+	return $build;
+}
+
+sub declarations {
+	my $self = shift;
+	my $method = shift;
+	my $build = "";
+	my $orig = $self->_originalMethodPointerDeclaration($method);
+	$build .= $orig."; " if $orig;
+	$build .= $self->_methodPrototype($method)."; ";
+	return $build;
+}
+
+sub initializers {
+	my $self = shift;
+	my $method = shift;
+	my $cgen = Logos::Generator::for($method->class);
+	my $classvar = ($method->scope eq "+" ? $cgen->metaVariable : $cgen->variable);
+	if(!$method->isNew) {
+		my $r = "";
+		$r .= "if (".$classvar.") {";
+		$r .=   "if (class_getInstanceMethod(".$classvar.", ".$self->selectorRef($method->selector).")) {";
+		$r .=     "MSHookMessageEx(".$classvar.", ".$self->selectorRef($method->selector).", (IMP)&".$self->newFunctionName($method).", (IMP*)&".$self->originalFunctionName($method).");";
+		$r .=   "} else {";
+		$r .=     "HBLogError(@\"logos: message not found [%s %s]\", \"".$method->class->name."\", \"".$method->selector."\");";
+		$r .=   "}";
+		$r .= "} else {";
+		$r .=   "HBLogError(@\"logos: nil class %s\", \"".$method->class->name."\");";
+		$r .= "}";
+	} else {
+		my $r = "";
+		$r .= "{ ";
+		if(!$method->type) {
+			$r .= "char _typeEncoding[1024]; unsigned int i = 0; ";
+			for ($method->return, "id", "SEL", @{$method->argtypes}) {
+				my $typeEncoding = Logos::Method::typeEncodingForArgType($_);
+				if(defined $typeEncoding) {
+					my @typeEncodingBits = split(//, $typeEncoding);
+					my $i = 0;
+					for my $char (@typeEncodingBits) {
+						$r .= "_typeEncoding[i".($i > 0 ? " + $i" : "")."] = '$char'; ";
+						$i++;
+					}
+					$r .= "i += ".(scalar @typeEncodingBits)."; ";
+				} else {
+					$r .= "memcpy(_typeEncoding + i, \@encode($_), strlen(\@encode($_))); i += strlen(\@encode($_)); ";
+				}
+			}
+			$r .= "_typeEncoding[i] = '\\0'; ";
+		} else {
+			$r .= "const char *_typeEncoding = \"".$method->type."\"; ";
+		}
+		$r .= "class_addMethod(".$classvar.", ".$self->selectorRef($method->selector).", (IMP)&".$self->newFunctionName($method).", _typeEncoding); ";
+		$r .= "}";
+		return $r;
+	}
+}
+
+1;

+ 32 - 0
theos/bin/lib/Logos/Generator/MobileSubstrate/Subclass.pm

@@ -0,0 +1,32 @@
+package Logos::Generator::MobileSubstrate::Subclass;
+use strict;
+use parent qw(Logos::Generator::MobileSubstrate::Class);
+
+# declarations is inherited from Class.
+
+sub _initExpression {
+	my $self = shift;
+	my $class = shift;
+	my $cgen = Logos::Generator::for($class->superclass);
+	return "objc_allocateClassPair(".$cgen->variable.", \"".$class->name."\", 0); objc_registerClassPair(".$self->variable($class).")";
+}
+
+sub initializers {
+	my $self = shift;
+	my $class = shift;
+	my $return = "";
+	$return .= "{ ";
+	$return .= $self->SUPER::initializers($class)." ";
+	# <ivars>
+	foreach(@{$class->ivars}) {
+		$return .= Logos::Generator::for($_)->initializers;
+	}
+	# </ivars>
+	foreach(keys %{$class->protocols}) {
+		$return .= "class_addProtocol(".$self->variable($class).", objc_getProtocol(\"$_\")); ";
+	}
+	$return .= "}";
+	return $return;
+}
+
+1;

+ 54 - 0
theos/bin/lib/Logos/Generator/Thunk.pm

@@ -0,0 +1,54 @@
+package Logos::Generator::Thunk;
+use strict;
+
+our $AUTOLOAD;
+
+my %subrefCache;
+
+sub AUTOLOAD {
+	my $self = shift;
+	my $method = $AUTOLOAD;
+	return if $method eq "DESTROY";
+
+	$method =~ s/.*:://;
+	my $fullyQualified = $self->{PACKAGE}."::".$method;
+	my $subref = $subrefCache{$fullyQualified};
+
+	$subref = $self->can($method) if !$subref;
+
+	unshift @_, $self->{OBJECT} if $self->{OBJECT};
+	goto &$subref;
+}
+
+sub can {
+	my $self = shift;
+	my $method = shift;
+	my $subref = $self->SUPER::can($method);
+	return $subref if $subref;
+
+	$method =~ s/.*:://;
+	my $fullyQualified = $self->{PACKAGE}."::".$method;
+	return $subrefCache{$fullyQualified} if $subrefCache{$fullyQualified};
+
+	$subref = sub {unshift @_, $self->{PACKAGE}; goto &{$self->{PACKAGE}->can($method)}};
+	$subrefCache{$fullyQualified} = $subref;
+
+	return $subref;
+}
+
+sub DESTROY {
+	my $self = shift;
+	$self->SUPER::destroy();
+}
+
+sub for {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = {};
+	$self->{PACKAGE} = shift;
+	$self->{OBJECT} = shift;
+	bless($self, $class);
+	return $self;
+}
+
+1;

+ 51 - 0
theos/bin/lib/Logos/Generator/internal/Class.pm

@@ -0,0 +1,51 @@
+package Logos::Generator::internal::Class;
+use strict;
+use parent qw(Logos::Generator::Base::Class);
+
+sub superVariable {
+	my $self = shift;
+	my $class = shift;
+	return Logos::sigil("superclass").$class->group->name."\$".$class->name;
+}
+
+sub superMetaVariable {
+	my $self = shift;
+	my $class = shift;
+	return Logos::sigil("supermetaclass").$class->group->name."\$".$class->name;
+}
+
+sub declarations {
+	my $self = shift;
+	my $class = shift;
+	my $return = "";
+	if($class->hasinstancehooks) {
+		$return .= "static Class ".$self->superVariable($class)."; ";
+	}
+	if($class->hasmetahooks) {
+		$return .= "static Class ".$self->superMetaVariable($class)."; ";
+	}
+	$return .= $self->SUPER::declarations($class);
+	return $return;
+}
+
+sub initializers {
+	my $self = shift;
+	my $class = shift;
+	my $return = "";
+	if($class->required || $class->overridden || $class->hasinstancehooks || $class->hasmetahooks) {
+		$return .= "Class ".$self->variable($class)." = ".$self->_initExpression($class)."; ";
+	}
+	if($class->hasmetahooks) {
+		$return .= "Class ".$self->metaVariable($class)." = ".$self->_metaInitExpression($class)."; ";
+	}
+	if ($class->hasinstancehooks) {
+		$return .= $self->superVariable($class)." = class_getSuperclass(".$self->variable($class)."); ";
+	}
+	if ($class->hasmetahooks) {
+		$return .= $self->superMetaVariable($class)." = class_getSuperclass(".$self->metaVariable($class)."); ";
+	}
+	$return .= $self->SUPER::initializers($class);
+	return $return;
+}
+
+1;

+ 14 - 0
theos/bin/lib/Logos/Generator/internal/Function.pm

@@ -0,0 +1,14 @@
+package Logos::Generator::internal::Function;
+use strict;
+use parent qw(Logos::Generator::Base::Function);
+
+sub initializers {
+	my $self = shift;
+	my $function = shift;
+
+	my $return = " /* internal::Function does not implement initializers */;";
+
+	return $return;
+}
+
+1;

+ 22 - 0
theos/bin/lib/Logos/Generator/internal/Generator.pm

@@ -0,0 +1,22 @@
+package Logos::Generator::internal::Generator;
+use strict;
+use parent qw(Logos::Generator::Base::Generator);
+
+sub findPreamble {
+	my $self = shift;
+	my $aref = shift;
+	my @matches = grep(/\s*#\s*(import|include)\s*[<"]objc\/message\.h[">]/, @$aref);
+	return $self->SUPER::findPreamble($aref) && @matches > 0;
+}
+
+sub preamble {
+	my $self = shift;
+	my $skipIncludes = shift;
+	if ($skipIncludes) {
+		return $self->SUPER::preamble();
+	} else {
+		return join("\n", ($self->SUPER::preamble(), "#include <objc/message.h>"));
+	}
+}
+
+1;

+ 141 - 0
theos/bin/lib/Logos/Generator/internal/Method.pm

@@ -0,0 +1,141 @@
+package Logos::Generator::internal::Method;
+use strict;
+use parent qw(Logos::Generator::Base::Method);
+
+sub superFunctionName {
+	my $self = shift;
+	my $method = shift;
+	return Logos::sigil(($method->scope eq "+" ? "meta_" : "")."super").$method->groupIdentifier."\$".$method->class->name."\$".$method->_new_selector;
+}
+
+sub originalCallParams {
+	my $self = shift;
+	my $method = shift;
+	my $customargs = shift;
+	return "" if $method->isNew;
+
+	my $build = "(self, _cmd";
+	if(defined $customargs && $customargs ne "") {
+		$build .= ", ".$customargs;
+	} elsif($method->numArgs > 0) {
+		$build .= ", ".join(", ",@{$method->argnames});
+	}
+	$build .= ")";
+	return $build;
+}
+
+sub definition {
+	my $self = shift;
+	my $method = shift;
+	my $build = "";
+	my $selftype = $self->selfTypeForMethod($method);
+	my $classref = "";
+	my $cgen = Logos::Generator::for($method->class);
+	if($method->scope eq "+") {
+		$classref = $cgen->superMetaVariable;
+	} else {
+		$classref = $cgen->superVariable;
+	}
+	my $arglist = "";
+	map $arglist .= ", ".Logos::Method::declarationForTypeWithName($method->argtypes->[$_], $method->argnames->[$_]), (0..$method->numArgs - 1);
+	my $parameters = "(".$selftype." self, SEL _cmd".$arglist.")";
+	my $return = $self->returnTypeForMethod($method);
+	my $functionAttributes = $self->functionAttributesForMethod($method);
+	if(!$method->isNew) {
+		my $argtypelist = join(", ", @{$method->argtypes});
+
+		$build .= "static ".Logos::Method::declarationForTypeWithName($return, $self->superFunctionName($method).$parameters).$functionAttributes." {";
+		my $pointerType = "(*)(".$selftype.", SEL";
+		$pointerType .=       ", ".$argtypelist if $argtypelist;
+		$pointerType .=   ")";
+		$build .=     "return ((".$functionAttributes." ".Logos::Method::declarationForTypeWithName($return, $pointerType).")class_getMethodImplementation(".$classref.",".$self->selectorRef($method->selector)."))";
+		$build .=         $self->originalCallParams($method).";";
+		$build .= "}";
+
+	}
+	$build .= "static ".Logos::Method::declarationForTypeWithName($return, $self->newFunctionName($method).$parameters).$functionAttributes;
+	return $build;
+}
+
+sub originalCall {
+	my $self = shift;
+	my $method = shift;
+	my $customargs = shift;
+	return $self->originalFunctionName($method).$self->originalCallParams($method, $customargs);
+}
+
+sub declarations {
+	my $self = shift;
+	my $method = shift;
+	my $build = "";
+	if(!$method->isNew) {
+		my $selftype = $self->selfTypeForMethod($method);
+		my $functionAttributes = $self->functionAttributesForMethod($method);
+		$build .= "static ";
+		my $name = "";
+		$name .= $functionAttributes."(*".$self->originalFunctionName($method).")(".$selftype.", SEL";
+		my $argtypelist = join(", ", @{$method->argtypes});
+		$name .= ", ".$argtypelist if $argtypelist;
+		$name .= ")";
+		$build .= Logos::Method::declarationForTypeWithName($self->returnTypeForMethod($method), $name);
+		$build .= ";";
+	}
+	return $build;
+}
+
+sub initializers {
+	my $self = shift;
+	my $method = shift;
+	my $cgen = Logos::Generator::for($method->class);
+	my $classvar = ($method->scope eq "+" ? $cgen->metaVariable : $cgen->variable);
+	my $r = "{ ";
+	if(!$method->isNew) {
+		my $classargtype = "";
+		if($method->scope eq "+") {
+			$classargtype = "Class";
+		} else {
+			$classargtype = $method->class->type;
+		}
+		$r .= "Class _class = ".$classvar.";";
+		$r .= "Method _method = class_getInstanceMethod(_class, ".$self->selectorRef($method->selector)."));";
+		$r .= "if (_class) {";
+		$r .=   "if (_method) {";
+		$r .=     $self->originalFunctionName($method)." = ".$self->superFunctionName($method).";";
+		$r .=     "if (!class_addMethod(_class, ".$self->selectorRef($method->selector)."), (IMP)&".$self->newFunctionName($method).", method_getTypeEncoding(_method))) {";
+		$r .=       $self->originalFunctionName($method)." = (".$pointertype.")method_getImplementation(_method);";
+		$r .=       $self->originalFunctionName($method)." = (".$pointertype.")method_setImplementation(_method, (IMP)&".$self->newFunctionName($method).");";
+		$r .=     "}";
+		$r .=   "} else {";
+		$r .=     "HBLogError(@\"logos: message not found [%s %s]\", \"".$method->class->name."\", \"".$method->selector."\");";
+		$r .=   "}";
+		$r .= "} else {";
+		$r .=   "HBLogError(@\"logos: nil class %s\", \"".$method->class->name."\");";
+		$r .= "}";
+	} else {
+		if(!$method->type) {
+			$r .= "char _typeEncoding[1024]; unsigned int i = 0; ";
+			for ($method->return, "id", "SEL", @{$method->argtypes}) {
+				my $typeEncoding = Logos::Method::typeEncodingForArgType($_);
+				if(defined $typeEncoding) {
+					my @typeEncodingBits = split(//, $typeEncoding);
+					my $i = 0;
+					for my $char (@typeEncodingBits) {
+						$r .= "_typeEncoding[i".($i > 0 ? " + $i" : "")."] = '$char'; ";
+						$i++;
+					}
+					$r .= "i += ".(scalar @typeEncodingBits)."; ";
+				} else {
+					$r .= "memcpy(_typeEncoding + i, \@encode($_), strlen(\@encode($_))); i += strlen(\@encode($_)); ";
+				}
+			}
+			$r .= "_typeEncoding[i] = '\\0'; ";
+		} else {
+			$r .= "const char *_typeEncoding = \"".$method->type."\"; ";
+		}
+		$r .= "class_addMethod(".$classvar.", ".$self->selectorRef($method->selector).", (IMP)&".$self->newFunctionName($method).", _typeEncoding); ";
+	}
+	$r .= "}";
+	return $r;
+}
+
+1;

+ 33 - 0
theos/bin/lib/Logos/Generator/internal/Subclass.pm

@@ -0,0 +1,33 @@
+package Logos::Generator::internal::Subclass;
+use strict;
+use parent qw(Logos::Generator::internal::Class);
+
+# declarations is inherited from Class.
+
+sub _initExpression {
+	my $self = shift;
+	my $class = shift;
+	my $cgen = Logos::Generator::for($class->superclass);
+	return "objc_allocateClassPair(".$cgen->variable.", \"".$class->name."\", 0)";
+}
+
+sub initializers {
+	my $self = shift;
+	my $class = shift;
+	my $return = "";
+	$return .= "{ ";
+	$return .= $self->SUPER::initializers($class)." ";
+	# <ivars>
+	foreach(@{$class->ivars}) {
+		$return .= Logos::Generator::for($_)->initializers;
+	}
+	# </ivars>
+	$return .= "objc_registerClassPair(".$self->variable($class)."); ";
+	foreach(keys %{$class->protocols}) {
+		$return .= "class_addProtocol(".$self->variable($class).", objc_getProtocol(\"$_\")); ";
+	}
+	$return .= "}";
+	return $return;
+}
+
+1;

+ 136 - 0
theos/bin/lib/Logos/Group.pm

@@ -0,0 +1,136 @@
+package Logos::Group;
+use strict;
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = {};
+	$self->{NAME} = undef;
+	$self->{EXPLICIT} = 1;
+	$self->{INITIALIZED} = 0;
+	$self->{INITLINE} = -1;
+	$self->{CLASSES} = [];
+	$self->{FUNCTIONS} = [];
+	bless($self, $class);
+	return $self;
+}
+
+##################### #
+# Setters and Getters #
+# #####################
+sub name {
+	my $self = shift;
+	if(@_) { $self->{NAME} = shift; }
+	return $self->{NAME};
+}
+
+sub explicit {
+	my $self = shift;
+	if(@_) { $self->{EXPLICIT} = shift; }
+	return $self->{EXPLICIT};
+}
+
+sub initialized {
+	my $self = shift;
+	if(@_) { $self->{INITIALIZED} = shift; }
+	return $self->{INITIALIZED};
+}
+
+sub initRequired {
+	my $self = shift;
+	for(@{$self->{CLASSES}}) {
+		return 1 if $_->initRequired;
+	}
+	for(@{$self->{FUNCTIONS}}) {
+		return 1 if $_->initRequired;
+	}
+	return 0;
+}
+
+sub identifier {
+	my $self = shift;
+	return main::sanitize($self->{NAME});
+}
+
+sub initLine {
+	my $self = shift;
+	if(@_) { $self->{INITLINE} = shift; }
+	return $self->{INITLINE};
+}
+
+sub classes {
+	my $self = shift;
+	return $self->{CLASSES};
+}
+
+sub functions {
+	my $self = shift;
+	return $self->{FUNCTIONS};
+}
+##### #
+# END #
+# #####
+
+sub addClass {
+	my $self = shift;
+	my $class = shift;
+	$class->group($self);
+	push(@{$self->{CLASSES}}, $class);
+}
+
+sub addClassNamed {
+	my $self = shift;
+	my $name = shift;
+
+	my $class = $self->getClassNamed($name);
+	return $class if defined($class);
+
+	$class = ::Class()->new();
+	$class->name($name);
+	$self->addClass($class);
+	return $class;
+}
+
+sub getClassNamed {
+	my $self = shift;
+	my $name = shift;
+	foreach(@{$self->{CLASSES}}) {
+		return $_ if $_->name eq $name;
+	}
+	return undef;
+}
+
+sub addFunction {
+	my $self = shift;
+	my $args = shift;
+
+	my $functionRetval = undef;
+	my $functionName = undef;
+	my $functionArgs = [];
+
+	my $argIdx = 0;
+	for (@$args) {
+		if ($argIdx == 0) {
+			$argIdx++;
+			$functionRetval = $_;
+		} elsif ($argIdx == 1) {
+			$argIdx++;
+			$functionName = $_;
+		} else {
+			push(@$functionArgs, $_);
+		}
+	}
+	
+	my $function = ::Function()->new();
+	$function->retval($functionRetval);
+	$function->name($functionName);
+	for(@$functionArgs) {
+		$function->addArg($_);
+	}
+	$function->group($self);
+	push(@{$self->{FUNCTIONS}}, $function);
+
+	return $function;
+}
+
+1;

+ 39 - 0
theos/bin/lib/Logos/Ivar.pm

@@ -0,0 +1,39 @@
+package Logos::Ivar;
+use strict;
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = {};
+	$self->{NAME} = shift;
+	$self->{TYPE} = shift;
+	$self->{CLASS} = undef;
+	bless($self, $class);
+	return $self;
+}
+
+##################### #
+# Setters and Getters #
+# #####################
+sub name {
+	my $self = shift;
+	if(@_) { $self->{NAME} = shift; }
+	return $self->{NAME};
+}
+
+sub type {
+	my $self = shift;
+	if(@_) { $self->{TYPE} = shift; }
+	return $self->{TYPE};
+}
+
+sub class {
+	my $self = shift;
+	if(@_) { $self->{CLASS} = shift; }
+	return $self->{CLASS};
+}
+##### #
+# END #
+# #####
+
+1;

+ 285 - 0
theos/bin/lib/Logos/Method.pm

@@ -0,0 +1,285 @@
+package Logos::Method;
+use strict;
+use Logos::Util qw(matchedParenthesisSet);
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = {};
+	$self->{CLASS} = undef;
+	$self->{SCOPE} = undef;
+	$self->{RETURN} = undef;
+	$self->{SELECTOR_PARTS} = [];
+	$self->{ARGNAMES} = [];
+	$self->{ARGTYPES} = [];
+	$self->{NEW} = 0;
+	$self->{TYPE} = "";
+	bless($self, $class);
+	return $self;
+}
+
+##################### #
+# Setters and Getters #
+# #####################
+sub class {
+	my $self = shift;
+	if(@_) { $self->{CLASS} = shift; }
+	return $self->{CLASS};
+}
+
+sub scope {
+	my $self = shift;
+	if(@_) { $self->{SCOPE} = shift; }
+	return $self->{SCOPE};
+}
+
+sub return {
+	my $self = shift;
+	if(@_) { $self->{RETURN} = shift; }
+	return $self->{RETURN};
+}
+
+sub groupIdentifier {
+	my $self = shift;
+	return $self->class->group->identifier;
+}
+
+sub selectorParts {
+	my $self = shift;
+	if(@_) { @{$self->{SELECTOR_PARTS}} = @_; }
+	return $self->{SELECTOR_PARTS};
+}
+
+sub setNew {
+	my $self = shift;
+	if(@_) { $self->{NEW} = shift; }
+	return $self->{NEW};
+}
+
+sub isNew {
+	my $self = shift;
+	return $self->{NEW};
+}
+
+sub type {
+	my $self = shift;
+	if(@_) { $self->{TYPE} = shift; }
+	return $self->{TYPE};
+}
+
+sub argnames {
+	my $self = shift;
+	return $self->{ARGNAMES};
+}
+
+sub argtypes {
+	my $self = shift;
+	return $self->{ARGTYPES};
+}
+##### #
+# END #
+# #####
+
+sub numArgs {
+	my $self = shift;
+	return scalar @{$self->{ARGTYPES}};
+}
+
+sub addArgument {
+	my $self = shift;
+	my ($type, $name) = @_;
+	push(@{$self->{ARGTYPES}}, $type);
+	push(@{$self->{ARGNAMES}}, $name);
+}
+
+sub selector {
+	my $self = shift;
+	if($self->numArgs == 0) {
+		return $self->{SELECTOR_PARTS}[0];
+	} else {
+		return join(":", @{$self->{SELECTOR_PARTS}}).":";
+	}
+}
+
+sub _new_selector {
+	my $self = shift;
+	if($self->numArgs == 0) {
+		return $self->{SELECTOR_PARTS}[0];
+	} else {
+		return join("\$", @{$self->{SELECTOR_PARTS}})."\$";
+	}
+}
+
+sub methodFamily {
+	my $self = shift;
+	my $selector = $self->selector;
+	if ($self->scope eq "+") {
+		if ($selector =~ /^alloc($|[A-Z,:])/) {
+			return "alloc" if $self->return eq "id" || $self->return eq "instancetype";
+		}
+		if ($selector eq "new") {
+			return "new" if $self->return eq "id" || $self->return eq "instancetype";
+		}
+	} else {
+		if ($selector =~ /^init($|[A-Z,:])/) {
+			return "init" if $self->return eq "id" || $self->return eq "instancetype";
+		}
+		if (($selector eq "copy") || ($selector eq "copyWithZone:")) {
+			return "copy";
+		}
+		if (($selector eq "mutableCopy") || ($selector eq "mutableCopyWithZone:")) {
+			return "mutableCopy";
+		}
+	}
+	return "";
+}
+
+sub printArgForArgType {
+	my $argtype = shift;
+	my $argname = shift;
+
+	my ($formatchar, $fallthrough) = formatCharForArgType($argtype);
+	return undef if $formatchar eq "--";
+
+	$argtype =~ s/^\s+//g;
+	$argtype =~ s/\s+$//g;
+
+	return "NSStringFromSelector($argname)" if $argtype =~ /^SEL$/;
+	return "$argname" if $argtype =~ /^Class$/;
+	return "$argname.location, $argname.length" if $argtype =~ /^NSRange$/;
+	return "$argname.origin.x, $argname.origin.y, $argname.size.width, $argname.size.height" if $argtype =~ /^(CG|NS)Rect$/;
+	return "$argname.x, $argname.y" if $argtype =~ /^(CG|NS)Point$/;
+	return "$argname.width, $argname.height" if $argtype =~ /^(CG|NS)Size$/;
+	return "(long)$argname" if $argtype =~ /^NS(Integer|SocketNativeHandle|StringEncoding|SortOptions|ComparisonResult|EnumerationOptions|(Hash|Map)TableOptions|SearchPath(Directory|DomainMask))$/i;
+	return "(unsigned long)$argname" if $argtype =~ /^NSUInteger$/i;
+
+	return ($fallthrough ? "(unsigned int)" : "").$argname;
+}
+
+sub formatCharForArgType {
+	local $_ = shift;
+	s/^\s+//g;
+	s/\s+$//g;
+
+	# Integral Types
+	# Straight characters get %c. Signed/Unsigned characters get %hhu/%hhd.
+	return "'%c'" if /^char$/;
+	if(/^((signed|unsigned)\s+)?(unsigned|signed|int|long|long\s+long|bool|BOOL|_Bool|char|short)$/) {
+		my $conversion = "d";
+		$conversion = "u" if /\bunsigned\b/;
+
+		my $length;
+		$length = "" if /\bint\b/;
+		$length = "l" if /\blong\b/;
+		$length = "ll" if /\blong long\b/;
+		$length = "h" if /\bshort\b/;
+		$length = "hh" if /\bchar\b/;
+
+		return "%".$length.$conversion;
+	}
+	return "%ld" if /^NS(Integer|SocketNativeHandle|StringEncoding|SortOptions|ComparisonResult|EnumerationOptions|(Hash|Map)TableOptions|SearchPath(Directory|DomainMask))$/i;
+	return "%lu" if /^NSUInteger$/i;
+	return "%d" if /^GS(FontTraitMask)$/i;
+
+	# Pointer Types
+	return "%s" if /^char\s*\*$/;
+	return "%p" if /^void\s*\*$/; # void *
+	return "%p" if /^id\s*\*$/; # id *
+	return "%p" if /^((unsigned|signed)\s+)?(unsigned|signed|int|long|long\s+long|bool|BOOL|_Bool|char|short|float|double)\s*\*+$/;
+	return "%p" if /^NS.*?(Pointer|Array)$/;
+	return "%p" if /^NSZone\s*\*$/;
+	return "%p" if /^struct.*\*$/; # struct pointer
+	return "%p" if /\*\*+$/; # anything with more than one pointer indirection
+	return "%p" if /\[.*\]$/; # any array
+
+	# Objects
+	return "%@" if /^id$/; # id is an objc_object.
+	return "%@" if /^\w+\s*\*$/; # try to treat *any* other pointer as an objc_object.
+	return "%@" if /^\w+Ref$/; # *Ref can be printed with %@.
+
+	# Floating-Point Types
+	return "%f" if /^(double|float|CGFloat|CGDouble|NSTimeInterval)$/;
+
+	# Special Types (should also have an entry in printArgForArgType
+	return "%@" if /^SEL$/;
+	return "%@" if /^Class$/;
+
+	# Even-more-special expanded types
+	return "(%d:%d)" if /^NSRange$/;
+	return "{{%g, %g}, {%g, %g}}" if /^(CG|NS)Rect$/;
+	return "{%g, %g}" if /^(CG|NS)Point$/;
+	return "{%g, %g}" if /^(CG|NS)Size$/;
+
+	# Discarded Types
+	return "--" if /^(CG\w*|CF\w*|void)$/;
+	return "--" if /^NS(HashTable(Callbacks)?|Map(Table((Key|Value)Callbacks)?|Enumerator))$/;
+	return "--" if /^struct/; # structs that aren't covered by 'struct ... *'
+
+	# Fallthrough - Treat everything we don't understand as POD.
+	return ("0x%x", 1) if wantarray; # The 1 is the fallthrough flag - used to signal to argName(...) that we should be casting.
+	return "0x%x";
+}
+
+sub typeEncodingForArgType {
+	local $_ = shift;
+	s/^\s+//g;
+	s/\s+$//g;
+
+	return "c" if /^char$/;
+	return "i" if /^int$/;
+	return "s" if /^short$/;
+	return "l" if /^long$/;
+	return "q" if /^long long$/;
+
+	return "C" if /^unsigned\s+char$/;
+	return "I" if /^unsigned\s+int$/;
+	return "S" if /^unsigned\s+short$/;
+	return "L" if /^unsigned\s+long$/;
+	return "Q" if /^unsigned\s+long long$/;
+
+	return "f" if /^float$/;
+	return "d" if /^double$/;
+	return "B" if /^(bool|_Bool)$/;
+
+	return "v" if /^void$/;
+
+	return "*" if /^char\s*\*$/;
+
+	return "@" if /^id$/;
+	return "@" if /^instancetype$/;
+	return "#" if /^Class$/;
+	return ":" if /^SEL$/;
+
+	if(/^([^*\s]+)\s*\*$/) {
+		my $subEncoding = typeEncodingForArgType($1);
+		return undef if(!defined $subEncoding);
+		return "^".$subEncoding;
+	}
+
+	return undef;
+}
+
+sub declarationForTypeWithName {
+	my $argtype = shift;
+	my $argname = shift;
+	if($argtype !~ /\(\s*[*^]/) {
+		return $argtype." ".$argname;
+	}
+	my $substring = $argtype;
+	my ($opening, $closing) = matchedParenthesisSet($substring, 0);
+	my $offset = 0;
+	while(1) {
+		# We want to put the parameter name right before the closing ) in the deepest nested set if we found a (^ or (*.
+		$substring = substr($substring, $opening, $closing - $opening - 1);
+		$offset += $opening;
+		my ($newopening, $newclosing) = matchedParenthesisSet($substring, 0);
+		last if !defined $newopening;
+		$opening = $newopening;
+		$closing = $newclosing;
+	}
+	my $out = $argtype;
+	substr($out, $offset-$opening+$closing-1, 0, $argname);
+	return $out;
+}
+
+1;

+ 98 - 0
theos/bin/lib/Logos/Patch.pm

@@ -0,0 +1,98 @@
+package Logos::Patch;
+use strict;
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = {};
+	$self->{LINE} = -1;
+	$self->{RANGE} = [];
+	$self->{SOURCE} = undef;
+	$self->{SQUASH} = 0;
+	bless($self, $class);
+	return $self;
+}
+
+##################### #
+# Setters and Getters #
+# #####################
+sub line {
+	my $self = shift;
+	if(@_) { $self->{LINE} = shift; }
+	return $self->{LINE};
+}
+
+sub range {
+	my $self = shift;
+	if(@_) { @{$self->{RANGE}} = @_; }
+	return $self->{RANGE};
+}
+
+sub start {
+	my $self = shift;
+	if(@_) { $self->{RANGE}[0] = shift; }
+	return $self->{RANGE}[0];
+}
+
+sub end {
+	my $self = shift;
+	if(@_) { $self->{RANGE}[1] = shift; }
+	return $self->{RANGE}[1];
+}
+
+sub source {
+	my $self = shift;
+	if(@_) { $self->{SOURCE} = shift; }
+	return $self->{SOURCE};
+}
+
+sub squash {
+	my $self = shift;
+	if(@_) { $self->{SQUASH} = shift; }
+	return $self->{SQUASH};
+}
+
+##### #
+# END #
+# #####
+
+sub evalSource {
+	my $self = shift;
+	my $source = shift;
+	my $sourceref = ref($source);
+	my @lines;
+	if($sourceref) {
+		if($sourceref eq "ARRAY") {
+			for(@$source) {
+				splice(@lines, scalar @lines, 0, $self->evalSource($_));
+			}
+		} else {
+			push(@lines, $source->eval());
+		}
+	} else {
+		push(@lines, $source);
+	}
+	return @lines;
+}
+
+sub apply {
+	my $self = shift;
+	my $lref = shift;
+	my $line = $self->{LINE};
+	my ($start, $end) = @{$self->{RANGE}};
+	my $source = $self->{SOURCE};
+	my @lines = $self->evalSource($source);
+	if(!defined $start) {
+		push(@lines, ::generateLineDirectiveForPhysicalLine($line));
+		if($self->{SQUASH}) {
+			push(@$lref, join('', @lines));
+		} else {
+			splice(@$lref, $line, 0, @lines);
+		}
+	} else {
+		substr($lref->[$line], $start, $end-$start) = join('', @lines);
+	}
+	return;
+}
+
+1;

+ 27 - 0
theos/bin/lib/Logos/Patch/Source/Generator.pm

@@ -0,0 +1,27 @@
+package Logos::Patch::Source::Generator;
+use strict;
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = {};
+	$self->{OBJECT} = shift;
+	$self->{METHOD} = shift;
+	my @args = @_;
+	$self->{ARGS} = \@args;
+	bless($self, $class);
+	return $self;
+}
+
+sub eval {
+	#no strict 'refs';
+	my $self = shift;
+	my @args = @{$self->{ARGS}};
+	splice(@args, 0, 0, $self->{OBJECT}) if $self->{OBJECT};
+	return Logos::Generator::for($self->{OBJECT})->can($self->{METHOD})->(@args);
+	#my $thunk = Logos::Generator::for($self->{OBJECT})->can($self->{METHOD})-(>${$self->{ARGS}});;
+	#my $mname = $self->{METHOD};
+	#return $thunk->$mname(@{$self->{ARGS}});
+}
+
+1;

+ 69 - 0
theos/bin/lib/Logos/Property.pm

@@ -0,0 +1,69 @@
+package Logos::Property;
+use strict;
+
+##################### #
+# Setters and Getters #
+# #####################
+
+sub new{
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = {};
+	$self->{CLASS} = undef;
+	$self->{GROUP} = undef;
+	$self->{NAME} = undef;
+	$self->{TYPE} = undef;
+	$self->{NUMATTR} = undef;
+	$self->{ASSOCIATIONPOLICY} = undef;
+	$self->{ATTRIBUTES} = [];
+	bless($self, $class);
+	return $self;
+}
+
+sub class {
+	my $self = shift;
+	if(@_) { $self->{CLASS} = shift; }
+	return $self->{CLASS};
+}
+
+sub group {
+	my $self = shift;
+	if(@_) { $self->{GROUP} = shift; }
+	return $self->{GROUP};
+}
+
+sub name {
+	my $self = shift;
+	if(@_) { $self->{NAME} = shift; }
+	return $self->{NAME};
+}
+
+sub type {
+	my $self = shift;
+	if(@_) { $self->{TYPE} = shift; }
+	return $self->{TYPE};
+}
+
+sub numattr {
+	my $self = shift;
+	if(@_) { $self->{NUMATTR} = shift; }
+	return $self->{NUMATTR};
+}
+
+sub attributes {
+	my $self = shift;
+	if(@_) { @{$self->{ATTRIBUTES}} = @_; }
+	return $self->{ATTRIBUTES};
+}
+
+sub associationPolicy {
+	my $self = shift;
+	if (@_) { $self->{ASSOCIATIONPOLICY} = shift; }
+	return $self->{ASSOCIATIONPOLICY};
+}
+
+##### #
+# END #
+# #####
+
+1;

+ 51 - 0
theos/bin/lib/Logos/StaticClassGroup.pm

@@ -0,0 +1,51 @@
+package Logos::StaticClassGroup;
+use Logos::Group;
+our @ISA = ('Logos::Group');
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = Logos::Group->new();
+	$self->name("_staticClass");
+	$self->explicit(0);
+	$self->{DECLAREDONLYCLASSES} = {};
+	$self->{USEDCLASSES} = {};
+	$self->{USEDMETACLASSES} = {};
+	bless($self, $class);
+	return $self;
+}
+
+sub addUsedClass {
+	my $self = shift;
+	my $class = shift;
+	$self->{USEDCLASSES}{$class}++;
+}
+
+sub addUsedMetaClass {
+	my $self = shift;
+	my $class = shift;
+	$self->{USEDMETACLASSES}{$class}++;
+}
+
+sub addDeclaredOnlyClass {
+	my $self = shift;
+	my $class = shift;
+	$self->{DECLAREDONLYCLASSES}{$class}++;
+}
+
+sub declaredOnlyClasses {
+	my $self = shift;
+	return $self->{DECLAREDONLYCLASSES};
+}
+
+sub usedClasses {
+	my $self = shift;
+	return $self->{USEDCLASSES};
+}
+
+sub usedMetaClasses {
+	my $self = shift;
+	return $self->{USEDMETACLASSES};
+}
+
+1;

+ 67 - 0
theos/bin/lib/Logos/Subclass.pm

@@ -0,0 +1,67 @@
+package Logos::Subclass;
+use Logos::Class;
+our @ISA = ('Logos::Class');
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = $class->SUPER::new();
+	$self->{SUPERCLASS} = undef;
+	$self->{PROTOCOLS} = {};
+	$self->{IVARS} = [];
+	$self->{OVERRIDDEN} = 1;
+	bless($self, $class);
+	return $self;
+}
+
+##################### #
+# Setters and Getters #
+# #####################
+sub superclass {
+	my $self = shift;
+	if(@_) { $self->{SUPERCLASS} = shift; }
+	return $self->{SUPERCLASS};
+}
+
+sub ivars {
+	my $self = shift;
+	return $self->{IVARS};
+}
+
+sub protocols {
+	my $self = shift;
+	return $self->{PROTOCOLS};
+}
+
+sub initRequired {
+	my $self = shift;
+	return 1; # Subclasses must always be initialized.
+}
+
+##### #
+# END #
+# #####
+
+sub addProtocol {
+	my $self = shift;
+	my $protocol = shift;
+	$self->{PROTOCOLS}{$protocol}++;
+}
+
+sub addIvar {
+	my $self = shift;
+	my $ivar = shift;
+	$ivar->class($self);
+	push(@{$self->{IVARS}}, $ivar);
+}
+
+sub getIvarNamed {
+	my $self = shift;
+	my $name = shift;
+	foreach(@{$self->{IVARS}}) {
+		return $_ if $_->name eq $name;
+	}
+	return undef;
+}
+
+1;

+ 113 - 0
theos/bin/lib/Logos/Util.pm

@@ -0,0 +1,113 @@
+package Logos::Util;
+use 5.006;
+use strict;
+our @ISA = ('Exporter');
+our @EXPORT = qw(quotes fallsBetween sanitize matchedParenthesisSet nestedParenString smartSplit);
+our $errorhandler = \&_defaultErrorHandler;
+
+sub _defaultErrorHandler {
+	die shift;
+}
+
+sub quotes {
+	my ($line) = @_;
+	my @quotes = ();
+	while($line =~ /(?<!\\)\"/g) {
+		push(@quotes, $-[0]);
+	}
+	return @quotes;
+}
+
+sub fallsBetween {
+	my $idx = shift;
+	while(@_ > 0) {
+		my $start = shift;
+		my $end = shift;
+		return 1 if ($start < $idx && (!defined($end) || $end > $idx))
+	}
+	return 0;
+}
+
+sub sanitize {
+	my $input = shift;
+	my $output = $input;
+	$output =~ s/[^\w]//g;
+	return $output;
+}
+
+sub matchedParenthesisSet {
+	my $in = shift;
+	my $atstart = shift;
+	$atstart = 1 if !defined $atstart;
+	my $untilend = shift;
+	$untilend = 0 if !defined $untilend;
+
+	my @parens;
+	if(!$atstart || $in =~ /^\s*\(/) {
+		# If we encounter a ) that puts us back at zero, we found a (
+		# and have reached its closing ).
+		my $parenmatch = $in;
+		my $pdepth = 0;
+		my @pquotes = quotes($parenmatch);
+		while($parenmatch =~ /[;()]/g) {
+			next if fallsBetween($-[0], @pquotes);
+
+			if($& eq "(") {
+				if($pdepth == 0) { push(@parens, $+[0]); }
+				$pdepth++;
+			} elsif($& eq ")") {
+				$pdepth--;
+				if($pdepth == 0) { push(@parens, $+[0]); last if(!$untilend); }
+			}
+		}
+	}
+
+	return undef if scalar @parens == 0;
+	# Odd number of parens means a closing paren was left off!
+	&$errorhandler("missing closing parenthesis") if scalar @parens % 2 == 1;
+	return @parens;
+}
+
+sub nestedParenString {
+	my $in = shift;
+	my ($opening, $closing) = matchedParenthesisSet($in);
+
+	my @ret;
+	if(defined $opening) {
+		$ret[0] = substr($in, $opening, $closing - $opening - 1);
+		$in = substr($in, $closing);
+	}
+	$ret[1] = $in;
+	return @ret;
+}
+
+sub smartSplit {
+	my $re = shift;
+	my $in = shift;
+	return () if !$in || $in eq "";
+
+	my $limit = shift;
+	$limit = 0 if !defined $limit;
+
+	my @quotes = quotes($in);
+	# We pass 1 for arg 3 to catch all matching parentheses until the end of the string
+	# as smartSplit only operates on a substring.
+	my @parens = matchedParenthesisSet($in, 0, 1);
+
+	my $lstart = 0;
+	my @pieces = ();
+	my $piece = "";
+	while($in =~ /$re/g) {
+		next if (defined $parens[0] && fallsBetween($-[0], @parens)) || fallsBetween($-[0], @quotes);
+		$piece = substr($in, $lstart, $-[0]-$lstart);
+		push(@pieces, $piece);
+		$lstart = $+[0];
+		$limit--;
+		last if($limit == 1); # One item left? Bail out and throw the rest of the string into it!
+	}
+	$piece = substr($in, $lstart);
+	push(@pieces, $piece);
+	return @pieces;
+}
+
+1;

+ 120 - 0
theos/bin/lib/NIC/Bridge/Context.pm

@@ -0,0 +1,120 @@
+package NIC::Bridge::Context;
+use strict;
+use warnings;
+use subs qw(warn exit);
+use Module::Load::Conditional qw(can_load);
+use NIC::Tie::PrefixedHandleRedirect;
+use NIC::Bridge::NICBase;
+
+our %handlers = (
+	PROMPT => sub { },
+);
+
+sub import {
+	my $package = shift;
+	my %arg = @_;
+	for(keys %arg) {
+		$handlers{$_} = $arg{$_};
+	}
+}
+
+our $bridge = undef;
+our $global_ret = undef;
+our $errored_out = undef;
+
+sub NIC {
+	return $bridge;
+}
+
+sub warn(@) {
+	print STDERR "[".$bridge->{FOR}->name."/warning] ",@_,$/;
+}
+
+sub error(@) {
+	print STDERR "[".$bridge->{FOR}->name."/error] ",@_,$/;
+	$errored_out = 1;
+	die;
+}
+
+sub exit {
+	$global_ret = shift;
+	die;
+}
+
+sub prompt {
+	__PACKAGE__->_prompt($bridge->{FOR}, undef, @_);
+}
+
+sub _prompt {
+	my $self = shift;
+	my $nic = shift;
+
+	my $n = scalar @_;
+	my $opts = $_[$n-1];
+	if(ref $opts eq "HASH") {
+		$n--;
+	} else {
+		$opts = {};
+	}
+
+	my $variable;
+	$variable = shift unless $n == 1;
+	my $promptstring = shift;
+
+	$handlers{PROMPT}->($nic, $variable, $promptstring, $opts->{default});
+}
+
+sub _wrap {
+	my $self = shift;
+	my @r = map {
+		my $wrap = $_;
+		my $_wrapType = $wrap ? (ref $wrap) : "_Undefined";
+		if(!$_wrapType || (ref($wrap) && $wrap->isa("NIC::Bridge::_BridgedObject"))) {
+			return $wrap;
+		} else {
+			$_wrapType =~ s/.*:://;
+			my $wrappingClass = "NIC::Bridge::$_wrapType";
+			can_load(modules=>{$wrappingClass=>undef}, verbose=>0) or return undef;
+			my $wrapper = $wrappingClass->new($self, $wrap);
+			return $wrapper;
+		}
+	} (@_);
+	return @r if wantarray;
+	return (@r > 0 ? $r[0] : undef);
+}
+
+sub _unwrap {
+	my $self = shift;
+	my @r = map { (ref($_) && $_->isa("NIC::Bridge::_BridgedObject")) ? $_->{FOR} : $_; } (@_);
+	return @r if wantarray;
+	return (@r > 0 ? $r[0] : undef);
+}
+
+sub _execute {
+	my $self = shift;
+	my $nic = shift;
+	my $script = shift;
+	my $ret = 1;
+	{
+		local $global_ret;
+		local $errored_out;
+		local $bridge = NIC::Bridge::NICBase->new($self, $nic);
+		local $SIG{__DIE__} = sub { };
+		tie *OVERRIDE, "NIC::Tie::PrefixedHandleRedirect", *STDERR, $nic->name;
+		my $stdout = select(*OVERRIDE);
+		eval("#line 1 ".$nic->name."/control.pl\n".$script);
+		select($stdout);
+		if(defined $errored_out) {
+			$ret = 0;
+		} elsif(defined $global_ret) {
+			$ret = $global_ret;
+			print STDERR "[".$nic->name."/error] Control script exited with status $ret.",$/;
+		} elsif($@) {
+			$ret = 0;
+			print STDERR "[".$nic->name."/error] Control script exited due to an error: $@";
+		}
+	}
+	return $ret;
+}
+
+1;

+ 6 - 0
theos/bin/lib/NIC/Bridge/Directory.pm

@@ -0,0 +1,6 @@
+package NIC::Bridge::Directory;
+use strict;
+use warnings;
+use parent qw(NIC::Bridge::NICType);
+
+1;

+ 13 - 0
theos/bin/lib/NIC/Bridge/File.pm

@@ -0,0 +1,13 @@
+package NIC::Bridge::File;
+use strict;
+use warnings;
+use parent qw(NIC::Bridge::NICType);
+use NIC::Tie::Method;
+
+sub data :lvalue {
+	my $self = shift;
+	tie my $tied, 'NIC::Tie::Method', $self->{FOR}, "data";
+	$tied;
+}
+
+1;

+ 76 - 0
theos/bin/lib/NIC/Bridge/NICBase.pm

@@ -0,0 +1,76 @@
+package NIC::Bridge::NICBase;
+use strict;
+use warnings;
+use parent qw(NIC::Bridge::_BridgedObject);
+use NIC::NICBase::Directory;
+use NIC::NICBase::File;
+use NIC::NICBase::Symlink;
+
+sub variables {
+	my $self = shift;
+	return keys %{$self->{FOR}->{VARIABLES}};
+}
+
+sub variable :lvalue {
+	my $self = shift;
+	$self->{FOR}->variable(@_);
+}
+
+sub mkdir {
+	my $self = shift;
+	my $dirname = shift;
+	my $mode = shift;
+	my $ref = $self->{FOR}->_getContent($dirname);
+	NIC::NICBase::Directory->take($ref);
+	$ref->mode($mode) if $mode;
+	return $self->{CONTEXT}->_wrap($ref);
+}
+
+sub mkfile {
+	my $self = shift;
+	my $name = shift;
+	my $mode = shift;
+	my $ref = $self->{FOR}->_getContent($name);
+	NIC::NICBase::File->take($ref);
+	$ref->mode($mode) if $mode;
+	return $self->{CONTEXT}->_wrap($ref);
+}
+
+sub symlink {
+	my $self = shift;
+	my $oldfile = shift;
+	my $newfile = shift;
+	my $ref = $self->{FOR}->_getContent($newfile);
+
+	my $realtarget = ref($oldfile) ? $self->{CONTEXT}->_unwrap($oldfile) : $self->{FOR}->_getContentWithoutCreate($oldfile);
+	$realtarget = $oldfile if !$realtarget;
+
+	NIC::NICBase::Symlink->take($ref, $realtarget);
+
+	return $self->{CONTEXT}->_wrap($ref);
+}
+
+sub lookup {
+	my $self = shift;
+	my $name = shift;
+	return $self->{CONTEXT}->_wrap($self->{FOR}->_getContentWithoutCreate($name));
+}
+
+sub setConstraint {
+	my $self = shift;
+	my $constraint = shift;
+	$self->{FOR}->addConstraint($constraint);
+}
+
+sub clearConstraint {
+	my $self = shift;
+	my $constraint = shift;
+	$self->{FOR}->removeConstraint($constraint);
+}
+
+sub prompt {
+	my $self = shift;
+	$self->{CONTEXT}->_prompt($self->{FOR}, @_);
+}
+
+1;

+ 29 - 0
theos/bin/lib/NIC/Bridge/NICType.pm

@@ -0,0 +1,29 @@
+package NIC::Bridge::NICType;
+use strict;
+use warnings;
+use parent qw(NIC::Bridge::_BridgedObject);
+use NIC::Tie::Method;
+
+sub name :lvalue {
+	my $self = shift;
+	tie my $tied, 'NIC::Tie::Method', $self->{FOR}, "name";
+	$tied;
+}
+
+sub mode :lvalue {
+	my $self = shift;
+	tie my $tied, 'NIC::Tie::Method', $self->{FOR}, "mode";
+	$tied;
+}
+
+sub constraints {
+	my $self = shift;
+	return $self->{FOR}->constraints;
+}
+
+sub constrain {
+	my $self = shift;
+	$self->{FOR}->addConstraint(shift);
+}
+
+1;

+ 13 - 0
theos/bin/lib/NIC/Bridge/Symlink.pm

@@ -0,0 +1,13 @@
+package NIC::Bridge::Symlink;
+use strict;
+use warnings;
+use parent qw(NIC::Bridge::NICType);
+use NIC::Bridge::Tie::WrappedMethod;
+
+sub target :lvalue {
+	my $self = shift;
+	tie my $tied, 'NIC::Bridge::Tie::WrappedMethod', $self->{CONTEXT}, $self->{FOR}, "target";
+	$tied;
+}
+
+1;

+ 28 - 0
theos/bin/lib/NIC/Bridge/Tie/WrappedMethod.pm

@@ -0,0 +1,28 @@
+package NIC::Bridge::Tie::WrappedMethod;
+use strict;
+use warnings;
+use parent qw(Tie::Scalar);
+
+sub TIESCALAR {
+	my $class = shift;
+	my $self = { CONTEXT => shift, FOR => shift, METHOD => shift };
+	bless($self, $class);
+	return $self;
+}
+
+sub FETCH {
+	my $self = shift;
+	my $obj = $self->{FOR};
+	my $ret = $self->{FOR}->can($self->{METHOD})->($self->{FOR});
+	return $self->{CONTEXT}->_wrap($ret);
+}
+
+sub STORE {
+	my $self = shift;
+	my $obj = $self->{FOR};
+	my $in = shift;
+	$self->{FOR}->can($self->{METHOD})->($self->{FOR}, $self->{CONTEXT}->_unwrap($in));
+}
+
+1;
+

+ 19 - 0
theos/bin/lib/NIC/Bridge/_BridgedObject.pm

@@ -0,0 +1,19 @@
+package NIC::Bridge::_BridgedObject;
+use strict;
+use warnings;
+
+use overload '""' => sub {
+	my $self = shift;
+	return "[".($self->{FOR}//"(undefined)")."]";
+};
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = { CONTEXT => shift, FOR => shift };
+	bless($self, $proto);
+	return $self;
+}
+
+
+1;

+ 21 - 0
theos/bin/lib/NIC/Bridge/_Undefined.pm

@@ -0,0 +1,21 @@
+package NIC::Bridge::_Undefined;
+use strict;
+use warnings;
+use parent qw(NIC::Bridge::_BridgedObject);
+
+use overload "bool" => sub {
+	return 0;
+};
+
+our $AUTOLOAD;
+sub AUTOLOAD {
+	my $method = $AUTOLOAD;
+	$method =~ s/.*:://;
+	NIC::Bridge::Context::error("Method '$method' called on nonexistent NIC Bridge object.");
+}
+
+sub DESTROY {
+	my $self = shift;
+}
+
+1;

+ 60 - 0
theos/bin/lib/NIC/Formats/NIC1.pm

@@ -0,0 +1,60 @@
+package NIC::Formats::NIC1;
+use parent NIC::NICBase;
+use strict;
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+
+	my $fh = shift;
+	my $self = NIC::NICBase->new(@_);
+	bless($self, $class);
+
+	$self->load($fh);
+	return $self;
+}
+
+sub _processLine {
+	my $self = shift;
+	my $fh = shift;
+	local $_ = shift;
+	if(/^name \"(.*)\"$/) {
+		$self->name($1);
+	} elsif(/^dir (.+)$/) {
+		$self->registerDirectory($1);
+	} elsif(/^file (\d+) (.+)$/) {
+		my $lines = $1;
+		my $filename = $2;
+		my $fref = $self->registerFile($filename);
+		my $filedata = "";
+		while($lines > 0) {
+			$filedata .= <$fh>;
+			$lines--;
+		}
+		$fref->data($filedata);
+	} elsif(/^prompt (\w+) \"(.*?)\"( \"(.*?)\")?$/) {
+		my $key = $1;
+		my $prompt = $2;
+		my $default = $4 || undef;
+		$self->registerPrompt($key, $prompt, $default);
+	} elsif(/^symlink \"(.+)\" \"(.+)\"$/) {
+		my $name = $1;
+		my $dest = $2;
+		$self->registerSymlink($name, $dest);
+	} elsif(/^constrain file \"(.+)\" to (.+)$/) {
+		my $constraint = $2;
+		my $filename = $1;
+		$self->registerFileConstraint($filename, $constraint);
+	}
+}
+
+sub load {
+	my $self = shift;
+	my $fh = shift;
+	while(<$fh>) {
+		$self->_processLine($fh, $_);
+	}
+	$self->resolveSymlinks;
+}
+
+1;

+ 146 - 0
theos/bin/lib/NIC/Formats/NICTar.pm

@@ -0,0 +1,146 @@
+package NIC::Formats::NICTar;
+use parent NIC::NICBase;
+use strict;
+use NIC::Formats::NICTar::File;
+use NIC::Formats::NICTar::Directory;
+use NIC::Formats::NICTar::Symlink;
+use Archive::Tar;
+use File::Temp;
+use File::Path;
+use File::Spec;
+use NIC::Bridge::Context;
+$Archive::Tar::WARN = 0;
+
+sub new {
+	my $proto = shift;
+	my $fh_or_tar = shift;
+	my $class = ref($proto) || $proto;
+
+	my $tar = ref($fh_or_tar) eq "Archive::Tar" ? $fh_or_tar : Archive::Tar->new($fh_or_tar);
+	return undef if(!$tar);
+
+	my $control = _fileFromTar(undef, $tar, "NIC/control");
+	return undef if(!$control);
+
+	my $self = NIC::NICBase->new(@_);
+	$self->{_TAR} = $tar;
+	bless($self, $class);
+
+	$self->_processData($control->get_content);
+	$self->load();
+
+	return $self;
+}
+
+sub _fileClass { "NIC::Formats::NICTar::File"; }
+sub _directoryClass { "NIC::Formats::NICTar::Directory"; }
+sub _symlinkClass { "NIC::Formats::NICTar::Symlink"; }
+
+sub _fileFromTar {
+	my $self = shift;
+	my $tar = $self ? $self->{_TAR} : shift;
+	my $filename = shift;
+	my @_tarfiles = $tar->get_files("./$filename", $filename);
+	return (scalar @_tarfiles > 0) ? $_tarfiles[0] : undef;
+}
+
+sub _processData {
+	my $self = shift;
+	my $data = shift;
+	for(split /\n\r?/, $data) {
+		$self->_processLine($_);
+	}
+}
+
+sub _processLine {
+	my $self = shift;
+	local $_ = shift;
+	if(/^name\s+\"(.*)\"$/ || /^name\s+(.*)$/) {
+		$self->name($1);
+	} elsif(/^prompt (\w+) \"(.*?)\"( \"(.*?)\")?$/) {
+		my $key = $1;
+		my $prompt = $2;
+		my $default = $4 || undef;
+		$self->registerPrompt($key, $prompt, $default);
+	} elsif(/^constrain (file )?\"(.+)\" to (.+)$/) {
+		my $constraint = $3;
+		my $filename = $2;
+		$self->registerFileConstraint($filename, $constraint);
+	} elsif(/^ignore (\w+)$/) {
+		$self->ignoreVariable($1);
+	}
+}
+
+sub load {
+	my $self = shift;
+	for($self->{_TAR}->get_files()) {
+		next if !$_->full_path || $_->full_path =~ /^(\.\/)?NIC(\/|$)/;
+		my $n = $_->full_path;
+		$n =~ s/^\.\///;
+		next if length $n == 0;
+		if($_->is_dir) {
+			my $ref = $self->registerDirectory($n);
+			$ref->tarfile($_);
+		} elsif($_->is_symlink) {
+			my $target = $_->linkname;
+			$target =~ s/^\.\///;
+
+			my $ref = $self->registerSymlink($n, $target);
+			$ref->tarfile($_);
+		} elsif($_->is_file) {
+			my $ref = $self->registerFile($n);
+			$ref->tarfile($_);
+		}
+	}
+	$self->resolveSymlinks;
+}
+
+sub _execPackageScript {
+	my $self = shift;
+	my $script = shift;
+	my $tarfile = $self->_fileFromTar("NIC/$script");
+	return if !$tarfile || $tarfile->mode & 0500 != 0500;
+	my $filename = File::Spec->catfile($self->{_TEMPDIR}, $script);
+	my $nicfile = NIC::NICType->new($self, $filename);
+	$self->_fileClass->take($nicfile);
+	$nicfile->tarfile($tarfile);
+	$nicfile->create();
+	my $ret = system $filename $script;
+	return ($ret >> 8) == 0
+}
+
+sub prebuild {
+	my $self = shift;
+	return $self->_execPackageScript("prebuild");
+}
+
+sub postbuild {
+	my $self = shift;
+	return $self->_execPackageScript("postbuild");
+}
+
+sub exec {
+	my $self = shift;
+	my $_controlpl = $self->_fileFromTar("NIC/control.pl");
+	if($_controlpl) {
+		return NIC::Bridge::Context->_execute($self, $_controlpl->get_content);
+	}
+	return 1;
+}
+
+sub build {
+	my $self = shift;
+
+	for(keys %{$self->{VARIABLES}}) {
+		$ENV{"NIC_".$_} = $self->variable($_);
+	}
+
+	$self->{_TEMPDIR} = File::Temp::tempdir();
+
+	$self->SUPER::build(@_);
+
+	File::Path::rmtree($self->{_TEMPDIR});
+	$self->{_TEMPDIR} = undef;
+}
+
+1;

+ 11 - 0
theos/bin/lib/NIC/Formats/NICTar/Directory.pm

@@ -0,0 +1,11 @@
+package NIC::Formats::NICTar::Directory;
+use parent qw(NIC::Formats::NICTar::_TarMixin NIC::NICBase::Directory);
+use strict;
+
+sub _take_init {
+	my $self = shift;
+	$self->NIC::NICBase::Directory::_take_init(@_);
+	$self->NIC::Formats::NICTar::_TarMixin::_take_init(@_);
+}
+
+1;

+ 16 - 0
theos/bin/lib/NIC/Formats/NICTar/File.pm

@@ -0,0 +1,16 @@
+package NIC::Formats::NICTar::File;
+use parent qw(NIC::Formats::NICTar::_TarMixin NIC::NICBase::File);
+use strict;
+
+sub _take_init {
+	my $self = shift;
+	$self->NIC::NICBase::File::_take_init(@_);
+	$self->NIC::Formats::NICTar::_TarMixin::_take_init(@_);
+}
+
+sub data {
+	my $self = shift;
+	return $self->SUPER::data(@_) // $self->tarfile->get_content;
+}
+
+1;

+ 11 - 0
theos/bin/lib/NIC/Formats/NICTar/Symlink.pm

@@ -0,0 +1,11 @@
+package NIC::Formats::NICTar::Symlink;
+use parent NIC::NICBase::Symlink;
+use strict;
+
+sub tarfile {
+	my $self = shift;
+	if(@_) { $self->{TARFILE} = shift; }
+	return $self->{TARFILE};
+}
+
+1;

+ 20 - 0
theos/bin/lib/NIC/Formats/NICTar/_TarMixin.pm

@@ -0,0 +1,20 @@
+package NIC::Formats::NICTar::_TarMixin;
+use strict;
+
+sub _take_init {
+	my $self = shift;
+	$self->{TARFILE} = undef;
+}
+
+sub tarfile {
+	my $self = shift;
+	if(@_) { $self->{TARFILE} = shift; }
+	return $self->{TARFILE};
+}
+
+sub _mode {
+	my $self = shift;
+	return $self->tarfile->mode;
+}
+
+1;

+ 221 - 0
theos/bin/lib/NIC/NICBase.pm

@@ -0,0 +1,221 @@
+package NIC::NICBase;
+use strict;
+use warnings;
+
+use NIC::NICBase::File;
+use NIC::NICBase::Directory;
+use NIC::NICBase::Symlink;
+
+use List::Util qw(first);
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = {};
+	$self->{NAME} = shift;
+	$self->{CONTENTS} = [];
+	$self->{VARIABLES} = {};
+	$self->{CONSTRAINTS} = {};
+	$self->{PROMPTS} = [];
+	$self->{IGNORED_VARS} = {};
+	bless($self, $class);
+
+	return $self;
+}
+
+sub _fileClass { "NIC::NICBase::File"; }
+sub _directoryClass { "NIC::NICBase::Directory"; }
+sub _symlinkClass { "NIC::NICBase::Symlink"; }
+
+sub _getContentWithoutCreate {
+	my $self = shift;
+	my $name = shift;
+	return first { $_->name eq $name } @{$self->{CONTENTS}};
+}
+
+sub _getContent {
+	my $self = shift;
+	my $ref = $self->_getContentWithoutCreate(@_);
+	return $ref if $ref;
+	$ref = NIC::NICType->new($self, @_);
+	push(@{$self->{CONTENTS}}, $ref);
+	return $ref;
+}
+
+sub _generate {
+	my $self = shift;
+	my $class = shift;
+	my $name = shift;
+	my $ref = $self->_getContent($name, @_);
+	if($ref->type == NIC::NICType::TYPE_UNKNOWN) {
+		$class->take($ref, @_);
+	}
+	return $ref;
+}
+
+sub registerDirectory {
+	my $self = shift;
+	my $dir = $self->_generate($self->_directoryClass, @_);
+	return $dir;
+}
+
+sub registerFile {
+	my $self = shift;
+	my $file = $self->_generate($self->_fileClass, @_);
+	return $file;
+}
+
+sub registerSymlink {
+	my $self = shift;
+	my $symlink = $self->_generate($self->_symlinkClass, @_);
+	return $symlink;
+}
+
+sub registerPrompt {
+	my($self, $key, $prompt, $default) = @_;
+	push(@{$self->{PROMPTS}}, {
+			name => $key,
+			prompt => $prompt,
+			default => $default
+		});
+}
+
+sub registerFileConstraint {
+	my $self = shift;
+	my $filename = shift;
+	my $constraint = shift;
+	$self->_getContent($filename)->addConstraint($constraint);
+}
+
+sub resolveSymlinks {
+	my $self = shift;
+	for(@{$self->{CONTENTS}}) {
+		next unless $_->type == NIC::NICType::TYPE_SYMLINK;
+		next if $_->target_type != NIC::NICType::TYPE_UNKNOWN;
+		my $ref = $self->_getContentWithoutCreate($_->target);
+		$_->target($ref) if $ref;
+	}
+}
+
+sub variable: lvalue {
+	my $self = shift;
+	my $key = shift;
+	$self->{VARIABLES}->{$key};
+}
+
+sub name {
+	my $self = shift;
+	if(@_) { $self->{NAME} = shift; }
+	return $self->{NAME} // "(unnamed template)";
+}
+
+sub prompts {
+	my $self = shift;
+	return @{$self->{PROMPTS}};
+}
+
+sub addConstraint {
+	my $self = shift;
+	my $constraint = shift;
+	$self->{CONSTRAINTS}->{$constraint} = 1;
+}
+
+sub removeConstraint {
+	my $self = shift;
+	my $constraint = shift;
+	delete $self->{CONSTRAINTS}->{$constraint};
+}
+
+sub _constraintMatch {
+	my $self = shift;
+	my $constraint = shift;
+	my $negated = 0;
+	if(substr($constraint, 0, 1) eq "!") {
+		$negated = 1;
+		substr($constraint, 0, 1, "");
+	}
+	return 0 if(!$negated && (!defined $self->{CONSTRAINTS}->{$constraint} || $self->{CONSTRAINTS}->{$constraint} != 1));
+	return 0 if($negated && (defined $self->{CONSTRAINTS}->{$constraint} || $self->{CONSTRAINTS}->{$constraint} != 0));
+	return 1;
+}
+
+sub _meetsConstraints {
+	my $self = shift;
+	my $content = shift;
+	foreach ($content->constraints) {
+		return 0 if !$self->_constraintMatch($_);
+	}
+	return 1;
+}
+
+sub substituteVariables {
+	my $self = shift;
+	my $line = shift;
+	foreach my $key (keys %{$self->{VARIABLES}}) {
+		my $value = $self->{VARIABLES}->{$key};
+		$line =~ s/\@\@$key\@\@/$value/g;
+	}
+	return $line;
+}
+
+sub ignoreVariable {
+	my $self = shift;
+	my $var = shift;
+	$self->{IGNORED_VARS}->{$var}++;
+}
+
+sub variableIgnored {
+	my $self = shift;
+	my $var = shift;
+	return defined $self->{IGNORED_VARS}->{$var};
+}
+
+sub prebuild {
+
+}
+
+sub postbuild {
+
+}
+
+sub exec {
+	return 1;
+}
+
+sub build {
+	my $self = shift;
+	my $dir = shift;
+	mkdir($dir) or die "Failed to create the directory '$dir': $!\n";
+	chdir($dir) or die $!;
+	$self->prebuild();
+	foreach my $content (sort { $a->type <=> $b->type } (@{$self->{CONTENTS}})) {
+		next if $content->type == NIC::NICType::TYPE_UNKNOWN;
+		next if !$self->_meetsConstraints($content);
+		$content->create();
+	}
+	$self->postbuild();
+}
+
+sub dumpPreamble {
+	my $self = shift;
+	my $preamblefn = shift;
+	open(my $pfh, ">", $preamblefn);
+	print $pfh "name \"".$self->{NAME}."\"",$/;
+	foreach my $prompt (@{$self->{PROMPTS}}) {
+		print $pfh "prompt ".$prompt->{name}." \"".$prompt->{prompt}."\"";
+		print $pfh " \"".$prompt->{default}."\"" if defined $prompt->{default};
+		print $pfh $/;
+	}
+	foreach my $filename (keys %{$self->{FILES}}) {
+		my $file = $self->{FILES}->{$filename};
+		if(!defined $file->{constraints}) {
+			next;
+		}
+		foreach (@{$file->{constraints}}) {
+			print $pfh "constrain file \"".$filename."\" to ".$_,$/
+		}
+	}
+	close($pfh);
+}
+
+1;

+ 24 - 0
theos/bin/lib/NIC/NICBase/Directory.pm

@@ -0,0 +1,24 @@
+package NIC::NICBase::Directory;
+use strict;
+use warnings;
+use parent qw(NIC::NICType);
+use File::Path qw(mkpath);
+
+sub type {
+	my $self = shift;
+	return NIC::NICType::TYPE_DIRECTORY;
+}
+
+sub _mode {
+	return 0755;
+}
+
+sub create {
+	my $self = shift;
+	mkpath($self->{OWNER}->substituteVariables($self->{NAME}), { mode => $self->mode }) or return 0;
+	return 1;
+}
+
+
+1;
+

+ 36 - 0
theos/bin/lib/NIC/NICBase/File.pm

@@ -0,0 +1,36 @@
+package NIC::NICBase::File;
+use strict;
+use warnings;
+use parent qw(NIC::NICType);
+
+sub _take_init {
+	my $self = shift;
+	$self->{DATA} = undef;
+}
+
+sub type {
+	my $self = shift;
+	return NIC::NICType::TYPE_FILE;
+}
+
+sub _mode {
+	return 0644;
+}
+
+sub data {
+	my $self = shift;
+	$self->{DATA} = shift if @_;
+	$self->{DATA};
+}
+
+sub create {
+	my $self = shift;
+	my $filename = $self->{OWNER}->substituteVariables($self->name);
+	open(my $nicfile, ">", $filename) or return 0;
+	print $nicfile $self->{OWNER}->substituteVariables($self->data);
+	close($nicfile);
+	chmod($self->mode, $filename);
+	return 1;
+}
+
+1;

+ 52 - 0
theos/bin/lib/NIC/NICBase/Symlink.pm

@@ -0,0 +1,52 @@
+package NIC::NICBase::Symlink;
+use strict;
+use warnings;
+use parent qw(NIC::NICType);
+use Scalar::Util qw(refaddr);
+
+use overload '""' => sub {
+	my $self = shift;
+	my $ref = ref($self);
+	$ref =~ s/^.*::(\w+)$/$1/g;
+	my $target = (ref($self->target) && refaddr($self) == refaddr($self->target)) ? "itself" : "\"".$self->target."\"";
+	return '"'.$self->name."\" ($ref to $target)";
+};
+
+sub _take_init {
+	my $self = shift;
+	$self->{TARGET} = shift // undef;
+}
+
+sub type {
+	my $self = shift;
+	return NIC::NICType::TYPE_SYMLINK;
+}
+
+sub target {
+	my $self = shift;
+	if(@_) { $self->{TARGET} = shift; }
+	return $self->{TARGET};
+}
+
+sub target_type {
+	my $self = shift;
+	my $t = $self->{TARGET};
+	return ref($t) ? $t->type : NIC::NICType::TYPE_UNKNOWN;
+}
+
+sub target_name {
+	my $self = shift;
+	my $t = $self->{TARGET};
+	return ref($t) ? $t->name : $t;
+}
+
+sub create {
+	my $self = shift;
+	my $name = $self->{OWNER}->substituteVariables($self->{NAME});
+	my $dest = $self->{OWNER}->substituteVariables($self->target_name);
+	symlink($dest, $name) or return 0;
+	return 1;
+}
+
+1;
+

+ 79 - 0
theos/bin/lib/NIC/NICType.pm

@@ -0,0 +1,79 @@
+package NIC::NICType;
+use strict;
+use warnings;
+
+use constant {
+	TYPE_UNKNOWN => 0,
+	TYPE_DIRECTORY => 1,
+	TYPE_FILE => 2,
+	TYPE_SYMLINK => 3
+};
+
+use overload '""' => sub {
+	my $self = shift;
+	my $ref = ref($self);
+	$ref =~ s/^.*::(\w+)$/$1/g;
+	return '"'.$self->name."\" ($ref, mode ".sprintf("%4.04o", $self->mode).")";
+};
+
+sub new {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $self = {};
+	$self->{OWNER} = shift // undef;
+	$self->{NAME} = shift // undef;
+	$self->{MODE} = undef;
+	$self->{CONSTRAINTS} = [];
+	bless($self, $class);
+
+	return $self;
+}
+
+sub _take_init {
+}
+
+sub take {
+	my $proto = shift;
+	my $class = ref($proto) || $proto;
+	my $obj = shift;
+	bless($obj, $class);
+	$obj->_take_init(@_);
+}
+
+sub name {
+	my $self = shift;
+	if(@_) { $self->{NAME} = shift; }
+	return $self->{NAME};
+}
+
+sub _mode {
+	return 0;
+}
+
+sub mode {
+	my $self = shift;
+	if(@_) { $self->{MODE} = shift; }
+	return $self->{MODE} // $self->_mode;
+}
+
+sub type {
+	my $self = shift;
+	return TYPE_UNKNOWN;
+}
+
+sub constraints {
+	my $self = shift;
+	return @{$self->{CONSTRAINTS}};
+}
+
+sub addConstraint {
+	my $self = shift;
+	my $constraint = shift;
+	push(@{$self->{CONSTRAINTS}}, $constraint);
+}
+
+sub create {
+	return 0;
+}
+
+1;

+ 26 - 0
theos/bin/lib/NIC/Tie/Method.pm

@@ -0,0 +1,26 @@
+package NIC::Tie::Method;
+use parent qw(Tie::Scalar);
+
+sub TIESCALAR {
+	my $class = shift;
+	my $for = shift;
+	my $method = shift;
+	my $self = { FOR => $for, METHOD => $method };
+	bless($self, $class);
+	return $self;
+}
+
+sub FETCH {
+	my $self = shift;
+	my $obj = $self->{FOR};
+	{ unshift @_, $obj; goto &{$self->{FOR}->can($self->{METHOD})}; }
+}
+
+sub STORE {
+	my $self = shift;
+	my $obj = $self->{FOR};
+	{ unshift @_, $obj; goto &{$self->{FOR}->can($self->{METHOD})}; }
+}
+
+1;
+

+ 34 - 0
theos/bin/lib/NIC/Tie/PrefixedHandleRedirect.pm

@@ -0,0 +1,34 @@
+package NIC::Tie::PrefixedHandleRedirect;
+use strict;
+use parent qw(Tie::Handle);
+sub TIEHANDLE {
+	my $proto = shift;
+	my $fh = shift;
+	my $prefix = shift;
+	return bless [$fh, $prefix], $proto;
+}
+
+sub _token {
+	return "[".$_[0]->[1]."] ";
+}
+
+sub PRINT {
+	my $t = $_[0]->_token;
+	my $fh = $_[0]->[0];
+	shift;
+	my $str = $t.join('', @_);
+	$str =~ s#$/+$##g;
+	$str =~ s#$/#$/$t#g;
+	print $fh $str,$/;
+}
+
+sub PRINTF {
+	my $t = $_[0]->_token;
+	my $fh = $_[0]->[0];
+	shift;
+	my $str = $t.sprintf(shift, @_);
+	$str =~ s#$/+$##g;
+	$str =~ s#$/#$/$t#g;
+	print $fh $str,$/;
+}
+1;

+ 326 - 0
theos/bin/lib/aliased.pm

@@ -0,0 +1,326 @@
+package aliased;
+no warnings 'deprecated';
+
+our $VERSION = '0.30_01';
+$VERSION = eval $VERSION;
+
+require Exporter;
+@ISA    = qw(Exporter);
+@EXPORT = qw(alias prefix);
+
+use strict;
+
+sub _croak {
+    require Carp;
+    Carp::croak(@_);
+}
+
+sub import {
+    my ( $class, $package, $alias, @import ) = @_;
+
+    if ( @_ <= 1 ) {
+        $class->export_to_level(1);
+        return;
+    }
+
+    my $callpack = caller(0);
+    _load_alias( $package, $callpack, @import );
+    _make_alias( $package, $callpack, $alias );
+}
+
+sub _get_alias {
+    my $package = shift;
+    $package =~ s/.*(?:::|')//;
+    return $package;
+}
+
+sub _make_alias {
+    my ( $package, $callpack, $alias ) = @_;
+
+    $alias ||= _get_alias($package);
+
+    no strict 'refs';
+    *{ join q{::} => $callpack, $alias } = sub () { $package };
+}
+
+sub _load_alias {
+    my ( $package, $callpack, @import ) = @_;
+
+    # We don't localize $SIG{__DIE__} here because we need to be careful about
+    # restoring its value if there is a failure.  Very, very tricky.
+    my $sigdie = $SIG{__DIE__};
+    {
+        my $code =
+          @import == 0
+          ? "package $callpack; use $package;"
+          : "package $callpack; use $package (\@import)";
+        eval $code;
+        if ( my $error = $@ ) {
+            $SIG{__DIE__} = $sigdie;
+            _croak($error);
+        }
+        $sigdie = $SIG{__DIE__}
+          if defined $SIG{__DIE__};
+    }
+
+    # Make sure a global $SIG{__DIE__} makes it out of the localization.
+    $SIG{__DIE__} = $sigdie if defined $sigdie;
+    return $package;
+}
+
+sub alias {
+    my ( $package, @import ) = @_;
+
+    my $callpack = scalar caller(0);
+    return _load_alias( $package, $callpack, @import );
+}
+
+sub prefix {
+    my ($class) = @_;
+    return sub {
+        my ($name) = @_;
+        my $callpack = caller(0);
+        if ( not @_ ) {
+            return _load_alias( $class, $callpack );
+        }
+        elsif ( @_ == 1 && defined $name ) {
+            return _load_alias( "${class}::$name", $callpack );
+        }
+        else {
+            _croak("Too many arguments to prefix('$class')");
+        }
+    };
+}
+
+1;
+__END__
+
+=head1 NAME
+
+aliased - Use shorter versions of class names.
+
+=head1 VERSION
+
+0.30
+
+=head1 SYNOPSIS
+
+  # Class name interface
+  use aliased 'My::Company::Namespace::Customer';
+  my $cust = Customer->new;
+
+  use aliased 'My::Company::Namespace::Preferred::Customer' => 'Preferred';
+  my $pref = Preferred->new;
+
+
+  # Variable interface
+  use aliased;
+  my $Customer  = alias "My::Other::Namespace::Customer";
+  my $cust      = $Customer->new;
+
+  my $Preferred = alias "My::Other::Namespace::Preferred::Customer";
+  my $pref      = $Preferred->new;  
+
+
+=head1 DESCRIPTION
+
+C<aliased> is simple in concept but is a rather handy module.  It loads the
+class you specify and exports into your namespace a subroutine that returns
+the class name.  You can explicitly alias the class to another name or, if you
+prefer, you can do so implicitly.  In the latter case, the name of the
+subroutine is the last part of the class name.  Thus, it does something
+similar to the following:
+
+  #use aliased 'Some::Annoyingly::Long::Module::Name::Customer';
+
+  use Some::Annoyingly::Long::Module::Name::Customer;
+  sub Customer {
+    return 'Some::Annoyingly::Long::Module::Name::Customer';
+  }
+  my $cust = Customer->new;
+
+This module is useful if you prefer a shorter name for a class.  It's also
+handy if a class has been renamed.
+
+(Some may object to the term "aliasing" because we're not aliasing one
+namespace to another, but it's a handy term.  Just keep in mind that this is
+done with a subroutine and not with typeglobs and weird namespace munging.)
+
+Note that this is B<only> for C<use>ing OO modules.  You cannot use this to
+load procedural modules.  See the L<Why OO Only?|Why OO Only?> section.  Also,
+don't let the version number fool you.  This code is ridiculously simple and
+is just fine for most use.
+
+=head2 Implicit Aliasing
+
+The most common use of this module is:
+
+  use aliased 'Some::Module::name';
+
+C<aliased> will  allow you to reference the class by the last part of the
+class name.  Thus, C<Really::Long::Name> becomes C<Name>.  It does this by
+exporting a subroutine into your namespace with the same name as the aliased
+name.  This subroutine returns the original class name.
+
+For example:
+
+  use aliased "Acme::Company::Customer";
+  my $cust = Customer->find($id);
+
+Note that any class method can be called on the shorter version of the class
+name, not just the constructor.
+
+=head2 Explicit Aliasing
+
+Sometimes two class names can cause a conflict (they both end with C<Customer>
+for example), or you already have a subroutine with the same name as the
+aliased name.  In that case, you can make an explicit alias by stating the
+name you wish to alias to:
+
+  use aliased 'Original::Module::Name' => 'NewName';
+
+Here's how we use C<aliased> to avoid conflicts:
+
+  use aliased "Really::Long::Name";
+  use aliased "Another::Really::Long::Name" => "Aname";
+  my $name  = Name->new;
+  my $aname = Aname->new;
+
+You can even alias to a different package:
+
+  use aliased "Another::Really::Long::Name" => "Another::Name";
+  my $aname = Another::Name->new;
+
+Messing around with different namespaces is a really bad idea and you probably
+don't want to do this.  However, it might prove handy if the module you are
+using has been renamed.  If the interface has not changed, this allows you to
+use the new module by only changing one line of code.
+
+  use aliased "New::Module::Name" => "Old::Module::Name";
+  my $thing = Old::Module::Name->new;
+
+=head2 Import Lists
+
+Sometimes, even with an OO module, you need to specify extra arguments when
+using the module.  When this happens, simply use L<Explicit Aliasing> followed
+by the import list:
+
+Snippet 1:
+
+  use Some::Module::Name qw/foo bar/;
+  my $o = Some::Module::Name->some_class_method; 
+
+Snippet 2 (equivalent to snippet 1):
+
+  use aliased 'Some::Module::Name' => 'Name', qw/foo bar/;
+  my $o = Name->some_class_method;
+
+B<Note>:  remember, you cannot use import lists with L<Implicit Aliasing>.  As
+a result, you may simply prefer to only use L<Explicit Aliasing> as a matter
+of style.
+
+=head2 alias()
+
+This function is only exported if you specify C<use aliased> with no import
+list.
+
+    use aliased;
+    my $alias = alias($class);
+    my $alias = alias($class, @imports);
+
+alias() is an alternative to C<use aliased ...> which uses less magic and
+avoids some of the ambiguities.
+
+Like C<use aliased> it C<use>s the $class (pass in @imports, if given) but
+instead of providing an C<Alias> constant it simply returns a scalar set to
+the $class name.
+
+    my $thing = alias("Some::Thing::With::A::Long::Name");
+
+    # Just like Some::Thing::With::A::Long::Name->method
+    $thing->method;
+
+The use of a scalar instead of a constant avoids any possible ambiguity
+when aliasing two similar names:
+
+    # No ambiguity despite the fact that they both end with "Name"
+    my $thing = alias("Some::Thing::With::A::Long::Name");
+    my $other = alias("Some::Other::Thing::With::A::Long::Name");
+
+and there is no magic constant exported into your namespace.
+
+The only caveat is loading of the $class happens at run time.  If $class
+exports anything you might want to ensure it is loaded at compile time with:
+
+    my $thing;
+    BEGIN { $thing = alias("Some::Thing"); }
+
+However, since OO classes rarely export this should not be necessary.
+
+=head2 prefix() (experimental)
+
+This function is only exported if you specify C<use aliased> with no import
+list.
+
+    use aliased;
+
+Sometimes you find you have a ton of packages in the same top-level namespace
+and you want to alias them, but only use them on demand.  For example:
+
+    # instead of:
+    MailVerwaltung::Client::Exception::REST::Response->throw()
+
+    my $error = prefix('MailVerwaltung::Client::Exception');
+    $error->('REST::Response')->throw();   # same as above
+    $error->()->throw; # same as MailVerwaltung::Client::Exception->throw
+
+=head2 Why OO Only?
+
+Some people have asked why this code only support object-oriented modules
+(OO).  If I were to support normal subroutines, I would have to allow the
+following syntax:
+
+  use aliased 'Some::Really::Long::Module::Name';
+  my $data = Name::data();
+
+That causes a serious problem.  The only (reasonable) way it can be done is to
+handle the aliasing via typeglobs.  Thus, instead of a subroutine that
+provides the class name, we alias one package to another (as the
+L<namespace|namespace> module does.)  However, we really don't want to simply
+alias one package to another and wipe out namespaces willy-nilly.  By merely
+exporting a single subroutine to a namespace, we minimize the issue. 
+
+Fortunately, this doesn't seem to be that much of a problem.  Non-OO modules
+generally support exporting of the functions you need and this eliminates the
+need for a module such as this.
+
+=head1 EXPORT
+
+This modules exports a subroutine with the same name as the "aliased" name.
+
+=head1 BUGS
+
+There are no known bugs in this module, but feel free to email me reports.
+
+=head1 SEE ALSO
+
+The L<namespace> module.
+
+=head1 THANKS
+
+Many thanks to Rentrak, Inc. (http://www.rentrak.com/) for graciously allowing
+me to replicate the functionality of some of their internal code.
+
+=head1 AUTHOR
+
+Curtis Poe, C<< ovid [at] cpan [dot] org >>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright (C) 2005 by Curtis "Ovid" Poe
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself, either Perl version 5.8.5 or,
+at your option, any later version of Perl 5 you may have available.
+
+=cut

+ 136 - 0
theos/bin/lib/parent.pm

@@ -0,0 +1,136 @@
+package parent;
+use strict;
+use vars qw($VERSION);
+$VERSION = '0.225';
+
+sub import {
+    my $class = shift;
+
+    my $inheritor = caller(0);
+
+    if ( @_ and $_[0] eq '-norequire' ) {
+        shift @_;
+    } else {
+        for ( my @filename = @_ ) {
+            if ( $_ eq $inheritor ) {
+                warn "Class '$inheritor' tried to inherit from itself\n";
+            };
+
+            s{::|'}{/}g;
+            require "$_.pm"; # dies if the file is not found
+        }
+    }
+
+    {
+        no strict 'refs';
+        push @{"$inheritor\::ISA"}, @_;
+    };
+};
+
+"All your base are belong to us"
+
+__END__
+
+=encoding utf8
+
+=head1 NAME
+
+parent - Establish an ISA relationship with base classes at compile time
+
+=head1 SYNOPSIS
+
+    package Baz;
+    use parent qw(Foo Bar);
+
+=head1 DESCRIPTION
+
+Allows you to both load one or more modules, while setting up inheritance from
+those modules at the same time.  Mostly similar in effect to
+
+    package Baz;
+    BEGIN {
+        require Foo;
+        require Bar;
+        push @ISA, qw(Foo Bar);
+    }
+
+By default, every base class needs to live in a file of its own.
+If you want to have a subclass and its parent class in the same file, you
+can tell C<parent> not to load any modules by using the C<-norequire> switch:
+
+  package Foo;
+  sub exclaim { "I CAN HAS PERL" }
+
+  package DoesNotLoadFooBar;
+  use parent -norequire, 'Foo', 'Bar';
+  # will not go looking for Foo.pm or Bar.pm
+
+This is equivalent to the following code:
+
+  package Foo;
+  sub exclaim { "I CAN HAS PERL" }
+
+  package DoesNotLoadFooBar;
+  push @DoesNotLoadFooBar::ISA, 'Foo', 'Bar';
+
+This is also helpful for the case where a package lives within
+a differently named file:
+
+  package MyHash;
+  use Tie::Hash;
+  use parent -norequire, 'Tie::StdHash';
+
+This is equivalent to the following code:
+
+  package MyHash;
+  require Tie::Hash;
+  push @ISA, 'Tie::StdHash';
+
+If you want to load a subclass from a file that C<require> would
+not consider an eligible filename (that is, it does not end in
+either C<.pm> or C<.pmc>), use the following code:
+
+  package MySecondPlugin;
+  require './plugins/custom.plugin'; # contains Plugin::Custom
+  use parent -norequire, 'Plugin::Custom';
+
+=head1 DIAGNOSTICS
+
+=over 4
+
+=item Class 'Foo' tried to inherit from itself
+
+Attempting to inherit from yourself generates a warning.
+
+    package Foo;
+    use parent 'Foo';
+
+=back
+
+=head1 HISTORY
+
+This module was forked from L<base> to remove the cruft
+that had accumulated in it.
+
+=head1 CAVEATS
+
+=head1 SEE ALSO
+
+L<base>
+
+=head1 AUTHORS AND CONTRIBUTORS
+
+Rafaël Garcia-Suarez, Bart Lateur, Max Maischein, Anno Siegel, Michael Schwern
+
+=head1 MAINTAINER
+
+Max Maischein C< corion@cpan.org >
+
+Copyright (c) 2007-10 Max Maischein C<< <corion@cpan.org> >>
+Based on the idea of C<base.pm>, which was introduced with Perl 5.004_04.
+
+=head1 LICENSE
+
+This module is released under the same terms as Perl itself.
+
+=cut

+ 63 - 0
theos/bin/logify.pl

@@ -0,0 +1,63 @@
+#!/usr/bin/env perl
+# logify.pl
+############
+# Converts an Objective-C header file (or anything containing a @interface and method definitions)
+#+into a Logos input file which causes all function calls to be logged.
+#
+# Accepts input on stdin or via filename specified on the commandline.
+
+# Lines are only processed if we were in an @interface, so you can run this on a file containing
+# an @implementation, as well.
+use strict;
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Logos::Method;
+use Logos::Util;
+$Logos::Util::errorhandler = sub {
+	die "$ARGV:$.: error: missing closing parenthesis$/"
+};
+
+my $interface = 0;
+while(my $line = <>) {
+	if($line =~ m/^[+-]\s*\((.*?)\).*?(?=;)/ && $interface == 1) {
+		print logLineForDeclaration($&);
+	} elsif($line =~ m/^\s*\@property\s*\((.*?)\)\s*(.*?)\b([\$a-zA-Z_][\$_a-zA-Z0-9]*)(?=;)/ && $interface == 1) {
+		my @attributes = smartSplit(qr/\s*,\s*/, $1);
+		my $propertyName = $3;
+		my $type = $2;
+		my $readonly = scalar(grep(/readonly/, @attributes));
+		my %methods = ("setter" => "set".ucfirst($propertyName).":", "getter" => $propertyName);
+		foreach my $attribute (@attributes) {
+			next if($attribute !~ /=/);
+			my @x = smartSplit(qr/\s*=\s*/, $attribute);
+			$methods{$x[0]} = $x[1];
+		}
+		if($readonly == 0) {
+			print logLineForDeclaration("- (void)".$methods{"setter"}."($type)$propertyName");
+		}
+		print logLineForDeclaration("- ($type)".$methods{"getter"});
+	} elsif($line =~ m/^\@interface\s+(.*?)\s*[:(]/ && $interface == 0) {
+		print "%hook $1\n";
+		$interface = 1;
+	} elsif($line =~ m/^\@end/ && $interface == 1) {
+		print "%end\n";
+		$interface = 0;
+	}
+}
+
+sub logLineForDeclaration {
+	my $declaration = shift;
+	$declaration =~ m/^[+-]\s*\((.*?)\).*?/;
+	my $rtype = $1;
+	my $innards = "%log; ";
+	if($rtype ne "void") {
+		$innards .= "$rtype r = %orig; ";
+		$innards .= "HBLogDebug(@\" = ".Logos::Method::formatCharForArgType($rtype)."\", ".Logos::Method::printArgForArgType($rtype, "r")."); " if defined Logos::Method::printArgForArgType($rtype, "r");
+		$innards .= "return r; ";
+	} else {
+		$innards .= "%orig; ";
+	}
+	return "$declaration { $innards}\n";
+}

+ 967 - 0
theos/bin/logos.pl

@@ -0,0 +1,967 @@
+#!/usr/bin/perl
+
+use 5.006;
+use warnings;
+use strict;
+use FindBin;
+use lib "$FindBin::Bin/lib";
+use Digest::MD5 'md5_hex';
+use Module::Load;
+use Module::Load::Conditional 'can_load';
+use Getopt::Long;
+
+package Logos;
+sub sigil { my $id = shift; return "_logos_$id\$"; }
+package main;
+
+use Logos::Util;
+$Logos::Util::errorhandler = \&utilErrorHandler;
+
+use aliased 'Logos::Patch';
+use aliased 'Logos::Patch::Source::Generator' => 'Patch::Source::Generator';
+use aliased 'Logos::Patch';
+use aliased 'Logos::Group';
+use aliased 'Logos::Method';
+use aliased 'Logos::Class';
+use aliased 'Logos::Subclass';
+use aliased 'Logos::StaticClassGroup' ;
+use aliased 'Logos::Property';
+use aliased 'Logos::Function';
+
+use Logos::Generator;
+
+%main::CONFIG = ( generator => "MobileSubstrate",
+		  warnings => "default",
+		);
+$main::warnings = 0;
+
+GetOptions("config|c=s" => \%main::CONFIG);
+
+my $filename = $ARGV[0];
+die "Syntax: $FindBin::Script filename\n" if !$filename;
+open(FILE, $filename) or die "Could not open $filename.\n";
+
+my @lines = ();
+my @patches = ();
+my $preprocessed = 0;
+
+my %lineMapping = ();
+
+{ # If the first line matches "# \d \"...\"", this file has been run through the preprocessor already.
+my $firstline = <FILE>;
+seek(FILE, 0, Fcntl::SEEK_SET);
+if($firstline && $firstline =~ /^# \d+ \"(.*?)\"$/) {
+	$preprocessed = 1;
+	$filename = $1;
+}
+$.--; # Reset line number.
+}
+
+{
+my $readignore = 0;
+my $built = "";
+my $building = 0;
+my $iflevel = -1;
+READLOOP: while(my $line = <FILE>) {
+	chomp($line);
+
+	if($preprocessed && $line =~ /^# (\d+) \"(.*?)\"/) {
+		$lineMapping{$.+1} = [$2, $1];
+	}
+
+	if($readignore) {
+		# Handle #if nesting.
+		if($iflevel > -1 && $line =~ /^#\s*if(n?def)?/) {
+			$iflevel++;
+		} elsif($iflevel > 0 && $line =~ /^#\s*endif/) {
+			$line = $';
+			$iflevel--;
+		}
+
+		# End of a multi-line comment or #if block while ignoring input.
+		if($iflevel == 0 || ($iflevel == -1 && $line =~ /^.*?\*\/\s*/)) {
+			$readignore = 0;
+			$iflevel = -1;
+			$line = $';
+		}
+	}
+	if($readignore) { push(@lines, ""); next; }
+
+	my @quotes = quotes($line);
+
+	# Delete all single-line /* xxx */ comments.
+	while($line =~ /\/\*.*?\*\//g) {
+		next if fallsBetween($-[0], @quotes);
+		$line = $`.$';
+		redo READLOOP;
+	}
+
+	# Start of a multi-line /* comment.
+	while($line =~ /\/\*.*$/g) {
+		next if fallsBetween($-[0], @quotes);
+		$line = $`;
+		push(@lines, $line);
+		$readignore = 1;
+		next READLOOP;
+	}
+
+	# Delete all single-line to-EOL // xxx comments.
+	while($line =~ /\/\//g) {
+		next if fallsBetween($-[0], @quotes);
+		$line = $`;
+		redo READLOOP;
+	}
+
+	# #if 0.
+	while($line =~ /^\s*#\s*if\s+0/) {
+		$iflevel = 1;
+		push(@lines, "");
+		$readignore = 1;
+		next READLOOP;
+	}
+
+	if(!$readignore) {
+		# Line starts with - (return), start gluing lines together until we find a { or ;...
+		if(!$building
+				&& (
+					$line =~ /^\s*(%new.*?)?\s*([+-])\s*\(\s*(.*?)\s*\)/
+					|| $line =~ /%orig[^;]*$/
+					|| $line =~ /%init[^;]*$/
+				)
+				&& index($line, "{") < $-[0] && index($line, ";") < $-[0]) {
+			if(fallsBetween($-[0], @quotes)) {
+				push(@lines, $line);
+				next;
+			}
+			$building = 1;
+			$built = $line;
+			push(@lines, "");
+			next;
+		} elsif($building) {
+			$line =~ s/^\s+//g;
+			$built .= " ".$line;
+			if(index($line,"{") != -1 || index($line,";") != -1) {
+				push(@lines, $built);
+				$building = 0;
+				$built = "";
+				next;
+			}
+			push(@lines, "");
+			next;
+		}
+		push(@lines, $line) if !$readignore;
+	}
+}
+if($building == 1) {
+	push(@lines, $built);
+}
+}
+
+close(FILE);
+
+$lineMapping{0} = ["$filename", 0] if scalar keys %lineMapping == 0;
+
+my $lineno = 0;
+
+my $defaultGroup = Group->new();
+$defaultGroup->name("_ungrouped");
+$defaultGroup->explicit(0);
+my $staticClassGroup = StaticClassGroup->new();
+my @groups = ($defaultGroup);
+
+my $currentGroup = $defaultGroup;
+my $currentClass = undef;
+my $currentMethod = undef;
+my $newMethodTypeEncoding = undef;
+my $currentFunction = undef;
+
+my %classes = ();
+
+my @nestingstack = ();
+
+my @firstDirectivePosition;
+my @lastInitPosition;
+
+my %depthMapping = ("0:0" => 0);
+my $depth = 0;
+
+# Mk. I processing loop - directive processing.
+foreach my $line (@lines) {
+	# We don't want to process in-order, so %group %thing %end won't kill itself automatically
+	# because it found a %end with the %group. This allows things to proceed out-of-order:
+	# we re-start the scan loop with the next % every time we find a match so that the commands don't need to
+	# be in the processed order on every line. That would be pointless.
+
+	my @quotes = quotes($line);
+
+	# Brace Depth Mapping
+	pos($line) = 0;
+	my %depthsForCurrentLine;
+	$depthsForCurrentLine{"$lineno:0"} = $depth;
+	while($line =~ /([{}]|(?<=@)(interface|implementation|protocol|end))/g) {
+		next if fallsBetween($-[0], @quotes);
+
+		my $depthtoken = $lineno.":".($-[0]+1);
+
+		$depth++ if($& eq "{");
+		$depth++ if($& eq "implementation");
+		$depth++ if($& eq "interface");
+		# @protocol, but not "@protocol X;" or "@protocol("
+		$depth++ if($& eq "protocol" && substr($line, $-[0]) !~ /^protocol(\s+([_\$A-Za-z0-9]+(,\s*)?)+;|\s*\()/);
+		$depth-- if($& eq "}");
+		$depth-- if($& eq "end");
+		fileError($lineno, "fell off the face of the planet when we found a \'$&\'") if $depth < 0;
+		$depthMapping{$depthtoken} = $depth;
+		$depthsForCurrentLine{$depthtoken} = $depth;
+	}
+
+	# Directive
+	pos($line) = 0;
+	while($line =~ m/\B(?=(\%\w|&\s*\%\w|[+-]\s*\(\s*.*?\s*\)))/gc) {
+		next if fallsBetween($-[0], @quotes);
+
+		my @directiveDepthTokens = locationOpeningDepthAtPositionInMapping(\%depthsForCurrentLine, $lineno, $-[0]);
+		my $directiveDepth;
+		$directiveDepth = 0 if(!@directiveDepthTokens);
+		$directiveDepth = $depthsForCurrentLine{join(':', @directiveDepthTokens)} if @directiveDepthTokens;
+
+		if($line =~ /\G%hook\s+([\$_\w]+)/gc) {
+			# "%hook <identifier>"
+			fileError($lineno, "%hook does not make sense inside a block") if($directiveDepth >= 1);
+			nestingMustNotContain($lineno, "%hook", \@nestingstack, "hook", "subclass");
+
+			@firstDirectivePosition = ($lineno, $-[0]) if !@firstDirectivePosition;
+
+			nestPush("hook", $lineno, \@nestingstack);
+
+			$currentClass = $currentGroup->addClassNamed($1);
+			$classes{$currentClass->name}++;
+			patchHere(undef);
+		} elsif($line =~ /\G%subclass\s+([\$_\w]+)\s*:\s*([\$_\w]+)\s*(\<\s*(.*?)\s*\>)?/gc) {
+			# %subclass <identifier> : <identifier> \<<protocols ...>\>
+			fileError($lineno, "%subclass does not make sense inside a block") if($directiveDepth >= 1);
+			nestingMustNotContain($lineno, "%subclass", \@nestingstack, "hook", "subclass");
+
+			@firstDirectivePosition = ($lineno, $-[0]) if !@firstDirectivePosition;
+
+			nestPush("subclass", $lineno, \@nestingstack);
+
+			my $classname = $1;
+			my $superclassname = $2;
+			my $superclass = $currentGroup->addClassNamed($superclassname);
+			$superclass->required(1);
+			$currentClass = Subclass->new();
+			$currentClass->name($classname);
+			$currentClass->superclass($superclass);
+			if(defined($3) && defined($4)) {
+				my @protocols = split(/\s*,\s*/, $4);
+				foreach(@protocols) {
+					$currentClass->addProtocol($_);
+				}
+			}
+			$currentGroup->addClass($currentClass);
+
+			$staticClassGroup->addDeclaredOnlyClass($classname);
+			$classes{$superclassname}++;
+			$classes{$classname}++;
+
+			patchHere(undef);
+		} elsif($line =~ /\G%group\s+([\$_\w]+)/gc) {
+			# %group <identifier>
+			fileError($lineno, "%group does not make sense inside a block") if($directiveDepth >= 1);
+			nestingMustNotContain($lineno, "%group", \@nestingstack, "group");
+
+			@firstDirectivePosition = ($lineno, $-[0]) if !@firstDirectivePosition;
+
+			nestPush("group", $lineno, \@nestingstack);
+
+			$currentGroup = getGroup($1);
+			my $patchSource = undef;
+			if(!defined($currentGroup)) {
+				$currentGroup = Group->new();
+				$currentGroup->name($1);
+				push(@groups, $currentGroup);
+				my $capturedGroup = $currentGroup;
+				$patchSource = Patch::Source::Generator->new($capturedGroup, 'declarations');
+			}
+			patchHere($patchSource);
+		} elsif($line =~ /\G%class\s+([+-])?([\$_\w]+)/gc) {
+			# %class [+-]<identifier>
+			@firstDirectivePosition = ($lineno, $-[0]) if !@firstDirectivePosition;
+
+			fileWarning($lineno, "%class is deprecated and will be removed in the future; consider switching to inline %c()");
+
+			my $scope = $1;
+			$scope = "-" if !$scope;
+			my $classname = $2;
+			if($scope eq "+") {
+				$staticClassGroup->addUsedMetaClass($classname);
+			} else {
+				$staticClassGroup->addUsedClass($classname);
+			}
+			$classes{$classname}++;
+			patchHere(undef);
+		} elsif($line =~ /\G%c\(\s*([+-])?([\$_\w]+)\s*\)/gc) {
+			# %c([+-]<identifier>)
+			@firstDirectivePosition = ($lineno, $-[0]) if !@firstDirectivePosition;
+
+			my $scope = $1;
+			$scope = "-" if !$scope;
+			my $classname = $2;
+			if($scope eq "+") {
+				$staticClassGroup->addUsedMetaClass($classname);
+			} else {
+				$staticClassGroup->addUsedClass($classname);
+			}
+			$classes{$classname}++;
+			patchHere(Patch::Source::Generator->new($classname, 'classReferenceWithScope', $scope));
+		} elsif($line =~ /\G%new(\((.*?)\))?(?=\W?)/gc) {
+			# %new[(type)]
+			nestingMustContain($lineno, "%new", \@nestingstack, "hook", "subclass");
+			my $xtype = "";
+			$xtype = $2 if $2;
+			$newMethodTypeEncoding = $xtype;
+			patchHere(undef);
+		} elsif($currentClass && $line =~ /\G([+-])\s*\(\s*(.*?)\s*\)(?=\s*[\$\w:])/gc && $directiveDepth < 1) {
+			# [+-] (<return>)<[X:]>, but only when we're in a %hook.
+
+			# Gasp! We've been moved to a different group!
+			if($currentClass->group != $currentGroup) {
+				my $classname = $currentClass->name;
+				$currentClass = $currentGroup->addClassNamed($classname);
+			}
+
+			my $scope = $1;
+			my $return = $2;
+
+			my $method = Method->new();
+
+			$method->class($currentClass);
+			if($scope eq "+") {
+				$currentClass->hasmetahooks(1);
+			} else {
+				$currentClass->hasinstancehooks(1);
+			}
+
+			$method->scope($scope);
+			$method->return($return);
+
+			if(defined $newMethodTypeEncoding) {
+				$method->setNew(1);
+				$method->type($newMethodTypeEncoding);
+				$newMethodTypeEncoding = undef;
+			}
+
+			my @selparts = ();
+
+			my $patchStart = $-[0];
+
+			# word, then an optional: ": (argtype)argname"
+			while($line =~ /\G\s*([\$\w]*)(\s*:\s*(\((.+?)\))?\s*([\$\w]+?)\b)?/gc) {
+				if(!$1 && !$2) { # Exit the loop if both Keywords and Args are missing: e.g. false positive.
+					pos($line) = $-[0];
+					last;
+				}
+
+				my $keyword = $1; # Add any keyword.
+				push(@selparts, $keyword);
+
+				last if !$2;  # Exit the loop if there are no args (single keyword.)
+				$method->addArgument($3 ? $4 : "id", $5);
+			}
+
+			$method->selectorParts(@selparts);
+			$currentClass->addMethod($method);
+			$currentMethod = $method;
+
+			my $patch = Patch->new();
+			$patch->line($lineno);
+			$patch->range($patchStart, pos($line));
+			$patch->source(Patch::Source::Generator->new($method, 'definition'));
+			addPatch($patch);
+		} elsif($line =~ /\G%orig\b/gc) {
+			# %orig, with optional following parens.
+			if (!defined $currentClass) {
+				fileError($lineno, "%orig does not make sense outside a function") if(!defined($currentFunction));
+				my $patchStart = $-[0];
+
+				my $remaining = substr($line, pos($line));
+				my $orig_args = undef;
+
+				my ($popen, $pclose) = matchedParenthesisSet($remaining);
+				if(defined $popen) {
+					$orig_args = substr($remaining, $popen, $pclose-$popen-1);;
+					pos($line) = pos($line) + $pclose;
+				}
+
+				my $patch = Patch->new();
+				$patch->line($lineno);
+				$patch->range($patchStart, pos($line));
+				$patch->source(Patch::Source::Generator->new($currentFunction, 'originalFunctionCall', $orig_args));
+				addPatch($patch);
+			} else {
+				nestingMustContain($lineno, "%orig", \@nestingstack, "hook", "subclass");
+				fileError($lineno, "%orig does not make sense outside a method") if(!defined($currentMethod));
+				fileError($lineno, "%orig does not make sense outside a block") if($directiveDepth < 1);
+				fileWarning($lineno, "%orig in new method ".prettyPrintMethod($currentMethod)." will be non-operative.") if $currentMethod->isNew;
+
+				my $patchStart = $-[0];
+
+				my $remaining = substr($line, pos($line));
+				my $orig_args = undef;
+
+				my ($popen, $pclose) = matchedParenthesisSet($remaining);
+				if(defined $popen) {
+					$orig_args = substr($remaining, $popen, $pclose-$popen-1);;
+					pos($line) = pos($line) + $pclose;
+				}
+
+				my $capturedMethod = $currentMethod;
+				my $patch = Patch->new();
+				$patch->line($lineno);
+				$patch->range($patchStart, pos($line));
+				$patch->source(Patch::Source::Generator->new($capturedMethod, 'originalCall', $orig_args));
+				addPatch($patch);
+			}
+		} elsif($line =~ /\G&\s*%orig\b/gc) {
+			# &%orig, at a word boundary
+			if (!defined $currentClass) {
+				fileError($lineno, "%orig does not make sense outside a function") if(!defined($currentFunction));
+				my $patchStart = $-[0];
+				my $patchEnd = $patchStart + 6;
+				my $patch = Patch->new();
+				$patch->line($lineno);
+				$patch->range($patchStart, $patchEnd);
+				$patch->source(Patch::Source::Generator->new($currentFunction, 'originalFunctionName'));
+				addPatch($patch);
+			} else {
+				nestingMustContain($lineno, "%orig", \@nestingstack, "hook", "subclass");
+				fileError($lineno, "%orig does not make sense outside a method") if(!defined($currentMethod));
+				fileError($lineno, "%orig does not make sense outside a block") if($directiveDepth < 1);
+				fileError($lineno, "no original method pointer for &%orig in new method ".prettyPrintMethod($currentMethod).".") if $currentMethod->isNew;
+
+				my $capturedMethod = $currentMethod;
+				patchHere(Patch::Source::Generator->new($capturedMethod, 'originalFunctionName'));
+			}
+		} elsif($line =~ /\G%log\b/gc) {
+			# %log
+			nestingMustContain($lineno, "%log", \@nestingstack, "hook", "subclass");
+
+			my $patchStart = $-[0];
+
+			my $remaining = substr($line, pos($line));
+			my $log_args = undef;
+
+			my ($popen, $pclose) = matchedParenthesisSet($remaining);
+			if(defined $popen) {
+				$log_args = substr($remaining, $popen, $pclose-$popen-1);
+				pos($line) = pos($line) + $pclose;
+			}
+
+			my $capturedMethod = $currentMethod;
+			my $patch = Patch->new();
+			$patch->line($lineno);
+			$patch->range($patchStart, pos($line));
+			$patch->source(Patch::Source::Generator->new($capturedMethod, 'buildLogCall', $log_args));
+			addPatch($patch);
+		} elsif($line =~ /\G%ctor\b/gc) {
+			# %ctor
+			fileError($lineno, "%ctor does not make sense inside a block") if($directiveDepth >= 1);
+			nestingMustNotContain($lineno, "%ctor", \@nestingstack, "hook", "subclass");
+			my $replacement = "static __attribute__((constructor)) void _logosLocalCtor_".substr(md5_hex($`.$lineno.$'), 0, 8)."(int argc, char **argv, char **envp)";
+			patchHere($replacement);
+		} elsif($line =~ /\G%dtor\b/gc) {
+			# %dtor
+			fileError($lineno, "%dtor does not make sense inside a block") if($directiveDepth >= 1);
+			nestingMustNotContain($lineno, "%dtor", \@nestingstack, "hook", "subclass");
+			my $replacement = "static __attribute__((destructor)) void _logosLocalDtor_".substr(md5_hex($`.$lineno.$'), 0, 8)."(int argc, char **argv, char **envp)";
+			patchHere($replacement);
+		} elsif($line =~ /\G%init\b/gc) {
+			# %init, with optional following parens
+			fileError($lineno, "%init does not make sense outside a block") if($directiveDepth < 1);
+			my $groupname = "_ungrouped";
+
+			my $patchStart = $-[0];
+
+			my $remaining = substr($line, pos($line));
+			my $argstring = undef;
+			my ($popen, $pclose) = matchedParenthesisSet($remaining);
+			if(defined $popen) {
+				$argstring = substr($remaining, $popen, $pclose-$popen-1);;
+				pos($line) = pos($line) + $pclose;
+			}
+
+			my @args;
+			@args = smartSplit(qr/\s*,\s*/, $argstring) if defined($argstring);
+
+			my $tempgroupname = undef;
+			$tempgroupname = $args[0] if $args[0] && $args[0] !~ /=/;
+			if(defined($tempgroupname)) {
+				$groupname = $tempgroupname;
+				shift(@args);
+			}
+
+			my $group = getGroup($groupname);
+
+			fileError($lineno, "%init for an undefined %group $groupname") if !$group;
+			fileError($lineno, "re-%init of %group ".$group->name.", first initialized at ".lineDescriptionForPhysicalLine($group->initLine)) if $group->initialized;
+
+			foreach my $arg (@args) {
+				if($arg !~ /=/) {
+					fileWarning($lineno, "unknown argument to %init: $arg");
+					next;
+				}
+
+				my @parts = smartSplit(qr/\s*=\s*/, $arg, 2);
+				if(!defined($parts[0]) || !defined($parts[1])) {
+					fileWarning($lineno, "invalid class=expr in %init");
+					next;
+				}
+
+				my $classname = $parts[0];
+				my $expr = $parts[1];
+				my $scope = "-";
+				if($classname =~ /^([+-])/) {
+					$scope = $1;
+					$classname = $';
+				}
+
+				my $class = $group->getClassNamed($classname);
+				if(!defined($class)) {
+					fileWarning($lineno, "tried to set expression for unknown class $classname in group $groupname");
+					next;
+				}
+
+				$class->expression($expr) if $scope eq "-";
+				$class->metaexpression($expr) if $scope eq "+";
+			}
+
+			$group->initLine($lineno);
+			$group->initialized(1);
+
+			while($line =~ /\G\s*;/gc) { };
+			my $patchEnd = pos($line);
+
+			my $patch = Patch->new();
+			$patch->line($lineno);
+			$patch->range($patchStart, pos($line));
+			$patch->source(Patch::Source::Generator->new($group, 'initializers'));
+			addPatch($patch);
+
+			@lastInitPosition = ($lineno, pos($line));
+		} elsif($line =~ /\G%end\b/gc) {
+			# %end
+			fileError($lineno, "%end does not make sense inside a block") if($directiveDepth >= 1);
+			my $closing = nestPop(\@nestingstack);
+			fileError($lineno, "dangling %end") if !$closing;
+			if($closing eq "group") {
+				$currentGroup = getGroup("_ungrouped");
+			}
+			if($closing eq "hook" || $closing eq "subclass") {
+				$currentClass = undef;
+			}
+			patchHere(undef);
+		} elsif($line =~ /\G%config\s*\(\s*(\w+)\s*=\s*(.*?)\s*\)/gc) {
+			$main::CONFIG{$1} = $2;
+			patchHere(undef);
+		} elsif($line =~ /\G%property\s*(?:\((\s*\w+\s*(?:,\s*(?:\w|\=|:)+\s*)*)\))?\s*((?:\w+\s+\**)+)(\w+)\s*;/gc){
+			nestingMustContain($lineno, "%property", \@nestingstack, "hook", "subclass");
+
+			# check property attribute validity
+			my @attributes = split/\(?\s*,\s*\)?/, $1;
+			my ($assign, $retain, $copy, $nonatomic, $getter, $setter);
+			my $numattr = 0;
+
+			foreach(@attributes){
+				$numattr++;
+
+				if($_ =~ /assign/){
+					$assign = 1;
+				}elsif($_ =~ /retain/){
+					$retain = 1;
+				}elsif($_ =~ /copy/){
+					$copy = 1;
+				}elsif($_ =~ /nonatomic/){
+					$nonatomic = 1;
+				}elsif($_ =~ /getter=(\w+)/){
+					$getter = $1;
+				}elsif($_ =~ /setter=(\w+:)/){
+					$setter = $1;
+				}elsif($_ =~ /readwrite|readonly/){
+					fileError($lineno, "property attribute '".$_."' not supported.");
+				}else{
+					fileError($lineno, "unknown property attribute '".$_."'.");
+				}
+			}
+
+			if(!$assign && !$retain && !$copy){
+				fileWarning($lineno, "no 'assign', 'retain', or 'copy' attribute is specified - 'assign' is assumed");
+				push(@attributes, "assign");
+				$numattr++;
+			}
+
+			if($assign && $retain){
+				fileError($lineno, "property attributes 'assign' and 'retain' are mutually exclusive.");
+			}
+
+			if($assign && $copy){
+				fileError($lineno, "property attributes 'assign' and 'copy' are mutually exclusive.");
+			}
+
+			if($copy && $retain){
+				fileError($lineno, "property attributes 'copy' and 'retain' are mutually exclusive.");
+			}
+
+			my $property = Property->new();
+
+
+			$property->class($currentClass->name);
+
+			if($currentGroup){
+				$property->group($currentGroup->name);
+			}else{
+				$property->group("_ungrouped");
+			}
+
+			$property->numattr($numattr);
+			$property->attributes(@attributes);
+			$property->type($2);
+			$property->name($3);
+
+			$currentClass->addProperty($property);
+
+			my $patchStart = $-[0];
+			my $patch = Patch->new();
+			$patch->line($lineno);
+			$patch->range($patchStart, pos($line));
+			$patch->source(Patch::Source::Generator->new($property, 'getters_setters'));
+			addPatch($patch);
+		} elsif($line =~ /\G%hookf\b/gc) {
+			#%hookf
+			fileError($lineno, "%hookf does not make sense inside a block") if($directiveDepth >= 1);
+			nestingMustNotContain($lineno, "%hookf", \@nestingstack, "hook", "subclass");
+
+			@firstDirectivePosition = ($lineno, $-[0]) if !@firstDirectivePosition;
+
+			my $patchStart = $-[0];
+
+			my $remaining = substr($line, pos($line));
+			my $argumentString = undef;
+			my $args = [];
+
+			my ($popen, $pclose) = matchedParenthesisSet($remaining);
+			if(defined $popen) {
+				$argumentString = substr($remaining, $popen, $pclose-$popen-1);
+				pos($line) = pos($line) + $pclose;
+				@$args = Logos::Util::smartSplit(qr/\s*,\s*/, $argumentString);
+			}
+
+			$currentFunction = $currentGroup->addFunction($args);
+
+			my $patch = Patch->new();
+			$patch->line($lineno);
+			$patch->range($patchStart, pos($line));
+			$patch->source(Patch::Source::Generator->new($currentFunction, 'declaration'));
+			addPatch($patch);
+		}
+	}
+
+	$lineno++;
+}
+
+while(scalar(@nestingstack) > 0) {
+	my $closing = pop(@nestingstack);
+	my @parts = split(/:/, $closing);
+	fileWarning($lineno, "missing %end (%".$parts[0]." opened at ".lineDescriptionForPhysicalLine($parts[1])." extends to EOF)");
+}
+
+Logos::Generator::use($main::CONFIG{"generator"});
+
+my $hasGeneratorPreamble = $preprocessed; # If we're already preprocessed, we cannot insert #include statements.
+$hasGeneratorPreamble = Logos::Generator::for->findPreamble(\@lines) if !$hasGeneratorPreamble;
+
+if(@firstDirectivePosition) {
+	# Loop until we find a blank line at depth 0 to splice our preamble in.
+	# The top of the file (or, alternatively, the first line of our file post-
+	# preprocessing) will be considered to be a blank line.
+	#
+	# This breaks if one includes a blank line between "int blah()" and its
+	# corresponding "{", however. Nobody codes like that anyway.
+	# This will probably also break if you keep your "{" and "}" inside header files
+	# that you #include into your code. Nobody codes like that, either.
+
+	# Optimization: Only do this once.
+	my @depthKeys = sort {
+		my @ba=split(/:/,$b);
+		my @aa=split(/:/,$a);
+		($ba[0] == $aa[0]
+		? $ba[1] <=> $aa[1]
+			: $ba[0] <=> $aa[0])
+	} keys %depthMapping;
+	my $line = $firstDirectivePosition[0];
+	my $pos = $firstDirectivePosition[1];
+	while(1) {
+		my $depth = lookupDepthMapping($line, $pos, \@depthKeys);
+		my $above;
+		$above = "" if $line eq 0;
+		if($preprocessed) {
+			my @lm = lookupLineMapping($line);
+			$above = "" if($lm[0] eq $filename && $lm[1] == 1);
+		}
+		$above = $lines[$line-1] if !defined $above;
+
+		last if $depth == 0 && $above =~ /^\s*$/;
+
+		$line-- if($pos == 0);
+		$pos = 0;
+	}
+	my $patch = Patch->new();
+	$patch->line($line);
+	my @patchsource = ();
+	push(@patchsource, Patch::Source::Generator->new(undef, 'preamble', $hasGeneratorPreamble));
+	push(@patchsource, Patch::Source::Generator->new(undef, 'generateClassList', keys %classes));
+	push(@patchsource, Patch::Source::Generator->new($groups[0], 'declarations'));
+	push(@patchsource, Patch::Source::Generator->new($staticClassGroup, 'declarations'));
+	$patch->source(\@patchsource);
+	addPatch($patch);
+
+	if(!@lastInitPosition) {
+		# If we haven't seen any %init directives, generate the default constructor.
+		my $patch = Patch->new();
+		$patch->line(scalar @lines);
+		$patch->squash(1);
+		$patch->source(defaultConstructorSource());
+		addPatch($patch);
+	}
+
+}
+
+my @unInitGroups = map {$_->name;} (grep {!$_->initialized && $_->initRequired;} @groups);
+fileError($lineno, "non-initialized hook group".(scalar @unInitGroups == 1 ? "" : "s").": ".join(", ", @unInitGroups)) if scalar @unInitGroups > 0;
+
+my @sortedPatches = sort { ($b->line == $a->line ? ($b->start || -1) <=> ($a->start || -1) : $b->line <=> $a->line) } @patches;
+
+if(exists $main::CONFIG{"dump"}) {
+	my $dumphref = {
+			linemap=>\%lineMapping,
+			depthmap=>\%depthMapping,
+			groups=>\@groups,
+			patches=>\@patches,
+			lines=>\@lines,
+			config=>\%::CONFIG
+		};
+	if($main::CONFIG{"dump"} eq "yaml") {
+		load 'YAML::Syck';
+		print STDERR YAML::Syck::Dump($dumphref);
+	}
+	#print STDERR Data::Dumper->Dump([\@groups, \@patches, \@lines, \%::CONFIG], [qw(groups patches lines config)]);
+}
+
+if($main::warnings > 0 && exists $main::CONFIG{"warnings"} && $main::CONFIG{"warnings"} eq "error") {
+	exit(1);
+}
+
+for(@sortedPatches) {
+	$_->apply(\@lines);
+}
+
+splice(@lines, 0, 0, generateLineDirectiveForPhysicalLine(0)) if !$preprocessed;
+foreach my $oline (@lines) {
+	print $oline."\n" if defined($oline);
+}
+
+sub defaultConstructorSource {
+	my @return;
+	my @initRequiredGroups = grep {$_->initRequired;} @groups;
+	my @explicitGroups = grep {$_->explicit;} @initRequiredGroups;
+	fileError($lineno, "Cannot generate an autoconstructor with multiple %groups. Please explicitly create a constructor.") if scalar @explicitGroups > 0;
+	if(scalar @initRequiredGroups > 0) {
+		push(@return, "static __attribute__((constructor)) void _logosLocalInit() {\n");
+		foreach(@initRequiredGroups) {
+			fileError($lineno, "re-%init of %group ".$_->name.", first initialized at ".lineDescriptionForPhysicalLine($_->initLine)) if $_->initialized;
+			push(@return, Patch::Source::Generator->new($_, 'initializers'));
+			push(@return, " ");
+			$_->initLine($lineno);
+			$_->initialized(1);
+		}
+		push(@return, "}\n");
+	}
+	return @return > 0 ? \@return : undef;
+}
+
+sub fileWarning {
+	my $curline = shift;
+	my $reason = shift;
+	my @lineMap = lookupLineMapping($curline);
+	my $filename = $lineMap[0];
+	my $print = 1;
+	if(exists($main::CONFIG{"warnings"})) {
+		if($main::CONFIG{"warnings"} eq "error") {
+			if($main::warnings == 0) {
+				print STDERR "logos: warnings being treated as errors\n";
+			}
+		} elsif($main::CONFIG{"warnings"} eq "none") {
+			$print = 0;
+		}
+	}
+	print STDERR "$filename:".($curline > -1 ? $lineMap[1].":" : "")." warning: $reason\n" if($print == 1);
+	$main::warnings++;
+}
+
+sub fileError {
+	my $curline = shift;
+	my $reason = shift;
+	my @lineMap = lookupLineMapping($curline);
+	my $filename = $lineMap[0];
+	die "$filename:".($curline > -1 ? $lineMap[1].":" : "")." error: $reason\n";
+}
+
+sub nestingError {
+	my $curline = shift;
+	my $thisblock = shift;
+	my $reason = shift;
+	my @parts = split(/:/, $reason);
+	fileError $curline, "$thisblock inside a %".$parts[0].", opened at ".lineDescriptionForPhysicalLine($parts[1]);
+}
+
+sub nestingMustContain {
+	my $lineno = shift;
+	my $trying = shift;
+	my $stackref = shift;
+	return if nestingContains($stackref, @_);
+	fileError($lineno, "$trying found outside of ".join(" or ", @_));
+}
+
+sub nestingMustNotContain {
+	my $lineno = shift;
+	my $trying = shift;
+	my $stackref = shift;
+	nestingError($lineno, $trying, $_) if nestingContains($stackref, @_);
+}
+
+sub nestingContains {
+	my $stackref = shift;
+	my @stack = @$stackref;
+	my @search = @_;
+	my @parts = ();
+	foreach my $nest (@stack) {
+		@parts = split(/:/, $nest);
+		foreach my $find (@search) {
+			if($find eq $parts[0]) {
+				$_ = $nest;
+				return $_;
+			}
+		}
+	}
+	$_ = undef;
+	return undef;
+}
+
+sub nestPush {
+	my $type = shift;
+	my $line = shift;
+	my $ref_stack = shift;
+	push(@{$ref_stack}, $type.":".$line);
+}
+
+sub nestPop {
+	my $ref_stack = shift;
+	my $outgoing = pop(@{$ref_stack});
+	return undef if !$outgoing;
+	my @parts = split(/:/, $outgoing);
+	return $parts[0];
+}
+
+sub getGroup {
+	my $name = shift;
+	foreach(@groups) {
+		return $_ if $_->name eq $name;
+	}
+	return undef;
+}
+
+sub lookupLineMapping {
+	my $fileline = shift;
+	$fileline++;
+	for (sort {$b <=> $a} keys %lineMapping) {
+		if($fileline >= $_) {
+			my @x = @{$lineMapping{$_}};
+			return ($x[0], $x[1] + ($fileline-$_));
+		}
+	}
+	return undef;
+}
+
+sub generateLineDirectiveForPhysicalLine {
+	my $physline = shift;
+	my @lineMap = lookupLineMapping($physline);
+	my $filename = $lineMap[0];
+	my $lineno = $lineMap[1];
+	return ($preprocessed ? "# " : "#line ").$lineno." \"$filename\"";
+}
+
+sub lineDescriptionForPhysicalLine {
+	my $physline = shift;
+	my @lineMap = lookupLineMapping($physline);
+	my $filename = $lineMap[0];
+	my $lineno = $lineMap[1];
+	return "$filename:$lineno";
+}
+
+sub locationOpeningDepthAtPositionInMapping {
+	my $dref = shift;
+	my $fileline = shift;
+	my $pos = shift;
+	my $kref = shift;
+	my @keys;
+	if($kref) {
+		@keys = @$kref;
+	} else {
+		@keys = sort {
+			my @ba=split(/:/,$b);
+			my @aa=split(/:/,$a);
+			($ba[0] == $aa[0]
+			? $ba[1] <=> $aa[1]
+				: $ba[0] <=> $aa[0])
+		} keys %$dref;
+	}
+	for (@keys) {
+		my @depthTokens = split(/:/, $_);
+		if($fileline > $depthTokens[0] || ($fileline == $depthTokens[0] && $pos >= $depthTokens[1])) {
+			return @depthTokens;
+		}
+	}
+	return undef;
+}
+
+sub lookupDepthMapping {
+	my $fileline = shift;
+	my $pos = shift;
+	my $kref = shift;
+	my @depthTokens = locationOpeningDepthAtPositionInMapping(\%depthMapping, $fileline, $pos, $kref);
+	return 0 if(!@depthTokens);
+	return $depthMapping{join(':', @depthTokens)};
+}
+
+sub patchHere {
+	my $source = shift;
+	my $patch = Patch->new();
+	$patch->line($lineno);
+	$patch->range($-[0], $+[0]);
+	$patch->source($source);
+	push @patches, $patch;
+}
+
+sub addPatch {
+	my $patch = shift;
+	push @patches, $patch;
+}
+
+sub prettyPrintMethod {
+	my $method = shift;
+	return $method->scope."[".$method->class->name." ".$method->selector."]";
+}
+
+sub utilErrorHandler {
+	fileError($lineno, shift);
+}

+ 324 - 0
theos/bin/nic.pl

@@ -0,0 +1,324 @@
+#!/usr/bin/perl
+
+my $VER = "2.0";
+
+use strict;
+use warnings;
+use FindBin;
+use lib "$FindBin::Bin/lib";
+
+use Getopt::Long;
+use Cwd qw(abs_path getcwd);
+use File::Spec;
+use File::Find;
+use File::Copy;
+use User::pwent;
+use POSIX qw(getuid);
+use Module::Load::Conditional 'can_load';
+use Tie::File;
+
+use NIC::Bridge::Context (PROMPT => \&nicPrompt);
+use NIC::Formats::NICTar;
+use NIC::NICType;
+
+our $savedStdout = *STDOUT;
+
+my @_dirs = File::Spec->splitdir(abs_path($FindBin::Bin));
+$_dirs[$#_dirs]="templates";
+our $_templatepath = File::Spec->catdir(@_dirs);
+$#_dirs--;
+my $_theospath = File::Spec->catdir(@_dirs);
+
+{
+	my $_abstheospath = abs_path($_theospath);
+	my $_cwd = abs_path(Cwd::getcwd());
+	$_abstheospath .= '/' if $_abstheospath !~ /\/$/;
+	$_cwd .= '/' if $_cwd !~ /\/$/;
+	exitWithError("Cowardly refusing to make a project inside \$THEOS ($_abstheospath)") if($_cwd =~ /^$_abstheospath/);
+}
+
+my %CONFIG = (link_theos => 0);
+loadConfig();
+
+my $clean_project_name = "";
+my $project_name = "";
+my $package_prefix = $CONFIG{'package_prefix'};
+$package_prefix = "com.yourcompany" if !$package_prefix;
+my $package_name = "";
+my $username = $CONFIG{'username'};
+$username = "" if !$username;
+
+my $template = undef;
+my $nicfile = undef;
+
+Getopt::Long::Configure("bundling");
+
+GetOptions(	"packagename|p=s" => \$package_name,
+		"name|n=s" => \$project_name,
+		"user|u=s" => \$username,
+		"nic=s" => \$nicfile,
+		"template|t=s" => \$template);
+
+$project_name = $ARGV[0] if($ARGV[0]);
+
+my $_versionstring = "NIC $VER - New Instance Creator";
+print $_versionstring,$/;
+print "-" x length($_versionstring),$/;
+
+my $NIC;
+if($nicfile) {
+	$NIC = _loadNIC($nicfile) if $nicfile && -f $nicfile;
+} else {
+	my @templates = getTemplates();
+	if(scalar @templates == 0) {
+		exitWithError("No file specified with --nic and no templates found.");
+	}
+
+	if($template) {
+		my @matched = grep { $_->name eq $template } @templates;
+		$NIC = $matched[0] if(scalar @matched > 0);
+	}
+	if(!$NIC) {
+		$NIC = promptList(undef, "Choose a Template (required)", sub { local $_ = shift; return $_->name; }, @templates);
+	}
+}
+
+exitWithError("No NIC file loaded.") if !$NIC;
+
+promptIfMissing(\$project_name, undef, "Project Name (required)");
+exitWithError("I can't live without a project name! Aieeee!") if !$project_name;
+$clean_project_name = cleanProjectName($project_name);
+
+$package_name = $package_prefix.".".packageNameIze($project_name) if $CONFIG{'skip_package_name'};
+promptIfMissing(\$package_name, $package_prefix.".".packageNameIze($project_name), "Package Name") unless $NIC->variableIgnored("PACKAGENAME");
+
+promptIfMissing(\$username, getUserName(), "Author/Maintainer Name") unless $NIC->variableIgnored("USER");
+
+my $directory = lc($clean_project_name);
+if(-d $directory) {
+	my $response;
+	promptIfMissing(\$response, "N", "There's already something in $directory. Continue");
+	exit 1 if(uc($response) eq "N");
+}
+
+$NIC->variable("FULLPROJECTNAME") = $project_name;
+$NIC->variable("PROJECTNAME") = $clean_project_name;
+$NIC->variable("PACKAGENAME") = $package_name;
+$NIC->variable("USER") = $username;
+
+if(! -e "control" && ! -e "layout/DEBIAN/control") {
+	$NIC->addConstraint("package");
+}
+
+foreach my $prompt ($NIC->prompts) {
+	nicPrompt($NIC, $prompt->{name}, $prompt->{prompt}, $prompt->{default});
+}
+
+my $cwd = abs_path(getcwd());
+
+# Add theos symlink to the template, if necessary
+if($CONFIG{'link_theos'} != 0 && !$NIC->variableIgnored("THEOS")) {
+	$NIC->addConstraint("link_theos");
+
+	my $template_theos_reference = $NIC->_getContentWithoutCreate("theos");
+	if(!$template_theos_reference || $template_theos_reference->type == NIC::NICType::TYPE_UNKNOWN) {
+		print STDERR "[warning] Asked to link theos, but template lacks an optional theos link. Creating one! Contact the author of this template about this issue.",$/;
+		$NIC->registerSymlink("theos", '@@THEOS_PATH@@');
+	}
+
+	my $theosLinkPath = $CONFIG{'theos_path'};
+	$theosLinkPath = readlink("$cwd/theos") if !$theosLinkPath && (-l "$cwd/theos") && !$CONFIG{'ignore_parent_theos'};
+	$theosLinkPath = "$cwd/theos" if !$theosLinkPath && (-d "$cwd/theos") && !$CONFIG{'ignore_parent_theos'};
+	$theosLinkPath = $_theospath if !$theosLinkPath;
+
+	# Set @@THEOS@@ to 'theos', so that the project refers to its linked copy of theos.
+	$NIC->variable("THEOS") = "theos";
+	$NIC->variable("THEOS_PATH") = $theosLinkPath;
+} else {
+	# Trust that the user knows what he's doing and set @@THEOS@@ to $(THEOS). (or whatever the user prefers)
+	my $theosLocalName = '$(THEOS)';
+	$theosLocalName = $CONFIG{"theos_local_name"} if $CONFIG{"theos_local_name"};
+	$NIC->variable("THEOS") = $theosLocalName;
+}
+
+# Execute control script.
+$NIC->exec or exitWithError("Failed to build template '".$NIC->name."'.");
+
+print "Instantiating ".$NIC->name." in ".lc($clean_project_name)."/...",$/;
+my $dirname = lc($clean_project_name);
+$NIC->build($dirname);
+chdir($cwd);
+
+my @makefiles = ("GNUmakefile", "makefile", "Makefile");
+my $makefile;
+map { $makefile = $_ if -e $_; } @makefiles;
+if($makefile) {
+	tie(my @lines, 'Tie::File', $makefile);
+	my $hasCommon = 0;
+	map {$hasCommon++ if /common\.mk/;} @lines;
+	if($hasCommon > 0) {
+		my $alreadyHas = 0;
+		map {$alreadyHas++ if /^\s*SUBPROJECTS.*$dirname/;} @lines;
+		if($alreadyHas == 0) {
+			print "Adding '$project_name' as an aggregate subproject in Theos makefile '$makefile'.",$/;
+			my $newline = "SUBPROJECTS += $dirname";
+			my $i = 0;
+			my $aggLine = -1;
+			map {$aggLine = $i if /aggregate\.mk/; $i++;} @lines;
+			if($aggLine == -1) {
+				push(@lines, $newline);
+				push(@lines, "include \$(THEOS_MAKE_PATH)/aggregate.mk");
+			} else {
+				splice(@lines, $aggLine, 0, $newline);
+			}
+		}
+	}
+	untie(@lines);
+}
+print "Done.",$/;
+
+sub promptIfMissing {
+	my $vref = shift;
+	return if(${$vref});
+
+	my $default = shift;
+	my $prompt = shift;
+
+	if($default) {
+		print $::savedStdout $prompt, " [$default]: ";
+	} else {
+		print $::savedStdout $prompt, ": ";
+	}
+
+	$| = 1; $_ = <STDIN>;
+	chomp;
+
+	if($default) {
+		${$vref} = $_ ? $_ : $default;
+	} else {
+		${$vref} = $_;
+	}
+}
+
+sub promptList {
+	my $default = shift;
+	my $prompt = shift;
+	my $formatsub = shift // sub { shift; };
+	my @list = @_;
+
+	$default = -1 if(!defined $default);
+
+	for(0..$#list) { print " ".($_==$default?">":" ")."[".($_+1).".] ",$formatsub->($list[$_]),$/; }
+	print $prompt,": ";
+	$| = 1;
+	my $idx = -1;
+	while(<STDIN>) {
+		chomp;
+		if($default > -1 && $_ eq "") {
+			$idx = $default;
+			last;
+		}
+		if($_ < 1 || $_ > $#list+1) {
+			print "Invalid value.",$/,$prompt,": ";
+			next;	
+		}
+		$idx = $_-1;
+		last;
+	}
+	return $list[$idx];
+}
+
+sub exitWithError {
+	my $error = shift;
+	print STDERR "[error] ", $error, $/;
+	exit 1;
+}
+
+sub _loadNIC {
+	my $nicfile = shift;
+	open(my $nichandle, "<", $nicfile);
+	my $line = <$nichandle>;
+	seek($nichandle, 0, 0);
+
+	(my $prettyname = $nicfile) =~ s/$::_templatepath\/(.*)\.nic(\.tar)?/$1/g;
+	$prettyname .= " (unnamed template)";
+
+	my $nicversion = 1;
+	my $NIC = undef;
+	if($line =~ /^nic (\w+)$/) {
+		$nicversion = $1;
+		my $NICPackage = "NIC$nicversion";
+		return undef if(!can_load(modules => {"NIC::Formats::$NICPackage" => undef}));
+		$NIC = "NIC::Formats::$NICPackage"->new($nichandle, $prettyname);
+	} else {
+		$NIC = NIC::Formats::NICTar->new($nichandle, $prettyname);
+	}
+
+	close($nichandle);
+	return $NIC;
+}
+
+sub getTemplates {
+	our @templates = ();
+	find({wanted => \&templateWanted, no_chdir => 1}, $_templatepath);
+	sub templateWanted {
+		if(-f && (/\.nic$/ || /\.nic\.tar$/)) {
+			my $nic = _loadNIC($_);
+			push(@templates, $nic) if $nic;
+		}
+	}
+	return sort { $a->name cmp $b->name } @templates;
+}
+
+sub packageNameIze {
+	my $name = shift;
+	$name =~ s/ //g;
+	$name =~ s/[^\w\+-.]//g;
+	return lc($name);
+}
+
+sub cleanProjectName {
+	my $name = shift;
+	$name =~ s/ //g;
+	$name =~ s/\W//g;
+	return $name;
+}
+
+sub getUserName {
+	my $pw = getpw(getuid());
+	my ($fullname) = split(/\s*,\s*/, $pw->gecos);
+	return $fullname ? $fullname : $pw->name;
+}
+
+sub getHomeDir {
+	my $pw = getpw(getuid());
+	return $pw->dir;
+}
+
+sub loadConfig {
+	open(my $cfh, "<", getHomeDir()."/.nicrc") or return;
+	while(<$cfh>) {
+		if(/^(.+?)\s*=\s*\"(.*)\"$/) {
+			my $key = $1;
+			my $value = $2;
+			$CONFIG{$key} = $value;
+		}
+	}
+}
+
+sub nicPrompt {
+	# Do we want to import these variables into the NIC automatically? In the format name.VARIABLE?
+	# If so, this could become awesome. We could $NIC->get($prompt->{name})
+	# and have loaded the variables in a loop beforehand.
+	# This would also allow the user to set certain variables (package prefix, username) for different templates.
+	my ($nic, $variable, $prompt, $default) = @_;
+	my $response = undef;
+	$response = $CONFIG{$nic->name().".".$variable} if($variable);
+
+	promptIfMissing(\$response, $default, "[".$nic->name."] ".$prompt);
+
+	$NIC->variable($variable) = $response if $variable;
+
+	# Return the response for anybody who's interested.
+	$response;
+}

+ 147 - 0
theos/bin/nicify.pl

@@ -0,0 +1,147 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use File::Find;
+use File::Spec;
+use Archive::Tar;
+use Cwd qw(abs_path getcwd);
+
+use FindBin;
+use lib "$FindBin::Bin/lib";
+use NIC::Formats::NICTar;
+
+package NIC::Archive::Tar::File;
+use parent "Archive::Tar::File";
+sub new {
+	my $class = shift;
+	my $self = Archive::Tar::File->new(@_);
+	bless($self, $class);
+	return $self;
+}
+
+sub _prefix_and_file {
+	my $self = shift;
+	my $path = shift;
+	my ($prefix, $file) = $self->SUPER::_prefix_and_file($path);
+	$prefix =~ s/^/\.\// if $prefix ne "" && $prefix ne "." && $prefix !~ /^\.\//;
+	return ($prefix, $file);
+}
+1;
+package main;
+
+if(@ARGV == 0) {
+	exitWithError("Syntax: $FindBin::Script <directory>");
+}
+
+my $cwd = abs_path(getcwd());
+my $tar = Archive::Tar->new();
+my $controlfile = undef;
+our @tarfiles = (
+	NIC::Archive::Tar::File->new(data=>"./", "", {type=>Archive::Tar::Constant::DIR, uid=>0, gid=>0, mode=>0755}),
+	NIC::Archive::Tar::File->new(data=>"./NIC/", "", {type=>Archive::Tar::Constant::DIR, uid=>0, gid=>0, mode=>0777})
+);
+
+chdir $ARGV[0];
+
+my $control_in = undef;
+
+if(-f "pre.NIC") {
+	warning("Using legacy pre.NIC as ./NIC/control.");
+	$control_in = "./pre.NIC";
+} elsif(-f "NIC/control") {
+	$control_in = "./NIC/control";
+}
+
+if(!$control_in) {
+	exitWithError("No control file found at NIC/control.");
+	exit 1;
+}
+
+$controlfile = NIC::Archive::Tar::File->new(file=>$control_in);
+$controlfile->prefix("./NIC");
+$controlfile->name("control");
+push(@tarfiles, $controlfile);
+
+find({wanted => \&wanted, preprocess => \&preprocess, follow => 0, no_chdir => 1}, ".");
+
+$tar->add_files(@tarfiles);
+
+chdir($cwd);
+my $newnic = NIC::Formats::NICTar->new($tar);
+if(!defined $newnic->name) {
+	exitWithError("Template has no name. Please insert a `name \"<name>\"` directive into $control_in.");
+}
+
+{ my $_ = scalar @{$newnic->{CONTENTS}}; info("$_ entr".($_==1?"y.":"ies.")); }
+{ my $_ = scalar @{$newnic->{PROMPTS}}; info("$_ prompt".($_==1?".":"s.")); }
+my $constraints = 0;
+{
+	my %constrainthash;
+	for(@{$newnic->{CONTENTS}}) {
+		for my $c ($_->constraints) {
+			$constrainthash{$c}++;
+		}
+	}
+	$constraints = scalar keys %constrainthash;
+}
+{ my $_ = $constraints; info("$_ constraint".($_==1?".":"s.")); }
+
+my $fixedfn = join("_", File::Spec->splitdir($newnic->name));
+my $filename = $fixedfn.".nic.tar";
+$tar->write($filename) and info("Archived template \"".$newnic->name."\" to $filename.");
+
+sub preprocess {
+	my @list = @_;
+	if($File::Find::dir eq "./NIC") {
+		@list = grep !/^control$/, @list;
+	}
+	@list = grep !/^pre.NIC$/ && !/^\.svn$/ && !/^\.git$/ && !/^_MTN$/ && !/\.nic\.tar$/ && !/^\.DS_Store$/ && !/^\._/, @list;
+	return @list;
+}
+
+sub wanted {
+	local $_ = $File::Find::name;
+	my $mode = (stat)[2];
+
+	my $tarfile = undef;
+	if(-d) {
+		s/$/\// if !/\/$/;
+		return if /^\.\/$/;
+		return if /^\.\/NIC\/?$/;
+		$tarfile = NIC::Archive::Tar::File->new(data=>$_, "", {mode=>$mode, uid=>0, gid=>0, type=>Archive::Tar::Constant::DIR});
+	} elsif(-f && ! -l) {
+		$tarfile = NIC::Archive::Tar::File->new(file=>$_);
+		$tarfile->mode($mode);
+		$tarfile->uid(0);
+		$tarfile->gid(0);
+	} elsif(-l) {
+		$tarfile = NIC::Archive::Tar::File->new(data=>$_, "", {linkname=>readlink($_), uid=>0, gid=>0, type=>Archive::Tar::Constant::SYMLINK});
+	}
+	push(@tarfiles, $tarfile) if $tarfile;
+}
+
+sub slurp {
+	my $fn = shift;
+	open(my($fh), "<", $fn);
+	local $/ = undef;
+	my $d = <$fh>;
+	return $d;
+}
+
+sub info {
+	my $text = shift;
+	print STDERR "[info] ", $text, $/;
+}
+
+sub warning {
+	my $text = shift;
+	print STDERR "[warning] ", $text, $/;
+}
+
+sub exitWithError {
+	my $error = shift;
+	print STDERR "[error] ", $error, $/;
+	exit 1;
+}
+

+ 44 - 0
theos/bin/package_version.sh

@@ -0,0 +1,44 @@
+#!/bin/bash
+
+function build_num_from_file {
+	version=$(< "$1")
+	version=${version##*-}
+	version=${version%%+*}
+	version=${version%%~*}
+	echo -n "$version"
+}
+
+while getopts ":N:V:" flag; do
+	case "$flag" in
+		:)	echo "$0: Option -$OPTARG requires an argument." 1>&2
+			exit 1
+			;;
+		\?)	echo "$0: What're you talking about?" 1>&2
+			exit 1
+			;;
+		N)	package="$OPTARG" ;;
+		V)	version="$OPTARG" ;;
+	esac
+done
+
+if [[ ! -d "${THEOS_PROJECT_DIR}/.theos/packages" ]]; then
+	if [[ -d "${THEOS_PROJECT_DIR}/.debmake" ]]; then
+		mkdir -p "${THEOS_PROJECT_DIR}/.theos"
+		mv "${THEOS_PROJECT_DIR}/.debmake" "${THEOS_PROJECT_DIR}/.theos/packages"
+	else
+		mkdir -p "${THEOS_PROJECT_DIR}/.theos/packages"
+	fi
+fi
+
+versionfile="${THEOS_PROJECT_DIR}/.theos/packages/$package-$version"
+build_number=0
+
+if [[ ! -e "$versionfile" ]]; then
+	build_number=1
+else
+	build_number=$(build_num_from_file "$versionfile")
+	let build_number++
+fi
+
+echo -n "$build_number" > "$versionfile"
+echo "$build_number"

+ 26 - 0
theos/bin/target.pl

@@ -0,0 +1,26 @@
+#!/usr/bin/perl
+use File::Temp;
+
+my @o;
+for(reverse @ARGV) {
+	my $i = 0;
+	for my $a (split /:/) {
+		if(length $a > 0) {
+			@o = () if($i == 0 && $o[$i] && $o[$i] ne $a);
+			$o[$i] = $a;
+		}
+		$i++;
+	}
+}
+#print join(':', map { $_ eq "" ? "-" : $_ } @o);
+my $i = 0;
+my ($fh, $tempfile) = File::Temp::tempfile();
+binmode($fh, ":utf8");
+
+for(@o) {
+	print $fh "export __THEOS_TARGET_ARG_$i := $_\n";
+	++$i;
+}
+
+close($fh);
+print $tempfile,$/;

+ 23 - 0
theos/bin/vercmp.pl

@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+use warnings;
+use strict;
+if(@ARGV < 3) {
+	exit 1;
+}
+my @afirst = split(/\./, $ARGV[0]);
+my $op = $ARGV[1];
+my @asecond = split(/\./, $ARGV[2]);
+
+push(@afirst, 0) while(@afirst < @asecond);
+push(@asecond, 0) while(@asecond < @afirst);
+
+my $v1 = 0;
+my $v2 = 0;
+map { ($v1 *= 100) += $_ } (@afirst);
+map { ($v2 *= 100) += $_ } (@asecond);
+
+print "1" if $v1 > $v2 && $op eq "gt";
+print "1" if $v1 < $v2 && $op eq "lt";
+print "1" if $v1 >= $v2 && $op eq "ge";
+print "1" if $v1 <= $v2 && $op eq "le";
+print "1" if $v1 == $v2 && $op eq "eq";

+ 7 - 0
theos/documentation/Makefile

@@ -0,0 +1,7 @@
+all: makefiles.html
+
+%.html: %.docbook
+	xsltproc --output $@ /usr/share/sgml/docbook/xsl-stylesheets/xhtml/docbook.xsl $<
+
+clean:
+	rm *.html

+ 405 - 0
theos/documentation/makefiles.docbook

@@ -0,0 +1,405 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
+"http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
+<!ENTITY project "<literal>Theos</literal>">
+]>
+<chapter>
+  <title>Makefiles</title>
+
+  <section id="intro">
+    <title>Introduction</title>
+
+    <para>&project; is a set of Makefiles designed to take away the complexity
+    of building and organizing iPhoneOS projects without the use of Xcode (or
+    even Mac OS X.)</para>
+  </section>
+
+  <section id="structure">
+    <title>Structure of a Makefile</title>
+
+    <para>Here is an example makefile for a project using &project;</para>
+
+    <programlisting># System variables such as MODULES and TARGET.
+
+include theos/makefiles/common.mk
+
+# Instance-related variables such as xxx_FILES.
+
+TOOL_NAME = Simple
+Simple_FILES = simple.mm
+
+include $(THEOS_MAKE_PATH)/tool.mk
+
+# Custom rules</programlisting>
+  </section>
+
+  <section id="project-types">
+    <title>Project Types</title>
+
+    <para>Projects are divided into different types, briefly described below.
+    To create a project of a given type, simply include its makefile. For
+    example, to create a command-line tool:</para>
+
+    <programlisting>include theos/makefiles/tool.mk</programlisting>
+
+    <para>From one Makefile, you can build multiple types of project (just
+    include both project type makefiles). An example:</para>
+
+    <programlisting>include theos/makefiles/common.mk
+
+TWEAK_NAME = Simple
+Simple_FILES = Tweak.mm
+
+TOOL_NAME = simpleutility
+simpleutility_FILES = su.c
+
+include $(THEOS_MAKE_PATH)/tweak.mk
+include $(THEOS_MAKE_PATH)/tool.mk</programlisting>
+
+    <para>You can also build multiple instances of a single project type from
+    one Makefile.</para>
+
+    <programlisting>include theos/makefiles/common.mk
+
+TWEAK_NAME = Simple Complex
+Simple_FILES = Tweak.mm
+Complex_FILES = 1.mm 2.mm 3.mm 4.mm
+
+include $(THEOS_MAKE_PATH)/tweak.mk</programlisting>
+
+    <section>
+      <title>Aggregate (<filename>aggregate.mk</filename>)</title>
+
+      <para>An Aggregate project is a project that consists of several
+      subprojects. Each subproject can be any valid type (including another
+      Aggregate).</para>
+
+      <variablelist>
+        <varlistentry>
+          <term><varname>SUBPROJECTS</varname></term>
+
+          <listitem>
+            <para><varname>The SUBPROJECTS</varname> variable defines the
+            directory names that contain the subprojects this Aggregate
+            project should build.</para>
+          </listitem>
+        </varlistentry>
+      </variablelist>
+    </section>
+
+    <section>
+      <title><systemitem class="library">UIKit</systemitem> Applications
+      (<filename>application.mk</filename>)</title>
+
+      <para>An <literal>application</literal> is an Objective-C program that includes a GUI
+      component, and by default links against <systemitem
+      class="library">UIKit</systemitem>.</para>
+    </section>
+
+    <section>
+      <title>Command Line Tools (<filename>tool.mk</filename>)</title>
+
+      <para>A <literal>tool</literal> is a program that does not have a GUI component, and differs from an <literal>application</literal> wherein it does
+      not link against <systemitem class="library">UIKit</systemitem>. This
+      project type is intended for command-line tools, daemons, etc.</para>
+    </section>
+
+    <section>
+      <title>MobileSubstrate Tweaks (<filename>tweak.mk</filename>)</title>
+
+      <para>A <literal>tweak</literal> is a dynamic library that links against <systemitem
+      class="library">MobileSubstrate</systemitem> for the purposes of adding
+      and replacing functions and methods at runtime.</para>
+
+      <para>Tweaks in &project; are often written with the help of the Logos
+      preprocessor.</para>
+
+      <note>
+        <para>A <literal>tweak</literal> does not, by default, link against <systemitem
+        class="library">UIKit</systemitem>. If you want to link against
+        <systemitem class="library">UIKit</systemitem>, add it to
+        <varname>xxx_FRAMEWORKS</varname>.</para>
+      </note>
+    </section>
+
+    <section>
+      <title>Bundles (<filename>bundle.mk</filename>)</title>
+
+      <para>A <literal>bundle</literal> is a dynamic library meant to be loaded into another
+      application at runtime, using the <classname>NSBundle</classname> class.</para>
+    </section>
+
+    <section>
+      <title>Frameworks (<filename>framework.mk</filename>)</title>
+
+      <!--para>A framework is a dynamic library bundle, containing resources, meant to be linked by another
+      application or dynamic library.</para-->
+    </section>
+
+    <section>
+      <title>Dynamic Libraries (<filename>library.mk</filename>)</title>
+    </section>
+  </section>
+
+  <section>
+    <title>Variables</title>
+
+    <section>
+      <title>System Constants</title>
+
+      <para>These constants are listed for use in toplevel Makefiles.</para>
+
+      <variablelist>
+        <varlistentry>
+          <term><varname>THEOS</varname></term>
+          <term><varname>THEOS_MAKE_PATH</varname></term>
+          <term><varname>THEOS_BIN_PATH</varname></term>
+          <term><varname>THEOS_LIBRARY_PATH</varname></term>
+          <term><varname>THEOS_INCLUDE_PATH</varname></term>
+          <term><varname>THEOS_MODULE_PATH</varname></term>
+
+          <listitem>
+            <para>Used for locating other &project; resources, such as binaries, scripts, modules and other makefiles.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>THEOS_PLATFORM_NAME</varname></term>
+          <term><varname>THEOS_TARGET_NAME</varname></term>
+
+          <listitem>
+            <para>The build platform and the platform being targeted, normalized.</para>
+          </listitem>
+        </varlistentry>
+      </variablelist>
+    </section>
+
+    <section>
+      <title>System Variables</title>
+
+      <para>These variables are listed for use in toplevel Makefiles, but if
+      you really want to change them, you can.</para>
+
+      <variablelist>
+        <varlistentry>
+          <term><varname>THEOS_BUILD_DIR</varname></term>
+
+          <listitem>
+            <para>Build directory (objects are placed in
+            <filename><varname>THEOS_BUILD_DIR</varname>/<varname>THEOS_OBJ_DIR_NAME</varname></filename>).
+            Defaults to the current directory.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>THEOS_OBJ_DIR_NAME</varname></term>
+
+          <listitem>
+            <para>Output file directory name. Defaults to
+            <filename>.theos/obj</filename>.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>THEOS_STAGING_DIR</varname></term>
+
+          <listitem>
+            <para>Package staging directory. Defaults to
+            <filename><varname>THEOS_PROJECT_DIR</varname>/_</filename>.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>Blah</varname></term>
+
+          <listitem>
+            <para>Description</para>
+          </listitem>
+        </varlistentry>
+      </variablelist>
+    </section>
+
+    <section>
+      <title>Local Variables</title>
+
+      <para>These variables are not tied to any particular project instance,
+      and can be set either in the toplevel Makefile or in the
+      environment.</para>
+
+      <variablelist>
+        <varlistentry>
+          <term><varname>ADDITIONAL_CFLAGS</varname></term>
+
+          <term><varname>ADDITIONAL_CCFLAGS</varname></term>
+
+          <term><varname>ADDITIONAL_OBJCFLAGS</varname></term>
+
+          <term><varname>ADDITIONAL_OBJCCFLAGS</varname></term>
+
+          <term><varname>ADDITIONAL_LDFLAGS</varname></term>
+
+          <listitem>
+            <para>The <varname>ADDITIONAL_FLAGS</varname> variables control
+            additional compilation flags for an entire project. These
+            variables are not passed into subdirectories or subprojects, but
+            can be made to do so with <command>export</command>, as in
+            <command>export
+            <varname>ADDITIONAL_CFLAGS</varname></command>.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>CFLAGS</varname></term>
+
+          <term><varname>CCFLAGS</varname></term>
+
+          <term><varname>OBJCFLAGS</varname></term>
+
+          <term><varname>OBJCCFLAGS</varname></term>
+
+          <term><varname>LDFLAGS</varname></term>
+
+          <listitem>
+            <para>The unqualified <varname>FLAGS</varname> variables can be
+            used for additional compilation flags stored in the environment or
+            given on the commandline, as in <command>make
+            <varname>CFLAGS</varname>=-funroll-loops</command>.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>OPTFLAG</varname></term>
+
+          <listitem>
+            <para>The <varname>OPTFLAG</varname> variable controls
+            optimization. Its default value is <literal>-O2</literal>.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>DEBUG</varname></term>
+
+          <listitem>
+            <para>The <varname>DEBUG</varname> variable controls compilation
+            of debug symbols and stripping. When set to <literal>1</literal>,
+            <literal>-ggdb -DDEBUG</literal> is added to the compilation
+            flags, stripping is disabled, and optimization flags are stripped
+            from <varname>OPTFLAG</varname>. Additionally,
+            <literal>+debug</literal> is appended to the package build
+            identifier.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>Blah</varname></term>
+
+          <listitem>
+            <para>Description</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>Blah</varname></term>
+
+          <listitem>
+            <para>Description</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>Blah</varname></term>
+
+          <listitem>
+            <para>Description</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>Blah</varname></term>
+
+          <listitem>
+            <para>Description</para>
+          </listitem>
+        </varlistentry>
+      </variablelist>
+    </section>
+
+    <section>
+      <title>Project Variables</title>
+
+      <para>The various project type makefiles all support a common set of
+      variables, described below. In this list, <literal>xxx</literal> is
+      assumed to be the project instance name.</para>
+
+      <variablelist>
+        <varlistentry>
+          <term><varname>xxx_FILES</varname></term>
+
+          <listitem>
+            <para>The <varname>FILES</varname> variables contain
+            space-delimited lists of the source files comprising the project.
+	    Including files with the <filename>.m</filename> or <filename>.mm</filename> extensions causes the Objective-C runtime and
+            Foundation framework to be linked with your project.</para>
+            <para>The older type-specific <varname>FILES</varname> variables are deprecated in favour of <varname>xxx_FILES</varname>.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>xxx_OBJ_FILES</varname></term>
+
+          <listitem>
+            <para>The <varname>OBJ_FILES</varname> variable contains a
+            space-delimited list of precompiled object files
+	    (<filename>.o</filename> or library/framework binaries) to be linked with the project.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>xxx_FRAMEWORKS</varname></term>
+
+          <term><varname>xxx_PRIVATE_FRAMEWORKS</varname></term>
+
+          <listitem>
+            <para>The <varname>FRAMEWORKS</varname> variables contain
+            space-delimited lists of frameworks to link with the project, if
+            Objective-C source files are used. Including
+            <varname>PRIVATE_FRAMEWORKS</varname> causes the private Framework
+            directory to be included in the Framework search path.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>xxx_CFLAGS</varname></term>
+
+          <term><varname>xxx_CCFLAGS</varname></term>
+
+          <term><varname>xxx_OBJCFLAGS</varname></term>
+
+          <term><varname>xxx_OBJCCFLAGS</varname></term>
+
+          <listitem>
+            <para>The <varname>FLAGS</varname> variables contain flags passed
+            to the compiler for a given filetype.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>xxx_LDFLAGS</varname></term>
+
+          <listitem>
+            <para>The <varname>LDFLAGS</varname> variable contains flags
+            passed to the linker for a project.</para>
+          </listitem>
+        </varlistentry>
+
+        <varlistentry>
+          <term><varname>Blah</varname></term>
+
+          <listitem>
+            <para>Description</para>
+          </listitem>
+        </varlistentry>
+      </variablelist>
+    </section>
+  </section>
+</chapter>

+ 3 - 0
theos/extras/vim/README

@@ -0,0 +1,3 @@
+Place the following in filetype.vim.
+
+au BufNewFile,BufRead *.xm,*.xmm,*.l.mm setf logos

+ 11 - 0
theos/extras/vim/ftplugin/logos.vim

@@ -0,0 +1,11 @@
+" Vim filetype plugin file
+" Language: Logos (Objective-C++)
+" Maintainer: Dustin Howett
+" Latest Revision: December 22, 2009
+
+if exists("b:did_ftplugin")
+	finish
+endif
+
+" Behaves just like Objective-C
+runtime! ftplugin/objc.vim

+ 6 - 0
theos/extras/vim/indent/logos.vim

@@ -0,0 +1,6 @@
+" Vim filetype plugin file
+" Language: Logos (Objective-C++)
+" Maintainer: Dustin Howett
+" Latest Revision: December 22, 2009
+
+runtime! indent/objc.vim

+ 69 - 0
theos/extras/vim/syntax/logos.vim

@@ -0,0 +1,69 @@
+" Vim syntax file
+" Language: Logos (Objective-C++)
+" Maintainer: Dustin Howett
+" Latest Revision: April 2, 2011
+
+if exists("b:current_syntax")
+	finish
+endif
+
+runtime! syntax/objc.vim
+
+syn match logosDirective '%\(hook\|group\|subclass\|ctor\)' display
+
+syn match logosDirective '%end' display
+
+syn match logosDirective '%class' display skipwhite nextgroup=logosClassName
+
+syn match logosDirective '%log' display contained containedin=logosHook,logosSubclass,logosGroup
+syn match logosDirective '%orig' display contained containedin=logosHook,logosSubclass,logosGroup
+
+syn match logosDirective '%init' display
+syn match logosDirective '%new' display
+syn region logosInit matchgroup=logosDirective start='%init(' end=')' contains=cParen
+
+syn region logosNew oneline matchgroup=logosNew start='%new(' end=')' contains=logosTypeEncoding,logosTypeEncodingUnion
+
+syn match logosTypeEncoding '[*@#:\[\]^?{}A-Za-z0-9$=]' display contained
+syn region logosTypeEncodingUnion oneline matchgroup=logosTypeEncoding start='(' end=')' contained transparent
+
+syn region logosInfixClass oneline matchgroup=logosInfixClass start='%c(' end=')' contains=logosClassName containedin=objcMessage
+syn match logosClassName '[A-Za-z$_][A-Za-z0-9_$]*' display contained
+
+syn region logosHook start="%hook" end="%end" fold transparent keepend extend
+syn region logosGroup start="%group" end="%end" fold transparent keepend extend
+syn region logosSubclass start="%subclass" end="%end" fold transparent keepend extend
+
+syn match logosError '\(@interface\|@implementation\)' contained containedin=logosHook,logosSubclass,logosGroup
+syn match logosError '\(%hook\|%group\|%subclass\)' contained containedin=objcImp,objcHeader
+
+syn match logosDirectiveArgument '\(%\(hook\|subclass\|group\)\)\@<=\s\+\k\+' display contained containedin=logosHook,logosSubclass,logosGroup
+syn match logosSubclassSuperclassName '\(%subclass\s\+\k\+\s*:\)\@<=\s*\k\+' display contained containedin=logosHook,logosSubclass,logosGroup
+
+syn cluster cParenGroup add=logosNew,logosInfixClass,logosInit
+syn cluster cParenGroup add=logosTypeEncoding,logosTypeEncodingUnion
+syn cluster cParenGroup add=logosClassName
+syn cluster cParenGroup add=logosDirectiveArgument,logosSubclassSuperclassName
+syn cluster cParenGroup add=logosError
+syn cluster cParenGroup add=logosHook,logosGroup,logosSubclass
+
+syn cluster cPreProcGroup add=logosClassName,logosDirective,logosTypeEncoding,logosTypeEncodingUnion
+
+syn cluster cMultiGroup add=logosTypeEncoding,logosTypeEncodingUnion,logosClassName
+
+syn sync match logosHookSync grouphere logosHook "%hook"
+syn sync match logosGroupSync grouphere logosGroup "%group"
+syn sync match logosSubclassSync grouphere logosSubclass "%subclass"
+syn sync match logosEndSync grouphere NONE "%end"
+
+let b:current_syntax = "logos"
+hi def link logosDirective PreProc
+hi def link logosDirectiveArgument String
+hi def link logosError Error
+
+hi def link logosTypeEncoding logosDirectiveArgument
+hi def link logosGroupName logosDirectiveArgument
+hi def link logosClassName logosDirectiveArgument
+hi def link logosSubclassSuperclassName logosClassName
+hi def link logosNew logosDirective
+hi def link logosInfixClass logosDirective

+ 11 - 0
theos/git-submodule-recur.sh

@@ -0,0 +1,11 @@
+#!/bin/sh
+
+SELF=$(cd ${0%/*} && echo $PWD/${0##*/})
+
+case "$1" in
+        "init") CMD="submodule update --init" ;;
+        *) CMD="$*" ;;
+esac
+
+git $CMD
+git submodule foreach "$SELF" $CMD

+ 0 - 0
theos/include/.keep


+ 1 - 0
theos/include/APDeviceController.h

@@ -0,0 +1 @@
+../../yourTube/APDeviceController.h

+ 32 - 0
theos/include/AppSupport/CPDistributedMessagingCenter.h

@@ -0,0 +1,32 @@
+@interface CPDistributedMessagingCenter : NSObject
+
++ (CPDistributedMessagingCenter*)centerNamed:(NSString*)serverName;
+
+- (BOOL)sendMessageName:(NSString*)name
+			   userInfo:(NSDictionary*)info;
+
+- (NSDictionary*)sendMessageAndReceiveReplyName:(NSString*)name
+									   userInfo:(NSDictionary*)info;
+
+- (NSDictionary*)sendMessageAndReceiveReplyName:(NSString*)name
+									   userInfo:(NSDictionary*)info
+										  error:(NSError**)error;
+
+- (void)sendMessageAndReceiveReplyName:(NSString*)name
+							  userInfo:(NSDictionary*)info
+							  toTarget:(id)target
+							  selector:(SEL)selector
+							   context:(void*)context;
+
+- (void)runServerOnCurrentThread;
+- (void)runServerOnCurrentThreadProtectedByEntitlement:(id)entitlement;
+- (void)stopServer;
+
+- (void)registerForMessageName:(NSString*)messageName
+						target:(id)target
+					  selector:(SEL)selector;
+
+- (void)unregisterForMessageName:(NSString*)messageName;
+
+@end
+

+ 65 - 0
theos/include/ContextFilterLogFormatter.h

@@ -0,0 +1,65 @@
+#import <Foundation/Foundation.h>
+#import "DDLog.h"
+
+@class ContextFilterLogFormatter;
+
+/**
+ * Welcome to Cocoa Lumberjack!
+ * 
+ * The project page has a wealth of documentation if you have any questions.
+ * https://github.com/robbiehanson/CocoaLumberjack
+ * 
+ * If you're new to the project you may wish to read the "Getting Started" page.
+ * https://github.com/robbiehanson/CocoaLumberjack/wiki/GettingStarted
+ * 
+ * 
+ * This class provides a log formatter that filters log statements from a logging context not on the whitelist.
+ * 
+ * A log formatter can be added to any logger to format and/or filter its output.
+ * You can learn more about log formatters here:
+ * https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomFormatters
+ * 
+ * You can learn more about logging context's here:
+ * https://github.com/robbiehanson/CocoaLumberjack/wiki/CustomContext
+ *
+ * But here's a quick overview / refresher:
+ * 
+ * Every log statement has a logging context.
+ * These come from the underlying logging macros defined in DDLog.h.
+ * The default logging context is zero.
+ * You can define multiple logging context's for use in your application.
+ * For example, logically separate parts of your app each have a different logging context.
+ * Also 3rd party frameworks that make use of Lumberjack generally use their own dedicated logging context.
+**/
+@interface ContextWhitelistFilterLogFormatter : NSObject <DDLogFormatter>
+
+- (id)init;
+
+- (void)addToWhitelist:(int)loggingContext;
+- (void)removeFromWhitelist:(int)loggingContext;
+
+- (NSArray *)whitelist;
+
+- (BOOL)isOnWhitelist:(int)loggingContext;
+
+@end
+
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+#pragma mark -
+////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
+
+/**
+ * This class provides a log formatter that filters log statements from a logging context on the blacklist.
+**/
+@interface ContextBlacklistFilterLogFormatter : NSObject <DDLogFormatter>
+
+- (id)init;
+
+- (void)addToBlacklist:(int)loggingContext;
+- (void)removeFromBlacklist:(int)loggingContext;
+
+- (NSArray *)blacklist;
+
+- (BOOL)isOnBlacklist:(int)loggingContext;
+
+@end

+ 41 - 0
theos/include/DDASLLogger.h

@@ -0,0 +1,41 @@
+#import <Foundation/Foundation.h>
+#import <asl.h>
+
+#import "DDLog.h"
+
+/**
+ * Welcome to Cocoa Lumberjack!
+ * 
+ * The project page has a wealth of documentation if you have any questions.
+ * https://github.com/robbiehanson/CocoaLumberjack
+ * 
+ * If you're new to the project you may wish to read the "Getting Started" wiki.
+ * https://github.com/robbiehanson/CocoaLumberjack/wiki/GettingStarted
+ * 
+ * 
+ * This class provides a logger for the Apple System Log facility.
+ * 
+ * As described in the "Getting Started" page,
+ * the traditional NSLog() function directs it's output to two places:
+ * 
+ * - Apple System Log
+ * - StdErr (if stderr is a TTY) so log statements show up in Xcode console
+ * 
+ * To duplicate NSLog() functionality you can simply add this logger and a tty logger.
+ * However, if you instead choose to use file logging (for faster performance),
+ * you may choose to use a file logger and a tty logger.
+**/
+
+@interface DDASLLogger : DDAbstractLogger <DDLogger>
+{
+	aslclient client;
+}
+
++ (DDASLLogger *)sharedInstance;
+
+// Inherited from DDAbstractLogger
+
+// - (id <DDLogFormatter>)logFormatter;
+// - (void)setLogFormatter:(id <DDLogFormatter>)formatter;
+
+@end

+ 0 - 0
theos/include/DDAbstractDatabaseLogger.h


Some files were not shown because too many files changed in this diff