4 exception Emphasized_error of string
5 exception Disamb_error of string
7 module Stack = Continuationals.Stack
9 let debug = prerr_endline
10 (* disable for debug *)
11 let prerr_endline _ = ()
13 let rt_path () = Helm_registry.get "matita.rt_base_dir"
15 let libdir uid = (rt_path ()) ^ "/users/" ^ uid
17 let utf8_length = Netconversion.ustring_length `Enc_utf8
19 let mutex = Mutex.create ();;
21 let to_be_committed = ref [];;
23 let html_of_matita s =
24 let patt1 = Str.regexp "\005" in
25 let patt2 = Str.regexp "\006" in
26 let patt3 = Str.regexp "<" in
27 let patt4 = Str.regexp ">" in
28 let res = Str.global_replace patt4 ">" s in
29 let res = Str.global_replace patt3 "<" res in
30 let res = Str.global_replace patt2 ">" res in
31 let res = Str.global_replace patt1 "<" res in
35 (* adds a user to the commit queue; concurrent instances possible, so we
36 * enclose the update in a CS
38 let add_user_for_commit uid =
40 to_be_committed := uid::List.filter (fun x -> x <> uid) !to_be_committed;
44 let do_global_commit () =
45 prerr_endline ("to be committed: " ^ String.concat " " !to_be_committed);
48 let ft = MatitaAuthentication.read_ft u in
50 (* first we add new files/dirs to the repository *)
51 (* must take the reverse because svn requires the add to be performed in
53 (otherwise run with --parents option) *)
54 let to_be_added = List.rev (List.map fst
55 (List.filter (fun (_,flag) -> flag = MatitaFilesystem.MAdd) ft))
57 prerr_endline ("@@@ ADDING files: " ^ String.concat ", " to_be_added);
60 let newout = MatitaFilesystem.add_files u to_be_added in
63 | MatitaFilesystem.SvnError outstr ->
64 prerr_endline ("ADD OF " ^ u ^ "FAILED:" ^ outstr);
68 (* now we update the local copy (to merge updates from other users) *)
70 let files,anomalies,(added,conflict,del,upd,merged) =
71 MatitaFilesystem.update_user u
73 let anomalies = String.concat "\n" anomalies in
74 let details = Printf.sprintf
76 "%d deleted files\n"^^
77 "%d updated files\n"^^
79 "%d conflicting files\n\n" ^^
80 "Anomalies:\n%s") added del upd merged conflict anomalies
82 prerr_endline ("update details:\n" ^ details);
83 MatitaAuthentication.set_file_flag u files;
86 | MatitaFilesystem.SvnError outstr ->
87 prerr_endline ("UPDATE OF " ^ u ^ "FAILED:" ^ outstr);
91 (* we re-read the file table after updating *)
92 let ft = MatitaAuthentication.read_ft u in
94 (* finally we perform the real commit *)
95 let modified = (List.map fst
96 (List.filter (fun (_,flag) -> flag = MatitaFilesystem.MModified) ft))
98 let to_be_committed = to_be_added @ modified
101 let newout = MatitaFilesystem.commit u to_be_committed in
104 | MatitaFilesystem.SvnError outstr ->
105 prerr_endline ("COMMIT OF " ^ u ^ "FAILED:" ^ outstr);
109 (* call stat to get the final status *)
110 let files, anomalies = MatitaFilesystem.stat_user u in
111 let added,not_added = List.fold_left
112 (fun (a_acc, na_acc) fname ->
113 if List.mem fname (List.map fst files) then
116 fname::a_acc, na_acc)
119 let committed,not_committed = List.fold_left
120 (fun (c_acc, nc_acc) fname ->
121 if List.mem fname (List.map fst files) then
124 fname::c_acc, nc_acc)
127 let conflicts = List.map fst (List.filter
128 (fun (_,f) -> f = Some MatitaFilesystem.MConflict) files)
130 MatitaAuthentication.set_file_flag u
131 (List.map (fun x -> x, Some MatitaFilesystem.MSynchronized) (added@committed));
132 MatitaAuthentication.set_file_flag u files;
133 out ^ "\n\n" ^ (Printf.sprintf
134 ("COMMIT RESULTS for %s\n" ^^
135 "==============\n" ^^
136 "added and committed (%d of %d): %s\n" ^^
137 "modified and committed (%d of %d): %s\n" ^^
139 "not committed: %s\n" ^^
141 u (List.length added) (List.length to_be_added) (String.concat ", " added)
142 (List.length committed) (List.length modified) (String.concat ", " committed)
143 (String.concat ", " not_added)
144 (String.concat ", " not_committed) (String.concat ", " conflicts)))
146 (* XXX: at the moment, we don't keep track of the order in which users have
147 scheduled their commits, but we should, otherwise we will get a
148 "first come, random served" policy *)
149 "" (* (List.rev !to_be_committed) *) (MatitaAuthentication.get_users ())
152 (*** from matitaScript.ml ***)
153 (* let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$" *)
155 let eval_statement include_paths (* (buffer : GText.buffer) *) status (* script *)
158 let ast,unparsed_text =
161 (* if Pcre.pmatch ~rex:only_dust_RE text then raise Margin; *)
162 prerr_endline ("raw text = " ^ text);
164 GrafiteParser.parsable_statement status
165 (Ulexing.from_utf8_string text) in
166 prerr_endline "before get_ast";
167 let ast = MatitaEngine.get_ast status include_paths strm in
168 prerr_endline "after get_ast";
170 | `Ast (st, text) -> st, text
173 (* do we want to generate a trace? *)
175 not (List.mem_assoc "demod" a || List.mem_assoc "paramod" a ||
176 List.mem_assoc "fast_paramod" a || List.assoc "depth" a = "1" ||
180 let get_param a param =
182 Some (param ^ "=" ^ List.assoc param a)
183 with Not_found -> None
186 let floc = match ast with
187 | GrafiteAst.Executable (loc, _)
188 | GrafiteAst.Comment (loc, _) -> loc in
190 let lstart,lend = HExtlib.loc_of_floc floc in
191 let parsed_text, _parsed_text_len =
192 HExtlib.utf8_parsed_text unparsed_text (HExtlib.floc_of_loc (0,lend)) in
193 let parsed_text_len = utf8_length parsed_text in
194 let byte_parsed_text_len = String.length parsed_text in
196 String.sub unparsed_text byte_parsed_text_len
197 (String.length unparsed_text - byte_parsed_text_len)
199 prerr_endline (Printf.sprintf "ustring_sub caso 1: lstart=%d, parsed=%s" lstart parsed_text);
200 let pre = Netconversion.ustring_sub `Enc_utf8 0 lstart parsed_text in
204 Printf.sprintf "\005A href=\"%s\"\006%s\005/A\006"
205 (NReference.string_of_reference r) (NCicPp.r2s status true r)
207 (*if trace = [] then "{}"
208 else*) String.concat ", "
209 (HExtlib.filter_map (function
210 | NotationPt.NRef r -> Some (href r)
216 | GrafiteAst.Executable (_,
217 GrafiteAst.NCommand (_,
218 GrafiteAst.NObj (loc, astobj,_))) ->
219 let objname = NotationPt.name_of_obj astobj in
221 MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
223 let new_parsed_text = Ulexing.from_utf8_string parsed_text in
224 let interpr = GrafiteDisambiguate.get_interpr status#disambiguate_db in
225 let outstr = ref "" in
226 ignore (SmallLexer.mk_small_printer interpr outstr new_parsed_text);
227 let x, y = HExtlib.loc_of_floc floc in
228 let pre = Netconversion.ustring_sub `Enc_utf8 0 x !outstr in
229 let post = Netconversion.ustring_sub `Enc_utf8 x
230 (Netconversion.ustring_length `Enc_utf8 !outstr - x) !outstr in
231 outstr := Printf.sprintf
232 "%s\005input type=\"radio\" class=\"anchor\" id=\"%s\" /\006%s" pre objname post;
233 prerr_endline ("baseuri after advance = " ^ status#baseuri);
234 (* prerr_endline ("parser output: " ^ !outstr); *)
235 (status,!outstr, unparsed_txt'),parsed_text_len
236 | GrafiteAst.Executable (_,
237 GrafiteAst.NTactic (_,
238 [GrafiteAst.NAuto (_, (l,a as auto_params))])) when is_auto auto_params
242 | Some (_,l') -> Some (List.map (fun x -> "",0,x) l')
244 let trace_ref = ref [] in
245 let status = NnAuto.auto_tac ~params:(l,a) ~trace_ref status in
246 let new_parsed_text = pre ^ (Printf.sprintf
247 "/\005span class='autotactic'\006%s\005span class='autotrace'\006 trace %s\005/span\006\005/span\006/"
249 (List.assoc "depth" a::
250 HExtlib.filter_map (get_param a) ["width";"size"]))
251 (mk_univ !trace_ref))
253 (status,new_parsed_text, unparsed_txt'),parsed_text_len
256 MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
258 let new_parsed_text = Ulexing.from_utf8_string parsed_text in
259 let interpr = GrafiteDisambiguate.get_interpr status#disambiguate_db in
260 let outstr = ref "" in
261 ignore (SmallLexer.mk_small_printer interpr outstr new_parsed_text);
262 prerr_endline ("baseuri after advance = " ^ status#baseuri);
263 (* prerr_endline ("parser output: " ^ !outstr); *)
264 (status,!outstr, unparsed_txt'),parsed_text_len
266 (*let save_moo status =
267 let script = MatitaScript.current () in
268 let baseuri = status#baseuri in
269 match script#bos, script#eos with
272 GrafiteTypes.Serializer.serialize ~baseuri:(NUri.uri_of_string baseuri)
274 | _ -> clean_current_baseuri status
277 let sequent_size = ref 40;;
279 let include_paths = ref [];;
282 * <meta number="...">
283 * <metaname>...</metaname>
289 let output_status s =
290 let _,_,metasenv,subst,_ = s#obj in
291 let render_switch = function
292 | Stack.Open i -> "?" ^ (string_of_int i)
293 | Stack.Closed i -> "<S>?" ^ (string_of_int i) ^ "</S>"
295 let int_of_switch = function
296 | Stack.Open i | Stack.Closed i -> i
298 let sequent = function
300 let meta = List.assoc i metasenv in
301 snd (ApplyTransformation.ntxt_of_cic_sequent
302 ~metasenv ~subst ~map_unicode_to_tex:false !sequent_size s (i,meta))
303 | Stack.Closed _ -> "This goal has already been closed."
305 let render_sequent is_loc acc depth tag (pos,sw) =
306 let metano = int_of_switch sw in
309 (match depth, pos with
310 | 0, 0 -> "<span class=\"activegoal\">" ^ (render_switch sw) ^ "</span>"
312 Printf.sprintf "<span class=\"activegoal\">|<SUB>%d</SUB>: %s</span>" pos (render_switch sw)
313 | 1, pos when Stack.head_tag s#stack = `BranchTag ->
314 Printf.sprintf "<span class=\"passivegoal\">|<SUB>%d</SUB> : %s</span>" pos (render_switch sw)
315 | _ -> render_switch sw)
316 else render_switch sw
319 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () markup in
320 let markup = "<metaname>" ^ markup ^ "</metaname>" in
322 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () (sequent sw)
324 let txt0 = "<goal>" ^ sequent ^ "</goal>" in
325 "<meta number=\"" ^ (string_of_int metano) ^ "\">" ^ markup ^
326 txt0 ^ "</meta>" ^ acc
330 ~env:(render_sequent true) ~cont:(render_sequent false)
331 ~todo:(render_sequent false) "" s#stack) ^
333 (* prerr_endline ("sending metasenv:\n" ^ res); res *)
336 let heading_nl_RE = Pcre.regexp "^\\s*\n\\s*";;
339 let s = Pcre.replace ~rex:heading_nl_RE s in
341 let nl_pos = String.index s '\n' in
342 String.sub s 0 nl_pos
346 let read_file fname =
347 let chan = open_in fname in
348 let lines = ref [] in
351 lines := input_line chan :: !lines
353 with End_of_file -> close_in chan);
354 String.concat "\n" (List.rev !lines)
357 let load_index outchan =
358 let s = read_file "index.html" in
359 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
362 let load_doc filename outchan =
363 let s = read_file filename in
365 try String.sub filename (String.length filename - 4) 4 = ".png"
366 with Invalid_argument _ -> false
368 let contenttype = if is_png then "image/png" else "text/html" in
369 Http_daemon.respond ~headers:["Content-Type", contenttype] ~code:(`Code 200) ~body:s outchan
372 let retrieve (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
373 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
374 let env = cgi#environment in
376 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
377 let sid = HExtlib.unopt sid in
378 let uid = MatitaAuthentication.user_of_session sid in
382 ~content_type:"text/xml; charset=\"utf-8\""
385 let readonly = cgi # argument_value "readonly" in
386 let filename = libdir uid ^ "/" ^ (cgi # argument_value "file") in
387 (* prerr_endline ("reading file " ^ filename); *)
389 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
390 (html_of_matita (read_file filename)) in
392 (* html_of_matita (read_file filename) in *)
393 (* prerr_endline ("sending:\nBEGIN\n" ^ body ^ "\nEND"); *)
394 let body = "<response><file>" ^ body ^ "</file></response>" in
395 let baseuri, incpaths =
397 let root, baseuri, _fname, _tgt =
398 Librarian.baseuri_of_script ~include_paths:[] filename in
401 Str.split (Str.regexp " ")
402 (List.assoc "include_paths" (Librarian.load_root_file (root^"/root")))
405 let rc = root :: includes in
406 List.iter (HLog.debug) rc; baseuri, rc
408 Librarian.NoRootFor _ | Librarian.FileNotFound _ -> "",[] in
409 include_paths := incpaths;
410 if readonly <> "true" then
411 (let status = (MatitaAuthentication.get_status sid)#set_baseuri baseuri in
412 let history = [status] in
413 MatitaAuthentication.set_status sid status;
414 MatitaAuthentication.set_history sid history);
417 ~content_type:"text/xml; charset=\"utf-8\""
419 cgi#out_channel#output_string body;
423 ~status:`Internal_server_error
425 ~content_type:"text/html; charset=\"utf-8\""
427 cgi#out_channel#commit_work()
430 let xml_of_disamb_error l =
431 let mk_alias = function
432 | GrafiteAst.Ident_alias (_,uri) -> "href=\"" ^ uri ^ "\""
433 | GrafiteAst.Symbol_alias (_,uri,desc)
434 | GrafiteAst.Number_alias (uri,desc) ->
435 let uri = try HExtlib.unopt uri with _ -> "cic:/fakeuri.def(1)" in
436 "href=\"" ^ uri ^ "\" title=\"" ^
437 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
441 let mk_interpr (loc,a) =
442 let x,y = HExtlib.loc_of_floc loc in
443 Printf.sprintf "<interpretation start=\"%d\" stop=\"%d\" %s />"
447 let mk_failure (il,loc,msg) =
448 let x,y = HExtlib.loc_of_floc loc in
449 Printf.sprintf "<failure start=\"%d\" stop=\"%d\" title=\"%s\">%s</failure>"
450 x y (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () msg)
451 (String.concat "" (List.map mk_interpr il))
454 let mk_choice (a,fl) =
455 let fl' = String.concat "" (List.map mk_failure fl) in
457 | None -> "<choice>" ^ fl' ^ "</choice>"
458 | Some a -> Printf.sprintf "<choice %s>%s</choice>" (mk_alias a) fl'
461 let mk_located (loc,cl) =
462 let x,y = HExtlib.loc_of_floc loc in
463 Printf.sprintf "<choicepoint start=\"%d\" stop=\"%d\">%s</choicepoint>"
464 x y (String.concat "" (List.map mk_choice cl))
466 "<disamberror>" ^ (String.concat "" (List.map mk_located l)) ^ "</disamberror>"
469 let advance0 sid text =
470 let status = MatitaAuthentication.get_status sid in
471 let history = MatitaAuthentication.get_history sid in
472 let status = status#reset_disambiguate_db () in
473 let (st,new_statements,new_unparsed),parsed_len =
474 let rec do_exc = function
475 | HExtlib.Localized (floc,e) ->
476 let x, y = HExtlib.loc_of_floc floc in
477 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
478 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
479 let post = Netconversion.ustring_sub `Enc_utf8 y
480 (Netconversion.ustring_length `Enc_utf8 text - y) text in
481 let _,title = MatitaExcPp.to_string e in
482 (* let title = "" in *)
484 pre ^ "\005span class=\"error\" title=\"" ^ title ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
485 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
486 () (html_of_matita marked) in
487 raise (Emphasized_error marked)
488 | Disambiguate.NoWellTypedInterpretation (floc,e) ->
489 let x, y = HExtlib.loc_of_floc floc in
490 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
491 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
492 let post = Netconversion.ustring_sub `Enc_utf8 y
493 (Netconversion.ustring_length `Enc_utf8 text - y) text in
494 (*let _,title = MatitaExcPp.to_string e in*)
495 (* let title = "" in *)
497 pre ^ "\005span class=\"error\" title=\"" ^ e ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
498 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
499 () (html_of_matita marked) in
500 raise (Emphasized_error marked)
501 | NCicRefiner.Uncertain m as exn ->
502 let floc, e = Lazy.force m in
503 let x, y = HExtlib.loc_of_floc floc in
504 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
505 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
506 let post = Netconversion.ustring_sub `Enc_utf8 y
507 (Netconversion.ustring_length `Enc_utf8 text - y) text in
508 (* let _,title = MatitaExcPp.to_string e in *)
509 (* let title = "" in *)
511 pre ^ "\005span class=\"error\" title=\"" ^ e ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
512 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
513 () (html_of_matita marked) in
514 raise (Emphasized_error marked)
515 | NTacStatus.Error (s,None) as e ->
517 ("NTacStatus.Error " ^ (Lazy.force s)); raise e
518 | NTacStatus.Error (s,Some exc) as e ->
520 ("NTacStatus.Error " ^ Lazy.force s ^ " -- " ^ (Printexc.to_string exc));
522 | GrafiteDisambiguate.Ambiguous_input (loc,choices) ->
523 let x,y = HExtlib.loc_of_floc loc in
524 let choice_of_alias = function
525 | GrafiteAst.Ident_alias (_,uri) -> uri, None, uri
526 | GrafiteAst.Number_alias (None,desc)
527 | GrafiteAst.Symbol_alias (_,None,desc) -> "cic:/fakeuri.def(1)", Some desc, desc
528 | GrafiteAst.Number_alias (Some uri,desc)
529 | GrafiteAst.Symbol_alias (_,Some uri,desc) -> uri, Some desc, desc
531 let tag_of_choice (uri,title,desc) =
533 | None -> Printf.sprintf "<choice href=\"%s\">%s</choice>"
535 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
536 | Some t -> Printf.sprintf "<choice href=\"%s\" title=\"%s\">%s</choice>"
538 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () t)
539 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
543 (List.map (fun x -> tag_of_choice (choice_of_alias x)) choices)
545 prerr_endline (Printf.sprintf
546 "@@@ Ambiguous input at (%d,%d). Possible choices:\n\n%s\n\n@@@ End."
549 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
550 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
551 let post = Netconversion.ustring_sub `Enc_utf8 y
552 (Netconversion.ustring_length `Enc_utf8 text - y) text in
553 let title = "Disambiguation Error" in
554 (* let title = "" in *)
556 pre ^ "\005span class=\"error\" title=\"" ^ title ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
557 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
558 () (html_of_matita marked) in
560 let strchoices = Printf.sprintf
561 "<ambiguity start=\"%d\" stop=\"%d\">%s</ambiguity>" x y strchoices
562 in raise (Disamb_error strchoices)
563 | GrafiteDisambiguate.Error l -> raise (Disamb_error (xml_of_disamb_error l))
564 (* | End_of_file -> ... *)
566 prerr_endline ("matitadaemon *** Unhandled exception " ^ Printexc.to_string e);
571 eval_statement !include_paths (*buffer*) status (`Raw text)
574 debug "BEGIN PRINTGRAMMAR";
575 (*prerr_endline (Print_grammar.ebnf_of_term status);*)
576 (*let kwds = String.concat ", " status#get_kwds in
577 debug ("keywords = " ^ kwds );*)
578 debug "END PRINTGRAMMAR";
579 MatitaAuthentication.set_status sid st;
580 MatitaAuthentication.set_history sid (st::history);
581 (* prerr_endline "previous timestamp";
582 status#print_timestamp();
583 prerr_endline "current timestamp";
584 st#print_timestamp(); *)
586 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
587 () (html_of_matita new_statements), new_unparsed, st
589 let register (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
590 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
591 let _env = cgi#environment in
593 assert (cgi#arguments <> []);
594 let uid = cgi#argument_value "userid" in
595 let userpw = cgi#argument_value "password" in
597 (* currently registering only unprivileged users *)
598 MatitaAuthentication.add_user uid userpw false;
599 (* env#set_output_header_field "Location" "/index.html" *)
600 cgi#out_channel#output_string
601 ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/login.html\">"
602 ^ "</head><body>Redirecting to login page...</body></html>")
604 | MatitaAuthentication.UsernameCollision _ ->
607 ~content_type:"text/html; charset=\"utf-8\""
609 cgi#out_channel#output_string
610 "<html><head></head><body>Error: User id collision!</body></html>"
611 | MatitaFilesystem.SvnError msg ->
614 ~content_type:"text/html; charset=\"utf-8\""
616 cgi#out_channel#output_string
617 ("<html><head></head><body><p>Error: Svn checkout failed!<p><p><textarea>"
618 ^ msg ^ "</textarea></p></body></html>"));
619 cgi#out_channel#commit_work()
622 let login (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
623 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
624 let env = cgi#environment in
626 assert (cgi#arguments <> []);
627 let uid = cgi#argument_value "userid" in
628 let userpw = cgi#argument_value "password" in
630 MatitaAuthentication.check_pw uid userpw;
631 NCicLibrary.init (Some uid);
632 let ft = MatitaAuthentication.read_ft uid in
633 let _ = MatitaFilesystem.html_of_library uid ft in
634 let sid = MatitaAuthentication.create_session uid in
635 (* let cookie = Netcgi.Cookie.make "session" (Uuidm.to_string sid) in
636 cgi#set_header ~set_cookies:[cookie] (); *)
637 env#set_output_header_field
638 "Set-Cookie" ("session=" ^ (Uuidm.to_string sid));
639 (* env#set_output_header_field "Location" "/index.html" *)
640 cgi#out_channel#output_string
641 ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/index.html\">"
642 ^ "</head><body>Redirecting to Matita page...</body></html>")
643 with MatitaAuthentication.InvalidPassword ->
646 ~content_type:"text/html; charset=\"utf-8\""
648 cgi#out_channel#output_string
649 "<html><head></head><body>Authentication error</body></html>");
650 cgi#out_channel#commit_work()
653 let logout (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
654 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
655 let env = cgi#environment in
657 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
658 let sid = HExtlib.unopt sid in
659 MatitaAuthentication.logout_user sid;
662 ~content_type:"text/html; charset=\"utf-8\""
664 let text = read_file (rt_path () ^ "/logout.html") in
665 cgi#out_channel#output_string text
669 ~status:`Internal_server_error
671 ~content_type:"text/html; charset=\"utf-8\""
673 cgi#out_channel#commit_work()
676 exception File_already_exists;;
678 let save (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
679 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
680 let env = cgi#environment in
682 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
683 let sid = HExtlib.unopt sid in
684 let status = MatitaAuthentication.get_status sid in
685 let uid = MatitaAuthentication.user_of_session sid in
686 assert (cgi#arguments <> []);
687 let locked = cgi#argument_value "locked" in
688 let unlocked = cgi#argument_value "unlocked" in
689 let dir = cgi#argument_value "dir" in
690 let rel_filename = cgi # argument_value "file" in
691 let filename = libdir uid ^ "/" ^ rel_filename in
692 let force = bool_of_string (cgi#argument_value "force") in
693 let already_exists = Sys.file_exists filename in
695 if ((not force) && already_exists) then
696 raise File_already_exists;
699 Unix.mkdir filename 0o744
702 let oc = open_out filename in
703 output_string oc (locked ^ unlocked);
705 if MatitaEngine.eos status unlocked then
707 (* prerr_endline ("serializing proof objects..."); *)
708 GrafiteTypes.Serializer.serialize
709 ~baseuri:(NUri.uri_of_string status#baseuri) status;
710 (* prerr_endline ("done."); *)
715 List.assoc rel_filename (MatitaAuthentication.read_ft uid)
716 with Not_found -> MatitaFilesystem.MUnversioned
718 (if old_flag <> MatitaFilesystem.MConflict &&
719 old_flag <> MatitaFilesystem.MAdd then
721 if already_exists then MatitaFilesystem.MModified
722 else MatitaFilesystem.MAdd
724 MatitaAuthentication.set_file_flag uid [rel_filename, Some newflag]);
727 ~content_type:"text/xml; charset=\"utf-8\""
729 cgi#out_channel#output_string "<response>ok</response>"
731 | File_already_exists ->
732 cgi#out_channel#output_string "<response>cancelled</response>"
735 ~status:`Internal_server_error
737 ~content_type:"text/xml; charset=\"utf-8\""
740 let estr = Printexc.to_string e in
741 cgi#out_channel#output_string ("<response>" ^ estr ^ "</response>"));
742 cgi#out_channel#commit_work()
745 let initiate_commit (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
746 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
747 let env = cgi#environment in
749 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
750 let sid = HExtlib.unopt sid in
751 MatitaAuthentication.probe_commit_priv sid;
752 let out = do_global_commit () in
755 ~content_type:"text/xml; charset=\"utf-8\""
757 cgi#out_channel#output_string "<commit>";
758 cgi#out_channel#output_string "<response>ok</response>";
759 cgi#out_channel#output_string ("<details>" ^ out ^ "</details>");
760 cgi#out_channel#output_string "</commit>"
764 ~status:`Internal_server_error
766 ~content_type:"text/xml; charset=\"utf-8\""
768 cgi#out_channel#commit_work()
771 let svn_update (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
772 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
773 let env = cgi#environment in
774 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
775 let sid = HExtlib.unopt sid in
776 let uid = MatitaAuthentication.user_of_session sid in
778 MatitaAuthentication.probe_commit_priv sid;
779 let files,anomalies,(added,conflict,del,upd,merged) =
780 MatitaFilesystem.update_user uid
782 let anomalies = String.concat "\n" anomalies in
783 let details = Printf.sprintf
785 "%d deleted files\n"^^
786 "%d updated files\n"^^
787 "%d merged files\n"^^
788 "%d conflicting files\n\n" ^^
789 "Anomalies:\n%s") added del upd merged conflict anomalies
791 prerr_endline ("update details:\n" ^ details);
793 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () details
795 MatitaAuthentication.set_file_flag uid files;
798 ~content_type:"text/xml; charset=\"utf-8\""
800 cgi#out_channel#output_string "<update>";
801 cgi#out_channel#output_string "<response>ok</response>";
802 cgi#out_channel#output_string ("<details>" ^ details ^ "</details>");
803 cgi#out_channel#output_string "</update>";
807 ~status:`Internal_server_error
809 ~content_type:"text/xml; charset=\"utf-8\""
811 cgi#out_channel#commit_work()
814 (* returns the length of the executed text and an html representation of the
817 let advance (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
818 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
819 let env = cgi#environment in
821 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
822 let sid = HExtlib.unopt sid in
826 ~content_type:"text/xml; charset=\"utf-8\""
829 let text = cgi#argument_value "body" in
830 (* prerr_endline ("body =\n" ^ text); *)
831 let parsed_len, new_parsed, new_unparsed, new_status = advance0 sid text in
832 let txt = output_status new_status in
834 "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\">" ^
835 new_parsed ^ "</parsed>" ^ txt
838 (* prerr_endline ("sending advance response:\n" ^ body); *)
841 ~content_type:"text/xml; charset=\"utf-8\""
843 cgi#out_channel#output_string body
845 | Emphasized_error text ->
846 (* | MultiPassDisambiguator.DisambiguationError (offset,errorll) -> *)
847 let body = "<response><error>" ^ text ^ "</error></response>" in
850 ~content_type:"text/xml; charset=\"utf-8\""
852 cgi#out_channel#output_string body
853 | Disamb_error text ->
854 let body = "<response>" ^ text ^ "</response>" in
857 ~content_type:"text/xml; charset=\"utf-8\""
859 cgi#out_channel#output_string body
862 ~status:`Internal_server_error
864 ~content_type:"text/xml; charset=\"utf-8\""
867 cgi#out_channel#commit_work()
870 let gotoBottom (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
871 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
872 let env = cgi#environment in
874 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
875 let sid = HExtlib.unopt sid in
877 let error_msg = function
878 | Emphasized_error text -> "<localized>" ^ text ^ "</localized>"
879 | Disamb_error text -> text
880 | End_of_file _ -> (* not an error *) ""
881 | e -> (* unmanaged error *)
883 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
884 (Printexc.to_string e)) ^ "</error>"
887 let rec aux acc text =
889 prerr_endline ("evaluating: " ^ first_line text);
890 let plen,new_parsed,new_unparsed,_new_status = advance0 sid text in
891 aux ((plen,new_parsed)::acc) new_unparsed
892 with e -> acc, error_msg e
893 (* DON'T SERIALIZE NOW!!!
894 let status = MatitaAuthentication.get_status sid in
895 GrafiteTypes.Serializer.serialize
896 ~baseuri:(NUri.uri_of_string status#baseuri) status;
902 ~content_type:"text/xml; charset=\"utf-8\""
905 let text = cgi#argument_value "body" in
906 (* prerr_endline ("body =\n" ^ text); *)
907 let len_parsedlist, err_msg = aux [] text in
908 let status = MatitaAuthentication.get_status sid in
909 let txt = output_status status in
910 let parsed_tag (len,txt) =
911 "<parsed length=\"" ^ (string_of_int len) ^ "\">" ^ txt ^ "</parsed>"
913 (* List.rev: the list begins with the older parsed txt *)
916 String.concat "" (List.rev (List.map parsed_tag len_parsedlist)) ^
917 txt ^ err_msg ^ "</response>"
919 (* prerr_endline ("sending goto bottom response:\n" ^ body); *)
922 ~content_type:"text/xml; charset=\"utf-8\""
924 cgi#out_channel#output_string body;
925 (* with Not_found -> cgi#set_header ~status:`Internal_server_error
927 ~content_type:"text/xml; charset=\"utf-8\"" ()); *)
928 cgi#out_channel#commit_work()
931 let gotoTop (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
932 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
933 let env = cgi#environment in
934 prerr_endline "executing goto Top";
936 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
937 let sid = HExtlib.unopt sid in
941 ~content_type:"text/xml; charset=\"utf-8\""
944 let status = MatitaAuthentication.get_status sid in
945 let uid = MatitaAuthentication.user_of_session sid in
946 let baseuri = status#baseuri in
947 let new_status = new MatitaEngine.status (Some uid) baseuri in
948 prerr_endline "gototop prima della time travel";
949 (* NCicLibrary.time_travel new_status; *)
950 prerr_endline "gototop dopo della time travel";
951 let new_history = [new_status] in
952 MatitaAuthentication.set_history sid new_history;
953 MatitaAuthentication.set_status sid new_status;
954 (* NCicLibrary.time_travel new_status; *)
957 ~content_type:"text/xml; charset=\"utf-8\""
959 cgi#out_channel#output_string "<response>ok</response>"
961 (cgi#set_header ~status:`Internal_server_error
963 ~content_type:"text/xml; charset=\"utf-8\"" ();
964 cgi#out_channel#output_string "<response>ok</response>"));
965 cgi#out_channel#commit_work()
968 let retract (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
969 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
970 let env = cgi#environment in
972 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
973 let sid = HExtlib.unopt sid in
977 ~content_type:"text/xml; charset=\"utf-8\""
980 let history = MatitaAuthentication.get_history sid in
981 let old_status = MatitaAuthentication.get_status sid in
982 let new_history,new_status =
984 _::(status::_ as history) ->
986 | [_] -> (prerr_endline "singleton";failwith "retract")
987 | _ -> (prerr_endline "nil"; assert false) in
988 (* prerr_endline "timestamp prima della retract";
989 old_status#print_timestamp ();
990 prerr_endline "timestamp della retract";
991 new_status#print_timestamp ();
992 prerr_endline ("prima della time travel"); *)
993 NCicLibrary.time_travel new_status;
994 prerr_endline ("dopo della time travel");
995 MatitaAuthentication.set_history sid new_history;
996 MatitaAuthentication.set_status sid new_status;
997 prerr_endline ("baseuri after retract = " ^ new_status#baseuri);
998 let body = output_status new_status in
1001 ~content_type:"text/xml; charset=\"utf-8\""
1003 cgi#out_channel#output_string body
1005 prerr_endline ("error in retract: " ^ Printexc.to_string e);
1006 cgi#set_header ~status:`Internal_server_error
1008 ~content_type:"text/xml; charset=\"utf-8\"" ());
1009 cgi#out_channel#commit_work()
1013 let viewLib (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
1014 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
1015 let env = cgi#environment in
1017 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
1018 let sid = HExtlib.unopt sid in
1022 ~content_type:"text/html; charset=\"utf-8\""
1025 let uid = MatitaAuthentication.user_of_session sid in
1027 let ft = MatitaAuthentication.read_ft uid in
1028 let html = MatitaFilesystem.html_of_library uid ft in
1031 ~content_type:"text/html; charset=\"utf-8\""
1033 cgi#out_channel#output_string
1036 "<title>XML Tree Control</title>\n" ^
1037 "<link href=\"treeview/xmlTree.css\" type=\"text/css\" rel=\"stylesheet\">\n" ^
1038 "<script src=\"treeview/xmlTree.js\" type=\"text/javascript\"></script>\n" ^
1040 html (* ^ "\n</body></html>" *) );
1042 let files,anomalies = MatitaFilesystem.stat_user uid in
1043 let changed = HExtlib.filter_map
1044 (fun (n,f) -> if (f = Some MatitaFilesystem.MModified) then Some n else None) files
1046 let changed = String.concat "\n" changed in
1047 let anomalies = String.concat "\n" anomalies in
1048 prerr_endline ("Changed:\n" ^ changed ^ "\n\nAnomalies:\n" ^ anomalies);
1049 cgi#out_channel#commit_work()
1053 let resetLib (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
1054 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
1055 MatitaAuthentication.reset ();
1058 ~content_type:"text/html; charset=\"utf-8\""
1061 cgi#out_channel#output_string
1063 "<title>Matitaweb Reset</title>\n" ^
1064 "<body><H1>Reset completed</H1></body></html>");
1065 cgi#out_channel#commit_work()
1067 open Netcgi1_compat.Netcgi_types;;
1069 (**********************************************************************)
1070 (* Create the webserver *)
1071 (**********************************************************************)
1075 let (opt_list, cmdline_cfg) = Netplex_main.args() in
1077 let use_mt = ref true in
1080 [ "-mt", Arg.Set use_mt,
1081 " Use multi-threading instead of multi-processing"
1086 (fun s -> raise (Arg.Bad ("Don't know what to do with: " ^ s)))
1087 "usage: netplex [options]";
1090 Netplex_mt.mt() (* multi-threading *)
1092 Netplex_mp.mp() in (* multi-processing *)
1095 { Nethttpd_services.dyn_handler = (fun _ -> process1);
1096 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1097 dyn_uri = None; (* not needed *)
1098 dyn_translator = (fun _ -> ""); (* not needed *)
1099 dyn_accept_all_conditionals = false;
1103 { Nethttpd_services.dyn_handler = (fun _ -> advance);
1104 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1105 dyn_uri = None; (* not needed *)
1106 dyn_translator = (fun _ -> ""); (* not needed *)
1107 dyn_accept_all_conditionals = false;
1110 { Nethttpd_services.dyn_handler = (fun _ -> retract);
1111 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1112 dyn_uri = None; (* not needed *)
1113 dyn_translator = (fun _ -> ""); (* not needed *)
1114 dyn_accept_all_conditionals = false;
1117 { Nethttpd_services.dyn_handler = (fun _ -> gotoBottom);
1118 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1119 dyn_uri = None; (* not needed *)
1120 dyn_translator = (fun _ -> ""); (* not needed *)
1121 dyn_accept_all_conditionals = false;
1124 { Nethttpd_services.dyn_handler = (fun _ -> gotoTop);
1125 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1126 dyn_uri = None; (* not needed *)
1127 dyn_translator = (fun _ -> ""); (* not needed *)
1128 dyn_accept_all_conditionals = false;
1131 { Nethttpd_services.dyn_handler = (fun _ -> retrieve);
1132 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1133 dyn_uri = None; (* not needed *)
1134 dyn_translator = (fun _ -> ""); (* not needed *)
1135 dyn_accept_all_conditionals = false;
1138 { Nethttpd_services.dyn_handler = (fun _ -> register);
1139 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1140 dyn_uri = None; (* not needed *)
1141 dyn_translator = (fun _ -> ""); (* not needed *)
1142 dyn_accept_all_conditionals = false;
1145 { Nethttpd_services.dyn_handler = (fun _ -> login);
1146 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1147 dyn_uri = None; (* not needed *)
1148 dyn_translator = (fun _ -> ""); (* not needed *)
1149 dyn_accept_all_conditionals = false;
1152 { Nethttpd_services.dyn_handler = (fun _ -> logout);
1153 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1154 dyn_uri = None; (* not needed *)
1155 dyn_translator = (fun _ -> ""); (* not needed *)
1156 dyn_accept_all_conditionals = false;
1159 { Nethttpd_services.dyn_handler = (fun _ -> viewLib);
1160 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1161 dyn_uri = None; (* not needed *)
1162 dyn_translator = (fun _ -> ""); (* not needed *)
1163 dyn_accept_all_conditionals = false;
1166 { Nethttpd_services.dyn_handler = (fun _ -> resetLib);
1167 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1168 dyn_uri = None; (* not needed *)
1169 dyn_translator = (fun _ -> ""); (* not needed *)
1170 dyn_accept_all_conditionals = false;
1173 { Nethttpd_services.dyn_handler = (fun _ -> save);
1174 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1175 dyn_uri = None; (* not needed *)
1176 dyn_translator = (fun _ -> ""); (* not needed *)
1177 dyn_accept_all_conditionals = false;
1180 { Nethttpd_services.dyn_handler = (fun _ -> initiate_commit);
1181 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1182 dyn_uri = None; (* not needed *)
1183 dyn_translator = (fun _ -> ""); (* not needed *)
1184 dyn_accept_all_conditionals = false;
1187 { Nethttpd_services.dyn_handler = (fun _ -> svn_update);
1188 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1189 dyn_uri = None; (* not needed *)
1190 dyn_translator = (fun _ -> ""); (* not needed *)
1191 dyn_accept_all_conditionals = false;
1195 let nethttpd_factory =
1196 Nethttpd_plex.nethttpd_factory
1197 ~handlers:[ "advance", do_advance
1198 ; "retract", do_retract
1199 ; "bottom", goto_bottom
1202 ; "register", do_register
1204 ; "logout", do_logout
1205 ; "reset", do_resetlib
1206 ; "viewlib", do_viewlib
1208 ; "commit", do_commit
1209 ; "update", do_update]
1211 MatitaInit.initialize_all ();
1212 MatitaAuthentication.deserialize ();
1213 Netplex_main.startup
1215 Netplex_log.logger_factories (* allow all built-in logging styles *)
1216 Netplex_workload.workload_manager_factories (* ... all ways of workload management *)
1217 [ nethttpd_factory ] (* make this nethttpd available *)
1221 Sys.set_signal Sys.sigpipe Sys.Signal_ignore;