1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/cocoa/utilsexec.mm
3 // Purpose: Execution-related utilities for wxCocoa
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 // Notes: This code may be useful on platforms other than Darwin.
11 // On Darwin we share the CoreFoundation code with wxMac.
12 /////////////////////////////////////////////////////////////////////////////
14 #include "wx/wxprec.h"
24 #include "wx/stream.h"
25 #endif // wxUSE_STREAMS
28 #include "wx/process.h"
30 #include "wx/cocoa/string.h"
32 #include "wx/cocoa/objc/objc_uniquifying.h"
34 #import <Foundation/Foundation.h>
35 #import <AppKit/NSWorkspace.h>
38 // RN: This is a prelimenary implementation - simple
39 // launching and process redirection works,
40 // but with the piping tests in the exec sample
41 // SIGPIPE is triggered...
44 class wxPipeInputStream : public wxInputStream
47 wxPipeInputStream(NSPipe* thePipe) :
49 m_theHandle([m_thePipe fileHandleForReading])
59 virtual size_t OnSysRead(void *buffer, size_t size)
61 NSData* theData = [m_theHandle readDataOfLength:size];
62 memcpy(buffer, [theData bytes], [theData length]);
63 return [theData length];
68 NSFileHandle* m_theHandle;
71 class wxPipeOutputStream : public wxOutputStream
74 wxPipeOutputStream(NSPipe* thePipe) :
76 m_theHandle([m_thePipe fileHandleForWriting])
87 virtual size_t OnSysWrite(const void *buffer, size_t bufsize)
89 NSData* theData = [NSData dataWithBytesNoCopy:(void*)buffer
91 [m_theHandle writeData:theData];
96 NSFileHandle* m_theHandle;
99 @interface wxTaskHandler : NSObject
104 -(id)init:(void*)handle processIdentifier:(long)pid;
105 - (void)termHandler:(NSNotification *)aNotification;
107 WX_DECLARE_GET_OBJC_CLASS(wxTaskHandler,NSObject)
109 @implementation wxTaskHandler : NSObject
111 -(id)init:(void*)handle processIdentifier:(long)pid
118 [[NSNotificationCenter defaultCenter] addObserver:self
119 selector:@selector(termHandler:)
120 name:NSTaskDidTerminateNotification
125 - (void)termHandler:(NSNotification *)aNotification
127 NSTask* theTask = [aNotification object];
129 if ([theTask processIdentifier] == m_pid)
131 ((wxProcess*)m_handle)->OnTerminate([theTask processIdentifier],
132 [theTask terminationStatus]);
139 WX_IMPLEMENT_GET_OBJC_CLASS(wxTaskHandler,NSObject)
141 long wxExecute(const wxString& command,
144 const wxExecuteEnv *env)
146 NSTask* theTask = [[NSTask alloc] init];
148 if (handle && handle->IsRedirected())
150 NSPipe* theStdinPipe = [[NSPipe alloc] init];
151 NSPipe* theStderrPipe = [[NSPipe alloc] init];
152 NSPipe* theStdoutPipe = [[NSPipe alloc] init];
154 [theTask setStandardInput:theStdinPipe];
155 [theTask setStandardError:theStderrPipe];
156 [theTask setStandardOutput:theStdoutPipe];
158 handle->SetPipeStreams(new wxPipeInputStream(theStdoutPipe),
159 new wxPipeOutputStream(theStdinPipe),
160 new wxPipeInputStream(theStderrPipe) );
163 NSArray* theQuoteArguments =
164 [wxNSStringWithWxString(command) componentsSeparatedByString:@"\""];
166 NSMutableArray* theSeparatedArguments =
167 [NSMutableArray arrayWithCapacity:10];
169 for (unsigned i = 0; i < [theQuoteArguments count]; ++i)
171 [theSeparatedArguments addObjectsFromArray:
172 [[theQuoteArguments objectAtIndex:i] componentsSeparatedByString:@" "]
175 if(++i < [theQuoteArguments count])
176 [theSeparatedArguments addObject:[theQuoteArguments objectAtIndex:i]];
179 [theTask setLaunchPath:[theSeparatedArguments objectAtIndex:0]];
180 [theTask setArguments:theSeparatedArguments];
183 if(sync & wxEXEC_ASYNC)
185 [[WX_GET_OBJC_CLASS(wxTaskHandler) alloc]init:handle
186 processIdentifier:[theTask processIdentifier]];
192 [theTask waitUntilExit];
194 return [theTask terminationStatus];