[geany/geany-osx] 9380c7: Launch Geany using exec() in the binary launcher

Jiří Techet git-noreply at xxxxx
Tue Oct 15 16:47:09 UTC 2019


Branch:      refs/heads/master
Author:      Jiří Techet <techet at gmail.com>
Committer:   Jiří Techet <techet at gmail.com>
Date:        Tue, 15 Oct 2019 16:47:09 UTC
Commit:      9380c7d7d800f92be983e3f3ab3820763edcf78a
             https://github.com/geany/geany-osx/commit/9380c7d7d800f92be983e3f3ab3820763edcf78a

Log Message:
-----------
Launch Geany using exec() in the binary launcher

The previous method lauched Geany using NSTask so the result was 2
processes - one for launcher and one for Geany. For MacOS the main
process was the launcher and when some events were sent to it,
it just hanged because the launcher was waiting for Geany's process
to terminate.

By using exec() we have just one process and events are sent correctly
to Geany.


Modified Paths:
--------------
    LauncherGtk3/geany/geany/main.m

Modified: LauncherGtk3/geany/geany/main.m
88 lines changed, 53 insertions(+), 35 deletions(-)
===================================================================
@@ -1,40 +1,34 @@
 #import <Foundation/Foundation.h>
 
+#define MAX_ARR_SIZE 100
 
-NSString *get_locale(NSString *bundle_data)
-{
+static NSString *get_locale(NSString *bundle_data) {
     NSString *fallback = @"en_US.UTF-8";
     
     BOOL ignore_locale = [[NSFileManager defaultManager] fileExistsAtPath: [@"~/.config/geany/ignore_locale" stringByExpandingTildeInPath]];
-    if (ignore_locale)
-    {
+    if (ignore_locale) {
         return fallback;
     }
     
     NSArray<NSString *> *langs = [NSLocale preferredLanguages];
-    for (NSString *lng in langs)
-    {
+    for (NSString *lng in langs) {
         BOOL found = NO;
         NSString *lang;
         NSArray<NSString *> *comps = [lng componentsSeparatedByString:@"-"];
-        if (comps.count > 1)
-        {
+        if (comps.count > 1) {
             lang = [NSString stringWithFormat:@"%@_%@", comps[0], comps[1]];
             NSString *path = [NSString stringWithFormat:@"%@/locale/%@", bundle_data, lang];
             found = [[NSFileManager defaultManager] fileExistsAtPath:path];
         }
-        if (!found && comps.count > 0)
-        {
+        if (!found && comps.count > 0) {
             NSString *lng = comps[0];
             NSString *path = [NSString stringWithFormat:@"%@/locale/%@", bundle_data, lng];
             found = [[NSFileManager defaultManager] fileExistsAtPath:path];
-            if (found && comps.count == 1)
-            {
+            if (found && comps.count == 1) {
                 lang = lng;
             }
         }
-        if (found)
-        {
+        if (found) {
             return [lang stringByAppendingString:@".UTF-8"];
         }
     }
@@ -43,8 +37,34 @@
 }
 
 
-int run_geany()
-{
+static void fill_env_array(const char *arr[], NSDictionary<NSString *, NSString *> *dict) {
+    int i = 0;
+    for (NSString *key in dict) {
+        NSString *var = [NSString stringWithFormat:@"%@=%@", key, dict[key]];
+        arr[i] = [var UTF8String];
+        i++;
+        if (i == MAX_ARR_SIZE - 1) {
+            break;
+        }
+    }
+    arr[i] = NULL;
+}
+
+
+static void fill_argv_array(const char *arr[], NSArray<NSString *> *array) {
+    int i = 0;
+    for (NSString *value in array) {
+        arr[i] = [value UTF8String];
+        i++;
+        if (i == MAX_ARR_SIZE - 1) {
+            break;
+        }
+    }
+    arr[i] = NULL;
+}
+
+
+static void run_geany() {
     NSString *bundle_dir = [[NSBundle mainBundle] bundlePath];
     
     NSString *bundle_contents = [bundle_dir stringByAppendingPathComponent: @"Contents"];
@@ -86,40 +106,38 @@ int run_geany()
     NSArray<NSString *> *argv = NSProcessInfo.processInfo.arguments;
     NSString *binary = [argv[0] stringByAppendingString:@"-bin"];
     NSMutableArray<NSString *> *args = [argv mutableCopy];
-    [args removeObjectAtIndex: 0];
     
-    if (args.count > 0 && [args[0] hasPrefix:@"-psn_"])
-    {
-        [args removeObjectAtIndex: 0];
+    if (args.count > 1 && [args[1] hasPrefix:@"-psn_"]) {
+        [args removeObjectAtIndex: 1];
     }
 
     [args addObject:[NSString stringWithFormat:@"--vte-lib=%@/libvte-2.91.0.dylib", bundle_lib]];
     
     //debugging
     NSString *verbose_param = @"--osx-verbose";
-    if ([args containsObject: verbose_param])
-    {
+    if ([args containsObject: verbose_param]) {
         [args removeObject:verbose_param];
         NSLog(@"env: %@", env);
-        NSLog(@"args: %@", args);
-        NSLog(@"binary: %@", binary);
+        NSLog(@"argv: %@", args);
+        NSLog(@"executable: %@", binary);
     }
     
-    //run Geany binary
-    NSTask *task = [[NSTask alloc] init];
-    task.launchPath = binary;
-    task.environment = env;
-    task.arguments = args;
-    [task launch];
-    [task waitUntilExit];
-
-    return task.terminationStatus;
+    const char *envp[MAX_ARR_SIZE];
+    fill_env_array(envp, env);
+
+    const char *argp[MAX_ARR_SIZE];
+    fill_argv_array(argp, args);
+
+    execve([binary UTF8String], (char * const *)argp, (char * const *)envp);
+    
+    //should never reach this
+    NSLog(@"execve() failed");
 }
 
 
 int main(int argc, const char * argv[]) {
     @autoreleasepool {
-        return run_geany();
+        run_geany();
     }
-    return 0;
+    return 1;
 }



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list