struggling with port_command()

Gregory Haskins gregory.haskins@REDACTED
Thu Oct 21 22:39:06 CEST 2010

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 ->
	Data ->
	    true = port_command(Port, Data),	    

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]),


cmd_receive(Port, Acc) ->
        {Port, {data, Data}} ->
	    cmd_receive(Port, string:concat(Acc, Data));
        {Port, {exit_status, Status}} ->
	    {ok, Status, Acc};
	Else ->
	    throw({unexpected_msg, Else})

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.


PS: As a secondary question, is the right way to signal EOF on the stdin to the port to use port_close()?
-------------- 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: <>

More information about the erlang-questions mailing list