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