src/Pure/System/isabelle_process.ML
author wenzelm
Fri Oct 31 17:08:54 2014 +0100 (2014-10-31)
changeset 58848 fd0c85d7da38
parent 58843 521cea5fa777
child 58850 1bb0ad7827b4
permissions -rw-r--r--
eliminated odd flags and hook;
     1 (*  Title:      Pure/System/isabelle_process.ML
     2     Author:     Makarius
     3 
     4 Isabelle process wrapper.
     5 *)
     6 
     7 signature ISABELLE_PROCESS =
     8 sig
     9   val is_active: unit -> bool
    10   val protocol_command: string -> (string list -> unit) -> unit
    11   val reset_tracing: Document_ID.exec -> unit
    12   val crashes: exn list Synchronized.var
    13   val init_fifos: string -> string -> unit
    14   val init_socket: string -> unit
    15 end;
    16 
    17 structure Isabelle_Process: ISABELLE_PROCESS =
    18 struct
    19 
    20 (* print mode *)
    21 
    22 val isabelle_processN = "isabelle_process";
    23 
    24 fun is_active () = Print_Mode.print_mode_active isabelle_processN;
    25 
    26 val _ = Output.add_mode isabelle_processN Output.default_output Output.default_escape;
    27 val _ = Markup.add_mode isabelle_processN YXML.output_markup;
    28 
    29 
    30 (* protocol commands *)
    31 
    32 local
    33 
    34 val commands =
    35   Synchronized.var "Isabelle_Process.commands"
    36     (Symtab.empty: (string list -> unit) Symtab.table);
    37 
    38 in
    39 
    40 fun protocol_command name cmd =
    41   Synchronized.change commands (fn cmds =>
    42    (if not (Symtab.defined cmds name) then ()
    43     else warning ("Redefining Isabelle protocol command " ^ quote name);
    44     Symtab.update (name, cmd) cmds));
    45 
    46 fun run_command name args =
    47   (case Symtab.lookup (Synchronized.value commands) name of
    48     NONE => error ("Undefined Isabelle protocol command " ^ quote name)
    49   | SOME cmd =>
    50       (Runtime.debugging NONE cmd args handle exn =>
    51         error ("Isabelle protocol command failure: " ^ quote name ^ "\n" ^
    52           Runtime.exn_message exn)));
    53 
    54 end;
    55 
    56 
    57 (* restricted tracing messages *)
    58 
    59 val tracing_messages =
    60   Synchronized.var "tracing_messages" (Inttab.empty: int Inttab.table);
    61 
    62 fun reset_tracing exec_id =
    63   Synchronized.change tracing_messages (Inttab.delete_safe exec_id);
    64 
    65 fun update_tracing () =
    66   (case Position.parse_id (Position.thread_data ()) of
    67     NONE => ()
    68   | SOME exec_id =>
    69       let
    70         val ok =
    71           Synchronized.change_result tracing_messages (fn tab =>
    72             let
    73               val n = the_default 0 (Inttab.lookup tab exec_id) + 1;
    74               val ok = n <= Options.default_int "editor_tracing_messages";
    75             in (ok, Inttab.update (exec_id, n) tab) end);
    76       in
    77         if ok then ()
    78         else
    79           let
    80             val (text, promise) = Active.dialog_text ();
    81             val _ =
    82               writeln ("Tracing paused.  " ^ text "Stop" ^ ", or continue with next " ^
    83                 text "100" ^ ", " ^ text "1000" ^ ", " ^ text "10000" ^ " messages?")
    84             val m = Markup.parse_int (Future.join promise)
    85               handle Fail _ => error "Stopped";
    86           in
    87             Synchronized.change tracing_messages
    88               (Inttab.map_default (exec_id, 0) (fn k => k - m))
    89           end
    90       end);
    91 
    92 
    93 (* output channels *)
    94 
    95 val serial_props = Markup.serial_properties o serial;
    96 
    97 fun init_channels channel =
    98   let
    99     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdOut, IO.LINE_BUF);
   100     val _ = TextIO.StreamIO.setBufferMode (TextIO.getOutstream TextIO.stdErr, IO.LINE_BUF);
   101 
   102     val msg_channel = Message_Channel.make channel;
   103 
   104     fun message name props body =
   105       Message_Channel.send msg_channel (Message_Channel.message name props body);
   106 
   107     fun standard_message props name body =
   108       if forall (fn s => s = "") body then ()
   109       else
   110         let
   111           val props' =
   112             (case (Properties.defined props Markup.idN, Position.get_id (Position.thread_data ())) of
   113               (false, SOME id') => props @ [(Markup.idN, id')]
   114             | _ => props);
   115         in message name props' body end;
   116   in
   117     Output.status_fn := standard_message [] Markup.statusN;
   118     Output.report_fn := standard_message [] Markup.reportN;
   119     Output.result_fn :=
   120       (fn props => fn s => standard_message (props @ serial_props ()) Markup.resultN s);
   121     Output.writeln_fn := (fn s => standard_message (serial_props ()) Markup.writelnN s);
   122     Output.tracing_fn :=
   123       (fn s => (update_tracing (); standard_message (serial_props ()) Markup.tracingN s));
   124     Output.warning_fn := (fn s => standard_message (serial_props ()) Markup.warningN s);
   125     Output.error_message_fn :=
   126       (fn (i, s) => standard_message (Markup.serial_properties i) Markup.errorN s);
   127     Output.system_message_fn := message Markup.systemN [];
   128     Output.protocol_message_fn := message Markup.protocolN;
   129     Output.prompt_fn := ignore;
   130     message Markup.initN [] [Session.welcome ()];
   131     msg_channel
   132   end;
   133 
   134 
   135 (* protocol loop -- uninterruptible *)
   136 
   137 val crashes = Synchronized.var "Isabelle_Process.crashes" ([]: exn list);
   138 
   139 local
   140 
   141 fun recover crash =
   142   (Synchronized.change crashes (cons crash);
   143     Output.physical_stderr
   144       "Recovered from Isabelle process crash -- see also Isabelle_Process.crashes\n");
   145 
   146 fun read_chunk channel len =
   147   let
   148     val n =
   149       (case Int.fromString len of
   150         SOME n => n
   151       | NONE => error ("Isabelle process: malformed header " ^ quote len));
   152     val chunk = System_Channel.inputN channel n;
   153     val i = size chunk;
   154   in
   155     if i <> n then
   156       error ("Isabelle process: bad chunk (unexpected EOF after " ^
   157         string_of_int i ^ " of " ^ string_of_int n ^ " bytes)")
   158     else chunk
   159   end;
   160 
   161 fun read_command channel =
   162   (case System_Channel.input_line channel of
   163     NONE => raise Runtime.TERMINATE
   164   | SOME line => map (read_chunk channel) (space_explode "," line));
   165 
   166 fun task_context e =
   167   Future.task_context "Isabelle_Process.loop" (Future.new_group NONE) e ();
   168 
   169 in
   170 
   171 fun loop channel =
   172   let val continue =
   173     (case read_command channel of
   174       [] => (Output.system_message "Isabelle process: no input"; true)
   175     | name :: args => (task_context (fn () => run_command name args); true))
   176     handle Runtime.TERMINATE => false
   177       | exn => (Runtime.exn_system_message exn handle crash => recover crash; true);
   178   in
   179     if continue then loop channel
   180     else (Future.shutdown (); Execution.reset (); ())
   181   end;
   182 
   183 end;
   184 
   185 
   186 (* init *)
   187 
   188 val default_modes1 =
   189   [Syntax_Trans.no_bracketsN, Syntax_Trans.no_type_bracketsN, Graph_Display.active_graphN];
   190 val default_modes2 = [Symbol.xsymbolsN, isabelle_processN, Pretty.symbolicN];
   191 
   192 val init = uninterruptible (fn _ => fn rendezvous =>
   193   let
   194     val _ = SHA1_Samples.test ()
   195       handle exn as Fail msg => (Output.physical_stderr (msg ^ "\n"); reraise exn);
   196     val _ = Output.physical_stderr Symbol.STX;
   197 
   198     val _ = Printer.show_markup_default := true;
   199     val _ = Context.set_thread_data NONE;
   200     val _ =
   201       Unsynchronized.change print_mode
   202         (fn mode => (mode @ default_modes1) |> fold (update op =) default_modes2);
   203 
   204     val channel = rendezvous ();
   205     val msg_channel = init_channels channel;
   206     val _ = Session.init_protocol_handlers ();
   207     val _ = loop channel;
   208   in Message_Channel.shutdown msg_channel end);
   209 
   210 fun init_fifos fifo1 fifo2 = init (fn () => System_Channel.fifo_rendezvous fifo1 fifo2);
   211 fun init_socket name = init (fn () => System_Channel.socket_rendezvous name);
   212 
   213 end;
   214