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");;
36 let history = ref [!status];;
38 let include_paths = ["/home/barolo/matitaB/matita/lib"];;
40 (* lista [meta n., goal; meta n., goal; ... ] *)
41 (* "item1#item2#...#" *)
44 let _,_,metasenv,subst,_ = s#obj in
46 (fun acc (nmeta,_ as meta) ->
47 let txt0 = snd (ApplyTransformation.ntxt_of_cic_sequent
48 ~metasenv ~subst ~map_unicode_to_tex:false 80 s meta)
50 (*let txt0 = Netencoding.Html.encode ~in_enc:`Enc_utf8 () txt0 in*)
51 prerr_endline ("prima dell'encoding: BEGIN\n" ^ txt0 ^ "\nEND");
52 let txt0 = Netencoding.Url.encode ~plus:false txt0 in
53 (string_of_int nmeta) ^ "|" ^ txt0 ^ "#" ^ acc)
57 (* let html_of_status s =
58 let _,_,metasenv,subst,_ = s#obj in
59 let txt = List.fold_left
60 (fun acc (nmeta,_ as meta) ->
61 let txt0 = snd (ApplyTransformation.ntxt_of_cic_sequent
62 ~metasenv ~subst ~map_unicode_to_tex:false 80 s meta)
64 prerr_endline ("### txt0 = " ^ txt0);
65 ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
68 String.concat "\n\n" txt
71 let advance text (* (?bos=false) *) =
72 let (st,new_statements),(* newtext TODO *) _,parsed_len =
74 eval_statement include_paths (*buffer*) !status (`Raw text)
75 (* with End_of_file -> raise Margin *)
78 history := st :: !history;
79 let txt = output_status !status in
84 let new_history,new_status =
86 _::(status::_ as history) ->
88 | [_] -> failwith "retract"
89 | _ -> assert false in
90 NCicLibrary.time_travel !status;
91 history := new_history;
97 let chan = open_in fname in
101 lines := input_line chan :: !lines
103 with End_of_file -> close_in chan);
104 String.concat "\r\n" (List.rev !lines)
107 let load_index outchan =
108 let s = read_file "index.html" in
109 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
112 let call_service outchan =
114 (ignore(MatitaEngine.assert_ng
115 ~include_paths:["/home/barolo/matitaB/matita/lib"] (* ~outch:outchan *)
116 "/home/barolo/matitaB/matita/lib/basics/pts.ma");
117 prerr_endline "fatto";
118 let s = read_file "/home/barolo/matitaB/matita/lib/basics/pts.ma.mad"
120 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
123 e -> Http_daemon.respond ~code:(`Code 500) outchan
127 let callback req outchan =
129 (sprintf "request path = %s\n" req#path) ^
130 (sprintf "request GET params = %s\n"
132 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_GET))) ^
133 (sprintf "request POST params = %s\n"
135 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_POST))) ^
136 (sprintf "request ALL params = %s\n"
138 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
139 (sprintf "cookies = %s\n"
140 (match req#cookies with
143 ^ (if req#hasHeader ~name:"cookie"
144 then "('Cookie:' header was '" ^ req#header ~name:"cookie" ^ "')"
145 else "(No 'Cookie:' header received)")
148 (List.map (fun (n,v) -> String.concat "=" [n;v]) cookies)))) ^
149 (sprintf "request BODY = '%s'\n\n" req#body)
151 (* Http_daemon.respond ~code:(`Code 200) ~body: str outchan *)
156 | "/" -> load_index outchan
157 | "/matita" -> call_service outchan
159 prerr_endline "getting 'file' argument";
160 let filename = List.assoc "file" req#params_GET in
161 prerr_endline ("reading file " ^ filename);
162 let body = read_file filename in
164 Librarian.baseuri_of_script ~include_paths:[] filename
166 status := (!status)#set_baseuri baseuri;
167 Http_daemon.respond ~code:(`Code 200) ~body outchan
169 let script = req#body in
170 prerr_endline ("body length = " ^ (string_of_int (String.length script)));
171 let (parsed_len,txt), res, code =
172 try advance script, "OK", `Code 200
174 | HExtlib.Localized(_,e)
176 (prerr_endline ("exception: " ^ Printexc.to_string e);
178 NTacStatus.pp_tac_status !status
179 with e -> prerr_endline ("inner exception: " ^
180 Printexc.to_string e));
181 prerr_endline "end status";
182 let txt = output_status !status in
183 (0,txt), Printexc.to_string e, `Code 500)
185 prerr_endline ("server response:\n" ^ txt);
186 let body = (string_of_int parsed_len) ^ "@" ^ txt in
187 Http_daemon.respond ~code ~body outchan
190 let body = retract () in
191 Http_daemon.respond ~code:(`Code 200) ~body outchan
193 (prerr_endline (Printexc.to_string e);
194 Http_daemon.respond ~code:(`Code 500) outchan))
195 | url -> Http_daemon.respond_not_found ~url outchan
200 { Http_daemon.default_spec with
208 MatitaInit.initialize_all ();
209 Http_daemon.main spec