author | webertj |
Tue, 01 Jun 2004 00:18:01 +0200 | |
changeset 14850 | 393a7be73160 |
parent 14519 | 4ca3608fdf4f |
child 14870 | c5cf7c001313 |
permissions | -rw-r--r-- |
2341 | 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 |
||
11078 | 6 |
Compatibility file for Poly/ML (version 4.0). |
2341 | 7 |
*) |
8 |
||
11078 | 9 |
(** ML system related **) |
2341 | 10 |
|
11078 | 11 |
(* old Poly/ML emulation *) |
2341 | 12 |
|
11078 | 13 |
local |
14 |
val orig_exit = exit; |
|
15 |
in |
|
16 |
open PolyML; |
|
17 |
val exit = orig_exit; |
|
18 |
fun quit () = exit 0; |
|
7148 | 19 |
end; |
6042 | 20 |
|
2341 | 21 |
|
11078 | 22 |
(* restore old-style character / string functions *) |
3588 | 23 |
|
11078 | 24 |
val ord = SML90.ord; |
25 |
val chr = SML90.chr; |
|
26 |
val explode = SML90.explode; |
|
27 |
val implode = SML90.implode; |
|
28 |
||
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12989
diff
changeset
|
29 |
(* compiler-independent timing functions *) |
2341 | 30 |
|
14519
4ca3608fdf4f
Added support for the newer versions of SML/NJ, which break several of the
skalberg
parents:
12989
diff
changeset
|
31 |
use "ML-Systems/cpu-timer-basis.ML"; |
2341 | 32 |
|
14850 | 33 |
(* bounded time execution *) |
34 |
||
35 |
use "ML-Systems/polyml-time-limit.ML"; |
|
36 |
||
4977 | 37 |
(* prompts *) |
38 |
||
4984 | 39 |
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2); |
4977 | 40 |
|
41 |
||
3588 | 42 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *) |
2341 | 43 |
|
11078 | 44 |
fun make_pp _ pprint (str, blk, brk, en) _ _ obj = |
2341 | 45 |
pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0), |
46 |
fn () => brk (99999, 0), en); |
|
47 |
||
48 |
||
3588 | 49 |
(* ML command execution -- 'eval' *) |
50 |
||
7890 | 51 |
local |
52 |
||
53 |
fun drop_last [] = [] |
|
54 |
| drop_last [x] = [] |
|
55 |
| drop_last (x :: xs) = x :: drop_last xs; |
|
56 |
||
57 |
val drop_last_char = implode o drop_last o explode; |
|
58 |
||
59 |
in |
|
60 |
||
10914 | 61 |
fun use_text (print, err) verbose txt = |
3588 | 62 |
let |
5090 | 63 |
val in_buffer = ref (explode txt); |
64 |
val out_buffer = ref ([]: string list); |
|
10914 | 65 |
fun output () = drop_last_char (implode (rev (! out_buffer))); |
5090 | 66 |
|
5038 | 67 |
fun get () = |
5090 | 68 |
(case ! in_buffer of |
5038 | 69 |
[] => "" |
5090 | 70 |
| c :: cs => (in_buffer := cs; c)); |
71 |
fun put s = out_buffer := s :: ! out_buffer; |
|
72 |
||
5038 | 73 |
fun exec () = |
5090 | 74 |
(case ! in_buffer of |
5038 | 75 |
[] => () |
5090 | 76 |
| _ => (PolyML.compiler (get, put) (); exec ())); |
77 |
in |
|
10914 | 78 |
exec () handle exn => (err (output ()); raise exn); |
79 |
if verbose then print (output ()) else () |
|
5090 | 80 |
end; |
3588 | 81 |
|
11078 | 82 |
end; |
83 |
||
84 |
||
85 |
||
86 |
(** interrupts **) |
|
87 |
||
88 |
exception Interrupt = SML90.Interrupt; |
|
89 |
||
90 |
local |
|
91 |
||
12989 | 92 |
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn; |
11078 | 93 |
|
12989 | 94 |
fun release NONE = () |
95 |
| release (SOME exn) = raise exn; |
|
3588 | 96 |
|
11078 | 97 |
val sig_int = 2; |
98 |
||
99 |
fun change_signal new_handler f x = |
|
100 |
let |
|
101 |
(*RACE wrt. other signals*) |
|
102 |
val old_handler = Signal.signal (sig_int, new_handler); |
|
103 |
val result = capture f x; |
|
104 |
val _ = Signal.signal (sig_int, old_handler); |
|
105 |
in release result end; |
|
5813 | 106 |
|
11078 | 107 |
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ()); |
108 |
||
109 |
in |
|
110 |
||
111 |
val _ = Signal.signal (sig_int, default_handler); |
|
112 |
||
12989 | 113 |
fun ignore_interrupt f = change_signal Signal.SIG_IGN f; |
114 |
fun raise_interrupt f = change_signal default_handler f; |
|
11078 | 115 |
|
116 |
end; |
|
5813 | 117 |
|
118 |
||
119 |
||
3588 | 120 |
(** OS related **) |
2341 | 121 |
|
3588 | 122 |
(* system command execution *) |
2341 | 123 |
|
3588 | 124 |
(*execute Unix command which doesn't take any input from stdin and |
11078 | 125 |
sends its output to stdout; could be done more easily by Unix.execute, |
126 |
but that function doesn't use the PATH*) |
|
3588 | 127 |
fun execute command = |
128 |
let |
|
11078 | 129 |
val tmp_name = OS.FileSys.tmpName (); |
130 |
val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name); |
|
131 |
val result = TextIO.inputAll is; |
|
132 |
in |
|
133 |
TextIO.closeIn is; |
|
134 |
OS.FileSys.remove tmp_name; |
|
135 |
result |
|
136 |
end; |
|
7855 | 137 |
|
11078 | 138 |
(*plain version; with return code*) |
139 |
fun system cmd = |
|
140 |
if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1; |
|
2341 | 141 |
|
142 |
||
3588 | 143 |
(* file handling *) |
2341 | 144 |
|
6227 | 145 |
(*get time of last modification*) |
11078 | 146 |
fun file_info name = Time.toString (OS.FileSys.modTime name) handle _ => ""; |
3588 | 147 |
|
148 |
||
149 |
(* getenv *) |
|
150 |
||
11078 | 151 |
fun getenv var = |
152 |
(case OS.Process.getEnv var of |
|
153 |
NONE => "" |
|
154 |
| SOME txt => txt); |