<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><br><div><div>On Oct 22, 2010, at 11:39 AM, Ahmed Omar wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">not sure why you are doing the "io:get_line" thing<div>for example if you commented out pipe stdin thing, run the script to execute ls </div><div>it works fine</div></blockquote><div><br></div><div>Ah, that was just to feed data into the escript, which would then try to pass it along a port connection.  If you comment out the pipe_stdin, the script doesn't have a purpose ;).</div><div><br></div><div>But no bother, I have figured out the issue per my previous reply.  Thanks for looking into it!  Appreciated.</div><div><br></div><div>-Greg</div><br><blockquote type="cite"><div><br></div><div><br><div class="gmail_quote">
On Fri, Oct 22, 2010 at 4:26 PM, Hynek Vychodil <span dir="ltr"><<a href="mailto:hynek@gooddata.com">hynek@gooddata.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div></div><div class="h5">On Thu, Oct 21, 2010 at 10:39 PM, Gregory Haskins<br>
<<a href="mailto:gregory.haskins@gmail.com">gregory.haskins@gmail.com</a>> wrote:<br>
> Hi All,<br>
><br>
> I've been struggling all afternoon with what is perhaps a completely trivial problem, but I can't seem to get past it.<br>
><br>
> Ultimately, I want to be able to have a simple external port program transform a string (XML, in fact, but that shouldn't matter).  So I want to have my erlang program pump a string in on stdin, and then get a string back out on stdout.  Simple stuff, but it just will not work for me.<br>

><br>
> I scoured the net for examples and/or threads on problems in doing this, but was unable to figure out what I am doing wrong.  Here is an escript which exemplifies the issue:<br>
><br>
> #!/usr/bin/env escript<br>
><br>
> pipe_stdin(Port) -><br>
>    case io:get_line("") of<br>
>        eof -><br>
>            %port_close(Port),<br>
>            ok;<br>
>        Data -><br>
>            true = port_command(Port, Data),<br>
>            pipe_stdin(Port)<br>
>    end.<br>
><br>
> main([Cmd]) -><br>
>    io:format("Executing ~s~n", [Cmd]),<br>
>    Port = erlang:open_port({spawn, Cmd}, [stream, use_stdio, exit_status]),<br>
><br>
>    ok = pipe_stdin(Port),<br>
>    {ok, _Status, Output} = cmd_receive(Port, ""),<br>
><br>
>    io:format("-----~s~n", [Output]),<br>
><br>
>    ok.<br>
><br>
> cmd_receive(Port, Acc) -><br>
>    receive<br>
>        {Port, {data, Data}} -><br>
>            cmd_receive(Port, string:concat(Acc, Data));<br>
>        {Port, {exit_status, Status}} -><br>
>            {ok, Status, Acc};<br>
>        Else -><br>
>            throw({unexpected_msg, Else})<br>
>    end.<br>
><br>
> The problem I am seeing is that the data never seems to reach the port program (with or without the port_close()).  I pump the data in with port_command() and it returns "true" but nothing seems to come out the other side.  Any help would be greatly appreciated.<br>

><br>
> -Greg<br>
><br>
> PS: As a secondary question, is the right way to signal EOF on the stdin to the port to use port_close()?<br>
<br>
</div></div>1> Port = open_port({spawn, "cat"}, [stream]).<br>
#Port<0.522><br>
2> port_command(Port, "Hello World\n").<br>
true<br>
3> flush().<br>
Shell got {#Port<0.522>,{data,{eol,"Hello World\n"}}}<br>
ok<br>
4> port_close(Port).<br>
true<br>
<br>
Are you sure that data doesn't arrive to your port program? It seems<br>
that your port program is unable detect end of input data because<br>
there is not possible send eof (or I don't know how). When you use<br>
stream option it is up to you implement correct wire protocol. May be<br>
you have to make some wrapper around your port program.<br>
<br>
--<br>
--Hynek (Pichi) Vychodil<br>
<br>
Analyze your data in minutes. Share your insights instantly. Thrill<br>
your boss.  Be a data hero!<br>
Try GoodData now for free: <a href="http://www.gooddata.com/" target="_blank">www.gooddata.com</a><br>
<br>
________________________________________________________________<br>
erlang-questions (at) <a href="http://erlang.org/" target="_blank">erlang.org</a> mailing list.<br>
See <a href="http://www.erlang.org/faq.html" target="_blank">http://www.erlang.org/faq.html</a><br>
To unsubscribe; mailto:<a href="mailto:erlang-questions-unsubscribe@erlang.org">erlang-questions-unsubscribe@erlang.org</a><br>
<br>
</blockquote></div><br><br clear="all"><br>-- <br>Best Regards,<br>- Ahmed Omar<div><a href="http://nl.linkedin.com/in/adiaa" target="_blank">http://nl.linkedin.com/in/adiaa</a></div><div>Follow me on twitter</div><div><a href="http://twitter.com/#!/spawn_think" target="_blank">@spawn_think</a></div>
<br>
</div>
</blockquote></div><br></body></html>