Re: NSTask output and input - Related question
Re: NSTask output and input - Related question
- Subject: Re: NSTask output and input - Related question
- From: Rudi Sherry <email@hidden>
- Date: Fri, 28 Jan 2005 09:19:07 -0800
On Jan 20, 2005, at 10:33 AM, email@hidden wrote:
This brings a point to mind.
I have in several tasks I've interacted with, had the worst time
trying to
get the task output data. I set up the pipes exactly as prescribed in
the
link below and yet I get either no output from the task, or I get
partial
data. Is there a flaw in the NSTask or the NSPipe which causes it not
to
get all the data correctly?
I've tried writing my own clu's to test it and the problem is worse.
Whether I use printf and make sure to use a new line character at the
end,
or I use NSLog, fprintf(stdout,"text %i\n",variable); or what ever I
tend
to get absolutely no data, or I get only partial data.
April.
I noticed that I only get the data when the task is done (if anyone
knows how to get data while it's still executing, please let me know
what you use).
Here is the excerpt from my code:
NS_DURING
//NSLog( @"About to execute shell command:\r%@ %@\r", command, [
arguments componentsJoinedByString: @" " ] );
NSTask *task = [ [ NSTask alloc ] init ];
[ task setLaunchPath: command ];
[ task setArguments: arguments ? arguments : [ NSArray array ] ];
NSPipe *stdOutPipe = [ [[ NSPipe alloc ] init ] retain ];
[ task setStandardOutput: stdOutPipe ];
[ task setStandardError: stdOutPipe ];
NSFileHandle *stdOutFileHandle = [ stdOutPipe fileHandleForReading ];
// Do it. If we get an exception, we're through
BOOL ok = NO;
NS_DURING
[ task launch ];
ok = YES;
NS_HANDLER
NSLog( @"Error launching task:\r%@\r", [ localException description
] );
NS_ENDHANDLER
if ( ok == NO )
{
NSLog( @"EXCEPTION from [ task launch ]:\r%@\r%@", command,
arguments );
[ outputHandler outputDone ];
return @"";
}
unsigned keepGoing = (unsigned) YES;
while ( keepGoing == (unsigned) YES ) // TBD: timeout
{
// This raises a NSFileHandlerOperationException if the channel
isn't opened
// yet: let that happen a few times until the task launches.
NSData *avail = NULL;
keepGoing = [ task isRunning ];
if ( !keepGoing )
break;
NS_DURING
avail = [ stdOutFileHandle availableData ];
NS_HANDLER
//NSLog( @"found first exception; continuing\r" );
NS_ENDHANDLER
if ( avail != NULL && [ avail length ] == 0 )
{
[ outputHandler outputDone ];
//NSLog( @"Empty data returned: end of communication?" );
break;
}
else
{
NSString *availString = [ [ NSString alloc ] initWithData: avail
encoding: NSASCIIStringEncoding ];
//NSLog( @"Return in this loop:\r%@\r", availString );
[ outputHandler handleOutput: availString ];
stdOutString = [ stdOutString stringByAppendingString: availString
];
}
}
[ stdOutPipe release ];
NS_HANDLER
// Shouldn't happen; I'm looking for an unhandled exception:
// NSDictionary setObject: NULL forKey:...
stdOutString = [ NSString stringWithFormat: @"EXCEPTION in
executeShellCommand: \r\t:%@",
[ localException description ] ];
NS_ENDHANDLER
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden