]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/cocoa/utilsexc.mm
WinCE build fixes.
[wxWidgets.git] / src / cocoa / utilsexc.mm
... / ...
CommitLineData
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/cocoa/utilsexec.mm
3// Purpose: Execution-related utilities for wxCocoa
4// Author: Ryan Norton
5// Modified by:
6// Created: 2004-10-05
7// RCS-ID: $Id$
8// Copyright: (c) Ryan Norton
9// Licence: wxWindows licence
10// Notes: This code may be useful on platforms other than Darwin.
11// On Darwin we share the CoreFoundation code with wxMac.
12/////////////////////////////////////////////////////////////////////////////
13
14#include "wx/wxprec.h"
15#ifndef WX_PRECOMP
16#endif
17#include "wx/unix/execute.h"
18#include "wx/utils.h"
19
20#if 0
21
22#include "wx/utils.h"
23
24#include "wx/process.h"
25#include "wx/stream.h"
26
27#include "wx/cocoa/string.h"
28
29#import <Foundation/Foundation.h>
30#import <AppKit/NSWorkspace.h>
31
32//
33// RN: This is a prelimenary implementation - simple
34// launching and process redirection works,
35// but with the piping tests in the exec sample
36// SIGPIPE is triggered...
37//
38
39class wxPipeInputStream : public wxInputStream
40{
41public:
42 wxPipeInputStream(NSPipe* thePipe) :
43 m_thePipe(thePipe),
44 m_theHandle([m_thePipe fileHandleForReading])
45 {
46 }
47
48 ~wxPipeInputStream()
49 {
50 [m_thePipe release];
51 }
52
53protected:
54 virtual size_t OnSysRead(void *buffer, size_t size)
55 {
56 NSData* theData = [m_theHandle readDataOfLength:size];
57 memcpy(buffer, [theData bytes], [theData length]);
58 return [theData length];
59 }
60
61
62 NSPipe* m_thePipe;
63 NSFileHandle* m_theHandle;
64};
65
66class wxPipeOutputStream : public wxOutputStream
67{
68public:
69 wxPipeOutputStream(NSPipe* thePipe) :
70 m_thePipe(thePipe),
71 m_theHandle([m_thePipe fileHandleForWriting])
72 {
73 }
74
75 ~wxPipeOutputStream()
76 {
77 [m_thePipe release];
78 }
79
80protected:
81
82 virtual size_t OnSysWrite(const void *buffer, size_t bufsize)
83 {
84 NSData* theData = [NSData dataWithBytesNoCopy:(void*)buffer
85 length:bufsize];
86 [m_theHandle writeData:theData];
87 return bufsize;
88 }
89
90 NSPipe* m_thePipe;
91 NSFileHandle* m_theHandle;
92};
93
94@interface wxTaskHandler : NSObject
95{
96 long m_pid;
97 void* m_handle;
98}
99-(id)init:(void*)handle processIdentifier:(long)pid;
100- (void)termHandler:(NSNotification *)aNotification;
101@end
102
103@implementation wxTaskHandler : NSObject
104
105-(id)init:(void*)handle processIdentifier:(long)pid
106{
107 self = [super init];
108
109 m_handle = handle;
110 m_pid = pid;
111
112 [[NSNotificationCenter defaultCenter] addObserver:self
113 selector:@selector(termHandler:)
114 name:NSTaskDidTerminateNotification
115 object:nil];
116 return self;
117}
118
119- (void)termHandler:(NSNotification *)aNotification
120{
121 NSTask* theTask = [aNotification object];
122
123 if ([theTask processIdentifier] == m_pid)
124 {
125 ((wxProcess*)m_handle)->OnTerminate([theTask processIdentifier],
126 [theTask terminationStatus]);
127
128 [self release];
129 }
130}
131
132@end
133
134long wxExecute(const wxString& command,
135 int sync,
136 wxProcess *handle)
137{
138 NSTask* theTask = [[NSTask alloc] init];
139
140 if (handle && handle->IsRedirected())
141 {
142 NSPipe* theStdinPipe = [[NSPipe alloc] init];
143 NSPipe* theStderrPipe = [[NSPipe alloc] init];
144 NSPipe* theStdoutPipe = [[NSPipe alloc] init];
145
146 [theTask setStandardInput:theStdinPipe];
147 [theTask setStandardError:theStderrPipe];
148 [theTask setStandardOutput:theStdoutPipe];
149
150 handle->SetPipeStreams(new wxPipeInputStream(theStdoutPipe),
151 new wxPipeOutputStream(theStdinPipe),
152 new wxPipeInputStream(theStderrPipe) );
153 }
154
155 NSArray* theQuoteArguments =
156 [wxNSStringWithWxString(command) componentsSeparatedByString:@"\""];
157
158 NSMutableArray* theSeparatedArguments =
159 [NSMutableArray arrayWithCapacity:10];
160
161 for (unsigned i = 0; i < [theQuoteArguments count]; ++i)
162 {
163 [theSeparatedArguments addObjectsFromArray:
164 [[theQuoteArguments objectAtIndex:i] componentsSeparatedByString:@" "]
165 ];
166
167 if(++i < [theQuoteArguments count])
168 [theSeparatedArguments addObject:[theQuoteArguments objectAtIndex:i]];
169 }
170
171 [theTask setLaunchPath:[theSeparatedArguments objectAtIndex:0]];
172 [theTask setArguments:theSeparatedArguments];
173 [theTask launch];
174
175 if(sync & wxEXEC_ASYNC)
176 {
177 [[wxTaskHandler alloc]init:handle
178 processIdentifier:[theTask processIdentifier]];
179
180 return 0;
181 }
182 else
183 {
184 [theTask waitUntilExit];
185
186 return [theTask terminationStatus];
187 }
188}
189#endif //0
190