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"
17 #include "wx/unix/execute.h"
23 #pragma implementation
28 #include "wx/process.h"
29 #include "wx/stream.h"
31 #include "wx/cocoa/string.h"
33 #import <Foundation/Foundation.h>
34 #import <AppKit/NSWorkspace.h>
37 // RN: This is a prelimenary implementation - simple
38 // launching and process redirection works,
39 // but with the piping tests in the exec sample
40 // SIGPIPE is triggered...
43 class wxPipeInputStream : public wxInputStream
46 wxPipeInputStream(NSPipe* thePipe) :
48 m_theHandle([m_thePipe fileHandleForReading])
58 virtual size_t OnSysRead(void *buffer, size_t size)
60 NSData* theData = [m_theHandle readDataOfLength:size];
61 memcpy(buffer, [theData bytes], [theData length]);
62 return [theData length];
67 NSFileHandle* m_theHandle;
70 class wxPipeOutputStream : public wxOutputStream
73 wxPipeOutputStream(NSPipe* thePipe) :
75 m_theHandle([m_thePipe fileHandleForWriting])
86 virtual size_t OnSysWrite(const void *buffer, size_t bufsize)
88 NSData* theData = [NSData dataWithBytesNoCopy:(void*)buffer
90 [m_theHandle writeData:theData];
95 NSFileHandle* m_theHandle;
98 @interface wxTaskHandler : NSObject
103 -(id)init:(void*)handle processIdentifier:(long)pid;
104 - (void)termHandler:(NSNotification *)aNotification;
107 @implementation wxTaskHandler : NSObject
109 -(id)init:(void*)handle processIdentifier:(long)pid
116 [[NSNotificationCenter defaultCenter] addObserver:self
117 selector:@selector(termHandler:)
118 name:NSTaskDidTerminateNotification
123 - (void)termHandler:(NSNotification *)aNotification
125 NSTask* theTask = [aNotification object];
127 if ([theTask processIdentifier] == m_pid)
129 ((wxProcess*)m_handle)->OnTerminate([theTask processIdentifier],
130 [theTask terminationStatus]);
138 long wxExecute(const wxString& command,
142 NSTask* theTask = [[NSTask alloc] init];
144 if (handle && handle->IsRedirected())
146 NSPipe* theStdinPipe = [[NSPipe alloc] init];
147 NSPipe* theStderrPipe = [[NSPipe alloc] init];
148 NSPipe* theStdoutPipe = [[NSPipe alloc] init];
150 [theTask setStandardInput:theStdinPipe];
151 [theTask setStandardError:theStderrPipe];
152 [theTask setStandardOutput:theStdoutPipe];
154 handle->SetPipeStreams(new wxPipeInputStream(theStdoutPipe),
155 new wxPipeOutputStream(theStdinPipe),
156 new wxPipeInputStream(theStderrPipe) );
159 NSArray* theQuoteArguments =
160 [wxNSStringWithWxString(command) componentsSeparatedByString:@"\""];
162 NSMutableArray* theSeparatedArguments =
163 [NSMutableArray arrayWithCapacity:10];
165 for (unsigned i = 0; i < [theQuoteArguments count]; ++i)
167 [theSeparatedArguments addObjectsFromArray:
168 [[theQuoteArguments objectAtIndex:i] componentsSeparatedByString:@" "]
171 if(++i < [theQuoteArguments count])
172 [theSeparatedArguments addObject:[theQuoteArguments objectAtIndex:i]];
175 [theTask setLaunchPath:[theSeparatedArguments objectAtIndex:0]];
176 [theTask setArguments:theSeparatedArguments];
179 if(sync & wxEXEC_ASYNC)
181 [[wxTaskHandler alloc]init:handle
182 processIdentifier:[theTask processIdentifier]];
188 [theTask waitUntilExit];
190 return [theTask terminationStatus];