2012-06-15 13 views
8

Ok, quindi so di poter effettuare una NSTask per eseguire gli strumenti da riga di comando con Objective-C:cacao wrapper per un comando interattivo Unix

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

Mi chiedo solo se c'è un modo per comunicare con interattivo strumenti da riga di comando come gdb. Ciò implicherebbe dare gli input di comando in base all'interazione dell'utente (come run, kill o quit con gdb) e quindi reagire in base alle informazioni che emette.

risposta

2

È possibile utilizzare i selettori di NSTask setStandardInput:, setStandardOutput: e setStandardError: insieme alle istanze NSPipe per comunicare con il programma avviato.

Ad esempio, per leggere l'uscita del compito:

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

È quindi possibile ottenere un'istanza NSFileHandle che è possibile utilizzare per leggere l'uscita del compito con:

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

Per impostare un tubo per inviare comandi a gdb, aggiungere

[task setStandardInput: [NSPipe pipe]]; 

prima di si avvia l'attività. Quindi si ottiene la NSFileHandle con

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

Cheers! Usereste qualcosa come 'NSData * data = [inString dataUsingEncoding: NSUTF8StringEncoding]; [writeToMe writeData: data]; 'per inviare commenti positivi? L'ho provato ma non sembra funzionare. – bjz

+3

@bjz Questo dovrebbe funzionare. Non dimenticare di aggiungere un '\ n' alla fine della stringa per simulare che l'utente debba tornare dopo aver digitato un comando. – sjs

2

Usa setStandardInput: e setStandardOutput: metodi di NSTaks class.

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"); 
} 
+0

Ciao, hai ottenuto questo lavoro? Mi sembra di avere problemi nell'implementazione di questo. Non ricevo nessuna notifica. Sto usando questo per eseguire un clone git. Ho bisogno delle notifiche per farmi sapere quando mi sta chiedendo il nome utente e la password. Qualsiasi aiuto? –

Problemi correlati