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 let html_of_status s =
41 let _,_,metasenv,subst,_ = s#obj in
42 let txt = List.fold_left
43 (fun acc (nmeta,_ as meta) ->
44 let txt0 = snd (ApplyTransformation.ntxt_of_cic_sequent
45 ~metasenv ~subst ~map_unicode_to_tex:false 80 s meta)
47 prerr_endline ("### txt0 = " ^ txt0);
48 ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
51 String.concat "\n\n" txt
54 let advance text (* (?bos=false) *) =
55 let (st,new_statements),(* newtext TODO *) _,parsed_len =
57 eval_statement include_paths (*buffer*) !status (`Raw text)
58 (* with End_of_file -> raise Margin *)
61 history := st :: !history;
62 let txt = html_of_status !status in
67 let new_history,new_status =
69 _::(status::_ as history) ->
71 | [_] -> failwith "retract"
72 | _ -> assert false in
73 NCicLibrary.time_travel !status;
74 history := new_history;
76 html_of_status !status
80 let chan = open_in fname in
84 lines := input_line chan :: !lines
86 with End_of_file -> close_in chan);
87 String.concat "\r\n" (List.rev !lines)
90 let load_index outchan =
91 let s = read_file "index.html" in
92 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
95 let call_service outchan =
97 (ignore(MatitaEngine.assert_ng
98 ~include_paths:["/home/barolo/matitaB/matita/lib"] (* ~outch:outchan *)
99 "/home/barolo/matitaB/matita/lib/basics/pts.ma");
100 prerr_endline "fatto";
101 let s = read_file "/home/barolo/matitaB/matita/lib/basics/pts.ma.mad"
103 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
106 e -> Http_daemon.respond ~code:(`Code 500) outchan
110 let callback req outchan =
112 (sprintf "request path = %s\n" req#path) ^
113 (sprintf "request GET params = %s\n"
115 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_GET))) ^
116 (sprintf "request POST params = %s\n"
118 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_POST))) ^
119 (sprintf "request ALL params = %s\n"
121 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
122 (sprintf "cookies = %s\n"
123 (match req#cookies with
126 ^ (if req#hasHeader ~name:"cookie"
127 then "('Cookie:' header was '" ^ req#header ~name:"cookie" ^ "')"
128 else "(No 'Cookie:' header received)")
131 (List.map (fun (n,v) -> String.concat "=" [n;v]) cookies)))) ^
132 (sprintf "request BODY = '%s'\n\n" req#body)
134 (* Http_daemon.respond ~code:(`Code 200) ~body: str outchan *)
139 | "/" -> load_index outchan
140 | "/matita" -> call_service outchan
142 prerr_endline "getting 'file' argument";
143 let filename = List.assoc "file" req#params_GET in
144 prerr_endline ("reading file " ^ filename);
145 let body = read_file filename in
147 Librarian.baseuri_of_script ~include_paths:[] filename
149 status := (!status)#set_baseuri baseuri;
150 Http_daemon.respond ~code:(`Code 200) ~body outchan
152 let script = req#body in
153 prerr_endline ("body length = " ^ (string_of_int (String.length script)));
154 let (parsed_len,txt), res, code =
155 try advance script, "OK", `Code 200
157 | HExtlib.Localized(_,e)
159 (prerr_endline ("exception: " ^ Printexc.to_string e);
161 NTacStatus.pp_tac_status !status
162 with e -> prerr_endline ("inner exception: " ^
163 Printexc.to_string e));
164 prerr_endline "end status";
165 let txt = html_of_status !status in
166 (0,txt), Printexc.to_string e, `Code 500)
168 let txt = Netencoding.Url.encode ~plus:false txt in
169 let body = (string_of_int parsed_len) ^ "#" ^ txt in
170 Http_daemon.respond ~code ~body outchan
173 let txt = retract () in
174 Http_daemon.respond ~code:(`Code 200) ~body:txt outchan
176 (prerr_endline (Printexc.to_string e);
177 Http_daemon.respond ~code:(`Code 500) outchan))
178 | url -> Http_daemon.respond_not_found ~url outchan
185 { Http_daemon.default_spec with
193 MatitaInit.initialize_all ();
194 Http_daemon.main spec