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