ObjFW  Check-in [e8b7d0dd0d]

Overview
Comment:Implement OFProcess for Win32.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e8b7d0dd0d9e3216ff3e1213c38eae198d9332e72f6dc066577b3843598bbc41
User & Date: js on 2011-12-01 02:16:59
Other Links: manifest | tags
Context
2011-12-01
11:29
Remove OF{Float,Double}{Vector,Matrix}.
Those fit better into an extra library.
check-in: feae803c21 user: js tags: trunk
02:16
Implement OFProcess for Win32. check-in: e8b7d0dd0d user: js tags: trunk
2011-11-30
23:55
Don't copy properties of OFApplication. check-in: 9ec315d070 user: js tags: trunk
Changes

Modified src/OFProcess.h from [de525c3e8f] to [95b4e89bc7].

10
11
12
13
14
15
16

17

18
19




20
21
22
23
24
25

26
27



28
29
30
31
32
33
34
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */


#include <sys/types.h>


#import "OFStream.h"





/**
 * \brief A class for stream-like communication with a newly created process.
 */
@interface OFProcess: OFStream
{

	pid_t pid;
	int readPipe[2], writePipe[2];



	int status;
	BOOL atEndOfStream;
}

/**
 * \brief Creates a new OFProcess with the specified program and invokes the
 *	  program.







>
|
>


>
>
>
>






>


>
>
>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 *
 * Alternatively, it may be distributed under the terms of the GNU General
 * Public License, either version 2 or 3, which can be found in the file
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#ifndef _WIN32
# include <sys/types.h>
#endif

#import "OFStream.h"

#ifdef _WIN32
# include <windows.h>
#endif

/**
 * \brief A class for stream-like communication with a newly created process.
 */
@interface OFProcess: OFStream
{
#ifndef _WIN32
	pid_t pid;
	int readPipe[2], writePipe[2];
#else
	HANDLE readPipe[2], writePipe[2];
#endif
	int status;
	BOOL atEndOfStream;
}

/**
 * \brief Creates a new OFProcess with the specified program and invokes the
 *	  program.

Modified src/OFProcess.m from [3ee20593c1] to [afc4a2ba04].

13
14
15
16
17
18
19
20
21


22

23
24
25
26

27
28
29
30




31
32
33
34
35
36
37
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdlib.h>
#include <unistd.h>



#include <sys/wait.h>


#import "OFProcess.h"
#import "OFString.h"
#import "OFArray.h"


#import "OFInitializationFailedException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"





@implementation OFProcess
+ processWithProgram: (OFString*)program
{
	return [[[self alloc] initWithProgram: program] autorelease];
}








<

>
>
|
>




>




>
>
>
>







13
14
15
16
17
18
19

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

#include "config.h"

#include <stdlib.h>


#ifndef _WIN32
# include <unistd.h>
# include <sys/wait.h>
#endif

#import "OFProcess.h"
#import "OFString.h"
#import "OFArray.h"
#import "OFAutoreleasePool.h"

#import "OFInitializationFailedException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"

#ifdef _WIN32
# include <windows.h>
#endif

@implementation OFProcess
+ processWithProgram: (OFString*)program
{
	return [[[self alloc] initWithProgram: program] autorelease];
}

69
70
71
72
73
74
75

76
77
78
79
80
81
82
- initWithProgram: (OFString*)program
      programName: (OFString*)programName
	arguments: (OFArray*)arguments
{
	self = [super init];

	@try {

		if (pipe(readPipe) != 0 || pipe(writePipe) != 0)
			@throw [OFInitializationFailedException
			    exceptionWithClass: isa];

		switch ((pid = fork())) {
		case 0:;
			OFString **cArray = [arguments cArray];







>







76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
- initWithProgram: (OFString*)program
      programName: (OFString*)programName
	arguments: (OFArray*)arguments
{
	self = [super init];

	@try {
#ifndef _WIN32
		if (pipe(readPipe) != 0 || pipe(writePipe) != 0)
			@throw [OFInitializationFailedException
			    exceptionWithClass: isa];

		switch ((pid = fork())) {
		case 0:;
			OFString **cArray = [arguments cArray];
106
107
108
109
110
111
112































































































113
114
115
116
117
118
119
120
121
122

123



124
125
126
127
128
129
130
131

132



133

134
135









136
137
138

139
140
141
142
143
144
145
146
147
148

149
150







151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

171
172
173
174






175
176
177
178

179
180
181
182
183
184
185
186
187
188
189





190




191
			@throw [OFInitializationFailedException
			    exceptionWithClass: isa];
		default:
			close(readPipe[1]);
			close(writePipe[0]);
			break;
		}































































































	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (BOOL)_isAtEndOfStream
{

	if (readPipe[0] == -1)



		return YES;

	return atEndOfStream;
}

- (size_t)_readNBytes: (size_t)length
	   intoBuffer: (void*)buffer
{

	ssize_t ret;





	if (readPipe[0] == -1 || atEndOfStream ||
	    (ret = read(readPipe[0], buffer, length)) < 0)









		@throw [OFReadFailedException exceptionWithClass: isa
							  stream: self
						 requestedLength: length];


	if (ret == 0)
		atEndOfStream = YES;

	return ret;
}

- (void)_writeNBytes: (size_t)length
	  fromBuffer: (const void*)buffer
{

	if (writePipe[1] == -1 || atEndOfStream ||
	    write(writePipe[1], buffer, length) < length)







		@throw [OFWriteFailedException exceptionWithClass: isa
							   stream: self
						  requestedLength: length];
}

- (void)dealloc
{
	[self close];

	[super dealloc];
}

/*
 * FIXME: Add -[fileDescriptor]. The problem is that we have two FDs, which is
 *	  not yet supported by OFStreamObserver. This has to be split into one
 *	  FD for reading and one for writing.
 */

- (void)closeForWriting
{

	if (writePipe[1] != -1)
		close(writePipe[1]);

	writePipe[1] = -1;






}

- (void)close
{

	if (readPipe[0] != -1)
		close(readPipe[0]);
	if (writePipe[1] != -1)
		close(writePipe[1]);

	if (pid != -1)
		waitpid(pid, &status, WNOHANG);

	pid = -1;
	readPipe[0] = -1;
	writePipe[1] = -1;





}




@end







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>










>

>
>
>








>

>
>
>

>

|
>
>
>
>
>
>
>
>
>



>










>


>
>
>
>
>
>
>




















>




>
>
>
>
>
>




>











>
>
>
>
>
|
>
>
>
>

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
			@throw [OFInitializationFailedException
			    exceptionWithClass: isa];
		default:
			close(readPipe[1]);
			close(writePipe[0]);
			break;
		}
#else
		SECURITY_ATTRIBUTES sa;
		PROCESS_INFORMATION pi;
		STARTUPINFO si;
		OFAutoreleasePool *pool;
		OFMutableString *argumentsString;
		OFEnumerator *enumerator;
		OFString *argument;
		char *argumentsCString;

		sa.nLength = sizeof(sa);
		sa.bInheritHandle = TRUE;
		sa.lpSecurityDescriptor = NULL;

		if (!CreatePipe(&readPipe[0], &readPipe[1], &sa, 0))
			@throw [OFInitializationFailedException
			    exceptionWithClass: isa];

		if (!SetHandleInformation(readPipe[0], HANDLE_FLAG_INHERIT, 0))
			@throw [OFInitializationFailedException
			    exceptionWithClass: isa];

		if (!CreatePipe(&writePipe[0], &writePipe[1], &sa, 0))
			@throw [OFInitializationFailedException
			    exceptionWithClass: isa];

		if (!SetHandleInformation(writePipe[1], HANDLE_FLAG_INHERIT, 0))
			@throw [OFInitializationFailedException
			    exceptionWithClass: isa];

		memset(&pi, 0, sizeof(pi));
		memset(&si, 0, sizeof(si));

		si.cb = sizeof(si);
		si.hStdInput = writePipe[0];
		si.hStdOutput = readPipe[1];
		si.hStdError = GetStdHandle(STD_ERROR_HANDLE);
		si.dwFlags |= STARTF_USESTDHANDLES;

		pool = [[OFAutoreleasePool alloc] init];

		argumentsString =
		    [OFMutableString stringWithString: programName];
		[argumentsString replaceOccurrencesOfString: @"\\\""
						 withString: @"\\\\\""];
		[argumentsString replaceOccurrencesOfString: @"\""
						 withString: @"\\\""];

		if ([argumentsString containsString: @" "]) {
			[argumentsString prependString: @"\""];
			[argumentsString appendString: @"\""];
		}

		enumerator = [arguments objectEnumerator];
		while ((argument = [enumerator nextObject]) != nil) {
			OFMutableString *tmp =
			    [[argument mutableCopy] autorelease];
			BOOL containsSpaces = [tmp containsString: @" "];

			[argumentsString appendString: @" "];

			if (containsSpaces)
				[argumentsString appendString: @"\""];

			[tmp replaceOccurrencesOfString: @"\\\""
					     withString: @"\\\\\""];
			[tmp replaceOccurrencesOfString: @"\""
					     withString: @"\\\""];;

			[argumentsString appendString: tmp];

			if (containsSpaces)
				[argumentsString appendString: @"\""];
		}

		argumentsCString = strdup([argumentsString
		    cStringWithEncoding: OF_STRING_ENCODING_NATIVE]);
		@try {
			if (!CreateProcess([program cStringWithEncoding:
			    OF_STRING_ENCODING_NATIVE], argumentsCString, NULL,
			    NULL, TRUE, 0, NULL, NULL, &si, &pi))
				@throw [OFInitializationFailedException
				    exceptionWithClass: isa];
		} @finally {
			free(argumentsString);
		}

		[pool release];

		CloseHandle(pi.hProcess);
		CloseHandle(pi.hThread);

		CloseHandle(readPipe[1]);
		CloseHandle(writePipe[0]);
#endif
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- (BOOL)_isAtEndOfStream
{
#ifndef _WIN32
	if (readPipe[0] == -1)
#else
	if (readPipe[0] == NULL)
#endif
		return YES;

	return atEndOfStream;
}

- (size_t)_readNBytes: (size_t)length
	   intoBuffer: (void*)buffer
{
#ifndef _WIN32
	ssize_t ret;
#else
	DWORD ret;
#endif

#ifndef _WIN32
	if (readPipe[0] == -1 || atEndOfStream ||
	    (ret = read(readPipe[0], buffer, length)) < 0) {
#else
	if (readPipe[0] == NULL || atEndOfStream ||
	    !ReadFile(readPipe[0], buffer, length, &ret, NULL)) {
		if (GetLastError() == ERROR_BROKEN_PIPE) {
			atEndOfStream = YES;
			return 0;
		}

#endif
		@throw [OFReadFailedException exceptionWithClass: isa
							  stream: self
						 requestedLength: length];
	}

	if (ret == 0)
		atEndOfStream = YES;

	return ret;
}

- (void)_writeNBytes: (size_t)length
	  fromBuffer: (const void*)buffer
{
#ifndef _WIN32
	if (writePipe[1] == -1 || atEndOfStream ||
	    write(writePipe[1], buffer, length) < length)
#else
	DWORD ret;

	if (writePipe[1] == NULL || atEndOfStream ||
	    !WriteFile(writePipe[1], buffer, length, &ret, NULL) ||
	    ret < length)
#endif
		@throw [OFWriteFailedException exceptionWithClass: isa
							   stream: self
						  requestedLength: length];
}

- (void)dealloc
{
	[self close];

	[super dealloc];
}

/*
 * FIXME: Add -[fileDescriptor]. The problem is that we have two FDs, which is
 *	  not yet supported by OFStreamObserver. This has to be split into one
 *	  FD for reading and one for writing.
 */

- (void)closeForWriting
{
#ifndef _WIN32
	if (writePipe[1] != -1)
		close(writePipe[1]);

	writePipe[1] = -1;
#else
	if (writePipe[1] != NULL)
		CloseHandle(writePipe[1]);

	writePipe[1] = NULL;
#endif
}

- (void)close
{
#ifndef _WIN32
	if (readPipe[0] != -1)
		close(readPipe[0]);
	if (writePipe[1] != -1)
		close(writePipe[1]);

	if (pid != -1)
		waitpid(pid, &status, WNOHANG);

	pid = -1;
	readPipe[0] = -1;
	writePipe[1] = -1;
#else
	if (readPipe[0] != NULL)
		CloseHandle(readPipe[0]);
	if (writePipe[1] != NULL)
		CloseHandle(writePipe[1]);

	readPipe[0] = NULL;
	writePipe[1] = NULL;
#endif
}
@end