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 = snd (ApplyTransformation.ntxt_of_cic_sequent
54 ~metasenv ~subst ~map_unicode_to_tex:false 80 !status
56 prerr_endline ("### txt0 = " ^ txt0);
57 ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
60 let txt = String.concat "\n\n" txt in
63 let time2 = Unix.gettimeofday () in
64 HLog.debug ("... done in " ^ string_of_float (time2 -. time1) ^ "s");
65 let new_statuses, new_statements =
66 let statuses, texts = List.split entries in
69 history <- new_statuses @ history;
70 statements <- new_statements @ statements;
71 let start = buffer#get_iter_at_mark (`MARK locked_mark) in
72 let new_text = String.concat "" (List.rev new_statements) in
73 if statement <> None then
74 buffer#insert ~iter:start new_text
76 let parsed_text = String.sub s 0 parsed_len in
77 if new_text <> parsed_text then begin
78 let stop = start#copy#forward_chars (Glib.Utf8.length parsed_text) in
79 buffer#delete ~start ~stop;
80 buffer#insert ~iter:start new_text;
83 self#moveMark (Glib.Utf8.length new_text);
84 buffer#insert ~iter:(buffer#get_iter_at_mark (`MARK locked_mark)) newtext*)
88 let chan = open_in fname in
92 lines := input_line chan :: !lines
94 with End_of_file -> close_in chan);
95 String.concat "\r\n" (List.rev !lines)
98 let load_index outchan =
99 let s = read_file "index.html" in
100 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
103 let call_service outchan =
105 (ignore(MatitaEngine.assert_ng
106 ~include_paths:["/home/barolo/matitaB/matita/lib"] (* ~outch:outchan *)
107 "/home/barolo/matitaB/matita/lib/basics/pts.ma");
108 prerr_endline "fatto";
109 let s = read_file "/home/barolo/matitaB/matita/lib/basics/pts.ma.mad"
111 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
114 e -> Http_daemon.respond ~code:(`Code 500) outchan
117 let callback req outchan =
119 (sprintf "request path = %s\n" req#path) ^
120 (sprintf "request GET params = %s\n"
122 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_GET))) ^
123 (sprintf "request POST params = %s\n"
125 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_POST))) ^
126 (sprintf "request ALL params = %s\n"
128 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
129 (sprintf "cookies = %s\n"
130 (match req#cookies with
133 ^ (if req#hasHeader ~name:"cookie"
134 then "('Cookie:' header was '" ^ req#header ~name:"cookie" ^ "')"
135 else "(No 'Cookie:' header received)")
138 (List.map (fun (n,v) -> String.concat "=" [n;v]) cookies)))) ^
139 (sprintf "request BODY = '%s'\n\n" req#body)
141 (* Http_daemon.respond ~code:(`Code 200) ~body: str outchan *)
146 | "/" -> load_index outchan
147 | "/matita" -> call_service outchan
149 prerr_endline "getting 'file' argument";
150 let filename = List.assoc "file" req#params_GET in
151 prerr_endline ("reading file " ^ filename);
152 let body = read_file filename in
154 Librarian.baseuri_of_script ~include_paths:[] filename
156 status := (!status)#set_baseuri baseuri;
157 Http_daemon.respond ~code:(`Code 200) ~body outchan
159 let script = req#body in
160 prerr_endline ("body length = " ^ (string_of_int (String.length script)));
161 let (parsed_len,txt), res, code =
162 try advance script, "OK", `Code 200
164 | HExtlib.Localized(_,e)
166 (prerr_endline ("exception: " ^ Printexc.to_string e);
168 NTacStatus.pp_tac_status !status
169 with e -> prerr_endline ("inner exception: " ^
170 Printexc.to_string e));
171 prerr_endline "end status";
172 let _,_,metasenv,subst,_ = !status#obj in
173 let txt = List.fold_left
174 (fun acc (nmeta,_ as meta) ->
175 let txt0 = snd (ApplyTransformation.ntxt_of_cic_sequent
176 ~metasenv ~subst ~map_unicode_to_tex:false 80 !status
178 prerr_endline ("### txt0 = " ^ txt0);
179 ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
182 let txt = String.concat "\n\n" txt in
183 (0,txt), Printexc.to_string e, `Code 500)
185 let txt = Netencoding.Url.encode ~plus:false txt in
186 let body = (string_of_int parsed_len) ^ "#" ^ txt in
187 Http_daemon.respond ~code ~body outchan
188 | url -> Http_daemon.respond_not_found ~url outchan
195 { Http_daemon.default_spec with
203 MatitaInit.initialize_all ();
204 Http_daemon.main spec