1 (* Title: Pure/ML-Systems/polyml.ML
3 Author: Lawrence C Paulson, Cambridge University Computer Laboratory
4 Copyright 1991 University of Cambridge
6 Compatibility file for Poly/ML (versions 2.x and 3.x).
9 open PolyML ExtendedIO;
11 (*needs the Basis Library emulation*)
17 fun substring (s,i,j) = StringBuiltIns.substring (s, i+1, j)
18 handle Substring => raise Subscript
19 fun isPrefix s1 s2 = (s1 = substring(s2,0, size s1))
20 handle Subscript => false
24 (** ML system related **)
26 (** Compiler-independent timing functions **)
28 (*Note start point for timing*)
29 fun startTiming() = System.processtime ();
31 (*Finish timing and return string*)
32 fun endTiming before =
34 makestring (real (System.processtime () - before) / 10.0) ^
40 fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
43 (* toplevel pretty printing (see also Pure/install_pp.ML) *)
45 fun make_pp _ pprint (str, blk, brk, en) obj =
46 pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
47 fn () => brk (99999, 0), en);
50 (* ML command execution -- 'eval' *)
56 | drop_last (x :: xs) = x :: drop_last xs;
58 val drop_last_char = implode o drop_last o explode;
62 fun use_text print verbose txt =
64 val in_buffer = ref (explode txt);
65 val out_buffer = ref ([]: string list);
70 | c :: cs => (in_buffer := cs; c));
71 fun put s = out_buffer := s :: ! out_buffer;
76 | _ => (PolyML.compiler (get, put) (); exec ()));
78 fun show_output () = print (drop_last_char (implode (rev (! out_buffer))));
80 exec () handle exn => (show_output (); raise exn);
81 if verbose then show_output () else ()
86 (** interrupts **) (*Note: may get into race conditions*)
88 fun mask_interrupt f x = f x;
89 fun unmask_interrupt f x = f x;
90 fun exhibit_interrupt f x = f x;
96 (* system command execution *)
98 (*execute Unix command which doesn't take any input from stdin and
99 sends its output to stdout*)
100 fun execute command =
102 val (is, os) = ExtendedIO.execute command;
103 val _ = close_out os;
104 val result = input (is, 999999);
105 in close_in is; result end;
107 fun system cmd = (print (execute cmd); 0); (* FIXME rc not available *)
112 (*get time of last modification*)
113 fun file_info name = Int.toString (System.filedate name) handle _ => "";
119 val chDir = PolyML.cd;
120 fun remove name = (execute ("rm " ^ name); ());
121 fun getDir () = drop_last_char (execute "pwd");
128 fun getenv var = drop_last_char
129 (execute ("env | grep '^" ^ var ^ "=' | sed -e 's/" ^ var ^ "=//'"));
135 (* non-ASCII input (see also Thy/use.ML) *)
137 val needs_filtered_use =
138 (case explode ml_system of
139 "p" :: "o" :: "l" :: "y" :: "m" :: "l" :: "-" :: "3" :: _ => true