Output from programs started with open_port

Ulf Norell ulfn@REDACTED
Tue Feb 2 12:04:10 CET 2010


I'm using open_port to communicate with external programs, and I'm having
some problems with the output from these programs. The output I'm interested
in is mainly debug output so I'd prefer to have it echoed to the terminal.
The problem is that on unixy systems (Linux/Mac) newlines are treated just
as line feeds without carriage return and on Windows I'm not getting any
output at all.

Here's a simple example:

% The Erlang code
-module(simple).

-compile(export_all).

test() ->
  Port = open_port({spawn_executable, "./main"}, [{packet, 1}]),
  receive
    {Port, {data, [0]}} ->
      port_close(Port)
  end.

// The external program
#include <stdio.h>

int main (void)
{
  fprintf(stderr, "hello\nworld\nHELLO\n\rWORLD\n\r");
  write(1, "\1\0", 2);
  return 0;
}

On unixy system I get

1> simple:test().
hello
     world
          HELLO
WORLD
true

and on Windows simply

1> simple:test().
true

Having to add \r's when you have the source of the external program is a
nuisance but not the end of the world, but in many cases I don't have the
source code of the external program and in that case the output is pretty
much useless without proper newlines. On Windows it even worse since I don't
get any output at all.

Is this a known issue, and is there anything I can do about it?

/ Ulf


More information about the erlang-questions mailing list