Interacting with an NSTask via pipes
Interacting with an NSTask via pipes
- Subject: Interacting with an NSTask via pipes
- From: Tony Gray <email@hidden>
- Date: Wed, 06 Feb 2002 19:26:12 +1100
There have been several questions on this list from people wanting to
interact with a process started via NSTask using pipes, and there have been
several replies that point to two tutorials on cocoadevcentral, as well as
the moriarty demo code in the ADC sample code area.
Unless I've missed something, all of these examples involve starting the
task and then just reading it's output (perhaps in the background, to be
really nice).
So far, I've been unable to find any examples of "driving" the external
process, ie., reading the lines that it outputs, parsing them, and sending
various responses back (eg., to respond to requests for input). Does
anybody know of any sample code that demonstrates this?
I've done some initial experimentation (using the TaskWrapper from the
moriarty code), but I'm not always getting back responses from the spawned
process. This seems to be a problem with buffering.
To test my code, I wrote a simple Perl script:
#!/usr/bin/perl
$|++;
print "hello there\n";
<STDIN>;
print "this is another line\n";
<STDIN>;
print "done\n";
The odd second line tells Perl not to buffer output, but to flush after
every print. When I spawn this via NSTask, the parent process receives the
first message that the script outputs. When the parent process sends the
script a line of text it responds with the second message, and when the
parent sends another line the child replies with its last line and then
terminates normally. This is (a dumbed-down version of) exactly what I
want.
If I remove that odd second line from the Perl script, things stop working -
my controller's "appendOutput:" method never gets called. In my Perl
sample, I can fix this by leaving the second line in, but for my "real"
project, I need to talk to a precompiled program that I don't have the
source code for, and it's not flushing the buffer before waiting for input.
Can anyone give me any tips on how to proceed here?
Thanks,
Tony
--
Tony Gray
School of Computing,
University of Tasmania
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.