ObjFW  Check-in [f51bceaa35]

Overview
Comment:OFProcess: Implement environment passing on Win32.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f51bceaa354fa7b4e4f8dbb7d05f6b5f8d12f7a4bad10f77abf888754408fa43
User & Date: js on 2013-01-07 14:49:52
Other Links: manifest | tags
Context
2013-01-07
22:07
OFString: Zero-terminate UTF-16 strings. check-in: 9d3cd5e5fe user: js tags: trunk
14:49
OFProcess: Implement environment passing on Win32. check-in: f51bceaa35 user: js tags: trunk
13:36
Fix compilation on Win32. check-in: 2965720dbc user: js tags: trunk
Changes

Modified src/OFProcess.h from [580e231512] to [e1c603419d].

160
161
162
163
164
165
166
167

168
169
170
171
172
173
174
175
176
177
160
161
162
163
164
165
166

167
168
169
170
171
172
173
174
175
176
177







-
+










 * @return An initialized OFProcess.
 */
- initWithProgram: (OFString*)program
      programName: (OFString*)programName
	arguments: (OFArray*)arguments
      environment: (OFDictionary*)environment;

- (void)OF_setEnvironment: (OFDictionary*)dictionary;
- (char**)OF_environmentForDictionary: (OFDictionary*)dictionary;

/*!
 * @brief Closes the write direction of the process.
 *
 * This method needs to be called for some programs before data can be read,
 * since some programs don't start processing before the write direction is
 * closed.
 */
- (void)closeForWriting;
@end

Modified src/OFProcess.m from [2f6a6d8482] to [805ee3827f].

28
29
30
31
32
33
34

35
36
37
38
39
40
41
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42







+







# include <crt_externs.h>
#endif

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

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

#ifdef _WIN32
# include <windows.h>
135
136
137
138
139
140
141
142
143









144
145
146
147
148
149
150
136
137
138
139
140
141
142


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158







-
-
+
+
+
+
+
+
+
+
+







			for (i = 0; i < count; i++)
				argv[i + 1] = (char*)[objects[i]
				    cStringUsingEncoding:
				    OF_STRING_ENCODING_NATIVE];

			argv[i + 1] = NULL;

			if (environment != nil)
				[self OF_setEnvironment: environment];
			if (environment != nil) {
#ifdef __MACH__
				*_NSGetEnviron() = [self
				    OF_environmentForDictionary: environment];
#else
				environ = [self
				    OF_environmentForDictionary: environment];
#endif
			}

			close(readPipe[0]);
			close(writePipe[1]);
			dup2(writePipe[0], 0);
			dup2(readPipe[1], 1);
			execvp([program cStringUsingEncoding:
			    OF_STRING_ENCODING_NATIVE], argv);
235
236
237
238
239
240
241

242

243
244
245
246
247
248
249
243
244
245
246
247
248
249
250

251
252
253
254
255
256
257
258







+
-
+







		}

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

		objc_autoreleasePoolPop(pool);
258
259
260
261
262
263
264
265

266
267

268
269



270
271
272
273
274
275
276
267
268
269
270
271
272
273

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289







-
+


+


+
+
+







		[self release];
		@throw e;
	}

	return self;
}

- (void)OF_setEnvironment: (OFDictionary*)environment
- (char**)OF_environmentForDictionary: (OFDictionary*)environment
{
	OFEnumerator *keyEnumerator, *objectEnumerator;
#ifndef _WIN32
	char **envp;
	size_t i, count;

	if (environment == nil)
		return NULL;

	count = [environment count];
	envp = [self allocMemoryWithSize: sizeof(char*)
				   count: count + 1];

	keyEnumerator = [environment keyEnumerator];
	objectEnumerator = [environment objectEnumerator];
296
297
298
299
300
301
302
303
304

305


306



















307
308
309
310
311
312
313
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
339
340
341
342
343
344
345







-
-
+

+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







		memcpy(envp[i] + keyLen + 1, [object cStringUsingEncoding:
		    OF_STRING_ENCODING_NATIVE], objectLen);
		envp[i][keyLen + objectLen + 1] = '\0';
	}

	envp[i] = NULL;

#ifdef __MACH__
	*_NSGetEnviron() = envp;
	return envp;
#else
	OFDataArray *env = [OFDataArray dataArray];
	OFString *key, *object;
	environ = envp;

	keyEnumerator = [environment keyEnumerator];
	objectEnumerator = [environment objectEnumerator];

	while ((key = [keyEnumerator nextObject]) != nil &&
	    (object = [objectEnumerator nextObject]) != nil) {
		[env addItems: [key UTF8String]
			count: [key UTF8StringLength]];
		[env addItems: "="
			count: 1];
		[env addItems: [object UTF8String]
			count: [object UTF8StringLength]];
		[env addItems: ""
			count: 1];
	}
	[env addItems: ""
		count: 1];

	return [env items];
#endif
}

- (BOOL)lowlevelIsAtEndOfStream
{
#ifndef _WIN32
	if (readPipe[0] == -1)