9735
|
1 |
(* Title: Pure/ML-Systems/polyml.ML
|
|
2 |
ID: $Id$
|
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory
|
|
4 |
Copyright 1991 University of Cambridge
|
|
5 |
|
9738
|
6 |
Compatibility file for Poly/ML (version 4.0).
|
9735
|
7 |
*)
|
|
8 |
|
|
9 |
(** ML system related **)
|
|
10 |
|
9766
|
11 |
(* old Poly/ML emulation *)
|
|
12 |
|
|
13 |
local
|
|
14 |
val orig_exit = exit;
|
|
15 |
in
|
|
16 |
open PolyML;
|
|
17 |
val exit = orig_exit;
|
|
18 |
fun quit () = exit 0;
|
|
19 |
end;
|
9735
|
20 |
|
|
21 |
|
|
22 |
(* restore old-style character / string functions *)
|
|
23 |
|
|
24 |
fun ord s = Char.ord (String.sub (s, 0));
|
|
25 |
val chr = str o Char.chr;
|
|
26 |
val explode = (map str) o String.explode;
|
|
27 |
val implode = String.concat;
|
|
28 |
|
|
29 |
|
|
30 |
(*Note start point for timing*)
|
|
31 |
fun startTiming() =
|
|
32 |
let val CPUtimer = Timer.startCPUTimer();
|
|
33 |
val time = Timer.checkCPUTimer(CPUtimer)
|
|
34 |
in (CPUtimer,time) end;
|
|
35 |
|
|
36 |
(*Finish timing and return string*)
|
|
37 |
fun endTiming (CPUtimer, {sys,usr}) =
|
|
38 |
let open Time (*...for Time.toString, Time.+ and Time.- *)
|
|
39 |
val {sys=sys2,usr=usr2} = Timer.checkCPUTimer(CPUtimer)
|
|
40 |
in "User " ^ toString (usr2-usr) ^
|
|
41 |
" All "^ toString (sys2-sys + usr2-usr) ^
|
|
42 |
" secs"
|
|
43 |
handle Time => ""
|
|
44 |
end;
|
|
45 |
|
|
46 |
|
|
47 |
(* prompts *)
|
|
48 |
|
|
49 |
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
|
|
50 |
|
|
51 |
|
|
52 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
|
|
53 |
|
9766
|
54 |
fun make_pp _ pprint (str, blk, brk, en) _ _ obj =
|
9735
|
55 |
pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
|
|
56 |
fn () => brk (99999, 0), en);
|
|
57 |
|
|
58 |
|
|
59 |
(* ML command execution -- 'eval' *)
|
|
60 |
|
|
61 |
local
|
|
62 |
|
|
63 |
fun drop_last [] = []
|
|
64 |
| drop_last [x] = []
|
|
65 |
| drop_last (x :: xs) = x :: drop_last xs;
|
|
66 |
|
|
67 |
val drop_last_char = implode o drop_last o explode;
|
|
68 |
|
|
69 |
in
|
|
70 |
|
|
71 |
fun use_text print verbose txt =
|
|
72 |
let
|
|
73 |
val in_buffer = ref (explode txt);
|
|
74 |
val out_buffer = ref ([]: string list);
|
|
75 |
|
|
76 |
fun get () =
|
|
77 |
(case ! in_buffer of
|
|
78 |
[] => ""
|
|
79 |
| c :: cs => (in_buffer := cs; c));
|
|
80 |
fun put s = out_buffer := s :: ! out_buffer;
|
|
81 |
|
|
82 |
fun exec () =
|
|
83 |
(case ! in_buffer of
|
|
84 |
[] => ()
|
|
85 |
| _ => (PolyML.compiler (get, put) (); exec ()));
|
|
86 |
|
|
87 |
fun show_output () = print (drop_last_char (implode (rev (! out_buffer))));
|
|
88 |
in
|
|
89 |
exec () handle exn => (show_output (); raise exn);
|
|
90 |
if verbose then show_output () else ()
|
|
91 |
end;
|
|
92 |
|
|
93 |
end;
|
|
94 |
|
|
95 |
|
|
96 |
|
9766
|
97 |
(** interrupts **)
|
|
98 |
|
|
99 |
local
|
|
100 |
|
|
101 |
datatype 'a result =
|
|
102 |
Result of 'a |
|
|
103 |
Exn of exn;
|
|
104 |
|
|
105 |
fun capture f x = Result (f x) handle exn => Exn exn;
|
|
106 |
|
|
107 |
fun release (Result x) = x
|
|
108 |
| release (Exn exn) = raise exn;
|
|
109 |
|
9735
|
110 |
|
9766
|
111 |
val sig_int = 2;
|
|
112 |
|
|
113 |
fun change_signal new_handler f x =
|
|
114 |
let
|
|
115 |
(*RACE wrt. other signals*)
|
|
116 |
val old_handler = Signal.signal (sig_int, new_handler);
|
|
117 |
val result = capture f x;
|
|
118 |
val _ = Signal.signal (sig_int, old_handler);
|
|
119 |
in release result end;
|
|
120 |
|
|
121 |
in
|
|
122 |
|
|
123 |
fun mask_interrupt f = change_signal Signal.SIG_IGN f;
|
|
124 |
fun exhibit_interrupt f = change_signal Signal.SIG_DFL f;
|
|
125 |
|
|
126 |
end;
|
9735
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
(** OS related **)
|
|
131 |
|
|
132 |
(* system command execution *)
|
|
133 |
|
|
134 |
(*execute Unix command which doesn't take any input from stdin and
|
|
135 |
sends its output to stdout; could be done more easily by Unix.execute,
|
|
136 |
but that function doesn't use the PATH*)
|
|
137 |
fun execute command =
|
|
138 |
let
|
|
139 |
val tmp_name = OS.FileSys.tmpName ();
|
|
140 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
|
|
141 |
val result = TextIO.inputAll is;
|
|
142 |
in
|
|
143 |
TextIO.closeIn is;
|
|
144 |
OS.FileSys.remove tmp_name;
|
|
145 |
result
|
|
146 |
end;
|
|
147 |
|
|
148 |
(*plain version; with return code*)
|
|
149 |
fun system cmd =
|
|
150 |
if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1;
|
|
151 |
|
|
152 |
|
|
153 |
(* file handling *)
|
|
154 |
|
|
155 |
(*get time of last modification*)
|
|
156 |
fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => "";
|
|
157 |
|
|
158 |
|
|
159 |
(* getenv *)
|
|
160 |
|
|
161 |
fun getenv var =
|
|
162 |
(case OS.Process.getEnv var of
|
|
163 |
NONE => ""
|
|
164 |
| SOME txt => txt);
|
|
165 |
|
|
166 |
|
|
167 |
(* non-ASCII input (see also Thy/use.ML) *)
|
|
168 |
|
|
169 |
val needs_filtered_use = true;
|