-
Notifications
You must be signed in to change notification settings - Fork 7
/
Taskit.h
145 lines (100 loc) · 3.54 KB
/
Taskit.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
// Taskit
// Written by Alex Gordon on 09/09/2011.
// Licensed under the WTFPL: http://sam.zoy.org/wtfpl/
#import <Foundation/Foundation.h>
typedef enum {
TaskitWaitFor_Exit = 1,
TaskitWaitFor_Output = 1 << 1,
TaskitWaitFor_Error = 1 << 2,
} TaskitWaitMaskComponent;
typedef unsigned TaskitWaitMask;
@interface Taskit : NSObject
{
BOOL hasLaunched;
NSString *launchPath;
NSMutableArray *arguments;
NSMutableDictionary *environment;
NSString *workingDirectory;
NSData *input;
NSString *inputString;
NSString* inputPath; // Optional alternative to inputString
//TODO: BOOL usesAuthorization;
NSPipe *inPipe;
NSPipe *outPipe;
NSPipe *errPipe;
BOOL shouldSetUpFileHandlesAutomatically;
pid_t pid;
int waitpid_status;
BOOL isRunning;
#ifdef TASKIT_BACKGROUNDING
void (^receivedOutputData)(NSData *output);
void (^receivedOutputString)(NSString *outputString);
void (^receivedErrorData)(NSData *err);
void (^receivedErrorString)(NSString *errString);
#endif
NSMutableData *outputBuffer;
NSMutableData *errorBuffer;
BOOL hasFinishedReadingOutput;
BOOL hasFinishedReadingError;
BOOL hasRetainedForOutput;
BOOL hasRetainedForError;
NSTimeInterval timeoutIfNothing;
NSTimeInterval timeoutSinceOutput;
NSTimeInterval timeoutSinceError;
NSInteger priority;
}
+ (id)task;
- (id)init;
#pragma mark Setup
@property (copy) NSString *launchPath;
@property (readonly) NSMutableArray *arguments;
@property (readonly) NSMutableDictionary *environment;
@property (copy) NSString *workingDirectory;
@property (copy) NSData *input;
@property (copy) NSString *inputString;
@property (copy) NSString *inputPath;
@property NSInteger priority;
@property BOOL shouldSetUpFileHandlesAutomatically;
- (void)setUpFileHandles;
- (void)populateWithCurrentEnvironment;
//TODO: @property BOOL usesAuthorization;
#pragma mark Concurrency
#ifdef TASKIT_BACKGROUNDING
@property (copy) void (^receivedOutputData)(NSData *output);
@property (copy) void (^receivedOutputString)(NSString *outputString);
@property (copy) void (^receivedErrorData)(NSData *err);
@property (copy) void (^receivedErrorString)(NSString *errString);
#endif
//TODO: @property (copy) void (^processExited)(NSString *outputString);
#pragma mark Timeouts
// The amount of time to wait if nothing has been read yet
@property NSTimeInterval timeoutIfNothing;
// The amount of time to wait for stderr if stdout HAS been read
@property NSTimeInterval timeoutSinceOutput;
// The amount of time to wait for stdout if stderr HAS been read
@property NSTimeInterval timeoutSinceError;
#pragma mark Status
- (NSInteger)processIdentifier;
- (NSInteger)terminationStatus;
- (NSTaskTerminationReason)terminationReason;
- (NSInteger)terminationSignal;
#pragma mark Control
- (BOOL)launch;
- (void)interrupt; // Not always possible. Sends SIGINT.
- (void)terminate; // Not always possible. Sends SIGTERM.
- (void)kill;
- (BOOL)suspend;
- (BOOL)resume;
- (BOOL)isRunning;
- (void)reapOnExit;
#pragma mark Blocking methods
- (void)waitUntilExit;
- (BOOL)waitUntilExitWithTimeout:(NSTimeInterval)timeout;
- (BOOL)waitForIntoOutputData:(NSMutableData *)output intoErrorData:(NSMutableData *)error;
- (BOOL)waitForOutputData:(NSData **)output errorData:(NSData **)error;
- (void)waitForOutputString:(NSString **)output errorString:(NSString **)error;
- (NSData *)waitForOutput;
- (NSString *)waitForOutputString;
- (NSData *)waitForError;
- (NSString *)waitForErrorString;
@end