Fix file_contains_string so it matches exact strings, not regexps.
[goaljobs.git] / goaljobs.ml
index 9688a9c..4024b6d 100644 (file)
@@ -33,11 +33,31 @@ exception Goal_result of goal_result_t
 
 let goal_failed msg = raise (Goal_result (Goal_failed msg))
 
+let depth = ref 0
+let indent fs =
+  let do_indent str =
+    prerr_string "| ";
+    for i = 0 to !depth-1 do prerr_string "  " done;
+    prerr_string str;
+    Pervasives.flush Pervasives.stderr
+  in
+  ksprintf do_indent fs
+
 let target v =
   if v then raise (Goal_result Goal_OK)
 let target_all vs = target (List.fold_left (&&) true vs)
 let target_exists vs = target (List.fold_left (||) false vs)
-let require f = f ()
+let require name f =
+  indent "require: %s\n" name;
+  incr depth;
+  let r = (try Either (f ()) with exn -> Or exn) in
+  decr depth;
+  match r with
+  | Either x -> x
+  | Or exn -> raise exn
+
+let _enter_goal name = indent "enter goal: %s\n" name
+let _leave_goal name = indent "leave goal: %s\n" name
 
 type period_t = Seconds | Days | Months | Years
 let seconds = (1, Seconds)
@@ -196,7 +216,7 @@ let url_exists url =
     goal_failed msg
 
 let file_contains_string filename str =
-  let cmd = sprintf "grep -q %s %s" (quote str) (quote filename) in
+  let cmd = sprintf "grep -q -F %s %s" (quote str) (quote filename) in
   match Sys.command cmd with
   | 0 -> true
   | 1 -> false
@@ -340,6 +360,7 @@ let with_memory_locked ?(write = false) f =
   let filename = getenv "HOME" // ".goaljobs-memory" in
   let fd = openfile filename [O_RDWR; O_CREAT] 0o644 in
   lockf fd (if write then F_LOCK else F_RLOCK) 0;
+
   (* If the file is newly created with zero size, write an
    * empty hash table.
    *)
@@ -354,6 +375,7 @@ let with_memory_locked ?(write = false) f =
   (* Run the function. *)
   let r = try Either (f fd) with exn -> Or exn in
   lockf fd F_ULOCK 0;
+  close fd;
   match r with
   | Either x -> x
   | Or exn -> raise exn
@@ -421,6 +443,7 @@ let mailto ?from ~subject ?(attach = []) to_ =
   List.iter (
     fun a -> cmd := !cmd ^ " -a " ^ quote a
   ) attach;
+  cmd := !cmd ^ " " ^ quote to_;
   if Sys.command !cmd <> 0 then
     goal_failed "mailto: could not send email"