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