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 |
|
3588
|
6 |
Compatibility file for Poly/ML (versions 2.x and 3.x).
|
2341
|
7 |
*)
|
|
8 |
|
|
9 |
open PolyML ExtendedIO;
|
|
10 |
|
3588
|
11 |
(*needs the Basis Library emulation*)
|
|
12 |
use "basis.ML";
|
|
13 |
|
2341
|
14 |
|
|
15 |
|
3588
|
16 |
(** ML system related **)
|
|
17 |
|
4326
|
18 |
(** Compiler-independent timing functions **)
|
3588
|
19 |
|
4326
|
20 |
(*Note start point for timing*)
|
|
21 |
fun startTiming() = System.processtime ();
|
2341
|
22 |
|
4326
|
23 |
(*Finish timing and return string*)
|
|
24 |
fun endTiming before =
|
|
25 |
"User + GC: " ^
|
|
26 |
makestring (real (System.processtime () - before) / 10.0) ^
|
|
27 |
" secs";
|
2341
|
28 |
|
|
29 |
|
4977
|
30 |
(* prompts *)
|
|
31 |
|
4984
|
32 |
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
|
4977
|
33 |
|
|
34 |
|
3588
|
35 |
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
|
2341
|
36 |
|
|
37 |
fun make_pp _ pprint (str, blk, brk, en) obj =
|
|
38 |
pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
|
|
39 |
fn () => brk (99999, 0), en);
|
|
40 |
|
|
41 |
|
3588
|
42 |
(* ML command execution -- 'eval' *)
|
|
43 |
|
3631
|
44 |
val use_strings =
|
3588
|
45 |
let
|
|
46 |
fun exec line =
|
|
47 |
let
|
|
48 |
val is_first = ref true;
|
|
49 |
|
|
50 |
fun get_line () =
|
|
51 |
if ! is_first then (is_first := false; line)
|
3631
|
52 |
else raise Io "use_strings: buffer exhausted";
|
3588
|
53 |
in
|
|
54 |
PolyML.compiler (get_line, print) ()
|
|
55 |
end;
|
|
56 |
|
|
57 |
fun execs [] = ()
|
|
58 |
| execs (line :: lines) = (exec line; execs lines);
|
|
59 |
in execs end;
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
(** OS related **)
|
2341
|
64 |
|
|
65 |
local
|
|
66 |
|
3588
|
67 |
fun drop_last [] = []
|
|
68 |
| drop_last [x] = []
|
|
69 |
| drop_last (x :: xs) = x :: drop_last xs;
|
2341
|
70 |
|
3588
|
71 |
val drop_last_char = implode o drop_last o explode;
|
2341
|
72 |
|
|
73 |
in
|
|
74 |
|
|
75 |
|
3588
|
76 |
(* system command execution *)
|
2341
|
77 |
|
3588
|
78 |
(*execute Unix command which doesn't take any input from stdin and
|
|
79 |
sends its output to stdout*)
|
|
80 |
fun execute command =
|
|
81 |
let
|
|
82 |
val (is, os) = ExtendedIO.execute command;
|
|
83 |
val result = input (is, 999999);
|
|
84 |
in
|
|
85 |
close_out os;
|
|
86 |
close_in is;
|
|
87 |
result
|
2341
|
88 |
end;
|
|
89 |
|
|
90 |
|
3588
|
91 |
(* file handling *)
|
2341
|
92 |
|
3588
|
93 |
(*get time of last modification; if file doesn't exist return an empty string*)
|
|
94 |
fun file_info "" = "" (* FIXME !? *)
|
|
95 |
| file_info name = Int.toString (System.filedate name) handle _ => "";
|
2341
|
96 |
|
3588
|
97 |
structure OS =
|
|
98 |
struct
|
|
99 |
structure FileSys =
|
|
100 |
struct
|
|
101 |
val chDir = PolyML.cd;
|
|
102 |
fun remove name = (execute ("rm " ^ name); ());
|
|
103 |
fun getDir () = drop_last_char (execute "pwd");
|
|
104 |
end;
|
|
105 |
end;
|
|
106 |
|
|
107 |
|
|
108 |
(* getenv *)
|
|
109 |
|
|
110 |
fun getenv var = drop_last_char
|
|
111 |
(execute ("env | grep '^" ^ var ^ "=' | sed -e 's/" ^ var ^ "=//'"));
|
|
112 |
|
2341
|
113 |
|
|
114 |
end;
|
|
115 |
|
|
116 |
|
4428
|
117 |
(* non-ASCII input (see also Thy/use.ML) *)
|
2408
|
118 |
|
|
119 |
val needs_filtered_use =
|
|
120 |
(case explode ml_system of
|
|
121 |
"p" :: "o" :: "l" :: "y" :: "m" :: "l" :: "-" :: "3" :: _ => true
|
|
122 |
| _ => false);
|