︙ | | | ︙ | |
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#include <string.h>
/* Work around __block being used by glibc */
#ifdef __GLIBC__
# undef __USE_XOPEN
#endif
#ifndef _WIN32
# include <unistd.h>
# include <signal.h>
# include <sys/wait.h>
#endif
#ifdef HAVE_SPAWN_H
# include <spawn.h>
|
>
>
|
|
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
#include <string.h>
/* Work around __block being used by glibc */
#ifdef __GLIBC__
# undef __USE_XOPEN
#endif
#include "platform.h"
#ifndef OF_WINDOWS
# include <unistd.h>
# include <signal.h>
# include <sys/wait.h>
#endif
#ifdef HAVE_SPAWN_H
# include <spawn.h>
|
︙ | | | ︙ | |
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#import "OFSystemInfo.h"
#import "OFInitializationFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"
#ifdef _WIN32
# include <windows.h>
#endif
#ifndef __MACH__
extern char **environ;
#endif
@interface OFProcess (OF_PRIVATE_CATEGORY)
#ifndef _WIN32
- (void)OF_getArgV: (char***)argv
forProgramName: (OFString*)programName
andArguments: (OFArray*)arguments;
- (char**)OF_environmentForDictionary: (OFDictionary*)dictionary;
#else
- (of_char16_t*)OF_environmentForDictionary: (OFDictionary*)dictionary;
#endif
|
|
|
|
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#import "OFSystemInfo.h"
#import "OFInitializationFailedException.h"
#import "OFOutOfRangeException.h"
#import "OFReadFailedException.h"
#import "OFWriteFailedException.h"
#ifdef OF_WINDOWS
# include <windows.h>
#endif
#if !defined(OF_WINDOWS) && !defined(HAVE_POSIX_SPAWNP)
extern char **environ;
#endif
@interface OFProcess (OF_PRIVATE_CATEGORY)
#ifndef OF_WINDOWS
- (void)OF_getArgV: (char***)argv
forProgramName: (OFString*)programName
andArguments: (OFArray*)arguments;
- (char**)OF_environmentForDictionary: (OFDictionary*)dictionary;
#else
- (of_char16_t*)OF_environmentForDictionary: (OFDictionary*)dictionary;
#endif
|
︙ | | | ︙ | |
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
programName: (OFString*)programName
arguments: (OFArray*)arguments
environment: (OFDictionary*)environment
{
self = [super init];
@try {
#ifndef _WIN32
void *pool = objc_autoreleasePoolPush();
const char *path;
char **argv;
if (pipe(_readPipe) != 0 || pipe(_writePipe) != 0)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
|
|
|
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
programName: (OFString*)programName
arguments: (OFArray*)arguments
environment: (OFDictionary*)environment
{
self = [super init];
@try {
#ifndef OF_WINDOWS
void *pool = objc_autoreleasePoolPush();
const char *path;
char **argv;
if (pipe(_readPipe) != 0 || pipe(_writePipe) != 0)
@throw [OFInitializationFailedException
exceptionWithClass: [self class]];
|
︙ | | | ︙ | |
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
|
- (void)dealloc
{
[self close];
[super dealloc];
}
#ifndef _WIN32
- (void)OF_getArgV: (char***)argv
forProgramName: (OFString*)programName
andArguments: (OFArray*)arguments
{
OFString *const *objects = [arguments objects];
size_t i, count = [arguments count];
of_string_encoding_t encoding;
|
|
|
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
- (void)dealloc
{
[self close];
[super dealloc];
}
#ifndef OF_WINDOWS
- (void)OF_getArgV: (char***)argv
forProgramName: (OFString*)programName
andArguments: (OFArray*)arguments
{
OFString *const *objects = [arguments objects];
size_t i, count = [arguments count];
of_string_encoding_t encoding;
|
︙ | | | ︙ | |
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
return [env items];
}
#endif
- (bool)lowlevelIsAtEndOfStream
{
#ifndef _WIN32
if (_readPipe[0] == -1)
#else
if (_readPipe[0] == NULL)
#endif
return true;
return _atEndOfStream;
}
- (size_t)lowlevelReadIntoBuffer: (void*)buffer
length: (size_t)length
{
#ifndef _WIN32
ssize_t ret;
#else
DWORD ret;
#endif
#ifndef _WIN32
if (_readPipe[0] == -1 || _atEndOfStream)
@throw [OFReadFailedException exceptionWithObject: self
requestedLength: length];
if ((ret = read(_readPipe[0], buffer, length)) < 0)
@throw [OFReadFailedException exceptionWithObject: self
requestedLength: length
errNo: errno];
#else
if (length > UINT32_MAX)
@throw [OFOutOfRangeException exception];
if (_readPipe[0] == NULL || _atEndOfStream)
@throw [OFReadFailedException exceptionWithObject: self
requestedLength: length];
|
|
|
<
<
<
<
>
>
|
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
return [env items];
}
#endif
- (bool)lowlevelIsAtEndOfStream
{
#ifndef OF_WINDOWS
if (_readPipe[0] == -1)
#else
if (_readPipe[0] == NULL)
#endif
return true;
return _atEndOfStream;
}
- (size_t)lowlevelReadIntoBuffer: (void*)buffer
length: (size_t)length
{
#ifndef OF_WINDOWS
ssize_t ret;
if (_readPipe[0] == -1 || _atEndOfStream)
@throw [OFReadFailedException exceptionWithObject: self
requestedLength: length];
if ((ret = read(_readPipe[0], buffer, length)) < 0)
@throw [OFReadFailedException exceptionWithObject: self
requestedLength: length
errNo: errno];
#else
DWORD ret;
if (length > UINT32_MAX)
@throw [OFOutOfRangeException exception];
if (_readPipe[0] == NULL || _atEndOfStream)
@throw [OFReadFailedException exceptionWithObject: self
requestedLength: length];
|
︙ | | | ︙ | |
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
return ret;
}
- (void)lowlevelWriteBuffer: (const void*)buffer
length: (size_t)length
{
#ifndef _WIN32
if (_writePipe[1] == -1 || _atEndOfStream)
@throw [OFWriteFailedException exceptionWithObject: self
requestedLength: length];
if (length > SSIZE_MAX)
@throw [OFOutOfRangeException exception];
|
|
|
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
return ret;
}
- (void)lowlevelWriteBuffer: (const void*)buffer
length: (size_t)length
{
#ifndef OF_WINDOWS
if (_writePipe[1] == -1 || _atEndOfStream)
@throw [OFWriteFailedException exceptionWithObject: self
requestedLength: length];
if (length > SSIZE_MAX)
@throw [OFOutOfRangeException exception];
|
︙ | | | ︙ | |
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
errNo: errNo];
}
#endif
}
- (int)fileDescriptorForReading
{
#ifndef _WIN32
return _readPipe[0];
#else
OF_UNRECOGNIZED_SELECTOR
#endif
}
- (int)fileDescriptorForWriting
{
#ifndef _WIN32
return _writePipe[1];
#else
OF_UNRECOGNIZED_SELECTOR
#endif
}
- (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) {
kill(_pid, SIGTERM);
|
|
|
|
|
|
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
|
errNo: errNo];
}
#endif
}
- (int)fileDescriptorForReading
{
#ifndef OF_WINDOWS
return _readPipe[0];
#else
OF_UNRECOGNIZED_SELECTOR
#endif
}
- (int)fileDescriptorForWriting
{
#ifndef OF_WINDOWS
return _writePipe[1];
#else
OF_UNRECOGNIZED_SELECTOR
#endif
}
- (void)closeForWriting
{
#ifndef OF_WINDOWS
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 OF_WINDOWS
if (_readPipe[0] != -1)
close(_readPipe[0]);
if (_writePipe[1] != -1)
close(_writePipe[1]);
if (_pid != -1) {
kill(_pid, SIGTERM);
|
︙ | | | ︙ | |