用于交互式Unix命令的Cocoa包装器

时间:2012-06-15 05:23:35

标签: objective-c cocoa unix command-line-tool interactive-mode

好的,所以我知道你可以使用Objective-C运行命令行工具NSTask:

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/gdb"];
[task launch];

我只是想知道是否有办法与交互式命令行工具(例如gdb)进行通信。这将涉及根据用户交互(如runkillquitgdb)提供命令输入,然后根据其输出的信息做出反应。

2 个答案:

答案 0 :(得分:3)

您可以将NSTask的setStandardInput:setStandardOutput:setStandardError:选择器与NSPipe实例结合使用,以便与已启动的程序进行通信。

例如,要读取任务的输出:

task = [[NSTask alloc] init];
[task setStandardOutput: [NSPipe pipe]];
[task setStandardError: [task standardOutput]]; // Get standard error output too
[task setLaunchPath: @"/usr/bin/gdb"];
[task launch];

然后,您可以获得一个NSFileHandle实例,您可以使用该实例读取任务的输出:

NSFileHandle *readFromMe = [[task standardOutput] fileHandleForReading]; 

要设置管道以将命令发送到gdb,您需要添加

[task setStandardInput: [NSPipe pipe]];
在之前

启动任务。然后你得到NSFileHandle

NSFileHandle *writeToMe = [[task standardInput] fileHandleForWriting];

答案 1 :(得分:3)

使用NSTaks classsetStandardInput:setStandardOutput:方法。

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/bin/gdb"];

NSPipe *outputpipe=[[NSPipe alloc]init];
NSPipe *errorpipe=[[NSPipe alloc]init];
NSFileHandle *output,*error;

[task setArguments: arguments];
[task setStandardOutput:outputpipe];
[task setStandardError:errorpipe];

NSLog(@"%@",arguments);

output=[outputpipe fileHandleForReading];    
error=[errorpipe  fileHandleForReading];    
[task launch]; 

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedData:)  name: NSFileHandleReadCompletionNotification object:output];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receivedError:)  name: NSFileHandleReadCompletionNotification object:error];    
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TaskCompletion:)  name: NSTaskDidTerminateNotification object:task];

//[input writeData:[NSMutableData initWithString:@"test"]];
[output readInBackgroundAndNotify];
[error readInBackgroundAndNotify];

[task waitUntilExit];
[outputpipe release];
[errorpipe release];
[task release];

-(void) receivedData:(NSNotification*) rec_not {
    NSFileHandle *out=[[task standardOutput] fileHandleForReading]; 
    NSData *dataOutput=[[rec_not userInfo] objectForKey:NSFileHandleNotificationDataItem];

    if( !dataOutput)
        NSLog(@">>>>>>>>>>>>>>Empty Data");

    NSString *strfromdata=[[NSString alloc] initWithData:dataOutput encoding:NSUTF8StringEncoding];    
    [out readInBackgroundAndNotify];
    [strfromdata release];
}

/* Called when there is some data in the error pipe */
-(void) receivedError:(NSNotification*) rec_not {
    NSFileHandle *err=[[task standardError] fileHandleForReading];  
    NSData *dataOutput=[[rec_not userInfo] objectForKey:NSFileHandleNotificationDataItem];

    if( !dataOutput)    
        NSLog(@">>>>>>>>>>>>>>Empty Data");
    else {
        NSString *strfromdata=[[NSString alloc] initWithData:dataOutput encoding:NSUTF8StringEncoding];
    [strfromdata release];
    }
    [err readInBackgroundAndNotify];
}

/* Called when the task is complete */
-(void) TaskCompletion :(NSNotification*) rec_not { 
    NSLog(@"task ended");
}