[erlang-questions] struggling with port_command()

Gregory Haskins <>
Fri Oct 22 23:00:04 CEST 2010


On Oct 22, 2010, at 11:39 AM, Ahmed Omar wrote:

> not sure why you are doing the "io:get_line" thing
> for example if you commented out pipe stdin thing, run the script to execute ls 
> it works fine

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 ;).

But no bother, I have figured out the issue per my previous reply.  Thanks for looking into it!  Appreciated.

-Greg

> 
> 
> On Fri, Oct 22, 2010 at 4:26 PM, Hynek Vychodil <> wrote:
> On Thu, Oct 21, 2010 at 10:39 PM, Gregory Haskins
> <> wrote:
> > Hi All,
> >
> > I've been struggling all afternoon with what is perhaps a completely trivial problem, but I can't seem to get past it.
> >
> > 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.
> >
> > 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:
> >
> > #!/usr/bin/env escript
> >
> > pipe_stdin(Port) ->
> >    case io:get_line("") of
> >        eof ->
> >            %port_close(Port),
> >            ok;
> >        Data ->
> >            true = port_command(Port, Data),
> >            pipe_stdin(Port)
> >    end.
> >
> > main([Cmd]) ->
> >    io:format("Executing ~s~n", [Cmd]),
> >    Port = erlang:open_port({spawn, Cmd}, [stream, use_stdio, exit_status]),
> >
> >    ok = pipe_stdin(Port),
> >    {ok, _Status, Output} = cmd_receive(Port, ""),
> >
> >    io:format("-----~s~n", [Output]),
> >
> >    ok.
> >
> > cmd_receive(Port, Acc) ->
> >    receive
> >        {Port, {data, Data}} ->
> >            cmd_receive(Port, string:concat(Acc, Data));
> >        {Port, {exit_status, Status}} ->
> >            {ok, Status, Acc};
> >        Else ->
> >            throw({unexpected_msg, Else})
> >    end.
> >
> > 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.
> >
> > -Greg
> >
> > PS: As a secondary question, is the right way to signal EOF on the stdin to the port to use port_close()?
> 
> 1> Port = open_port({spawn, "cat"}, [stream]).
> #Port<0.522>
> 2> port_command(Port, "Hello World\n").
> true
> 3> flush().
> Shell got {#Port<0.522>,{data,{eol,"Hello World\n"}}}
> ok
> 4> port_close(Port).
> true
> 
> Are you sure that data doesn't arrive to your port program? It seems
> that your port program is unable detect end of input data because
> there is not possible send eof (or I don't know how). When you use
> stream option it is up to you implement correct wire protocol. May be
> you have to make some wrapper around your port program.
> 
> --
> --Hynek (Pichi) Vychodil
> 
> Analyze your data in minutes. Share your insights instantly. Thrill
> your boss.  Be a data hero!
> Try GoodData now for free: www.gooddata.com
> 
> ________________________________________________________________
> erlang-questions (at) erlang.org mailing list.
> See http://www.erlang.org/faq.html
> To unsubscribe; mailto:
> 
> 
> 
> 
> -- 
> Best Regards,
> - Ahmed Omar
> http://nl.linkedin.com/in/adiaa
> Follow me on twitter
> @spawn_think
> 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20101022/55fa70f5/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 203 bytes
Desc: This is a digitally signed message part
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20101022/55fa70f5/attachment.bin>


More information about the erlang-questions mailing list