Rename 'cleanup' to 'post'.
[whenjobs.git] / lib / whenexpr.ml
index 8ccde2c..2cb6863 100644 (file)
@@ -83,6 +83,23 @@ type shell_script = {
   sh_script : string;
 }
 
+type preinfo = {
+  pi_job_name : string;
+  pi_serial : Big_int.big_int;
+}
+
+type result = {
+  res_job_name : string;
+  res_serial : Big_int.big_int;
+  res_code : int;
+  res_tmpdir : string;
+  res_output : string;
+  res_start_time : float;
+}
+
+type pre = preinfo -> bool
+type post = result -> unit
+
 type variable =
   | T_unit
   | T_bool of bool
@@ -113,6 +130,8 @@ type job_cond =
 type job = {
   job_loc : Loc.t;
   job_name : string;
+  job_pre : pre option;
+  job_post : post option;
   job_cond : job_cond;
   job_script : shell_script;
 }
@@ -616,3 +635,22 @@ let next_periodexpr =
     let t0 = Date.make 1970 1 1 in
     let t' = Date.add t0 (Date.Period.month months) in
     Date.to_unixfloat t'
+
+let check_valid_variable_name name =
+  (* Don't permit certain names. *)
+  if name = "JOBSERIAL" then
+    failwith "JOBSERIAL variable cannot be set";
+
+  let len = String.length name in
+  if len = 0 then
+    failwith "variable name is an empty string";
+  if name.[0] <> '_' && not (isalpha name.[0]) then
+    failwith "variable name must start with alphabetic character or underscore";
+
+  let rec loop i =
+    if i >= len then ()
+    else if name.[i] <> '_' && not (isalnum name.[i]) then
+      failwith "variable name contains non-alphanumeric non-underscore character"
+    else loop (i+1)
+  in
+  loop 1