src/Pure/System/isabelle_process.ML
author wenzelm
Wed, 11 Aug 2010 00:44:48 +0200
changeset 38270 71bb3c273dd1
parent 38265 cc9fde54311f
child 38445 ba9ea6b9b75c
permissions -rw-r--r--
native Isabelle_Process commands, based on efficient byte channel protocol for string lists; misc clarification of proc/pid state, eliminated closing flag; misc tuning and simplification;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30173
eabece26b89b moved isabelle_process.ML, isabelle_process.scala, isar.ML, session.ML to Pure/System/ (together with associated Isar commands);
wenzelm
parents: 29522
diff changeset
     1
(*  Title:      Pure/System/isabelle_process.ML
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     3
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
     4
Isabelle process wrapper.
25631
9036ccd685b4 added output protocol specification;
wenzelm
parents: 25576
diff changeset
     5
9036ccd685b4 added output protocol specification;
wenzelm
parents: 25576
diff changeset
     6
General format of process output:
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
     7
  (1) message = "\002"  header chunk  body chunk
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
     8
  (2) chunk = size (ASCII digits)  \n  content (YXML)
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
     9
*)
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    10
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    11
signature ISABELLE_PROCESS =
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    12
sig
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    13
  val isabelle_processN: string
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    14
  val add_command: string -> (string list -> unit) -> unit
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    15
  val command: string -> string list -> unit
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    16
  val crashes: exn list Unsynchronized.ref
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
    17
  val init: string -> string -> unit
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    18
end;
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    19
31797
203d5e61e3bc renamed IsabelleProcess to Isabelle_Process;
wenzelm
parents: 31384
diff changeset
    20
structure Isabelle_Process: ISABELLE_PROCESS =
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    21
struct
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    22
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    23
(* print modes *)
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    24
25748
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    25
val isabelle_processN = "isabelle_process";
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    26
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    27
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
28036
wenzelm
parents: 27986
diff changeset
    28
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    29
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    30
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    31
(* commands *)
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    32
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    33
local
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    34
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    35
val global_commands = Unsynchronized.ref (Symtab.empty: (string list -> unit) Symtab.table);
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    36
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    37
in
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    38
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    39
fun add_command name cmd = CRITICAL (fn () =>
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    40
  Unsynchronized.change global_commands (fn cmds =>
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    41
   (if not (Symtab.defined cmds name) then ()
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    42
    else warning ("Redefining Isabelle process command " ^ quote name);
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    43
    Symtab.update (name, cmd) cmds)));
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    44
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    45
fun command name args =
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    46
  (case Symtab.lookup (! global_commands) name of
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    47
    NONE => error ("Undefined Isabelle process command " ^ quote name)
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    48
  | SOME cmd => cmd args);
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    49
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    50
end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    51
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
    52
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    53
(* message markup *)
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    54
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    55
local
25810
bac99880fa99 output message properties: id or position;
wenzelm
parents: 25748
diff changeset
    56
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    57
fun chunk s = string_of_int (size s) ^ "\n" ^ s;
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    58
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    59
fun message _ _ _ "" = ()
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    60
  | message out_stream ch props body =
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    61
      let
38265
cc9fde54311f renamed YXML.binary_text to YXML.escape_controls to emphasize what it actually does;
wenzelm
parents: 38259
diff changeset
    62
        val header = YXML.string_of (XML.Elem ((ch, map (pairself YXML.escape_controls) props), []));
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    63
        val msg = Symbol.STX ^ chunk header ^ chunk body;
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    64
      in TextIO.output (out_stream, msg) (*atomic output*) end;
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    65
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    66
in
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    67
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    68
fun standard_message out_stream ch body =
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    69
  message out_stream ch (Position.properties_of (Position.thread_data ())) body;
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    70
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    71
fun init_message out_stream =
34214
99eefb83a35d simplified init message -- removed redundant session property;
wenzelm
parents: 34206
diff changeset
    72
  message out_stream "A" [(Markup.pidN, process_id ())] (Session.welcome ());
25748
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    73
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    74
end;
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    75
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    76
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    77
(* channels *)
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    78
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    79
local
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    80
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    81
fun auto_flush stream =
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    82
  let
28189
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
    83
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    84
    fun loop () =
38256
d2f094d97c91 auto_flush: higher frequency;
wenzelm
parents: 38254
diff changeset
    85
      (OS.Process.sleep (Time.fromMilliseconds 20); try TextIO.flushOut stream; loop ());
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    86
  in loop end;
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    87
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
    88
fun rendezvous f fifo =
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
    89
  let
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
    90
    val path = File.platform_path (Path.explode fifo);
38254
fb1b255d6e36 more robust fifo rendezvous: Cygwin 1.7 does not really block as expected;
wenzelm
parents: 38253
diff changeset
    91
    val result = f fifo;  (*should block until peer is ready*)
fb1b255d6e36 more robust fifo rendezvous: Cygwin 1.7 does not really block as expected;
wenzelm
parents: 38253
diff changeset
    92
    val _ =
fb1b255d6e36 more robust fifo rendezvous: Cygwin 1.7 does not really block as expected;
wenzelm
parents: 38253
diff changeset
    93
      if String.isSuffix "cygwin" ml_platform then ()  (*Cygwin 1.7: no proper blocking on open*)
fb1b255d6e36 more robust fifo rendezvous: Cygwin 1.7 does not really block as expected;
wenzelm
parents: 38253
diff changeset
    94
      else OS.FileSys.remove path;  (*prevent future access*)
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
    95
  in result end;
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
    96
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    97
in
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    98
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
    99
fun setup_channels in_fifo out_fifo =
28189
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
   100
  let
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   101
    val in_stream = rendezvous TextIO.openIn in_fifo;
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   102
    val out_stream = rendezvous TextIO.openOut out_fifo;
37216
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37191
diff changeset
   103
    val _ = Simple_Thread.fork false (auto_flush out_stream);
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37191
diff changeset
   104
    val _ = Simple_Thread.fork false (auto_flush TextIO.stdOut);
3165bc303f66 modernized some structure names, keeping a few legacy aliases;
wenzelm
parents: 37191
diff changeset
   105
    val _ = Simple_Thread.fork false (auto_flush TextIO.stdErr);
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   106
  in
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
   107
    Output.status_fn   := standard_message out_stream "B";
38236
d8c7be27e01d explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents: 38228
diff changeset
   108
    Output.report_fn   := standard_message out_stream "C";
d8c7be27e01d explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents: 38228
diff changeset
   109
    Output.writeln_fn  := standard_message out_stream "D";
d8c7be27e01d explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents: 38228
diff changeset
   110
    Output.tracing_fn  := standard_message out_stream "E";
d8c7be27e01d explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents: 38228
diff changeset
   111
    Output.warning_fn  := standard_message out_stream "F";
d8c7be27e01d explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents: 38228
diff changeset
   112
    Output.error_fn    := standard_message out_stream "G";
d8c7be27e01d explicitly distinguish Output.status (essential feedback) vs. Output.report (useful markup);
wenzelm
parents: 38228
diff changeset
   113
    Output.debug_fn    := standard_message out_stream "H";
37121
8e51fc35d59f eliminated obsolete priority message from Isabelle_Process protocol;
wenzelm
parents: 36950
diff changeset
   114
    Output.priority_fn := ! Output.writeln_fn;
28498
cb1b43edb5ed eliminated prompt messages;
wenzelm
parents: 28491
diff changeset
   115
    Output.prompt_fn   := ignore;
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   116
    (in_stream, out_stream)
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
   117
  end;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   118
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   119
end;
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
   120
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
   121
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   122
(* protocol loop *)
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   123
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   124
val crashes = Unsynchronized.ref ([]: exn list);
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   125
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   126
local
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   127
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   128
fun recover crash =
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   129
  (CRITICAL (fn () => Unsynchronized.change crashes (cons crash));
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   130
    warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   131
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   132
fun read_chunk stream len =
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   133
  let
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   134
    val n =
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   135
      (case Int.fromString len of
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   136
        SOME n => n
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   137
      | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   138
    val chunk = TextIO.inputN (stream, n);
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   139
    val m = size chunk;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   140
  in
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   141
    if m = n then chunk
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   142
    else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   143
  end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   144
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   145
fun read_command stream =
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   146
  (case TextIO.inputLine stream of
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   147
    NONE => raise Runtime.TERMINATE
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   148
  | SOME line => map (read_chunk stream) (space_explode "," line));
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   149
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   150
fun run_command name args =
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   151
  Runtime.debugging (command name) args
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   152
    handle exn =>
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   153
      error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   154
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   155
in
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   156
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   157
fun loop stream =
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   158
  let val continue =
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   159
    (case read_command stream of
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   160
      [] => (Output.error_msg "Isabelle process: no input"; true)
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   161
    | name :: args => (run_command name args; true))
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   162
    handle Runtime.TERMINATE => false
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   163
      | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   164
  in if continue then loop stream else () end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   165
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   166
end;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   167
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   168
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
   169
(* init *)
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
   170
38253
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   171
fun init in_fifo out_fifo =
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   172
  let
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   173
    val _ = Unsynchronized.change print_mode
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   174
      (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   175
    val (in_stream, out_stream) = setup_channels in_fifo out_fifo;
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   176
    val _ = init_message out_stream;
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   177
    val _ = quick_and_dirty := true;  (* FIXME !? *)
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   178
    val _ = Keyword.status ();
3d4e521014f7 Isabelle_Process: separate input fifo for commands (still using the old tty protocol);
wenzelm
parents: 38236
diff changeset
   179
    val _ = Output.status (Markup.markup Markup.ready "");
38270
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   180
    val _ = Context.set_thread_data NONE;
71bb3c273dd1 native Isabelle_Process commands, based on efficient byte channel protocol for string lists;
wenzelm
parents: 38265
diff changeset
   181
    val _ = Simple_Thread.fork false (fn () => (loop in_stream; quit ()));
38259
2b61c5e27399 distinguish proper Isabelle_Process INPUT vs. raw STDIN, tuned corresponding method names;
wenzelm
parents: 38256
diff changeset
   182
  in () end;
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   183
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   184
end;