4 module Stack = Continuationals.Stack
6 (*** from matitaScript.ml ***)
7 (* let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$" *)
9 let eval_statement include_paths (* (buffer : GText.buffer) *) status (* script *)
12 let ast,unparsed_text =
15 (* if Pcre.pmatch ~rex:only_dust_RE text then raise Margin; *)
17 GrafiteParser.parsable_statement status
18 (Ulexing.from_utf8_string text) in
19 let ast = MatitaEngine.get_ast status include_paths strm in
21 | `Ast (st, text) -> st, text
23 let floc = match ast with
24 | GrafiteAst.Executable (loc, _)
25 | GrafiteAst.Comment (loc, _) -> loc in
27 let _,lend = HExtlib.loc_of_floc floc in
28 let parsed_text, parsed_text_len =
29 MatitaGtkMisc.utf8_parsed_text unparsed_text (HExtlib.floc_of_loc (0,lend)) in
32 MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
34 (status, parsed_text),"",(*parsed_text_len*) Glib.Utf8.length parsed_text
37 let status = ref (new MatitaEngine.status "cic:/matita");;
38 let history = ref [!status];;
40 let include_paths = ["/home/barolo/matitaB/matita/lib"];;
42 (* lista [meta n., goal; meta n., goal; ... ] *)
43 (* "item1#item2#...#" *)
46 let _,_,metasenv,subst,_ = s#obj in
47 let render_switch = function
48 | Stack.Open i -> "?" ^ (string_of_int i)
49 | Stack.Closed i -> "<S>?" ^ (string_of_int i) ^ "</S>"
51 let int_of_switch = function
52 | Stack.Open i | Stack.Closed i -> i
54 let sequent = function
56 let meta = List.assoc i metasenv in
57 snd (ApplyTransformation.ntxt_of_cic_sequent
58 ~metasenv ~subst ~map_unicode_to_tex:false 80 s (i,meta))
59 | Stack.Closed _ -> "This goal has already been closed."
61 let render_sequent is_loc acc depth tag (pos,sw) =
62 let metano = int_of_switch sw in
65 (match depth, pos with
66 | 0, 0 -> "<B>" ^ (render_switch sw) ^ "</B> "
68 Printf.sprintf "<B>|<SUB>%d</SUB>: %s</B>" pos (render_switch sw)
69 | 1, pos when Stack.head_tag s#stack = `BranchTag ->
70 Printf.sprintf "|<SUB>%d</SUB> : %s" pos (render_switch sw)
71 | _ -> render_switch sw)
74 let markup = Netencoding.Url.encode ~plus:false markup in
75 let txt0 = Netencoding.Url.encode ~plus:false (sequent sw) in
76 (string_of_int metano ^ "|" ^ markup ^ "|" ^ txt0 ^ "#" ^ acc)
79 ~env:(render_sequent true) ~cont:(render_sequent false)
80 ~todo:(render_sequent false) "" s#stack
83 (* let html_of_status s =
84 let _,_,metasenv,subst,_ = s#obj in
85 let txt = List.fold_left
86 (fun acc (nmeta,_ as meta) ->
87 let txt0 = snd (ApplyTransformation.ntxt_of_cic_sequent
88 ~metasenv ~subst ~map_unicode_to_tex:false 80 s meta)
90 prerr_endline ("### txt0 = " ^ txt0);
91 ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
94 String.concat "\n\n" txt
97 let advance text (* (?bos=false) *) =
98 let (st,new_statements),(* newtext TODO *) _,parsed_len =
100 eval_statement include_paths (*buffer*) !status (`Raw text)
101 (* with End_of_file -> raise Margin *)
104 history := st :: !history;
105 let txt = output_status !status in
110 let new_history,new_status =
112 _::(status::_ as history) ->
114 | [_] -> failwith "retract"
115 | _ -> assert false in
116 NCicLibrary.time_travel !status;
117 history := new_history;
118 status := new_status;
119 output_status !status
122 let read_file fname =
123 let chan = open_in fname in
124 let lines = ref [] in
127 lines := input_line chan :: !lines
129 with End_of_file -> close_in chan);
130 String.concat "\r\n" (List.rev !lines)
133 let load_index outchan =
134 let s = read_file "index.html" in
135 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
138 let load_doc filename outchan =
139 let s = read_file filename in
140 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
143 let call_service outchan =
145 (ignore(MatitaEngine.assert_ng
146 ~include_paths:["/home/barolo/matitaB/matita/lib"] (* ~outch:outchan *)
147 "/home/barolo/matitaB/matita/lib/basics/pts.ma");
148 prerr_endline "fatto";
149 let s = read_file "/home/barolo/matitaB/matita/lib/basics/pts.ma.mad"
151 Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
154 e -> Http_daemon.respond ~code:(`Code 500) outchan
158 let callback req outchan =
160 (sprintf "request path = %s\n" req#path) ^
161 (sprintf "request GET params = %s\n"
163 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_GET))) ^
164 (sprintf "request POST params = %s\n"
166 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_POST))) ^
167 (sprintf "request ALL params = %s\n"
169 (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
170 (sprintf "cookies = %s\n"
171 (match req#cookies with
174 ^ (if req#hasHeader ~name:"cookie"
175 then "('Cookie:' header was '" ^ req#header ~name:"cookie" ^ "')"
176 else "(No 'Cookie:' header received)")
179 (List.map (fun (n,v) -> String.concat "=" [n;v]) cookies)))) ^
180 (sprintf "request BODY = '%s'\n\n" req#body)
182 (* Http_daemon.respond ~code:(`Code 200) ~body: str outchan *)
187 | "/" -> load_index outchan
188 | "/matita" -> call_service outchan
190 prerr_endline "getting 'file' argument";
191 let filename = List.assoc "file" req#params_GET in
192 prerr_endline ("reading file " ^ filename);
193 let body = read_file filename in
195 Librarian.baseuri_of_script ~include_paths:[] filename
197 status := (!status)#set_baseuri baseuri;
198 Http_daemon.respond ~code:(`Code 200) ~body outchan
200 let script = req#body in
201 prerr_endline ("body length = " ^ (string_of_int (String.length script)));
202 let (parsed_len,txt), res, code =
203 try advance script, "OK", `Code 200
205 | HExtlib.Localized(_,e)
207 (prerr_endline ("exception: " ^ Printexc.to_string e);
209 NTacStatus.pp_tac_status !status
210 with e -> prerr_endline ("inner exception: " ^
211 Printexc.to_string e));
212 prerr_endline "end status";
213 let txt = output_status !status in
214 (0,txt), Printexc.to_string e, `Code 500)
216 prerr_endline ("server response:\n" ^ txt);
217 let body = (string_of_int parsed_len) ^ "@" ^ txt in
218 Http_daemon.respond ~code ~body outchan
221 let body = retract () in
222 Http_daemon.respond ~code:(`Code 200) ~body outchan
224 (prerr_endline (Printexc.to_string e);
225 Http_daemon.respond ~code:(`Code 500) outchan))
228 let url = String.sub url 1 (String.length url - 1) in
230 with _ -> Http_daemon.respond_not_found ~url outchan
234 { Http_daemon.default_spec with
242 MatitaInit.initialize_all ();
243 Http_daemon.main spec