why3session_html.ml 18 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
(**************************************************************************)
(*                                                                        *)
(*  Copyright (C) 2010-2011                                               *)
(*    François Bobot                                                      *)
(*    Jean-Christophe Filliâtre                                           *)
(*    Claude Marché                                                       *)
(*    Andrei Paskevich                                                    *)
(*                                                                        *)
(*  This software is free software; you can redistribute it and/or        *)
(*  modify it under the terms of the GNU Library General Public           *)
(*  License version 2.1, with the special exception on linking            *)
(*  described in file LICENSE.                                            *)
(*                                                                        *)
(*  This software is distributed in the hope that it will be useful,      *)
(*  but WITHOUT ANY WARRANTY; without even the implied warranty of        *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *)
(*                                                                        *)
(**************************************************************************)

20
open Format
21
open Why3
22
open Why3session_lib
23

24 25
module S = Session

26
let output_dir = ref ""
27
let opt_context = ref false
28

29
type style = SimpleTree | Jstree | Table
30

31 32
let opt_style = ref Table
let default_style = "table"
33 34

let set_opt_style = function
35
  | "simple" -> opt_style := SimpleTree
36
  | "jstree" -> opt_style := Jstree
37 38 39 40 41
  | "table" -> opt_style := Table
  | _ -> 
    eprintf "Unknown html style, ignored@."

let () = set_opt_style default_style
42

43 44 45 46 47 48 49 50 51
let opt_pp = ref []

let set_opt_pp_in,set_opt_pp_cmd,set_opt_pp_out =
  let suf = ref "" in
  let cmd = ref "" in
  (fun s -> suf := s),
  (fun s -> cmd := s),
  (fun s -> opt_pp := (!suf,(!cmd,s))::!opt_pp)

52
let spec =
53 54
  ("-o",
   Arg.Set_string output_dir,
55 56 57
   " The directory to output the files ('-' for stdout)") ::
  ("--context", Arg.Set opt_context,
   " Add context around the generated code in order to allow direct \
58
    visualisation (header, css, ...). It also add in the directory \
59
    all the needed external file. It can't be set with stdout output") ::
60 61 62 63 64
  ("--style", Arg.Symbol (["simpletree";"jstree";"table"], set_opt_style),
   " Set the style to use, defaults to '" ^ default_style ^ "'."
(* "'simple' use only 'ul' and 'il' tag. 'jstree' use \
    the 'jstree' plugin of the javascript library 'jquery'." *)
) ::
65
  ("--add_pp", Arg.Tuple
66 67 68 69 70 71
    [Arg.String set_opt_pp_in;
     Arg.String set_opt_pp_cmd;
     Arg.String set_opt_pp_out],
  "<suffix> <cmd> <out_suffix> \
Add for the given prefix the given pretty-printer, \
the new file as the given out_suffix. cmd must contain '%i' which will be \
72 73 74
replace by the input file and '%o' which will be replaced by the output file.") ::
  ("--coqdoc",
   Arg.Unit (fun ()->
75
    opt_pp := (".v",("coqdoc --no-index --html -o %o %i",".html"))::!opt_pp),
76
  " same as '--add_pp .v \"coqdoc --no-index --html -o %o %i\" .html'") ::
77
  common_options
78

79
(*
80
let version_msg = sprintf
81 82 83
  "Why3 html session output, version %s (build date: %s)"
  Config.version Config.builddate

84
let usage_str = sprintf
85 86 87 88 89 90 91 92 93
  "Usage: %s [options] [<file.why>|<project directory>] ... "
  (Filename.basename Sys.argv.(0))

let set_file f = Queue.push f files

let () = Arg.parse spec set_file usage_str

let () =
  if !opt_version then begin
94
    printf "%s@." version_msg;
95 96
    exit 0
  end
97
*)
98

99 100 101 102
(* let () = *)
(*   List.iter (fun (in_,(cmd,out)) -> *)
(*     printf "in : %s, cmd : %s, out : %s@." in_ cmd out) !opt_pp *)

103
(*
104 105 106
let () =
  Debug.Opt.set_flags_selected ();
  if  Debug.Opt.option_list () then exit 0
107
*)
108

109
(*
110
let output_dir =
111
  match !output_dir with
112
   | "" ->
113
      printf "Error: output_dir must be set@.";
114 115 116 117 118 119
      exit 1
    | "-" when !opt_context ->
      printf
        "Error: context and stdout output can't be set at the same time@.";
      exit 1
    | _ -> !output_dir
120

121 122
let edited_dst = Filename.concat output_dir "edited"

François Bobot's avatar
François Bobot committed
123
let whyconf = Whyconf.read_config !opt_config
124
  *)
François Bobot's avatar
François Bobot committed
125 126

open Session
127 128
open Util

129
type context =
130
    (string ->
François Bobot's avatar
François Bobot committed
131
     (formatter -> notask session -> unit) -> notask session
132 133
     -> unit, formatter, unit) format

134 135 136 137 138 139 140 141 142 143 144
let run_file (context : context) print_session fname =
  let project_dir = Session.get_project_dir fname in
  let session = Session.read_session project_dir in
  let output_dir =
    if !output_dir = "" then project_dir else !output_dir
  in
  let basename = Filename.basename project_dir in
  let cout =
    if output_dir = "-" then stdout else
      open_out (Filename.concat output_dir (basename^".html"))
  in
145 146 147 148 149 150 151 152
  let fmt = formatter_of_out_channel cout in
  if !opt_context
  then fprintf fmt context basename (print_session basename) session
  else print_session basename fmt session;
  pp_print_flush fmt ();
  if output_dir <> "-" then close_out cout


153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
module Table =
struct


  let rec transf_depth tr =
    List.fold_left
      (fun depth g -> max depth (goal_depth g)) 0 tr.S.transf_goals
  and goal_depth g =
    S.PHstr.fold
      (fun _st tr depth -> max depth (1 + transf_depth tr))
      g.S.goal_transformations 1

  let theory_depth t =
    List.fold_left
      (fun depth g -> max depth (goal_depth g)) 0 t.S.theory_goals

  let rec provers_stats provers theory =
    S.theory_iter_proof_attempt (fun a ->
      Hashtbl.replace provers a.S.proof_prover a.S.proof_prover) theory

  let print_prover = Whyconf.print_prover


  let color_of_status ?(dark=false) fmt b = 
    fprintf fmt "%s" (if b then 
        if dark then "008000" else "C0FFC0" 
      else "FF0000")

let print_results fmt provers proofs =
  List.iter (fun p ->
    fprintf fmt "<td bgcolor=\"#";
    begin
      try
        let pr = S.PHprover.find proofs p in
        let s = pr.S.proof_state in
        match s with
	  | S.Done res ->
	    begin
	      match res.Call_provers.pr_answer with
		| Call_provers.Valid ->
                  fprintf fmt "C0FFC0\">%.2f" res.Call_provers.pr_time
		| Call_provers.Invalid ->
                  fprintf fmt "FF0000\">Invalid"
		| Call_provers.Timeout ->
                  fprintf fmt "FF8000\">Timeout"
		| Call_provers.Unknown _ ->
MARCHE Claude's avatar
MARCHE Claude committed
199
                  fprintf fmt "FF8000\">%.2f" res.Call_provers.pr_time
200
		| _ ->
MARCHE Claude's avatar
MARCHE Claude committed
201
                  fprintf fmt "FF8000\">Failure "
202
	    end
MARCHE Claude's avatar
MARCHE Claude committed
203 204
	  | _ -> fprintf fmt "E0E0E0\">Undone"
      with Not_found -> fprintf fmt "E0E0E0\">---"
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
    end;
    fprintf fmt "</td>") provers

  let rec print_transf fmt depth max_depth provers tr =
    fprintf fmt "<tr>";
    for i=1 to 0 (* depth-1 *) do fprintf fmt "<td></td>" done;
    fprintf fmt "<td bgcolor=\"#%a\">" (color_of_status ~dark:false) tr.S.transf_verified;
    for i=1 to depth-1 do fprintf fmt "&nbsp;&nbsp;&nbsp;&nbsp;" done;
    fprintf fmt "%s</td>" tr.transf_name;
    for i=1 (* depth *) to (*max_depth - 1 + *) List.length provers do
      fprintf fmt "<td bgcolor=\"#E0E0E0\"></td>"
    done;
    fprintf fmt "</tr>@\n";
    List.iter
      (print_goal fmt (depth+1) max_depth provers)
      tr.transf_goals

  and print_goal fmt depth max_depth provers g =
    fprintf fmt "<tr>";
    for i=1 to 0 (* depth-1 *) do fprintf fmt "<td></td>" done;
    fprintf fmt "<td bgcolor=\"#%a\">" (color_of_status ~dark:false) g.S.goal_verified;
    for i=1 to depth-1 do fprintf fmt "&nbsp;&nbsp;&nbsp;&nbsp;" done;
    fprintf fmt "%s</td>" (S.goal_expl g);
(*    for i=depth to max_depth-1 do fprintf fmt "<td></td>" done; *)
    print_results fmt provers g.goal_external_proofs;
    fprintf fmt "</tr>@\n";
    PHstr.iter
      (fun _ tr -> print_transf fmt depth max_depth provers tr)
      g.goal_transformations

  let print_theory fmt th =
    let depth = theory_depth th in
    if depth > 0 then
    let provers = Hashtbl.create 9 in
    provers_stats provers th;
    let provers =
      Hashtbl.fold (fun _ pr acc -> pr :: acc) provers []
    in
    let provers = List.sort Whyconf.Prover.compare provers in
    let name = th.S.theory_name.Ident.id_string in
    fprintf fmt "<h2><font color=\"#%a\">Theory \"%s\": %s</font></h2>@\n" 
      (color_of_status ~dark:true) th.S.theory_verified
      name (if th.S.theory_verified then "fully verified" else "not fully verified")
    ;
(*
    fprintf fmt "<table border=\"1\"><tr><td colspan=\"%d\">Obligations</td>" depth;
*)
    fprintf fmt "<table border=\"1\"><tr><td>Obligations</td>";
    List.iter
      (fun pr -> fprintf fmt "<td text-rotation=\"90\">%a</td>" print_prover pr)
      provers;
    fprintf fmt "</td></tr>@\n";
    List.iter (print_goal fmt 1 depth provers) th.theory_goals;
    fprintf fmt "</table>@\n"

  let print_file fmt f =
    (* fprintf fmt "<h1>File %s</h1>@\n" f.file_name; *)
    fprintf fmt "%a"
      (Pp.print_list Pp.newline print_theory) f.file_theories

  let print_session name fmt s =
    fprintf fmt "<h1>Why3 Proof Results for Project \"%s\"</h1>@\n" name;
    fprintf fmt "%a"
      (Pp.print_iter2 PHstr.iter Pp.newline Pp.nothing Pp.nothing
         print_file) s.session_files


  let context : context = "<!DOCTYPE html
PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
<title>Why3 session of %s</title>
</head>
<body>
%a
</body>
</html>
"

  let run_one = run_file context print_session

end



291 292 293
module Simple =
struct

294
  let print_prover = Whyconf.print_prover
295 296

  let print_proof_status fmt = function
François Bobot's avatar
François Bobot committed
297
    | Undone _ -> fprintf fmt "Undone"
298 299 300
    | Done pr -> fprintf fmt "Done : %a" Call_provers.print_prover_result pr
    | InternalFailure exn ->
      fprintf fmt "Failure : %a"Exn_printer.exn_printer exn
301

302 303
  let print_proof_attempt fmt pa =
    fprintf fmt "<li>%a : %a</li>"
François Bobot's avatar
François Bobot committed
304
      print_prover pa.proof_prover
305 306 307 308 309
      print_proof_status pa.proof_state

  let rec print_transf fmt tr =
    fprintf fmt "<li>%s : <ul>%a</ul></li>"
      tr.transf_name
François Bobot's avatar
François Bobot committed
310
      (Pp.print_list Pp.newline print_goal) tr.transf_goals
311 312 313

  and print_goal fmt g =
    fprintf fmt "<li>%s : <ul>%a%a</ul></li>"
François Bobot's avatar
François Bobot committed
314 315
      g.goal_name.Ident.id_string
      (Pp.print_iter2 PHprover.iter Pp.newline Pp.nothing
316
         Pp.nothing print_proof_attempt)
François Bobot's avatar
François Bobot committed
317 318
      g.goal_external_proofs
      (Pp.print_iter2 PHstr.iter Pp.newline Pp.nothing
319
         Pp.nothing print_transf)
François Bobot's avatar
François Bobot committed
320
      g.goal_transformations
321 322 323

  let print_theory fmt th =
    fprintf fmt "<li>%s : <ul>%a</ul></li>"
François Bobot's avatar
François Bobot committed
324 325
      th.theory_name.Ident.id_string
      (Pp.print_list Pp.newline print_goal) th.theory_goals
326 327 328 329

  let print_file fmt f =
    fprintf fmt "<li>%s : <ul>%a</ul></li>"
      f.file_name
François Bobot's avatar
François Bobot committed
330
      (Pp.print_list Pp.newline print_theory) f.file_theories
331 332 333

  let print_session _name fmt s =
    fprintf fmt "<ul>%a</ul>"
François Bobot's avatar
François Bobot committed
334 335
      (Pp.print_iter2 PHstr.iter Pp.newline Pp.nothing Pp.nothing
         print_file) s.session_files
336 337 338


  let context : context = "<!DOCTYPE html
339 340 341 342
PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
343
<title>Why3 session of %s</title>
344 345 346 347 348 349
</head>
<body>
%a
</body>
</html>
"
350

351
  let run_one = run_file context print_session
352

353
end
354

355 356
(*

357 358 359
module Jstree =
struct

360 361 362 363 364
  let print_verified fmt b =
    if b
    then fprintf fmt "class='verified'"
    else fprintf fmt "class='notverified'"

365
  let print_prover = Whyconf.print_prover
366 367

  let print_proof_status fmt = function
François Bobot's avatar
François Bobot committed
368
    | Undone _ -> fprintf fmt "<span class='notverified'>Undone</span>"
369 370
    | Done pr -> fprintf fmt "<span class='verified'>Done : %a</span>"
      Call_provers.print_prover_result pr
371
    | InternalFailure exn ->
372 373
      fprintf fmt "<span class='notverified'>Failure : %a</span>"
        Exn_printer.exn_printer exn
374

375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
  let cmd_regexp = Str.regexp "%\\(.\\)"

  let pp_run input cmd output =
    let replace s = match Str.matched_group 1 s with
      | "%" -> "%"
      | "i" -> input
      | "o" -> output
      | _ -> failwith "unknown format specifier, use %%i, %%o"
    in
    let cmd = Str.global_substitute cmd_regexp replace cmd in
    let c = Sys.command cmd in
    if c <> 0 then
      eprintf "[Error] '%s' stopped abnormaly : code %i" cmd c

  let find_pp edited =
    let basename = Filename.basename edited in
    try
      let suff,(cmd,suff_out) =
        List.find (fun (s,_) -> ends_with basename s) !opt_pp in
      let base =
        String.sub basename 0 (String.length basename - String.length suff) in
      let base_dst = (base^suff_out) in
      if !opt_context then
        pp_run edited cmd (Filename.concat edited_dst base_dst);
      base_dst
    with Not_found ->
      eprintf "Default %s@." basename;
      (** default printer *)
      let base = try Filename.chop_extension basename with _ -> basename in
      let base_dst = base^".txt" in
      if !opt_context then
        Sysutil.copy_file edited (Filename.concat edited_dst base_dst);
      base_dst

409
  let print_proof_attempt fmt pa =
François Bobot's avatar
François Bobot committed
410
    match pa.proof_edited_as with
411 412
      | None ->
        fprintf fmt "@[<hov><li rel='prover' ><a href='#'>%a : %a</a></li>@]"
François Bobot's avatar
François Bobot committed
413
          print_prover pa.proof_prover
414 415 416 417 418 419
          print_proof_status pa.proof_state
      | Some f ->
        let output = find_pp f in
        fprintf fmt "@[<hov><li rel='prover' ><a href='#' \
onclick=\"showedited('%s'); return false;\">%a : %a</a></li>@]"
          output
François Bobot's avatar
François Bobot committed
420
          print_prover pa.proof_prover
421
          print_proof_status pa.proof_state
422 423 424

  let rec print_transf fmt tr =
    fprintf fmt
425 426 427
      "@[<hov><li rel='transf'><a href='#'>\
<span %a>%s</span></a>@,<ul>%a</ul></li>@]"
      print_verified tr.transf_verified
428
      tr.transf_name
François Bobot's avatar
François Bobot committed
429
      (Pp.print_list Pp.newline print_goal) tr.transf_goals
430 431 432

  and print_goal fmt g =
    fprintf fmt
433 434 435
      "@[<hov><li rel='goal'><a href='#'>\
<span %a>%s</a></a>@,<ul>%a%a</ul></li>@]"
      print_verified g.goal_verified
François Bobot's avatar
François Bobot committed
436 437
      g.goal_name.Ident.id_string
      (Pp.print_iter2 PHprover.iter Pp.newline Pp.nothing
438
         Pp.nothing print_proof_attempt)
François Bobot's avatar
François Bobot committed
439 440
      g.goal_external_proofs
      (Pp.print_iter2 PHstr.iter Pp.newline Pp.nothing
441
         Pp.nothing print_transf)
François Bobot's avatar
François Bobot committed
442
      g.goal_transformations
443 444 445

  let print_theory fmt th =
    fprintf fmt
446 447 448
      "@[<hov><li rel='theory'><a href='#'>\
<span %a>%s</span></a>@,<ul>%a</ul></li>@]"
      print_verified th.theory_verified
François Bobot's avatar
François Bobot committed
449 450
      th.theory_name.Ident.id_string
      (Pp.print_list Pp.newline print_goal) th.theory_goals
451 452

  let print_file fmt f =
453 454 455 456
    fprintf fmt
      "@[<hov><li rel='file'><a href='#'>\
<span %a>%s</span></a>@,<ul>%a</ul></li>@]"
      print_verified f.file_verified
457
      f.file_name
François Bobot's avatar
François Bobot committed
458
      (Pp.print_list Pp.newline print_theory) f.file_theories
459 460 461 462

  let print_session_aux name fmt s =
    fprintf fmt "@[<hov><ul><a href='#'>%s</a>@,%a</ul>@]"
      name
François Bobot's avatar
François Bobot committed
463 464
      (Pp.print_iter2 PHstr.iter Pp.newline Pp.nothing Pp.nothing print_file)
      s.session_files
465 466 467 468 469 470 471 472 473 474 475 476 477 478


  let print_session name fmt s =
    fprintf fmt
      "<a href='#' onclick=\"$('#%s_session').jstree('open_all');\">
expand all
</a> <a href='#' onclick=\"$('#%s_session').jstree('close_all');\">
close all
</a>
<div id=\"%s_session\" class=\"session\">
%a
</div>
<script type=\"text/javascript\" class=\"source\">
$(function () {
479
  $(\"#%s_session\").jstree({
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
      \"types\" : {
	   \"types\" : {
	   \"file\" : {
		\"icon\" : { \"image\" : \"images/folder16.png\"},
		      },
	   \"theory\" : {
		\"icon\" : { \"image\" : \"images/folder16.png\"},
		      },
	   \"goal\" : {
		\"icon\" : { \"image\" : \"images/file16.png\"},
		      },
	   \"prover\" : {
		\"icon\" : { \"image\" : \"images/wizard16.png\"},
		      },
	   \"transf\" : {
		\"icon\" : { \"image\" : \"images/configure16.png\"},
		      },
                        }
                },
      \"plugins\" : [\"themes\",\"html_data\",\"types\"]
        });
});
</script>
"
  name name name (print_session_aux name) s name


  let context : context = "<!DOCTYPE html
PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\">
<head>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />
    <title>Why3 session of %s</title>
514 515
    <link rel=\"stylesheet\" type=\"text/css\"
     href=\"javascript/session.css\" />
516
    <script type=\"text/javascript\" src=\"javascript/jquery.js\"></script>
517 518 519 520
    <script type=\"text/javascript\" src=\"javascript/jquery.jstree.js\">\
</script>
    <script type=\"text/javascript\" src=\"javascript/session.js\">\
</script>
521 522 523
</head>
<body>
%a
524 525 526 527 528 529 530 531
<iframe src=\"\"  id='edited'>
<p>Your browser does not support iframes.</p>
</iframe>
<script type=\"text/javascript\" class=\"source\">
$(function () {
  $('#edited').hide()
})
</script>
532 533 534 535 536
</body>
</html>
"

  let run_files () =
537 538
    if !opt_context then
      if not (Sys.file_exists edited_dst) then Unix.mkdir edited_dst 0o755;
539 540
    Queue.iter (run_file context print_session) files;
    if !opt_context then
François Bobot's avatar
François Bobot committed
541
      let data_dir = Whyconf.datadir (Whyconf.get_main whyconf) in
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
      (** copy images *)
      let img_dir_src = Filename.concat data_dir "images" in
      let img_dir_dst = Filename.concat output_dir "images" in
      if not (Sys.file_exists img_dir_dst) then Unix.mkdir img_dir_dst 0o755;
      List.iter (fun img_name ->
        let from = Filename.concat img_dir_src img_name in
        let to_  = Filename.concat img_dir_dst img_name in
        Sysutil.copy_file from to_)
        ["folder16.png";"file16.png";"wizard16.png";"configure16.png"];
      (** copy javascript *)
      let js_dir_src = Filename.concat data_dir "javascript" in
      let js_dir_dst = Filename.concat output_dir "javascript" in
      Sysutil.copy_dir js_dir_src js_dir_dst

end
557
*)
558 559


560
let run_one fname =
561
  match !opt_style with
562 563
    | Table -> Table.run_one fname
    | SimpleTree -> Simple.run_one fname
564 565 566 567
    | Jstree ->
      eprintf "style jstree not yet available@.";
      exit 1
      (* Jstree.run_files () *)
568 569 570 571

open Why3session_lib

let run () =
MARCHE Claude's avatar
MARCHE Claude committed
572
  let _,_,should_exit1 = read_env_spec () in
573 574 575 576 577 578 579 580 581 582
  if should_exit1 then exit 1;
  iter_files run_one


let cmd =
  { cmd_spec = spec;
    cmd_desc = "output session in HTML format.";
    cmd_name = "html";
    cmd_run  = run;
  }
583 584 585 586 587 588 589


(*
Local Variables:
compile-command: "unset LANG; make -C ../.. bin/why3session.byte"
End:
*)