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