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