Version française
Home     About     Download     Resources     Contact us    
Browse thread
Camlp4 in a Unix pipe
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Alessandro Baretta <a.baretta@b...>
Subject: Re: [Caml-list] Camlp4 in a Unix pipe
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