jens@20: // jens@20: // MYTask.h jens@20: // Murky jens@20: // jens@20: // Copyright 2008 Jens Alfke. All rights reserved. jens@20: // jens@20: jens@20: #import jens@20: jens@20: jens@20: extern NSString* const MYTaskErrorDomain; jens@20: extern NSString* const MYTaskExitCodeKey; jens@20: extern NSString* const MYTaskObjectKey; jens@20: enum { jens@20: kMYTaskError = 2 jens@20: }; jens@20: jens@20: jens@20: jens@20: @interface MYTask : NSObject jens@20: { jens@20: @private jens@20: NSString *_command; jens@20: NSMutableArray *_arguments; jens@20: NSString *_currentDirectoryPath; jens@20: NSTask *_task; jens@20: int _resultCode; jens@20: NSError *_error; jens@20: BOOL _ignoreOutput; jens@20: NSFileHandle *_outHandle, *_errHandle; jens@20: NSMutableData *_outputData, *_errorData; jens@20: NSString *_output; jens@20: NSMutableArray *_modes; jens@20: BOOL _isRunning, _taskRunning; jens@20: } jens@20: jens@20: - (id) initWithCommand: (NSString*)subcommand, ... NS_REQUIRES_NIL_TERMINATION; jens@20: jens@20: /* designated initializer (subclasses can override) */ jens@20: - (id) initWithCommand: (NSString*)subcommand jens@20: arguments: (NSArray*)arguments; jens@20: jens@20: - (id) initWithError: (NSError*)error; jens@20: jens@20: - (void) addArgument: (id)argument; jens@20: - (void) addArguments: (id)arg1, ... NS_REQUIRES_NIL_TERMINATION; jens@20: - (void) addArgumentsFromArray: (NSArray*)arguments; jens@20: - (void) prependArguments: (id)arg1, ... NS_REQUIRES_NIL_TERMINATION; jens@20: jens@20: - (void) ignoreOutput; jens@20: jens@20: @property (copy) NSString* currentDirectoryPath; jens@20: jens@27: /** Prettified description of command string. Doesn't do full shell-style quoting, though. */ jens@27: - (NSString*) commandLine; jens@27: jens@20: - (BOOL) run; jens@20: - (BOOL) run: (NSError**)outError; jens@20: jens@20: - (BOOL) start; jens@20: - (void) stop; jens@20: - (BOOL) waitTillFinished; jens@20: jens@20: @property (readonly,nonatomic) BOOL isRunning; jens@20: @property (readonly,retain,nonatomic) NSError* error; jens@20: @property (readonly,nonatomic) NSString *output, *outputAndError; jens@20: @property (readonly,nonatomic) NSData *outputData; jens@20: jens@20: // protected: jens@20: jens@20: /** Subclasses can override this to add arguments or customize the task */ jens@20: - (NSTask*) createTask; jens@20: jens@20: /** Sets the error based on the message and parameters. Always returns NO. */ jens@20: - (BOOL) makeError: (NSString*)fmt, ...; jens@20: jens@20: /** Called when the task finishes, just before the isRunning property changes back to NO. jens@20: You can override this to do your own post-processing. */ jens@20: - (void) finished; jens@20: jens@20: @end