]> git.saurik.com Git - wxWidgets.git/blame - src/mac/carbon/utilsexc.cpp
making returns in a line to always be \n (13 in CFM (mpwnewline) and 10 in Mach)
[wxWidgets.git] / src / mac / carbon / utilsexc.cpp
CommitLineData
e9576ca5
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: utilsexec.cpp
3// Purpose: Execution-related utilities
a31a5f85 4// Author: Stefan Csomor
c8023fed 5// Modified by: David Elliott
a31a5f85 6// Created: 1998-01-01
e9576ca5 7// RCS-ID: $Id$
a31a5f85 8// Copyright: (c) Stefan Csomor
65571936 9// Licence: wxWindows licence
e9576ca5
SC
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
90771fe5 13//#pragma implementation
e9576ca5
SC
14#endif
15
c8023fed 16#include "wx/log.h"
e9576ca5 17#include "wx/utils.h"
f5c6eb5c 18#ifdef __DARWIN__
5fde6fcc 19#include "wx/unix/execute.h"
c8023fed
DE
20#include <unistd.h>
21#include <sys/wait.h>
90771fe5 22extern "C" {
c8023fed 23#include <mach/mach.h>
90771fe5 24}
c8023fed 25#include <CoreFoundation/CFMachPort.h>
5fde6fcc 26#endif
e9576ca5
SC
27
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
f5c6eb5c 32#ifndef __DARWIN__
e9576ca5 33
fadb227e 34#include "wx/mac/private.h"
a2b77260 35#include "LaunchServices.h"
fadb227e
DS
36
37long wxExecute(const wxString& command, int flags, wxProcess *WXUNUSED(handler))
e9576ca5 38{
fadb227e
DS
39 wxASSERT_MSG( flags == wxEXEC_ASYNC,
40 wxT("wxExecute: Only wxEXEC_ASYNC is supported") );
41
a2b77260
SC
42 FSRef fsRef ;
43 OSErr err = noErr ;
44 err = wxMacPathToFSRef( command , &fsRef ) ;
45 if ( noErr == err )
46 {
47 err = LSOpenFSRef( &fsRef , NULL ) ;
48 }
fadb227e
DS
49
50 // 0 means execution failed. Returning non-zero is a PID, but not
51 // on Mac where PIDs are 64 bits and won't fit in a long, so we
52 // return a dummy value for now.
a2b77260 53 return ( err == noErr ) ? -1 : 0;
e9576ca5 54}
fadb227e 55
5fde6fcc
GD
56#endif
57
f5c6eb5c 58#ifdef __DARWIN__
c8023fed 59void wxMAC_MachPortEndProcessDetect(CFMachPortRef port, void *data)
5fde6fcc 60{
c8023fed 61 wxEndProcessData *proc_data = (wxEndProcessData*)data;
5f3f0f17 62 wxLogDebug(wxT("Wow.. this actually worked!"));
c8023fed
DE
63 int status = 0;
64 int rc = waitpid(abs(proc_data->pid), &status, WNOHANG);
65 if(!rc)
66 {
5f3f0f17 67 wxLogDebug(wxT("Mach port was invalidated, but process hasn't terminated!"));
c8023fed
DE
68 return;
69 }
70 if((rc != -1) && WIFEXITED(status))
71 proc_data->exitcode = WEXITSTATUS(status);
72 else
73 proc_data->exitcode = -1;
74 wxHandleProcessTermination(proc_data);
75}
76
77int wxAddProcessCallbackForPid(wxEndProcessData *proc_data, int pid)
78{
79 if(pid < 1)
80 return -1;
81 kern_return_t kernResult;
82 mach_port_t taskOfOurProcess;
83 mach_port_t machPortForProcess;
84 taskOfOurProcess = mach_task_self();
85 if(taskOfOurProcess == MACH_PORT_NULL)
86 {
5f3f0f17 87 wxLogDebug(wxT("No mach_task_self()"));
c8023fed
DE
88 return -1;
89 }
5f3f0f17 90 wxLogDebug(wxT("pid=%d"),pid);
c8023fed
DE
91 kernResult = task_for_pid(taskOfOurProcess,pid, &machPortForProcess);
92 if(kernResult != KERN_SUCCESS)
93 {
c32a538a 94 wxLogDebug(wxT("no task_for_pid()"));
c8023fed
DE
95 // try seeing if it is already dead or something
96 // FIXME: a better method would be to call the callback function
97 // from idle time until the process terminates. Of course, how
98 // likely is it that it will take more than 0.1 seconds for the
99 // mach terminate event to make its way to the BSD subsystem?
100 usleep(100); // sleep for 0.1 seconds
101 wxMAC_MachPortEndProcessDetect(NULL, (void*)proc_data);
102 return -1;
103 }
104 CFMachPortContext termcb_contextinfo;
105 termcb_contextinfo.version = NULL;
106 termcb_contextinfo.info = (void*)proc_data;
107 termcb_contextinfo.retain = NULL;
108 termcb_contextinfo.release = NULL;
109 termcb_contextinfo.copyDescription = NULL;
110 CFMachPortRef CFMachPortForProcess;
111 Boolean ShouldFreePort;
112 CFMachPortForProcess = CFMachPortCreateWithPort(NULL, machPortForProcess, NULL, &termcb_contextinfo, &ShouldFreePort);
113 if(!CFMachPortForProcess)
114 {
5f3f0f17 115 wxLogDebug(wxT("No CFMachPortForProcess"));
c8023fed
DE
116 mach_port_deallocate(taskOfOurProcess, machPortForProcess);
117 return -1;
118 }
119 if(ShouldFreePort)
120 {
121 kernResult = mach_port_deallocate(taskOfOurProcess, machPortForProcess);
122 if(kernResult!=KERN_SUCCESS)
123 {
5f3f0f17 124 wxLogDebug(wxT("Couldn't deallocate mach port"));
c8023fed
DE
125 return -1;
126 }
127 }
128 CFMachPortSetInvalidationCallBack(CFMachPortForProcess, &wxMAC_MachPortEndProcessDetect);
129 CFRunLoopSourceRef runloopsource;
130 runloopsource = CFMachPortCreateRunLoopSource(NULL,CFMachPortForProcess, (CFIndex)0);
131 if(!runloopsource)
132 {
5f3f0f17 133 wxLogDebug(wxT("Couldn't create runloopsource"));
c8023fed
DE
134 return -1;
135 }
136
137 CFRelease(CFMachPortForProcess);
138
139 CFRunLoopAddSource(CFRunLoopGetCurrent(),runloopsource,kCFRunLoopDefaultMode);
140 CFRelease(runloopsource);
5f3f0f17 141 wxLogDebug(wxT("Successfully added notification to the runloop"));
c8023fed 142 return 0;
5fde6fcc 143}
f5c6eb5c 144#endif