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