caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* commands.getoutput () in ocaml?
@ 2007-08-22 19:56 Luca de Alfaro
  2007-08-22 20:05 ` [Caml-list] " malc
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Luca de Alfaro @ 2007-08-22 19:56 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 366 bytes --]

Dear All,

I am looking for a quick way to do the equivalent of

s = commands.getoutput ("ls " + name + "*")

in Ocaml.

Unix.system does perform a call, but the output of the call is not
returned... yes, I can redirect it to /tmp/output.ocaml and then read the
file, or even go crazy and build pipes, but is there a more elegant (read:
succint) way to do it?

Luca

[-- Attachment #2: Type: text/html, Size: 429 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 19:56 commands.getoutput () in ocaml? Luca de Alfaro
@ 2007-08-22 20:05 ` malc
  2007-08-22 20:10   ` malc
  2007-08-22 20:11 ` Dave Benjamin
  2007-08-22 20:17 ` Brian Hurt
  2 siblings, 1 reply; 14+ messages in thread
From: malc @ 2007-08-22 20:05 UTC (permalink / raw)
  To: Luca de Alfaro; +Cc: caml-list

On Wed, 22 Aug 2007, Luca de Alfaro wrote:

> Dear All,
>
> I am looking for a quick way to do the equivalent of
>
> s = commands.getoutput ("ls " + name + "*")
>
> in Ocaml.
>
> Unix.system does perform a call, but the output of the call is not
> returned... yes, I can redirect it to /tmp/output.ocaml and then read the
> file, or even go crazy and build pipes, but is there a more elegant (read:
> succint) way to do it?

I needed something to that effect yesterday and wrote following:

open Unix;;

let getprocout cmd =
   let ic = open_process_in cmd in
   let b = Buffer.create 10 in
   let rec loop () =
     match try Some (input_line ic) with End_of_file -> None with
       | None -> Buffer.contents b
       | Some line ->
           Buffer.add_string b line;
           loop ()
   in
     loop ()
;;

-- 
vale


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 20:05 ` [Caml-list] " malc
@ 2007-08-22 20:10   ` malc
  0 siblings, 0 replies; 14+ messages in thread
From: malc @ 2007-08-22 20:10 UTC (permalink / raw)
  To: Luca de Alfaro; +Cc: caml-list

On Thu, 23 Aug 2007, malc wrote:

> On Wed, 22 Aug 2007, Luca de Alfaro wrote:
>
>> Dear All,

[..snip..]

>
> open Unix;;
>
> let getprocout cmd =
[..snip..]
> ;;

This does not include closing of input channel and processing of
status. So Unix.close_process_in ought to be utilized somewhere.
Sorry about that.

-- 
vale


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 19:56 commands.getoutput () in ocaml? Luca de Alfaro
  2007-08-22 20:05 ` [Caml-list] " malc
@ 2007-08-22 20:11 ` Dave Benjamin
  2007-08-22 21:32   ` Luca de Alfaro
                     ` (2 more replies)
  2007-08-22 20:17 ` Brian Hurt
  2 siblings, 3 replies; 14+ messages in thread
From: Dave Benjamin @ 2007-08-22 20:11 UTC (permalink / raw)
  To: Luca de Alfaro; +Cc: caml-list

On Wed, 22 Aug 2007, Luca de Alfaro wrote:

> I am looking for a quick way to do the equivalent of
> s = commands.getoutput ("ls " + name + "*")
> in Ocaml.

I have translated many of the examples from the Perl Cookbook for the 
Process Management and Communication chapter of the OCaml PLEAC. This is 
one of the topics covered.

http://pleac.sourceforge.net/pleac_ocaml/processmanagementetc.html

Dave


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 19:56 commands.getoutput () in ocaml? Luca de Alfaro
  2007-08-22 20:05 ` [Caml-list] " malc
  2007-08-22 20:11 ` Dave Benjamin
@ 2007-08-22 20:17 ` Brian Hurt
  2 siblings, 0 replies; 14+ messages in thread
From: Brian Hurt @ 2007-08-22 20:17 UTC (permalink / raw)
  To: Luca de Alfaro; +Cc: caml-list

Luca de Alfaro wrote:

> Dear All,
>
> I am looking for a quick way to do the equivalent of
>
> s = commands.getoutput ("ls " + name + "*")
>
> in Ocaml.
>
> Unix.system does perform a call, but the output of the call is not 
> returned... yes, I can redirect it to /tmp/output.ocaml and then read 
> the file, or even go crazy and build pipes, but is there a more 
> elegant (read: succint) way to do it?
>
> Luca
>
If what you want is to spawn a process and redirect the I/O, the 
standard library is short on convience functions- in this way Ocaml is 
more similiar to C++/Java than it is Perl/Python/Ruby.  Although the 
convience functions are not that hard to write.  If you want to read a 
directory, you might want to look at Unix.opendir, Unix.readdir, etc.

Brian


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 20:11 ` Dave Benjamin
@ 2007-08-22 21:32   ` Luca de Alfaro
  2007-08-22 22:11     ` Yitzhak Mandelbaum
  2007-08-22 22:13     ` Daniel Bünzli
  2007-08-22 22:35   ` Olivier Andrieu
  2007-08-23 15:24   `  Dr. Axel Poigné 
  2 siblings, 2 replies; 14+ messages in thread
From: Luca de Alfaro @ 2007-08-22 21:32 UTC (permalink / raw)
  To: Dave Benjamin; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 646 bytes --]

Thanks, this is truly wonderful!

I wish more of the things we are discussing and discovering in this email
list would make it to the official Ocaml site...

Luca

On 8/22/07, Dave Benjamin <dave@ramenlabs.com> wrote:
>
> On Wed, 22 Aug 2007, Luca de Alfaro wrote:
>
> > I am looking for a quick way to do the equivalent of
> > s = commands.getoutput ("ls " + name + "*")
> > in Ocaml.
>
> I have translated many of the examples from the Perl Cookbook for the
> Process Management and Communication chapter of the OCaml PLEAC. This is
> one of the topics covered.
>
> http://pleac.sourceforge.net/pleac_ocaml/processmanagementetc.html
>
> Dave
>

[-- Attachment #2: Type: text/html, Size: 1068 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 21:32   ` Luca de Alfaro
@ 2007-08-22 22:11     ` Yitzhak Mandelbaum
  2007-08-22 22:13     ` Daniel Bünzli
  1 sibling, 0 replies; 14+ messages in thread
From: Yitzhak Mandelbaum @ 2007-08-22 22:11 UTC (permalink / raw)
  To: Luca de Alfaro; +Cc: Dave Benjamin, caml-list

[-- Attachment #1: Type: text/plain, Size: 1553 bytes --]

Perhaps its time for a stronger community process. A discussion about  
this was started here a few weeks ago but it fizzled out pretty  
quickly.  It seems we need to find a balance between the best  
interests of the ocaml developer community and the best interests of  
ocaml compiler developers.

Is anybody familiar with the Java community process and how that  
worked out?

Cheers,
Yitzhak

On Aug 22, 2007, at 5:32 PM, Luca de Alfaro wrote:

> Thanks, this is truly wonderful!
>
> I wish more of the things we are discussing and discovering in this  
> email list would make it to the official Ocaml site...
>
> Luca
>
> On 8/22/07, Dave Benjamin <dave@ramenlabs.com> wrote:
> On Wed, 22 Aug 2007, Luca de Alfaro wrote:
>
> > I am looking for a quick way to do the equivalent of
> > s = commands.getoutput ("ls " + name + "*")
> > in Ocaml.
>
> I have translated many of the examples from the Perl Cookbook for the
> Process Management and Communication chapter of the OCaml PLEAC.  
> This is
> one of the topics covered.
>
> http://pleac.sourceforge.net/pleac_ocaml/processmanagementetc.html
>
> Dave
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs

--------------------------------------------------
Yitzhak Mandelbaum
AT&T Labs - Research

http://www.research.att.com/~yitzhak



[-- Attachment #2: Type: text/html, Size: 5079 bytes --]

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 21:32   ` Luca de Alfaro
  2007-08-22 22:11     ` Yitzhak Mandelbaum
@ 2007-08-22 22:13     ` Daniel Bünzli
  1 sibling, 0 replies; 14+ messages in thread
From: Daniel Bünzli @ 2007-08-22 22:13 UTC (permalink / raw)
  To: Luca de Alfaro; +Cc: caml-list


Le 22 août 07 à 23:32, Luca de Alfaro a écrit :

> I wish more of the things we are discussing and discovering in this  
> email list would make it to the official Ocaml site...

Feel free to add what you discover to this page :

http://cocan.org/faqs_and_programming_guidelines

Best,

Daniel


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 20:11 ` Dave Benjamin
  2007-08-22 21:32   ` Luca de Alfaro
@ 2007-08-22 22:35   ` Olivier Andrieu
  2007-08-22 23:04     ` Eric Cooper
  2007-08-23 15:24   `  Dr. Axel Poigné 
  2 siblings, 1 reply; 14+ messages in thread
From: Olivier Andrieu @ 2007-08-22 22:35 UTC (permalink / raw)
  To: Dave Benjamin; +Cc: Luca de Alfaro, caml-list

On 8/22/07, Dave Benjamin <dave@ramenlabs.com> wrote:
> On Wed, 22 Aug 2007, Luca de Alfaro wrote:
>
> > I am looking for a quick way to do the equivalent of
> > s = commands.getoutput ("ls " + name + "*")
> > in Ocaml.
>
> I have translated many of the examples from the Perl Cookbook for the
> Process Management and Communication chapter of the OCaml PLEAC. This is
> one of the topics covered.
>
> http://pleac.sourceforge.net/pleac_ocaml/processmanagementetc.html

about this function:

(* Run a command and return its results as a string. *)
let read_process command =
  let buffer_size = 2048 in
  let buffer = Buffer.create buffer_size in
  let string = String.create buffer_size in
  let in_channel = Unix.open_process_in command in
  let chars_read = ref 1 in
  while !chars_read <> 0 do
    chars_read := input in_channel string 0 buffer_size;
    Buffer.add_string buffer (String.sub string 0 !chars_read)
  done;
  close_in in_channel;
  Buffer.contents buffer

you could use Buffer.add_substring instead of add_string, that will
avoid an intermediate copy of the string

... and you should be using Unix.close_process_in, not close_in
otherwise the subprocess will stay as a zombie.

-- 
  Olivier


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 23:04     ` Eric Cooper
@ 2007-08-22 22:52       ` Dave Benjamin
  2007-08-23  4:03       ` Dave Benjamin
  1 sibling, 0 replies; 14+ messages in thread
From: Dave Benjamin @ 2007-08-22 22:52 UTC (permalink / raw)
  To: Eric Cooper; +Cc: caml-list

On Wed, 22 Aug 2007, Eric Cooper wrote:

> On Thu, Aug 23, 2007 at 12:35:14AM +0200, Olivier Andrieu wrote:
>> you could use Buffer.add_substring instead of add_string, that will
>> avoid an intermediate copy of the string
>
> And you can use Buffer.add_channel to avoid intermediate strings
> altogether (until the final Buffer.to_string).

Thanks, guys! I really appreciate the feedback, and will make these 
suggested improvements (though it may take awhile to show up on the site). 
If you find any other mistakes, please let me know.

Dave


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 22:35   ` Olivier Andrieu
@ 2007-08-22 23:04     ` Eric Cooper
  2007-08-22 22:52       ` Dave Benjamin
  2007-08-23  4:03       ` Dave Benjamin
  0 siblings, 2 replies; 14+ messages in thread
From: Eric Cooper @ 2007-08-22 23:04 UTC (permalink / raw)
  To: caml-list

On Thu, Aug 23, 2007 at 12:35:14AM +0200, Olivier Andrieu wrote:
> you could use Buffer.add_substring instead of add_string, that will
> avoid an intermediate copy of the string

And you can use Buffer.add_channel to avoid intermediate strings
altogether (until the final Buffer.to_string).

-- 
Eric Cooper             e c c @ c m u . e d u


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 23:04     ` Eric Cooper
  2007-08-22 22:52       ` Dave Benjamin
@ 2007-08-23  4:03       ` Dave Benjamin
  2007-08-23 17:07         ` Eric Cooper
  1 sibling, 1 reply; 14+ messages in thread
From: Dave Benjamin @ 2007-08-23  4:03 UTC (permalink / raw)
  To: Eric Cooper; +Cc: caml-list

On Wed, 22 Aug 2007, Eric Cooper wrote:

> On Thu, Aug 23, 2007 at 12:35:14AM +0200, Olivier Andrieu wrote:
>> you could use Buffer.add_substring instead of add_string, that will
>> avoid an intermediate copy of the string
>
> And you can use Buffer.add_channel to avoid intermediate strings
> altogether (until the final Buffer.to_string).

I'm not quite sure how to do this, actually. Here's what I've got so far:

let read_process command =
   let buffer_size = 2048 in
   let buffer = Buffer.create buffer_size in
   let in_channel = Unix.open_process_in command in
   begin
     try
       while true do
         Buffer.add_channel buffer in_channel buffer_size;
       done
     with
       | End_of_file -> ()
       | e ->
           ignore (Unix.close_process_in in_channel);
           raise e;
   end;
   ignore (Unix.close_process_in in_channel);
   Buffer.contents buffer

However, this doesn't work unless I set buffer_size to 1. Otherwise, the 
End_of_file fires before all the data is read in, so I either get nothing 
or an incomplete result.

Any advice?

Thanks,
Dave


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-22 20:11 ` Dave Benjamin
  2007-08-22 21:32   ` Luca de Alfaro
  2007-08-22 22:35   ` Olivier Andrieu
@ 2007-08-23 15:24   `  Dr. Axel Poigné 
  2 siblings, 0 replies; 14+ messages in thread
From:  Dr. Axel Poigné  @ 2007-08-23 15:24 UTC (permalink / raw)
  To: caml-list

>
>
>> I am looking for a quick way to do the equivalent of
>> s = commands.getoutput ("ls " + name + "*")
>> in Ocaml.
>>
>
> I have translated many of the examples from the Perl Cookbook for  
> the Process Management and Communication chapter of the OCaml  
> PLEAC. This is one of the topics covered.
>
> http://pleac.sourceforge.net/pleac_ocaml/processmanagementetc.html
>
> Dave
>

There is a problem with the read_process_lines function though: if  
the output buffer gets too long the process does not return to close  
the input channel. Consequence is that the spawning process is blocked.

That's a problem I do not know how to solve.

Axel


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [Caml-list] commands.getoutput () in ocaml?
  2007-08-23  4:03       ` Dave Benjamin
@ 2007-08-23 17:07         ` Eric Cooper
  0 siblings, 0 replies; 14+ messages in thread
From: Eric Cooper @ 2007-08-23 17:07 UTC (permalink / raw)
  To: caml-list

On Wed, Aug 22, 2007 at 11:03:42PM -0500, Dave Benjamin wrote:
> However, this doesn't work unless I set buffer_size to 1. Otherwise, the 
> End_of_file fires before all the data is read in, so I either get nothing 
> or an incomplete result.

Since you can't use Pervasives.in_channel_length on a pipe, the only
way I see is to write a special add_channel_tail function, called only
when you catch the first Eof, that tries successively to add_channel
n, n-1, n-2, ... etc. bytes until it succeeds. Sigh.

-- 
Eric Cooper             e c c @ c m u . e d u


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2007-08-23 17:07 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-22 19:56 commands.getoutput () in ocaml? Luca de Alfaro
2007-08-22 20:05 ` [Caml-list] " malc
2007-08-22 20:10   ` malc
2007-08-22 20:11 ` Dave Benjamin
2007-08-22 21:32   ` Luca de Alfaro
2007-08-22 22:11     ` Yitzhak Mandelbaum
2007-08-22 22:13     ` Daniel Bünzli
2007-08-22 22:35   ` Olivier Andrieu
2007-08-22 23:04     ` Eric Cooper
2007-08-22 22:52       ` Dave Benjamin
2007-08-23  4:03       ` Dave Benjamin
2007-08-23 17:07         ` Eric Cooper
2007-08-23 15:24   `  Dr. Axel Poigné 
2007-08-22 20:17 ` Brian Hurt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).