| author | wenzelm |
| Fri, 02 Feb 2001 23:59:30 +0100 | |
| changeset 11039 | 55de839f4850 |
| parent 10914 | aded4ba99b88 |
| permissions | -rw-r--r-- |
| 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 |
||
| 10708 | 24 |
val ord = SML90.ord; |
25 |
val chr = SML90.chr; |
|
26 |
val explode = SML90.explode; |
|
27 |
val implode = SML90.implode; |
|
| 9735 | 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 |
||
| 10914 | 71 |
fun use_text (print, err) verbose txt = |
| 9735 | 72 |
let |
73 |
val in_buffer = ref (explode txt); |
|
74 |
val out_buffer = ref ([]: string list); |
|
| 10914 | 75 |
fun output () = drop_last_char (implode (rev (! out_buffer))); |
| 9735 | 76 |
|
77 |
fun get () = |
|
78 |
(case ! in_buffer of |
|
79 |
[] => "" |
|
80 |
| c :: cs => (in_buffer := cs; c)); |
|
81 |
fun put s = out_buffer := s :: ! out_buffer; |
|
82 |
||
83 |
fun exec () = |
|
84 |
(case ! in_buffer of |
|
85 |
[] => () |
|
86 |
| _ => (PolyML.compiler (get, put) (); exec ())); |
|
87 |
in |
|
| 10914 | 88 |
exec () handle exn => (err (output ()); raise exn); |
89 |
if verbose then print (output ()) else () |
|
| 9735 | 90 |
end; |
91 |
||
92 |
end; |
|
93 |
||
94 |
||
95 |
||
| 9766 | 96 |
(** interrupts **) |
97 |
||
| 10518 | 98 |
exception Interrupt = SML90.Interrupt; |
|
10517
b9f7adf3ff11
added exception Interrupt for use in function Library/try
paulson
parents:
10209
diff
changeset
|
99 |
|
| 9766 | 100 |
local |
101 |
||
102 |
datatype 'a result = |
|
103 |
Result of 'a | |
|
104 |
Exn of exn; |
|
105 |
||
106 |
fun capture f x = Result (f x) handle exn => Exn exn; |
|
107 |
||
108 |
fun release (Result x) = x |
|
109 |
| release (Exn exn) = raise exn; |
|
110 |
||
| 9735 | 111 |
|
| 9766 | 112 |
val sig_int = 2; |
113 |
||
114 |
fun change_signal new_handler f x = |
|
115 |
let |
|
116 |
(*RACE wrt. other signals*) |
|
117 |
val old_handler = Signal.signal (sig_int, new_handler); |
|
118 |
val result = capture f x; |
|
119 |
val _ = Signal.signal (sig_int, old_handler); |
|
120 |
in release result end; |
|
121 |
||
|
10209
b24210573eca
install default_handler for SIGINT initially as well;
wenzelm
parents:
10194
diff
changeset
|
122 |
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ()); |
|
b24210573eca
install default_handler for SIGINT initially as well;
wenzelm
parents:
10194
diff
changeset
|
123 |
|
| 9766 | 124 |
in |
125 |
||
|
10209
b24210573eca
install default_handler for SIGINT initially as well;
wenzelm
parents:
10194
diff
changeset
|
126 |
val _ = Signal.signal (sig_int, default_handler); |
|
b24210573eca
install default_handler for SIGINT initially as well;
wenzelm
parents:
10194
diff
changeset
|
127 |
|
| 9766 | 128 |
fun mask_interrupt f = change_signal Signal.SIG_IGN f; |
|
10209
b24210573eca
install default_handler for SIGINT initially as well;
wenzelm
parents:
10194
diff
changeset
|
129 |
fun exhibit_interrupt f = change_signal default_handler f; |
| 9766 | 130 |
|
131 |
end; |
|
| 9735 | 132 |
|
133 |
||
134 |
||
135 |
(** OS related **) |
|
136 |
||
137 |
(* system command execution *) |
|
138 |
||
139 |
(*execute Unix command which doesn't take any input from stdin and |
|
140 |
sends its output to stdout; could be done more easily by Unix.execute, |
|
141 |
but that function doesn't use the PATH*) |
|
142 |
fun execute command = |
|
143 |
let |
|
144 |
val tmp_name = OS.FileSys.tmpName (); |
|
145 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
146 |
val result = TextIO.inputAll is; |
|
147 |
in |
|
148 |
TextIO.closeIn is; |
|
149 |
OS.FileSys.remove tmp_name; |
|
150 |
result |
|
151 |
end; |
|
152 |
||
153 |
(*plain version; with return code*) |
|
154 |
fun system cmd = |
|
155 |
if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1; |
|
156 |
||
157 |
||
158 |
(* file handling *) |
|
159 |
||
160 |
(*get time of last modification*) |
|
161 |
fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => ""; |
|
162 |
||
163 |
||
164 |
(* getenv *) |
|
165 |
||
166 |
fun getenv var = |
|
167 |
(case OS.Process.getEnv var of |
|
168 |
NONE => "" |
|
169 |
| SOME txt => txt); |
|
170 |
||
171 |
||
172 |
(* non-ASCII input (see also Thy/use.ML) *) |
|
173 |
||
174 |
val needs_filtered_use = true; |