Browse Source

Finished biniou in tmp file

 - Writing is now more atomical (each time file is read and written)
 - Say when the file format is not correct and reinitialises the file
Leo 10 years ago
parent
commit
094a8c30bd
4 changed files with 41 additions and 39 deletions
  1. 4 4
      src/default.ml
  2. 6 6
      src/exec_cmd.ml
  3. 3 7
      src/oclaunch.ml
  4. 28 22
      src/tmp_file.ml

+ 4 - 4
src/default.ml

@@ -41,18 +41,18 @@ open Core.Std;;
 
 (* cmd_number is the number of the command the user wants
  * to execute *)
-let run ~rc:rc_content ~tmp:tmp_content cmd_number =
+let run ~rc:rc_content cmd_number =
   match cmd_number with
     | None -> begin
         (* Execute each item (one by one) in config file *)
         let open Settings_t in (* This prevent warning 40 for ~cmd_list:rc_content.progs *)
-          let cmd_to_exec = Exec_cmd.what_next ~cmd_list:rc_content.progs ~tmp:tmp_content in
+          let cmd_to_exec = Exec_cmd.what_next ~cmd_list:rc_content.progs in
             (* TODO Use display option in rc file *)
-            Exec_cmd.execute ~tmp:tmp_content cmd_to_exec;
+            Exec_cmd.execute cmd_to_exec;
       end
     | Some num -> begin
         let open Settings_t in
         let cmd_to_exec = Exec_cmd.num_cmd_to_cmd ~cmd_list:rc_content.progs num in
-          Exec_cmd.execute ~tmp:tmp_content cmd_to_exec;
+          Exec_cmd.execute cmd_to_exec;
       end
 ;;

+ 6 - 6
src/exec_cmd.ml

@@ -57,11 +57,11 @@ let num_cmd_to_cmd ~cmd_list number =
 
 (* Function to determinate what is the next command to
  * execute *)
-let what_next ~tmp ~cmd_list =
-  let tmp_json = Yojson.Basic.from_file tmp in
+let what_next ~cmd_list =
+  let tmp_file = Tmp_file.read () in
   let open Yojson.Basic.Util in
-  let num_next = tmp_json |> member "num" |> to_int in (* Number of the next cmd to run *)
-    num_cmd_to_cmd ~cmd_list:cmd_list num_next
+    let num_next = Tmp_file.read () in (* Number of the next cmd to run *)
+    num_cmd_to_cmd ~cmd_list:cmd_list num_next.number
   ;;
 
 (* Display an error message if command can't run
@@ -76,8 +76,8 @@ let display_result command status =
 ;;
 
 (* Execute some command and log it *)
-let execute ?(display=true) ~tmp cmd =
-    Tmp_file.log ~func:((+) 1) ~file_name:tmp;
+let execute ?(display=true) cmd =
+    Tmp_file.log ~func:((+) 1) ();
     if display then
         print_endline cmd;
     Sys.command cmd

+ 3 - 7
src/oclaunch.ml

@@ -48,10 +48,6 @@ let build_info = ( "Build with OCaml version " ^ (Sys.ocaml_version) ^ " on " ^
 (* Obtain data from rc file *)
 let rc_content = File_com.init_rc ~rc:Const.rc_file;;
 
-(* Obtain data from tmp file *)
-let tmp_content = Tmp_file.init ~tmp:Const.tmp_file;;
-
-
 (* Define commands *)
 let commands =
   Command.basic
@@ -67,9 +63,9 @@ let commands =
     +> anon (maybe ("Command number" %: int)))
     (fun reset_tmp num_cmd () ->
        match reset_tmp with
-         | true -> Tmp_file.reset ~tmp:tmp_content (Option.value ~default:0
-         num_cmd) (* Reset temp file, if nothing is given, put 0 value *)
-         | false -> Default.run ~rc:rc_content ~tmp:tmp_content num_cmd
+         | true -> (* Reset temp file, if nothing is given, put 0 value *)
+                 Tmp_file.reset (Option.value ~default:0 num_cmd)
+         | false -> Default.run ~rc:rc_content num_cmd
     )
 ;;
 

+ 28 - 22
src/tmp_file.ml

@@ -38,7 +38,9 @@ open Core.Std;;
 
 (* XXX Using and keyword because each function can call each other *)
 (* Function to read the tmp file *)
-let rec read ~name =
+let rec read () =
+    (* Short name *)
+    let name = Const.tmp_file in
     (* Get the string corresponding to the file *)
     let file_content = In_channel.read_all name in
     try
@@ -46,37 +48,42 @@ let rec read ~name =
     (* In previous version, the JSON format was used, otherwise the file can
      * have a bad format. In this case, the Ag_ob_run.Error("Read error (1)")
      * exeption is throw. We catch it here *)
-    with Ag_ob_run.Error("Read error (1)") ->
+    with Ag_ob_run.Error("Read error (1)") | Bi_inbuf.End_of_input ->
         (* If file is not in the right format, delete it and create a new one.
          * Then, read it *)
+        printf "Reinitialises tmp file\n"; (* TODO Make it settable *)
         Sys.remove name;
-        create_tmp_file ~name;
-        read ~name
+        create_tmp_file ();
+        read ()
 
 (* Function to write the tmp file *)
-and write ~name (tmp_file:Tmp_biniou_t.tmp_file) =
+and write (tmp_file:Tmp_biniou_t.tmp_file) =
+    (* Short name *)
+    let name = Const.tmp_file in
     let biniou_tmp = Tmp_biniou_b.string_of_tmp_file tmp_file in
     Out_channel.write_all name ~data:biniou_tmp
 
 (* Function to create the tmp file *)
-and create_tmp_file ~name =
+and create_tmp_file () =
+    (* Short name *)
+    let name = Const.tmp_file in
     Tmp_biniou_v.create_tmp_file ~command:[] ~number:0 ()
     (* Convert it to biniou *)
-    |> write ~name
+    |> write
 ;;
 
 (* Function to open tmp file *)
-let rec init ~tmp =
+let rec init () =
   (* If file do not exists, create it *)
-  let file_exists = (Sys.file_exists tmp) in
+  let file_exists = (Sys.file_exists Const.tmp_file) in
     match file_exists with
-      | `No -> create_tmp_file ~name:tmp;
-          init ~tmp:tmp
+      | `No -> create_tmp_file ();
+          init ()
       | `Unknown -> begin
-          Core_extended.Shell.rm tmp;
-          init ~tmp:tmp
+          (*Core_extended.Shell.rm tmp;*)
+          init ()
         end
-      | `Yes -> tmp
+      | `Yes -> read ()
 ;;
 
 (* Verify that the value exist *)
@@ -97,25 +104,24 @@ let rec is_prog_in_rc list_from_rc_file program =
 
 (* Log when a program has been launched in a file in /tmp
    ~func is the function applied to the value *)
-let log ?(func= (+) 1 ) ~file_name =
+let log ?(func= (+) 1 ) () =
   (* We will use tmp_file type *)
   let open Tmp_biniou_t in
-  let file = read ~name:file_name in
+  (* Make sure that file exists, otherwise strange things appears *)
+  let file = init () in
   (* Write the file with the new value *)
-  write ~name:file_name { file with number = (func file.number)}
+  write { file with number = (func file.number)}
 ;;
 
 (* Reset command number in two ways :
     * if cmd_num is 0, delete tmp file, to reinitialise program
     * if cmd_num is 0>, set to this value
     * else display an error message *)
-let reset ~tmp cmd_num=
+let reset cmd_num=
     match cmd_num with
-    | 0 -> Sys.remove tmp
+    | 0 -> Sys.remove Const.tmp_file
     | n when n > 0 ->
-            (* Make sure that file exists, otherwise strange things appears *)
-            let file_name = init ~tmp in
             (* Set the number *)
-            log ~func:((fun a b -> a) n) ~file_name
+            log ~func:((fun a b -> a) n) ()
     | _ -> printf "Invalid number" (* TODO Make it settable *)
 ;;