1val _ = PolyML.Compiler.prompt1:="";
2val _ = PolyML.Compiler.prompt2:="";
3val _ = PolyML.print_depth 0;
4
5use "../Holmake/QuoteFilter.sml";
6open OS.Process
7
8fun read_from_stream is n = TextIO.input is
9
10fun main() = let
11  (* magic to ensure that interruptions (SIGINTs) are actually seen by the
12     linked executable as Interrupt exceptions *)
13  val _ = Signal.signal(2, Signal.SIG_HANDLE
14                               (fn _ => Thread.Thread.broadcastInterrupt()))
15  val (instream, outstream) =
16      case CommandLine.arguments() of
17        [] => (TextIO.stdIn, TextIO.stdOut)
18      | [ifile, ofile] => let
19          open TextIO
20          val is = TextIO.openIn ifile
21              handle OS.SysErr _ =>
22                     (output(stdErr, "Error opening "^ifile^"\n");
23                      exit failure)
24          val os = TextIO.openOut ofile
25              handle IO.Io {cause = OS.SysErr (_, eo), ...} =>
26                     (case eo of
27                        SOME e => output(stdErr, OS.errorMsg e)
28                      | NONE => ();
29                      exit failure)
30        in
31          (is, os)
32        end
33      | _ => (TextIO.output(TextIO.stdErr,
34                            "Usage:\n  " ^ CommandLine.name() ^
35                            " [<inputfile> <outputfile>]\n");
36              exit failure)
37
38(* with many thanks to Ken Friis Larsen, Peter Sestoft, Claudio Russo and
39   Kenn Heinrich who helped me see the light with respect to this code *)
40
41  open QuoteFilter.UserDeclarations
42  val state as QFS args = newstate true
43
44  fun loop() =
45    let
46      val lexer = QuoteFilter.makeLexer (read_from_stream instream) state
47      fun coreloop () =
48        case lexer() of
49            "" => ()
50          | s => (TextIO.output(outstream, s); coreloop())
51    in
52      coreloop() handle Interrupt => (resetstate state; loop())
53    end
54in
55  loop();
56  TextIO.closeOut outstream;
57  exit success
58end;
59