src/Pure/System/isabelle_process.ML
author wenzelm
Tue, 29 Dec 2009 20:30:40 +0100
changeset 34206 c29264a16ad8
parent 34096 e438a5875c16
child 34214 99eefb83a35d
permissions -rw-r--r--
removed slightly odd Isar_Document.init; simplified begin_document: associate empty command/state with no_id, which is implicit start; replaced make_id by create_id (cf. Scala version); eliminated CRITICAL/Unsynchronized.ref in favour of Synchronized.var; tuned;
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
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    14
  val init: string -> unit
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    15
end;
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    16
31797
203d5e61e3bc renamed IsabelleProcess to Isabelle_Process;
wenzelm
parents: 31384
diff changeset
    17
structure Isabelle_Process: ISABELLE_PROCESS =
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    18
struct
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
    19
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    20
(* print modes *)
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    21
25748
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    22
val isabelle_processN = "isabelle_process";
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    23
26550
a8740ad422d2 removed yxmlN for now;
wenzelm
parents: 26543
diff changeset
    24
val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
28036
wenzelm
parents: 27986
diff changeset
    25
val _ = Markup.add_mode isabelle_processN YXML.output_markup;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    26
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    27
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    28
(* message markup *)
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
local
25810
bac99880fa99 output message properties: id or position;
wenzelm
parents: 25748
diff changeset
    31
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    32
fun chunk s = string_of_int (size s) ^ "\n" ^ s;
26574
560d07845442 support "YXML" mode for output transfer notation;
wenzelm
parents: 26550
diff changeset
    33
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    34
fun message _ _ _ "" = ()
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    35
  | message out_stream ch props body =
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    36
      let
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    37
        val header = YXML.string_of (XML.Elem (ch, map (pairself YXML.binary_text) props, []));
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    38
        val msg = Symbol.STX ^ chunk header ^ chunk body;
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    39
      in TextIO.output (out_stream, msg) (*atomic output*) end;
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    40
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    41
in
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    42
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    43
fun standard_message out_stream ch body =
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    44
  message out_stream ch (Position.properties_of (Position.thread_data ())) body;
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    45
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    46
fun init_message out_stream =
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    47
  let
28491
c5420429a5aa plain process_id function;
wenzelm
parents: 28343
diff changeset
    48
    val pid = (Markup.pidN, process_id ());
27972
a87be8fcb25c symbolic message markup;
wenzelm
parents: 27961
diff changeset
    49
    val session = (Markup.sessionN, List.last (Session.id ()) handle List.Empty => "unknown");
29522
793766d4c1b5 moved message markup into Scala layer -- reduced redundancy;
wenzelm
parents: 29328
diff changeset
    50
    val text = Session.welcome ();
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    51
  in message out_stream "A" [pid, session] text end;
25748
55a458a31e37 added PROMPT message;
wenzelm
parents: 25631
diff changeset
    52
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    53
end;
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    54
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    55
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    56
(* channels *)
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    57
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    58
local
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    59
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    60
fun auto_flush stream =
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    61
  let
28189
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
    62
    val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    63
    fun loop () =
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    64
      (OS.Process.sleep (Time.fromMilliseconds 50); try TextIO.flushOut stream; loop ());
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    65
  in loop end;
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    66
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    67
in
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    68
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    69
fun setup_channels out =
28189
fbad2eb5be9e auto_flush: uniform block buffering for all output streams;
wenzelm
parents: 28188
diff changeset
    70
  let
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    71
    val path = File.platform_path (Path.explode out);
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    72
    val out_stream = TextIO.openOut path;  (*fifo blocks until reader is ready*)
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    73
    val _ = OS.FileSys.remove path;  (*prevent alien access, indicate writer is ready*)
28242
f978c8e75118 SimpleThread.fork;
wenzelm
parents: 28189
diff changeset
    74
    val _ = SimpleThread.fork false (auto_flush out_stream);
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    75
    val _ = SimpleThread.fork false (auto_flush TextIO.stdOut);
28242
f978c8e75118 SimpleThread.fork;
wenzelm
parents: 28189
diff changeset
    76
    val _ = SimpleThread.fork false (auto_flush TextIO.stdErr);
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    77
  in
34096
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    78
    Output.status_fn   := standard_message out_stream "B";
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    79
    Output.writeln_fn  := standard_message out_stream "C";
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    80
    Output.priority_fn := standard_message out_stream "D";
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    81
    Output.tracing_fn  := standard_message out_stream "E";
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    82
    Output.warning_fn  := standard_message out_stream "F";
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    83
    Output.error_fn    := standard_message out_stream "G";
e438a5875c16 simplified message format: chunks with explicit size in bytes;
wenzelm
parents: 33225
diff changeset
    84
    Output.debug_fn    := standard_message out_stream "H";
28498
cb1b43edb5ed eliminated prompt messages;
wenzelm
parents: 28491
diff changeset
    85
    Output.prompt_fn   := ignore;
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    86
    out_stream
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    87
  end;
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    88
28188
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    89
end;
51ccf7fa6f18 auto_flush stdout, stderr as well;
wenzelm
parents: 28182
diff changeset
    90
25841
af7566faaa0f added symbol output mode, with XML escapes;
wenzelm
parents: 25820
diff changeset
    91
25554
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    92
(* init *)
082d97057e23 added test_markup;
wenzelm
parents: 25528
diff changeset
    93
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    94
fun init out =
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 31797
diff changeset
    95
 (Unsynchronized.change print_mode (update (op =) isabelle_processN);
28044
e4b569b53e10 explicit output stream, typically a named pipe;
wenzelm
parents: 28036
diff changeset
    96
  setup_channels out |> init_message;
28342
d0db291f7194 init: OuterKeyword.report;
wenzelm
parents: 28242
diff changeset
    97
  OuterKeyword.report ();
31384
ce169bd37fc0 IsabelleProcess: emit status "ready" after initialization and reports;
wenzelm
parents: 30173
diff changeset
    98
  Output.status (Markup.markup Markup.ready "");
26643
99f5407c05ef Isar.toplevel_loop: separate init/welcome flag;
wenzelm
parents: 26607
diff changeset
    99
  Isar.toplevel_loop {init = true, welcome = false, sync = true, secure = true});
25528
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   100
e67230c2b952 Isabelle process wrapper -- interaction via external program.
wenzelm
parents:
diff changeset
   101
end;