/* NSTask+MiscExtensions.m created by tsengel on Thu 04-Sep-1997 */

#import "NSTask+MiscExtensions.h"

@implementation NSTask (MiscExtensions)

+ (NSData *)outputOfCommand:(NSString *)commandString
/*"
    Returns the data which is the result of executing the command described in command string. Sicne this method blocks until the command is finished and the output stream reaches end-of-file...be sure to only executes commands which terminate (no "tail -f console.log" or interactive things like "emacs myFile").

    #Note: This currently only works for very simple commands...however we should be able to reach the state of accepting things like "ps -aux | grep username | gnutar cvfz blah.tar.gz -"... Jup. That would be nice. But for this we need some generic command string parsing...and this needs the integration of MiscCommandLineParser or similar code.
"*/
{
    id		commandList;
    id		commandPath;
    id		commandTask;
    id		ourPipe;
    id		theFileHandle;
    id		argArray;
    id		parsingHackArray;

    // First extract the single commands which get piped all over the place

    commandList = [[[commandString componentsSeparatedByString:@"|"] mutableCopy] autorelease];

    // Now take teh frist command and wire it up to next next etc. pp..

    commandString = [commandList objectAtIndex:0];
    parsingHackArray = [[[commandString componentsSeparatedByString:@" "] mutableCopy] autorelease];

   // The first argument is considered the path...if it is not executable return nil.

    commandPath = [parsingHackArray objectAtIndex:0];
    if( ![[NSFileManager defaultManager] isExecutableFileAtPath:commandPath] ) return nil;

    [parsingHackArray removeObjectAtIndex:0];
    argArray = parsingHackArray;

    commandTask = [[NSTask new] autorelease];
    ourPipe = [NSPipe pipe];

    [commandTask setStandardOutput:ourPipe];
    [commandTask setArguments:argArray];

    [commandTask setLaunchPath:commandPath];
    [commandTask launch];

    theFileHandle = [ourPipe fileHandleForReading];

    return [theFileHandle readDataToEndOfFile];
}

+ (NSData *)outputOfCommandArray:(NSArray *)commandArray
/*"
   Taskes and array as an argument where each entry is a dictionary which contains the LaunchPath, Arguments. All commands are "piped" together in the given order. 
"*/
{
    // <<HACK>> Nothing..
}

@end
