author | paulson |
Thu, 22 May 1997 15:10:37 +0200 | |
changeset 3298 | 5f0ed3caa991 |
parent 2724 | ddc6cf6b62e9 |
child 3406 | 131262e21ada |
permissions | -rw-r--r-- |
0 | 1 |
(* Title: Pure/POLY |
2 |
ID: $Id$ |
|
3 |
Author: Lawrence C Paulson, Cambridge University Computer Laboratory |
|
4 |
Copyright 1991 University of Cambridge |
|
5 |
||
6 |
Compatibility file for Poly/ML (AHL release 1.88) |
|
7 |
*) |
|
8 |
||
9 |
open PolyML ExtendedIO; |
|
10 |
||
2242
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
11 |
use"basis.ML"; |
2141 | 12 |
|
0 | 13 |
(*A conditional timing function: applies f to () and, if the flag is true, |
14 |
prints its runtime.*) |
|
15 |
||
16 |
fun cond_timeit flag f = |
|
17 |
if flag then |
|
18 |
let val before = System.processtime() |
|
19 |
val result = f() |
|
20 |
val both = real(System.processtime() - before) / 10.0 |
|
21 |
in output(std_out, "Process time (incl GC): "^ |
|
22 |
makestring both ^ " secs\n"); |
|
23 |
result |
|
24 |
end |
|
25 |
else f(); |
|
26 |
||
27 |
||
28 |
(*Save function: in Poly/ML, ignores filename and commits to present file*) |
|
29 |
||
30 |
fun xML filename banner = commit(); |
|
31 |
||
32 |
||
33 |
(*Interface for toplevel pretty printers, see also Pure/install_pp.ML*) |
|
34 |
||
35 |
fun make_pp _ pprint (str, blk, brk, en) obj = |
|
19 | 36 |
pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0), |
37 |
fn () => brk (99999, 0), en); |
|
0 | 38 |
|
39 |
||
66 | 40 |
(*** File handling ***) |
0 | 41 |
|
100
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
42 |
local |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
43 |
|
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
44 |
(*These are functions from library.ML *) |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
45 |
fun take_suffix _ [] = ([], []) |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
46 |
| take_suffix pred (x :: xs) = |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
47 |
(case take_suffix pred xs of |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
48 |
([], sffx) => if pred x then ([], x :: sffx) |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
49 |
else ([x], sffx) |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
50 |
| (prfx, sffx) => (x :: prfx, sffx)); |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
51 |
|
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
52 |
fun apr (f,y) x = f(x,y); |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
53 |
|
396
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
54 |
fun seq (proc: 'a -> unit) : 'a list -> unit = |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
55 |
let fun seqf [] = () |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
56 |
| seqf (x :: xs) = (proc x; seqf xs) |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
57 |
in seqf end; |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
58 |
|
1437 | 59 |
fun radixpand num : int list = |
60 |
let fun radix (n, tail) = |
|
61 |
if n < 10 then n :: tail else radix (n div 10, (n mod 10) :: tail) |
|
62 |
in radix (num, []) end; |
|
63 |
||
64 |
fun radixstring num = |
|
65 |
let fun chrof n = chr (ord "0" + n); |
|
66 |
in implode (map chrof (radixpand num)) end; |
|
67 |
||
100
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
68 |
in |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
69 |
|
1480 | 70 |
(*Get time of last modification; if file doesn't exist return an empty string*) |
0 | 71 |
fun file_info "" = "" |
1480 | 72 |
| file_info name = radixstring (System.filedate name) handle _ => ""; |
66 | 73 |
|
2242
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
74 |
|
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
75 |
structure OS = |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
76 |
struct |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
77 |
structure FileSys = |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
78 |
struct |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
79 |
val chDir = PolyML.cd |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
80 |
|
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
81 |
fun remove name = (*Delete a file *) |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
82 |
let val (is, os) = ExtendedIO.execute ("rm " ^ name) |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
83 |
in close_in is; close_out os end; |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
84 |
|
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
85 |
(*Get pathname of current working directory *) |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
86 |
fun getDir () = |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
87 |
let val (is, os) = ExtendedIO.execute ("pwd") |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
88 |
val (path, _) = take_suffix (apr(op=,"\n")) (*remove newline*) |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
89 |
(explode (ExtendedIO.input_line is)) |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
90 |
in close_in is; close_out os; implode path end; |
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
91 |
|
fa8c6c695a97
Basis library emulation, especially of I/O operations
paulson
parents:
2141
diff
changeset
|
92 |
end |
100
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
93 |
end; |
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
94 |
|
e95b98536b3d
fixed a bug in POLY.ML: delete_file didn't close streams;
clasohm
parents:
73
diff
changeset
|
95 |
|
378
85ff48546a05
added use_string: string -> unit to execute ML commands passed in a string
clasohm
parents:
100
diff
changeset
|
96 |
|
85ff48546a05
added use_string: string -> unit to execute ML commands passed in a string
clasohm
parents:
100
diff
changeset
|
97 |
(*** ML command execution ***) |
85ff48546a05
added use_string: string -> unit to execute ML commands passed in a string
clasohm
parents:
100
diff
changeset
|
98 |
|
396
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
99 |
val use_string = |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
100 |
let fun exec command = |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
101 |
let val firstLine = ref true; |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
102 |
|
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
103 |
fun getLine () = |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
104 |
if !firstLine |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
105 |
then (firstLine := false; command) |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
106 |
else raise Io "use_string: buffer exhausted" |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
107 |
in PolyML.compiler (getLine, fn s => output (std_out, s)) () end |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
108 |
in seq exec end; |
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
109 |
|
18c9c28d0f7e
changed use_string's type to string list -> unit because POLY can only
clasohm
parents:
378
diff
changeset
|
110 |
end; |
1289 | 111 |
|
112 |
||
113 |
(*** System command execution ***) |
|
114 |
||
115 |
(*Execute an Unix command which doesn't take any input from stdin and |
|
116 |
sends its output to stdout.*) |
|
117 |
fun execute command = |
|
118 |
let val (is, os) = ExtendedIO.execute command; |
|
119 |
val result = input (is, 999999); |
|
120 |
in close_out os; |
|
121 |
close_in is; |
|
122 |
result |
|
123 |
end; |
|
2408 | 124 |
|
125 |
||
2724 | 126 |
(*Non-printing and 8-bit chars are forbidden in string constants*) |
2408 | 127 |
val needs_filtered_use = true; |