src/Pure/ML-Systems/smlnj.ML
author wenzelm
Tue, 17 Jul 2007 13:19:21 +0200
changeset 23826 463903573934
parent 23770 2711e0285072
child 23921 947152add153
permissions -rw-r--r--
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/ML-Systems/smlnj.ML
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     3
5708
fb09ab6a447f dropped support for SML/NJ 109.x;
wenzelm
parents: 5090
diff changeset
     4
Compatibility file for Standard ML of New Jersey 110 or later.
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     5
*)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     6
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
     7
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     8
(** ML system related **)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     9
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    10
(*low-level pointer equality*)
16502
5a56e59526a5 added pointer_eq;
wenzelm
parents: 16266
diff changeset
    11
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    12
CM.autoload "$smlnj/init/init.cmi";
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    13
val pointer_eq = InlineT.ptreql;
16528
25a7459d4d4a pointer equality for sml/nj
paulson
parents: 16502
diff changeset
    14
16502
5a56e59526a5 added pointer_eq;
wenzelm
parents: 16266
diff changeset
    15
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    16
(* restore old-style character / string functions *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    17
10725
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    18
val ord     = SML90.ord;
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    19
val chr     = SML90.chr;
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    20
val explode = SML90.explode;
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    21
val implode = SML90.implode;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    22
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    23
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    24
(* New Jersey ML parameters *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    25
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    26
val _ =
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    27
 (Control.Print.printLength := 1000;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    28
  Control.Print.printDepth := 350;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    29
  Control.Print.stringDepth := 250;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    30
  Control.Print.signatures := 2;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    31
  Control.MC.matchRedundantError := false);
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    32
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    33
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    34
(* Poly/ML emulation *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    35
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    36
fun quit () = exit 0;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    37
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    38
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    39
fun print_depth n =
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    40
 (Control.Print.printDepth := n div 2;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    41
  Control.Print.printLength := n);
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    42
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    43
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
    44
(* compiler-independent timing functions *)
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    45
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    46
fun start_timing () =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    47
  let val CPUtimer = Timer.startCPUTimer();
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    48
      val time = Timer.checkCPUTimer(CPUtimer)
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    49
  in  (CPUtimer,time)  end;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    50
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    51
fun end_timing (CPUtimer, {sys,usr}) =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    52
  let open Time  (*...for Time.toString, Time.+ and Time.- *)
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    53
      val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    54
  in  "User " ^ toString (usr2-usr) ^
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    55
      "  All "^ toString (sys2-sys + usr2-usr) ^
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    56
      " secs"
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    57
      handle Time => ""
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    58
  end;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    59
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    60
fun check_timer timer =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    61
  let
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    62
    val {sys, usr} = Timer.checkCPUTimer timer;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    63
    val gc = Timer.checkGCTime timer;    (* FIXME already included in usr? *)
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    64
  in (sys, usr, gc) end;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    65
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    66
16660
76613dff2c9a added profiler interface (dummy);
wenzelm
parents: 16542
diff changeset
    67
(*prompts*)
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    68
fun ml_prompts p1 p2 =
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    69
  (Control.primaryPrompt := p1; Control.secondaryPrompt := p2);
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    70
17511
wenzelm
parents: 16681
diff changeset
    71
(*dummy implementation*)
16681
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    72
fun profile (n: int) f x = f x;
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    73
17511
wenzelm
parents: 16681
diff changeset
    74
(*dummy implementation*)
16681
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    75
fun exception_trace f = f ();
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    76
18384
fa38cca42913 added dummy 'print' to non-polyml systems
haftmann
parents: 17824
diff changeset
    77
(*dummy implementation*)
fa38cca42913 added dummy 'print' to non-polyml systems
haftmann
parents: 17824
diff changeset
    78
fun print x = x;
16681
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    79
23770
2711e0285072 added dummy makestring function
obua
parents: 23139
diff changeset
    80
(*dummy implementation*)
2711e0285072 added dummy makestring function
obua
parents: 23139
diff changeset
    81
fun makestring x = "dummy string for SML New Jersey";
2711e0285072 added dummy makestring function
obua
parents: 23139
diff changeset
    82
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    83
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    84
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    85
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    86
fun make_pp path pprint =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    87
  let
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    88
    open PrettyPrint;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    89
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    90
    fun pp pps obj =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    91
      pprint obj
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    92
        (string pps, openHOVBox pps o Rel,
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    93
          fn wd => break pps {nsp=wd, offset=0}, fn () => newline pps,
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    94
          fn () => closeBox pps);
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    95
  in (path, pp) end;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    96
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
    97
fun install_pp (path, pp) = CompilerPPTable.install_pp path pp;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    98
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    99
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   100
(* ML command execution *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   101
22144
c33450acd873 use_text: added name argument;
wenzelm
parents: 21770
diff changeset
   102
fun use_text name (print, err) verbose txt =
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   103
  let
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   104
    val ref out_orig = Control.Print.out;
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   105
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   106
    val out_buffer = ref ([]: string list);
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   107
    val out = {say = (fn s => out_buffer := s :: ! out_buffer), flush = (fn () => ())};
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
   108
    fun output () =
7890
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
   109
      let val str = implode (rev (! out_buffer))
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
   110
      in String.substring (str, 0, Int.max (0, size str - 1)) end;
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   111
  in
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   112
    Control.Print.out := out;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   113
    Backend.Interact.useStream (TextIO.openString txt) handle exn =>
22144
c33450acd873 use_text: added name argument;
wenzelm
parents: 21770
diff changeset
   114
      (Control.Print.out := out_orig;
c33450acd873 use_text: added name argument;
wenzelm
parents: 21770
diff changeset
   115
        err ((if name = "" then "" else "Error in " ^ name ^ "\n") ^ output ()); raise exn);
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   116
    Control.Print.out := out_orig;
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
   117
    if verbose then print (output ()) else ()
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   118
  end;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   119
21770
ea6f846d8c4b added use_file;
wenzelm
parents: 21298
diff changeset
   120
fun use_file _ _ name = use name;
ea6f846d8c4b added use_file;
wenzelm
parents: 21298
diff changeset
   121
ea6f846d8c4b added use_file;
wenzelm
parents: 21298
diff changeset
   122
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   123
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   124
(** interrupts **)
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   125
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   126
exception Interrupt;
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   127
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   128
local
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   129
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   130
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   131
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   132
fun release NONE = ()
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   133
  | release (SOME exn) = raise exn;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   134
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   135
in
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   136
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   137
fun ignore_interrupt f x =
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   138
  let
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   139
    val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   140
    val result = capture f x;
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   141
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   142
  in release result end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   143
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   144
fun raise_interrupt f x =
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   145
  let
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   146
    val interrupted = ref false;
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   147
    val result = ref NONE;
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   148
    val old_handler = Signals.inqHandler Signals.sigINT;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   149
  in
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   150
    SMLofNJ.Cont.callcc (fn cont =>
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   151
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   152
      result := capture f x));
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   153
    Signals.setHandler (Signals.sigINT, old_handler);
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   154
    if ! interrupted then raise Interrupt else release (! result)
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   155
  end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   156
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   157
end;
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   158
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   159
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   160
(* basis library fixes *)
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   161
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   162
structure TextIO =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   163
struct
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   164
  open TextIO;
23139
aa899bce7c3b TextIO.inputLine: use present SML B library version;
wenzelm
parents: 22144
diff changeset
   165
  fun inputLine is = TextIO.inputLine is
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   166
    handle IO.Io _ => raise Interrupt;
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   167
end;
17511
wenzelm
parents: 16681
diff changeset
   168
18790
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   169
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   170
(* bounded time execution *)
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   171
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   172
fun interrupt_timeout time f x =
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   173
  TimeLimit.timeLimit time f x
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   174
    handle TimeLimit.TimeOut => raise Interrupt;
18790
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   175
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   176
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   177
(** Signal handling: emulation of the Poly/ML Signal structure. Note that types
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   178
    Posix.Signal.signal and Signals.signal differ **)
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   179
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   180
structure IsaSignal =
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   181
struct
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   182
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   183
datatype sig_handle = SIG_DFL | SIG_IGN | SIG_HANDLE of Signals.signal -> unit;
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   184
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   185
(*From the SML/NJ documentation: "HANDLER(f) installs a handler for a
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   186
  signal. When signal is delivered to the process, the execution state
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   187
  of the current thread will be bundled up as a continuation k, then
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   188
  f(signal,n,k) will be called. The number n is the number of times
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   189
  signal has been signalled since the last time f was invoked for it."*)
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   190
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   191
fun toAction SIG_DFL = Signals.DEFAULT
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   192
  | toAction SIG_IGN = Signals.IGNORE
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   193
  | toAction (SIG_HANDLE iu) =
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   194
      Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   195
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   196
(*The types are correct, but I'm not sure about the semantics!*)
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   197
fun fromAction Signals.DEFAULT = SIG_DFL
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   198
  | fromAction Signals.IGNORE = SIG_IGN
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   199
  | fromAction (Signals.HANDLER f) =
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   200
      SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   201
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   202
(*Poly/ML version has type  int * sig_handle -> sig_handle*)
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   203
fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   204
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   205
val usr1 = UnixSignals.sigUSR1
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   206
val usr2 = UnixSignals.sigUSR2
17763
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   207
val alrm = UnixSignals.sigALRM
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   208
val chld = UnixSignals.sigCHLD
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   209
val cont = UnixSignals.sigCONT
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   210
val int = UnixSignals.sigINT
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   211
val quit = UnixSignals.sigQUIT
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   212
val term = UnixSignals.sigTERM
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   213
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   214
end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   215
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   216
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   217
(** OS related **)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   218
23826
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   219
(* current directory *)
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   220
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   221
val cd = OS.FileSys.chDir;
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   222
val pwd = OS.FileSys.getDir;
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   223
463903573934 moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents: 23770
diff changeset
   224
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   225
(* system command execution *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   226
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   227
(*execute Unix command which doesn't take any input from stdin and
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   228
  sends its output to stdout; could be done more easily by Unix.execute,
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   229
  but that function doesn't use the PATH*)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   230
fun execute command =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   231
  let
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   232
    val tmp_name = OS.FileSys.tmpName ();
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   233
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   234
    val result = TextIO.inputAll is;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   235
  in
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   236
    TextIO.closeIn is;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   237
    OS.FileSys.remove tmp_name;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   238
    result
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   239
  end;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   240
7855
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   241
(*plain version; with return code*)
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   242
val system = OS.Process.system: string -> int;
7855
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   243
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   244
17824
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   245
(*Convert a process ID to a decimal string (chiefly for tracing)*)
21298
6d2306b2376d tuned names of start_timing,/end_timing/check_timer;
wenzelm
parents: 18790
diff changeset
   246
fun string_of_pid pid =
17824
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   247
    Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   248
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   249
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   250
(* getenv *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   251
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   252
fun getenv var =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   253
  (case OS.Process.getEnv var of
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   254
    NONE => ""
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   255
  | SOME txt => txt);