caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* Camlp4 in a Unix pipe
@ 2005-11-17 14:20 Alessandro Baretta
  2005-11-21  9:39 ` [Caml-list] " Hendrik Tews
  0 siblings, 1 reply; 8+ messages in thread
From: Alessandro Baretta @ 2005-11-17 14:20 UTC (permalink / raw)
  To: Ocaml

I'm musing a little with camlp4. Specifically, I'm trying to get it to 
preprocess input written according to some syntax extension and output the 
result. Of course, since I'm just playing, I'd like to do this in type-eval-loop 
kind of interface. I tried typing the following command in my shell:

$ cat - | camlp4 pa_o.cmo pa_extend.cmo pr_o.cmo pa_openin.cmo

Camlp4 dies miserably discarding it's standard input. So then I tried the following:

cat - | camlp4 pa_o.cmo pa_extend.cmo pr_o.cmo pa_openin.cmo -impl /dev/stdin

Again, camlp4 dies miserably with the following error message:
I/O error: Illegal seek

Short of writing my own Pr_stdout module, is there any way to achieve the result 
of interactively getting camlp4 to preprocess some code and print it back to stdout?

Alex


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

* Re: [Caml-list] Camlp4 in a Unix pipe
  2005-11-17 14:20 Camlp4 in a Unix pipe Alessandro Baretta
@ 2005-11-21  9:39 ` Hendrik Tews
  2005-11-22 16:42   ` Alessandro Baretta
  0 siblings, 1 reply; 8+ messages in thread
From: Hendrik Tews @ 2005-11-21  9:39 UTC (permalink / raw)
  To: Ocaml

Alessandro Baretta <a.baretta@barettadeit.com> writes:

   Short of writing my own Pr_stdout module, is there any way to achieve
   the result of interactively getting camlp4 to preprocess some code and
   print it back to stdout?
   
cat - | camlp4 pa_o.cmo pr_r.cmo -impl -
let f = function | [] -> 0 | _ -> 1;;
<CONTROL-D>
value f =
  fun
  [ [] -> 0
  | _ -> 1 ]
;


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

* Re: [Caml-list] Camlp4 in a Unix pipe
  2005-11-21  9:39 ` [Caml-list] " Hendrik Tews
@ 2005-11-22 16:42   ` Alessandro Baretta
  2005-11-22 18:15     ` Florian Hars
                       ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Alessandro Baretta @ 2005-11-22 16:42 UTC (permalink / raw)
  To: Hendrik Tews; +Cc: Ocaml

Hendrik Tews wrote:
> Alessandro Baretta <a.baretta@barettadeit.com> writes:
> 
>    Short of writing my own Pr_stdout module, is there any way to achieve
>    the result of interactively getting camlp4 to preprocess some code and
>    print it back to stdout?
>    
> cat - | camlp4 pa_o.cmo pr_r.cmo -impl -

Cool command line! How did you discover the '-' option? As far as I can see it 
is not documented.

Alex


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

* Re: [Caml-list] Camlp4 in a Unix pipe
  2005-11-22 16:42   ` Alessandro Baretta
@ 2005-11-22 18:15     ` Florian Hars
  2005-11-23 11:27       ` Alessandro Baretta
  2005-11-22 22:34     ` Christophe Raffalli
  2005-11-23  9:05     ` Hendrik Tews
  2 siblings, 1 reply; 8+ messages in thread
From: Florian Hars @ 2005-11-22 18:15 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: Hendrik Tews, Ocaml

Alessandro Baretta wrote:
> Cool command line! How did you discover the '-' option? 

Out of habit? In the the unix shell monad, "foo >>= bar" happens to be written
as "foo | bar -" ("return" is written "cat", in case you wondered. So "Hey,
that is a useless use of cat!" really means 'Hey, you ignored the monad laws!")

Yours, Florian (time to go home, I know).


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

* Re: [Caml-list] Camlp4 in a Unix pipe
  2005-11-22 16:42   ` Alessandro Baretta
  2005-11-22 18:15     ` Florian Hars
@ 2005-11-22 22:34     ` Christophe Raffalli
  2005-11-23  9:05     ` Hendrik Tews
  2 siblings, 0 replies; 8+ messages in thread
From: Christophe Raffalli @ 2005-11-22 22:34 UTC (permalink / raw)
  To: Alessandro Baretta; +Cc: Hendrik Tews, Ocaml

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

Alessandro Baretta a écrit :
> Hendrik Tews wrote:
> 
>> Alessandro Baretta <a.baretta@barettadeit.com> writes:
>>
>>    Short of writing my own Pr_stdout module, is there any way to achieve
>>    the result of interactively getting camlp4 to preprocess some code and
>>    print it back to stdout?
>>    cat - | camlp4 pa_o.cmo pr_r.cmo -impl -
> 
> 
> Cool command line! How did you discover the '-' option? As far as I can
> see it is not documented.
> 

this is traditional, "-" replaces a filename by stdin or stdout
depending upon the context ... so it is always worth a try !!

> Alex
> 
> _______________________________________________
> 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


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 894 bytes --]

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

* Re: [Caml-list] Camlp4 in a Unix pipe
  2005-11-22 16:42   ` Alessandro Baretta
  2005-11-22 18:15     ` Florian Hars
  2005-11-22 22:34     ` Christophe Raffalli
@ 2005-11-23  9:05     ` Hendrik Tews
  2 siblings, 0 replies; 8+ messages in thread
From: Hendrik Tews @ 2005-11-23  9:05 UTC (permalink / raw)
  To: Ocaml

Alessandro Baretta <a.baretta@barettadeit.com> writes:

   Cool command line! How did you discover the '-' option? As far as I
   can see it is not documented.
   
I believe I first tried and later found the following line 

    let ic = if name = "-" then stdin else open_in_bin name in

(in function parse_file in camlp4/camlp4/argl.ml) but maybe it
was the other way round.

Bye,

Hendrik


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

* Re: [Caml-list] Camlp4 in a Unix pipe
  2005-11-22 18:15     ` Florian Hars
@ 2005-11-23 11:27       ` Alessandro Baretta
  2005-11-24 11:30         ` Hendrik Tews
  0 siblings, 1 reply; 8+ messages in thread
From: Alessandro Baretta @ 2005-11-23 11:27 UTC (permalink / raw)
  To: Florian Hars; +Cc: Hendrik Tews, Ocaml

Florian Hars wrote:
> Alessandro Baretta wrote:
> 
>> Cool command line! How did you discover the '-' option? 
> 
> 
> Out of habit? In the the unix shell monad, "foo >>= bar" happens to be 
> written
> as "foo | bar -" ("return" is written "cat", in case you wondered. So "Hey,
> that is a useless use of cat!" really means 'Hey, you ignored the monad 
> laws!")
> 
> Yours, Florian (time to go home, I know).
> 

I'm not sure I understand your point.

My original attempt at this problem was the following:

cat - | camlp4 pa_o.cmo pr_o.cmo

but it did not work: here's what happens.

alex@alex:~/void$ cat - | camlp4 pa_o.cmo pr_r.cmo
let 1 = 1;;
alex@alex:~/void$

Nothing at all. This seemed strange to me, because, according the camlp4 manpage

> DESCRIPTION
>        camlp4  is  a  Pre-Processor-Pretty-Printer for OCaml, parsing a source
>        file and printing some result on standard output.

I would expect to see the result of camlp4 actions somewhere. But, apparently, 
although camlp4 uses stdout as its default output descriptor, it does not use 
stdin as its default input descriptor. Hence, the following attempt:

alex@alex:~/void$ cat - | camlp4 pa_o.cmo pr_r.cmo -impl /dev/stdin
let 1 = 1;;
I/O error: Illegal seek
alex@alex:~/void$

Here, I tried to force camlp4 to explicitely use stdin as its input file. I have 
no clue as why campl4 would use the Unix.seek syscall on this descriptor, but 
apparently it does, and this fails if it is a pipe. Given the described 
experience, I am completely bewildered by the fact that Hendrik's proposal 
works, yet it does.

alex@alex:~/void$ cat - | camlp4 pa_o.cmo pr_r.cmo -impl -
let 1 = 1;;
value _ = match 1 with [ 1 -> () ];
alex@alex:~/void$

The '-' option to camlp4 is undocumented, as far as I can see from the camlp4 
manpage, so I was asking Hendrik to explain to me where he found this clue. 
Also, if '-' means to read from stdin, as is the case with many command line 
tools, why should this work any better than reading from /dev/stdin?

Alex


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

* Re: [Caml-list] Camlp4 in a Unix pipe
  2005-11-23 11:27       ` Alessandro Baretta
@ 2005-11-24 11:30         ` Hendrik Tews
  0 siblings, 0 replies; 8+ messages in thread
From: Hendrik Tews @ 2005-11-24 11:30 UTC (permalink / raw)
  To: Ocaml

Alessandro Baretta <a.baretta@barettadeit.com> writes:

   I'm not sure I understand your point.
   
   My original attempt at this problem was the following:
   
   cat - | camlp4 pa_o.cmo pr_o.cmo
   
If you don't set a filename nothing is done, see function go in
camlp4/camlp4/argl.ml: 

      if Pcaml.input_file.val <> "" then
        match file_kind.val with
        [ Intf -> process_intf ()
        | Impl -> process_impl () ]
      else ()

(the input_file reference is set via -intf, -impl and from
arguments ending in .ml or .mli)

   This seemed strange to me, because, according the camlp4 manpage
   
Agreed, the docs could be more precise. But all the camlp4 docs
are quite outdated.

   alex@alex:~/void$ cat - | camlp4 pa_o.cmo pr_r.cmo -impl /dev/stdin
   let 1 = 1;;
   I/O error: Illegal seek

pr_r and pr_o contain seek's to copy parts of source (for
instance comments) into the output. It will work if you use
another printer.

   alex@alex:~/void$ cat - | camlp4 pa_o.cmo pr_r.cmo -impl -
   
   The '-' option to camlp4 is undocumented, as far as I can see from the

I wouldn't say '-' is an option, rather it is a special filename,
see parse_file in camlp4/camlp4/argl.ml: 

  let name = Pcaml.input_file.val in
  do {
    Pcaml.warning.val := print_warning;
    let ic = if name = "-" then stdin else open_in_bin name in


Alessandro Baretta continues:

   found this clue. Also, if '-' means to read from stdin, as is the case
   with many command line tools, why should this work any better than

Because the offending seeks are covered by if's, like in
apply_printer, camlp4/etc/pr_o.ml:

  if Pcaml.input_file.val <> "-" && Pcaml.input_file.val <> "" then do {

Bye,

Hendrik


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

end of thread, other threads:[~2005-11-24 11:30 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-11-17 14:20 Camlp4 in a Unix pipe Alessandro Baretta
2005-11-21  9:39 ` [Caml-list] " Hendrik Tews
2005-11-22 16:42   ` Alessandro Baretta
2005-11-22 18:15     ` Florian Hars
2005-11-23 11:27       ` Alessandro Baretta
2005-11-24 11:30         ` Hendrik Tews
2005-11-22 22:34     ` Christophe Raffalli
2005-11-23  9:05     ` Hendrik Tews

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).