discontinued Output.debug, which belongs to early PGIP experiments (b6788dbd2ef9) and causes just too many problems (like spamming the message channel if it is used by more than one module);
1 (* Title: Pure/System/isabelle_process.ML
4 Isabelle process wrapper, based on private fifos for maximum
5 robustness and performance.
8 signature ISABELLE_PROCESS =
10 val isabelle_processN: string
11 val add_command: string -> (string list -> unit) -> unit
12 val command: string -> string list -> unit
13 val crashes: exn list Unsynchronized.ref
14 val init: string -> string -> unit
17 structure Isabelle_Process: ISABELLE_PROCESS =
22 val isabelle_processN = "isabelle_process";
24 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
25 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
32 val global_commands = Unsynchronized.ref (Symtab.empty: (string list -> unit) Symtab.table);
36 fun add_command name cmd = CRITICAL (fn () =>
37 Unsynchronized.change global_commands (fn cmds =>
38 (if not (Symtab.defined cmds name) then ()
39 else warning ("Redefining Isabelle process command " ^ quote name);
40 Symtab.update (name, cmd) cmds)));
42 fun command name args =
43 (case Symtab.lookup (! global_commands) name of
44 NONE => error ("Undefined Isabelle process command " ^ quote name)
45 | SOME cmd => cmd args);
54 fun chunk s = string_of_int (size s) ^ "\n" ^ s;
56 fun message _ _ _ "" = ()
57 | message out_stream ch raw_props body =
59 val robust_props = map (pairself YXML.escape_controls) raw_props;
60 val header = YXML.string_of (XML.Elem ((ch, robust_props), []));
61 in TextIO.output (out_stream, chunk header ^ chunk body) (*atomic output!*) end;
65 fun standard_message out_stream with_serial ch body =
67 ((if with_serial then cons (Markup.serialN, serial_string ()) else I)
68 (Position.properties_of (Position.thread_data ()))) body;
70 fun init_message out_stream =
71 message out_stream "A" [(Markup.pidN, process_id ())] (Session.welcome ());
80 fun auto_flush stream =
82 val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream stream, IO.BLOCK_BUF);
84 (OS.Process.sleep (Time.fromMilliseconds 20); try TextIO.flushOut stream; loop ());
87 fun rendezvous f fifo =
89 val path = File.platform_path (Path.explode fifo);
90 val result = f fifo; (*should block until peer is ready*)
92 if String.isSuffix "cygwin" ml_platform then () (*Cygwin 1.7: no proper blocking on open*)
93 else OS.FileSys.remove path; (*prevent future access*)
98 fun setup_channels in_fifo out_fifo =
100 val in_stream = rendezvous TextIO.openIn in_fifo;
101 val out_stream = rendezvous TextIO.openOut out_fifo;
102 val _ = Simple_Thread.fork false (auto_flush out_stream);
103 val _ = Simple_Thread.fork false (auto_flush TextIO.stdOut);
104 val _ = Simple_Thread.fork false (auto_flush TextIO.stdErr);
106 Output.status_fn := standard_message out_stream false "B";
107 Output.report_fn := standard_message out_stream false "C";
108 Output.writeln_fn := standard_message out_stream true "D";
109 Output.tracing_fn := standard_message out_stream true "E";
110 Output.warning_fn := standard_message out_stream true "F";
111 Output.error_fn := standard_message out_stream true "G";
112 Output.priority_fn := ! Output.writeln_fn;
113 Output.prompt_fn := ignore;
114 (in_stream, out_stream)
120 (* protocol loop -- uninterruptible *)
122 val crashes = Unsynchronized.ref ([]: exn list);
127 (CRITICAL (fn () => Unsynchronized.change crashes (cons crash));
128 warning "Recovering from Isabelle process crash -- see also Isabelle_Process.crashes");
130 fun read_chunk stream len =
133 (case Int.fromString len of
135 | NONE => error ("Isabelle process: malformed chunk header " ^ quote len));
136 val chunk = TextIO.inputN (stream, n);
140 else error ("Isabelle process: bad chunk (" ^ string_of_int m ^ " vs. " ^ string_of_int n ^ ")")
143 fun read_command stream =
144 (case TextIO.inputLine stream of
145 NONE => raise Runtime.TERMINATE
146 | SOME line => map (read_chunk stream) (space_explode "," line));
148 fun run_command name args =
149 Runtime.debugging (command name) args
151 error ("Isabelle process command failure: " ^ name ^ "\n" ^ ML_Compiler.exn_message exn);
157 (case read_command stream of
158 [] => (Output.error_msg "Isabelle process: no input"; true)
159 | name :: args => (run_command name args; true))
160 handle Runtime.TERMINATE => false
161 | exn => (Output.error_msg (ML_Compiler.exn_message exn) handle crash => recover crash; true);
162 in if continue then loop stream else () end;
169 fun init in_fifo out_fifo =
171 val _ = Unsynchronized.change print_mode
172 (fold (update op =) [isabelle_processN, Keyword.keyword_statusN, Pretty.symbolicN]);
173 val (in_stream, out_stream) = setup_channels in_fifo out_fifo;
174 val _ = init_message out_stream;
175 val _ = quick_and_dirty := true; (* FIXME !? *)
176 val _ = Keyword.status ();
177 val _ = Output.status (Markup.markup Markup.ready "");
178 val _ = Context.set_thread_data NONE;
179 val _ = Simple_Thread.fork false (fn () => (loop in_stream; quit ()));