src/Pure/ML-Systems/smlnj.ML
author webertj
Thu, 26 Jan 2006 20:42:15 +0100
changeset 18790 418131f631fc
parent 18760 97aaecb84afe
child 21298 6d2306b2376d
permissions -rw-r--r--
interrupt_timeout now raises Interrupt instead of SML90.Interrupt
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
    Author:     Carsten Clasohm and Markus Wenzel, TU Muenchen
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     4
5708
fb09ab6a447f dropped support for SML/NJ 109.x;
wenzelm
parents: 5090
diff changeset
     5
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
     6
*)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
     7
12874
368966ceafe5 ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents: 12581
diff changeset
     8
(case #version_id (Compiler.version) of
368966ceafe5 ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents: 12581
diff changeset
     9
  [110, x] => if x >= 35 then use "ML-Systems/smlnj-compiler.ML" else ()
368966ceafe5 ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents: 12581
diff changeset
    10
| _ => ());
368966ceafe5 ML-Systems/smlnj-compiler.ML compatibility tweak;
wenzelm
parents: 12581
diff changeset
    11
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    12
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    13
(** ML system related **)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    14
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    15
(*low-level pointer equality*)
16502
5a56e59526a5 added pointer_eq;
wenzelm
parents: 16266
diff changeset
    16
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    17
(*dummy version -- may get overridden in smlnj-ptreql.ML*)
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    18
fun pointer_eq (x:'a, y) = false;
16502
5a56e59526a5 added pointer_eq;
wenzelm
parents: 16266
diff changeset
    19
16528
25a7459d4d4a pointer equality for sml/nj
paulson
parents: 16502
diff changeset
    20
(case #version_id (Compiler.version) of
25a7459d4d4a pointer equality for sml/nj
paulson
parents: 16502
diff changeset
    21
  [110, x] => if x >= 49 then use "ML-Systems/smlnj-ptreql.ML" else ()
25a7459d4d4a pointer equality for sml/nj
paulson
parents: 16502
diff changeset
    22
| _ => ());
25a7459d4d4a pointer equality for sml/nj
paulson
parents: 16502
diff changeset
    23
16502
5a56e59526a5 added pointer_eq;
wenzelm
parents: 16266
diff changeset
    24
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    25
(* restore old-style character / string functions *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    26
10725
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    27
val ord     = SML90.ord;
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    28
val chr     = SML90.chr;
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    29
val explode = SML90.explode;
ea048ad15283 better definitions of SML90 features
paulson
parents: 7890
diff changeset
    30
val implode = SML90.implode;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    31
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
(* New Jersey ML parameters *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    34
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    35
val _ =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    36
 (Compiler.Control.Print.printLength := 1000;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    37
  Compiler.Control.Print.printDepth := 350;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    38
  Compiler.Control.Print.stringDepth := 250;
12581
dceea9dbdedd Redundant patterns no longer cause errors.
berghofe
parents: 12108
diff changeset
    39
  Compiler.Control.Print.signatures := 2;
dceea9dbdedd Redundant patterns no longer cause errors.
berghofe
parents: 12108
diff changeset
    40
  Compiler.Control.MC.matchRedundantError := false);
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    41
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
(* Poly/ML emulation *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    44
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    45
fun quit () = exit 0;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    46
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    47
(*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
    48
fun print_depth n =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    49
 (Compiler.Control.Print.printDepth := n div 2;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    50
  Compiler.Control.Print.printLength := n);
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    51
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    52
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
    53
(* compiler-independent timing functions *)
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    54
14519
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    55
(case #version_id (Compiler.version) of
14656
765badface6a changed SML/NJ v 45 to 44
paulson
parents: 14520
diff changeset
    56
  [110, x] => if x >= 44
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    57
              then use "ML-Systems/cpu-timer-basis.ML"
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    58
              else use "ML-Systems/cpu-timer-gc.ML"
14520
af9d7fcf873e Whoops. Those default cases can be tricky.
skalberg
parents: 14519
diff changeset
    59
| _ => use "ML-Systems/cpu-timer-gc.ML");
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    60
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    61
16660
76613dff2c9a added profiler interface (dummy);
wenzelm
parents: 16542
diff changeset
    62
(*prompts*)
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    63
fun ml_prompts p1 p2 =
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    64
  (Compiler.Control.primaryPrompt := p1; Compiler.Control.secondaryPrompt := p2);
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    65
17511
wenzelm
parents: 16681
diff changeset
    66
(*dummy implementation*)
16681
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    67
fun profile (n: int) f x = f x;
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    68
17511
wenzelm
parents: 16681
diff changeset
    69
(*dummy implementation*)
16681
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    70
fun exception_trace f = f ();
4977
6cec2c0ffdbf added ml_prompts;
wenzelm
parents: 4428
diff changeset
    71
18384
fa38cca42913 added dummy 'print' to non-polyml systems
haftmann
parents: 17824
diff changeset
    72
(*dummy implementation*)
fa38cca42913 added dummy 'print' to non-polyml systems
haftmann
parents: 17824
diff changeset
    73
fun print x = x;
16681
d54dfd724b35 dummy exception_trace;
wenzelm
parents: 16660
diff changeset
    74
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    75
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    76
14519
4ca3608fdf4f Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents: 12990
diff changeset
    77
(case #version_id (Compiler.version) of
14656
765badface6a changed SML/NJ v 45 to 44
paulson
parents: 14520
diff changeset
    78
  [110, x] => if x >= 44
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    79
              then use "ML-Systems/smlnj-pp-new.ML"
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
    80
              else use "ML-Systems/smlnj-pp-old.ML"
14520
af9d7fcf873e Whoops. Those default cases can be tricky.
skalberg
parents: 14519
diff changeset
    81
| _ => use "ML-Systems/smlnj-pp-old.ML");
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    82
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    83
fun install_pp (path, pp) = Compiler.PPTable.install_pp path pp;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    84
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    85
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    86
(* ML command execution *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
    87
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
    88
fun use_text (print, err) verbose txt =
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    89
  let
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    90
    val ref out_orig = Compiler.Control.Print.out;
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    91
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    92
    val out_buffer = ref ([]: string list);
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    93
    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
    94
    fun output () =
7890
0aa16bc2abdb use_text: remove last char from output;
wenzelm
parents: 7855
diff changeset
    95
      let val str = implode (rev (! out_buffer))
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
    96
      in String.substring (str, 0, Int.max (0, size str - 1)) end;
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    97
  in
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    98
    Compiler.Control.Print.out := out;
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
    99
    Compiler.Interact.useStream (TextIO.openString txt) handle exn =>
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
   100
      (Compiler.Control.Print.out := out_orig; err (output ()); raise exn);
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   101
    Compiler.Control.Print.out := out_orig;
10914
aded4ba99b88 use_text etc.: proper output of error messages;
wenzelm
parents: 10725
diff changeset
   102
    if verbose then print (output ()) else ()
5090
75ac9b451ae0 use_text: verbose flag;
wenzelm
parents: 5038
diff changeset
   103
  end;
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   104
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   105
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   106
(** interrupts **)
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   107
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   108
exception Interrupt;
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   109
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   110
local
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   111
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   112
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   113
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   114
fun release NONE = ()
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   115
  | release (SOME exn) = raise exn;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   116
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   117
in
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   118
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   119
fun ignore_interrupt f x =
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   120
  let
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   121
    val old_handler = Signals.setHandler (Signals.sigINT, Signals.IGNORE);
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   122
    val result = capture f x;
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   123
    val _ = Signals.setHandler (Signals.sigINT, old_handler);
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   124
  in release result end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   125
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   126
fun raise_interrupt f x =
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   127
  let
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   128
    val interrupted = ref false;
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   129
    val result = ref NONE;
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   130
    val old_handler = Signals.inqHandler Signals.sigINT;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   131
  in
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   132
    SMLofNJ.Cont.callcc (fn cont =>
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   133
      (Signals.setHandler (Signals.sigINT, Signals.HANDLER (fn _ => (interrupted := true; cont)));
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   134
      result := capture f x));
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   135
    Signals.setHandler (Signals.sigINT, old_handler);
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   136
    if ! interrupted then raise Interrupt else release (! result)
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   137
  end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   138
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   139
end;
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   140
17529
a436d89845af use "ML-Systems/smlnj-basis-compat.ML" *after* Interrupt;
wenzelm
parents: 17511
diff changeset
   141
(case #version_id (Compiler.version) of
a436d89845af use "ML-Systems/smlnj-basis-compat.ML" *after* Interrupt;
wenzelm
parents: 17511
diff changeset
   142
  [110, x] => if x >= 44
a436d89845af use "ML-Systems/smlnj-basis-compat.ML" *after* Interrupt;
wenzelm
parents: 17511
diff changeset
   143
              then use "ML-Systems/smlnj-basis-compat.ML"
a436d89845af use "ML-Systems/smlnj-basis-compat.ML" *after* Interrupt;
wenzelm
parents: 17511
diff changeset
   144
              else ()
a436d89845af use "ML-Systems/smlnj-basis-compat.ML" *after* Interrupt;
wenzelm
parents: 17511
diff changeset
   145
| _ => ());
a436d89845af use "ML-Systems/smlnj-basis-compat.ML" *after* Interrupt;
wenzelm
parents: 17511
diff changeset
   146
17511
wenzelm
parents: 16681
diff changeset
   147
18790
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   148
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   149
(* bounded time execution *)
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   150
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   151
use "ML-Systems/smlnj-interrupt-timeout.ML";
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   152
418131f631fc interrupt_timeout now raises Interrupt instead of SML90.Interrupt
webertj
parents: 18760
diff changeset
   153
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   154
(** 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
   155
    Posix.Signal.signal and Signals.signal differ **)
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   156
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   157
structure IsaSignal =
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   158
struct
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   159
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   160
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
   161
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   162
(*From the SML/NJ documentation: "HANDLER(f) installs a handler for a
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   163
  signal. When signal is delivered to the process, the execution state
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   164
  of the current thread will be bundled up as a continuation k, then
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   165
  f(signal,n,k) will be called. The number n is the number of times
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   166
  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
   167
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   168
fun toAction SIG_DFL = Signals.DEFAULT
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   169
  | toAction SIG_IGN = Signals.IGNORE
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   170
  | toAction (SIG_HANDLE iu) =
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   171
      Signals.HANDLER (fn (signo,_,cont) => (iu signo; cont));
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   172
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   173
(*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
   174
fun fromAction Signals.DEFAULT = SIG_DFL
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   175
  | fromAction Signals.IGNORE = SIG_IGN
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   176
  | fromAction (Signals.HANDLER f) =
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   177
      SIG_HANDLE (fn signo => SMLofNJ.Cont.callcc (fn k => (f (signo,0,k); ())));
16542
1259088dc448 tuned pointer_eq;
wenzelm
parents: 16528
diff changeset
   178
15702
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   179
(*Poly/ML version has type  int * sig_handle -> sig_handle*)
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   180
fun signal (signo, sh) = fromAction (Signals.setHandler (signo, toAction sh));
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   181
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   182
val usr1 = UnixSignals.sigUSR1
2677db44c795 new signalling primmitives for sml/nj compatibility
paulson
parents: 14981
diff changeset
   183
val usr2 = UnixSignals.sigUSR2
17763
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   184
val alrm = UnixSignals.sigALRM
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   185
val chld = UnixSignals.sigCHLD
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   186
val cont = UnixSignals.sigCONT
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   187
val int = UnixSignals.sigINT
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   188
val quit = UnixSignals.sigQUIT
6f933b702f44 more signals
paulson
parents: 17529
diff changeset
   189
val term = UnixSignals.sigTERM
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
end;
5816
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   192
6f3cb53502fa smart interrupt handler;
wenzelm
parents: 5708
diff changeset
   193
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   194
(** OS related **)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   195
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   196
(* system command execution *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   197
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   198
(*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
   199
  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
   200
  but that function doesn't use the PATH*)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   201
fun execute command =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   202
  let
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   203
    val tmp_name = OS.FileSys.tmpName ();
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   204
    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
   205
    val result = TextIO.inputAll is;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   206
  in
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   207
    TextIO.closeIn is;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   208
    OS.FileSys.remove tmp_name;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   209
    result
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   210
  end;
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   211
7855
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   212
(*plain version; with return code*)
12990
c11adf2b1c1e renamed mask_interrupt to ignore_interrupt;
wenzelm
parents: 12874
diff changeset
   213
val system = OS.Process.system: string -> int;
7855
092a6435afad system;
wenzelm
parents: 6227
diff changeset
   214
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   215
17824
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   216
(*Convert a process ID to a decimal string (chiefly for tracing)*)
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   217
fun string_of_pid pid = 
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   218
    Word.fmt StringCvt.DEC (Word.fromLargeWord (Posix.Process.pidToWord pid));
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   219
36b2978d339a added string_of_pid;
wenzelm
parents: 17763
diff changeset
   220
4403
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   221
(* getenv *)
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   222
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   223
fun getenv var =
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   224
  (case OS.Process.getEnv var of
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   225
    NONE => ""
1914f727f93f Compatibility file for Standard ML of New Jersey.
wenzelm
parents:
diff changeset
   226
  | SOME txt => txt);