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