#! /usr/local/mira -exp
output

output::[sys_message]
output = map f (tl $*), if # $* > 1
       = [Stdout (revlines $-)], otherwise ||no files, so use stdin

f::[char]->sys_message
f fil = Stderr ("mrev: cannot open "++fil++"\n"), if badfil
      = Stdout (revlines (read fil)), otherwise
	where
	badfil = ~ member (filemode fil) 'r'

revlines::[char]->[char]
revlines = lay.map reverse.lines

||This shows how to implement a UNIX command  in  Miranda  -  see  under
||``UNIX/Miranda system interface'' in the manual for more details.

||The usage of this command (from a UNIX shell) is
||	mrev [file] ...
||It  is a Miranda version of the UNIX command `rev' which reverses each
||line of its input.  If no files given, takes data from stdin.

||You can use this example  as  a  template,  for  turning  any  Miranda
||function,  of  type  [char]->[char],  into  a  UNIX  filter.   Replace
||`revlines' in the above text, by your chosen function.  More typically
||the function would be defined in a normal Miranda script, and acquired
||here by a %include directive, so  that  its  definition  will  not  be
||recompiled each time this program is executed.
