src/Pure/ML-Systems/smlnj.ML
author paulson
Wed Apr 13 09:48:41 2005 +0200 (2005-04-13)
changeset 15702 2677db44c795
parent 14981 e73f8140af78
child 16266 7a6616be8712
permissions -rw-r--r--
new signalling primmitives for sml/nj compatibility
     1 (*  Title:      Pure/ML-Systems/smlnj.ML
     2     ID:         $Id$
     3     Author:     Carsten Clasohm and Markus Wenzel, TU Muenchen
     4 
     5 Compatibility file for Standard ML of New Jersey 110 or later.
     6 *)
     7 
     8 (case #version_id (Compiler.version) of
     9   [110, x] => if x >= 35 then use "ML-Systems/smlnj-compiler.ML" else ()
    10 | _ => ());
    11 
    12 (** ML system related **)
    13 
    14 (* restore old-style character / string functions *)
    15 
    16 val ord     = SML90.ord;
    17 val chr     = SML90.chr;
    18 val explode = SML90.explode;
    19 val implode = SML90.implode;
    20 
    21 
    22 (* New Jersey ML parameters *)
    23 
    24 val _ =
    25  (Compiler.Control.Print.printLength := 1000;
    26   Compiler.Control.Print.printDepth := 350;
    27   Compiler.Control.Print.stringDepth := 250;
    28   Compiler.Control.Print.signatures := 2;
    29   Compiler.Control.MC.matchRedundantError := false);
    30 
    31 
    32 (* Poly/ML emulation *)
    33 
    34 fun quit () = exit 0;
    35 
    36 (*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
    37 fun print_depth n =
    38  (Compiler.Control.Print.printDepth := n div 2;
    39   Compiler.Control.Print.printLength := n);
    40 
    41 
    42 (* compiler-independent timing functions *)
    43 
    44 (case #version_id (Compiler.version) of
    45   [110, x] => if x >= 44
    46 	      then use "ML-Systems/cpu-timer-basis.ML"
    47 	      else use "ML-Systems/cpu-timer-gc.ML"
    48 | _ => use "ML-Systems/cpu-timer-gc.ML");
    49 
    50 
    51 (* prompts *)
    52 
    53 fun ml_prompts p1 p2 =
    54   (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
    55 
    56 
    57 (case #version_id (Compiler.version) of
    58   [110, x] => if x >= 44
    59 	      then use "ML-Systems/smlnj-basis-compat.ML"
    60 	      else ()
    61 | _ => ());
    62 
    63 (* toplevel pretty printing (see also Pure/install_pp.ML) *)
    64 
    65 (case #version_id (Compiler.version) of
    66   [110, x] => if x >= 44
    67 	      then use "ML-Systems/smlnj-pp-new.ML"
    68 	      else use "ML-Systems/smlnj-pp-old.ML"
    69 | _ => use "ML-Systems/smlnj-pp-old.ML");
    70 
    71 fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
    72 
    73 
    74 (* ML command execution *)
    75 
    76 fun use_text (print, err) verbose txt =
    77   let
    78     val ref out_orig = Compiler.Control.Print.out;
    79 
    80     val out_buffer = ref ([]: string list);
    81     val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
    82     fun output () =
    83       let val str = implode (rev (! out_buffer))
    84       in String.substring (str, 0, Int.max (0, size str - 1)) end;
    85   in
    86     Compiler.Control.Print.out := out;
    87     Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
    88       (Compiler.Control.Print.out := out_orig; err (output ()); raise exn);
    89     Compiler.Control.Print.out := out_orig;
    90     if verbose then print (output ()) else ()
    91   end;
    92 
    93 
    94 
    95 (** interrupts **)
    96 
    97 exception Interrupt;
    98 
    99 local
   100 
   101 fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
   102 
   103 fun release NONE = ()
   104   | release (SOME exn) = raise exn;
   105 
   106 in
   107 
   108 fun ignore_interrupt f x =
   109   let
   110     val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
   111     val result = capture f x;
   112     val _ = Signals.setHandler (Signals.sigINT, old_handler);
   113   in release result end;
   114 
   115 fun raise_interrupt f x =
   116   let
   117     val interrupted = ref false;
   118     val result = ref NONE;
   119     val old_handler = Signals.inqHandler Signals.sigINT;
   120   in
   121     SMLofNJ.Cont.callcc (fn cont =>
   122       (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
   123       result := capture f x));
   124     Signals.setHandler (Signals.sigINT, old_handler);
   125     if ! interrupted then raise Interrupt else release (! result)
   126   end;
   127 
   128 end;
   129 
   130 (** Signal handling: emulation of the Poly/ML Signal structure. Note that types 
   131     Posix.Signal.signal and Signals.signal differ **)
   132 
   133 structure IsaSignal =
   134 struct
   135 
   136 datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
   137 
   138 (*From the SML/NJ documentation:
   139 "HANDLER(f) installs a handler for a signal. When signal is delivered to the process, the execution state of the current thread will be bundled up as a continuation k, then f(signal,n,k) will be called. The number n is the number of times signal has been signalled since the last time f was invoked for it."*)
   140 
   141 fun toAction SIG_DFL = Signals.DEFAULT
   142   | toAction SIG_IGN = Signals.IGNORE
   143   | toAction (SIG_HANDLE iu) = 
   144       Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
   145   
   146 (*The types are correct, but I'm not sure about the semantics!*)
   147 fun fromAction Signals.DEFAULT = SIG_DFL
   148   | fromAction Signals.IGNORE = SIG_IGN
   149   | fromAction (Signals.HANDLER f) = 
   150       SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
   151     
   152 (*Poly/ML version has type  int * sig_handle -> sig_handle*)
   153 fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
   154 
   155 val usr1 = UnixSignals.sigUSR1
   156 val usr2 = UnixSignals.sigUSR2
   157 
   158 end;
   159 
   160 
   161 (** OS related **)
   162 
   163 (* system command execution *)
   164 
   165 (*execute Unix command which doesn't take any input from stdin and
   166   sends its output to stdout; could be done more easily by Unix.execute,
   167   but that function doesn't use the PATH*)
   168 fun execute command =
   169   let
   170     val tmp_name = OS.FileSys.tmpName ();
   171     val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
   172     val result = TextIO.inputAll is;
   173   in
   174     TextIO.closeIn is;
   175     OS.FileSys.remove tmp_name;
   176     result
   177   end;
   178 
   179 (*plain version; with return code*)
   180 val system = OS.Process.system: string -> int;
   181 
   182 
   183 (* file handling *)
   184 
   185 (*get time of last modification*)
   186 fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
   187 
   188 
   189 (* getenv *)
   190 
   191 fun getenv var =
   192   (case OS.Process.getEnv var of
   193     NONE => ""
   194   | SOME txt => txt);