]> gitweb.factorcode.org Git - factor.git/blobdiff - vm/os-macosx.mm
webapps.wiki: adding search bar
[factor.git] / vm / os-macosx.mm
index ec05eefbb944b5f34425a92e686a3d6d63dbc5fc..71c70f7a489cf505f1a90c4e7c5a317c64a5e83e 100644 (file)
@@ -2,6 +2,8 @@
 
 #include <mach/mach_time.h>
 #include <sys/utsname.h>
+#include <unistd.h>
+#include <stdio.h>
 
 #include "master.hpp"
 
@@ -22,31 +24,41 @@ void early_init(void) {
   }
 }
 
+// You must free() this yourself.
 const char* vm_executable_path(void) {
-  return [[[NSBundle mainBundle] executablePath] UTF8String];
+  return safe_strdup([[[NSBundle mainBundle] executablePath] UTF8String]);
 }
 
 const char* default_image_path(void) {
   NSBundle* bundle = [NSBundle mainBundle];
   NSString* path = [bundle bundlePath];
-  NSString* executable = [[bundle executablePath] lastPathComponent];
+  NSString* executablePath = [[bundle executablePath] stringByResolvingSymlinksInPath];
+  NSString* executable = [executablePath lastPathComponent];
   NSString* image = [executable stringByAppendingString:@".image"];
 
   NSString* returnVal;
 
   if ([path hasSuffix:@".app"] || [path hasSuffix:@".app/"]) {
     NSFileManager* mgr = [NSFileManager defaultManager];
+    NSString* root = [path stringByDeletingLastPathComponent];
+    NSString* resources = [path stringByAppendingPathComponent:@"Contents/Resources"];
 
-    NSString* imageInBundle =
-        [[path stringByAppendingPathComponent:@"Contents/Resources"]
-            stringByAppendingPathComponent:image];
-    NSString* imageAlongBundle = [[path stringByDeletingLastPathComponent]
-        stringByAppendingPathComponent:image];
+    NSString* imageInBundle = [resources stringByAppendingPathComponent:image];
+    NSString* imageAlongBundle = [root stringByAppendingPathComponent:image];
 
     returnVal = ([mgr fileExistsAtPath:imageInBundle] ? imageInBundle
                                                       : imageAlongBundle);
-  } else
+  } else if ([executablePath hasSuffix:@".app/Contents/MacOS/factor"]) {
+    returnVal = executablePath;
+    returnVal = [returnVal stringByDeletingLastPathComponent];
+    returnVal = [returnVal stringByDeletingLastPathComponent];
+    returnVal = [returnVal stringByDeletingLastPathComponent];
+    returnVal = [returnVal stringByDeletingLastPathComponent];
+    returnVal = [returnVal stringByAppendingPathComponent:image];
+
+  } else {
     returnVal = [path stringByAppendingPathComponent:image];
+  }
 
   return [returnVal UTF8String];
 }
@@ -56,7 +68,7 @@ void factor_vm::init_signals(void) {
   mach_initialize();
 }
 
-/* Amateurs at Apple: implement this function, properly! */
+// Amateurs at Apple: implement this function, properly!
 Protocol* objc_getProtocol(char* name) {
   if (strcmp(name, "NSTextInput") == 0)
     return @protocol(NSTextInput);