Differences From Artifact [b53baca85e]:
- File
src/platform/windows/OFProcess.m
— part of check-in
[627511b032]
at
2020-11-14 12:23:55
on branch trunk
— Rename of_malloc and of_calloc
The new names should be more accurate. (user: js, size: 10694) [annotate] [blame] [check-ins using] [more...]
To Artifact [be45f52a11]:
- File src/platform/windows/OFSubprocess.m — part of check-in [79548b3c29] at 2021-03-30 21:06:33 on branch trunk — Rename OFProcess to OFSubprocess (user: js, size: 10579) [annotate] [blame] [check-ins using] [more...]
1 | /* | < < | | | | | | | | | | | | | | | 1 2 3 4 5 6 7 8 9 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | /* * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im> * * All rights reserved. * * This file is part of ObjFW. It may be distributed under the terms of the * Q Public License 1.0, which can be found in the file LICENSE.QPL included in * the packaging of this file. * * 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 "config.h" #include <errno.h> #include <string.h> #import "OFSubprocess.h" #import "OFArray.h" #import "OFData.h" #import "OFDictionary.h" #import "OFLocale.h" #import "OFString.h" #import "OFSystemInfo.h" #import "OFInitializationFailedException.h" #import "OFNotOpenException.h" #import "OFOutOfRangeException.h" #import "OFReadFailedException.h" #import "OFWriteFailedException.h" #include <windows.h> @interface OFSubprocess () - (of_char16_t *)of_wideEnvironmentForDictionary: (OFDictionary *)dictionary; - (char *)of_environmentForDictionary: (OFDictionary *)environment; @end @implementation OFSubprocess + (instancetype)subprocessWithProgram: (OFString *)program { return [[[self alloc] initWithProgram: program] autorelease]; } + (instancetype)subprocessWithProgram: (OFString *)program arguments: (OFArray *)arguments { return [[[self alloc] initWithProgram: program arguments: arguments] autorelease]; } + (instancetype)subprocessWithProgram: (OFString *)program programName: (OFString *)programName arguments: (OFArray *)arguments { return [[[self alloc] initWithProgram: program programName: programName arguments: arguments] autorelease]; } + (instancetype)subprocessWithProgram: (OFString *)program programName: (OFString *)programName arguments: (OFArray *)arguments environment: (OFDictionary *)environment { return [[[self alloc] initWithProgram: program programName: programName arguments: arguments environment: environment] autorelease]; } |
︙ | ︙ | |||
115 116 117 118 119 120 121 | @try { SECURITY_ATTRIBUTES sa; PROCESS_INFORMATION pi; void *pool; OFMutableString *argumentsString; | | | 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | @try { SECURITY_ATTRIBUTES sa; PROCESS_INFORMATION pi; void *pool; OFMutableString *argumentsString; _handle = INVALID_HANDLE_VALUE; _readPipe[0] = _writePipe[1] = NULL; sa.nLength = sizeof(sa); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; if (!CreatePipe(&_readPipe[0], &_readPipe[1], &sa, 0)) |
︙ | ︙ | |||
228 229 230 231 232 233 234 | NULL, &si, &pi)) @throw [OFInitializationFailedException exceptionWithClass: self.class]; } objc_autoreleasePoolPop(pool); | | | 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | NULL, &si, &pi)) @throw [OFInitializationFailedException exceptionWithClass: self.class]; } objc_autoreleasePoolPop(pool); _handle = pi.hProcess; CloseHandle(pi.hThread); CloseHandle(_readPipe[1]); CloseHandle(_writePipe[0]); } @catch (id e) { [self release]; @throw e; |
︙ | ︙ | |||
266 267 268 269 270 271 272 | env = [OFMutableData dataWithItemSize: sizeof(of_char16_t)]; keyEnumerator = [environment keyEnumerator]; objectEnumerator = [environment objectEnumerator]; while ((key = [keyEnumerator nextObject]) != nil && (object = [objectEnumerator nextObject]) != nil) { | | < | < | < | < | 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | env = [OFMutableData dataWithItemSize: sizeof(of_char16_t)]; keyEnumerator = [environment keyEnumerator]; objectEnumerator = [environment objectEnumerator]; while ((key = [keyEnumerator nextObject]) != nil && (object = [objectEnumerator nextObject]) != nil) { [env addItems: key.UTF16String count: key.UTF16StringLength]; [env addItems: &equal count: 1]; [env addItems: object.UTF16String count: object.UTF16StringLength]; [env addItems: &zero count: 1]; } [env addItems: zero count: 2]; return env.mutableItems; } - (char *)of_environmentForDictionary: (OFDictionary *)environment { of_string_encoding_t encoding = [OFLocale encoding]; |
︙ | ︙ | |||
299 300 301 302 303 304 305 | keyEnumerator = [environment keyEnumerator]; objectEnumerator = [environment objectEnumerator]; while ((key = [keyEnumerator nextObject]) != nil && (object = [objectEnumerator nextObject]) != nil) { [env addItems: [key cStringWithEncoding: encoding] count: [key cStringLengthWithEncoding: encoding]]; | | < | < | < | < | 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 | keyEnumerator = [environment keyEnumerator]; objectEnumerator = [environment objectEnumerator]; while ((key = [keyEnumerator nextObject]) != nil && (object = [objectEnumerator nextObject]) != nil) { [env addItems: [key cStringWithEncoding: encoding] count: [key cStringLengthWithEncoding: encoding]]; [env addItems: "=" count: 1]; [env addItems: [object cStringWithEncoding: encoding] count: [object cStringLengthWithEncoding: encoding]]; [env addItems: "" count: 1]; } [env addItems: "\0" count: 2]; return env.mutableItems; } - (bool)lowlevelIsAtEndOfStream { if (_readPipe[0] == NULL) @throw [OFNotOpenException exceptionWithObject: self]; return _atEndOfStream; } - (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length { DWORD ret; if (length > UINT32_MAX) @throw [OFOutOfRangeException exception]; if (_readPipe[0] == NULL) |
︙ | ︙ | |||
348 349 350 351 352 353 354 | if (ret == 0) _atEndOfStream = true; return ret; } | | < | 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | if (ret == 0) _atEndOfStream = true; return ret; } - (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length { DWORD bytesWritten; if (length > UINT32_MAX) @throw [OFOutOfRangeException exception]; if (_writePipe[1] == NULL) |
︙ | ︙ | |||
391 392 393 394 395 396 397 | { if (_readPipe[0] == NULL) @throw [OFNotOpenException exceptionWithObject: self]; [self closeForWriting]; CloseHandle(_readPipe[0]); | | | | | | | | | | | 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | { if (_readPipe[0] == NULL) @throw [OFNotOpenException exceptionWithObject: self]; [self closeForWriting]; CloseHandle(_readPipe[0]); if (_handle != INVALID_HANDLE_VALUE) { TerminateProcess(_handle, 0); CloseHandle(_handle); } _handle = INVALID_HANDLE_VALUE; _readPipe[0] = NULL; [super close]; } - (int)waitForTermination { if (_readPipe[0] == NULL) @throw [OFNotOpenException exceptionWithObject: self]; if (_handle != INVALID_HANDLE_VALUE) { DWORD exitCode; WaitForSingleObject(_handle, INFINITE); if (GetExitCodeProcess(_handle, &exitCode)) _status = exitCode; else _status = GetLastError(); CloseHandle(_handle); _handle = INVALID_HANDLE_VALUE; } return _status; } @end |