]> git.saurik.com Git - wxWidgets.git/blame - src/cocoa/utilsexc.mm
Fit the generic preferences editor dialog to its pages size initially.
[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,
164db92c
VZ
145 wxProcess *handle,
146 const wxExecuteEnv *env)
dcb68102
RN
147{
148 NSTask* theTask = [[NSTask alloc] init];
de6185e2 149
dcb68102
RN
150 if (handle && handle->IsRedirected())
151 {
152 NSPipe* theStdinPipe = [[NSPipe alloc] init];
153 NSPipe* theStderrPipe = [[NSPipe alloc] init];
154 NSPipe* theStdoutPipe = [[NSPipe alloc] init];
de6185e2 155
dcb68102
RN
156 [theTask setStandardInput:theStdinPipe];
157 [theTask setStandardError:theStderrPipe];
158 [theTask setStandardOutput:theStdoutPipe];
de6185e2 159
dcb68102
RN
160 handle->SetPipeStreams(new wxPipeInputStream(theStdoutPipe),
161 new wxPipeOutputStream(theStdinPipe),
162 new wxPipeInputStream(theStderrPipe) );
163 }
de6185e2
WS
164
165 NSArray* theQuoteArguments =
dcb68102 166 [wxNSStringWithWxString(command) componentsSeparatedByString:@"\""];
de6185e2
WS
167
168 NSMutableArray* theSeparatedArguments =
dcb68102 169 [NSMutableArray arrayWithCapacity:10];
de6185e2 170
dcb68102
RN
171 for (unsigned i = 0; i < [theQuoteArguments count]; ++i)
172 {
3103e8a9 173 [theSeparatedArguments addObjectsFromArray:
dcb68102
RN
174 [[theQuoteArguments objectAtIndex:i] componentsSeparatedByString:@" "]
175 ];
de6185e2 176
dcb68102 177 if(++i < [theQuoteArguments count])
3103e8a9 178 [theSeparatedArguments addObject:[theQuoteArguments objectAtIndex:i]];
dcb68102 179 }
de6185e2 180
3103e8a9
JS
181 [theTask setLaunchPath:[theSeparatedArguments objectAtIndex:0]];
182 [theTask setArguments:theSeparatedArguments];
dcb68102 183 [theTask launch];
de6185e2 184
dcb68102
RN
185 if(sync & wxEXEC_ASYNC)
186 {
e7e1ad7d 187 [[WX_GET_OBJC_CLASS(wxTaskHandler) alloc]init:handle
dcb68102 188 processIdentifier:[theTask processIdentifier]];
de6185e2 189
dcb68102
RN
190 return 0;
191 }
192 else
193 {
194 [theTask waitUntilExit];
de6185e2 195
dcb68102 196 return [theTask terminationStatus];
de6185e2 197 }
b9562622 198}
5d553c56 199#endif //0