]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitadaemon.ml
matitaweb: added retract (undo)
[helm.git] / matitaB / matita / matitadaemon.ml
1 open Printf;;
2 open Http_types;;
3
4 (*** from matitaScript.ml ***)
5 (* let only_dust_RE = Pcre.regexp "^(\\s|\n|%%[^\n]*\n)*$" *)
6
7 let eval_statement include_paths (* (buffer : GText.buffer) *) status (* script *)
8  statement
9 =
10   let ast,unparsed_text =
11     match statement with
12     | `Raw text ->
13         (* if Pcre.pmatch ~rex:only_dust_RE text then raise Margin; *)
14         let strm =
15          GrafiteParser.parsable_statement status
16           (Ulexing.from_utf8_string text) in
17         let ast = MatitaEngine.get_ast status include_paths strm in
18          ast, text
19     | `Ast (st, text) -> st, text
20   in
21   let floc = match ast with
22   | GrafiteAst.Executable (loc, _)
23   | GrafiteAst.Comment (loc, _) -> loc in
24   
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
28   
29   let status = 
30     MatitaEngine.eval_ast ~include_paths ~do_heavy_checks:false status ("",0,ast)
31   in 
32   (status, parsed_text),"",(*parsed_text_len*) Glib.Utf8.length parsed_text
33
34
35 let status = ref (new MatitaEngine.status "cic:/matita");;
36 let history = ref [!status];;
37
38 let include_paths = ["/home/barolo/matitaB/matita/lib"];;
39
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)
46        in
47        prerr_endline ("### txt0 = " ^ txt0);
48       ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
49     [] metasenv
50   in
51   String.concat "\n\n" txt
52 ;;
53
54 let advance text (* (?bos=false) *) =
55      let (st,new_statements),(* newtext TODO *) _,parsed_len =
56        (* try *)
57          eval_statement include_paths (*buffer*) !status (`Raw text)
58        (* with End_of_file -> raise Margin *)
59      in
60      status := st;
61      history := st :: !history;
62      let txt = html_of_status !status in
63      parsed_len, txt
64 ;;
65
66 let retract () =
67   let new_history,new_status =
68      match !history with
69        _::(status::_ as history) ->
70         history, status
71     | [_] -> failwith "retract"
72     | _ -> assert false in
73   NCicLibrary.time_travel !status;
74   history := new_history;
75   status := new_status;
76   html_of_status !status
77 ;;
78
79 let read_file fname =
80   let chan = open_in fname in
81   let lines = ref [] in
82   (try
83      while true do
84        lines := input_line chan :: !lines
85      done;
86    with End_of_file -> close_in chan);
87   String.concat "\r\n" (List.rev !lines)
88 ;;
89
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
93 ;;
94
95 let call_service outchan =
96   try 
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"
102     in
103     Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
104    )
105   with
106   e -> Http_daemon.respond ~code:(`Code 500) outchan
107 ;;
108
109
110 let callback req outchan =
111   let str = 
112     (sprintf "request path = %s\n"  req#path) ^
113     (sprintf "request GET params = %s\n"
114       (String.concat ";"
115         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_GET))) ^
116     (sprintf "request POST params = %s\n"
117       (String.concat ";"
118         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_POST))) ^
119     (sprintf "request ALL params = %s\n"
120       (String.concat ";"
121         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
122     (sprintf "cookies = %s\n"
123       (match req#cookies with
124       | None ->
125           "NO COOKIES "
126           ^ (if req#hasHeader ~name:"cookie"
127              then "('Cookie:' header was '" ^ req#header ~name:"cookie" ^ "')"
128              else "(No 'Cookie:' header received)")
129       | Some cookies ->
130           (String.concat ";"
131             (List.map (fun (n,v) -> String.concat "=" [n;v]) cookies)))) ^
132     (sprintf "request BODY = '%s'\n\n" req#body)
133   in
134   (* Http_daemon.respond ~code:(`Code 200) ~body: str outchan *)
135
136   prerr_endline str;
137
138   match req#path with
139   | "/" -> load_index outchan
140   | "/matita" -> call_service outchan
141   | "/open" ->
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
146       let _,baseuri,_,_ = 
147         Librarian.baseuri_of_script ~include_paths:[] filename
148       in
149       status := (!status)#set_baseuri baseuri;
150       Http_daemon.respond ~code:(`Code 200) ~body outchan
151   | "/advance" ->
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
156         with 
157         | HExtlib.Localized(_,e) 
158         | e -> 
159                 (prerr_endline ("exception: " ^ Printexc.to_string e);
160                 (try 
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)
167       in
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
171   | "/retract" ->
172       (try
173         let txt = retract () in
174         Http_daemon.respond ~code:(`Code 200) ~body:txt outchan
175        with e -> 
176         (prerr_endline (Printexc.to_string e);
177          Http_daemon.respond ~code:(`Code 500) outchan))
178   | url -> Http_daemon.respond_not_found ~url outchan  
179
180 ;;
181
182
183
184 let spec =
185   { Http_daemon.default_spec with
186       callback = callback;
187       port = 9999;
188       mode = `Single;
189   }
190 ;;
191
192 let _ =
193   MatitaInit.initialize_all ();
194   Http_daemon.main spec
195 ;;