@@ -16,11 +16,11 @@ #include "config.h" #include #include -#import "OFProcess.h" +#import "OFSubprocess.h" #import "OFArray.h" #import "OFData.h" #import "OFDictionary.h" #import "OFLocale.h" #import "OFString.h" @@ -32,41 +32,41 @@ #import "OFReadFailedException.h" #import "OFWriteFailedException.h" #include -@interface OFProcess () -- (of_char16_t *)of_wideEnvironmentForDictionary: (OFDictionary *)dictionary; +@interface OFSubprocess () +- (OFChar16 *)of_wideEnvironmentForDictionary: (OFDictionary *)dictionary; - (char *)of_environmentForDictionary: (OFDictionary *)environment; @end -@implementation OFProcess -+ (instancetype)processWithProgram: (OFString *)program +@implementation OFSubprocess ++ (instancetype)subprocessWithProgram: (OFString *)program { return [[[self alloc] initWithProgram: program] autorelease]; } -+ (instancetype)processWithProgram: (OFString *)program - arguments: (OFArray *)arguments ++ (instancetype)subprocessWithProgram: (OFString *)program + arguments: (OFArray *)arguments { return [[[self alloc] initWithProgram: program arguments: arguments] autorelease]; } -+ (instancetype)processWithProgram: (OFString *)program - programName: (OFString *)programName - arguments: (OFArray *)arguments ++ (instancetype)subprocessWithProgram: (OFString *)program + programName: (OFString *)programName + arguments: (OFArray *)arguments { return [[[self alloc] initWithProgram: program programName: programName arguments: arguments] autorelease]; } -+ (instancetype)processWithProgram: (OFString *)program - programName: (OFString *)programName - arguments: (OFArray *)arguments - environment: (OFDictionary *)environment ++ (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,11 +115,11 @@ SECURITY_ATTRIBUTES sa; PROCESS_INFORMATION pi; void *pool; OFMutableString *argumentsString; - _process = INVALID_HANDLE_VALUE; + _handle = INVALID_HANDLE_VALUE; _readPipe[0] = _writePipe[1] = NULL; sa.nLength = sizeof(sa); sa.bInheritHandle = TRUE; sa.lpSecurityDescriptor = NULL; @@ -180,11 +180,11 @@ [argumentsString appendString: @"\""]; } if ([OFSystemInfo isWindowsNT]) { size_t length; - of_char16_t *argumentsCopy; + OFChar16 *argumentsCopy; STARTUPINFOW si; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); si.hStdInput = _writePipe[0]; @@ -191,12 +191,12 @@ si.hStdOutput = _readPipe[1]; si.hStdError = GetStdHandle(STD_ERROR_HANDLE); si.dwFlags |= STARTF_USESTDHANDLES; length = argumentsString.UTF16StringLength; - argumentsCopy = of_alloc(length + 1, - sizeof(of_char16_t)); + argumentsCopy = OFAllocMemory(length + 1, + sizeof(OFChar16)); memcpy(argumentsCopy, argumentsString.UTF16String, (length + 1) * 2); @try { if (!CreateProcessW(program.UTF16String, argumentsCopy, NULL, NULL, TRUE, @@ -204,14 +204,14 @@ [self of_wideEnvironmentForDictionary: environment], NULL, &si, &pi)) @throw [OFInitializationFailedException exceptionWithClass: self.class]; } @finally { - free(argumentsCopy); + OFFreeMemory(argumentsCopy); } } else { - of_string_encoding_t encoding = [OFLocale encoding]; + OFStringEncoding encoding = [OFLocale encoding]; STARTUPINFO si; memset(&si, 0, sizeof(si)); si.cb = sizeof(si); si.hStdInput = _writePipe[0]; @@ -228,11 +228,11 @@ exceptionWithClass: self.class]; } objc_autoreleasePoolPop(pool); - _process = pi.hProcess; + _handle = pi.hProcess; CloseHandle(pi.hThread); CloseHandle(_readPipe[1]); CloseHandle(_writePipe[0]); } @catch (id e) { @@ -249,45 +249,41 @@ [self close]; [super dealloc]; } -- (of_char16_t *)of_wideEnvironmentForDictionary: (OFDictionary *)environment +- (OFChar16 *)of_wideEnvironmentForDictionary: (OFDictionary *)environment { OFMutableData *env; OFEnumerator *keyEnumerator, *objectEnumerator; OFString *key, *object; - const of_char16_t equal = '='; - const of_char16_t zero[2] = { 0, 0 }; + const OFChar16 equal = '='; + const OFChar16 zero[2] = { 0, 0 }; if (environment == nil) return NULL; - env = [OFMutableData dataWithItemSize: sizeof(of_char16_t)]; + env = [OFMutableData dataWithItemSize: sizeof(OFChar16)]; 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: 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: 1]; } - [env addItems: zero - count: 2]; + [env addItems: zero count: 2]; return env.mutableItems; } - (char *)of_environmentForDictionary: (OFDictionary *)environment { - of_string_encoding_t encoding = [OFLocale encoding]; + OFStringEncoding encoding = [OFLocale encoding]; OFMutableData *env; OFEnumerator *keyEnumerator, *objectEnumerator; OFString *key, *object; if (environment == nil) @@ -299,19 +295,16 @@ 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: "=" count: 1]; [env addItems: [object cStringWithEncoding: encoding] count: [object cStringLengthWithEncoding: encoding]]; - [env addItems: "" - count: 1]; + [env addItems: "" count: 1]; } - [env addItems: "\0" - count: 2]; + [env addItems: "\0" count: 2]; return env.mutableItems; } - (bool)lowlevelIsAtEndOfStream @@ -320,12 +313,11 @@ @throw [OFNotOpenException exceptionWithObject: self]; return _atEndOfStream; } -- (size_t)lowlevelReadIntoBuffer: (void *)buffer - length: (size_t)length +- (size_t)lowlevelReadIntoBuffer: (void *)buffer length: (size_t)length { DWORD ret; if (length > UINT32_MAX) @throw [OFOutOfRangeException exception]; @@ -348,12 +340,11 @@ _atEndOfStream = true; return ret; } -- (size_t)lowlevelWriteBuffer: (const void *)buffer - length: (size_t)length +- (size_t)lowlevelWriteBuffer: (const void *)buffer length: (size_t)length { DWORD bytesWritten; if (length > UINT32_MAX) @throw [OFOutOfRangeException exception]; @@ -391,16 +382,16 @@ @throw [OFNotOpenException exceptionWithObject: self]; [self closeForWriting]; CloseHandle(_readPipe[0]); - if (_process != INVALID_HANDLE_VALUE) { - TerminateProcess(_process, 0); - CloseHandle(_process); + if (_handle != INVALID_HANDLE_VALUE) { + TerminateProcess(_handle, 0); + CloseHandle(_handle); } - _process = INVALID_HANDLE_VALUE; + _handle = INVALID_HANDLE_VALUE; _readPipe[0] = NULL; [super close]; } @@ -407,22 +398,22 @@ - (int)waitForTermination { if (_readPipe[0] == NULL) @throw [OFNotOpenException exceptionWithObject: self]; - if (_process != INVALID_HANDLE_VALUE) { + if (_handle != INVALID_HANDLE_VALUE) { DWORD exitCode; - WaitForSingleObject(_process, INFINITE); + WaitForSingleObject(_handle, INFINITE); - if (GetExitCodeProcess(_process, &exitCode)) + if (GetExitCodeProcess(_handle, &exitCode)) _status = exitCode; else _status = GetLastError(); - CloseHandle(_process); - _process = INVALID_HANDLE_VALUE; + CloseHandle(_handle); + _handle = INVALID_HANDLE_VALUE; } return _status; } @end