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