src/Pure/Tools/isabelle_process.ML
author wenzelm
Sat, 04 Oct 2008 14:43:40 +0200
changeset 28498 cb1b43edb5ed
parent 28491 c5420429a5aa
child 29327 e41274f6cc9d
permissions -rw-r--r--
eliminated prompt messages;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/Tools/isabelle_process.ML
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     3
    Author:     Makarius
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     4
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     5
Isabelle process wrapper -- interaction via external program.
25631
9036ccd685b4 added output protocol specification;
wenzelm
parents: 25576
diff changeset
     6
9036ccd685b4 added output protocol specification;
wenzelm
parents: 25576
diff changeset
     7
General format of process output:
9036ccd685b4 added output protocol specification;
wenzelm
parents: 25576
diff changeset
     8
9036ccd685b4 added output protocol specification;
wenzelm
parents: 25576
diff changeset
     9
  (a) unmarked stdout/stderr, no line structure (output should be
25842
fdabeed7ccd9 tuned comments;
wenzelm
parents: 25841
diff changeset
    10
  processed immediately as it arrives);
25631
9036ccd685b4 added output protocol specification;
wenzelm
parents: 25576
diff changeset
    11
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    12
  (b) properly marked-up messages, e.g. for writeln channel
25810
bac99880fa99 output message properties: id or position;
wenzelm
parents: 25748
diff changeset
    13
bac99880fa99 output message properties: id or position;
wenzelm
parents: 25748
diff changeset
    14
  "\002A" ^ props ^ "\002,\n" ^ text ^ "\002.\n"
bac99880fa99 output message properties: id or position;
wenzelm
parents: 25748
diff changeset
    15
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    16
  where the props consist of name=value lines terminated by "\002,\n"
25810
bac99880fa99 output message properties: id or position;
wenzelm
parents: 25748
diff changeset
    17
  each, and the remaining text is any number of lines (output is
25842
fdabeed7ccd9 tuned comments;
wenzelm
parents: 25841
diff changeset
    18
  supposed to be processed in one piece);
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    19
25842
fdabeed7ccd9 tuned comments;
wenzelm
parents: 25841
diff changeset
    20
  (c) special init message holds "pid" and "session" property.
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    21
*)
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    22
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    23
signature ISABELLE_PROCESS =
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    24
sig
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    25
  val isabelle_processN: string
27961
2cd133df7587 removed full_markup mode (default);
wenzelm
parents: 27844
diff changeset
    26
  val xmlN: string
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    27
  val init: string -> unit
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    28
end;
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    29
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    30
structure IsabelleProcess: ISABELLE_PROCESS =
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    31
struct
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    32
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    33
(* print modes *)
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    34
25748
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    35
val isabelle_processN = "isabelle_process";
27961
2cd133df7587 removed full_markup mode (default);
wenzelm
parents: 27844
diff changeset
    36
val xmlN = "XML";
25748
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    37
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    38
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
28036
wenzelm
parents: 27986
diff changeset
    39
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    40
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    41
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    42
(* message markup *)
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    43
26527
c392354a1b79 Symbol.STX, Symbol.DEL;
wenzelm
parents: 26463
diff changeset
    44
fun special ch = Symbol.STX ^ ch;
25810
bac99880fa99 output message properties: id or position;
wenzelm
parents: 25748
diff changeset
    45
val special_sep = special ",";
25576
ee11881606b7 special_end: replaced Z by dot;
wenzelm
parents: 25565
diff changeset
    46
val special_end = special ".";
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    47
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    48
local
25810
bac99880fa99 output message properties: id or position;
wenzelm
parents: 25748
diff changeset
    49
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    50
fun clean_string bad str =
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    51
  if exists_string (member (op =) bad) str then
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    52
    translate_string (fn c => if member (op =) bad c then Symbol.DEL else c) str
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    53
  else str;
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    54
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    55
fun message_props props =
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    56
  let val clean = clean_string [Symbol.STX, "\n", "\r"]
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    57
  in implode (map (fn (x, y) => clean x ^ "=" ^ clean y ^ special_sep ^ "\n") props) end;
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    58
27961
2cd133df7587 removed full_markup mode (default);
wenzelm
parents: 27844
diff changeset
    59
fun message_text class ts =
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    60
  let
27972
a87be8fcb25c symbolic message markup;
wenzelm
parents: 27961
diff changeset
    61
    val doc = XML.Elem (Markup.messageN, [(Markup.classN, class)], ts);
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    62
    val msg =
27961
2cd133df7587 removed full_markup mode (default);
wenzelm
parents: 27844
diff changeset
    63
      if print_mode_active xmlN then XML.header ^ XML.string_of doc
2cd133df7587 removed full_markup mode (default);
wenzelm
parents: 27844
diff changeset
    64
      else YXML.string_of doc;
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    65
  in clean_string [Symbol.STX] msg end;
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    66
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    67
fun message_pos ts = get_first get_pos ts
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    68
and get_pos (elem as XML.Elem (name, atts, ts)) =
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    69
      if name = Markup.positionN then SOME (Position.of_properties atts)
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    70
      else message_pos ts
27434
wenzelm
parents: 26706
diff changeset
    71
  | get_pos _ = NONE;
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    72
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    73
fun output out_stream s = NAMED_CRITICAL "IO" (fn () =>
28182
bfd7a8700676 out_stream: block-buffered, with separate autoflush thread (every 50ms);
wenzelm
parents: 28137
diff changeset
    74
  (TextIO.output (out_stream, s); TextIO.output (out_stream, "\n")));
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    75
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    76
in
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    77
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    78
fun message _ _ _ "" = ()
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    79
  | message out_stream ch class body =
27844
86f0f91471d0 message: ignored if body empty;
wenzelm
parents: 27604
diff changeset
    80
      let
86f0f91471d0 message: ignored if body empty;
wenzelm
parents: 27604
diff changeset
    81
        val (txt, pos) =
86f0f91471d0 message: ignored if body empty;
wenzelm
parents: 27604
diff changeset
    82
          let val ts = YXML.parse_body body
27961
2cd133df7587 removed full_markup mode (default);
wenzelm
parents: 27844
diff changeset
    83
          in (message_text class ts, the_default Position.none (message_pos ts)) end;
27844
86f0f91471d0 message: ignored if body empty;
wenzelm
parents: 27604
diff changeset
    84
        val props =
86f0f91471d0 message: ignored if body empty;
wenzelm
parents: 27604
diff changeset
    85
          Position.properties_of (Position.thread_data ())
86f0f91471d0 message: ignored if body empty;
wenzelm
parents: 27604
diff changeset
    86
          |> Position.default_properties pos;
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    87
      in output out_stream (special ch ^ message_props props ^ txt ^ special_end) end;
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    88
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    89
fun init_message out_stream =
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    90
  let
28491
c5420429a5aa plain process_id function;
wenzelm
parents: 28343
diff changeset
    91
    val pid = (Markup.pidN, process_id ());
27972
a87be8fcb25c symbolic message markup;
wenzelm
parents: 27961
diff changeset
    92
    val session = (Markup.sessionN, List.last (Session.id ()) handle List.Empty => "unknown");
27986
26e1a7a6695d init_message: class markup in message body, not header;
wenzelm
parents: 27972
diff changeset
    93
    val text = message_text Markup.initN [XML.Text (Session.welcome ())];
28343
7b605b8b7196 protocol change: remapped message codes to make room for nested system messages (e.g. for protocol proxy);
wenzelm
parents: 28342
diff changeset
    94
  in output out_stream (special "A" ^ message_props [pid, session] ^ text ^ special_end) end;
25748
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    95
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    96
end;
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    97
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    98
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    99
(* channels *)
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   100
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   101
local
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   102
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   103
fun auto_flush stream =
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   104
  let
28189
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   105
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   106
    fun loop () =
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   107
      (OS.Process.sleep (Time.fromMilliseconds 50); try TextIO.flushOut stream; loop ());
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   108
  in loop end;
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   109
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   110
in
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   111
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   112
fun setup_channels out =
28189
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   113
  let
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   114
    val out_stream =
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   115
      if out = "-" then TextIO.stdOut
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   116
      else
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   117
        let
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   118
          val path = File.platform_path (Path.explode out);
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   119
          val out_stream = TextIO.openOut path;  (*fifo blocks until reader is ready*)
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   120
          val _ = OS.FileSys.remove path;  (*prevent alien access, indicate writer is ready*)
28242
f978c8e75118 SimpleThread.fork;
wenzelm
parents: 28189
diff changeset
   121
          val _ = SimpleThread.fork false (auto_flush TextIO.stdOut);
28189
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   122
        in out_stream end;
28242
f978c8e75118 SimpleThread.fork;
wenzelm
parents: 28189
diff changeset
   123
    val _ = SimpleThread.fork false (auto_flush out_stream);
f978c8e75118 SimpleThread.fork;
wenzelm
parents: 28189
diff changeset
   124
    val _ = SimpleThread.fork false (auto_flush TextIO.stdErr);
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   125
  in
28343
7b605b8b7196 protocol change: remapped message codes to make room for nested system messages (e.g. for protocol proxy);
wenzelm
parents: 28342
diff changeset
   126
    Output.status_fn   := message out_stream "B" Markup.statusN;
7b605b8b7196 protocol change: remapped message codes to make room for nested system messages (e.g. for protocol proxy);
wenzelm
parents: 28342
diff changeset
   127
    Output.writeln_fn  := message out_stream "C" Markup.writelnN;
7b605b8b7196 protocol change: remapped message codes to make room for nested system messages (e.g. for protocol proxy);
wenzelm
parents: 28342
diff changeset
   128
    Output.priority_fn := message out_stream "D" Markup.priorityN;
7b605b8b7196 protocol change: remapped message codes to make room for nested system messages (e.g. for protocol proxy);
wenzelm
parents: 28342
diff changeset
   129
    Output.tracing_fn  := message out_stream "E" Markup.tracingN;
7b605b8b7196 protocol change: remapped message codes to make room for nested system messages (e.g. for protocol proxy);
wenzelm
parents: 28342
diff changeset
   130
    Output.warning_fn  := message out_stream "F" Markup.warningN;
7b605b8b7196 protocol change: remapped message codes to make room for nested system messages (e.g. for protocol proxy);
wenzelm
parents: 28342
diff changeset
   131
    Output.error_fn    := message out_stream "G" Markup.errorN;
7b605b8b7196 protocol change: remapped message codes to make room for nested system messages (e.g. for protocol proxy);
wenzelm
parents: 28342
diff changeset
   132
    Output.debug_fn    := message out_stream "H" Markup.debugN;
28498
cb1b43edb5ed eliminated prompt messages;
wenzelm
parents: 28491
diff changeset
   133
    Output.prompt_fn   := ignore;
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   134
    out_stream
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   135
  end;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   136
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   137
end;
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   138
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   139
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
   140
(* init *)
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
   141
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   142
fun init out =
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   143
 (change print_mode (update (op =) isabelle_processN);
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   144
  setup_channels out |> init_message;
28342
d0db291f7194 init: OuterKeyword.report;
wenzelm
parents: 28242
diff changeset
   145
  OuterKeyword.report ();
26643
99f5407c05ef Isar.toplevel_loop: separate init/welcome flag;
wenzelm
parents: 26607
diff changeset
   146
  Isar.toplevel_loop {init = true, welcome = false, sync = true, secure = true});
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   147
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   148
end;