src/Pure/ML-Systems/polyml.ML
author wenzelm
Sat Jun 11 23:17:28 2005 +0200 (2005-06-11)
changeset 16374 f4b7cf8975af
parent 16266 7a6616be8712
child 16453 af3afdbd09ea
permissions -rw-r--r--
some cygwin support;
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@14870
     6
Compatibility file for Poly/ML (version 4.0, 4.1, 4.1.x).
wenzelm@2341
     7
*)
wenzelm@2341
     8
wenzelm@16374
     9
(** ML system and platform related **)
wenzelm@16374
    10
wenzelm@16374
    11
(* cygwin *)
wenzelm@16374
    12
wenzelm@16374
    13
val cygwin_platform =
wenzelm@16374
    14
  let val n = size ml_platform
wenzelm@16374
    15
  in n >= 6 andalso String.substring (ml_platform, n - 6, 6) = "cygwin" end;
wenzelm@16374
    16
wenzelm@16374
    17
fun if_cygwin f x = if cygwin_platform then f x else ();
wenzelm@16374
    18
fun unless_cygwin f x = if not cygwin_platform then f x else ();
wenzelm@16374
    19
wenzelm@2341
    20
wenzelm@11078
    21
(* old Poly/ML emulation *)
wenzelm@2341
    22
wenzelm@11078
    23
local
wenzelm@11078
    24
  val orig_exit = exit;
wenzelm@11078
    25
in
wenzelm@11078
    26
  open PolyML;
wenzelm@11078
    27
  val exit = orig_exit;
wenzelm@11078
    28
  fun quit () = exit 0;
wenzelm@7148
    29
end;
paulson@6042
    30
wenzelm@2341
    31
wenzelm@11078
    32
(* restore old-style character / string functions *)
wenzelm@3588
    33
wenzelm@11078
    34
val ord = SML90.ord;
wenzelm@11078
    35
val chr = SML90.chr;
wenzelm@11078
    36
val explode = SML90.explode;
wenzelm@11078
    37
val implode = SML90.implode;
wenzelm@11078
    38
wenzelm@14870
    39
skalberg@14519
    40
(* compiler-independent timing functions *)
wenzelm@2341
    41
skalberg@14519
    42
use "ML-Systems/cpu-timer-basis.ML";
wenzelm@2341
    43
wenzelm@14870
    44
webertj@14850
    45
(* bounded time execution *)
webertj@14850
    46
wenzelm@16374
    47
unless_cygwin
wenzelm@16374
    48
  use "ML-Systems/polyml-time-limit.ML";
webertj@14850
    49
wenzelm@14870
    50
wenzelm@4977
    51
(* prompts *)
wenzelm@4977
    52
wenzelm@4984
    53
fun ml_prompts p1 p2 = (PolyML.Compiler.prompt1 := p1; PolyML.Compiler.prompt2 := p2);
wenzelm@4977
    54
wenzelm@4977
    55
wenzelm@3588
    56
(* toplevel pretty printing (see also Pure/install_pp.ML) *)
wenzelm@2341
    57
wenzelm@11078
    58
fun make_pp _ pprint (str, blk, brk, en) _ _ obj =
wenzelm@2341
    59
  pprint obj (str, fn ind => blk (ind, false), fn wd => brk (wd, 0),
wenzelm@2341
    60
    fn () => brk (99999, 0), en);
wenzelm@2341
    61
wenzelm@2341
    62
wenzelm@3588
    63
(* ML command execution -- 'eval' *)
wenzelm@3588
    64
wenzelm@7890
    65
local
wenzelm@7890
    66
wenzelm@7890
    67
fun drop_last [] = []
wenzelm@7890
    68
  | drop_last [x] = []
wenzelm@7890
    69
  | drop_last (x :: xs) = x :: drop_last xs;
wenzelm@7890
    70
wenzelm@7890
    71
in
wenzelm@7890
    72
wenzelm@10914
    73
fun use_text (print, err) verbose txt =
wenzelm@3588
    74
  let
wenzelm@5090
    75
    val in_buffer = ref (explode txt);
wenzelm@5090
    76
    val out_buffer = ref ([]: string list);
wenzelm@15832
    77
    fun output () = implode (drop_last (rev (! out_buffer)));
wenzelm@5090
    78
wenzelm@5038
    79
    fun get () =
wenzelm@5090
    80
      (case ! in_buffer of
wenzelm@5038
    81
        [] => ""
wenzelm@5090
    82
      | c :: cs => (in_buffer := cs; c));
wenzelm@5090
    83
    fun put s = out_buffer := s :: ! out_buffer;
wenzelm@5090
    84
wenzelm@5038
    85
    fun exec () =
wenzelm@5090
    86
      (case ! in_buffer of
wenzelm@5038
    87
        [] => ()
wenzelm@5090
    88
      | _ => (PolyML.compiler (get, put) (); exec ()));
wenzelm@5090
    89
  in
wenzelm@10914
    90
    exec () handle exn => (err (output ()); raise exn);
wenzelm@10914
    91
    if verbose then print (output ()) else ()
wenzelm@5090
    92
  end;
wenzelm@3588
    93
wenzelm@11078
    94
end;
wenzelm@11078
    95
wenzelm@11078
    96
wenzelm@15832
    97
(*eval command line arguments*)
wenzelm@15832
    98
local
wenzelm@15832
    99
  fun println s =
wenzelm@15832
   100
    (TextIO.output (TextIO.stdOut, s ^ "\n"); TextIO.flushOut TextIO.stdOut);
wenzelm@15851
   101
  val eval = use_text (println, println) true;
wenzelm@15832
   102
in
wenzelm@15832
   103
  val _ = PolyML.onEntry (fn () => app eval (CommandLine.arguments ()));
wenzelm@15832
   104
end;
wenzelm@15832
   105
wenzelm@15832
   106
wenzelm@11078
   107
wenzelm@11078
   108
(** interrupts **)
wenzelm@11078
   109
wenzelm@11078
   110
exception Interrupt = SML90.Interrupt;
wenzelm@11078
   111
wenzelm@11078
   112
local
wenzelm@11078
   113
wenzelm@12989
   114
fun capture f x = ((f x): unit; NONE) handle exn => SOME exn;
wenzelm@11078
   115
wenzelm@12989
   116
fun release NONE = ()
wenzelm@12989
   117
  | release (SOME exn) = raise exn;
wenzelm@3588
   118
wenzelm@11078
   119
val sig_int = 2;
wenzelm@11078
   120
wenzelm@11078
   121
fun change_signal new_handler f x =
wenzelm@11078
   122
  let
wenzelm@11078
   123
    (*RACE wrt. other signals*)
wenzelm@11078
   124
    val old_handler = Signal.signal (sig_int, new_handler);
wenzelm@11078
   125
    val result = capture f x;
wenzelm@11078
   126
    val _ = Signal.signal (sig_int, old_handler);
wenzelm@11078
   127
  in release result end;
wenzelm@5813
   128
wenzelm@11078
   129
val default_handler = Signal.SIG_HANDLE (fn _ => Process.interruptConsoleProcesses ());
wenzelm@11078
   130
wenzelm@11078
   131
in
wenzelm@11078
   132
wenzelm@11078
   133
val _ = Signal.signal (sig_int, default_handler);
wenzelm@11078
   134
wenzelm@12989
   135
fun ignore_interrupt f = change_signal Signal.SIG_IGN f;
wenzelm@12989
   136
fun raise_interrupt f = change_signal default_handler f;
wenzelm@11078
   137
wenzelm@11078
   138
end;
wenzelm@5813
   139
wenzelm@5813
   140
wenzelm@5813
   141
wenzelm@3588
   142
(** OS related **)
wenzelm@2341
   143
wenzelm@3588
   144
(* system command execution *)
wenzelm@2341
   145
wenzelm@3588
   146
(*execute Unix command which doesn't take any input from stdin and
wenzelm@11078
   147
  sends its output to stdout; could be done more easily by Unix.execute,
wenzelm@11078
   148
  but that function doesn't use the PATH*)
wenzelm@3588
   149
fun execute command =
wenzelm@3588
   150
  let
wenzelm@11078
   151
    val tmp_name = OS.FileSys.tmpName ();
wenzelm@11078
   152
    val is = (OS.Process.system (command ^ " > " ^ tmp_name); TextIO.openIn tmp_name);
wenzelm@11078
   153
    val result = TextIO.inputAll is;
wenzelm@11078
   154
  in
wenzelm@11078
   155
    TextIO.closeIn is;
wenzelm@11078
   156
    OS.FileSys.remove tmp_name;
wenzelm@11078
   157
    result
wenzelm@11078
   158
  end;
wenzelm@7855
   159
wenzelm@11078
   160
(*plain version; with return code*)
wenzelm@11078
   161
fun system cmd =
wenzelm@11078
   162
  if OS.Process.isSuccess (OS.Process.system cmd) then 0 else 1;
wenzelm@2341
   163
wenzelm@2341
   164
wenzelm@3588
   165
(* getenv *)
wenzelm@3588
   166
wenzelm@11078
   167
fun getenv var =
wenzelm@11078
   168
  (case OS.Process.getEnv var of
wenzelm@11078
   169
    NONE => ""
wenzelm@11078
   170
  | SOME txt => txt);
paulson@15028
   171
paulson@15028
   172
wenzelm@15851
   173
(* profiling: version that works even with
paulson@15028
   174
ML{*profiling 1*}
paulson@15028
   175
apply \\<dots>
paulson@15028
   176
ML{*profiling 0*}
paulson@15028
   177
*)
paulson@15028
   178
paulson@15028
   179
val profiling: int->unit =
paulson@15028
   180
     RunCall.run_call1 RuntimeCalls.POLY_SYS_profiler;
paulson@15699
   181
wenzelm@16374
   182
unless_cygwin
wenzelm@16374
   183
  use "ML-Systems/polyml-posix.ML";