4 module Stack = Continuationals.Stack
6 let utf8_length = Netconversion.ustring_length `Enc_utf8
8 (*** from matitaScript.ml ***)
9 (* let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$" *)
11 let eval_statement include_paths (* (buffer : GText.buffer) *) status (* script *)
14 let ast,unparsed_text =
17 (* if Pcre.pmatch ~rex:only_dust_RE text then raise Margin; *)
19 GrafiteParser.parsable_statement status
20 (Ulexing.from_utf8_string text) in
21 let ast = MatitaEngine.get_ast status include_paths strm in
23 | `Ast (st, text) -> st, text
25 let floc = match ast with
26 | GrafiteAst.Executable (loc, _)
27 | GrafiteAst.Comment (loc, _) -> loc in
29 let _,lend = HExtlib.loc_of_floc floc in
30 let parsed_text, parsed_text_len =
31 MatitaGtkMisc.utf8_parsed_text unparsed_text (HExtlib.floc_of_loc (0,lend)) in
32 let byte_parsed_text_len = String.length parsed_text in
34 String.sub unparsed_text byte_parsed_text_len
35 (String.length unparsed_text - byte_parsed_text_len)
39 MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
41 (status, parsed_text, unparsed_txt'),"",(*parsed_text_len*)
42 utf8_length parsed_text
45 let status = ref (new MatitaEngine.status "cic:/matita");;
46 let history = ref [!status];;
48 let include_paths = ["/home/barolo/matitaB/matita/lib"];;
52 * <metaname>...</metaname>
59 let _,_,metasenv,subst,_ = s#obj in
60 let render_switch = function
61 | Stack.Open i -> "?" ^ (string_of_int i)
62 | Stack.Closed i -> "<S>?" ^ (string_of_int i) ^ "</S>"
64 let int_of_switch = function
65 | Stack.Open i | Stack.Closed i -> i
67 let sequent = function
69 let meta = List.assoc i metasenv in
70 snd (ApplyTransformation.ntxt_of_cic_sequent
71 ~metasenv ~subst ~map_unicode_to_tex:false 80 s (i,meta))
72 | Stack.Closed _ -> "This goal has already been closed."
74 let render_sequent is_loc acc depth tag (pos,sw) =
75 let metano = int_of_switch sw in
78 (match depth, pos with
79 | 0, 0 -> "<B>" ^ (render_switch sw) ^ "</B>"
81 Printf.sprintf "<B>|<SUB>%d</SUB>: %s</B>" pos (render_switch sw)
82 | 1, pos when Stack.head_tag s#stack = `BranchTag ->
83 Printf.sprintf "|<SUB>%d</SUB> : %s" pos (render_switch sw)
84 | _ -> render_switch sw)
87 prerr_endline "pippo1";
89 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () markup in
90 let markup = "<metaname>" ^ markup ^ "</metaname>" in
91 prerr_endline "pippo2";
93 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () (sequent sw)
95 let txt0 = "<goal>" ^ sequent ^ "</goal>" in
96 "<meta number=\"" ^ (string_of_int metano) ^ "\">" ^ markup ^
97 txt0 ^ "</meta>" ^ acc
99 let res = "<metasenv>" ^
101 ~env:(render_sequent true) ~cont:(render_sequent false)
102 ~todo:(render_sequent false) "" s#stack) ^
105 prerr_endline ("sending metasenv:\n" ^ res); res
108 (* let html_of_status s =
109 let _,_,metasenv,subst,_ = s#obj in
110 let txt = List.fold_left
111 (fun acc (nmeta,_ as meta) ->
112 let txt0 = snd (ApplyTransformation.ntxt_of_cic_sequent
113 ~metasenv ~subst ~map_unicode_to_tex:false 80 s meta)
115 prerr_endline ("### txt0 = " ^ txt0);
116 ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
119 String.concat "\n\n" txt
122 let heading_nl_RE = Pcre.regexp "^\\s*\n\\s*";;
125 let s = Pcre.replace ~rex:heading_nl_RE s in
127 let nl_pos = String.index s '\n' in
128 String.sub s 0 nl_pos
132 let read_file fname =
133 let chan = open_in fname in
134 let lines = ref [] in
137 lines := input_line chan :: !lines
139 with End_of_file -> close_in chan);
140 String.concat "\n" (List.rev !lines)
143 let load_index outchan =
144 let s = read_file "index.html" in
145 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
148 let load_doc filename outchan =
149 let s = read_file filename in
151 try String.sub filename (String.length filename - 4) 4 = ".png"
152 with Invalid_argument _ -> false
154 let contenttype = if is_png then "image/png" else "text/html" in
155 Http_daemon.respond ~headers:["Content-Type", contenttype] ~code:(`Code 200) ~body:s outchan
158 let call_service outchan =
160 (ignore(MatitaEngine.assert_ng
161 ~include_paths:["/home/barolo/matitaB/matita/lib"] (* ~outch:outchan *)
162 "/home/barolo/matitaB/matita/lib/basics/pts.ma");
163 prerr_endline "fatto";
164 let s = read_file "/home/barolo/matitaB/matita/lib/basics/pts.ma.mad"
166 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
169 e -> Http_daemon.respond ~code:(`Code 500) outchan
172 let retrieve (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
173 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
176 ~content_type:"text/xml; charset=\"utf-8\""
178 let filename = cgi # argument_value "file" in
179 prerr_endline ("reading file " ^ filename);
181 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
182 (read_file filename) in
183 prerr_endline ("sending:\nBEGIN\n" ^ body ^ "\nEND");
184 let body = "<file>" ^ body ^ "</file>" in
186 Librarian.baseuri_of_script ~include_paths:[] filename
188 status := (!status)#set_baseuri baseuri;
189 cgi#out_channel#output_string body;
190 cgi#out_channel#commit_work()
194 let (st,new_statements,new_unparsed),(* newtext TODO *) _,parsed_len =
196 eval_statement include_paths (*buffer*) !status (`Raw text)
197 (* with End_of_file -> raise Margin *)
200 history := st :: !history;
201 parsed_len, new_unparsed
204 (* returns the length of the executed text and an html representation of the
206 let advance (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
207 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
210 ~content_type:"text/xml; charset=\"utf-8\""
212 let text = cgi#argument_value "body" in
213 prerr_endline ("body =\n" ^ text);
214 let parsed_len, new_unparsed = advance0 text in
215 let txt = output_status !status in
217 "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\" />" ^ txt
220 prerr_endline ("sending advance response:\n" ^ body);
221 cgi#out_channel#output_string body;
222 cgi#out_channel#commit_work()
225 let gotoBottom (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
226 let rec aux parsed_len text =
228 prerr_endline ("evaluating: " ^ first_line text);
229 let plen,new_unparsed = advance0 text in
230 aux (parsed_len+plen) new_unparsed
234 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
237 ~content_type:"text/xml; charset=\"utf-8\""
239 let text = cgi#argument_value "body" in
240 prerr_endline ("body =\n" ^ text);
241 let parsed_len = aux 0 text in
242 let txt = output_status !status in
244 "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\" />" ^ txt
247 prerr_endline ("sending goto bottom response:\n" ^ body);
248 cgi#out_channel#output_string body;
249 cgi#out_channel#commit_work()
252 let retract (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
253 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
256 ~content_type:"text/xml; charset=\"utf-8\""
258 let new_history,new_status =
260 _::(status::_ as history) ->
262 | [_] -> (prerr_endline "singleton";failwith "retract")
263 | _ -> (prerr_endline "nil"; assert false) in
264 NCicLibrary.time_travel !status;
265 history := new_history;
266 status := new_status;
267 let body = output_status !status in
268 cgi#out_channel#output_string body;
269 cgi#out_channel#commit_work()
273 open Netcgi1_compat.Netcgi_types;;
275 (**********************************************************************)
276 (* Create the webserver *)
277 (**********************************************************************)
281 let (opt_list, cmdline_cfg) = Netplex_main.args() in
283 let use_mt = ref true in
286 [ "-mt", Arg.Set use_mt,
287 " Use multi-threading instead of multi-processing"
292 (fun s -> raise (Arg.Bad ("Don't know what to do with: " ^ s)))
293 "usage: netplex [options]";
296 Netplex_mt.mt() (* multi-threading *)
298 Netplex_mp.mp() in (* multi-processing *)
301 { Nethttpd_services.dyn_handler = (fun _ -> process1);
302 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
303 dyn_uri = None; (* not needed *)
304 dyn_translator = (fun _ -> ""); (* not needed *)
305 dyn_accept_all_conditionals = false;
309 { Nethttpd_services.dyn_handler = (fun _ -> advance);
310 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
311 dyn_uri = None; (* not needed *)
312 dyn_translator = (fun _ -> ""); (* not needed *)
313 dyn_accept_all_conditionals = false;
316 { Nethttpd_services.dyn_handler = (fun _ -> retract);
317 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
318 dyn_uri = None; (* not needed *)
319 dyn_translator = (fun _ -> ""); (* not needed *)
320 dyn_accept_all_conditionals = false;
323 { Nethttpd_services.dyn_handler = (fun _ -> gotoBottom);
324 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
325 dyn_uri = None; (* not needed *)
326 dyn_translator = (fun _ -> ""); (* not needed *)
327 dyn_accept_all_conditionals = false;
330 { Nethttpd_services.dyn_handler = (fun _ -> retrieve);
331 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
332 dyn_uri = None; (* not needed *)
333 dyn_translator = (fun _ -> ""); (* not needed *)
334 dyn_accept_all_conditionals = false;
337 let nethttpd_factory =
338 Nethttpd_plex.nethttpd_factory
339 ~handlers:[ "advance", do_advance
340 ; "retract", do_retract
341 ; "bottom", goto_bottom
344 MatitaInit.initialize_all ();
347 Netplex_log.logger_factories (* allow all built-in logging styles *)
348 Netplex_workload.workload_manager_factories (* ... all ways of workload management *)
349 [ nethttpd_factory ] (* make this nethttpd available *)
353 Sys.set_signal Sys.sigpipe Sys.Signal_ignore;