changeset 2350:5ddc447fb367

Mac: Partial rewrite of dw_exect(). Implement DW_EXEC_CON with Terminal.app. Previously we had been using xterm, which hasn't been included MacOS since 10.7 (Lion). Also, rewrite the DW_EXEC_GUI to allow more than one file to open.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 04 Mar 2021 16:33:55 +0000
parents 123e22827a82
children 0c8d769df97e
files mac/dw.m
diffstat 1 files changed, 69 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/mac/dw.m	Thu Mar 04 12:32:56 2021 +0000
+++ b/mac/dw.m	Thu Mar 04 16:33:55 2021 +0000
@@ -12756,18 +12756,25 @@
 
         if(params && params[0] && params[1])
         {
-            NSString *file = [NSString stringWithUTF8String:params[1]];
-            
 #ifdef BUILDING_FOR_CATALINA
             if(@available(macOS 10.15, *))
             {
                 NSURL *url = _dw_url_from_program(nsprogram, ws);
-                NSURL *nsfile = [NSURL fileURLWithPath:file];
-                
-                [ws openURLs:[NSArray arrayWithObjects:nsfile, nil]
-                                    withApplicationAtURL:url
-                                           configuration:[NSWorkspaceOpenConfiguration configuration]
-                                       completionHandler:^(NSRunningApplication *app, NSError *error) {
+                NSMutableArray *array = [[NSMutableArray alloc] init];
+                int z = 1;
+
+                while(params[z])
+                {
+                    NSString *thisfile = [NSString stringWithUTF8String:params[z]];
+                    NSURL *nsfile = [NSURL fileURLWithPath:thisfile];
+
+                    [array addObject:nsfile];
+                    z++;
+                }
+
+                [ws openURLs:array withApplicationAtURL:url
+                                          configuration:[NSWorkspaceOpenConfiguration configuration]
+                                      completionHandler:^(NSRunningApplication *app, NSError *error) {
                     if(error)
                         NSLog(@"openURLs: %@", [error localizedDescription]);
                 }];
@@ -12779,8 +12786,16 @@
 
                 if([ws respondsToSelector:sofwa])
                 {
-                    DWIMP iofwa = (DWIMP)[ws methodForSelector:sofwa];
-                    iofwa(ws, sofwa, file, nsprogram);
+                    int z = 1;
+
+                    while(params[z])
+                    {
+                        NSString *file = [NSString stringWithUTF8String:params[z]];
+                        DWIMP iofwa = (DWIMP)[ws methodForSelector:sofwa];
+
+                        iofwa(ws, sofwa, file, nsprogram);
+                        z++;
+                    }
                 }
             }
         }
@@ -12810,50 +12825,50 @@
                 }
             }
         }
-        return DW_ERROR_NONE;
-    }
-
-    if((ret = fork()) == 0)
-    {
-        int i;
-
-        for (i = 3; i < 256; i++)
-            close(i);
-        setsid();
-
-        if(type == DW_EXEC_CON)
-        {
-            char **tmpargs;
-
-            if(!params)
-            {
-                tmpargs = malloc(sizeof(char *));
-                tmpargs[0] = NULL;
-            }
-            else
-            {
-                int z = 0;
-
-                while(params[z])
-                {
-                    z++;
-                }
-                tmpargs = malloc(sizeof(char *)*(z+3));
-                z=0;
-                tmpargs[0] = "xterm";
-                tmpargs[1] = "-e";
-                while(params[z])
-                {
-                    tmpargs[z+2] = params[z];
-                    z++;
-                }
-                tmpargs[z+2] = NULL;
-            }
-            execvp("xterm", tmpargs);
-            free(tmpargs);
-        }
-        /* If we got here exec failed */
-        _exit(-1);
+        ret = DW_ERROR_NONE;
+    }
+    else
+    {
+        /* Use AppleScript to Open Terminal.app or contact an existing copy,
+         * and execute the command passed in params[].
+         */
+        char *commandline;
+        char *format = "osascript -e \'tell app \"Terminal\" to do script \"";
+        int len = (int)strlen(format) + 3;
+
+        /* Generate a command line from the parameters */
+        if(params && *params)
+        {
+            int z = 0;
+
+            while(params[z])
+            {
+                len+=strlen(params[z]) + 1;
+                z++;
+            }
+            z=1;
+            commandline = calloc(1, len);
+            strcpy(commandline, format);
+            strcat(commandline, params[0]);
+            while(params[z])
+            {
+                strcat(commandline, " ");
+                strcat(commandline, params[z]);
+                z++;
+            }
+        }
+        else
+        {
+            len += strlen(program);
+            commandline = calloc(1, len);
+            strcpy(commandline, format);
+            strcat(commandline, program);
+        }
+        strcat(commandline, "\"\'");
+
+        /* Attempt to execute the commmand, DW_ERROR_NONE on success */
+        if(system(commandline) != -1)
+            ret = DW_ERROR_NONE;
     }
     return ret;
 }