Quantcast

open_pipe({spawn, "cat"}) --- how to close stdin of cat?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

open_pipe({spawn, "cat"}) --- how to close stdin of cat?

C K Tan
Hi all,

I am tryinng to use port to open a program that reads until EOF from
stdin, and then write results to stdout ... i.e. something like the
unix wc command. I can't figure out how to close the stdin of the
spawned process:

wc(InputText) ->
    P = open_port({spawn, "wc"}, [stream, exit_status, use_stdio,
                            stderr_to_stdout, in, out, eof]),
    P ! {self(), {command, "hello world"}},
    P ! {self(), {eof}},   %% ERROR -- how to close stdin of the cat process?
    receive {P, X} -> X end.
       
Also, where can I find detailed documentation on open_port?

Appreciate any help.

Thanks,
-cktan
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: open_pipe({spawn, "cat"}) --- how to close stdin of cat?

Michael Truog
You normally never try to close stdin on the port.  You can close stdin
on the port by calling erlang:port_close/1 but you won't be able to
receive on the port after you close it.  You should be able to use a
read within the port program that does not depend on eof to terminate
the data (see http://erlang.org/doc/tutorial/part_frame.html by the
Ports section).

The documentation for erlang:open_port/2 is at
http://erlang.org/doc/man/erlang.html .  The system commands without a
module name are within the erlang module.  Consider using
erlang:port_command/2 rather than using ! directly, for better behavior.

- Michael

C K Tan wrote:

> Hi all,
>
> I am tryinng to use port to open a program that reads until EOF from
> stdin, and then write results to stdout ... i.e. something like the
> unix wc command. I can't figure out how to close the stdin of the
> spawned process:
>
> wc(InputText) ->
>     P = open_port({spawn, "wc"}, [stream, exit_status, use_stdio,
>                             stderr_to_stdout, in, out, eof]),
>     P ! {self(), {command, "hello world"}},
>     P ! {self(), {eof}},   %% ERROR -- how to close stdin of the cat process?
>     receive {P, X} -> X end.
>
> Also, where can I find detailed documentation on open_port?
>
> Appreciate any help.
>
> Thanks,
> -cktan
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://www.erlang.org/mailman/listinfo/erlang-questions
>
>  

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

Re: open_pipe({spawn, "cat"}) --- how to close stdin of cat?

C K Tan
Michael,

Without closing stdin of wc, wc never returns because it thought there
are more data coming.

wc(InputText) ->
   P = open_port({spawn, "wc"}, [stream, exit_status, use_stdio,
                           stderr_to_stdout, in, out, eof]),
   port_command(P, InputText),
   %% P ! {self(), {eof}},   %% ERROR -- how to close stdin of the wc process?
   receive {P, X} -> X end,
   port_close(P).


wc("hello world").  %% blocks forever.




On Sun, Mar 1, 2009 at 2:18 AM, Michael Truog <[hidden email]> wrote:

> You normally never try to close stdin on the port.  You can close stdin on
> the port by calling erlang:port_close/1 but you won't be able to receive on
> the port after you close it.  You should be able to use a read within the
> port program that does not depend on eof to terminate the data (see
> http://erlang.org/doc/tutorial/part_frame.html by the Ports section).
>
> The documentation for erlang:open_port/2 is at
> http://erlang.org/doc/man/erlang.html .  The system commands without a
> module name are within the erlang module.  Consider using
> erlang:port_command/2 rather than using ! directly, for better behavior.
>
> - Michael
>
> C K Tan wrote:
>>
>> Hi all,
>>
>> I am tryinng to use port to open a program that reads until EOF from
>> stdin, and then write results to stdout ... i.e. something like the
>> unix wc command. I can't figure out how to close the stdin of the
>> spawned process:
>>
>> wc(InputText) ->
>>    P = open_port({spawn, "wc"}, [stream, exit_status, use_stdio,
>>                            stderr_to_stdout, in, out, eof]),
>>    P ! {self(), {command, "hello world"}},
>>    P ! {self(), {eof}},   %% ERROR -- how to close stdin of the cat
>> process?
>>    receive {P, X} -> X end.
>>
>> Also, where can I find detailed documentation on open_port?
>>
>> Appreciate any help.
>>
>> Thanks,
>> -cktan
>> _______________________________________________
>> erlang-questions mailing list
>> [hidden email]
>> http://www.erlang.org/mailman/listinfo/erlang-questions
>>
>>
>
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: open_pipe({spawn, "cat"}) --- how to close stdin of cat?

Anthony Shipman
On Sun, 1 Mar 2009 09:48:36 pm C K Tan wrote:

> Michael,
>
> Without closing stdin of wc, wc never returns because it thought there
> are more data coming.
>
> wc(InputText) ->
>    P = open_port({spawn, "wc"}, [stream, exit_status, use_stdio,
>                            stderr_to_stdout, in, out, eof]),
>    port_command(P, InputText),
>    %% P ! {self(), {eof}},   %% ERROR -- how to close stdin of the wc
> process? receive {P, X} -> X end,
>    port_close(P).

What I have done is use an intermediate shell script. I write EOF or some
similar marker to the port and the shell script closes the stdin for the
command it controls.

--
Anthony Shipman                    Mamas don't let your babies
[hidden email]                   grow up to be outsourced.
_______________________________________________
erlang-questions mailing list
[hidden email]
http://www.erlang.org/mailman/listinfo/erlang-questions
Loading...