4 (*** from matitaScript.ml ***)
5 (* let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$" *)
7 let eval_statement include_paths (* (buffer : GText.buffer) *) status (* script *)
10 let ast,unparsed_text =
13 (* if Pcre.pmatch ~rex:only_dust_RE text then raise Margin; *)
15 GrafiteParser.parsable_statement status
16 (Ulexing.from_utf8_string text) in
17 let ast = MatitaEngine.get_ast status include_paths strm in
19 | `Ast (st, text) -> st, text
21 let floc = match ast with
22 | GrafiteAst.Executable (loc, _)
23 | GrafiteAst.Comment (loc, _) -> loc in
25 let _,lend = HExtlib.loc_of_floc floc in
26 let parsed_text, parsed_text_len =
27 MatitaGtkMisc.utf8_parsed_text unparsed_text (HExtlib.floc_of_loc (0,lend)) in
30 MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
32 (status, parsed_text),"",(*parsed_text_len*) Glib.Utf8.length parsed_text
35 let status = ref (new MatitaEngine.status "cic:/matita");;
37 let include_paths = ["/home/barolo/matitaB/matita/lib"];;
39 let advance text (* (?bos=false) *) =
40 (* if bos then LibraryClean.clean_baseuris [self#buri_of_current_file];
41 (* HLog.debug ("evaluating: " ^ first_line s ^ " ...");*)
42 let time1 = Unix.gettimeofday () in
43 let entries, newtext, parsed_len = *)
44 let (st,_),_,parsed_len =
46 eval_statement include_paths (*buffer*) !status (`Raw text)
47 (* with End_of_file -> raise Margin *)
50 let _,_,metasenv,subst,_ = !status#obj in
51 let txt = List.fold_left
52 (fun acc (nmeta,_ as meta) ->
53 let txt0 = Netencoding.Html.encode ~in_enc:`Enc_utf8 ()
54 (snd (ApplyTransformation.ntxt_of_cic_sequent
55 ~metasenv ~subst ~map_unicode_to_tex:false 80 !status meta)) in
56 ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
59 let txt = String.concat "\n\n" txt in
62 let time2 = Unix.gettimeofday () in
63 HLog.debug ("... done in " ^ string_of_float (time2 -. time1) ^ "s");
64 let new_statuses, new_statements =
65 let statuses, texts = List.split entries in
68 history <- new_statuses @ history;
69 statements <- new_statements @ statements;
70 let start = buffer#get_iter_at_mark (`MARK locked_mark) in
71 let new_text = String.concat "" (List.rev new_statements) in
72 if statement <> None then
73 buffer#insert ~iter:start new_text
75 let parsed_text = String.sub s 0 parsed_len in
76 if new_text <> parsed_text then begin
77 let stop = start#copy#forward_chars (Glib.Utf8.length parsed_text) in
78 buffer#delete ~start ~stop;
79 buffer#insert ~iter:start new_text;
82 self#moveMark (Glib.Utf8.length new_text);
83 buffer#insert ~iter:(buffer#get_iter_at_mark (`MARK locked_mark)) newtext*)
87 let chan = open_in fname in
91 lines := input_line chan :: !lines
93 with End_of_file -> close_in chan);
94 String.concat "\r\n" (List.rev !lines)
97 let load_index outchan =
98 let s = read_file "index.html" in
99 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
102 let call_service outchan =
104 (ignore(MatitaEngine.assert_ng
105 ~include_paths:["/home/barolo/matitaB/matita/lib"] (* ~outch:outchan *)
106 "/home/barolo/matitaB/matita/lib/basics/pts.ma");
107 prerr_endline "fatto";
108 let s = read_file "/home/barolo/matitaB/matita/lib/basics/pts.ma.mad"
110 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
113 e -> Http_daemon.respond ~code:(`Code 500) outchan
116 let callback req outchan =
118 (sprintf "request path = %s\n" req#path) ^
119 (sprintf "request GET params = %s\n"
121 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_GET))) ^
122 (sprintf "request POST params = %s\n"
124 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_POST))) ^
125 (sprintf "request ALL params = %s\n"
127 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
128 (sprintf "cookies = %s\n"
129 (match req#cookies with
132 ^ (if req#hasHeader ~name:"cookie"
133 then "('Cookie:' header was '" ^ req#header ~name:"cookie" ^ "')"
134 else "(No 'Cookie:' header received)")
137 (List.map (fun (n,v) -> String.concat "=" [n;v]) cookies)))) ^
138 (sprintf "request BODY = '%s'\n\n" req#body)
140 (* Http_daemon.respond ~code:(`Code 200) ~body: str outchan *)
145 | "/" -> load_index outchan
146 | "/matita" -> call_service outchan
148 prerr_endline "getting 'file' argument";
149 let filename = List.assoc "file" req#params_GET in
150 prerr_endline ("reading file " ^ filename);
151 let body = read_file filename in
153 Librarian.baseuri_of_script ~include_paths:[] filename
155 status := (!status)#set_baseuri baseuri;
156 Http_daemon.respond ~code:(`Code 200) ~body outchan
158 let script = req#body in
159 prerr_endline ("body length = " ^ (string_of_int (String.length script)));
160 let (parsed_len,txt), res, code =
161 try advance script, "OK", `Code 200
163 | HExtlib.Localized(_,e)
165 (prerr_endline ("exception: " ^ Printexc.to_string e);
167 NTacStatus.pp_tac_status !status
168 with e -> prerr_endline ("inner exception: " ^
169 Printexc.to_string e));
170 prerr_endline "end status";
171 let _,_,metasenv,subst,_ = !status#obj in
172 let txt = List.fold_left
173 (fun acc (nmeta,_ as meta) ->
174 let txt0 = Netencoding.Html.encode ~in_enc:`Enc_utf8 ()
175 (snd (ApplyTransformation.ntxt_of_cic_sequent
176 ~metasenv ~subst ~map_unicode_to_tex:false 80 !status meta)) in
177 ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
180 let txt = String.concat "\n\n" txt in
181 (0,txt), Printexc.to_string e, `Code 500)
183 let txt = Netencoding.Url.encode ~plus:false txt in
184 let body = (string_of_int parsed_len) ^ "#" ^ txt in
185 Http_daemon.respond ~code ~body outchan
186 | url -> Http_daemon.respond_not_found ~url outchan
193 { Http_daemon.default_spec with
201 MatitaInit.initialize_all ();
202 Http_daemon.main spec