Skip to content
Browse files

Mocha is now a full-fleged scripting environment. Renamed moc to moch…

…a, added support for reading stdin. Moved print() from the interpreter to the runtime.
  • Loading branch information...
1 parent 5a5032a commit 905abc79bf181003a66b7b458d95b405c5617936 @logancollins committed Aug 8, 2012
View
6 InstallPackage/Scripts/postinstall
@@ -3,13 +3,13 @@
import os
-MOC_SYMLINK_SRC = "/Library/Frameworks/Mocha.framework/Resources/moc"
-MOC_SYMLINK_DEST = "/usr/local/bin/moc"
+MOCHA_SYMLINK_SRC = "/Library/Frameworks/Mocha.framework/Resources/mocha"
+MOCHA_SYMLINK_DEST = "/usr/local/bin/mocha"
# Create symbolic links
try:
os.makedirs('/usr/local/bin')
except OSError:
pass
-os.symlink(MOC_SYMLINK_SRC, MOC_SYMLINK_DEST)
+os.symlink(MOCHA_SYMLINK_SRC, MOCHA_SYMLINK_DEST)
View
5 InstallPackage/Scripts/preinstall
@@ -4,8 +4,13 @@ import os
MOC_SYMLINK_PATH = "/usr/local/bin/moc"
+MOCHA_SYMLINK_PATH = "/usr/local/bin/mocha"
# Remove moc symlink
if os.path.exists(MOC_SYMLINK_PATH):
os.remove(MOC_SYMLINK_PATH)
+
+# Remove mocha symlink
+if os.path.exists(MOCHA_SYMLINK_PATH):
+ os.remove(MOCHA_SYMLINK_PATH)
View
72 Mocha.xcodeproj/project.pbxproj
@@ -57,6 +57,7 @@
0211232C155D99F600415471 /* MOBridgeSupportSymbol.h in Headers */ = {isa = PBXBuildFile; fileRef = 0211232A155D99F600415471 /* MOBridgeSupportSymbol.h */; settings = {ATTRIBUTES = (Private, ); }; };
0211233E155DB8F800415471 /* MOBridgeSupportLibrary.h in Headers */ = {isa = PBXBuildFile; fileRef = 0211233C155DB8F800415471 /* MOBridgeSupportLibrary.h */; settings = {ATTRIBUTES = (Private, ); }; };
02112342155DB9A100415471 /* MOBridgeSupportParser.h in Headers */ = {isa = PBXBuildFile; fileRef = 02112340155DB9A100415471 /* MOBridgeSupportParser.h */; settings = {ATTRIBUTES = (Private, ); }; };
+ 02373A2715D2DDBA005C5D0A /* NSFileHandle+MochaAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 02373A2615D2DDBA005C5D0A /* NSFileHandle+MochaAdditions.m */; };
025184A115D182FA005D661F /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 025184A015D182FA005D661F /* Cocoa.framework */; };
026BE1FD15D05C9800BC4B73 /* MOMapTable.m in Sources */ = {isa = PBXBuildFile; fileRef = 026BE1FB15D05C9800BC4B73 /* MOMapTable.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
026BE1FE15D05C9800BC4B73 /* MOMapTable.m in Sources */ = {isa = PBXBuildFile; fileRef = 026BE1FB15D05C9800BC4B73 /* MOMapTable.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
@@ -281,6 +282,8 @@
0211233D155DB8F800415471 /* MOBridgeSupportLibrary.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MOBridgeSupportLibrary.m; sourceTree = "<group>"; };
02112340155DB9A100415471 /* MOBridgeSupportParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MOBridgeSupportParser.h; sourceTree = "<group>"; };
02112341155DB9A100415471 /* MOBridgeSupportParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MOBridgeSupportParser.m; sourceTree = "<group>"; };
+ 02373A2515D2DDBA005C5D0A /* NSFileHandle+MochaAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSFileHandle+MochaAdditions.h"; sourceTree = "<group>"; };
+ 02373A2615D2DDBA005C5D0A /* NSFileHandle+MochaAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSFileHandle+MochaAdditions.m"; sourceTree = "<group>"; };
023EB37E1587A2D40072EC4E /* postinstall */ = {isa = PBXFileReference; lastKnownFileType = text.script.python; path = postinstall; sourceTree = "<group>"; };
023EB37F1587A2D40072EC4E /* preinstall */ = {isa = PBXFileReference; lastKnownFileType = text.script.python; path = preinstall; sourceTree = "<group>"; };
025184A015D182FA005D661F /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
@@ -362,9 +365,9 @@
EAA45E22155CE57600F93E40 /* MochaRuntime.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MochaRuntime.h; sourceTree = "<group>"; wrapsLines = 1; };
EAA45E23155CE57600F93E40 /* MochaRuntime.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MochaRuntime.m; sourceTree = "<group>"; };
EAA45E26155CE66600F93E40 /* MochaRuntime_Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MochaRuntime_Private.h; sourceTree = "<group>"; };
- EAA45E71155EE5AF00F93E40 /* moc */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = moc; sourceTree = BUILT_PRODUCTS_DIR; };
+ EAA45E71155EE5AF00F93E40 /* mocha */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = mocha; sourceTree = BUILT_PRODUCTS_DIR; };
EAA45E75155EE5AF00F93E40 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
- EAA45E78155EE5AF00F93E40 /* moc-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "moc-Prefix.pch"; sourceTree = "<group>"; };
+ EAA45E78155EE5AF00F93E40 /* mocha-Prefix.pch */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "mocha-Prefix.pch"; sourceTree = "<group>"; };
EAA45E7F155EE68000F93E40 /* MOCInterpreter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MOCInterpreter.h; sourceTree = "<group>"; };
EAA45E80155EE68000F93E40 /* MOCInterpreter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MOCInterpreter.m; sourceTree = "<group>"; };
EAA45E86155EE97200F93E40 /* MOMethod.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MOMethod.h; sourceTree = "<group>"; };
@@ -452,6 +455,15 @@
path = BridgeSupport;
sourceTree = "<group>";
};
+ 02373A2415D2DDA1005C5D0A /* Categories */ = {
+ isa = PBXGroup;
+ children = (
+ 02373A2515D2DDBA005C5D0A /* NSFileHandle+MochaAdditions.h */,
+ 02373A2615D2DDBA005C5D0A /* NSFileHandle+MochaAdditions.m */,
+ );
+ path = Categories;
+ sourceTree = "<group>";
+ };
023EB37B1587A2AE0072EC4E /* InstallPackage */ = {
isa = PBXGroup;
children = (
@@ -613,9 +625,9 @@
EAA45DF7155CE44B00F93E40 = {
isa = PBXGroup;
children = (
- EAA45E0C155CE44B00F93E40 /* Mocha */,
+ EAA45E0C155CE44B00F93E40 /* Framework */,
EAA34FCA15647AD700B62244 /* libMocha (iOS) */,
- EAA45E74155EE5AF00F93E40 /* moc */,
+ EAA45E74155EE5AF00F93E40 /* mocha */,
023EB37B1587A2AE0072EC4E /* InstallPackage */,
02BF151215C096070004CA2C /* UnitTests */,
EAA45E05155CE44B00F93E40 /* Frameworks */,
@@ -628,7 +640,7 @@
isa = PBXGroup;
children = (
EAA45E03155CE44B00F93E40 /* Mocha.framework */,
- EAA45E71155EE5AF00F93E40 /* moc */,
+ EAA45E71155EE5AF00F93E40 /* mocha */,
EAA34FC815647AD700B62244 /* libMocha.a */,
02BF150915C096070004CA2C /* UnitTests.octest */,
);
@@ -650,7 +662,7 @@
name = Frameworks;
sourceTree = "<group>";
};
- EAA45E0C155CE44B00F93E40 /* Mocha */ = {
+ EAA45E0C155CE44B00F93E40 /* Framework */ = {
isa = PBXGroup;
children = (
EAA45E13155CE44B00F93E40 /* Mocha.h */,
@@ -665,6 +677,7 @@
EAC8BFCB155F42D3003653BA /* Categories */,
EAA45E0D155CE44B00F93E40 /* Supporting Files */,
);
+ name = Framework;
path = Mocha;
sourceTree = "<group>";
};
@@ -706,21 +719,23 @@
path = Utilities;
sourceTree = "<group>";
};
- EAA45E74155EE5AF00F93E40 /* moc */ = {
+ EAA45E74155EE5AF00F93E40 /* mocha */ = {
isa = PBXGroup;
children = (
EAA45E75155EE5AF00F93E40 /* main.m */,
EAA45E7F155EE68000F93E40 /* MOCInterpreter.h */,
EAA45E80155EE68000F93E40 /* MOCInterpreter.m */,
+ 02373A2415D2DDA1005C5D0A /* Categories */,
EAA45E77155EE5AF00F93E40 /* Supporting Files */,
);
+ name = mocha;
path = moc;
sourceTree = "<group>";
};
EAA45E77155EE5AF00F93E40 /* Supporting Files */ = {
isa = PBXGroup;
children = (
- EAA45E78155EE5AF00F93E40 /* moc-Prefix.pch */,
+ EAA45E78155EE5AF00F93E40 /* mocha-Prefix.pch */,
);
name = "Supporting Files";
sourceTree = "<group>";
@@ -882,9 +897,9 @@
productReference = EAA34FC815647AD700B62244 /* libMocha.a */;
productType = "com.apple.product-type.library.static";
};
- EAA45E02155CE44B00F93E40 /* Mocha */ = {
+ EAA45E02155CE44B00F93E40 /* Framework */ = {
isa = PBXNativeTarget;
- buildConfigurationList = EAA45E18155CE44B00F93E40 /* Build configuration list for PBXNativeTarget "Mocha" */;
+ buildConfigurationList = EAA45E18155CE44B00F93E40 /* Build configuration list for PBXNativeTarget "Framework" */;
buildPhases = (
EAA45DFE155CE44B00F93E40 /* Sources */,
EAA45DFF155CE44B00F93E40 /* Frameworks */,
@@ -895,14 +910,14 @@
);
dependencies = (
);
- name = Mocha;
+ name = Framework;
productName = Mocha;
productReference = EAA45E03155CE44B00F93E40 /* Mocha.framework */;
productType = "com.apple.product-type.framework";
};
- EAA45E70155EE5AF00F93E40 /* moc */ = {
+ EAA45E70155EE5AF00F93E40 /* mocha */ = {
isa = PBXNativeTarget;
- buildConfigurationList = EAA45E7B155EE5AF00F93E40 /* Build configuration list for PBXNativeTarget "moc" */;
+ buildConfigurationList = EAA45E7B155EE5AF00F93E40 /* Build configuration list for PBXNativeTarget "mocha" */;
buildPhases = (
EAA45E6D155EE5AF00F93E40 /* Sources */,
EAA45E6E155EE5AF00F93E40 /* Frameworks */,
@@ -913,9 +928,9 @@
dependencies = (
EAA3503915647DEA00B62244 /* PBXTargetDependency */,
);
- name = moc;
+ name = mocha;
productName = moc;
- productReference = EAA45E71155EE5AF00F93E40 /* moc */;
+ productReference = EAA45E71155EE5AF00F93E40 /* mocha */;
productType = "com.apple.product-type.tool";
};
/* End PBXNativeTarget section */
@@ -940,9 +955,9 @@
projectDirPath = "";
projectRoot = "";
targets = (
- EAA45E02155CE44B00F93E40 /* Mocha */,
+ EAA45E02155CE44B00F93E40 /* Framework */,
EAA34FC715647AD700B62244 /* libMocha (iOS) */,
- EAA45E70155EE5AF00F93E40 /* moc */,
+ EAA45E70155EE5AF00F93E40 /* mocha */,
02BF150815C096070004CA2C /* UnitTests */,
023EB3801587A31A0072EC4E /* FrameworkComplete */,
023EB3841587A3210072EC4E /* InstallPackage */,
@@ -985,7 +1000,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
- shellScript = "cp \"${BUILT_PRODUCTS_DIR}/moc\" \"${BUILT_PRODUCTS_DIR}/Mocha.framework/Resources/\"";
+ shellScript = "cp \"${BUILT_PRODUCTS_DIR}/mocha\" \"${BUILT_PRODUCTS_DIR}/Mocha.framework/Resources/\"";
};
023EB3901587A3520072EC4E /* Build Install Package */ = {
isa = PBXShellScriptBuildPhase;
@@ -1133,6 +1148,7 @@
files = (
EAA45E76155EE5AF00F93E40 /* main.m in Sources */,
EAA45E81155EE68000F93E40 /* MOCInterpreter.m in Sources */,
+ 02373A2715D2DDBA005C5D0A /* NSFileHandle+MochaAdditions.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -1141,12 +1157,12 @@
/* Begin PBXTargetDependency section */
023EB3891587A3300072EC4E /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
- target = EAA45E02155CE44B00F93E40 /* Mocha */;
+ target = EAA45E02155CE44B00F93E40 /* Framework */;
targetProxy = 023EB3881587A3300072EC4E /* PBXContainerItemProxy */;
};
023EB38B1587A3320072EC4E /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
- target = EAA45E70155EE5AF00F93E40 /* moc */;
+ target = EAA45E70155EE5AF00F93E40 /* mocha */;
targetProxy = 023EB38A1587A3320072EC4E /* PBXContainerItemProxy */;
};
023EB38F1587A34F0072EC4E /* PBXTargetDependency */ = {
@@ -1156,12 +1172,12 @@
};
028D628315C064BC00C12842 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
- target = EAA45E02155CE44B00F93E40 /* Mocha */;
+ target = EAA45E02155CE44B00F93E40 /* Framework */;
targetProxy = 028D628215C064BC00C12842 /* PBXContainerItemProxy */;
};
EAA3503915647DEA00B62244 /* PBXTargetDependency */ = {
isa = PBXTargetDependency;
- target = EAA45E02155CE44B00F93E40 /* Mocha */;
+ target = EAA45E02155CE44B00F93E40 /* Framework */;
targetProxy = EAA3503815647DEA00B62244 /* PBXContainerItemProxy */;
};
/* End PBXTargetDependency section */
@@ -1380,7 +1396,7 @@
GCC_PREFIX_HEADER = "Mocha/Mocha-Prefix.pch";
INFOPLIST_FILE = "Mocha/Mocha-Info.plist";
INSTALL_PATH = "@rpath";
- PRODUCT_NAME = "$(TARGET_NAME)";
+ PRODUCT_NAME = Mocha;
WRAPPER_EXTENSION = framework;
};
name = Debug;
@@ -1397,7 +1413,7 @@
GCC_PREFIX_HEADER = "Mocha/Mocha-Prefix.pch";
INFOPLIST_FILE = "Mocha/Mocha-Info.plist";
INSTALL_PATH = "@rpath";
- PRODUCT_NAME = "$(TARGET_NAME)";
+ PRODUCT_NAME = Mocha;
WRAPPER_EXTENSION = framework;
};
name = Release;
@@ -1407,7 +1423,7 @@
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
- GCC_PREFIX_HEADER = "moc/moc-Prefix.pch";
+ GCC_PREFIX_HEADER = "moc/mocha-Prefix.pch";
LD_RUNPATH_SEARCH_PATHS = "@loader_path/../../../../ @loader_path/";
PRODUCT_NAME = "$(TARGET_NAME)";
};
@@ -1418,7 +1434,7 @@
buildSettings = {
CLANG_ENABLE_OBJC_ARC = YES;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
- GCC_PREFIX_HEADER = "moc/moc-Prefix.pch";
+ GCC_PREFIX_HEADER = "moc/mocha-Prefix.pch";
LD_RUNPATH_SEARCH_PATHS = "@loader_path/../../../../ @loader_path/";
PRODUCT_NAME = "$(TARGET_NAME)";
};
@@ -1481,7 +1497,7 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
- EAA45E18155CE44B00F93E40 /* Build configuration list for PBXNativeTarget "Mocha" */ = {
+ EAA45E18155CE44B00F93E40 /* Build configuration list for PBXNativeTarget "Framework" */ = {
isa = XCConfigurationList;
buildConfigurations = (
EAA45E19155CE44B00F93E40 /* Debug */,
@@ -1490,7 +1506,7 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
- EAA45E7B155EE5AF00F93E40 /* Build configuration list for PBXNativeTarget "moc" */ = {
+ EAA45E7B155EE5AF00F93E40 /* Build configuration list for PBXNativeTarget "mocha" */ = {
isa = XCConfigurationList;
buildConfigurations = (
EAA45E7C155EE5AF00F93E40 /* Debug */,
View
11 Mocha/MochaRuntime.m
@@ -871,9 +871,20 @@ - (void)installBuiltins {
MOMethod *addFrameworkSearchPath = [MOMethod methodWithTarget:self selector:@selector(addFrameworkSearchPath:)];
[self setValue:addFrameworkSearchPath forKey:@"addFrameworkSearchPath"];
+ MOMethod *print = [MOMethod methodWithTarget:self selector:@selector(print:)];
+ [self setValue:print forKey:@"print"];
+
[self setValue:[MOObjCRuntime sharedRuntime] forKey:@"objc"];
}
+- (void)print:(id)o {
+ if (!o) {
+ printf("null\n");
+ return;
+ }
+ printf("%s\n", [[o description] UTF8String]);
+}
+
- (void)cleanUp {
// Cleanup if we created the JavaScriptCore context
if (_ownsContext) {
View
17 moc/Categories/NSFileHandle+MochaAdditions.h
@@ -0,0 +1,17 @@
+//
+// NSFileHandle+MochaAdditions.h
+// mocha
+//
+// Created by Logan Collins on 8/8/12.
+// Copyright (c) 2012 Sunflower Softworks. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+
+@interface NSFileHandle (MochaAdditions)
+
+- (BOOL)mo_isReadable;
+- (BOOL)mo_isTerminal;
+
+@end
View
30 moc/Categories/NSFileHandle+MochaAdditions.m
@@ -0,0 +1,30 @@
+//
+// NSFileHandle+MochaAdditions.m
+// mocha
+//
+// Created by Logan Collins on 8/8/12.
+// Copyright (c) 2012 Sunflower Softworks. All rights reserved.
+//
+
+#import "NSFileHandle+MochaAdditions.h"
+
+
+@implementation NSFileHandle (MochaAdditions)
+
+- (BOOL)mo_isReadable {
+ int fd = [self fileDescriptor];
+ fd_set fdset;
+ struct timeval tmout = { 0, 0 }; // return immediately
+ FD_ZERO(&fdset);
+ FD_SET(fd, &fdset);
+ if (select(fd + 1, &fdset, NULL, NULL, &tmout) <= 0)
+ return NO;
+ return FD_ISSET(fd, &fdset);
+}
+
+- (BOOL)mo_isTerminal {
+ int fd = [self fileDescriptor];
+ return (isatty(fd) == 1 ? YES : NO);
+}
+
+@end
View
3 moc/MOCInterpreter.h
@@ -1,6 +1,6 @@
//
// MOCInterpreter.h
-// Mocha
+// mocha
//
// Created by Logan Collins on 5/12/12.
// Copyright (c) 2012 Sunflower Softworks. All rights reserved.
@@ -13,6 +13,5 @@
@interface MOCInterpreter : NSObject <MochaDelegate>
- (void)run;
-- (void)runScriptAtPath:(NSString*)path;
@end
View
53 moc/MOCInterpreter.m
@@ -1,6 +1,6 @@
//
// MOCInterpreter.m
-// Mocha
+// mocha
//
// Created by Logan Collins on 5/12/12.
// Copyright (c) 2012 Sunflower Softworks. All rights reserved.
@@ -44,47 +44,6 @@ - (void)installBuiltins {
MOMethod *exit = [MOMethod methodWithTarget:self selector:@selector(exit)];
[runtime setValue:exit forKey:@"exit"];
-
- MOMethod *print = [MOMethod methodWithTarget:self selector:@selector(print:)];
- [runtime setValue:print forKey:@"print"];
-}
-
-- (void)runScriptAtPath:(NSString*)path {
-
- Mocha *runtime = [Mocha sharedRuntime];
- [runtime setDelegate:self];
-
- [self installBuiltins];
-
- NSError *err;
- NSString *s = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
-
- if (!s) {
- NSLog(@"Could not read the file at %@", path);
- NSLog(@"%@", err);
- exit(1);
- }
-
- @try {
- JSValueRef value = [runtime evalJSString:s scriptPath:path];
-
- if (value != NULL) {
- JSStringRef string = JSValueToStringCopy([runtime context], value, NULL);
- NSString *description = (NSString *)CFBridgingRelease(JSStringCopyCFString(NULL, string));
- JSStringRelease(string);
- printf("%s\n", [description UTF8String]);
- }
- }
-
- @catch (NSException *e) {
- if ([e userInfo] != nil) {
- printf("%s: %s\n%s\n", [[e name] UTF8String], [[e reason] UTF8String], [[[e userInfo] description] UTF8String]);
- }
- else {
- printf("%s: %s\n", [[e name] UTF8String], [[e reason] UTF8String]);
- }
- }
-
}
- (void)run {
@@ -142,16 +101,6 @@ - (void)exit {
exit(0);
}
-- (void)print:(id)o {
-
- if (!o) {
- printf("null\n");
- return;
- }
-
- printf("%s\n", [[o description] UTF8String]);
-}
-
@end
View
121 moc/main.m
@@ -1,28 +1,141 @@
//
// main.m
-// moc
+// mocha
//
// Created by Logan Collins on 5/12/12.
// Copyright (c) 2012 Sunflower Softworks. All rights reserved.
//
#import <Foundation/Foundation.h>
+#import <getopt.h>
+
+#import <Mocha/MochaRuntime_Private.h>
#import "MOCInterpreter.h"
+#import "NSFileHandle+MochaAdditions.h"
+
+
+static const char * program_name = "mocha";
+static const char * program_version = "1.0";
+
+
+static const char * short_options = "hv";
+static struct option long_options[] = {
+ { "help", optional_argument, NULL, 'h' },
+ { "version", optional_argument, NULL, 'v' },
+ { NULL, 0, NULL, 0 }
+};
+
+
+static void printUsage(FILE *stream) {
+ fprintf(stream, "%s %s\n", program_name, program_version);
+ fprintf(stream, "Usage: %s [-hv] [file]\n", program_name);
+ fprintf(stream,
+ " -h, --help Show this help information.\n"
+ " -v, --version Show the program's version number.\n"
+ );
+}
+
+
+static void printVersion(void) {
+ printf("%s %s\n", program_name, program_version);
+}
+
+
+void executeScript(NSString *script, NSString *path);
int main(int argc, const char * argv[]) {
@autoreleasepool {
+ NSMutableArray *filePaths = [NSMutableArray array];
- MOCInterpreter *interpreter = [[MOCInterpreter alloc] init];
+ int next_option;
+ do {
+ next_option = getopt_long(argc, (char * const *)argv, short_options, long_options, NULL);
+
+ switch (next_option) {
+ case -1: {
+ break;
+ }
+ case 'v': {
+ printVersion();
+ exit(0);
+ break;
+ }
+ case 'h': {
+ printUsage(stdout);
+ exit(0);
+ break;
+ }
+ case '?': {
+ printUsage(stderr);
+ exit(1);
+ break;
+ }
+ }
+ }
+ while (next_option != -1);
- if (argc > 1) {
- [interpreter runScriptAtPath:[NSString stringWithUTF8String:argv[1]]];
+ if (optind < argc) {
+ while (optind < argc) {
+ const char * arg = argv[optind++];
+ NSString *string = [NSString stringWithUTF8String:arg];
+ [filePaths addObject:string];
+ }
+ }
+
+ NSFileHandle *stdinHandle = [NSFileHandle fileHandleWithStandardInput];
+
+ if ([filePaths count] > 0) {
+ // Execute files
+ for (NSString *path in filePaths) {
+ NSError *err;
+ NSString *s = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
+
+ if (!s) {
+ NSLog(@"Could not read the file at %@", path);
+ NSLog(@"%@", err);
+ exit(1);
+ }
+
+ executeScript(s, path);
+ }
+ }
+ else if ([stdinHandle mo_isReadable]) {
+ // Execute contents of stdin
+ NSData *stdinData = [stdinHandle readDataToEndOfFile];
+ NSString *string = [[NSString alloc] initWithData:stdinData encoding:NSUTF8StringEncoding];
+ executeScript(string, nil);
}
else {
+ // Interactive mode
+ MOCInterpreter *interpreter = [[MOCInterpreter alloc] init];
[interpreter run];
}
}
return 0;
}
+
+void executeScript(NSString *script, NSString *path) {
+ Mocha *runtime = [[Mocha alloc] init];
+
+ if ([script length] >= 2 && [[script substringWithRange:NSMakeRange(0, 2)] isEqualToString:@"#!"]) {
+ // Ignore bash shebangs
+ NSRange lineRange = [script lineRangeForRange:NSMakeRange(0, 2)];
+ script = [script substringFromIndex:NSMaxRange(lineRange)];
+ }
+
+ @try {
+ [runtime evalJSString:script scriptPath:path];
+ }
+ @catch (NSException *e) {
+ if ([e userInfo] != nil) {
+ printf("%s: %s\n%s\n", [[e name] UTF8String], [[e reason] UTF8String], [[[e userInfo] description] UTF8String]);
+ }
+ else {
+ printf("%s: %s\n", [[e name] UTF8String], [[e reason] UTF8String]);
+ }
+ }
+}
+
View
7 moc/moc-Prefix.pch
@@ -1,7 +0,0 @@
-//
-// Prefix header for all source files of the 'moc' target in the 'moc' project
-//
-
-#ifdef __OBJC__
- #import <Foundation/Foundation.h>
-#endif
View
7 moc/mocha-Prefix.pch
@@ -0,0 +1,7 @@
+//
+// Prefix header for all source files of the 'mocha' target in the 'mocha' project
+//
+
+#ifdef __OBJC__
+ #import <Foundation/Foundation.h>
+#endif

0 comments on commit 905abc7

Please sign in to comment.
Something went wrong with that request. Please try again.