src/Pure/System/isabelle_process.ML
author wenzelm
Tue Aug 10 12:29:11 2010 +0200 (2010-08-10 ago)
changeset 38259 2b61c5e27399
parent 38256 d2f094d97c91
child 38265 cc9fde54311f
permissions -rw-r--r--
distinguish proper Isabelle_Process INPUT vs. raw STDIN, tuned corresponding method names;
asynchronous Isabelle_Process.init -- raw ML toplevel stays active;
simplified Isabelle_Process using actors;
misc tuning;
wenzelm@30173
     1
(*  Title:      Pure/System/isabelle_process.ML
wenzelm@25528
     2
    Author:     Makarius
wenzelm@25528
     3
wenzelm@34096
     4
Isabelle process wrapper.
wenzelm@25631
     5
wenzelm@25631
     6
General format of process output:
wenzelm@34096
     7
  (1) message = "\002"  header chunk  body chunk
wenzelm@34096
     8
  (2) chunk = size (ASCII digits)  \n  content (YXML)
wenzelm@25528
     9
*)
wenzelm@25528
    10
wenzelm@25528
    11
signature ISABELLE_PROCESS =
wenzelm@25528
    12
sig
wenzelm@26574
    13
  val isabelle_processN: string
wenzelm@38253
    14
  val init: string -> string -> unit
wenzelm@25528
    15
end;
wenzelm@25528
    16
wenzelm@31797
    17
structure Isabelle_Process: ISABELLE_PROCESS =
wenzelm@25528
    18
struct
wenzelm@25528
    19
wenzelm@26550
    20
(* print modes *)
wenzelm@25554
    21
wenzelm@25748
    22
val isabelle_processN = "isabelle_process";
wenzelm@25748
    23
wenzelm@26550
    24
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
wenzelm@28036
    25
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
wenzelm@25841
    26
wenzelm@25841
    27
wenzelm@25841
    28
(* message markup *)
wenzelm@25841
    29
wenzelm@25841
    30
local
wenzelm@25810
    31
wenzelm@34096
    32
fun chunk s = string_of_int (size s) ^ "\n" ^ s;
wenzelm@26574
    33
wenzelm@34096
    34
fun message _ _ _ "" = ()
wenzelm@34096
    35
  | message out_stream ch props body =
wenzelm@34096
    36
      let
wenzelm@38228
    37
        val header = YXML.string_of (XML.Elem ((ch, map (pairself YXML.binary_text) props), []));
wenzelm@34096
    38
        val msg = Symbol.STX ^ chunk header ^ chunk body;
wenzelm@34096
    39
      in TextIO.output (out_stream, msg) (*atomic output*) end;
wenzelm@25554
    40
wenzelm@25554
    41
in
wenzelm@25554
    42
wenzelm@34096
    43
fun standard_message out_stream ch body =
wenzelm@34096
    44
  message out_stream ch (Position.properties_of (Position.thread_data ())) body;
wenzelm@25554
    45
wenzelm@28044
    46
fun init_message out_stream =
wenzelm@34214
    47
  message out_stream "A" [(Markup.pidN, process_id ())] (Session.welcome ());
wenzelm@25748
    48
wenzelm@25554
    49
end;
wenzelm@25554
    50
wenzelm@25554
    51
wenzelm@25841
    52
(* channels *)
wenzelm@25841
    53
wenzelm@28188
    54
local
wenzelm@28188
    55
wenzelm@28188
    56
fun auto_flush stream =
wenzelm@28188
    57
  let
wenzelm@28189
    58
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
wenzelm@28188
    59
    fun loop () =
wenzelm@38256
    60
      (OS.Process.sleep (Time.fromMilliseconds 20); try TextIO.flushOut stream; loop ());
wenzelm@28188
    61
  in loop end;
wenzelm@28188
    62
wenzelm@38253
    63
fun rendezvous f fifo =
wenzelm@38253
    64
  let
wenzelm@38253
    65
    val path = File.platform_path (Path.explode fifo);
wenzelm@38254
    66
    val result = f fifo;  (*should block until peer is ready*)
wenzelm@38254
    67
    val _ =
wenzelm@38254
    68
      if String.isSuffix "cygwin" ml_platform then ()  (*Cygwin 1.7: no proper blocking on open*)
wenzelm@38254
    69
      else OS.FileSys.remove path;  (*prevent future access*)
wenzelm@38253
    70
  in result end;
wenzelm@38253
    71
wenzelm@28188
    72
in
wenzelm@28188
    73
wenzelm@38253
    74
fun setup_channels in_fifo out_fifo =
wenzelm@28189
    75
  let
wenzelm@38253
    76
    val in_stream = rendezvous TextIO.openIn in_fifo;
wenzelm@38253
    77
    val out_stream = rendezvous TextIO.openOut out_fifo;
wenzelm@37216
    78
    val _ = Simple_Thread.fork false (auto_flush out_stream);
wenzelm@37216
    79
    val _ = Simple_Thread.fork false (auto_flush TextIO.stdOut);
wenzelm@37216
    80
    val _ = Simple_Thread.fork false (auto_flush TextIO.stdErr);
wenzelm@28044
    81
  in
wenzelm@34096
    82
    Output.status_fn   := standard_message out_stream "B";
wenzelm@38236
    83
    Output.report_fn   := standard_message out_stream "C";
wenzelm@38236
    84
    Output.writeln_fn  := standard_message out_stream "D";
wenzelm@38236
    85
    Output.tracing_fn  := standard_message out_stream "E";
wenzelm@38236
    86
    Output.warning_fn  := standard_message out_stream "F";
wenzelm@38236
    87
    Output.error_fn    := standard_message out_stream "G";
wenzelm@38236
    88
    Output.debug_fn    := standard_message out_stream "H";
wenzelm@37121
    89
    Output.priority_fn := ! Output.writeln_fn;
wenzelm@28498
    90
    Output.prompt_fn   := ignore;
wenzelm@38253
    91
    (in_stream, out_stream)
wenzelm@28044
    92
  end;
wenzelm@25841
    93
wenzelm@28188
    94
end;
wenzelm@28188
    95
wenzelm@25841
    96
wenzelm@25554
    97
(* init *)
wenzelm@25554
    98
wenzelm@38253
    99
fun init in_fifo out_fifo =
wenzelm@38253
   100
  let
wenzelm@38253
   101
    val _ = Unsynchronized.change print_mode
wenzelm@38253
   102
      (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
wenzelm@38253
   103
    val (in_stream, out_stream) = setup_channels in_fifo out_fifo;
wenzelm@38253
   104
    val _ = init_message out_stream;
wenzelm@38253
   105
    val _ = quick_and_dirty := true;  (* FIXME !? *)
wenzelm@38253
   106
    val _ = Keyword.status ();
wenzelm@38253
   107
    val _ = Output.status (Markup.markup Markup.ready "");
wenzelm@38259
   108
    val _ =
wenzelm@38259
   109
      Simple_Thread.fork false (fn () =>
wenzelm@38259
   110
        (Isar.toplevel_loop in_stream {init = true, welcome = false, sync = true, secure = true};
wenzelm@38259
   111
          quit ()));
wenzelm@38259
   112
  in () end;
wenzelm@25528
   113
wenzelm@25528
   114
end;