4 exception Emphasized_error of string
5 exception Disamb_error of string
7 module Stack = Continuationals.Stack
9 (* disable for debug *)
10 let prerr_endline _ = ()
12 let rt_path () = Helm_registry.get "matita.rt_base_dir"
14 let libdir uid = (rt_path ()) ^ "/users/" ^ uid
16 let utf8_length = Netconversion.ustring_length `Enc_utf8
18 let mutex = Mutex.create ();;
20 let to_be_committed = ref [];;
22 let html_of_matita s =
23 let patt1 = Str.regexp "\005" in
24 let patt2 = Str.regexp "\006" in
25 let patt3 = Str.regexp "<" in
26 let patt4 = Str.regexp ">" in
27 let res = Str.global_replace patt4 ">" s in
28 let res = Str.global_replace patt3 "<" res in
29 let res = Str.global_replace patt2 ">" res in
30 let res = Str.global_replace patt1 "<" res in
34 (* adds a user to the commit queue; concurrent instances possible, so we
35 * enclose the update in a CS
37 let add_user_for_commit uid =
39 to_be_committed := uid::List.filter (fun x -> x <> uid) !to_be_committed;
43 let do_global_commit () =
44 prerr_endline ("to be committed: " ^ String.concat " " !to_be_committed);
47 let ft = MatitaAuthentication.read_ft u in
49 (* first we add new files/dirs to the repository *)
50 (* must take the reverse because svn requires the add to be performed in
52 (otherwise run with --parents option) *)
53 let to_be_added = List.rev (List.map fst
54 (List.filter (fun (_,flag) -> flag = MatitaFilesystem.MAdd) ft))
56 prerr_endline ("@@@ ADDING files: " ^ String.concat ", " to_be_added);
59 let newout = MatitaFilesystem.add_files u to_be_added in
62 | MatitaFilesystem.SvnError outstr ->
63 prerr_endline ("ADD OF " ^ u ^ "FAILED:" ^ outstr);
67 (* now we update the local copy (to merge updates from other users) *)
69 let files,anomalies,(added,conflict,del,upd,merged) =
70 MatitaFilesystem.update_user u
72 let anomalies = String.concat "\n" anomalies in
73 let details = Printf.sprintf
75 "%d deleted files\n"^^
76 "%d updated files\n"^^
78 "%d conflicting files\n\n" ^^
79 "Anomalies:\n%s") added del upd merged conflict anomalies
81 prerr_endline ("update details:\n" ^ details);
82 MatitaAuthentication.set_file_flag u files;
85 | MatitaFilesystem.SvnError outstr ->
86 prerr_endline ("UPDATE OF " ^ u ^ "FAILED:" ^ outstr);
90 (* we re-read the file table after updating *)
91 let ft = MatitaAuthentication.read_ft u in
93 (* finally we perform the real commit *)
94 let modified = (List.map fst
95 (List.filter (fun (_,flag) -> flag = MatitaFilesystem.MModified) ft))
97 let to_be_committed = to_be_added @ modified
100 let newout = MatitaFilesystem.commit u to_be_committed in
103 | MatitaFilesystem.SvnError outstr ->
104 prerr_endline ("COMMIT OF " ^ u ^ "FAILED:" ^ outstr);
108 (* call stat to get the final status *)
109 let files, anomalies = MatitaFilesystem.stat_user u in
110 let added,not_added = List.fold_left
111 (fun (a_acc, na_acc) fname ->
112 if List.mem fname (List.map fst files) then
115 fname::a_acc, na_acc)
118 let committed,not_committed = List.fold_left
119 (fun (c_acc, nc_acc) fname ->
120 if List.mem fname (List.map fst files) then
123 fname::c_acc, nc_acc)
126 let conflicts = List.map fst (List.filter
127 (fun (_,f) -> f = Some MatitaFilesystem.MConflict) files)
129 MatitaAuthentication.set_file_flag u
130 (List.map (fun x -> x, Some MatitaFilesystem.MSynchronized) (added@committed));
131 MatitaAuthentication.set_file_flag u files;
132 out ^ "\n\n" ^ (Printf.sprintf
133 ("COMMIT RESULTS for %s\n" ^^
134 "==============\n" ^^
135 "added and committed (%d of %d): %s\n" ^^
136 "modified and committed (%d of %d): %s\n" ^^
138 "not committed: %s\n" ^^
140 u (List.length added) (List.length to_be_added) (String.concat ", " added)
141 (List.length committed) (List.length modified) (String.concat ", " committed)
142 (String.concat ", " not_added)
143 (String.concat ", " not_committed) (String.concat ", " conflicts)))
145 (* XXX: at the moment, we don't keep track of the order in which users have
146 scheduled their commits, but we should, otherwise we will get a
147 "first come, random served" policy *)
148 "" (* (List.rev !to_be_committed) *) (MatitaAuthentication.get_users ())
151 (*** from matitaScript.ml ***)
152 (* let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$" *)
154 let eval_statement include_paths (* (buffer : GText.buffer) *) status (* script *)
157 let ast,unparsed_text =
160 (* if Pcre.pmatch ~rex:only_dust_RE text then raise Margin; *)
161 prerr_endline ("raw text = " ^ text);
163 GrafiteParser.parsable_statement status
164 (Ulexing.from_utf8_string text) in
165 prerr_endline "before get_ast";
166 let ast = MatitaEngine.get_ast status include_paths strm in
167 prerr_endline "after get_ast";
169 | `Ast (st, text) -> st, text
172 (* do we want to generate a trace? *)
174 not (List.mem_assoc "demod" a || List.mem_assoc "paramod" a ||
175 List.mem_assoc "fast_paramod" a || List.assoc "depth" a = "1" ||
179 let get_param a param =
181 Some (param ^ "=" ^ List.assoc param a)
182 with Not_found -> None
185 let floc = match ast with
186 | GrafiteAst.Executable (loc, _)
187 | GrafiteAst.Comment (loc, _) -> loc in
189 let lstart,lend = HExtlib.loc_of_floc floc in
190 let parsed_text, _parsed_text_len =
191 HExtlib.utf8_parsed_text unparsed_text (HExtlib.floc_of_loc (0,lend)) in
192 let parsed_text_len = utf8_length parsed_text in
193 let byte_parsed_text_len = String.length parsed_text in
195 String.sub unparsed_text byte_parsed_text_len
196 (String.length unparsed_text - byte_parsed_text_len)
198 prerr_endline (Printf.sprintf "ustring_sub caso 1: lstart=%d, parsed=%s" lstart parsed_text);
199 let pre = Netconversion.ustring_sub `Enc_utf8 0 lstart parsed_text in
203 Printf.sprintf "\005A href=\"%s\"\006%s\005/A\006"
204 (NReference.string_of_reference r) (NCicPp.r2s status true r)
206 (*if trace = [] then "{}"
207 else*) String.concat ", "
208 (HExtlib.filter_map (function
209 | NotationPt.NRef r -> Some (href r)
215 | GrafiteAst.Executable (_,
216 GrafiteAst.NTactic (_,
217 [GrafiteAst.NAuto (_, (l,a as auto_params))])) when is_auto auto_params
221 | Some (_,l') -> Some (List.map (fun x -> "",0,x) l')
223 let trace_ref = ref [] in
224 let status = NnAuto.auto_tac ~params:(l,a) ~trace_ref status in
225 let new_parsed_text = pre ^ (Printf.sprintf
226 "/\005span class='autotactic'\006%s\005span class='autotrace'\006 trace %s\005/span\006\005/span\006/"
228 (List.assoc "depth" a::
229 HExtlib.filter_map (get_param a) ["width";"size"]))
230 (mk_univ !trace_ref))
232 (status,new_parsed_text, unparsed_txt'),parsed_text_len
235 MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
237 let new_parsed_text = Ulexing.from_utf8_string parsed_text in
238 let interpr = GrafiteDisambiguate.get_interpr status#disambiguate_db in
239 let outstr = ref "" in
240 ignore (SmallLexer.mk_small_printer interpr outstr new_parsed_text);
241 prerr_endline ("baseuri after advance = " ^ status#baseuri);
242 (* prerr_endline ("parser output: " ^ !outstr); *)
243 (status,!outstr, unparsed_txt'),parsed_text_len
245 (*let save_moo status =
246 let script = MatitaScript.current () in
247 let baseuri = status#baseuri in
248 match script#bos, script#eos with
251 GrafiteTypes.Serializer.serialize ~baseuri:(NUri.uri_of_string baseuri)
253 | _ -> clean_current_baseuri status
256 let sequent_size = ref 40;;
258 let include_paths = ref [];;
261 * <meta number="...">
262 * <metaname>...</metaname>
268 let output_status s =
269 let _,_,metasenv,subst,_ = s#obj in
270 let render_switch = function
271 | Stack.Open i -> "?" ^ (string_of_int i)
272 | Stack.Closed i -> "<S>?" ^ (string_of_int i) ^ "</S>"
274 let int_of_switch = function
275 | Stack.Open i | Stack.Closed i -> i
277 let sequent = function
279 let meta = List.assoc i metasenv in
280 snd (ApplyTransformation.ntxt_of_cic_sequent
281 ~metasenv ~subst ~map_unicode_to_tex:false !sequent_size s (i,meta))
282 | Stack.Closed _ -> "This goal has already been closed."
284 let render_sequent is_loc acc depth tag (pos,sw) =
285 let metano = int_of_switch sw in
288 (match depth, pos with
289 | 0, 0 -> "<span class=\"activegoal\">" ^ (render_switch sw) ^ "</span>"
291 Printf.sprintf "<span class=\"activegoal\">|<SUB>%d</SUB>: %s</span>" pos (render_switch sw)
292 | 1, pos when Stack.head_tag s#stack = `BranchTag ->
293 Printf.sprintf "<span class=\"passivegoal\">|<SUB>%d</SUB> : %s</span>" pos (render_switch sw)
294 | _ -> render_switch sw)
295 else render_switch sw
298 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () markup in
299 let markup = "<metaname>" ^ markup ^ "</metaname>" in
301 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () (sequent sw)
303 let txt0 = "<goal>" ^ sequent ^ "</goal>" in
304 "<meta number=\"" ^ (string_of_int metano) ^ "\">" ^ markup ^
305 txt0 ^ "</meta>" ^ acc
309 ~env:(render_sequent true) ~cont:(render_sequent false)
310 ~todo:(render_sequent false) "" s#stack) ^
312 (* prerr_endline ("sending metasenv:\n" ^ res); res *)
315 let heading_nl_RE = Pcre.regexp "^\\s*\n\\s*";;
318 let s = Pcre.replace ~rex:heading_nl_RE s in
320 let nl_pos = String.index s '\n' in
321 String.sub s 0 nl_pos
325 let read_file fname =
326 let chan = open_in fname in
327 let lines = ref [] in
330 lines := input_line chan :: !lines
332 with End_of_file -> close_in chan);
333 String.concat "\n" (List.rev !lines)
336 let load_index outchan =
337 let s = read_file "index.html" in
338 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
341 let load_doc filename outchan =
342 let s = read_file filename in
344 try String.sub filename (String.length filename - 4) 4 = ".png"
345 with Invalid_argument _ -> false
347 let contenttype = if is_png then "image/png" else "text/html" in
348 Http_daemon.respond ~headers:["Content-Type", contenttype] ~code:(`Code 200) ~body:s outchan
351 let retrieve (cgi : Netcgi.cgi_activation) =
352 let env = cgi#environment in
354 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
355 let sid = HExtlib.unopt sid in
356 let uid = MatitaAuthentication.user_of_session sid in
360 ~content_type:"text/xml; charset=\"utf-8\""
363 let filename = libdir uid ^ "/" ^ (cgi # argument_value "file") in
364 (* prerr_endline ("reading file " ^ filename); *)
366 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
367 (html_of_matita (read_file filename)) in
369 (* html_of_matita (read_file filename) in *)
370 (* prerr_endline ("sending:\nBEGIN\n" ^ body ^ "\nEND"); *)
371 let body = "<response><file>" ^ body ^ "</file></response>" in
372 let baseuri, incpaths =
374 let root, baseuri, _fname, _tgt =
375 Librarian.baseuri_of_script ~include_paths:[] filename in
378 Str.split (Str.regexp " ")
379 (List.assoc "include_paths" (Librarian.load_root_file (root^"/root")))
382 let rc = root :: includes in
383 List.iter (HLog.debug) rc; baseuri, rc
385 Librarian.NoRootFor _ | Librarian.FileNotFound _ -> "",[] in
386 include_paths := incpaths;
387 let status = (MatitaAuthentication.get_status sid)#set_baseuri baseuri in
388 let history = [status] in
389 MatitaAuthentication.set_status sid status;
390 MatitaAuthentication.set_history sid history;
393 ~content_type:"text/xml; charset=\"utf-8\""
395 cgi#out_channel#output_string body;
399 ~status:`Internal_server_error
401 ~content_type:"text/html; charset=\"utf-8\""
403 cgi#out_channel#commit_work()
406 let xml_of_disamb_error l =
407 let mk_alias = function
408 | GrafiteAst.Ident_alias (_,uri) -> "href=\"" ^ uri ^ "\""
409 | GrafiteAst.Symbol_alias (_,uri,desc)
410 | GrafiteAst.Number_alias (uri,desc) ->
411 let uri = try HExtlib.unopt uri with _ -> "cic:/fakeuri.def(1)" in
412 "href=\"" ^ uri ^ "\" title=\"" ^
413 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
417 let mk_interpr (loc,a) =
418 let x,y = HExtlib.loc_of_floc loc in
419 Printf.sprintf "<interpretation start=\"%d\" stop=\"%d\" %s />"
423 let mk_failure (il,loc,msg) =
424 let x,y = HExtlib.loc_of_floc loc in
425 Printf.sprintf "<failure start=\"%d\" stop=\"%d\" title=\"%s\">%s</failure>"
426 x y (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () msg)
427 (String.concat "" (List.map mk_interpr il))
430 let mk_choice (a,fl) =
431 let fl' = String.concat "" (List.map mk_failure fl) in
433 | None -> "<choice>" ^ fl' ^ "</choice>"
434 | Some a -> Printf.sprintf "<choice %s>%s</choice>" (mk_alias a) fl'
437 let mk_located (loc,cl) =
438 let x,y = HExtlib.loc_of_floc loc in
439 Printf.sprintf "<choicepoint start=\"%d\" stop=\"%d\">%s</choicepoint>"
440 x y (String.concat "" (List.map mk_choice cl))
442 "<disamberror>" ^ (String.concat "" (List.map mk_located l)) ^ "</disamberror>"
445 let advance0 sid text =
446 let status = MatitaAuthentication.get_status sid in
447 let history = MatitaAuthentication.get_history sid in
448 let status = status#reset_disambiguate_db () in
449 let (st,new_statements,new_unparsed),parsed_len =
450 let rec do_exc = function
451 | HExtlib.Localized (floc,e) ->
452 let x, y = HExtlib.loc_of_floc floc in
453 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
454 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
455 let post = Netconversion.ustring_sub `Enc_utf8 y
456 (Netconversion.ustring_length `Enc_utf8 text - y) text in
457 let _,title = MatitaExcPp.to_string e in
458 (* let title = "" in *)
460 pre ^ "\005span class=\"error\" title=\"" ^ title ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
461 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
462 () (html_of_matita marked) in
463 raise (Emphasized_error marked)
464 | Disambiguate.NoWellTypedInterpretation (floc,e) ->
465 let x, y = HExtlib.loc_of_floc floc in
466 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
467 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
468 let post = Netconversion.ustring_sub `Enc_utf8 y
469 (Netconversion.ustring_length `Enc_utf8 text - y) text in
470 (*let _,title = MatitaExcPp.to_string e in*)
471 (* let title = "" in *)
473 pre ^ "\005span class=\"error\" title=\"" ^ e ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
474 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
475 () (html_of_matita marked) in
476 raise (Emphasized_error marked)
477 | NCicRefiner.Uncertain m as exn ->
478 let floc, e = Lazy.force m in
479 let x, y = HExtlib.loc_of_floc floc in
480 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
481 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
482 let post = Netconversion.ustring_sub `Enc_utf8 y
483 (Netconversion.ustring_length `Enc_utf8 text - y) text in
484 (* let _,title = MatitaExcPp.to_string e in *)
485 (* let title = "" in *)
487 pre ^ "\005span class=\"error\" title=\"" ^ e ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
488 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
489 () (html_of_matita marked) in
490 raise (Emphasized_error marked)
491 | NTacStatus.Error (s,None) as e ->
493 ("NTacStatus.Error " ^ (Lazy.force s)); raise e
494 | NTacStatus.Error (s,Some exc) as e ->
496 ("NTacStatus.Error " ^ Lazy.force s ^ " -- " ^ (Printexc.to_string exc));
498 | GrafiteDisambiguate.Ambiguous_input (loc,choices) ->
499 let x,y = HExtlib.loc_of_floc loc in
500 let choice_of_alias = function
501 | GrafiteAst.Ident_alias (_,uri) -> uri, None, uri
502 | GrafiteAst.Number_alias (None,desc)
503 | GrafiteAst.Symbol_alias (_,None,desc) -> "cic:/fakeuri.def(1)", Some desc, desc
504 | GrafiteAst.Number_alias (Some uri,desc)
505 | GrafiteAst.Symbol_alias (_,Some uri,desc) -> uri, Some desc, desc
507 let tag_of_choice (uri,title,desc) =
509 | None -> Printf.sprintf "<choice href=\"%s\">%s</choice>"
511 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
512 | Some t -> Printf.sprintf "<choice href=\"%s\" title=\"%s\">%s</choice>"
514 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () t)
515 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
519 (List.map (fun x -> tag_of_choice (choice_of_alias x)) choices)
521 prerr_endline (Printf.sprintf
522 "@@@ Ambiguous input at (%d,%d). Possible choices:\n\n%s\n\n@@@ End."
525 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
526 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
527 let post = Netconversion.ustring_sub `Enc_utf8 y
528 (Netconversion.ustring_length `Enc_utf8 text - y) text in
529 let title = "Disambiguation Error" in
530 (* let title = "" in *)
532 pre ^ "\005span class=\"error\" title=\"" ^ title ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
533 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
534 () (html_of_matita marked) in
536 let strchoices = Printf.sprintf
537 "<ambiguity start=\"%d\" stop=\"%d\">%s</ambiguity>" x y strchoices
538 in raise (Disamb_error strchoices)
539 | GrafiteDisambiguate.Error l -> raise (Disamb_error (xml_of_disamb_error l))
540 (* | End_of_file -> ... *)
545 eval_statement !include_paths (*buffer*) status (`Raw text)
548 MatitaAuthentication.set_status sid st;
549 MatitaAuthentication.set_history sid (st::history);
550 (* prerr_endline "previous timestamp";
551 status#print_timestamp();
552 prerr_endline "current timestamp";
553 st#print_timestamp(); *)
555 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
556 () (html_of_matita new_statements), new_unparsed, st
558 let register (cgi : Netcgi.cgi_activation) =
559 let _env = cgi#environment in
561 assert (cgi#arguments <> []);
562 let uid = cgi#argument_value "userid" in
563 let userpw = cgi#argument_value "password" in
565 (* currently registering only unprivileged users *)
566 MatitaAuthentication.add_user uid userpw false;
567 (* env#set_output_header_field "Location" "/index.html" *)
568 cgi#out_channel#output_string
569 ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/login.html\">"
570 ^ "</head><body>Redirecting to login page...</body></html>")
572 | MatitaAuthentication.UsernameCollision _ ->
575 ~content_type:"text/html; charset=\"utf-8\""
577 cgi#out_channel#output_string
578 "<html><head></head><body>Error: User id collision!</body></html>"
579 | MatitaFilesystem.SvnError msg ->
582 ~content_type:"text/html; charset=\"utf-8\""
584 cgi#out_channel#output_string
585 ("<html><head></head><body><p>Error: Svn checkout failed!<p><p><textarea>"
586 ^ msg ^ "</textarea></p></body></html>"));
587 cgi#out_channel#commit_work()
590 let login (cgi : Netcgi.cgi_activation) =
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 MatitaAuthentication.check_pw uid userpw;
598 NCicLibrary.init (Some uid);
599 let ft = MatitaAuthentication.read_ft uid in
600 let _ = MatitaFilesystem.html_of_library uid ft in
601 let sid = MatitaAuthentication.create_session uid in
602 (* let cookie = Netcgi.Cookie.make "session" (Uuidm.to_string sid) in
603 cgi#set_header ~set_cookies:[cookie] (); *)
604 env#set_output_header_field
605 "Set-Cookie" ("session=" ^ (Uuidm.to_string sid));
606 (* env#set_output_header_field "Location" "/index.html" *)
607 cgi#out_channel#output_string
608 ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/index.html\">"
609 ^ "</head><body>Redirecting to Matita page...</body></html>")
610 with MatitaAuthentication.InvalidPassword ->
613 ~content_type:"text/html; charset=\"utf-8\""
615 cgi#out_channel#output_string
616 "<html><head></head><body>Authentication error</body></html>");
617 cgi#out_channel#commit_work()
620 let logout (cgi : Netcgi.cgi_activation) =
621 let env = cgi#environment in
623 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
624 let sid = HExtlib.unopt sid in
625 MatitaAuthentication.logout_user sid;
628 ~content_type:"text/html; charset=\"utf-8\""
630 let text = read_file (rt_path () ^ "/logout.html") in
631 cgi#out_channel#output_string text
635 ~status:`Internal_server_error
637 ~content_type:"text/html; charset=\"utf-8\""
639 cgi#out_channel#commit_work()
642 exception File_already_exists;;
644 let save (cgi : Netcgi.cgi_activation) =
645 let env = cgi#environment in
647 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
648 let sid = HExtlib.unopt sid in
649 let status = MatitaAuthentication.get_status sid in
650 let uid = MatitaAuthentication.user_of_session sid in
651 assert (cgi#arguments <> []);
652 let locked = cgi#argument_value "locked" in
653 let unlocked = cgi#argument_value "unlocked" in
654 let dir = cgi#argument_value "dir" in
655 let rel_filename = cgi # argument_value "file" in
656 let filename = libdir uid ^ "/" ^ rel_filename in
657 let force = bool_of_string (cgi#argument_value "force") in
658 let already_exists = Sys.file_exists filename in
660 if ((not force) && already_exists) then
661 raise File_already_exists;
664 Unix.mkdir filename 0o744
667 let oc = open_out filename in
668 output_string oc (locked ^ unlocked);
670 if MatitaEngine.eos status unlocked then
672 (* prerr_endline ("serializing proof objects..."); *)
673 GrafiteTypes.Serializer.serialize
674 ~baseuri:(NUri.uri_of_string status#baseuri) status;
675 (* prerr_endline ("done."); *)
680 List.assoc rel_filename (MatitaAuthentication.read_ft uid)
681 with Not_found -> MatitaFilesystem.MUnversioned
683 (if old_flag <> MatitaFilesystem.MConflict &&
684 old_flag <> MatitaFilesystem.MAdd then
686 if already_exists then MatitaFilesystem.MModified
687 else MatitaFilesystem.MAdd
689 MatitaAuthentication.set_file_flag uid [rel_filename, Some newflag]);
692 ~content_type:"text/xml; charset=\"utf-8\""
694 cgi#out_channel#output_string "<response>ok</response>"
696 | File_already_exists ->
697 cgi#out_channel#output_string "<response>cancelled</response>"
700 ~status:`Internal_server_error
702 ~content_type:"text/xml; charset=\"utf-8\""
705 let estr = Printexc.to_string e in
706 cgi#out_channel#output_string ("<response>" ^ estr ^ "</response>"));
707 cgi#out_channel#commit_work()
710 let initiate_commit (cgi : Netcgi.cgi_activation) =
711 let env = cgi#environment in
713 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
714 let sid = HExtlib.unopt sid in
715 MatitaAuthentication.probe_commit_priv sid;
716 let out = do_global_commit () in
719 ~content_type:"text/xml; charset=\"utf-8\""
721 cgi#out_channel#output_string "<commit>";
722 cgi#out_channel#output_string "<response>ok</response>";
723 cgi#out_channel#output_string ("<details>" ^ out ^ "</details>");
724 cgi#out_channel#output_string "</commit>"
728 ~status:`Internal_server_error
730 ~content_type:"text/xml; charset=\"utf-8\""
732 cgi#out_channel#commit_work()
735 let svn_update (cgi : Netcgi.cgi_activation) =
736 let env = cgi#environment in
737 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
738 let sid = HExtlib.unopt sid in
739 let uid = MatitaAuthentication.user_of_session sid in
741 MatitaAuthentication.probe_commit_priv sid;
742 let files,anomalies,(added,conflict,del,upd,merged) =
743 MatitaFilesystem.update_user uid
745 let anomalies = String.concat "\n" anomalies in
746 let details = Printf.sprintf
748 "%d deleted files\n"^^
749 "%d updated files\n"^^
750 "%d merged files\n"^^
751 "%d conflicting files\n\n" ^^
752 "Anomalies:\n%s") added del upd merged conflict anomalies
754 prerr_endline ("update details:\n" ^ details);
756 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () details
758 MatitaAuthentication.set_file_flag uid files;
761 ~content_type:"text/xml; charset=\"utf-8\""
763 cgi#out_channel#output_string "<update>";
764 cgi#out_channel#output_string "<response>ok</response>";
765 cgi#out_channel#output_string ("<details>" ^ details ^ "</details>");
766 cgi#out_channel#output_string "</update>";
770 ~status:`Internal_server_error
772 ~content_type:"text/xml; charset=\"utf-8\""
774 cgi#out_channel#commit_work()
777 (* returns the length of the executed text and an html representation of the
780 let advance (cgi : Netcgi.cgi_activation) =
781 (* let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in *)
782 let env = cgi#environment in
784 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
785 let sid = HExtlib.unopt sid in
789 ~content_type:"text/xml; charset=\"utf-8\""
792 let text = cgi#argument_value "body" in
793 (* prerr_endline ("body =\n" ^ text); *)
794 let parsed_len, new_parsed, new_unparsed, new_status = advance0 sid text in
795 let txt = output_status new_status in
797 "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\">" ^
798 new_parsed ^ "</parsed>" ^ txt
801 (* prerr_endline ("sending advance response:\n" ^ body); *)
804 ~content_type:"text/xml; charset=\"utf-8\""
806 cgi#out_channel#output_string body
808 | Emphasized_error text ->
809 (* | MultiPassDisambiguator.DisambiguationError (offset,errorll) -> *)
810 let body = "<response><error>" ^ text ^ "</error></response>" in
813 ~content_type:"text/xml; charset=\"utf-8\""
815 cgi#out_channel#output_string body
816 | Disamb_error text ->
817 let body = "<response>" ^ text ^ "</response>" in
820 ~content_type:"text/xml; charset=\"utf-8\""
822 cgi#out_channel#output_string body
825 ~status:`Internal_server_error
827 ~content_type:"text/xml; charset=\"utf-8\""
830 cgi#out_channel#commit_work()
833 let gotoBottom (cgi : Netcgi.cgi_activation) =
834 let env = cgi#environment in
836 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
837 let sid = HExtlib.unopt sid in
839 let error_msg = function
840 | Emphasized_error text -> "<localized>" ^ text ^ "</localized>"
841 | Disamb_error text -> text
842 | End_of_file _ -> (* not an error *) ""
843 | e -> (* unmanaged error *)
845 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
846 (Printexc.to_string e)) ^ "</error>"
849 let rec aux acc text =
851 prerr_endline ("evaluating: " ^ first_line text);
852 let plen,new_parsed,new_unparsed,_new_status = advance0 sid text in
853 aux ((plen,new_parsed)::acc) new_unparsed
854 with e -> acc, error_msg e
855 (* DON'T SERIALIZE NOW!!!
856 let status = MatitaAuthentication.get_status sid in
857 GrafiteTypes.Serializer.serialize
858 ~baseuri:(NUri.uri_of_string status#baseuri) status;
864 ~content_type:"text/xml; charset=\"utf-8\""
867 let text = cgi#argument_value "body" in
868 (* prerr_endline ("body =\n" ^ text); *)
869 let len_parsedlist, err_msg = aux [] text in
870 let status = MatitaAuthentication.get_status sid in
871 let txt = output_status status in
872 let parsed_tag (len,txt) =
873 "<parsed length=\"" ^ (string_of_int len) ^ "\">" ^ txt ^ "</parsed>"
875 (* List.rev: the list begins with the older parsed txt *)
878 String.concat "" (List.rev (List.map parsed_tag len_parsedlist)) ^
879 txt ^ err_msg ^ "</response>"
881 (* prerr_endline ("sending goto bottom response:\n" ^ body); *)
884 ~content_type:"text/xml; charset=\"utf-8\""
886 cgi#out_channel#output_string body;
887 (* with Not_found -> cgi#set_header ~status:`Internal_server_error
889 ~content_type:"text/xml; charset=\"utf-8\"" ()); *)
890 cgi#out_channel#commit_work()
893 let gotoTop (cgi : Netcgi.cgi_activation) =
894 let env = cgi#environment in
895 prerr_endline "executing goto Top";
897 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
898 let sid = HExtlib.unopt sid in
902 ~content_type:"text/xml; charset=\"utf-8\""
905 let status = MatitaAuthentication.get_status sid in
906 let uid = MatitaAuthentication.user_of_session sid in
907 let baseuri = status#baseuri in
908 let new_status = new MatitaEngine.status (Some uid) baseuri in
909 prerr_endline "gototop prima della time travel";
910 (* NCicLibrary.time_travel new_status; *)
911 prerr_endline "gototop dopo della time travel";
912 let new_history = [new_status] in
913 MatitaAuthentication.set_history sid new_history;
914 MatitaAuthentication.set_status sid new_status;
915 (* NCicLibrary.time_travel new_status; *)
918 ~content_type:"text/xml; charset=\"utf-8\""
920 cgi#out_channel#output_string "<response>ok</response>"
922 (cgi#set_header ~status:`Internal_server_error
924 ~content_type:"text/xml; charset=\"utf-8\"" ();
925 cgi#out_channel#output_string "<response>ok</response>"));
926 cgi#out_channel#commit_work()
929 let retract (cgi : Netcgi.cgi_activation) =
930 let env = cgi#environment in
932 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
933 let sid = HExtlib.unopt sid in
937 ~content_type:"text/xml; charset=\"utf-8\""
940 let history = MatitaAuthentication.get_history sid in
941 let old_status = MatitaAuthentication.get_status sid in
942 let new_history,new_status =
944 _::(status::_ as history) ->
946 | [_] -> (prerr_endline "singleton";failwith "retract")
947 | _ -> (prerr_endline "nil"; assert false) in
948 (* prerr_endline "timestamp prima della retract";
949 old_status#print_timestamp ();
950 prerr_endline "timestamp della retract";
951 new_status#print_timestamp ();
952 prerr_endline ("prima della time travel"); *)
953 NCicLibrary.time_travel new_status;
954 prerr_endline ("dopo della time travel");
955 MatitaAuthentication.set_history sid new_history;
956 MatitaAuthentication.set_status sid new_status;
957 prerr_endline ("baseuri after retract = " ^ new_status#baseuri);
958 let body = output_status new_status in
961 ~content_type:"text/xml; charset=\"utf-8\""
963 cgi#out_channel#output_string body
965 prerr_endline ("error in retract: " ^ Printexc.to_string e);
966 cgi#set_header ~status:`Internal_server_error
968 ~content_type:"text/xml; charset=\"utf-8\"" ());
969 cgi#out_channel#commit_work()
973 let viewLib (cgi : Netcgi.cgi_activation) =
974 let env = cgi#environment in
976 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
977 let sid = HExtlib.unopt sid in
981 ~content_type:"text/html; charset=\"utf-8\""
984 let uid = MatitaAuthentication.user_of_session sid in
986 let ft = MatitaAuthentication.read_ft uid in
987 let html = MatitaFilesystem.html_of_library uid ft in
990 ~content_type:"text/html; charset=\"utf-8\""
992 cgi#out_channel#output_string
995 "<title>XML Tree Control</title>\n" ^
996 "<link href=\"treeview/xmlTree.css\" type=\"text/css\" rel=\"stylesheet\">\n" ^
997 "<script src=\"treeview/xmlTree.js\" type=\"text/javascript\"></script>\n" ^
999 html (* ^ "\n</body></html>" *) );
1001 let files,anomalies = MatitaFilesystem.stat_user uid in
1002 let changed = HExtlib.filter_map
1003 (fun (n,f) -> if (f = Some MatitaFilesystem.MModified) then Some n else None) files
1005 let changed = String.concat "\n" changed in
1006 let anomalies = String.concat "\n" anomalies in
1007 prerr_endline ("Changed:\n" ^ changed ^ "\n\nAnomalies:\n" ^ anomalies);
1008 cgi#out_channel#commit_work()
1012 let resetLib (cgi : Netcgi.cgi_activation) =
1013 MatitaAuthentication.reset ();
1016 ~content_type:"text/html; charset=\"utf-8\""
1019 cgi#out_channel#output_string
1021 "<title>Matitaweb Reset</title>\n" ^
1022 "<body><H1>Reset completed</H1></body></html>");
1023 cgi#out_channel#commit_work()
1025 open Netcgi1_compat.Netcgi_types;;
1027 (**********************************************************************)
1028 (* Create the webserver *)
1029 (**********************************************************************)
1033 let (opt_list, cmdline_cfg) = Netplex_main.args() in
1035 let use_mt = ref true in
1038 [ "-mt", Arg.Set use_mt,
1039 " Use multi-threading instead of multi-processing"
1044 (fun s -> raise (Arg.Bad ("Don't know what to do with: " ^ s)))
1045 "usage: netplex [options]";
1048 Netplex_mt.mt() (* multi-threading *)
1050 Netplex_mp.mp() in (* multi-processing *)
1053 { Nethttpd_services.dyn_handler = (fun _ -> process1);
1054 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1055 dyn_uri = None; (* not needed *)
1056 dyn_translator = (fun _ -> ""); (* not needed *)
1057 dyn_accept_all_conditionals = false;
1061 { Nethttpd_services.dyn_handler = (fun _ -> advance);
1062 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1063 dyn_uri = None; (* not needed *)
1064 dyn_translator = (fun _ -> ""); (* not needed *)
1065 dyn_accept_all_conditionals = false;
1068 { Nethttpd_services.dyn_handler = (fun _ -> retract);
1069 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1070 dyn_uri = None; (* not needed *)
1071 dyn_translator = (fun _ -> ""); (* not needed *)
1072 dyn_accept_all_conditionals = false;
1075 { Nethttpd_services.dyn_handler = (fun _ -> gotoBottom);
1076 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1077 dyn_uri = None; (* not needed *)
1078 dyn_translator = (fun _ -> ""); (* not needed *)
1079 dyn_accept_all_conditionals = false;
1082 { Nethttpd_services.dyn_handler = (fun _ -> gotoTop);
1083 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1084 dyn_uri = None; (* not needed *)
1085 dyn_translator = (fun _ -> ""); (* not needed *)
1086 dyn_accept_all_conditionals = false;
1089 { Nethttpd_services.dyn_handler = (fun _ -> retrieve);
1090 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1091 dyn_uri = None; (* not needed *)
1092 dyn_translator = (fun _ -> ""); (* not needed *)
1093 dyn_accept_all_conditionals = false;
1096 { Nethttpd_services.dyn_handler = (fun _ -> register);
1097 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1098 dyn_uri = None; (* not needed *)
1099 dyn_translator = (fun _ -> ""); (* not needed *)
1100 dyn_accept_all_conditionals = false;
1103 { Nethttpd_services.dyn_handler = (fun _ -> login);
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 _ -> logout);
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 _ -> viewLib);
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 _ -> resetLib);
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 _ -> save);
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 _ -> initiate_commit);
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 _ -> svn_update);
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;
1153 let nethttpd_factory =
1154 Nethttpd_plex.nethttpd_factory
1155 ~handlers:[ "advance", do_advance
1156 ; "retract", do_retract
1157 ; "bottom", goto_bottom
1160 ; "register", do_register
1162 ; "logout", do_logout
1163 ; "reset", do_resetlib
1164 ; "viewlib", do_viewlib
1166 ; "commit", do_commit
1167 ; "update", do_update]
1169 MatitaInit.initialize_all ();
1170 MatitaAuthentication.deserialize ();
1171 Netplex_main.startup
1173 Netplex_log.logger_factories (* allow all built-in logging styles *)
1174 Netplex_workload.workload_manager_factories (* ... all ways of workload management *)
1175 [ nethttpd_factory ] (* make this nethttpd available *)
1179 Sys.set_signal Sys.sigpipe Sys.Signal_ignore;