]>
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 | |
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 | #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 | ||
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: | |
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, | |
b9562622 RN |
139 | int sync, |
140 | wxProcess *handle) | |
dcb68102 RN |
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 | ||
3103e8a9 | 162 | NSMutableArray* theSeparatedArguments = |
dcb68102 RN |
163 | [NSMutableArray arrayWithCapacity:10]; |
164 | ||
165 | for (unsigned i = 0; i < [theQuoteArguments count]; ++i) | |
166 | { | |
3103e8a9 | 167 | [theSeparatedArguments addObjectsFromArray: |
dcb68102 RN |
168 | [[theQuoteArguments objectAtIndex:i] componentsSeparatedByString:@" "] |
169 | ]; | |
170 | ||
171 | if(++i < [theQuoteArguments count]) | |
3103e8a9 | 172 | [theSeparatedArguments addObject:[theQuoteArguments objectAtIndex:i]]; |
dcb68102 RN |
173 | } |
174 | ||
3103e8a9 JS |
175 | [theTask setLaunchPath:[theSeparatedArguments objectAtIndex:0]]; |
176 | [theTask setArguments:theSeparatedArguments]; | |
dcb68102 RN |
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 | } | |
b9562622 | 192 | } |
5d553c56 | 193 | #endif //0 |
b9562622 | 194 |