]> matita.cs.unibo.it Git - helm.git/blob - matitaB/matita/matitadaemon.ml
d1a7e207226d759d1992adee1ec80c0fe21a752f
[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
37 let include_paths = ["/home/barolo/matitaB/matita/lib"];;
38
39 let advance text (* (?bos=false) *) =
40    (* if bos then LibraryClean.clean_baseuris [self#buri_of_current_file];
41    (* HLog.debug ("evaluating: " ^ first_line s ^ " ...");*)
42    let time1 = Unix.gettimeofday () in
43    let entries, newtext, parsed_len = *)
44      let (st,_),_,parsed_len =
45        (* try *)
46          eval_statement include_paths (*buffer*) !status (`Raw text)
47        (* with End_of_file -> raise Margin *)
48      in
49      status := st; 
50      let _,_,metasenv,subst,_ = !status#obj in
51      let txt = List.fold_left 
52        (fun acc (nmeta,_ as meta) ->
53           let txt0 = snd (ApplyTransformation.ntxt_of_cic_sequent 
54             ~metasenv ~subst ~map_unicode_to_tex:false 80 !status
55            meta) in
56           prerr_endline ("### txt0 = " ^ txt0);
57          ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
58        [] metasenv
59      in
60      let txt = String.concat "\n\n" txt in
61      parsed_len, txt
62    (*in
63    let time2 = Unix.gettimeofday () in
64    HLog.debug ("... done in " ^ string_of_float (time2 -. time1) ^ "s");
65    let new_statuses, new_statements =
66      let statuses, texts = List.split entries in
67      statuses, texts
68    in
69    history <- new_statuses @ history;
70    statements <- new_statements @ statements;
71    let start = buffer#get_iter_at_mark (`MARK locked_mark) in
72    let new_text = String.concat "" (List.rev new_statements) in
73    if statement <> None then
74      buffer#insert ~iter:start new_text
75    else begin
76      let parsed_text = String.sub s 0 parsed_len in
77      if new_text <> parsed_text then begin
78        let stop = start#copy#forward_chars (Glib.Utf8.length parsed_text) in
79        buffer#delete ~start ~stop;
80        buffer#insert ~iter:start new_text;
81      end;
82    end;
83    self#moveMark (Glib.Utf8.length new_text);
84    buffer#insert ~iter:(buffer#get_iter_at_mark (`MARK locked_mark)) newtext*)
85 ;;
86
87 let read_file fname =
88   let chan = open_in fname in
89   let lines = ref [] in
90   (try
91      while true do
92        lines := input_line chan :: !lines
93      done;
94    with End_of_file -> close_in chan);
95   String.concat "\r\n" (List.rev !lines)
96 ;;
97
98 let load_index outchan =
99   let s = read_file "index.html" in
100   Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
101 ;;
102
103 let call_service outchan =
104   try 
105    (ignore(MatitaEngine.assert_ng 
106      ~include_paths:["/home/barolo/matitaB/matita/lib"] (* ~outch:outchan *)
107     "/home/barolo/matitaB/matita/lib/basics/pts.ma");
108     prerr_endline "fatto";
109     let s = read_file "/home/barolo/matitaB/matita/lib/basics/pts.ma.mad"
110     in
111     Http_daemon.respond ~headers:["Content-Type", "text/html"] ~code:(`Code 200) ~body:s outchan
112    )
113   with
114   e -> Http_daemon.respond ~code:(`Code 500) outchan
115 ;;
116
117 let callback req outchan =
118   let str = 
119     (sprintf "request path = %s\n"  req#path) ^
120     (sprintf "request GET params = %s\n"
121       (String.concat ";"
122         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_GET))) ^
123     (sprintf "request POST params = %s\n"
124       (String.concat ";"
125         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params_POST))) ^
126     (sprintf "request ALL params = %s\n"
127       (String.concat ";"
128         (List.map (fun (h,v) -> String.concat "=" [h;v]) req#params))) ^
129     (sprintf "cookies = %s\n"
130       (match req#cookies with
131       | None ->
132           "NO COOKIES "
133           ^ (if req#hasHeader ~name:"cookie"
134              then "('Cookie:' header was '" ^ req#header ~name:"cookie" ^ "')"
135              else "(No 'Cookie:' header received)")
136       | Some cookies ->
137           (String.concat ";"
138             (List.map (fun (n,v) -> String.concat "=" [n;v]) cookies)))) ^
139     (sprintf "request BODY = '%s'\n\n" req#body)
140   in
141   (* Http_daemon.respond ~code:(`Code 200) ~body: str outchan *)
142
143   prerr_endline str;
144
145   match req#path with
146   | "/" -> load_index outchan
147   | "/matita" -> call_service outchan
148   | "/open" ->
149       prerr_endline "getting 'file' argument";
150       let filename = List.assoc "file" req#params_GET in
151       prerr_endline ("reading file " ^ filename);
152       let body = read_file filename in
153       let _,baseuri,_,_ = 
154         Librarian.baseuri_of_script ~include_paths:[] filename
155       in
156       status := (!status)#set_baseuri baseuri;
157       Http_daemon.respond ~code:(`Code 200) ~body outchan
158   | "/advance" ->
159       let script = req#body in
160       prerr_endline ("body length = " ^ (string_of_int (String.length script)));
161       let (parsed_len,txt), res, code =
162         try advance script, "OK", `Code 200
163         with 
164         | HExtlib.Localized(_,e) 
165         | e -> 
166                 (prerr_endline ("exception: " ^ Printexc.to_string e);
167                 (try 
168                   NTacStatus.pp_tac_status !status
169                 with e -> prerr_endline ("inner exception: " ^
170                   Printexc.to_string e));
171                 prerr_endline "end status";
172                 let _,_,metasenv,subst,_ = !status#obj in
173                 let txt = List.fold_left 
174                   (fun acc (nmeta,_ as meta) ->
175                      let txt0 = snd (ApplyTransformation.ntxt_of_cic_sequent 
176                        ~metasenv ~subst ~map_unicode_to_tex:false 80 !status
177                       meta) in
178                      prerr_endline ("### txt0 = " ^ txt0);
179                     ("<B>Goal ?" ^ (string_of_int nmeta) ^ "</B>\n" ^ txt0)::acc)
180                   [] metasenv
181                 in
182                 let txt = String.concat "\n\n" txt in
183                 (0,txt), Printexc.to_string e, `Code 500)
184       in
185       let txt = Netencoding.Url.encode ~plus:false txt in
186       let body = (string_of_int parsed_len) ^ "#" ^ txt in
187       Http_daemon.respond ~code ~body outchan
188   | url -> Http_daemon.respond_not_found ~url outchan  
189
190 ;;
191
192
193
194 let spec =
195   { Http_daemon.default_spec with
196       callback = callback;
197       port = 9999;
198       mode = `Single;
199   }
200 ;;
201
202 let _ =
203   MatitaInit.initialize_all ();
204   Http_daemon.main spec
205 ;;