]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/utilsexc.mm
fix for bug 1371386, with some minor mods and cleanup
[wxWidgets.git] / src / cocoa / utilsexc.mm
CommitLineData
dcb68102 1/////////////////////////////////////////////////////////////////////////////
5d553c56 2// Name: src/cocoa/utilsexec.mm
dcb68102 3// Purpose: Execution-related utilities for wxCocoa
5d553c56 4// Author: Ryan Norton
dcb68102
RN
5// Modified by:
6// Created: 2004-10-05
7// RCS-ID: $Id$
8// Copyright: (c) Ryan Norton
9// Licence: wxWindows licence
5d553c56
DE
10// Notes: This code may be useful on platforms other than Darwin.
11// On Darwin we share the CoreFoundation code with wxMac.
dcb68102
RN
12/////////////////////////////////////////////////////////////////////////////
13
5d553c56
DE
14#include "wx/wxprec.h"
15#ifndef WX_PRECOMP
b9562622 16#endif
b9562622 17#include "wx/unix/execute.h"
571b0b13 18#include "wx/utils.h"
b9562622 19
5d553c56 20#if 0
b9562622 21
dcb68102
RN
22#include "wx/utils.h"
23
24#include "wx/process.h"
25#include "wx/stream.h"
26
27#include "wx/cocoa/string.h"
28
29#import <Foundation/Foundation.h>
30#import <AppKit/NSWorkspace.h>
31
b9562622
RN
32//
33// RN: This is a prelimenary implementation - simple
34// launching and process redirection works,
35// but with the piping tests in the exec sample
36// SIGPIPE is triggered...
37//
38
dcb68102
RN
39class wxPipeInputStream : public wxInputStream
40{
41public:
42 wxPipeInputStream(NSPipe* thePipe) :
43 m_thePipe(thePipe),
44 m_theHandle([m_thePipe fileHandleForReading])
45 {
46 }
47
48 ~wxPipeInputStream()
49 {
50 [m_thePipe release];
51 }
52
53protected:
54 virtual size_t OnSysRead(void *buffer, size_t size)
55 {
56 NSData* theData = [m_theHandle readDataOfLength:size];
57 memcpy(buffer, [theData bytes], [theData length]);
58 return [theData length];
59 }
60
61
62 NSPipe* m_thePipe;
63 NSFileHandle* m_theHandle;
64};
65
66class wxPipeOutputStream : public wxOutputStream
67{
68public:
69 wxPipeOutputStream(NSPipe* thePipe) :
70 m_thePipe(thePipe),
71 m_theHandle([m_thePipe fileHandleForWriting])
72 {
73 }
74
75 ~wxPipeOutputStream()
76 {
77 [m_thePipe release];
78 }
79
80protected:
81
82 virtual size_t OnSysWrite(const void *buffer, size_t bufsize)
83 {
84 NSData* theData = [NSData dataWithBytesNoCopy:(void*)buffer
85 length:bufsize];
86 [m_theHandle writeData:theData];
87 return bufsize;
88 }
89
90 NSPipe* m_thePipe;
91 NSFileHandle* m_theHandle;
92};
93
94@interface wxTaskHandler : NSObject
95{
96 long m_pid;
97 void* m_handle;
98}
99-(id)init:(void*)handle processIdentifier:(long)pid;
100- (void)termHandler:(NSNotification *)aNotification;
101@end
102
103@implementation wxTaskHandler : NSObject
104
105-(id)init:(void*)handle processIdentifier:(long)pid
106{
107 self = [super init];
108
109 m_handle = handle;
110 m_pid = pid;
111
112 [[NSNotificationCenter defaultCenter] addObserver:self
113 selector:@selector(termHandler:)
114 name:NSTaskDidTerminateNotification
115 object:nil];
116 return self;
117}
118
119- (void)termHandler:(NSNotification *)aNotification
120{
121 NSTask* theTask = [aNotification object];
122
123 if ([theTask processIdentifier] == m_pid)
124 {
125 ((wxProcess*)m_handle)->OnTerminate([theTask processIdentifier],
126 [theTask terminationStatus]);
127
128 [self release];
129 }
130}
131
132@end
133
134long wxExecute(const wxString& command,
b9562622
RN
135 int sync,
136 wxProcess *handle)
dcb68102
RN
137{
138 NSTask* theTask = [[NSTask alloc] init];
139
140 if (handle && handle->IsRedirected())
141 {
142 NSPipe* theStdinPipe = [[NSPipe alloc] init];
143 NSPipe* theStderrPipe = [[NSPipe alloc] init];
144 NSPipe* theStdoutPipe = [[NSPipe alloc] init];
145
146 [theTask setStandardInput:theStdinPipe];
147 [theTask setStandardError:theStderrPipe];
148 [theTask setStandardOutput:theStdoutPipe];
149
150 handle->SetPipeStreams(new wxPipeInputStream(theStdoutPipe),
151 new wxPipeOutputStream(theStdinPipe),
152 new wxPipeInputStream(theStderrPipe) );
153 }
154
155 NSArray* theQuoteArguments =
156 [wxNSStringWithWxString(command) componentsSeparatedByString:@"\""];
157
3103e8a9 158 NSMutableArray* theSeparatedArguments =
dcb68102
RN
159 [NSMutableArray arrayWithCapacity:10];
160
161 for (unsigned i = 0; i < [theQuoteArguments count]; ++i)
162 {
3103e8a9 163 [theSeparatedArguments addObjectsFromArray:
dcb68102
RN
164 [[theQuoteArguments objectAtIndex:i] componentsSeparatedByString:@" "]
165 ];
166
167 if(++i < [theQuoteArguments count])
3103e8a9 168 [theSeparatedArguments addObject:[theQuoteArguments objectAtIndex:i]];
dcb68102
RN
169 }
170
3103e8a9
JS
171 [theTask setLaunchPath:[theSeparatedArguments objectAtIndex:0]];
172 [theTask setArguments:theSeparatedArguments];
dcb68102
RN
173 [theTask launch];
174
175 if(sync & wxEXEC_ASYNC)
176 {
177 [[wxTaskHandler alloc]init:handle
178 processIdentifier:[theTask processIdentifier]];
179
180 return 0;
181 }
182 else
183 {
184 [theTask waitUntilExit];
185
186 return [theTask terminationStatus];
187 }
b9562622 188}
5d553c56 189#endif //0
b9562622 190