2413
|
1 |
(* Title: Pure/ML-Systems/smlnj-0.93.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Carsten Clasohm, TU Muenchen
|
|
4 |
Copyright 1996 TU Muenchen
|
|
5 |
|
|
6 |
Compatibility file for Standard ML of New Jersey version 0.93.
|
|
7 |
*)
|
|
8 |
|
3594
|
9 |
(*needs the Basis Library emulation*)
|
|
10 |
use "basis.ML";
|
2413
|
11 |
|
|
12 |
|
3594
|
13 |
(** ML system related **)
|
|
14 |
|
|
15 |
(* New Jersey ML parameters *)
|
|
16 |
|
|
17 |
System.Control.Runtime.gcmessages := 0;
|
|
18 |
|
|
19 |
|
|
20 |
(* Poly/ML emulation *)
|
2413
|
21 |
|
|
22 |
fun quit () = exit 0;
|
|
23 |
|
3594
|
24 |
(*limit the printing depth -- divided by 2 for comparibility with Poly/ML*)
|
|
25 |
fun print_depth n =
|
|
26 |
(System.Control.Print.printDepth := n div 2;
|
|
27 |
System.Control.Print.printLength := n);
|
|
28 |
|
|
29 |
|
4506
|
30 |
|
3594
|
31 |
(* timing *)
|
2413
|
32 |
|
4506
|
33 |
(*Note start point for timing*)
|
|
34 |
fun startTiming() = System.Timer.start_timer();
|
|
35 |
|
|
36 |
(*Finish timing and return string*)
|
3594
|
37 |
local
|
3631
|
38 |
(*print microseconds, suppressing trailing zeroes*)
|
|
39 |
fun umakestring 0 = ""
|
|
40 |
| umakestring n =
|
|
41 |
chr (ord "0" + n div 100000) ^ umakestring (10 * (n mod 100000));
|
4506
|
42 |
|
|
43 |
fun string_of_time (System.Timer.TIME {sec, usec}) =
|
|
44 |
makestring sec ^ "." ^ (if usec=0 then "0" else umakestring usec)
|
|
45 |
|
3594
|
46 |
in
|
4506
|
47 |
|
|
48 |
fun endTiming start =
|
|
49 |
let val nongc = System.Timer.check_timer(start)
|
|
50 |
and gc = System.Timer.check_timer_gc(start);
|
|
51 |
val both = System.Timer.add_time(nongc, gc)
|
|
52 |
in "Non GC " ^ string_of_time nongc ^
|
|
53 |
" GC " ^ string_of_time gc ^
|
|
54 |
" both "^ string_of_time both ^ " secs\n"
|
|
55 |
end
|
3594
|
56 |
end;
|
|
57 |
|
|
58 |
|
4977
|
59 |
(* prompts *)
|
|
60 |
|
|
61 |
fun ml_prompts p1 p2 = ();
|
4506
|
62 |
|
|
63 |
|
3594
|
64 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
|
2413
|
65 |
|
|
66 |
fun make_pp path pprint =
|
|
67 |
let
|
|
68 |
open System.PrettyPrint;
|
|
69 |
|
|
70 |
fun pp pps obj =
|
|
71 |
pprint obj
|
|
72 |
(add_string pps, begin_block pps INCONSISTENT,
|
|
73 |
fn wd => add_break pps (wd, 0), fn () => add_newline pps,
|
|
74 |
fn () => end_block pps);
|
|
75 |
in
|
|
76 |
(path, pp)
|
|
77 |
end;
|
|
78 |
|
|
79 |
fun install_pp (path, pp) = System.PrettyPrint.install_pp path pp;
|
|
80 |
|
|
81 |
|
3594
|
82 |
(* ML command execution *)
|
2413
|
83 |
|
5090
|
84 |
fun use_text _ = System.Compile.use_stream o open_string;
|
3594
|
85 |
|
2413
|
86 |
|
|
87 |
|
5816
|
88 |
(** interrupts **)
|
|
89 |
|
|
90 |
local
|
|
91 |
|
|
92 |
datatype 'a result =
|
|
93 |
Result of 'a |
|
|
94 |
Exn of exn;
|
|
95 |
|
|
96 |
fun capture f x = Result (f x) handle exn => Exn exn;
|
|
97 |
|
|
98 |
fun release (Result x) = x
|
|
99 |
| release (Exn exn) = raise exn;
|
|
100 |
|
|
101 |
|
|
102 |
val sig_int = System.Signals.SIGINT;
|
|
103 |
|
|
104 |
val interruptible = not o System.Signals.masked;
|
|
105 |
fun mask_signals () = System.Signals.maskSignals true;
|
|
106 |
fun unmask_signals () = System.Signals.maskSignals false;
|
|
107 |
|
|
108 |
fun change_mask ok change unchange f x =
|
|
109 |
if ok () then f x
|
|
110 |
else
|
|
111 |
let
|
|
112 |
val _ = change ();
|
|
113 |
val result = capture f x;
|
|
114 |
val _ = unchange ();
|
|
115 |
in release result end;
|
|
116 |
|
|
117 |
in
|
|
118 |
|
|
119 |
|
|
120 |
(* mask / unmask interrupt *)
|
|
121 |
|
|
122 |
fun mask_interrupt f = change_mask (not o interruptible) mask_signals unmask_signals f;
|
|
123 |
fun unmask_interrupt f = change_mask interruptible unmask_signals mask_signals f;
|
|
124 |
|
|
125 |
|
|
126 |
(* exhibit interrupt (via exception) *)
|
|
127 |
|
|
128 |
exception Interrupt;
|
|
129 |
|
|
130 |
fun exhibit_interrupt f x =
|
|
131 |
let
|
|
132 |
val orig_handler = System.Signals.inqHandler sig_int;
|
|
133 |
fun reset_handler () = (System.Signals.setHandler (sig_int, orig_handler); ());
|
|
134 |
|
|
135 |
val interrupted = ref false;
|
|
136 |
|
|
137 |
fun set_handler cont =
|
|
138 |
System.Signals.setHandler (sig_int, SOME (fn _ => (interrupted := true; cont)));
|
|
139 |
|
|
140 |
fun proceed cont =
|
|
141 |
let
|
|
142 |
val _ = set_handler cont;
|
|
143 |
val result = unmask_interrupt (capture f) x;
|
|
144 |
val _ = reset_handler ();
|
|
145 |
in release result end;
|
|
146 |
in
|
|
147 |
callcc proceed;
|
|
148 |
reset_handler ();
|
|
149 |
if ! interrupted then raise Interrupt else ()
|
|
150 |
end;
|
|
151 |
|
|
152 |
end;
|
|
153 |
|
|
154 |
|
|
155 |
|
3594
|
156 |
(** OS related **)
|
2413
|
157 |
|
3594
|
158 |
(* system command execution *)
|
2413
|
159 |
|
3594
|
160 |
(*execute Unix command which doesn't take any input from stdin and
|
|
161 |
sends its output to stdout; could be done more easily by IO.execute,
|
|
162 |
but that function seems to be buggy in SML/NJ 0.93*)
|
|
163 |
fun execute command =
|
|
164 |
let
|
4494
|
165 |
val tmp_name = "/tmp/isabelle-execute";
|
3594
|
166 |
val is = (System.system (command ^ " > " ^ tmp_name); open_in tmp_name);
|
|
167 |
val result = input (is, 999999);
|
|
168 |
in
|
|
169 |
close_in is;
|
|
170 |
System.Unsafe.SysIO.unlink tmp_name;
|
|
171 |
result
|
|
172 |
end;
|
2413
|
173 |
|
|
174 |
|
3594
|
175 |
(* file handling *)
|
2413
|
176 |
|
|
177 |
(*Get time of last modification; if file doesn't exist return an empty string*)
|
|
178 |
local
|
3594
|
179 |
open System.Timer System.Unsafe.SysIO;
|
2413
|
180 |
in
|
|
181 |
fun file_info "" = ""
|
3594
|
182 |
| file_info name = makestring (mtime (PATH name)) handle _ => "";
|
2413
|
183 |
end;
|
|
184 |
|
|
185 |
structure OS =
|
3594
|
186 |
struct
|
2413
|
187 |
structure FileSys =
|
3594
|
188 |
struct
|
|
189 |
val chDir = System.Directory.cd;
|
|
190 |
val remove = System.Unsafe.SysIO.unlink;
|
|
191 |
val getDir = System.Directory.getWD;
|
2413
|
192 |
end;
|
3594
|
193 |
end;
|
2413
|
194 |
|
3594
|
195 |
(*redefine to flush its output immediately -- temporary patch suggested
|
5816
|
196 |
by Kim Dam Petersen*) (* FIXME !? *)
|
3594
|
197 |
val output = fn (s, t) => (output (s, t); flush_out s);
|
2413
|
198 |
|
|
199 |
|
3594
|
200 |
(* getenv *)
|
2413
|
201 |
|
3594
|
202 |
local
|
|
203 |
fun drop_last [] = []
|
|
204 |
| drop_last [x] = []
|
|
205 |
| drop_last (x :: xs) = x :: drop_last xs;
|
2413
|
206 |
|
3594
|
207 |
val drop_last_char = implode o drop_last o explode;
|
|
208 |
in
|
|
209 |
fun getenv var = drop_last_char
|
|
210 |
(execute ("env | grep '^" ^ var ^ "=' | sed -e 's/" ^ var ^ "=//'"));
|
|
211 |
end;
|
2413
|
212 |
|
|
213 |
|
4428
|
214 |
(* non-ASCII input (see also Thy/use.ML) *)
|
2413
|
215 |
|
|
216 |
val needs_filtered_use = false;
|