src/Pure/ML-Systems/smlnj.ML
author wenzelm
Mon Nov 09 11:25:24 1998 +0100 (1998-11-09)
changeset 5816 6f3cb53502fa
parent 5708 fb09ab6a447f
child 6227 3198f547f8af
permissions -rw-r--r--
smart interrupt handler;
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@4403
     8
(** ML system related **)
wenzelm@4403
     9
wenzelm@4403
    10
(* restore old-style character / string functions *)
wenzelm@4403
    11
wenzelm@4403
    12
fun ord s = Char.ord (String.sub (s, 0));
wenzelm@4403
    13
val chr = str o Char.chr;
wenzelm@4403
    14
val explode = (map str) o String.explode;
wenzelm@4403
    15
val implode = String.concat;
wenzelm@4403
    16
wenzelm@4403
    17
wenzelm@4403
    18
(* New Jersey ML parameters *)
wenzelm@4403
    19
wenzelm@4403
    20
val _ =
wenzelm@4403
    21
 (Compiler.Control.Print.printLength := 1000;
wenzelm@4403
    22
  Compiler.Control.Print.printDepth := 350;
wenzelm@4403
    23
  Compiler.Control.Print.stringDepth := 250;
wenzelm@4403
    24
  Compiler.Control.Print.signatures := 2);
wenzelm@4403
    25
wenzelm@4403
    26
wenzelm@4403
    27
(* Poly/ML emulation *)
wenzelm@4403
    28
wenzelm@4403
    29
fun quit () = exit 0;
wenzelm@4403
    30
wenzelm@4403
    31
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
wenzelm@4403
    32
fun print_depth n =
wenzelm@4403
    33
 (Compiler.Control.Print.printDepth := n div 2;
wenzelm@4403
    34
  Compiler.Control.Print.printLength := n);
wenzelm@4403
    35
wenzelm@4403
    36
wenzelm@5816
    37
(* compiler-independent timing functions *)
wenzelm@4403
    38
wenzelm@4403
    39
(*Note start point for timing*)
wenzelm@5816
    40
fun startTiming() =
wenzelm@4403
    41
  let val CPUtimer = Timer.startCPUTimer();
wenzelm@4403
    42
      val time = Timer.checkCPUTimer(CPUtimer)
wenzelm@4403
    43
  in  (CPUtimer,time)  end;
wenzelm@4403
    44
wenzelm@4403
    45
(*Finish timing and return string*)
wenzelm@4403
    46
fun endTiming (CPUtimer, {gc,sys,usr}) =
wenzelm@4403
    47
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
wenzelm@4403
    48
      val {gc=gc2,sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
wenzelm@4403
    49
  in  "User " ^ toString (usr2-usr) ^
wenzelm@4403
    50
      "  GC " ^ toString (gc2-gc) ^
wenzelm@4403
    51
      "  All "^ toString (sys2-sys + usr2-usr + gc2-gc) ^
wenzelm@4403
    52
      " secs"
wenzelm@4403
    53
      handle Time => ""
wenzelm@4403
    54
  end;
wenzelm@4403
    55
wenzelm@4403
    56
wenzelm@4977
    57
(* prompts *)
wenzelm@4977
    58
wenzelm@4977
    59
fun ml_prompts p1 p2 =
wenzelm@4977
    60
  (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
wenzelm@4977
    61
wenzelm@4977
    62
wenzelm@4403
    63
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
wenzelm@4403
    64
wenzelm@4403
    65
fun make_pp path pprint =
wenzelm@4403
    66
  let
wenzelm@4403
    67
    open Compiler.PrettyPrint;
wenzelm@4403
    68
wenzelm@4403
    69
    fun pp pps obj =
wenzelm@4403
    70
      pprint obj
wenzelm@4403
    71
        (add_string pps, begin_block pps INCONSISTENT,
wenzelm@4403
    72
          fn wd => add_break pps (wd, 0), fn () => add_newline pps,
wenzelm@4403
    73
          fn () => end_block pps);
wenzelm@4403
    74
  in
wenzelm@4403
    75
    (path, pp)
wenzelm@4403
    76
  end;
wenzelm@4403
    77
wenzelm@4403
    78
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
wenzelm@4403
    79
wenzelm@4403
    80
wenzelm@4403
    81
(* ML command execution *)
wenzelm@4403
    82
wenzelm@5090
    83
fun use_text verbose txt =
wenzelm@5090
    84
  let
wenzelm@5090
    85
    val ref out_orig = Compiler.Control.Print.out;
wenzelm@5090
    86
wenzelm@5090
    87
    val out_buffer = ref ([]: string list);
wenzelm@5090
    88
    val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
wenzelm@5090
    89
wenzelm@5090
    90
    fun show_output () = print (implode (rev (! out_buffer)));
wenzelm@5090
    91
  in
wenzelm@5090
    92
    Compiler.Control.Print.out := out;
wenzelm@5090
    93
    Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
wenzelm@5090
    94
      (Compiler.Control.Print.out := out_orig; show_output (); raise exn);
wenzelm@5090
    95
    Compiler.Control.Print.out := out_orig;
wenzelm@5090
    96
    if verbose then show_output () else ()
wenzelm@5090
    97
  end;
wenzelm@4403
    98
wenzelm@4403
    99
wenzelm@4403
   100
wenzelm@5816
   101
(** interrupts **)
wenzelm@5816
   102
wenzelm@5816
   103
local
wenzelm@5816
   104
wenzelm@5816
   105
datatype 'a result =
wenzelm@5816
   106
  Result of 'a |
wenzelm@5816
   107
  Exn of exn;
wenzelm@5816
   108
wenzelm@5816
   109
fun capture f x = Result (f x) handle exn => Exn exn;
wenzelm@5816
   110
wenzelm@5816
   111
fun release (Result x) = x
wenzelm@5816
   112
  | release (Exn exn) = raise exn;
wenzelm@5816
   113
wenzelm@5816
   114
wenzelm@5816
   115
val sig_int = Signals.sigINT;
wenzelm@5816
   116
val sig_int_mask = Signals.MASK [Signals.sigINT];
wenzelm@5816
   117
wenzelm@5816
   118
fun interruptible () =
wenzelm@5816
   119
  (case Signals.masked () of
wenzelm@5816
   120
    Signals.MASKALL => false
wenzelm@5816
   121
  | Signals.MASK sigs => List.all (fn s => s <> sig_int) sigs);
wenzelm@5816
   122
wenzelm@5816
   123
val mask_signals = Signals.maskSignals;
wenzelm@5816
   124
val unmask_signals = Signals.unmaskSignals;
wenzelm@5816
   125
wenzelm@5816
   126
fun change_mask ok change unchange f x =
wenzelm@5816
   127
  if ok () then f x
wenzelm@5816
   128
  else
wenzelm@5816
   129
    let
wenzelm@5816
   130
      val _ = change sig_int_mask;
wenzelm@5816
   131
      val result = capture f x;
wenzelm@5816
   132
      val _ = unchange sig_int_mask;
wenzelm@5816
   133
    in release result end;
wenzelm@5816
   134
wenzelm@5816
   135
in
wenzelm@5816
   136
wenzelm@5816
   137
wenzelm@5816
   138
(* mask / unmask interrupt *)
wenzelm@5816
   139
wenzelm@5816
   140
fun mask_interrupt f = change_mask (not o interruptible) mask_signals unmask_signals f;
wenzelm@5816
   141
fun unmask_interrupt f = change_mask interruptible unmask_signals mask_signals f;
wenzelm@5816
   142
wenzelm@5816
   143
wenzelm@5816
   144
(* exhibit interrupt (via exception) *)
wenzelm@5816
   145
wenzelm@5816
   146
exception Interrupt;
wenzelm@5816
   147
wenzelm@5816
   148
fun exhibit_interrupt f x =
wenzelm@5816
   149
  let
wenzelm@5816
   150
    val orig_handler = Signals.inqHandler sig_int;
wenzelm@5816
   151
    fun reset_handler () = (Signals.setHandler (sig_int, orig_handler); ());
wenzelm@5816
   152
wenzelm@5816
   153
    val interrupted = ref false;
wenzelm@5816
   154
wenzelm@5816
   155
    fun set_handler cont =
wenzelm@5816
   156
      Signals.setHandler (sig_int, Signals.HANDLER (fn _ => (interrupted := true; cont)));
wenzelm@5816
   157
wenzelm@5816
   158
    fun proceed cont =
wenzelm@5816
   159
      let
wenzelm@5816
   160
        val _ = set_handler cont;
wenzelm@5816
   161
        val result = unmask_interrupt (capture f) x;
wenzelm@5816
   162
        val _ = reset_handler ();
wenzelm@5816
   163
      in release result end;
wenzelm@5816
   164
  in
wenzelm@5816
   165
    SMLofNJ.Cont.callcc proceed;
wenzelm@5816
   166
    reset_handler ();
wenzelm@5816
   167
    if ! interrupted then raise Interrupt else ()
wenzelm@5816
   168
   end;
wenzelm@5816
   169
wenzelm@5816
   170
end;
wenzelm@5816
   171
wenzelm@5816
   172
wenzelm@5816
   173
wenzelm@4403
   174
(** OS related **)
wenzelm@4403
   175
wenzelm@4403
   176
(* system command execution *)
wenzelm@4403
   177
wenzelm@4403
   178
(*execute Unix command which doesn't take any input from stdin and
wenzelm@4403
   179
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@4403
   180
  but that function doesn't use the PATH*)
wenzelm@4403
   181
fun execute command =
wenzelm@4403
   182
  let
wenzelm@4403
   183
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@4403
   184
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@4403
   185
    val result = TextIO.inputAll is;
wenzelm@4403
   186
  in
wenzelm@4403
   187
    TextIO.closeIn is;
wenzelm@4403
   188
    OS.FileSys.remove tmp_name;
wenzelm@4403
   189
    result
wenzelm@4403
   190
  end;
wenzelm@4403
   191
wenzelm@4403
   192
wenzelm@4403
   193
(* file handling *)
wenzelm@4403
   194
wenzelm@4403
   195
(*get time of last modification; if file doesn't exist return an empty string*)
wenzelm@5816
   196
fun file_info "" = ""           (* FIXME !? *)
wenzelm@4403
   197
  | file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
wenzelm@4403
   198
wenzelm@4403
   199
wenzelm@4403
   200
(* getenv *)
wenzelm@4403
   201
wenzelm@4403
   202
fun getenv var =
wenzelm@4403
   203
  (case OS.Process.getEnv var of
wenzelm@4403
   204
    NONE => ""
wenzelm@4403
   205
  | SOME txt => txt);
wenzelm@4403
   206
wenzelm@4403
   207
wenzelm@4428
   208
(* non-ASCII input (see also Thy/use.ML) *)
wenzelm@4403
   209
wenzelm@4403
   210
val needs_filtered_use = false;