Author: particle
Date: Mon Mar 20 15:54:21 2006
New Revision: 11957

Modified:
   trunk/compilers/pge/rulec.pir

Log:
pge: add --output option, to redirect output

Modified: trunk/compilers/pge/rulec.pir
==============================================================================
--- trunk/compilers/pge/rulec.pir       (original)
+++ trunk/compilers/pge/rulec.pir       Mon Mar 20 15:54:21 2006
@@ -31,6 +31,10 @@
 
 =over 4
 
+=item --output OUTFILE
+
+Send the output to OUTFILE. By default, output is directed to STDOUT.
+
 =item --help
 
 Print a friendly help message.
@@ -47,7 +51,7 @@
 .sub "main" :main
     .param pmc args
     .local string prog
-    .local string file
+    .local string infile, outfile
     .local pmc compilers
 
     load_bytecode "PGE.pbc"
@@ -67,7 +71,7 @@
     $I0 = args
     unless $I0 >= 2 goto ERR_TOO_FEW_ARGS
 
-    file = pop args
+    infile = pop args
 
     ## create option parser
     .local pmc getopts
@@ -76,6 +80,7 @@
 
     ## config with cmdline options
     push getopts, "compiler|c=s"
+    push getopts, "output|o=s"
     push getopts, "help|h"
 
     ## process cmdline options
@@ -86,6 +91,24 @@
     help = opts['help']
     if help goto USAGE
 
+    .local pmc outfh
+
+    .local int ck_output
+    ck_output = exists opts['output']
+    if ck_output goto OUTPUT_FILE
+
+  OUTPUT_STDOUT:
+    outfh = getstdout
+    goto OUTPUT_DONE
+
+  OUTPUT_FILE:
+    outfile = opts['output']
+    outfh = open outfile, '>'
+    unless outfh goto ERR_NO_OUTFILE
+    goto OUTPUT_DONE
+
+  OUTPUT_DONE:
+
     .local string compiler
     compiler = opts['compiler']
     unless compiler goto ERR_NO_COMPILER
@@ -94,66 +117,73 @@
     comp_module = compilers[compiler]
     unless comp_module goto ERR_COMPILER_NOT_REGISTERED
 
-    .local pmc fh
-    fh = open file, "<"
-    unless fh goto ERR_NO_FILE
+    .local pmc infh
+    infh = open infile, "<"
+    unless infh goto ERR_NO_INFILE
 
     .local string source
-    source = read fh, 65535
-    close fh
+    source = read infh, 65535
+    close infh
 
     .local string pir, onload
     $P0 = compreg comp_module
     (pir, onload) = $P0(source)
-    print onload
-    print pir
+    print outfh, onload
+    print outfh, pir
     goto END
 
   USAGE:
-    print "Usage: "
-    print prog
-    print " [OPTIONS] --compiler=COMPILER FILE\n"
+    printerr "Usage: "
+    printerr prog
+    printerr " [OPTIONS] --compiler=COMPILER FILE\n"
 
     .local pmc it_comp
     .local string comp
     it_comp = new .Iterator, compilers
     it_comp = .ITERATE_FROM_START
 
-    print " Registered Compilers:\n"
+    printerr " Registered Compilers:\n"
   IT_NEXT:
 
     unless it_comp goto IT_DONE
     shift comp, it_comp
-    print "  * "
-    print comp
-    print "\n"
+    printerr "  * "
+    printerr comp
+    printerr "\n"
     goto IT_NEXT
 
   IT_DONE:
-    print <<"OPTIONS"
+    printerr <<"OPTIONS"
  Options:
-  --help         -- print this message
+  --output=OUTFILE  -- redirect output to OUTFILE
+  --help            -- print this message
 OPTIONS
     goto END
 
   ERR_TOO_FEW_ARGS:
-    print "Error: too few arguments\n\n"
+    printerr "Error: too few arguments\n\n"
     goto USAGE
 
   ERR_NO_COMPILER:
-    print "Error: no compiler specified\n\n"
+    printerr "Error: no compiler specified\n\n"
     goto USAGE
 
   ERR_COMPILER_NOT_REGISTERED:
-    print "Error: not a registered compiler: "
-    print compiler
-    print "\n\n"
+    printerr "Error: not a registered compiler: "
+    printerr compiler
+    printerr "\n\n"
+    goto USAGE
+
+  ERR_NO_INFILE:
+    printerr "Error: file not found: "
+    printerr infile
+    printerr "\n\n"
     goto USAGE
 
-  ERR_NO_FILE:
-    print "Error: file not found: "
-    print file
-    print "\n\n"
+  ERR_NO_OUTFILE:
+    printerr "Error: file not found: "
+    printerr outfile
+    printerr "\n\n"
     goto USAGE
 
   END:

Reply via email to