src/Pure/ML-Systems/polyml.ML
author wenzelm
Mon Nov 09 11:09:33 1998 +0100 (1998-11-09)
changeset 5813 4eea84a9427d
parent 5090 75ac9b451ae0
child 6042 0ccde2f25dd6
permissions -rw-r--r--
fake interrupt handler;
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*)
wenzelm@5813
    24
fun endTiming before =
wenzelm@5813
    25
  "User + GC: " ^
wenzelm@5813
    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@4984
    32
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := 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@5090
    44
fun use_text verbose txt =
wenzelm@3588
    45
  let
wenzelm@5090
    46
    val in_buffer = ref (explode txt);
wenzelm@5090
    47
    val out_buffer = ref ([]: string list);
wenzelm@5090
    48
wenzelm@5038
    49
    fun get () =
wenzelm@5090
    50
      (case ! in_buffer of
wenzelm@5038
    51
        [] => ""
wenzelm@5090
    52
      | c :: cs => (in_buffer := cs; c));
wenzelm@5090
    53
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@5090
    54
wenzelm@5038
    55
    fun exec () =
wenzelm@5090
    56
      (case ! in_buffer of
wenzelm@5038
    57
        [] => ()
wenzelm@5090
    58
      | _ => (PolyML.compiler (get, put) (); exec ()));
wenzelm@5090
    59
wenzelm@5090
    60
    fun show_output () = print (implode (rev (! out_buffer)));
wenzelm@5090
    61
  in
wenzelm@5090
    62
    exec () handle exn => (show_output (); raise exn);
wenzelm@5090
    63
    if verbose then show_output () else ()
wenzelm@5090
    64
  end;
wenzelm@3588
    65
wenzelm@3588
    66
wenzelm@3588
    67
wenzelm@5813
    68
(** interrupts **)      (*Note: may get into race conditions*)
wenzelm@5813
    69
wenzelm@5813
    70
fun mask_interrupt f x = f x;
wenzelm@5813
    71
fun unmask_interrupt f x = f x;
wenzelm@5813
    72
fun exhibit_interrupt f x = f x;
wenzelm@5813
    73
wenzelm@5813
    74
wenzelm@5813
    75
wenzelm@3588
    76
(** OS related **)
wenzelm@2341
    77
wenzelm@2341
    78
local
wenzelm@2341
    79
wenzelm@3588
    80
fun drop_last [] = []
wenzelm@3588
    81
  | drop_last [x] = []
wenzelm@3588
    82
  | drop_last (x :: xs) = x :: drop_last xs;
wenzelm@2341
    83
wenzelm@3588
    84
val drop_last_char = implode o drop_last o explode;
wenzelm@2341
    85
wenzelm@2341
    86
in
wenzelm@2341
    87
wenzelm@2341
    88
wenzelm@3588
    89
(* system command execution *)
wenzelm@2341
    90
wenzelm@3588
    91
(*execute Unix command which doesn't take any input from stdin and
wenzelm@3588
    92
  sends its output to stdout*)
wenzelm@3588
    93
fun execute command =
wenzelm@3588
    94
  let
wenzelm@3588
    95
    val (is, os) =  ExtendedIO.execute command;
wenzelm@3588
    96
    val result = input (is, 999999);
wenzelm@3588
    97
  in
wenzelm@3588
    98
    close_out os;
wenzelm@3588
    99
    close_in is;
wenzelm@3588
   100
    result
wenzelm@2341
   101
  end;
wenzelm@2341
   102
wenzelm@2341
   103
wenzelm@3588
   104
(* file handling *)
wenzelm@2341
   105
wenzelm@3588
   106
(*get time of last modification; if file doesn't exist return an empty string*)
wenzelm@5813
   107
fun file_info "" = ""           (* FIXME !? *)
wenzelm@3588
   108
  | file_info name = Int.toString (System.filedate name) handle _ => "";
wenzelm@2341
   109
wenzelm@3588
   110
structure OS =
wenzelm@3588
   111
struct
wenzelm@3588
   112
  structure FileSys =
wenzelm@3588
   113
  struct
wenzelm@3588
   114
    val chDir = PolyML.cd;
wenzelm@3588
   115
    fun remove name = (execute ("rm " ^ name); ());
wenzelm@3588
   116
    fun getDir () = drop_last_char (execute "pwd");
wenzelm@3588
   117
  end;
wenzelm@3588
   118
end;
wenzelm@3588
   119
wenzelm@3588
   120
wenzelm@3588
   121
(* getenv *)
wenzelm@3588
   122
wenzelm@3588
   123
fun getenv var = drop_last_char
wenzelm@3588
   124
  (execute ("env | grep '^" ^ var ^ "=' | sed -e 's/" ^ var ^ "=//'"));
wenzelm@3588
   125
wenzelm@2341
   126
wenzelm@2341
   127
end;
wenzelm@2341
   128
wenzelm@2341
   129
wenzelm@4428
   130
(* non-ASCII input (see also Thy/use.ML) *)
wenzelm@2408
   131
wenzelm@2408
   132
val needs_filtered_use =
wenzelm@2408
   133
  (case explode ml_system of
wenzelm@2408
   134
    "p" :: "o" :: "l" :: "y" :: "m" :: "l" :: "-" :: "3" :: _ => true
wenzelm@2408
   135
  | _ => false);