Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

lurettetop.ml 30.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
(*pp camlp4o *)
(*-----------------------------------------------------------------------
** Copyright (C) 2002 - Verimag.
** This file may only be copied under the terms of the GNU Library General
** Public License 
**-----------------------------------------------------------------------
**
** File: lurettetop.ml
** Main author: jahier@imag.fr
*)

12

13
14
15
16
17
18
19
20
21
22
(** lurette toplevel loop. *)

let usage = "
usage: lurettetop [<options>] [<env ([x] env)*>]

lurettetop is a small top level loop that let one use lurette. 
Type help and/or man at the prompt for more info.

Command line <options> are:
"
23
type draw_mode = Vertices | Edges | Inside
24
25
26

type flagT = {
  mutable sut : string ;  
27
  mutable sut_node : string ;  
28
  mutable oracle : string option ;
29
  mutable oracle_node : string ;  
30
31
32
33
  mutable env : string ;
  mutable step_nb : int;
  mutable formula_nb : int ;
  mutable draw_nb : int ;
34
  mutable draw_mode : draw_mode ;
35
36
37
38
  mutable step_by_step : bool ref ;
  mutable display_local_var : bool ref ;
  mutable display_sim2chro : bool ref;
  mutable seed : int option ;
39
  mutable precision : int ;
40
  mutable verbose : bool ref ;
41
  mutable show_step : bool ref ;
42
43
  mutable output : string ;
  mutable make_opt : string ;
44
  mutable prompt : string option ;
45
  mutable go : bool ref ;
46
  mutable restore : string option;
47
(* a flag to know whether lurette_exe needs to be (re-)build *)
48
49
50
51
  mutable to_build : bool ref ;
  mutable prefix : string
  

52
53
54
55
56
}


let (flag : flagT) = {
  sut = "" ;
57
  sut_node = "" ;
58
  oracle = None ;
59
  oracle_node = "" ;
60
  env = "";
61
  make_opt = "nc" ;
62
63
64
  step_nb = 10;
  formula_nb = 1 ;
  draw_nb  = 1 ;
65
  draw_mode = Inside ;
66
  step_by_step = ref false ;
67
  show_step = ref false ;
68
69
70
  display_local_var = ref true ;
  display_sim2chro = ref true ;
  seed = None ;
71
  precision = 2;
72
73
  verbose = ref false ;
  output = "lurette.rif" ;
74
  prompt = None ;
75
  go = ref false ;
76
  restore = None ;
77
78
  to_build = ref true;
  prefix = ""
79
80
81
82
}

let rec speclist = 
  [
83
84
85
86
87
88
89
    "--sut", Arg.String 
      (fun s -> 
	 let file = 
	   try Filename.chop_extension s
	   with Invalid_argument _ -> s
	 in 
	   flag.sut <- file), 
90
       "<string>\tFile name of the system under test (without extension).";
91
92
93
94
95
96
97
98
99
    "-sut", Arg.String 
      (fun s ->
	 let file = 
	   try Filename.chop_extension s
	   with Invalid_argument _ -> s
	 in 
	   flag.sut <- file), 
      " <string>\n" ;

100
101
102
103
104
105
106
    "--main-sut-node", Arg.String 
      (fun s -> flag.sut_node <- s), 
       "<string>\tMain node name of the system under test.";
    "-msn", Arg.String 
      (fun s -> flag.sut_node <- s), 
      " <string>\n" ;

107
108
109
110
111
112
    "--oracle", Arg.String (fun s -> 
	 let file = 
	   try Filename.chop_extension s
	   with Invalid_argument _ -> s
	 in 
	   flag.oracle <- Some file), 
113
       "<string>\tFile name of the oracle (without extension).";
114
115
116
117
118
119
120
    "-oracle", Arg.String (fun s -> 
	 let file = 
	   try Filename.chop_extension s
	   with Invalid_argument _ -> s
	 in 
	   flag.oracle <- Some file), 
    " <string>\n";
121

122
123
124
125
126
127
128
129
    "--main-oracle-node", Arg.String 
      (fun s -> flag.oracle_node <- s), 
       "<string>\tMain node name of the oracle.";
    "-man", Arg.String 
      (fun s -> flag.oracle_node <- s), 
      " <string>\n" ;


130
131
132
    "--test-length", Arg.Int (fun i -> flag.step_nb <- i),
       "<int>\tNumber of steps to be done";
    "-l", Arg.Int (fun i -> flag.step_nb <- i),  
133
    ("<int>\t\t(currently, " ^ (string_of_int flag.step_nb) ^ ").\n");
134
135
136
137

    "--thick-form", Arg.Int (fun i -> flag.formula_nb <- i),
       "<int>\tNumber of formula to be drawn at each step";
    "-tf", Arg.Int (fun i -> flag.formula_nb <- i), 
138
    ("<int>\t\t(currently, " ^ (string_of_int flag.formula_nb) ^ ").\n");
139
140
141
142

    "--thick-draw", Arg.Int (fun i -> flag.draw_nb <- i),
       "<int>\tNumber of draw to be done in each formula ";
    "-td", Arg.Int (fun i -> flag.draw_nb <- i),
143
    ("<int>\t\tat each step (currently, " ^ 
144
145
     (string_of_int flag.draw_nb) ^ ").\n");
    
146
    "--draw-inside", Arg.Unit (fun _ -> flag.draw_mode <- Inside),
147
       "\tDraw on the edges of the convex hull of solutions.\n ";
148
149

    "--draw-edges", Arg.Unit (fun _ -> flag.draw_mode <- Edges),
150
       "\t\tDraw on the edges of the convex hull of solutions.\n ";
151

152
153
    "--draw-vertices", Arg.Unit (fun _ -> flag.draw_mode <- Vertices),
       "\tDraw among the vertices of the convex hull of solutions.\n ";
154
155


156
157
158
159
    "--seed", Arg.Int (fun i -> flag.seed <- Some i),
       "<int>\t\tSeed the random engine is init with." ;
    "-seed", Arg.Int (fun i -> flag.seed <- Some i), " <int>\n";

160
161
162
163
    "--precision", Arg.Int (fun i -> flag.precision <- i),
       "<int>\t\tSet the precision used with floats." ;
    "-p", Arg.Int (fun i -> flag.precision <- i), " <int>\n";

164
(* This option is not meant to be available to end-users...  *)
165
166
    "--make-opt", Arg.String (fun s -> flag.make_opt <- s), 
    ("<string>\tOptions to call make with when building \n" ^ 
167
     "\t\t\tlurette (currently, \"" ^ flag.make_opt ^ "\").\n");
168
169

    "--output", Arg.String (fun s -> flag.output <- s),
170
    ("<string>\tSet the output file name (currently,  \"" ^ 
171
172
173
174
175
176
177
    flag.output ^ "\").");
    "-o", Arg.String (fun s -> flag.output <- s), "<string>\n";

    "--go", Arg.Set flag.go, 
    "\t\t\tStart the testing process directly without prompting.";
    "-go", Arg.Set flag.go, "\n";

178
    "--restore", Arg.String (fun s -> flag.restore <- Some s), 
179
       "<string>\tFile name of the package containing"
180
       ^ "\n\t\t\tthe temporarily files to be restored (cf the pack command).\n";
181

182
183
184
    "--step", Arg.Set flag.step_by_step, "\t\tRun lurette step by step." ;
    "-s", Arg.Set flag.step_by_step, "\n";

185
186
187
    "--show-step", Arg.Set flag.show_step,
       "\t\tSet on the show step mode."; 

188
189
190
    "--do-not-show-step", Arg.Clear flag.show_step,
       "\t\tSet off the show step mode."; 

191
192
193
194
    "--verbose", Arg.Set flag.verbose,
       "\t\tSet on the verbose mode.";
    "-v", Arg.Set flag.verbose,"\n";

195
196
197
    "--prompt", Arg.String (fun s -> flag.prompt <- Some s), 
       "\t\tReplace the default prompt.\n";

198
199
200
    "--prefix", Arg.String (fun s -> flag.prefix <-  s), 
       "\t\tstring to append before the call to lurette (e.g., \"/usr/bin/times \").\n";

201

202
    "--sim2chro", Arg.Set flag.display_sim2chro,
203
       "\t\tCall sim2chro when lurette resumes.\n";
204
205

    "--no-sim2chro", Arg.Clear flag.display_sim2chro,
206
       "\tDo not call sim2chro when lurette resumes.";
207
208
209
    "-ns2c", Arg.Clear flag.display_sim2chro, "\n";

    "--local-var", Arg.Set flag.display_local_var,
210
       "\t\tDisplay environment local variables in sim2chro (on).";
211
212
213
214
215
216
217
218
219
220
221
222
223
224

    "--no-local-var", Arg.Clear flag.display_local_var,
       "\tDo not display environment local variables in sim2chro.\n" ;

    "--help", Arg.Unit (fun _ -> (Arg.usage speclist usage ; exit 0)),
       "\t\tDisplay this list of options." ;
    "-help", Arg.Unit (fun _ -> (Arg.usage speclist usage ; exit 0)),
    "";
    "-h", Arg.Unit (fun _ -> (Arg.usage speclist usage ; exit 0)),
    ""
]



225
226
let (build : string -> string -> string -> bool) =
  fun user_dir lurette_tmp_dir lurette_dir ->
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
    let sut = 
      if Filename.is_relative flag.sut 
      then (user_dir ^ flag.sut) 
      else flag.sut 
    in
      if not (Sys.file_exists (sut ^ ".lus"))
      then
	(
	  output_string stdout ("*** File " ^ sut ^ 
				".lus does not exist.\n");
	  flush stdout;
	  false
	)
      else
	(
	  output_string stderr "  building lurette ...\n";
	  flush stderr;
	  if (flag.sut = "" or flag.env = "")
	  then
	    (
	      print_string "*** 
248
249
  Both the sut and the env fields should be filled
  with set_sut and set_env commands respectively.\n ";
250
251
252
253
254
255
256
257
258
259
260
261
262
	      false
	    )
	  else
	    let (oracle, oracle2) =  
	      match flag.oracle 
	      with 
		  None -> "", (lurette_tmp_dir ^ "/always_true") 
		| Some str -> 
		    let str2 = 
		      if Filename.is_relative str then (user_dir ^ str) else str 
		    in
		      (str2, str2)
	    in
263
	    let make_cmd = 
264
	      (* XXX Not portable !! *)
265
	      ("make -I " ^ user_dir ^ " -f " 
266
267
	       ^ lurette_dir ^ "/Makefile.lurette " ^ flag.make_opt ^ "> " 
	       ^ user_dir ^ "/make_lurette.log \n")
268
269
270
271
272
273
274
275
276
277
278
279
280
281
	    in
	      if oracle2 <>  (lurette_tmp_dir ^ "/always_true")
		&& not (Sys.file_exists (oracle2 ^ ".lus"))
	      then
		(
		  output_string stdout ("*** File " ^ oracle2 ^ 
					".lus does not exist.\n");
		  flush stdout;
		  false
		)
	      else
		(
		  Unix.putenv "SUT" (sut ^ ".c");
		  Unix.putenv "ORACLE" (oracle2 ^ ".c");
282
283
284
285
286
		  let sut_node = 
		    if 
		      flag.sut_node = "" 
		    then 
		      (* try to guess the node name if not given *)
287
		      let base = Filename.basename flag.sut in
288
289
290
291
292
293
294
295
			try Filename.chop_extension base 
			with _ -> base  
		    else flag.sut_node
		  and 
		    oracle_node = 
		    if flag.oracle_node = "" 
		    then 
		      (* try to guess the node name if not given *)
296
		      let base = Filename.basename oracle in
297
298
299
300
			try Filename.chop_extension base 
			with _ -> base  
		    else flag.oracle_node
		  in
301
		  try 
302
		    Util.gen_stubs sut sut_node oracle oracle_node;
303
304
305
306
307
308
309
310
311
312
313
314
315
		    output_string stderr make_cmd ;
		    flush stderr ;
		    flush stdout;
		    if 
		      ((Sys.command make_cmd) <> 0)
		    then 
		      false
		    else 
		      (
			output_string stderr "   ...make ok.\n";
			true
		      )
		  with Failure e -> 
316
		    (
317
318
319
		      output_string stdout e ;
		      flush stdout ;
		      false
320
321
322
		    )
		)
	)
323
	
324
325
(* run lurette and returns the exit status *)
let (run : string -> int) =
326
  fun lurette_tmp_dir ->
327
328
    let seed_str = 
      match flag.seed with 
329
330
	  None -> []
	| Some i -> "-seed"::[(string_of_int i)]
331
    and precision_str = "--precision"::[string_of_int flag.precision]
332
333
    and verb_str = if !(flag.verbose) then ["-v"] else []
    and show_step_str = if !(flag.show_step) then ["--show-step"] else []
334
335
    and draw_mode_str = 
      match flag.draw_mode with
336
337
	  Inside -> ["--draw-inside"]
	| Edges -> ["--draw-edges"]
338
	| Vertices -> ["--draw-vertices" ]
339
340
    and orac_str = 
      match flag.oracle with 
341
342
343
344
345
346
347
348
	  None -> ["--no-oracle"]
	| Some str -> []
    and step_nb_str = [(string_of_int flag.step_nb)]
    and formula_nb_str = [(string_of_int flag.formula_nb)]
    and draw_nb_str = [(string_of_int flag.draw_nb)]
    and outp_str = "-o"::[flag.output]
    and step_str = if !(flag.step_by_step) then ["-s"] else []
    and sim2_str = if !(flag.display_sim2chro) then ["--call-sim2chro"] else ["-ns2c"]
349
    and dlvr_str = 
350
      if !(flag.display_local_var) then ["--display-local-var"] else ["-nlv"]
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
    and env_str = 
      let lut_list = Util.string_to_string_list flag.env in
	List.iter 
	  (fun lut -> 
	     if lut = "x" then () else
	     if Util.get_extension lut = ".lut" then
	       let file = Filename.chop_extension lut in
		 Util.lutin lut (file ^ ".luc")
	  )
	  lut_list;
	List.map 
	  (fun lut -> 
	     try let file = Filename.chop_extension lut in
	       (file ^ ".luc")
	     with _ -> lut
	  )
	  lut_list
368
    in
369

370
371
372
373
374
375
376
377
378
379
380
381
382
    let lurette = (lurette_tmp_dir ^ "/lurette") in
    let prefix = Util.string_to_string_list flag.prefix in
(*     let lurette = lurette_tmp_dir ^ "/lurette" in *)
    let arg_list0 =
      List.flatten
	[ 
	  prefix; 
	  [lurette];
	  step_nb_str ;
	  formula_nb_str ;
	  draw_nb_str ;
	  draw_mode_str ;
	  seed_str;
383
	  precision_str;
384
385
386
387
388
389
390
	  verb_str ;
	  show_step_str ;
	  orac_str ;
	  outp_str ;
	  step_str ;
	  sim2_str ;
	  dlvr_str ;
391
	  env_str
392
393
394
395
396
	] 
    in
    let prog = if flag.prefix = "" then lurette else List.hd prefix in
    let arg_list = Util.rm "" arg_list0 in
    let args = Array.of_list arg_list in
397
    let times0 = Unix.times () in
398
399
      
    let pid = Unix.create_process prog args Unix.stdin Unix.stdout Unix.stderr in
400
401
402
403
    let _ = 
      print_string ("\nThe Pid of lurette is " ^ (string_of_int pid) ^ "\n");
      flush stdout
    in
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
    let (_, status) = Unix.waitpid [Unix.WUNTRACED] pid in
      ( match status with
	    Unix.WEXITED i -> 
	      if i = 127 then 1 else
		let times1 = Unix.times () in
		let times = 
		  times1.Unix.tms_cutime
		  +.times1.Unix.tms_cstime
		  -.times0.Unix.tms_cutime
		  -.times0.Unix.tms_cstime
		in
		let times_str = string_of_float times in
		  output_string stdout 
		    ("\nThe execution lasted " ^ times_str ^ " second" ^ 
		     (if times > 1. then "s" else "") ^ ".\n" ^
		     "********************************************************************\n");
		  flush stdout;
		  0
	  | Unix.WSIGNALED i -> 
423
	      output_string stderr("lurette was killed by signal nb " 
424
			    ^ (string_of_int i) ^ ".\n");
425
426
	      flush stderr;
	      0
427
	  | Unix.WSTOPPED i -> 
428
	      output_string stderr ("lurette was stopped by signal nb " 
429
			     ^ (string_of_int i) ^ ".\n");
430
431
	      flush stderr;
	      0
432
433
      )
     
434
type cmd =
435
436
    Sut of string * string
  | Oracle of string * string
437
438
439
440
441
442
  | MakeOpt of string
  | Env of string
  | StepNb of int
  | FormulaNb of int
  | DrawNb of int
  | Step of bool
443
  | DisplaySim2chro of bool
444
  | DisplayLocalVar of bool
445
  | StepByStep of bool
446
  | Seed of int
447
  | Precision of int
448
  | RandomSeed
449
  | Verbose of bool
450
  | ShowStep of bool
451
  | Output of string
452
  | CallSim2chro
453
454
455
  | Build
  | Clean
  | Run
456
457
  | DrawInside
  | DrawEdges
458
  | DrawVertices
459
460
461
  | Quit
  | Help
  | Man
462
  | Prompt of string
463
  | Prefix of string
464
  | Pack of string
465
  | Show
466
467
468
469
470
471
472
  | HelpSimple
  | Error of string

let lexer = Genlex.make_lexer []

type tok = Genlex.token Stream.t

473
474
475
476
477
478
479
480
481
482
483
484
485

let (remove_extension : string -> string) =
  fun str -> 
    let file_ext = Filename.basename str
    and dir  = Filename.dirname str in
    let file = try Filename.chop_extension file_ext with _ -> file_ext in
      (Filename.concat dir file)

let _ = assert ((remove_extension "../toto/tutu.lus") = "../toto/tutu")
let _ = assert ((remove_extension "/home/toto/tutu.lus") = "/home/toto/tutu")
let _ = assert ((remove_extension "home/toto/tutu.lus") = "home/toto/tutu")
let _ = assert ((remove_extension "home/toto/tutu") = "home/toto/tutu")

486
487
488
489
490
let rec 
  (read_cmd : tok -> cmd) =
  fun tok -> 
    match tok with parser
      | [< 'Genlex.Ident "run" >] -> Run
491
      | [< 'Genlex.Ident "r" >] -> Run
492
      | [< 'Genlex.Ident "b" >] -> Build
493
      | [< 'Genlex.Ident "build" >] -> Build
494
495
496
497
498
      | [< 'Genlex.Ident "set_draw_mode" ;'Genlex.Ident id >] -> 
	  (
	    match id with 
		"inside" -> DrawInside
	      | "edges"  -> DrawEdges
499
	      | "vertices" -> DrawVertices
500
501
502
	      | _ -> Error ("Unknown draw mode (" ^ id ^ ")\n")
	  )
      | [< 'Genlex.Ident "show" >] -> Show
503
      | [< 'Genlex.Ident "clean" >] -> Clean
504
      | [< 'Genlex.Ident "set_prompt"; 'Genlex.String str >] -> Prompt(str)
505
      | [< 'Genlex.Ident "set_prefix"; 'Genlex.String str >] -> Prefix(str)
506
507
      | [< 'Genlex.Ident "set_env" ; 'Genlex.String str >] -> Env(str)
(*       | [< 'Genlex.Ident "set_env" ; str = parse_env >] -> Env(str) *)
508
509
      | [< 'Genlex.Ident "set_sut" ; str = parse_file_name ; node = parse_node >] -> Sut(str, node)
      | [< 'Genlex.Ident "set_oracle" ; str = parse_file_name ; node = parse_node>] -> Oracle(str, node)
510
511
512
513
514
      | [< 'Genlex.Ident "set_make_opt" ; 'Genlex.Ident str >] -> MakeOpt(str)
      | [< 'Genlex.Ident "set_test_length" ; 'Genlex.Int i >] -> StepNb(i)
      | [< 'Genlex.Ident "set_formula_nb" ; 'Genlex.Int i >] -> FormulaNb(i)
      | [< 'Genlex.Ident "set_draw_nb" ; 'Genlex.Int i >] -> DrawNb(i)
      | [< 'Genlex.Ident "set_seed" ; 'Genlex.Int i >] -> Seed(i)
515
      | [< 'Genlex.Ident "set_precision" ; 'Genlex.Int i >] -> Precision(i)
516
      | [< 'Genlex.Ident "set_seed_randomly" >] -> RandomSeed
517

518
519
520
521
522
      | [< 'Genlex.Ident "set_step_by_step" ; 'Genlex.Ident str >] -> 
	  if List.mem str ["t";"true"] 
	  then StepByStep(true)
	  else StepByStep(false)

523
524
525
526
      | [< 'Genlex.Ident "set_display_sim2chro" ; 'Genlex.Ident str >] -> 
	  if List.mem str ["t";"true"] 
	  then DisplaySim2chro(true)
	  else DisplaySim2chro(false)
527
528
529
530
531
532
533
534
535
536
      | [< 'Genlex.Ident "set_display_local_var" ; 'Genlex.Ident str >] -> 
	  if List.mem str ["t";"true"] 
	  then DisplayLocalVar(true)
	  else DisplayLocalVar(false)

      | [< 'Genlex.Ident "set_verbose" ; 'Genlex.Ident str >] -> 
	  if List.mem str ["t";"true"] 
	  then Verbose(true)
	  else Verbose(false)

537
538
539
540
541
      | [< 'Genlex.Ident "set_show_step" ; 'Genlex.Ident str >] -> 
	  if List.mem str ["t";"true"] 
	  then ShowStep(true)
	  else ShowStep(false)

542
      | [< 'Genlex.Ident "set_output" ; str = parse_file_name>] -> Output(str ^ ".rif")
543
      | [< 'Genlex.Ident "sim2chro" >] -> CallSim2chro
544
545
546
547
548
549
550
551

      | [< 'Genlex.Ident "quit"  >] -> Quit
      | [< 'Genlex.Ident "q"  >] -> Quit
      | [< 'Genlex.Ident "bye"  >] -> Quit
      | [< 'Genlex.Ident "exit"  >] -> Quit

      | [< 'Genlex.Ident "man"  >] -> Man

552
553
      | [< 'Genlex.Ident "pack"  ; 'Genlex.Ident file >] -> Pack(file)

554
555
556
557
558
559
      | [< 'Genlex.Ident "help"  >] -> Help
      | [< 'Genlex.Ident "h"  >] -> Help
      | [< 'Genlex.Ident "?"  >] -> Help
      | [< 'Genlex.Ident _ >]  -> Error "Unknown command.\n"
      | [<   >] -> HelpSimple

560
561
562
563
564
565
and  
  (parse_node : tok -> string) = 
  fun tok -> 
    try
      match tok with parser 
	| [< 'Genlex.Ident id  >] -> id
566
567
	| [< 'Genlex.String id >] -> id
	| [<   >] -> ""
568
569
570
571
572
      with _ -> 
	print_string 
	   "*** parse error: cannot parse that node name.\n";
	flush stdout;
	""
573
574
575
576
577
and  
  (parse_file_name : tok -> string) = 
  fun tok -> 
    try
      match tok with parser 
578
579
	| [<  'Genlex.String str >] -> 
	    if str = "" then "" else remove_extension str 
580
	| [<  'Genlex.Ident id  >] -> id
581
	| [<  >]  ->  "" 
582
583
      with _ -> 
	print_string 
584
585
586
	   "*** parse error: cannot parse that file name.\n";
	flush stdout;
	""
587
and  
588
  (parse_env : tok -> string) = 
589
590
  fun tok -> 
    try
591
592
593
      (
	match tok with parser 
	  | [<  'Genlex.Ident "x" ; tail = parse_env >] -> (" x " ^ tail)
594
	  | [<  'Genlex.String str ; tail = parse_env >] -> (str ^ tail) 
595
	  | [<  'Genlex.Ident id  ; tail = parse_env >] -> (id ^ ".luc " ^ tail)
596
597
598
599
600
601
602
603
604
605
	  | [< _ >]  ->  "" 
      )
    with e -> 
      print_string (Printexc.to_string e);
      print_string 
         "*** Error when parsing the environment field.\n";
      flush stdout;
      ""
	
	
606

607
let cmd_usage = "  Type h for help, or man for a small user manual.
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
"

let man = "
Once lurettetop has been run, a prompt is printed waiting for 
user queries. One first need at least to set the sut (system 
under test) and the environement fields like that:

    [your shell prompt] lurettetop  
    <lurette> set_sut my_program_to_test
    <lurette> set_oracle my_oracle
    <lurette> set_env my_env_file1 my_env_file2 x my_env_file3

And then the testing process can start:

    <lurette> run
       ... [test, test, test, ...]
   
Equivalently, you can directly set values at the command line:

    [your shell prompt] lurettetop --sut my_program_to_test  \\
                          --oracle my_oracle \\
                          my_env_file1 my_env_file2 x my_env_file3
    <lurette> run
       ... [test, test, test, ...]
"

	
let (display_cmd : unit -> unit) = 
  fun _ -> 
    let msg =  "The commands are: 

639
run, r
640
     to start the testing process. Note that the sut and the environment
641
642
     fields (described below) should be set.

643
quit q, bye  
644
645
646
647
648
649
650
651
652
653
654
655
     to quit the lurette top level


help, h, ?
     display this list of commands

man
     display a small user manual

clean
     run a make clean (you can try it if <<run>> failed)

656
657
658
659
660
pack <file_name>
     package up in <file_name>.tgz files created in a temporary 
     directory. This file can then be given in argument of the 
     <<--restore>> option of lurettetop so that they are not 
     computed again.
661
662
663
664

show
    show of post-script version of the current environment

665

666
set_env  \"<env ([x] env)*>\"  (env=<file name>)                   
667
     to set the environment field (.luc files). Automata of
668
669
670
671
672
673
674
     environments separated by \"x\" are multiplied.
     The current value of this field is "
	       ^ 
	       (if flag.env = "" 
		then "UNSET!" 
		else ("\"" ^ flag.env ^ "\" ")) ^ "

675
set_sut  <file name> [<sut file name> <main node name>]            
676
677
678
     to set the sut field. Its current value is " ^ 
	       (if flag.sut = "" 
		then "UNSET!" 
679
		else("\"" ^ flag.sut ^ " " ^ flag.sut_node ^ "\"")) ^ "
680

681
set_oracle  <file name>  [<oracle file name> <main node name>]
682
683
684
     to set the oracle field. Its current value is " ^ 
	       (match flag.oracle with
		    None ->  "unset" 
685
		  | Some str -> ("\"" ^ str ^ " " ^ flag.oracle_node ^ "\"")) ^ "
686
687
688
689
690
691
692
693
694
695
696
697
698
699
    
set_test_length  <integer>
     to set the test length. Its current value is \"" ^ 
	       (string_of_int flag.step_nb) ^ "\"

set_formula_nb  <integer>                      
     to set the number of formula to be drawn at each step. Its current 
     value is \"" 
	       ^ (string_of_int flag.formula_nb) ^ "\"

set_draw_nb  <integer>
     to set the number of draw to be done in each formula at each step
     Its current value is \"" ^ (string_of_int flag.draw_nb) ^ "\"

700
set_draw_mode <draw_mode>
701
    set the draw mode, which can be either be inside, edges, or vertices.
702
703
    - inside : draw inside the convex hull of solutions.
    - edges : draw on the edges of the convex hull of solutions.
704
    - vertices : draw among the vertices of the convex hull of solutions.
705
706
707
    Its current mode is \"" ^ (match flag.draw_mode with
			    	 Inside -> "inside"
			       | Edges -> "edges"
708
			       | Vertices -> "vertices" 
709
710
			    ) ^ "\".

711
712
713
714
715
set_seed  <integer>
     to set the seed the random engine is initialised with.
     Its current value is " ^ (match flag.seed with
			       None ->  "chose randomly" 
			     | Some i -> ("\"" ^ (string_of_int i) ^ "\""))  ^ "
716

717
718
719
720
set_precision  <integer>
     to set precision used to manipulate floats.
     Its current value is " ^ (string_of_int flag.precision)  ^ "

721
722
set_seed_randomly
     to the system set a seed randomly.
723
724
725
726
727
728
729
730
731
  
set_step_by_step  <boolean>
     to set a flag saying whether or not the test should proceed step
     by step. Its current value is " ^ (if !(flag.step_by_step) 
			   then "\"true\""
			   else "\"false\"") ^ "

set_display_sim2chro  <boolean>
     to set a flag saying whether or not sim2chro is called when 
732
       lurette resumes. Its current value is " 
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
	       ^ (if !(flag.display_sim2chro)
			   then "\"true\""
			   else "\"false\"") ^ "

set_display_local_var  <boolean>
     to set a flag saying whether or not the local var be displayed in  
     sim2cro. Its current value is " ^ (if !(flag.display_local_var) 
			   then "\"true\""
			   else "\"false\"") ^ "

set_verbose  <boolean>
     to set on and off a verbose mode. Its current value is " 
	       ^ (if !(flag.verbose) 
			   then "\"true\""
			   else "\"false\"") ^ "

749
750
751
752
753
754
755
756
757
758
759
set_show_step  <boolean>
     to set on and off a show step mode. Its current value is " 
	       ^ (if !(flag.show_step) 
			   then "\"true\""
			   else "\"false\"") ^ "

set_prefix  \"<string>\"
     to set the string that is appended before the call to lurette.
     This useful, e.g., for timings.
     Its current value is \"" ^ flag.prefix ^ "\"

760
set_make_opt  <string>
761
     to set the options to give to make for building lurette
762
763
764
765
766
767
     Its current value is \"" ^ flag.make_opt ^ "\"

set_output  <string>
     to set the name of the file the (rif) output of the test is
     put into. Its current value is \"" ^ flag.output ^ "\"

768
769
770
sim2chro
    call sim2chro

771
772
773
774
775
"
    in
      print_string msg
    

776
777
778
779
let (read_commands : string -> string -> string -> (unit -> string) -> bool) =
  fun user_dir lurette_tmp_dir lurette_dir readline -> 
    try 
      ( match (read_cmd (lexer (Stream.of_string (readline ())))) with
780
781
	    Sut(str, node) -> 
	      flag.sut <- str ; 
782
783
784
785
	      flag.sut_node <- node ;
	      flag.to_build := true; 
	      true
		
786
	  | Oracle(str, node) ->
787
788
789
790
791
792
	      if str = "" 
	      then
		( 
		  flag.oracle <- None ; 
		  flag.oracle_node <- "" 
		)
793
	      else 
794
795
		(
		  flag.oracle <- Some str ;
796
797
798
799
800
801
		  flag.oracle_node <- node
		);
	      flag.to_build := true; 
	      true
		
		
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
	  | MakeOpt(str) -> 
	      flag.make_opt <- str; true
	  | Env(llist) -> 
	      flag.env <- llist; true
	  | StepNb(i) -> 
	      flag.step_nb <- i; true
	  | FormulaNb(i) -> 
	      flag.formula_nb <- i; true
	  | DrawNb(i) -> 
	      flag.draw_nb <- i; true
	  | Step(b) -> 
	      flag.step_by_step := b; true
	  | DisplaySim2chro(b) -> 
	      flag.display_sim2chro := b; true
	  | DisplayLocalVar(b) -> 
	      flag.display_local_var := b; true
	  | StepByStep(b) -> 
	      flag.step_by_step := b; true
	  | Seed(i) -> 
	      flag.seed <- Some i; true
822
823
	  | Precision(i) -> 
	      flag.precision <-  i; true
824
825
826
827
828
829
830
831
832
833
834
835
836
837
	  | RandomSeed -> 
	      flag.seed <- None; true
	  | Verbose(b) -> 
	      flag.verbose := b; true
	  | ShowStep(b) -> 
	      flag.show_step := b; true
	  | Prompt(p) -> 
	      flag.prompt <- Some p; true
	  | Prefix(p) -> 
	      flag.prefix <-  p; true
	  | DrawInside -> 
	      flag.draw_mode <- Inside; true
	  | DrawEdges -> 
	      flag.draw_mode <- Edges; true
838
839
	  | DrawVertices -> 
	      flag.draw_mode <- Vertices; true
840
841
842
	  | Output(str) -> 
	      flag.output <- str; true
	  | Clean -> 
843
	      (* XXX Not portable !! *)
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
	      let rm_cmd = ("rm -f " ^ lurette_tmp_dir ^ "/*") in 
	      let _ = Sys.command rm_cmd in
		print_string (rm_cmd ^ "\n");
		flag.to_build := true; 
		true
	  | Build -> 
	      let build_ok = build user_dir lurette_tmp_dir lurette_dir in
		if 
		  not build_ok 
		then 
		  print_string "\n*** Cannot build lurette, sorry.\n"
		else
		  flag.to_build := false; 
		flush stdout;
		true
	  | Run -> 
	      if (not !(flag.to_build) or (build user_dir lurette_tmp_dir lurette_dir))
	      then 
		(
		  flag.to_build := false;
		  Unix.chdir user_dir;
		  let result = run lurette_tmp_dir in
		    if 
		      result <> 0  
		    then 
		      output_string stdout "\n*** Can not run lurette, sorry.\n"

		    else 
		      (output_string stderr "   ... lurette ok.\n"; flush stderr)
		)
	      else 
		print_string "\n*** Cannot build lurette, sorry.\n";
	      flush stdout;
	      Unix.chdir lurette_tmp_dir;
	      true
		
	  | Show ->
	      Unix.chdir user_dir;
	      Util.show_luc flag.env;
	      Unix.chdir lurette_tmp_dir;
	      true
	  | CallSim2chro ->
	      Util.sim2chro (Filename.concat user_dir flag.output);
	      true
	  | Quit -> false
	  | HelpSimple -> print_string cmd_usage; true
	  | Help -> display_cmd (); true
	  | Man -> print_string man ; true
	  | Pack(file) ->
893
	      (* XXX Not portable !! *)
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
	      let 
		cmd = ("mv " ^ lurette_tmp_dir ^ " /tmp/" ^ file ^  
		       "; cd " ^ user_dir ^ "; tar cvfz " ^ 
		       file ^ ".tgz /tmp/" ^ file ^ 
		       " > tar.log; mv /tmp/" ^ 
		       file ^ " " ^ lurette_tmp_dir ^ " >> tar.log " ) 
			
	      in
	      let tar_res = 
		output_string stderr (cmd ^ "\n") ; 
		flush stderr;
		Sys.command cmd 
	      in	 
		if tar_res <> 0 
		then 
		  (
		    print_string ("*** <<" ^ cmd ^ 
				  ">> failed. Is gnu-tar in your path ?\n");
		    flush stdout;
		    true
		  )
		else
		  true
	  | Error(errmsg) -> 
	      print_string errmsg; 
	      print_string cmd_usage;
	      true
      )
    with 
	End_of_file -> raise End_of_file
      | e -> 
	  print_string ("Bad command\n " ^ (Printexc.to_string e) ^ "\n") ; 
	  print_string cmd_usage ;
	  true
    



932
933
934
935
936
937
938
939
940
let rec (main_loop : string -> string -> string -> int -> unit) =
  fun user_dir lurette_tmp_dir lurette_dir cpt ->
    let _ = 
      print_string 
	(
	  match flag.prompt with 
	      None -> "<lurette " ^ (string_of_int cpt) ^ "> "
	    | Some prompt -> prompt
	) ; 
941
      flush stdout
942
    in
943
    let continue = read_commands user_dir lurette_tmp_dir lurette_dir (read_line) in
944
      if continue 
945
      then main_loop user_dir lurette_tmp_dir lurette_dir (cpt+1)
946
      else print_string "bye!\n"
947
948


949
950


951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
let (get_fresh_dir : unit -> string) = 
  fun _ ->  
    let rec get_fresh_dir_rec n = 
      let dir = "/tmp/lurette" ^ (string_of_int n) in 
	if 
	  not (Sys.file_exists dir) 
	then 
	  (  
	    Unix.mkdir dir 0o755;
	    dir 
	  )
	else 
	  get_fresh_dir_rec (n+1) 
    in 
      get_fresh_dir_rec 1 
966
967

   
968
969
970
971
972
973
974
975
976
977
978
979
let (rm_dir : string -> unit) =
  fun dir -> 
    (* XXX probably not very portable ...*)
    let cmd = ("rm -rf " ^ dir) in
      print_string cmd;
      print_string "\n";
      flush stdout;
      if Sys.command cmd <> 0 
      then print_string ("Can not remove" ^ dir ^ "\n")



980
let _ =
981
982
983
984
985
986
  let user_dir = (Unix.getcwd ()) ^ "/"  in 
  let lurette_dir = 
    try Sys.getenv "LURETTE_PATH"
    with _ -> 
      print_string "Environment var LURETTE_PATH is unset.\n";
      exit 2
987
  in 
988
  let tmp_dir = get_fresh_dir () in
989
990
  let lurette_tmp_dir = 
    match flag.restore with
991
	None -> tmp_dir
992
      | Some file ->
993
	  (* XXX Not portable !! *)
994
995
	  let cmd = ("tar xvfz " ^ user_dir ^ file ^ " --directory " ^ tmp_dir) in 
	  let tar_res = 
996
997
	    output_string stderr (cmd ^ "\n"); 
	    flush stderr;
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
	    Sys.command cmd 
	  in
	    if tar_res <> 0 
	    then 
	      (
		print_string ("*** <<" ^ cmd ^ 
			      ">> failed. Is gnu-tar in your path?\n");
		flush stdout;
		exit 2
	      )
	    else
	      (tmp_dir ^ "/tmp/" ^ (remove_extension file))
1010
  in
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033

    
  let lurette_rc = (user_dir ^ ".lurette_rc") in
  let _ = 
    (* read command in the .lurette_rc file *)
    ( 
      if Sys.file_exists lurette_rc then
	let ic = (open_in lurette_rc) in
	  try
	    while true do
	      let _ = 
		read_commands user_dir lurette_tmp_dir lurette_dir 
		  (fun _ -> (input_line ic))
	      in
		()
	   done 
	  with End_of_file -> 
	    close_in ic 
    );
    
    
    (* read the lurettetop command line options (that will override the 
       .lurette.=_rc ones) *)
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
    let env_saved = flag.env in
      flag.env <- "";
	
      ( try
	  Arg.parse speclist 
	      (fun s -> 
		 if s = "x" 
		 then flag.env <- (flag.env ^ " x ")
		 else flag.env <-
		   let env = 
		     try Filename.chop_extension s
		     with Invalid_argument _ -> s
		   in 
		     if Filename.is_implicit env
		     then (flag.env ^ user_dir ^ env ^ ".luc ")
		     else (flag.env ^ env ^ ".luc ")
	      ) 
	      usage
	with 
	    Failure(e) ->
	      print_string e;
	      flush stdout ;
	      flush stderr ;
	      exit 2
	  | _ ->
	      exit 2
      );
      if (flag.env = "") then flag.env <- env_saved;
	    
1063
  in
1064
    (* XXX Not portable !! *)
1065
1066
1067
  let _ = Sys.command ("cp " ^ lurette_dir ^ "/source/lurette.ml " ^ lurette_tmp_dir ^
		       " ; cp " ^ lurette_dir ^ "/source/lurette.mli " ^ lurette_tmp_dir)
  in
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
    Unix.chdir lurette_tmp_dir;
    if 
      !(flag.go)
    then 
      (
	if 
	  build user_dir lurette_tmp_dir lurette_dir
	then
	  (
	    Unix.chdir user_dir;
1078
1079
1080
1081
	    if 
	      (run lurette_tmp_dir) <> 0
	    then 
	      (
1082
		print_string "Can not run lurette, sorry.\n";
1083
1084
1085
		exit 1
	      );
	    rm_dir lurette_tmp_dir;
1086
1087
1088
	  )
	else  
	  (
1089
	    print_string "Can not build lurette, sorry\n";
1090
1091
1092
1093
1094
1095
1096
	    rm_dir lurette_tmp_dir;
	    exit 1
	  )
      )
    else 
      (* flag.go *)
      (
1097
	main_loop user_dir lurette_tmp_dir lurette_dir 1;
1098
1099
1100
	Unix.chdir user_dir;
	rm_dir lurette_tmp_dir
      )
1101
1102
1103
1104