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, err) verbose txt =
64 val in_buffer = ref (explode txt);
65 val out_buffer = ref ([]: string list);
66 fun output () = drop_last_char (implode (rev (! out_buffer)));
71 | c :: cs => (in_buffer := cs; c));
72 fun put s = out_buffer := s :: ! out_buffer;
77 | _ => (PolyML.compiler (get, put) (); exec ()));
79 exec () handle exn => (err (output ()); raise exn);
80 if verbose then print (output ()) else ()
85 (** interrupts **) (*Note: may get into race conditions*)
87 fun mask_interrupt f x = f x;
88 fun unmask_interrupt f x = f x;
89 fun exhibit_interrupt f x = f x;
95 (* system command execution *)
97 (*execute Unix command which doesn't take any input from stdin and
98 sends its output to stdout*)
101 val (is, os) = ExtendedIO.execute command;
102 val _ = close_out os;
103 val result = input (is, 999999);
104 in close_in is; result end;
106 fun system cmd = (print (execute cmd); 0); (* FIXME rc not available *)
111 (*get time of last modification*)
112 fun file_info name = Int.toString (System.filedate name) handle _ => "";
118 val chDir = PolyML.cd;
119 fun remove name = (execute ("rm " ^ name); ());
120 fun getDir () = drop_last_char (execute "pwd");
127 fun getenv var = drop_last_char
128 (execute ("env | grep '^" ^ var ^ "=' | sed -e 's/" ^ var ^ "=//'"));
134 (* non-ASCII input (see also Thy/use.ML) *)
136 val needs_filtered_use = true;