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 (* () *) uid =
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) *)
150 (* replace [uid] to commit all users:
151 (MatitaAuthentication.get_users ())
156 (*** from matitaScript.ml ***)
157 (* let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$" *)
159 let eval_statement include_paths (* (buffer : GText.buffer) *) status (* script *)
162 let ast,unparsed_text =
165 (* if Pcre.pmatch ~rex:only_dust_RE text then raise Margin; *)
166 prerr_endline ("raw text = " ^ text);
168 GrafiteParser.parsable_statement status
169 (Ulexing.from_utf8_string text) in
170 prerr_endline "before get_ast";
171 let ast = MatitaEngine.get_ast status include_paths strm in
172 prerr_endline "after get_ast";
174 | `Ast (st, text) -> st, text
177 (* do we want to generate a trace? *)
179 not (List.mem_assoc "demod" a || List.mem_assoc "paramod" a ||
180 List.mem_assoc "fast_paramod" a || List.assoc "depth" a = "1" ||
184 let get_param a param =
186 Some (param ^ "=" ^ List.assoc param a)
187 with Not_found -> None
190 let floc = match ast with
191 | GrafiteAst.Executable (loc, _)
192 | GrafiteAst.Comment (loc, _) -> loc in
194 let lstart,lend = HExtlib.loc_of_floc floc in
195 let parsed_text, _parsed_text_len =
196 HExtlib.utf8_parsed_text unparsed_text (HExtlib.floc_of_loc (0,lend)) in
197 let parsed_text_len = utf8_length parsed_text in
198 let byte_parsed_text_len = String.length parsed_text in
200 String.sub unparsed_text byte_parsed_text_len
201 (String.length unparsed_text - byte_parsed_text_len)
203 prerr_endline (Printf.sprintf "ustring_sub caso 1: lstart=%d, parsed=%s" lstart parsed_text);
204 let pre = Netconversion.ustring_sub `Enc_utf8 0 lstart parsed_text in
208 Printf.sprintf "\005A href=\"%s\"\006%s\005/A\006"
209 (NReference.string_of_reference r) (NCicPp.r2s status true r)
211 (*if trace = [] then "{}"
212 else*) String.concat ", "
213 (HExtlib.filter_map (function
214 | NotationPt.NRef r -> Some (href r)
220 | GrafiteAst.Executable (_,
221 GrafiteAst.NCommand (_,
222 GrafiteAst.NObj (loc, astobj,_))) ->
223 let objname = NotationPt.name_of_obj astobj in
225 MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
227 let new_parsed_text = Ulexing.from_utf8_string parsed_text in
228 let interpr = GrafiteDisambiguate.get_interpr status#disambiguate_db in
229 let outstr = ref "" in
230 ignore (SmallLexer.mk_small_printer interpr outstr new_parsed_text);
231 let x, y = HExtlib.loc_of_floc floc in
232 let pre = Netconversion.ustring_sub `Enc_utf8 0 x !outstr in
233 let post = Netconversion.ustring_sub `Enc_utf8 x
234 (Netconversion.ustring_length `Enc_utf8 !outstr - x) !outstr in
235 outstr := Printf.sprintf
236 "%s\005img class=\"anchor\" src=\"icons/tick.png\" id=\"%s\" /\006%s" pre objname post;
237 prerr_endline ("baseuri after advance = " ^ status#baseuri);
238 (* prerr_endline ("parser output: " ^ !outstr); *)
239 (status,!outstr, unparsed_txt'),parsed_text_len
240 | GrafiteAst.Executable (_,
241 GrafiteAst.NTactic (_,
242 [GrafiteAst.NAuto (_, (l,a as auto_params))])) when is_auto auto_params
246 | Some (_,l') -> Some (List.map (fun x -> "",0,x) l')
248 let trace_ref = ref [] in
249 let status = NnAuto.auto_tac ~params:(l,a) ~trace_ref status in
250 let new_parsed_text = pre ^ (Printf.sprintf
251 "/\005span class='autotactic'\006%s\005span class='autotrace'\006 trace %s\005/span\006\005/span\006/"
253 (List.assoc "depth" a::
254 HExtlib.filter_map (get_param a) ["width";"size"]))
255 (mk_univ !trace_ref))
257 (status,new_parsed_text, unparsed_txt'),parsed_text_len
260 MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
262 let new_parsed_text = Ulexing.from_utf8_string parsed_text in
263 let interpr = GrafiteDisambiguate.get_interpr status#disambiguate_db in
264 let outstr = ref "" in
265 ignore (SmallLexer.mk_small_printer interpr outstr new_parsed_text);
266 prerr_endline ("baseuri after advance = " ^ status#baseuri);
267 (* prerr_endline ("parser output: " ^ !outstr); *)
268 (status,!outstr, unparsed_txt'),parsed_text_len
270 (*let save_moo status =
271 let script = MatitaScript.current () in
272 let baseuri = status#baseuri in
273 match script#bos, script#eos with
276 GrafiteTypes.Serializer.serialize ~baseuri:(NUri.uri_of_string baseuri)
278 | _ -> clean_current_baseuri status
281 let sequent_size = ref 40;;
283 let include_paths = ref [];;
286 * <meta number="...">
287 * <metaname>...</metaname>
293 let output_status s =
294 let _,_,metasenv,subst,_ = s#obj in
295 let render_switch = function
296 | Stack.Open i -> "?" ^ (string_of_int i)
297 | Stack.Closed i -> "<S>?" ^ (string_of_int i) ^ "</S>"
299 let int_of_switch = function
300 | Stack.Open i | Stack.Closed i -> i
302 let sequent = function
304 let meta = List.assoc i metasenv in
305 snd (ApplyTransformation.ntxt_of_cic_sequent
306 ~metasenv ~subst ~map_unicode_to_tex:false !sequent_size s (i,meta))
307 | Stack.Closed _ -> "This goal has already been closed."
309 let render_sequent is_loc acc depth tag (pos,sw) =
310 let metano = int_of_switch sw in
313 (match depth, pos with
314 | 0, 0 -> "<span class=\"activegoal\">" ^ (render_switch sw) ^ "</span>"
316 Printf.sprintf "<span class=\"activegoal\">|<SUB>%d</SUB>: %s</span>" pos (render_switch sw)
317 | 1, pos when Stack.head_tag s#stack = `BranchTag ->
318 Printf.sprintf "<span class=\"passivegoal\">|<SUB>%d</SUB> : %s</span>" pos (render_switch sw)
319 | _ -> render_switch sw)
320 else render_switch sw
323 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () markup in
324 let markup = "<metaname>" ^ markup ^ "</metaname>" in
326 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () (sequent sw)
328 let txt0 = "<goal>" ^ sequent ^ "</goal>" in
329 "<meta number=\"" ^ (string_of_int metano) ^ "\">" ^ markup ^
330 txt0 ^ "</meta>" ^ acc
334 ~env:(render_sequent true) ~cont:(render_sequent false)
335 ~todo:(render_sequent false) "" s#stack) ^
337 (* prerr_endline ("sending metasenv:\n" ^ res); res *)
340 let heading_nl_RE = Pcre.regexp "^\\s*\n\\s*";;
343 let s = Pcre.replace ~rex:heading_nl_RE s in
345 let nl_pos = String.index s '\n' in
346 String.sub s 0 nl_pos
350 let read_file fname =
351 let chan = open_in fname in
352 let lines = ref [] in
355 lines := input_line chan :: !lines
357 with End_of_file -> close_in chan);
358 String.concat "\n" (List.rev !lines)
361 let load_index outchan =
362 let s = read_file "index.html" in
363 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
366 let load_doc filename outchan =
367 let s = read_file filename in
369 try String.sub filename (String.length filename - 4) 4 = ".png"
370 with Invalid_argument _ -> false
372 let contenttype = if is_png then "image/png" else "text/html" in
373 Http_daemon.respond ~headers:["Content-Type", contenttype] ~code:(`Code 200) ~body:s outchan
376 let retrieve (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
377 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
378 let env = cgi#environment in
380 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
381 let sid = HExtlib.unopt sid in
382 let uid = MatitaAuthentication.user_of_session sid in
386 ~content_type:"text/xml; charset=\"utf-8\""
389 let readonly = cgi # argument_value "readonly" in
390 let filename = libdir uid ^ "/" ^ (cgi # argument_value "file") in
391 (* prerr_endline ("reading file " ^ filename); *)
393 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
394 (html_of_matita (read_file filename)) in
396 (* html_of_matita (read_file filename) in *)
397 (* prerr_endline ("sending:\nBEGIN\n" ^ body ^ "\nEND"); *)
398 let body = "<response><file>" ^ body ^ "</file></response>" in
399 let baseuri, incpaths =
401 let root, baseuri, _fname, _tgt =
402 Librarian.baseuri_of_script ~include_paths:[] filename in
405 Str.split (Str.regexp " ")
406 (List.assoc "include_paths" (Librarian.load_root_file (root^"/root")))
409 let rc = root :: includes in
410 List.iter (HLog.debug) rc; baseuri, rc
412 Librarian.NoRootFor _ | Librarian.FileNotFound _ -> "",[] in
413 include_paths := incpaths;
414 if readonly <> "true" then
415 (let status = new MatitaEngine.status (Some uid) baseuri in
416 let history = [status] in
417 MatitaAuthentication.set_status sid status;
418 MatitaAuthentication.set_history sid history);
421 ~content_type:"text/xml; charset=\"utf-8\""
423 cgi#out_channel#output_string body;
428 ~content_type:"text/xml; charset=\"utf-8\""
430 cgi#out_channel#output_string "<error />"
433 ~status:`Internal_server_error
435 ~content_type:"text/html; charset=\"utf-8\""
437 cgi#out_channel#commit_work()
440 let xml_of_disamb_error l =
441 let mk_alias = function
442 | GrafiteAst.Ident_alias (_,uri) -> "href=\"" ^ uri ^ "\""
443 | GrafiteAst.Symbol_alias (_,uri,desc)
444 | GrafiteAst.Number_alias (uri,desc) ->
445 let uri = try HExtlib.unopt uri with _ -> "cic:/fakeuri.def(1)" in
446 "href=\"" ^ uri ^ "\" title=\"" ^
447 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
451 let mk_interpr (loc,a) =
452 let x,y = HExtlib.loc_of_floc loc in
453 Printf.sprintf "<interpretation start=\"%d\" stop=\"%d\" %s />"
457 let mk_failure (il,loc,msg) =
458 let x,y = HExtlib.loc_of_floc loc in
459 Printf.sprintf "<failure start=\"%d\" stop=\"%d\" title=\"%s\">%s</failure>"
460 x y (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () msg)
461 (String.concat "" (List.map mk_interpr il))
464 let mk_choice (a,fl) =
465 let fl' = String.concat "" (List.map mk_failure fl) in
467 | None -> "<choice>" ^ fl' ^ "</choice>"
468 | Some a -> Printf.sprintf "<choice %s>%s</choice>" (mk_alias a) fl'
471 let mk_located (loc,cl) =
472 let x,y = HExtlib.loc_of_floc loc in
473 Printf.sprintf "<choicepoint start=\"%d\" stop=\"%d\">%s</choicepoint>"
474 x y (String.concat "" (List.map mk_choice cl))
476 "<disamberror>" ^ (String.concat "" (List.map mk_located l)) ^ "</disamberror>"
479 let advance0 sid text =
480 let status = MatitaAuthentication.get_status sid in
481 let history = MatitaAuthentication.get_history sid in
482 let status = status#reset_disambiguate_db () in
483 let (st,new_statements,new_unparsed),parsed_len =
484 let rec do_exc = function
485 | HExtlib.Localized (floc,e) ->
486 let x, y = HExtlib.loc_of_floc floc in
487 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
488 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
489 let post = Netconversion.ustring_sub `Enc_utf8 y
490 (Netconversion.ustring_length `Enc_utf8 text - y) text in
491 let _,title = MatitaExcPp.to_string e in
492 (* let title = "" in *)
494 pre ^ "\005span class=\"error\" title=\"" ^ title ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
495 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
496 () (html_of_matita marked) in
497 raise (Emphasized_error marked)
498 | Disambiguate.NoWellTypedInterpretation (floc,e) ->
499 let x, y = HExtlib.loc_of_floc floc in
500 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
501 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
502 let post = Netconversion.ustring_sub `Enc_utf8 y
503 (Netconversion.ustring_length `Enc_utf8 text - y) text in
504 (*let _,title = MatitaExcPp.to_string e in*)
505 (* let title = "" in *)
507 pre ^ "\005span class=\"error\" title=\"" ^ e ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
508 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
509 () (html_of_matita marked) in
510 raise (Emphasized_error marked)
511 | NCicRefiner.Uncertain m as exn ->
512 let floc, e = Lazy.force m in
513 let x, y = HExtlib.loc_of_floc floc in
514 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
515 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
516 let post = Netconversion.ustring_sub `Enc_utf8 y
517 (Netconversion.ustring_length `Enc_utf8 text - y) text in
518 (* let _,title = MatitaExcPp.to_string e in *)
519 (* let title = "" in *)
521 pre ^ "\005span class=\"error\" title=\"" ^ e ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
522 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
523 () (html_of_matita marked) in
524 raise (Emphasized_error marked)
525 | NTacStatus.Error (s,None) as e ->
527 ("NTacStatus.Error " ^ (Lazy.force s)); raise e
528 | NTacStatus.Error (s,Some exc) as e ->
530 ("NTacStatus.Error " ^ Lazy.force s ^ " -- " ^ (Printexc.to_string exc));
532 | GrafiteDisambiguate.Ambiguous_input (loc,choices) ->
533 let x,y = HExtlib.loc_of_floc loc in
534 let choice_of_alias = function
535 | GrafiteAst.Ident_alias (_,uri) -> uri, None, uri
536 | GrafiteAst.Number_alias (None,desc)
537 | GrafiteAst.Symbol_alias (_,None,desc) -> "cic:/fakeuri.def(1)", Some desc, desc
538 | GrafiteAst.Number_alias (Some uri,desc)
539 | GrafiteAst.Symbol_alias (_,Some uri,desc) -> uri, Some desc, desc
541 let tag_of_choice (uri,title,desc) =
543 | None -> Printf.sprintf "<choice href=\"%s\">%s</choice>"
545 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
546 | Some t -> Printf.sprintf "<choice href=\"%s\" title=\"%s\">%s</choice>"
548 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () t)
549 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
553 (List.map (fun x -> tag_of_choice (choice_of_alias x)) choices)
555 prerr_endline (Printf.sprintf
556 "@@@ Ambiguous input at (%d,%d). Possible choices:\n\n%s\n\n@@@ End."
559 let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
560 let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
561 let post = Netconversion.ustring_sub `Enc_utf8 y
562 (Netconversion.ustring_length `Enc_utf8 text - y) text in
563 let title = "Disambiguation Error" in
564 (* let title = "" in *)
566 pre ^ "\005span class=\"error\" title=\"" ^ title ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
567 let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
568 () (html_of_matita marked) in
570 let strchoices = Printf.sprintf
571 "<ambiguity start=\"%d\" stop=\"%d\">%s</ambiguity>" x y strchoices
572 in raise (Disamb_error strchoices)
573 | GrafiteDisambiguate.Error l -> raise (Disamb_error (xml_of_disamb_error l))
574 (* | End_of_file -> ... *)
576 prerr_endline ("matitadaemon *** Unhandled exception " ^ Printexc.to_string e);
581 eval_statement !include_paths (*buffer*) status (`Raw text)
584 debug "BEGIN PRINTGRAMMAR";
585 (*prerr_endline (Print_grammar.ebnf_of_term status);*)
586 (*let kwds = String.concat ", " status#get_kwds in
587 debug ("keywords = " ^ kwds );*)
588 debug "END PRINTGRAMMAR";
589 MatitaAuthentication.set_status sid st;
590 MatitaAuthentication.set_history sid (st::history);
591 (* prerr_endline "previous timestamp";
592 status#print_timestamp();
593 prerr_endline "current timestamp";
594 st#print_timestamp(); *)
596 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
597 () (html_of_matita new_statements), new_unparsed, st
599 let register (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
600 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
601 let _env = cgi#environment in
603 assert (cgi#arguments <> []);
604 let uid = cgi#argument_value "userid" in
605 let userpw = cgi#argument_value "password" in
607 (* currently registering only unprivileged users *)
608 MatitaAuthentication.add_user uid userpw false;
609 (* env#set_output_header_field "Location" "/index.html" *)
610 cgi#out_channel#output_string
611 ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/login.html\">"
612 ^ "</head><body>Redirecting to login page...</body></html>")
614 | MatitaAuthentication.UsernameCollision _ ->
617 ~content_type:"text/html; charset=\"utf-8\""
619 cgi#out_channel#output_string
620 "<html><head></head><body>Error: User id collision!</body></html>"
621 | MatitaFilesystem.SvnError msg ->
624 ~content_type:"text/html; charset=\"utf-8\""
626 cgi#out_channel#output_string
627 ("<html><head></head><body><p>Error: Svn checkout failed!<p><p><textarea>"
628 ^ msg ^ "</textarea></p></body></html>"));
629 cgi#out_channel#commit_work()
632 let login (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
633 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
634 let env = cgi#environment in
636 assert (cgi#arguments <> []);
637 let uid = cgi#argument_value "userid" in
638 let userpw = cgi#argument_value "password" in
640 MatitaAuthentication.check_pw uid userpw;
641 NCicLibrary.init (Some uid);
642 let ft = MatitaAuthentication.read_ft uid in
643 let _ = MatitaFilesystem.html_of_library uid ft in
644 let sid = MatitaAuthentication.create_session uid in
645 (* let cookie = Netcgi.Cookie.make "session" (Uuidm.to_string sid) in
646 cgi#set_header ~set_cookies:[cookie] (); *)
647 env#set_output_header_field
648 "Set-Cookie" ("session=" ^ (Uuidm.to_string sid));
649 (* env#set_output_header_field "Location" "/index.html" *)
650 cgi#out_channel#output_string
651 ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/index.html\">"
652 ^ "</head><body>Redirecting to Matita page...</body></html>")
653 with MatitaAuthentication.InvalidPassword ->
656 ~content_type:"text/html; charset=\"utf-8\""
658 cgi#out_channel#output_string
659 "<html><head></head><body>Authentication error</body></html>");
660 cgi#out_channel#commit_work()
663 let logout (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
664 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
665 let env = cgi#environment in
667 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
668 let sid = HExtlib.unopt sid in
669 MatitaAuthentication.logout_user sid;
672 ~content_type:"text/html; charset=\"utf-8\""
674 let text = read_file (rt_path () ^ "/logout.html") in
675 cgi#out_channel#output_string text
679 ~status:`Internal_server_error
681 ~content_type:"text/html; charset=\"utf-8\""
683 cgi#out_channel#commit_work()
686 exception File_already_exists;;
688 let save (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
689 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
690 let env = cgi#environment in
692 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
693 let sid = HExtlib.unopt sid in
694 let status = MatitaAuthentication.get_status sid in
695 let uid = MatitaAuthentication.user_of_session sid in
696 assert (cgi#arguments <> []);
697 let locked = cgi#argument_value "locked" in
698 let unlocked = cgi#argument_value "unlocked" in
699 let dir = cgi#argument_value "dir" in
700 let rel_filename = cgi # argument_value "file" in
701 let filename = libdir uid ^ "/" ^ rel_filename in
702 let force = bool_of_string (cgi#argument_value "force") in
703 let already_exists = Sys.file_exists filename in
705 if ((not force) && already_exists) then
706 raise File_already_exists;
709 Unix.mkdir filename 0o744
712 let oc = open_out filename in
713 output_string oc (locked ^ unlocked);
715 if MatitaEngine.eos status unlocked then
717 (* prerr_endline ("serializing proof objects..."); *)
718 GrafiteTypes.Serializer.serialize
719 ~baseuri:(NUri.uri_of_string status#baseuri) status;
720 (* prerr_endline ("done."); *)
725 List.assoc rel_filename (MatitaAuthentication.read_ft uid)
726 with Not_found -> MatitaFilesystem.MUnversioned
728 (if old_flag <> MatitaFilesystem.MConflict &&
729 old_flag <> MatitaFilesystem.MAdd then
731 if already_exists then MatitaFilesystem.MModified
732 else MatitaFilesystem.MAdd
734 MatitaAuthentication.set_file_flag uid [rel_filename, Some newflag]);
737 ~content_type:"text/xml; charset=\"utf-8\""
739 cgi#out_channel#output_string "<response>ok</response>"
741 | File_already_exists ->
744 ~content_type:"text/xml; charset=\"utf-8\""
746 cgi#out_channel#output_string "<response>cancelled</response>"
749 ~status:`Internal_server_error
751 ~content_type:"text/xml; charset=\"utf-8\""
756 ~content_type:"text/xml; charset=\"utf-8\""
758 let estr = Printexc.to_string e in
759 cgi#out_channel#output_string ("<response>" ^ estr ^ "</response>"));
760 cgi#out_channel#commit_work()
763 let initiate_commit (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
764 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
765 let env = cgi#environment in
767 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
768 let sid = HExtlib.unopt sid in
769 MatitaAuthentication.probe_commit_priv sid;
770 let uid = MatitaAuthentication.user_of_session sid in
771 let out = do_global_commit (* () *) uid in
774 ~content_type:"text/xml; charset=\"utf-8\""
776 cgi#out_channel#output_string "<commit>";
777 cgi#out_channel#output_string "<response>ok</response>";
778 cgi#out_channel#output_string ("<details>" ^ out ^ "</details>");
779 cgi#out_channel#output_string "</commit>"
784 ~content_type:"text/xml; charset=\"utf-8\""
786 cgi#out_channel#output_string
787 "<commit><error>no commit privileges</error></commit>"
790 ~status:`Internal_server_error
792 ~content_type:"text/xml; charset=\"utf-8\""
794 cgi#out_channel#commit_work()
797 let svn_update (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
798 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
799 let env = cgi#environment in
800 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
801 let sid = HExtlib.unopt sid in
802 let uid = MatitaAuthentication.user_of_session sid in
804 MatitaAuthentication.probe_commit_priv sid;
805 let files,anomalies,(added,conflict,del,upd,merged) =
806 MatitaFilesystem.update_user uid
808 let anomalies = String.concat "\n" anomalies in
809 let details = Printf.sprintf
811 "%d deleted files\n"^^
812 "%d updated files\n"^^
813 "%d merged files\n"^^
814 "%d conflicting files\n\n" ^^
815 "Anomalies:\n%s") added del upd merged conflict anomalies
817 prerr_endline ("update details:\n" ^ details);
819 Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () details
821 MatitaAuthentication.set_file_flag uid files;
824 ~content_type:"text/xml; charset=\"utf-8\""
826 cgi#out_channel#output_string "<update>";
827 cgi#out_channel#output_string "<response>ok</response>";
828 cgi#out_channel#output_string ("<details>" ^ details ^ "</details>");
829 cgi#out_channel#output_string "</update>";
834 ~content_type:"text/xml; charset=\"utf-8\""
836 cgi#out_channel#output_string
837 "<commit><error>no commit privileges</error></commit>"
840 ~status:`Internal_server_error
842 ~content_type:"text/xml; charset=\"utf-8\""
844 cgi#out_channel#commit_work()
847 (* returns the length of the executed text and an html representation of the
850 let advance (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
851 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
852 let env = cgi#environment in
854 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
855 let sid = HExtlib.unopt sid in
859 ~content_type:"text/xml; charset=\"utf-8\""
862 let text = cgi#argument_value "body" in
863 (* prerr_endline ("body =\n" ^ text); *)
864 let parsed_len, new_parsed, new_unparsed, new_status = advance0 sid text in
865 let txt = output_status new_status in
867 "<response><parsed length=\"" ^ (string_of_int parsed_len) ^ "\">" ^
868 new_parsed ^ "</parsed>" ^ txt
871 (* prerr_endline ("sending advance response:\n" ^ body); *)
874 ~content_type:"text/xml; charset=\"utf-8\""
876 cgi#out_channel#output_string body
878 | Emphasized_error text ->
879 (* | MultiPassDisambiguator.DisambiguationError (offset,errorll) -> *)
880 let body = "<response><error>" ^ text ^ "</error></response>" in
883 ~content_type:"text/xml; charset=\"utf-8\""
885 cgi#out_channel#output_string body
886 | Disamb_error text ->
887 let body = "<response>" ^ text ^ "</response>" in
890 ~content_type:"text/xml; charset=\"utf-8\""
892 cgi#out_channel#output_string body
894 let body = "<response><parsed length=\"0\"></parsed></response>" in
897 ~content_type:"text/xml; charset=\"utf-8\""
899 cgi#out_channel#output_string body
902 ~status:`Internal_server_error
904 ~content_type:"text/xml; charset=\"utf-8\""
907 cgi#out_channel#commit_work()
910 let gotoBottom (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
911 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
912 let env = cgi#environment in
914 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
915 let sid = HExtlib.unopt sid in
917 let error_msg = function
918 | Emphasized_error text -> "<localized>" ^ text ^ "</localized>"
919 | Disamb_error text -> text
920 | End_of_file _ -> (* not an error *) ""
921 | e -> (* unmanaged error *)
923 (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
924 (Printexc.to_string e)) ^ "</error>"
927 let rec aux acc text =
929 prerr_endline ("evaluating: " ^ first_line text);
930 let plen,new_parsed,new_unparsed,_new_status = advance0 sid text in
931 aux ((plen,new_parsed)::acc) new_unparsed
932 with e -> acc, error_msg e
933 (* DON'T SERIALIZE NOW!!!
934 let status = MatitaAuthentication.get_status sid in
935 GrafiteTypes.Serializer.serialize
936 ~baseuri:(NUri.uri_of_string status#baseuri) status;
942 ~content_type:"text/xml; charset=\"utf-8\""
945 let text = cgi#argument_value "body" in
946 (* prerr_endline ("body =\n" ^ text); *)
947 let len_parsedlist, err_msg = aux [] text in
948 let status = MatitaAuthentication.get_status sid in
949 let txt = output_status status in
950 let parsed_tag (len,txt) =
951 "<parsed length=\"" ^ (string_of_int len) ^ "\">" ^ txt ^ "</parsed>"
953 (* List.rev: the list begins with the older parsed txt *)
956 String.concat "" (List.rev (List.map parsed_tag len_parsedlist)) ^
957 txt ^ err_msg ^ "</response>"
959 (* prerr_endline ("sending goto bottom response:\n" ^ body); *)
962 ~content_type:"text/xml; charset=\"utf-8\""
964 cgi#out_channel#output_string body;
965 (* with Not_found -> cgi#set_header ~status:`Internal_server_error
967 ~content_type:"text/xml; charset=\"utf-8\"" ()); *)
968 cgi#out_channel#commit_work()
971 let gotoTop (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
972 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
973 let env = cgi#environment in
974 prerr_endline "executing goto Top";
976 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
977 let sid = HExtlib.unopt sid in
981 ~content_type:"text/xml; charset=\"utf-8\""
984 let status = MatitaAuthentication.get_status sid in
985 let uid = MatitaAuthentication.user_of_session sid in
986 let baseuri = status#baseuri in
987 let new_status = new MatitaEngine.status (Some uid) baseuri in
988 prerr_endline "gototop prima della time travel";
989 (* NCicLibrary.time_travel new_status; *)
990 prerr_endline "gototop dopo della time travel";
991 let new_history = [new_status] in
992 MatitaAuthentication.set_history sid new_history;
993 MatitaAuthentication.set_status sid new_status;
994 (* NCicLibrary.time_travel new_status; *)
997 ~content_type:"text/xml; charset=\"utf-8\""
999 cgi#out_channel#output_string "<response>ok</response>"
1001 (cgi#set_header ~status:`Internal_server_error
1003 ~content_type:"text/xml; charset=\"utf-8\"" ();
1004 cgi#out_channel#output_string "<response>ok</response>"));
1005 cgi#out_channel#commit_work()
1008 let retract (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
1009 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
1010 let env = cgi#environment in
1012 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
1013 let sid = HExtlib.unopt sid in
1017 ~content_type:"text/xml; charset=\"utf-8\""
1020 let history = MatitaAuthentication.get_history sid in
1021 let old_status = MatitaAuthentication.get_status sid in
1022 let new_history,new_status =
1024 _::(status::_ as history) ->
1026 | [_] -> (prerr_endline "singleton";failwith "retract")
1027 | _ -> (prerr_endline "nil"; assert false) in
1028 (* prerr_endline "timestamp prima della retract";
1029 old_status#print_timestamp ();
1030 prerr_endline "timestamp della retract";
1031 new_status#print_timestamp ();
1032 prerr_endline ("prima della time travel"); *)
1033 NCicLibrary.time_travel new_status;
1034 prerr_endline ("dopo della time travel");
1035 MatitaAuthentication.set_history sid new_history;
1036 MatitaAuthentication.set_status sid new_status;
1037 prerr_endline ("baseuri after retract = " ^ new_status#baseuri);
1038 let body = output_status new_status in
1041 ~content_type:"text/xml; charset=\"utf-8\""
1043 cgi#out_channel#output_string body
1045 prerr_endline ("error in retract: " ^ Printexc.to_string e);
1046 cgi#set_header ~status:`Internal_server_error
1048 ~content_type:"text/xml; charset=\"utf-8\"" ());
1049 cgi#out_channel#commit_work()
1053 let viewLib (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
1054 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
1055 let env = cgi#environment in
1057 let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
1058 let sid = HExtlib.unopt sid in
1062 ~content_type:"text/html; charset=\"utf-8\""
1065 let uid = MatitaAuthentication.user_of_session sid in
1067 let ft = MatitaAuthentication.read_ft uid in
1068 let html = MatitaFilesystem.html_of_library uid ft in
1071 ~content_type:"text/html; charset=\"utf-8\""
1073 cgi#out_channel#output_string
1076 "<title>XML Tree Control</title>\n" ^
1077 "<link href=\"treeview/xmlTree.css\" type=\"text/css\" rel=\"stylesheet\">\n" ^
1078 "<script src=\"treeview/xmlTree.js\" type=\"text/javascript\"></script>\n" ^
1080 html (* ^ "\n</body></html>" *) );
1082 let files,anomalies = MatitaFilesystem.stat_user uid in
1083 let changed = HExtlib.filter_map
1084 (fun (n,f) -> if (f = Some MatitaFilesystem.MModified) then Some n else None) files
1086 let changed = String.concat "\n" changed in
1087 let anomalies = String.concat "\n" anomalies in
1088 prerr_endline ("Changed:\n" ^ changed ^ "\n\nAnomalies:\n" ^ anomalies);
1089 cgi#out_channel#commit_work()
1093 let resetLib (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
1094 let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
1095 MatitaAuthentication.reset ();
1098 ~content_type:"text/html; charset=\"utf-8\""
1101 cgi#out_channel#output_string
1103 "<title>Matitaweb Reset</title>\n" ^
1104 "<body><H1>Reset completed</H1></body></html>");
1105 cgi#out_channel#commit_work()
1107 open Netcgi1_compat.Netcgi_types;;
1109 (**********************************************************************)
1110 (* Create the webserver *)
1111 (**********************************************************************)
1115 let (opt_list, cmdline_cfg) = Netplex_main.args() in
1117 let use_mt = ref true in
1120 [ "-mt", Arg.Set use_mt,
1121 " Use multi-threading instead of multi-processing"
1126 (fun s -> raise (Arg.Bad ("Don't know what to do with: " ^ s)))
1127 "usage: netplex [options]";
1130 Netplex_mt.mt() (* multi-threading *)
1132 Netplex_mp.mp() in (* multi-processing *)
1135 { Nethttpd_services.dyn_handler = (fun _ -> process1);
1136 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1137 dyn_uri = None; (* not needed *)
1138 dyn_translator = (fun _ -> ""); (* not needed *)
1139 dyn_accept_all_conditionals = false;
1143 { Nethttpd_services.dyn_handler = (fun _ -> advance);
1144 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1145 dyn_uri = None; (* not needed *)
1146 dyn_translator = (fun _ -> ""); (* not needed *)
1147 dyn_accept_all_conditionals = false;
1150 { Nethttpd_services.dyn_handler = (fun _ -> retract);
1151 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1152 dyn_uri = None; (* not needed *)
1153 dyn_translator = (fun _ -> ""); (* not needed *)
1154 dyn_accept_all_conditionals = false;
1157 { Nethttpd_services.dyn_handler = (fun _ -> gotoBottom);
1158 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1159 dyn_uri = None; (* not needed *)
1160 dyn_translator = (fun _ -> ""); (* not needed *)
1161 dyn_accept_all_conditionals = false;
1164 { Nethttpd_services.dyn_handler = (fun _ -> gotoTop);
1165 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1166 dyn_uri = None; (* not needed *)
1167 dyn_translator = (fun _ -> ""); (* not needed *)
1168 dyn_accept_all_conditionals = false;
1171 { Nethttpd_services.dyn_handler = (fun _ -> retrieve);
1172 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1173 dyn_uri = None; (* not needed *)
1174 dyn_translator = (fun _ -> ""); (* not needed *)
1175 dyn_accept_all_conditionals = false;
1178 { Nethttpd_services.dyn_handler = (fun _ -> register);
1179 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1180 dyn_uri = None; (* not needed *)
1181 dyn_translator = (fun _ -> ""); (* not needed *)
1182 dyn_accept_all_conditionals = false;
1185 { Nethttpd_services.dyn_handler = (fun _ -> login);
1186 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1187 dyn_uri = None; (* not needed *)
1188 dyn_translator = (fun _ -> ""); (* not needed *)
1189 dyn_accept_all_conditionals = false;
1192 { Nethttpd_services.dyn_handler = (fun _ -> logout);
1193 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1194 dyn_uri = None; (* not needed *)
1195 dyn_translator = (fun _ -> ""); (* not needed *)
1196 dyn_accept_all_conditionals = false;
1199 { Nethttpd_services.dyn_handler = (fun _ -> viewLib);
1200 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1201 dyn_uri = None; (* not needed *)
1202 dyn_translator = (fun _ -> ""); (* not needed *)
1203 dyn_accept_all_conditionals = false;
1206 { Nethttpd_services.dyn_handler = (fun _ -> resetLib);
1207 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1208 dyn_uri = None; (* not needed *)
1209 dyn_translator = (fun _ -> ""); (* not needed *)
1210 dyn_accept_all_conditionals = false;
1213 { Nethttpd_services.dyn_handler = (fun _ -> save);
1214 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1215 dyn_uri = None; (* not needed *)
1216 dyn_translator = (fun _ -> ""); (* not needed *)
1217 dyn_accept_all_conditionals = false;
1220 { Nethttpd_services.dyn_handler = (fun _ -> initiate_commit);
1221 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1222 dyn_uri = None; (* not needed *)
1223 dyn_translator = (fun _ -> ""); (* not needed *)
1224 dyn_accept_all_conditionals = false;
1227 { Nethttpd_services.dyn_handler = (fun _ -> svn_update);
1228 dyn_activation = Nethttpd_services.std_activation `Std_activation_buffered;
1229 dyn_uri = None; (* not needed *)
1230 dyn_translator = (fun _ -> ""); (* not needed *)
1231 dyn_accept_all_conditionals = false;
1235 let nethttpd_factory =
1236 Nethttpd_plex.nethttpd_factory
1237 ~handlers:[ "advance", do_advance
1238 ; "retract", do_retract
1239 ; "bottom", goto_bottom
1242 ; "register", do_register
1244 ; "logout", do_logout
1245 ; "reset", do_resetlib
1246 ; "viewlib", do_viewlib
1248 ; "commit", do_commit
1249 ; "update", do_update]
1251 MatitaInit.initialize_all ();
1252 MatitaAuthentication.deserialize ();
1253 Netplex_main.startup
1255 Netplex_log.logger_factories (* allow all built-in logging styles *)
1256 Netplex_workload.workload_manager_factories (* ... all ways of workload management *)
1257 [ nethttpd_factory ] (* make this nethttpd available *)
1261 Sys.set_signal Sys.sigpipe Sys.Signal_ignore;