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