tmp_file.ml 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. (******************************************************************************)
  2. (* Copyright © Joly Clément, 2014 *)
  3. (* *)
  4. (* leowzukw@vmail.me *)
  5. (* *)
  6. (* Ce logiciel est un programme informatique servant à exécuter *)
  7. (* automatiquement des programmes à l'ouverture du terminal. *)
  8. (* *)
  9. (* Ce logiciel est régi par la licence CeCILL soumise au droit français et *)
  10. (* respectant les principes de diffusion des logiciels libres. Vous pouvez *)
  11. (* utiliser, modifier et/ou redistribuer ce programme sous les conditions *)
  12. (* de la licence CeCILL telle que diffusée par le CEA, le CNRS et l'INRIA *)
  13. (* sur le site "http://www.cecill.info". *)
  14. (* *)
  15. (* En contrepartie de l'accessibilité au code source et des droits de copie, *)
  16. (* de modification et de redistribution accordés par cette licence, il n'est *)
  17. (* offert aux utilisateurs qu'une garantie limitée. Pour les mêmes raisons, *)
  18. (* seule une responsabilité restreinte pèse sur l'auteur du programme, le *)
  19. (* titulaire des droits patrimoniaux et les concédants successifs. *)
  20. (* *)
  21. (* A cet égard l'attention de l'utilisateur est attirée sur les risques *)
  22. (* associés au chargement, à l'utilisation, à la modification et/ou au *)
  23. (* développement et à la reproduction du logiciel par l'utilisateur étant *)
  24. (* donné sa spécificité de logiciel libre, qui peut le rendre complexe à *)
  25. (* manipuler et qui le réserve donc à des développeurs et des professionnels *)
  26. (* avertis possédant des connaissances informatiques approfondies. Les *)
  27. (* utilisateurs sont donc invités à charger et tester l'adéquation du *)
  28. (* logiciel à leurs besoins dans des conditions permettant d'assurer la *)
  29. (* sécurité de leurs systèmes et ou de leurs données et, plus généralement, *)
  30. (* à l'utiliser et l'exploiter dans les mêmes conditions de sécurité. *)
  31. (* *)
  32. (* Le fait que vous puissiez accéder à cet en-tête signifie que vous avez *)
  33. (* pris connaissance de la licence CeCILL, et que vous en avez accepté les *)
  34. (* termes. *)
  35. (******************************************************************************)
  36. open Core.Std;;
  37. (* XXX Using and keyword because each function can call each other *)
  38. (* Function to read the tmp file *)
  39. let rec read () =
  40. (* Short name *)
  41. let name = Const.tmp_file in
  42. (* Get the string corresponding to the file *)
  43. let file_content = In_channel.read_all name in
  44. try
  45. Tmp_biniou_b.tmp_file_of_string file_content
  46. (* In previous version, the JSON format was used, otherwise the file can
  47. * have a bad format. In this case, the Ag_ob_run.Error("Read error (1)")
  48. * exeption is throw. We catch it here *)
  49. with Ag_ob_run.Error("Read error (1)") | Bi_inbuf.End_of_input ->
  50. (* If file is not in the right format, delete it and create a new one.
  51. * Then, read it *)
  52. printf "Reinitialises tmp file\n"; (* TODO Make it settable *)
  53. Sys.remove name;
  54. create_tmp_file ();
  55. read ()
  56. (* Function to write the tmp file *)
  57. and write (tmp_file:Tmp_biniou_t.tmp_file) =
  58. (* Short name *)
  59. let name = Const.tmp_file in
  60. let biniou_tmp = Tmp_biniou_b.string_of_tmp_file tmp_file in
  61. Out_channel.write_all name ~data:biniou_tmp
  62. (* Function to create the tmp file *)
  63. and create_tmp_file () =
  64. (* Short name *)
  65. let name = Const.tmp_file in
  66. Tmp_biniou_v.create_tmp_file ~command:[] ~number:0 ()
  67. (* Convert it to biniou *)
  68. |> write
  69. ;;
  70. (* Function to open tmp file *)
  71. let rec init () =
  72. (* If file do not exists, create it *)
  73. let file_exists = Sys.file_exists Const.tmp_file in
  74. match file_exists with
  75. | `No -> create_tmp_file ();
  76. init ()
  77. | `Unknown -> begin
  78. Sys.remove Const.tmp_file;
  79. init ()
  80. end
  81. | `Yes -> read ()
  82. ;;
  83. (* Verify that the value exist *)
  84. let verify_key_exist ~key entry =
  85. if entry = key then
  86. true
  87. else
  88. false
  89. ;;
  90. (* Return true if a program is in the rc file *)
  91. let rec is_prog_in_rc list_from_rc_file program =
  92. match list_from_rc_file with
  93. (* | None -> is_prog_in_rc program ~liste_from_rc_file:rc_content.progs *)
  94. | [] -> false
  95. | hd :: tl -> if hd = program then true else is_prog_in_rc tl program
  96. ;;
  97. (* Log when a program has been launched in a file in /tmp
  98. ~func is the function applied to the value *)
  99. let log ?(func= (+) 1 ) () =
  100. (* We will use tmp_file type *)
  101. let open Tmp_biniou_t in
  102. (* Make sure that file exists, otherwise strange things appears *)
  103. let file = init () in
  104. (* Write the file with the new value *)
  105. write { file with number = (func file.number)}
  106. ;;
  107. (* Reset command number in two ways :
  108. * if cmd_num is 0, delete tmp file, to reinitialise program
  109. * if cmd_num is 0>, set to this value
  110. * else display an error message *)
  111. let reset cmd_num=
  112. match cmd_num with
  113. | 0 -> Sys.remove Const.tmp_file
  114. | n when n > 0 ->
  115. (* Set the number *)
  116. log ~func:((fun a b -> a) n) ()
  117. | _ -> printf "Invalid number\n" (* TODO Make it settable *)
  118. ;;