src/Pure/ML-Systems/smlnj.ML
author wenzelm
Wed Oct 20 15:22:56 1999 +0200 (1999-10-20)
changeset 7890 0aa16bc2abdb
parent 7855 092a6435afad
child 10725 ea048ad15283
permissions -rw-r--r--
use_text: remove last char from output;
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@7855
    83
fun use_text print 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@7890
    90
    fun show_output () =
wenzelm@7890
    91
      let val str = implode (rev (! out_buffer))
wenzelm@7890
    92
      in print (String.substring (str, 0, Int.max (0, size str - 1))) end;
wenzelm@5090
    93
  in
wenzelm@5090
    94
    Compiler.Control.Print.out := out;
wenzelm@5090
    95
    Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
wenzelm@5090
    96
      (Compiler.Control.Print.out := out_orig; show_output (); raise exn);
wenzelm@5090
    97
    Compiler.Control.Print.out := out_orig;
wenzelm@5090
    98
    if verbose then show_output () else ()
wenzelm@5090
    99
  end;
wenzelm@4403
   100
wenzelm@4403
   101
wenzelm@4403
   102
wenzelm@5816
   103
(** interrupts **)
wenzelm@5816
   104
wenzelm@5816
   105
local
wenzelm@5816
   106
wenzelm@5816
   107
datatype 'a result =
wenzelm@5816
   108
  Result of 'a |
wenzelm@5816
   109
  Exn of exn;
wenzelm@5816
   110
wenzelm@5816
   111
fun capture f x = Result (f x) handle exn => Exn exn;
wenzelm@5816
   112
wenzelm@5816
   113
fun release (Result x) = x
wenzelm@5816
   114
  | release (Exn exn) = raise exn;
wenzelm@5816
   115
wenzelm@5816
   116
wenzelm@5816
   117
val sig_int = Signals.sigINT;
wenzelm@5816
   118
val sig_int_mask = Signals.MASK [Signals.sigINT];
wenzelm@5816
   119
wenzelm@5816
   120
fun interruptible () =
wenzelm@5816
   121
  (case Signals.masked () of
wenzelm@5816
   122
    Signals.MASKALL => false
wenzelm@5816
   123
  | Signals.MASK sigs => List.all (fn s => s <> sig_int) sigs);
wenzelm@5816
   124
wenzelm@5816
   125
val mask_signals = Signals.maskSignals;
wenzelm@5816
   126
val unmask_signals = Signals.unmaskSignals;
wenzelm@5816
   127
wenzelm@5816
   128
fun change_mask ok change unchange f x =
wenzelm@5816
   129
  if ok () then f x
wenzelm@5816
   130
  else
wenzelm@5816
   131
    let
wenzelm@5816
   132
      val _ = change sig_int_mask;
wenzelm@5816
   133
      val result = capture f x;
wenzelm@5816
   134
      val _ = unchange sig_int_mask;
wenzelm@5816
   135
    in release result end;
wenzelm@5816
   136
wenzelm@5816
   137
in
wenzelm@5816
   138
wenzelm@5816
   139
wenzelm@5816
   140
(* mask / unmask interrupt *)
wenzelm@5816
   141
wenzelm@5816
   142
fun mask_interrupt f = change_mask (not o interruptible) mask_signals unmask_signals f;
wenzelm@5816
   143
fun unmask_interrupt f = change_mask interruptible unmask_signals mask_signals f;
wenzelm@5816
   144
wenzelm@5816
   145
wenzelm@5816
   146
(* exhibit interrupt (via exception) *)
wenzelm@5816
   147
wenzelm@5816
   148
exception Interrupt;
wenzelm@5816
   149
wenzelm@5816
   150
fun exhibit_interrupt f x =
wenzelm@5816
   151
  let
wenzelm@5816
   152
    val orig_handler = Signals.inqHandler sig_int;
wenzelm@5816
   153
    fun reset_handler () = (Signals.setHandler (sig_int, orig_handler); ());
wenzelm@5816
   154
wenzelm@5816
   155
    val interrupted = ref false;
wenzelm@5816
   156
wenzelm@5816
   157
    fun set_handler cont =
wenzelm@5816
   158
      Signals.setHandler (sig_int, Signals.HANDLER (fn _ => (interrupted := true; cont)));
wenzelm@5816
   159
wenzelm@5816
   160
    fun proceed cont =
wenzelm@5816
   161
      let
wenzelm@5816
   162
        val _ = set_handler cont;
wenzelm@5816
   163
        val result = unmask_interrupt (capture f) x;
wenzelm@5816
   164
        val _ = reset_handler ();
wenzelm@5816
   165
      in release result end;
wenzelm@5816
   166
  in
wenzelm@5816
   167
    SMLofNJ.Cont.callcc proceed;
wenzelm@5816
   168
    reset_handler ();
wenzelm@5816
   169
    if ! interrupted then raise Interrupt else ()
wenzelm@5816
   170
   end;
wenzelm@5816
   171
wenzelm@5816
   172
end;
wenzelm@5816
   173
wenzelm@5816
   174
wenzelm@5816
   175
wenzelm@4403
   176
(** OS related **)
wenzelm@4403
   177
wenzelm@4403
   178
(* system command execution *)
wenzelm@4403
   179
wenzelm@4403
   180
(*execute Unix command which doesn't take any input from stdin and
wenzelm@4403
   181
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@4403
   182
  but that function doesn't use the PATH*)
wenzelm@4403
   183
fun execute command =
wenzelm@4403
   184
  let
wenzelm@4403
   185
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@4403
   186
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@4403
   187
    val result = TextIO.inputAll is;
wenzelm@4403
   188
  in
wenzelm@4403
   189
    TextIO.closeIn is;
wenzelm@4403
   190
    OS.FileSys.remove tmp_name;
wenzelm@4403
   191
    result
wenzelm@4403
   192
  end;
wenzelm@4403
   193
wenzelm@7855
   194
(*plain version; with return code*)
wenzelm@7855
   195
val system = OS.Process.system: string -> int; 
wenzelm@7855
   196
wenzelm@4403
   197
wenzelm@4403
   198
(* file handling *)
wenzelm@4403
   199
wenzelm@6227
   200
(*get time of last modification*)
wenzelm@6227
   201
fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
wenzelm@4403
   202
wenzelm@4403
   203
wenzelm@4403
   204
(* getenv *)
wenzelm@4403
   205
wenzelm@4403
   206
fun getenv var =
wenzelm@4403
   207
  (case OS.Process.getEnv var of
wenzelm@4403
   208
    NONE => ""
wenzelm@4403
   209
  | SOME txt => txt);
wenzelm@4403
   210
wenzelm@4403
   211
wenzelm@4428
   212
(* non-ASCII input (see also Thy/use.ML) *)
wenzelm@4403
   213
wenzelm@4403
   214
val needs_filtered_use = false;