src/Pure/ML-Systems/smlnj.ML
author wenzelm
Thu Feb 28 21:35:07 2002 +0100 (2002-02-28)
changeset 12990 c11adf2b1c1e
parent 12874 368966ceafe5
child 14519 4ca3608fdf4f
permissions -rw-r--r--
renamed mask_interrupt to ignore_interrupt;
renamed exhibit_interrupt to raise_interrupt;
fixed interrupt handling code (more correct use of continuation);
     1 (*  Title:      Pure/ML-Systems/smlnj.ML
     2     ID:         $Id$
     3     Author:     Carsten Clasohm and Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Compatibility file for Standard ML of New Jersey 110 or later.
     7 *)
     8 
     9 (case #version_id (Compiler.version) of
    10   [110, x] => if x >= 35 then use "ML-Systems/smlnj-compiler.ML" else ()
    11 | _ => ());
    12 
    13 
    14 (** ML system related **)
    15 
    16 (* restore old-style character / string functions *)
    17 
    18 val ord     = SML90.ord;
    19 val chr     = SML90.chr;
    20 val explode = SML90.explode;
    21 val implode = SML90.implode;
    22 
    23 
    24 (* New Jersey ML parameters *)
    25 
    26 val _ =
    27  (Compiler.Control.Print.printLength := 1000;
    28   Compiler.Control.Print.printDepth := 350;
    29   Compiler.Control.Print.stringDepth := 250;
    30   Compiler.Control.Print.signatures := 2;
    31   Compiler.Control.MC.matchRedundantError := false);
    32 
    33 
    34 (* Poly/ML emulation *)
    35 
    36 fun quit () = exit 0;
    37 
    38 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    39 fun print_depth n =
    40  (Compiler.Control.Print.printDepth := n div 2;
    41   Compiler.Control.Print.printLength := n);
    42 
    43 
    44 (* compiler-independent timing functions *)
    45 
    46 (*Note start point for timing*)
    47 fun startTiming() =
    48   let val CPUtimer = Timer.startCPUTimer();
    49       val time = Timer.checkCPUTimer(CPUtimer)
    50   in  (CPUtimer,time)  end;
    51 
    52 (*Finish timing and return string*)
    53 fun endTiming (CPUtimer, {gc,sys,usr}) =
    54   let open Time  (*...for Time.toString, Time.+ and Time.- *)
    55       val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
    56   in  "User " ^ toString (usr2-usr) ^
    57       "  GC " ^ toString (gc2-gc) ^
    58       "  All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^
    59       " secs"
    60       handle Time => ""
    61   end;
    62 
    63 
    64 (* prompts *)
    65 
    66 fun ml_prompts p1 p2 =
    67   (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
    68 
    69 
    70 (* toplevel pretty printing (see also Pure/install_pp.ML) *)
    71 
    72 fun make_pp path pprint =
    73   let
    74     open Compiler.PrettyPrint;
    75 
    76     fun pp pps obj =
    77       pprint obj
    78         (add_string pps, begin_block pps INCONSISTENT,
    79           fn wd => add_break pps (wd, 0), fn () => add_newline pps,
    80           fn () => end_block pps);
    81   in
    82     (path, pp)
    83   end;
    84 
    85 fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
    86 
    87 
    88 (* ML command execution *)
    89 
    90 fun use_text (print, err) verbose txt =
    91   let
    92     val ref out_orig = Compiler.Control.Print.out;
    93 
    94     val out_buffer = ref ([]: string list);
    95     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
    96     fun output () =
    97       let val str = implode (rev (! out_buffer))
    98       in String.substring (str, 0, Int.max (0, size str - 1)) end;
    99   in
   100     Compiler.Control.Print.out := out;
   101     Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
   102       (Compiler.Control.Print.out := out_orig; err (output ()); raise exn);
   103     Compiler.Control.Print.out := out_orig;
   104     if verbose then print (output ()) else ()
   105   end;
   106 
   107 
   108 
   109 (** interrupts **)
   110 
   111 exception Interrupt;
   112 
   113 local
   114 
   115 fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
   116 
   117 fun release NONE = ()
   118   | release (SOME exn) = raise exn;
   119 
   120 in
   121 
   122 fun ignore_interrupt f x =
   123   let
   124     val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
   125     val result = capture f x;
   126     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   127   in release result end;
   128 
   129 fun raise_interrupt f x =
   130   let
   131     val interrupted = ref false;
   132     val result = ref NONE;
   133     val old_handler = Signals.inqHandler Signals.sigINT;
   134   in
   135     SMLofNJ.Cont.callcc (fn cont =>
   136       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
   137       result := capture f x));
   138     Signals.setHandler (Signals.sigINT, old_handler);
   139     if ! interrupted then raise Interrupt else release (! result)
   140   end;
   141 
   142 end;
   143 
   144 
   145 
   146 (** OS related **)
   147 
   148 (* system command execution *)
   149 
   150 (*execute Unix command which doesn't take any input from stdin and
   151   sends its output to stdout; could be done more easily by Unix.execute,
   152   but that function doesn't use the PATH*)
   153 fun execute command =
   154   let
   155     val tmp_name = OS.FileSys.tmpName ();
   156     val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
   157     val result = TextIO.inputAll is;
   158   in
   159     TextIO.closeIn is;
   160     OS.FileSys.remove tmp_name;
   161     result
   162   end;
   163 
   164 (*plain version; with return code*)
   165 val system = OS.Process.system: string -> int;
   166 
   167 
   168 (* file handling *)
   169 
   170 (*get time of last modification*)
   171 fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
   172 
   173 
   174 (* getenv *)
   175 
   176 fun getenv var =
   177   (case OS.Process.getEnv var of
   178     NONE => ""
   179   | SOME txt => txt);