9

さて、Objective-C でコマンド ライン ツールを実行する NSTask を作成できることはわかっています。

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

などのインタラクティブなコマンドラインツールと通信する方法があるかどうか疑問に思っていgdbます. runこれには、ユーザーの操作 ( 、killまたは など)quitに基づいてコマンド入力を与え、gdbそれが出力する情報に基づいて反応することが含まれます。

4

2 に答える 2

3

NSTaksクラスの使用setStandardInput: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");
}
于 2012-06-15T06:02:36.910 に答える
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];
于 2012-06-15T05:55:22.733 に答える