]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitadaemon.ml
Preliminary version of matitaweb handling multiple goals in the sequent view.
[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 (* lista [meta n., goal; meta n., goal; ... ] *)
41 (* "item1#item2#...#" *)
42
43 let output_status s =
44   let _,_,metasenv,subst,_ = s#obj in
45   List.fold_left
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)
49        in
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)
54     "" metasenv
55 ;;
56
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)
63        in
64        prerr_endline ("### txt0 = " ^ txt0);
65       ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
66     [] metasenv
67   in
68   String.concat "\n\n" txt
69 ;; *)
70
71 let advance text (* (?bos=false) *) =
72      let (st,new_statements),(* newtext TODO *) _,parsed_len =
73        (* try *)
74          eval_statement include_paths (*buffer*) !status (`Raw text)
75        (* with End_of_file -> raise Margin *)
76      in
77      status := st;
78      history := st :: !history;
79      let txt = output_status !status in
80      parsed_len, txt
81 ;;
82
83 let retract () =
84   let new_history,new_status =
85      match !history with
86        _::(status::_ as history) ->
87         history, status
88     | [_] -> failwith "retract"
89     | _ -> assert false in
90   NCicLibrary.time_travel !status;
91   history := new_history;
92   status := new_status;
93   output_status !status
94 ;;
95
96 let read_file fname =
97   let chan = open_in fname in
98   let lines = ref [] in
99   (try
100      while true do
101        lines := input_line chan :: !lines
102      done;
103    with End_of_file -> close_in chan);
104   String.concat "\r\n" (List.rev !lines)
105 ;;
106
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
110 ;;
111
112 let call_service outchan =
113   try 
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"
119     in
120     Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
121    )
122   with
123   e -> Http_daemon.respond ~code:(`Code 500) outchan
124 ;;
125
126
127 let callback req outchan =
128   let str = 
129     (sprintf "request path = %s\n"  req#path) ^
130     (sprintf "request GET params = %s\n"
131       (String.concat ";"
132         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_GET))) ^
133     (sprintf "request POST params = %s\n"
134       (String.concat ";"
135         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_POST))) ^
136     (sprintf "request ALL params = %s\n"
137       (String.concat ";"
138         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
139     (sprintf "cookies = %s\n"
140       (match req#cookies with
141       | None ->
142           "NO COOKIES "
143           ^ (if req#hasHeader ~name:"cookie"
144              then "('Cookie:' header was '" ^ req#header ~name:"cookie" ^ "')"
145              else "(No 'Cookie:' header received)")
146       | Some cookies ->
147           (String.concat ";"
148             (List.map (fun (n,v) -> String.concat "=" [n;v]) cookies)))) ^
149     (sprintf "request BODY = '%s'\n\n" req#body)
150   in
151   (* Http_daemon.respond ~code:(`Code 200) ~body: str outchan *)
152
153   prerr_endline str;
154
155   match req#path with
156   | "/" -> load_index outchan
157   | "/matita" -> call_service outchan
158   | "/open" ->
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
163       let _,baseuri,_,_ = 
164         Librarian.baseuri_of_script ~include_paths:[] filename
165       in
166       status := (!status)#set_baseuri baseuri;
167       Http_daemon.respond ~code:(`Code 200) ~body outchan
168   | "/advance" ->
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
173         with 
174         | HExtlib.Localized(_,e) 
175         | e -> 
176                 (prerr_endline ("exception: " ^ Printexc.to_string e);
177                 (try 
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)
184       in
185       prerr_endline ("server response:\n" ^ txt);
186       let body = (string_of_int parsed_len) ^ "@" ^ txt in
187       Http_daemon.respond ~code ~body outchan
188   | "/retract" ->
189       (try
190         let body = retract () in
191         Http_daemon.respond ~code:(`Code 200) ~body outchan
192        with e -> 
193         (prerr_endline (Printexc.to_string e);
194          Http_daemon.respond ~code:(`Code 500) outchan))
195   | url -> Http_daemon.respond_not_found ~url outchan  
196
197 ;;
198
199 let spec =
200   { Http_daemon.default_spec with
201       callback = callback;
202       port = 9999;
203       mode = `Single;
204   }
205 ;;
206
207 let _ =
208   MatitaInit.initialize_all ();
209   Http_daemon.main spec
210 ;;