+ let env = cgi#environment in
+ (try
+ let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
+ let sid = HExtlib.unopt sid in
+ let uid = MatitaAuthentication.user_of_session sid in
+ (*
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ *)
+ let readonly = cgi # argument_value "readonly" in
+ let filename = libdir uid ^ "/" ^ (cgi # argument_value "file") in
+ (* prerr_endline ("reading file " ^ filename); *)
+ let body =
+ Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false ()
+ (html_of_matita (read_file filename)) in
+
+ (* html_of_matita (read_file filename) in *)
+ (* prerr_endline ("sending:\nBEGIN\n" ^ body ^ "\nEND"); *)
+ let body = "<response><file>" ^ body ^ "</file></response>" in
+ let baseuri, incpaths =
+ try
+ let root, baseuri, _fname, _tgt =
+ Librarian.baseuri_of_script ~include_paths:[] filename in
+ let includes =
+ try
+ Str.split (Str.regexp " ")
+ (List.assoc "include_paths" (Librarian.load_root_file (root^"/root")))
+ with Not_found -> []
+ in
+ let rc = root :: includes in
+ List.iter (HLog.debug) rc; baseuri, rc
+ with
+ Librarian.NoRootFor _ | Librarian.FileNotFound _ -> "",[] in
+ include_paths := incpaths;
+ if readonly <> "true" then
+ (let status = new MatitaEngine.status (Some uid) baseuri in
+ let history = [status] in
+ MatitaAuthentication.set_status sid status;
+ MatitaAuthentication.set_history sid history);
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string body;
+ with
+ | Sys_error _ ->
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/xml; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string "<error />"
+ | Not_found _ ->
+ cgi # set_header
+ ~status:`Internal_server_error
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ());
+ cgi#out_channel#commit_work()
+;;
+
+let xml_of_disamb_error l =
+ let mk_alias = function
+ | GrafiteAst.Ident_alias (_,uri) -> "href=\"" ^ uri ^ "\""
+ | GrafiteAst.Symbol_alias (_,uri,desc)
+ | GrafiteAst.Number_alias (uri,desc) ->
+ let uri = try HExtlib.unopt uri with _ -> "cic:/fakeuri.def(1)" in
+ "href=\"" ^ uri ^ "\" title=\"" ^
+ (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
+ ^ "\""
+ in
+
+ let mk_interpr (loc,a) =
+ let x,y = HExtlib.loc_of_floc loc in
+ Printf.sprintf "<interpretation start=\"%d\" stop=\"%d\" %s />"
+ x y (mk_alias a)
+ in
+
+ let mk_failure (il,loc,msg) =
+ let x,y = HExtlib.loc_of_floc loc in
+ Printf.sprintf "<failure start=\"%d\" stop=\"%d\" title=\"%s\">%s</failure>"
+ x y (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () msg)
+ (String.concat "" (List.map mk_interpr il))
+ in
+
+ let mk_choice (a,fl) =
+ let fl' = String.concat "" (List.map mk_failure fl) in
+ match a with
+ | None -> "<choice>" ^ fl' ^ "</choice>"
+ | Some a -> Printf.sprintf "<choice %s>%s</choice>" (mk_alias a) fl'
+ in
+
+ let mk_located (loc,cl) =
+ let x,y = HExtlib.loc_of_floc loc in
+ Printf.sprintf "<choicepoint start=\"%d\" stop=\"%d\">%s</choicepoint>"
+ x y (String.concat "" (List.map mk_choice cl))
+ in
+ "<disamberror>" ^ (String.concat "" (List.map mk_located l)) ^ "</disamberror>"
+;;
+
+let advance0 sid text =
+ let status = MatitaAuthentication.get_status sid in
+ let history = MatitaAuthentication.get_history sid in
+ let status = status#reset_disambiguate_db () in
+ let (st,new_statements,new_unparsed),parsed_len =
+ let rec do_exc = function
+ | MatitaEngine.EnrichedWithStatus (e,_) -> do_exc e
+ | NCicTypeChecker.TypeCheckerFailure s -> raise (Generic_error (Lazy.force s))
+ | HExtlib.Localized (floc,e) ->
+ let x, y = HExtlib.loc_of_floc floc in
+ let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
+ let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
+ let post = Netconversion.ustring_sub `Enc_utf8 y
+ (Netconversion.ustring_length `Enc_utf8 text - y) text in
+ let _,title = MatitaExcPp.to_string e in
+ (* let title = "" in *)
+ let marked =
+ pre ^ "\005span class=\"error\" title=\"" ^ title ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
+ let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
+ () (html_of_matita marked) in
+ raise (Emphasized_error marked)
+ | Disambiguate.NoWellTypedInterpretation (floc,e) ->
+ let x, y = HExtlib.loc_of_floc floc in
+ let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
+ let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
+ let post = Netconversion.ustring_sub `Enc_utf8 y
+ (Netconversion.ustring_length `Enc_utf8 text - y) text in
+ (*let _,title = MatitaExcPp.to_string e in*)
+ (* let title = "" in *)
+ let marked =
+ pre ^ "\005span class=\"error\" title=\"" ^ e ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
+ let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
+ () (html_of_matita marked) in
+ raise (Emphasized_error marked)
+ | NCicRefiner.Uncertain m as exn ->
+ let floc, e = Lazy.force m in
+ let x, y = HExtlib.loc_of_floc floc in
+ let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
+ let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
+ let post = Netconversion.ustring_sub `Enc_utf8 y
+ (Netconversion.ustring_length `Enc_utf8 text - y) text in
+ (* let _,title = MatitaExcPp.to_string e in *)
+ (* let title = "" in *)
+ let marked =
+ pre ^ "\005span class=\"error\" title=\"" ^ e ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
+ let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
+ () (html_of_matita marked) in
+ raise (Emphasized_error marked)
+ | NTacStatus.Error (s,None) as e ->
+ prerr_endline
+ ("NTacStatus.Error " ^ (Lazy.force s)); raise e
+ | NTacStatus.Error (s,Some exc) as e ->
+ prerr_endline
+ ("NTacStatus.Error " ^ Lazy.force s ^ " -- " ^ (Printexc.to_string exc));
+ do_exc exc
+ | GrafiteDisambiguate.Ambiguous_input (loc,choices) ->
+ let x,y = HExtlib.loc_of_floc loc in
+ let choice_of_alias = function
+ | GrafiteAst.Ident_alias (_,uri) -> uri, None, uri
+ | GrafiteAst.Number_alias (None,desc)
+ | GrafiteAst.Symbol_alias (_,None,desc) -> "cic:/fakeuri.def(1)", Some desc, desc
+ | GrafiteAst.Number_alias (Some uri,desc)
+ | GrafiteAst.Symbol_alias (_,Some uri,desc) -> uri, Some desc, desc
+ in
+ let tag_of_choice (uri,title,desc) =
+ match title with
+ | None -> Printf.sprintf "<choice href=\"%s\">%s</choice>"
+ uri
+ (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
+ | Some t -> Printf.sprintf "<choice href=\"%s\" title=\"%s\">%s</choice>"
+ uri
+ (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () t)
+ (Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false () desc)
+ in
+ let strchoices =
+ String.concat "\n"
+ (List.map (fun x -> tag_of_choice (choice_of_alias x)) choices)
+ in
+ prerr_endline (Printf.sprintf
+ "@@@ Ambiguous input at (%d,%d). Possible choices:\n\n%s\n\n@@@ End."
+ x y strchoices);
+ (*
+ let pre = Netconversion.ustring_sub `Enc_utf8 0 x text in
+ let err = Netconversion.ustring_sub `Enc_utf8 x (y-x) text in
+ let post = Netconversion.ustring_sub `Enc_utf8 y
+ (Netconversion.ustring_length `Enc_utf8 text - y) text in
+ let title = "Disambiguation Error" in
+ (* let title = "" in *)
+ let marked =
+ pre ^ "\005span class=\"error\" title=\"" ^ title ^ "\"\006" ^ err ^ "\005/span\006" ^ post in
+ let marked = Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
+ () (html_of_matita marked) in
+ *)
+ let strchoices = Printf.sprintf
+ "<ambiguity start=\"%d\" stop=\"%d\">%s</ambiguity>" x y strchoices
+ in raise (Disamb_error strchoices)
+ | GrafiteDisambiguate.Error l -> raise (Disamb_error (xml_of_disamb_error l))
+ (* | End_of_file -> ... *)
+ | e ->
+ (* prerr_endline ("matitadaemon *** Unhandled exception " ^ Printexc.to_string e); *)
+ prerr_endline ("matitadaemon *** Unhandled exception " ^ snd (MatitaExcPp.to_string e));
+ raise e
+ in
+
+ try
+ eval_statement !include_paths (*buffer*) status (`Raw text)
+ with e -> do_exc e
+ in
+ debug "BEGIN PRINTGRAMMAR";
+ (*prerr_endline (Print_grammar.ebnf_of_term status);*)
+ (*let kwds = String.concat ", " status#get_kwds in
+ debug ("keywords = " ^ kwds );*)
+ debug "END PRINTGRAMMAR";
+ MatitaAuthentication.set_status sid st;
+ MatitaAuthentication.set_history sid (st::history);
+(* prerr_endline "previous timestamp";
+ status#print_timestamp();
+ prerr_endline "current timestamp";
+ st#print_timestamp(); *)
+ parsed_len,
+ Netencoding.Html.encode ~in_enc:`Enc_utf8 ~prefer_name:false
+ () (html_of_matita new_statements), new_unparsed, st
+
+let register (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let _env = cgi#environment in
+
+ assert (cgi#arguments <> []);
+ let uid = cgi#argument_value "userid" in
+ let userpw = cgi#argument_value "password" in
+ (try
+ (* currently registering only unprivileged users *)
+ MatitaAuthentication.add_user uid userpw false;
+(* env#set_output_header_field "Location" "/index.html" *)
+ cgi#out_channel#output_string
+ ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/login.html\">"
+ ^ "</head><body>Redirecting to login page...</body></html>")
+ with
+ | MatitaAuthentication.UsernameCollision _ ->
+ cgi#set_header
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string
+ "<html><head></head><body>Error: User id collision!</body></html>"
+ | MatitaFilesystem.SvnError msg ->
+ cgi#set_header
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string
+ ("<html><head></head><body><p>Error: Svn checkout failed!<p><p><textarea>"
+ ^ msg ^ "</textarea></p></body></html>"));
+ cgi#out_channel#commit_work()
+;;
+
+let login (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+
+ assert (cgi#arguments <> []);
+ let uid = cgi#argument_value "userid" in
+ let userpw = cgi#argument_value "password" in
+ (try
+ MatitaAuthentication.check_pw uid userpw;
+ NCicLibrary.init (Some uid);
+ let ft = MatitaAuthentication.read_ft uid in
+ let _ = MatitaFilesystem.html_of_library uid ft in
+ let sid = MatitaAuthentication.create_session uid in
+ (* let cookie = Netcgi.Cookie.make "session" (Uuidm.to_string sid) in
+ cgi#set_header ~set_cookies:[cookie] (); *)
+ env#set_output_header_field
+ "Set-Cookie" ("session=" ^ (Uuidm.to_string sid));
+ (* env#set_output_header_field "Location" "/index.html" *)
+ cgi#out_channel#output_string
+ ("<html><head><meta http-equiv=\"refresh\" content=\"2;url=/index.html\">"
+ ^ "</head><body>Redirecting to Matita page...</body></html>")
+ with MatitaAuthentication.InvalidPassword ->
+ cgi#set_header
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ();
+ cgi#out_channel#output_string
+ "<html><head></head><body>Authentication error</body></html>");
+ cgi#out_channel#commit_work()
+;;
+
+let logout (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+ (try
+ let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
+ let sid = HExtlib.unopt sid in
+ MatitaAuthentication.logout_user sid;
+ cgi # set_header
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ();
+ let text = read_file (rt_path () ^ "/logout.html") in
+ cgi#out_channel#output_string text
+ with
+ | Not_found _ ->
+ cgi # set_header
+ ~status:`Internal_server_error
+ ~cache:`No_cache
+ ~content_type:"text/html; charset=\"utf-8\""
+ ());
+ cgi#out_channel#commit_work()
+;;
+
+exception File_already_exists;;
+
+let save (cgi : Netcgi1_compat.Netcgi_types.cgi_activation) =
+ let cgi = Netcgi1_compat.Netcgi_types.of_compat_activation cgi in
+ let env = cgi#environment in
+ (try
+ let sid = Uuidm.of_string (Netcgi.Cookie.value (env#cookie "session")) in
+ let sid = HExtlib.unopt sid in
+ let status = MatitaAuthentication.get_status sid in
+ let uid = MatitaAuthentication.user_of_session sid in
+ assert (cgi#arguments <> []);
+ let locked = cgi#argument_value "locked" in
+ let unlocked = cgi#argument_value "unlocked" in
+ let dir = cgi#argument_value "dir" in
+ let rel_filename = cgi # argument_value "file" in
+ let filename = libdir uid ^ "/" ^ rel_filename in
+ let force = bool_of_string (cgi#argument_value "force") in
+ let already_exists = Sys.file_exists filename in
+
+ if ((not force) && already_exists) then
+ raise File_already_exists;
+
+ if dir = "true" then
+ Unix.mkdir filename 0o744
+ else
+ begin
+ let oc = open_out filename in
+ output_string oc (locked ^ unlocked);
+ close_out oc;
+ if MatitaEngine.eos status unlocked then
+ begin
+ (* prerr_endline ("serializing proof objects..."); *)
+ GrafiteTypes.Serializer.serialize
+ ~baseuri:(NUri.uri_of_string status#baseuri) status;
+ (* prerr_endline ("done."); *)
+ end;
+ end;
+ let old_flag =
+ try
+ List.assoc rel_filename (MatitaAuthentication.read_ft uid)
+ with Not_found -> MatitaFilesystem.MUnversioned
+ in
+ (if old_flag <> MatitaFilesystem.MConflict &&
+ old_flag <> MatitaFilesystem.MAdd then
+ let newflag =
+ if already_exists then MatitaFilesystem.MModified
+ else MatitaFilesystem.MAdd