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