]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/utilsexc.mm
Make it possible to tell wxXmlResource which domain to pull
[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
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
44class wxPipeInputStream : public wxInputStream
45{
46public:
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
58protected:
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
71class wxPipeOutputStream : public wxOutputStream
72{
73public:
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
85protected:
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
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
138
de6185e2
WS
139long wxExecute(const wxString& command,
140 int sync,
141 wxProcess *handle)
dcb68102
RN
142{
143 NSTask* theTask = [[NSTask alloc] init];
de6185e2 144
dcb68102
RN
145 if (handle && handle->IsRedirected())
146 {
147 NSPipe* theStdinPipe = [[NSPipe alloc] init];
148 NSPipe* theStderrPipe = [[NSPipe alloc] init];
149 NSPipe* theStdoutPipe = [[NSPipe alloc] init];
de6185e2 150
dcb68102
RN
151 [theTask setStandardInput:theStdinPipe];
152 [theTask setStandardError:theStderrPipe];
153 [theTask setStandardOutput:theStdoutPipe];
de6185e2 154
dcb68102
RN
155 handle->SetPipeStreams(new wxPipeInputStream(theStdoutPipe),
156 new wxPipeOutputStream(theStdinPipe),
157 new wxPipeInputStream(theStderrPipe) );
158 }
de6185e2
WS
159
160 NSArray* theQuoteArguments =
dcb68102 161 [wxNSStringWithWxString(command) componentsSeparatedByString:@"\""];
de6185e2
WS
162
163 NSMutableArray* theSeparatedArguments =
dcb68102 164 [NSMutableArray arrayWithCapacity:10];
de6185e2 165
dcb68102
RN
166 for (unsigned i = 0; i < [theQuoteArguments count]; ++i)
167 {
3103e8a9 168 [theSeparatedArguments addObjectsFromArray:
dcb68102
RN
169 [[theQuoteArguments objectAtIndex:i] componentsSeparatedByString:@" "]
170 ];
de6185e2 171
dcb68102 172 if(++i < [theQuoteArguments count])
3103e8a9 173 [theSeparatedArguments addObject:[theQuoteArguments objectAtIndex:i]];
dcb68102 174 }
de6185e2 175
3103e8a9
JS
176 [theTask setLaunchPath:[theSeparatedArguments objectAtIndex:0]];
177 [theTask setArguments:theSeparatedArguments];
dcb68102 178 [theTask launch];
de6185e2 179
dcb68102
RN
180 if(sync & wxEXEC_ASYNC)
181 {
de6185e2 182 [[wxTaskHandler alloc]init:handle
dcb68102 183 processIdentifier:[theTask processIdentifier]];
de6185e2 184
dcb68102
RN
185 return 0;
186 }
187 else
188 {
189 [theTask waitUntilExit];
de6185e2 190
dcb68102 191 return [theTask terminationStatus];
de6185e2 192 }
b9562622 193}
5d553c56 194#endif //0