"Port reading stdout from external program"

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

"Port reading stdout from external program"

Alex Feng
Hi,

I am using an port to communicate with an external program, I would like to fetch each output from the external problem.
But I have problem to fetch the output when the external problem runs into waiting input," scanf " for example.

Here is an example to communicate with a simple C program.

I need to fetch the prompt line("Please input you name") from Port in order to interact with C code,  but with "scanf" added in C code, I couldn't fetch it from Port.
Does anyone know why is that ?  Any advice and suggestions will be greatly appreciated.



Erlang code:

read() ->
  Port = open_port({spawn, "/home/erlang/test/a.out"}, [binary, {line, 255}]), %binary,{line, 255}
  io:format("Port: ~p~n",[Port]),
  do_read(Port).

do_read(Port) ->
  receive
    {Port,{data,Data}} ->
        io:format("Data: ~p~n",[Data]);
    Any ->
      io:format("No match fifo_client:do_read/1, ~p~n",[Any])
  end,
  do_read(Port).

C code:

int main()
{

  char str[10];

  printf("Please input you name:");  //Prompt line

  scanf ("%9s",str);  // with this line, port can not read the output from this program.

  return 1;
}


Br,
Alex

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: "Port reading stdout from external program"

Stanislaw Klekot
On Wed, Nov 16, 2016 at 08:06:03PM +0100, Alex Feng wrote:

> Hi,
>
> I am using an port to communicate with an external program, I would like to
> fetch each output from the external problem.
> But I have problem to fetch the output when the external problem runs into
> waiting input," scanf " for example.
>
> Here is an example to communicate with a simple C program.
>
> I need to fetch the prompt line("Please input you name") from Port in order
> to interact with C code,  but with "scanf" added in C code, I couldn't
> fetch it from Port.
> Does anyone know why is that ?  Any advice and suggestions will be greatly
> appreciated.
[...]

> C code:
>
> int main()
> {
>
>   char str[10];
>
>   printf("Please input you name:");  //Prompt line
>
>   scanf ("%9s",str);  // with this line, port can not read the output from this program.
>
>   return 1;
> }

It's because STDOUT buffering differs between running things in terminal
directly and running them with STDOUT redirected somewhere. You can see
it yourself by first running this "a.out" in shell, and then running it
redirected to cat: "a.out | cat". In the latter case you won't get
prompt.

You need to call fflush() to flush anything buffered by printf() (see
its man page for details).

--
Stanislaw Klekot
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: "Port reading stdout from external program"

Frank Muller
Use erlexec in "async" mode instead:
https://github.com/saleyn/erlexec


Le mer. 16 nov. 2016 à 20:43, Stanislaw Klekot <[hidden email]> a écrit :
On Wed, Nov 16, 2016 at 08:06:03PM +0100, Alex Feng wrote:
> Hi,
>
> I am using an port to communicate with an external program, I would like to
> fetch each output from the external problem.
> But I have problem to fetch the output when the external problem runs into
> waiting input," scanf " for example.
>
> Here is an example to communicate with a simple C program.
>
> I need to fetch the prompt line("Please input you name") from Port in order
> to interact with C code,  but with "scanf" added in C code, I couldn't
> fetch it from Port.
> Does anyone know why is that ?  Any advice and suggestions will be greatly
> appreciated.
[...]
> C code:
>
> int main()
> {
>
>   char str[10];
>
>   printf("Please input you name:");  //Prompt line
>
>   scanf ("%9s",str);  // with this line, port can not read the output from this program.
>
>   return 1;
> }

It's because STDOUT buffering differs between running things in terminal
directly and running them with STDOUT redirected somewhere. You can see
it yourself by first running this "a.out" in shell, and then running it
redirected to cat: "a.out | cat". In the latter case you won't get
prompt.

You need to call fflush() to flush anything buffered by printf() (see
its man page for details).

--
Stanislaw Klekot
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions