Okay, I spoke of assistance... so, here's my go at it.

The general problem you're facing is that you need some way to tell when the sap program1 finished sending output. As it currently is, it just stops sending stuff, and enters a fgets() call to wait for the next command/input. This is no good, because in your Perl code you cannot tell when to exit the loop reading the response from sap... In other words, you'd need to have some indication that the output ended, for example a blank line (kind of a "prompt" saying new input expected...). A blank line seems okay here, as the program itself doesn't appear to write empty lines — otherwise, you'd have to come up with some other prompt, but note that it has to be terminated by a newline, or else your <SAPR> would return it too late...

A further complication is that sap is not flushing its stdout (which is block-buffered when being run without an interactive terminal). However, as it's open-source, you could easily patch the C source... Look for the input loop within main() (in sap.c, that is), and modify it to read

... for (;;) { // --- add this printf("\n"); // the prompt fflush(stdout); // make sure output gets written // --- if (fi) { if (!fgets(slowo,32,fi)) break; ...

(and recompile the program by calling make ...)

Then, your Perl code could look something like this:

... sub read_sap_response { while (my $line = <SAPR>) { last if $line =~ /^$/; # found "prompt" --> exit loop print $line; # show response } } my $pid = fork; if ($pid) { close PARENTR; close PARENTW; read_sap_response(); # this is needed, because we've patched the program to output # the blank line _before_ waiting for input (which was the # easiest option in this case...) while (1) { open FIFOR, "<fifo"; my $line = <FIFOR>; close FIFOR; print SAPW $line; read_sap_response(); } } else { close SAPR; close SAPW; open STDIN , '<&PARENTR'; open STDOUT, '>&PARENTW'; exec "sap"; die $!; }

That should be it.  At least it works for me :)

___

1 I'm specifically referring to this version, which I could find on the web.


In reply to Re^5: Pipe autoflush by almut
in thread Pipe autoflush by Zielony

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.