Implement cleanup functions, including 'mailto'.
[whenjobs.git] / lib / whenexpr.ml
1 (* whenjobs
2  * Copyright (C) 2012 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  *)
18
19 open Camlp4.PreCast
20 open Ast
21
22 open CalendarLib
23
24 open Whenutils
25
26 open Big_int
27 open Unix
28 open Printf
29
30 type whenexpr =
31   | Expr_unit
32   | Expr_bool of bool
33   | Expr_str of string
34   | Expr_int of Big_int.big_int
35   | Expr_float of float
36   | Expr_var of string
37   | Expr_and of whenexpr * whenexpr
38   | Expr_or of whenexpr * whenexpr
39   | Expr_lt of whenexpr * whenexpr
40   | Expr_le of whenexpr * whenexpr
41   | Expr_eq of whenexpr * whenexpr
42   | Expr_ge of whenexpr * whenexpr
43   | Expr_gt of whenexpr * whenexpr
44   | Expr_not of whenexpr
45   | Expr_add of whenexpr * whenexpr
46   | Expr_sub of whenexpr * whenexpr
47   | Expr_mul of whenexpr * whenexpr
48   | Expr_div of whenexpr * whenexpr
49   | Expr_mod of whenexpr * whenexpr
50   | Expr_len of whenexpr
51   | Expr_changes of string
52   | Expr_increases of string
53   | Expr_decreases of string
54   | Expr_prev of string
55   | Expr_reloaded
56
57 (* This internal type is used during conversion of the OCaml AST
58  * to the whenexpr type.
59  *)
60 type whenexpr_int =
61   | IExpr_unit
62   | IExpr_bool of bool
63   | IExpr_str of string
64   | IExpr_int of Big_int.big_int
65   | IExpr_float of float
66   | IExpr_var of string
67   | IExpr_app of string * whenexpr_int list
68
69 (* Note that days are not necessarily expressible in seconds (because
70  * of leap seconds), months are not expressible in days (because months
71  * have different lengths), and years are not expressible in days
72  * (because of leap days) although we could save a case here by
73  * expressing years in months.
74  *)
75 type periodexpr =
76   | Every_seconds of int
77   | Every_days of int
78   | Every_months of int
79   | Every_years of int
80
81 type shell_script = {
82   sh_loc : Loc.t;
83   sh_script : string;
84 }
85
86 type result = {
87   res_job_name : string;
88   res_code : int;
89   res_tmpdir : string;
90   res_output : string;
91 }
92
93 type cleanup = result -> unit
94
95 type variable =
96   | T_unit
97   | T_bool of bool
98   | T_string of string
99   | T_int of big_int
100   | T_float of float
101
102 let variable_of_rpc = function
103   | `unit_t -> T_unit
104   | `bool_t b -> T_bool b
105   | `string_t s -> T_string s
106   | `int_t i -> T_int (big_int_of_string i)
107   | `float_t f -> T_float f
108
109 let rpc_of_variable = function
110   | T_unit -> `unit_t
111   | T_bool b -> `bool_t b
112   | T_string s -> `string_t s
113   | T_int i -> `int_t (string_of_big_int i)
114   | T_float f -> `float_t f
115
116 type variables = variable StringMap.t
117
118 type job_cond =
119   | When_job of whenexpr
120   | Every_job of periodexpr
121
122 type job = {
123   job_loc : Loc.t;
124   job_name : string;
125   job_cleanup : cleanup option;
126   job_cond : job_cond;
127   job_script : shell_script;
128 }
129
130 let rec expr_of_ast _loc ast =
131   expr_of_iexpr _loc (iexpr_of_ast _loc ast)
132
133 and iexpr_of_ast _loc = function
134   | ExId (_, IdUid (_, "()")) -> IExpr_unit
135   | ExId (_, IdUid (_, "True")) -> IExpr_bool true
136   | ExId (_, IdUid (_, "False")) -> IExpr_bool false
137   | ExStr (_, str) -> IExpr_str str
138   | ExInt (_, i) -> IExpr_int (big_int_of_string i) (* XXX too large? *)
139   | ExFlo (_, f) -> IExpr_float (float_of_string f)
140   | ExId (_, IdLid (_, id)) -> IExpr_var id
141
142   (* In the OCaml AST, functions are curried right to left, so we
143    * must uncurry to get the list of arguments.
144    *)
145   | ExApp (_, left_tree, right_arg) ->
146     let f, left_args = uncurry_app_tree _loc left_tree in
147     IExpr_app (f, List.rev_map (iexpr_of_ast _loc) (right_arg :: left_args))
148
149   | e ->
150     (* https://groups.google.com/group/fa.caml/browse_thread/thread/f35452d085654bd6 *)
151     eprintf "expr_of_ast: invalid expression: %!";
152     let e = Ast.StExp (_loc, e) in
153     Printers.OCaml.print_implem ~output_file:"/dev/stderr" e;
154
155     invalid_arg (sprintf "%s: invalid expression" (Loc.to_string _loc))
156
157 and uncurry_app_tree _loc = function
158   | ExId (_, IdLid (_, f)) -> f, []
159   | ExApp (_, left_tree, right_arg) ->
160     let f, left_args = uncurry_app_tree _loc left_tree in
161     f, (right_arg :: left_args)
162   | e ->
163     eprintf "uncurry_app_tree: invalid expression: %!";
164     let e = Ast.StExp (_loc, e) in
165     Printers.OCaml.print_implem ~output_file:"/dev/stderr" e;
166
167     invalid_arg (sprintf "%s: invalid expression" (Loc.to_string _loc))
168
169 and expr_of_iexpr _loc = function
170   | IExpr_unit -> Expr_unit
171   | IExpr_bool b -> Expr_bool b
172   | IExpr_str s -> Expr_str s
173   | IExpr_int i -> Expr_int i
174   | IExpr_float f -> Expr_float f
175   | IExpr_var v -> Expr_var v
176
177   | IExpr_app ("&&", exprs) ->
178     two_params _loc "&&" exprs (fun e1 e2 -> Expr_and (e1, e2))
179
180   | IExpr_app ("||", exprs) ->
181     two_params _loc "||" exprs (fun e1 e2 -> Expr_or (e1, e2))
182
183   | IExpr_app ("<", exprs) ->
184     two_params _loc "<" exprs (fun e1 e2 -> Expr_lt (e1, e2))
185
186   | IExpr_app ("<=", exprs) ->
187     two_params _loc "<=" exprs (fun e1 e2 -> Expr_le (e1, e2))
188
189   | IExpr_app (("="|"=="), exprs) ->
190     two_params _loc "=" exprs (fun e1 e2 -> Expr_eq (e1, e2))
191
192   | IExpr_app (">=", exprs) ->
193     two_params _loc ">=" exprs (fun e1 e2 -> Expr_ge (e1, e2))
194
195   | IExpr_app (">", exprs) ->
196     two_params _loc ">" exprs (fun e1 e2 -> Expr_gt (e1, e2))
197
198   | IExpr_app ("!", exprs) ->
199     one_param _loc "!" exprs (fun e1 -> Expr_not e1)
200
201   | IExpr_app ("+", exprs) ->
202     two_params _loc "+" exprs (fun e1 e2 -> Expr_add (e1, e2))
203
204   | IExpr_app ("-", exprs) ->
205     two_params _loc "+" exprs (fun e1 e2 -> Expr_sub (e1, e2))
206
207   | IExpr_app ("*", exprs) ->
208     two_params _loc "+" exprs (fun e1 e2 -> Expr_mul (e1, e2))
209
210   | IExpr_app ("/", exprs) ->
211     two_params _loc "+" exprs (fun e1 e2 -> Expr_div (e1, e2))
212
213   | IExpr_app ("mod", exprs) ->
214     two_params _loc "+" exprs (fun e1 e2 -> Expr_mod (e1, e2))
215
216   | IExpr_app (("len"|"length"|"size"), exprs) ->
217     one_param _loc "len" exprs (fun e1 -> Expr_len e1)
218
219   | IExpr_app (("change"|"changes"|"changed"), [IExpr_var v]) ->
220     Expr_changes v
221
222   | IExpr_app (("inc"|"increase"|"increases"|"increased"), [IExpr_var v]) ->
223     Expr_increases v
224
225   | IExpr_app (("dec"|"decrease"|"decreases"|"decreased"), [IExpr_var v]) ->
226     Expr_decreases v
227
228   | IExpr_app (("prev"|"previous"), [IExpr_var v]) ->
229     Expr_prev v
230
231   | IExpr_app (("change"|"changes"|"changed"|"inc"|"increase"|"increases"|"increased"|"dec"|"decrease"|"decreases"|"decreased"|"prev"|"previous") as op, _) ->
232     invalid_arg (sprintf "%s: '%s' operator must be followed by a variable name"
233                    (Loc.to_string _loc) op)
234
235   | IExpr_app ("reloaded", [IExpr_unit]) ->
236     Expr_reloaded
237
238   | IExpr_app ("reloaded", _) ->
239     invalid_arg (sprintf "%s: you must use 'reloaded ()'" (Loc.to_string _loc))
240
241   | IExpr_app (op, _) ->
242     invalid_arg (sprintf "%s: unknown operator in expression: %s"
243                    (Loc.to_string _loc) op)
244
245 and two_params _loc op exprs f =
246   match exprs with
247   | [e1; e2] -> f (expr_of_iexpr _loc e1) (expr_of_iexpr _loc e2)
248   | _ ->
249     invalid_arg (sprintf "%s: %s operator must be applied to two parameters"
250                    op (Loc.to_string _loc))
251
252 and one_param _loc op exprs f =
253   match exprs with
254   | [e1] -> f (expr_of_iexpr _loc e1)
255   | _ ->
256     invalid_arg (sprintf "%s: %s operator must be applied to one parameter"
257                    op (Loc.to_string _loc))
258
259 let rec string_of_whenexpr = function
260   | Expr_unit -> "()"
261   | Expr_bool b -> sprintf "%b" b
262   | Expr_str s -> sprintf "%S" s
263   | Expr_int i -> sprintf "%s" (string_of_big_int i)
264   | Expr_float f -> sprintf "%f" f
265   | Expr_var v -> sprintf "%s" v
266   | Expr_and (e1, e2) ->
267     sprintf "%s && %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
268   | Expr_or (e1, e2) ->
269     sprintf "%s || %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
270   | Expr_lt (e1, e2) ->
271     sprintf "%s < %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
272   | Expr_le (e1, e2) ->
273     sprintf "%s <= %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
274   | Expr_eq (e1, e2) ->
275     sprintf "%s == %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
276   | Expr_ge (e1, e2) ->
277     sprintf "%s >= %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
278   | Expr_gt (e1, e2) ->
279     sprintf "%s > %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
280   | Expr_not e -> sprintf "! %s" (string_of_whenexpr e)
281   | Expr_add (e1, e2) ->
282     sprintf "%s + %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
283   | Expr_sub (e1, e2) ->
284     sprintf "%s - %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
285   | Expr_mul (e1, e2) ->
286     sprintf "%s * %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
287   | Expr_div (e1, e2) ->
288     sprintf "%s / %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
289   | Expr_mod (e1, e2) ->
290     sprintf "%s mod %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
291   | Expr_len e -> sprintf "len %s" (string_of_whenexpr e)
292   | Expr_changes v -> sprintf "changes %s" v
293   | Expr_increases v -> sprintf "increases %s" v
294   | Expr_decreases v -> sprintf "decreases %s" v
295   | Expr_prev v -> sprintf "prev %s" v
296   | Expr_reloaded -> "reloaded ()"
297
298 let string_of_periodexpr = function
299   | Every_seconds 1 -> "1 second"
300   | Every_seconds i -> sprintf "%d seconds" i
301   | Every_days 1 -> "1 day"
302   | Every_days i -> sprintf "%d days" i
303   | Every_months 1 -> "1 month"
304   | Every_months i -> sprintf "%d months" i
305   | Every_years 1 -> "1 year"
306   | Every_years i -> sprintf "%d years" i
307
308 let rec dependencies_of_whenexpr = function
309   | Expr_unit -> []
310   | Expr_bool _ -> []
311   | Expr_str _ -> []
312   | Expr_int _ -> []
313   | Expr_float _ -> []
314   | Expr_var v -> [v]
315   | Expr_and (e1, e2)
316   | Expr_or (e1, e2)
317   | Expr_lt (e1, e2)
318   | Expr_le (e1, e2)
319   | Expr_eq (e1, e2)
320   | Expr_ge (e1, e2)
321   | Expr_gt (e1, e2)
322   | Expr_add (e1, e2)
323   | Expr_sub (e1, e2)
324   | Expr_mul (e1, e2)
325   | Expr_div (e1, e2)
326   | Expr_mod (e1, e2) ->
327     dependencies_of_whenexpr e1 @ dependencies_of_whenexpr e2
328   | Expr_not e
329   | Expr_len e ->
330     dependencies_of_whenexpr e
331   | Expr_changes v
332   | Expr_increases v
333   | Expr_decreases v
334   | Expr_prev v -> [v]
335   | Expr_reloaded -> []
336
337 let dependencies_of_job = function
338   | { job_cond = When_job whenexpr } -> dependencies_of_whenexpr whenexpr
339   | { job_cond = Every_job _ } -> []
340
341 let rec eval_whenexpr variables prev_variables onload = function
342   | Expr_unit -> T_unit
343   | Expr_bool b -> T_bool b
344   | Expr_str s -> T_string s
345   | Expr_int i -> T_int i
346   | Expr_float f -> T_float f
347
348   | Expr_var v ->
349     get_variable variables v
350
351   | Expr_and (e1, e2) ->
352     if eval_whenexpr_as_bool variables prev_variables onload e1 &&
353        eval_whenexpr_as_bool variables prev_variables onload e2 then
354       T_bool true
355     else
356       T_bool false
357
358   | Expr_or (e1, e2) ->
359     if eval_whenexpr_as_bool variables prev_variables onload e1 ||
360        eval_whenexpr_as_bool variables prev_variables onload e2 then
361       T_bool true
362     else
363       T_bool false
364
365   | Expr_lt (e1, e2) ->
366     let e1 = eval_whenexpr variables prev_variables onload e1
367     and e2 = eval_whenexpr variables prev_variables onload e2 in
368     if compare_values e1 e2 < 0 then
369       T_bool true
370     else
371       T_bool false
372
373   | Expr_le (e1, e2) ->
374     let e1 = eval_whenexpr variables prev_variables onload e1
375     and e2 = eval_whenexpr variables prev_variables onload e2 in
376     if compare_values e1 e2 <= 0 then
377       T_bool true
378     else
379       T_bool false
380
381   | Expr_eq (e1, e2) ->
382     let e1 = eval_whenexpr variables prev_variables onload e1
383     and e2 = eval_whenexpr variables prev_variables onload e2 in
384     if compare_values e1 e2 = 0 then
385       T_bool true
386     else
387       T_bool false
388
389   | Expr_ge (e1, e2) ->
390     let e1 = eval_whenexpr variables prev_variables onload e1
391     and e2 = eval_whenexpr variables prev_variables onload e2 in
392     if compare_values e1 e2 >= 0 then
393       T_bool true
394     else
395       T_bool false
396
397   | Expr_gt (e1, e2) ->
398     let e1 = eval_whenexpr variables prev_variables onload e1
399     and e2 = eval_whenexpr variables prev_variables onload e2 in
400     if compare_values e1 e2 > 0 then
401       T_bool true
402     else
403       T_bool false
404
405   | Expr_not e ->
406     if not (eval_whenexpr_as_bool variables prev_variables onload e) then
407       T_bool true
408     else
409       T_bool false
410
411   | Expr_add (e1, e2) ->
412     let e1 = eval_whenexpr variables prev_variables onload e1
413     and e2 = eval_whenexpr variables prev_variables onload e2 in
414     add_values e1 e2
415
416   | Expr_sub (e1, e2) ->
417     let e1 = eval_whenexpr variables prev_variables onload e1
418     and e2 = eval_whenexpr variables prev_variables onload e2 in
419     sub_values e1 e2
420
421   | Expr_mul (e1, e2) ->
422     let e1 = eval_whenexpr variables prev_variables onload e1
423     and e2 = eval_whenexpr variables prev_variables onload e2 in
424     mul_values e1 e2
425
426   | Expr_div (e1, e2) ->
427     let e1 = eval_whenexpr variables prev_variables onload e1
428     and e2 = eval_whenexpr variables prev_variables onload e2 in
429     div_values e1 e2
430
431   | Expr_mod (e1, e2) ->
432     let e1 = eval_whenexpr variables prev_variables onload e1
433     and e2 = eval_whenexpr variables prev_variables onload e2 in
434     mod_values e1 e2
435
436   | Expr_len e ->
437     let e = eval_whenexpr variables prev_variables onload e in
438     let e = string_of_variable e in
439     T_int (big_int_of_int (String.length e))
440
441   | Expr_changes v ->
442     let prev_value, curr_value = get_prev_curr_value variables prev_variables v in
443     if compare_values prev_value curr_value <> 0 then
444       T_bool true
445     else
446       T_bool false
447
448   | Expr_increases v ->
449     let prev_value, curr_value = get_prev_curr_value variables prev_variables v in
450     if compare_values prev_value curr_value < 0 then
451       T_bool true
452     else
453       T_bool false
454
455   | Expr_decreases v ->
456     let prev_value, curr_value = get_prev_curr_value variables prev_variables v in
457     if compare_values prev_value curr_value > 0 then
458       T_bool true
459     else
460       T_bool false
461
462   | Expr_prev v ->
463     get_prev_variable prev_variables v
464
465   | Expr_reloaded ->
466     T_bool onload
467
468 and get_prev_curr_value variables prev_variables v =
469   let prev_value = get_prev_variable prev_variables v in
470   let curr_value = get_variable variables v in
471   prev_value, curr_value
472
473 and get_variable variables v =
474   try StringMap.find v variables with Not_found -> T_string ""
475
476 and get_prev_variable prev_variables v =
477   match prev_variables with
478   | None ->
479     (* Job has never run.  XXX Should do better here. *)
480     T_string ""
481   | Some prev_variables ->
482     get_variable prev_variables v
483
484 (* Call {!eval_whenexpr} and cast the result to a boolean. *)
485 and eval_whenexpr_as_bool variables prev_variables onload expr =
486   match eval_whenexpr variables prev_variables onload expr with
487   | T_unit -> false
488   | T_bool r -> r
489   | T_string s -> s <> ""
490   | T_int i -> sign_big_int i <> 0
491   | T_float f -> f <> 0.
492
493 (* Do a comparison on two typed values and return -1/0/+1.  If the
494  * types are different then we compare the values as strings.  The user
495  * can avoid this by specifying types.
496  *)
497 and compare_values value1 value2 =
498   match value1, value2 with
499   | T_bool b1, T_bool b2 -> compare b1 b2
500   | T_string s1, T_string s2 -> compare s1 s2
501   | T_int i1, T_int i2 -> compare_big_int i1 i2
502   | T_float f1, T_float f2 -> compare f1 f2
503     (* XXX BUG: int should be promoted to float in mixed numeric comparison *)
504   | _ ->
505     let value1 = string_of_variable value1
506     and value2 = string_of_variable value2 in
507     compare value1 value2
508
509 (* + operator is addition or string concatenation. *)
510 and add_values value1 value2 =
511   match value1, value2 with
512   | T_int i1, T_int i2 -> T_int (add_big_int i1 i2)
513   | T_float i1, T_float i2 -> T_float (i1 +. i2)
514   | T_int i1, T_float i2 -> T_float (float_of_big_int i1 +. i2)
515   | T_float i1, T_int i2 -> T_float (i1 +. float_of_big_int i2)
516   | T_string i1, T_string i2 -> T_string (i1 ^ i2)
517   | _ ->
518     invalid_arg
519       (sprintf "incompatible types in addition: %s + %s"
520          (printable_string_of_variable value1)
521          (printable_string_of_variable value2))
522
523 and sub_values value1 value2 =
524   match value1, value2 with
525   | T_int i1, T_int i2 -> T_int (sub_big_int i1 i2)
526   | T_float i1, T_float i2 -> T_float (i1 -. i2)
527   | T_int i1, T_float i2 -> T_float (float_of_big_int i1 -. i2)
528   | T_float i1, T_int i2 -> T_float (i1 -. float_of_big_int i2)
529   | _ ->
530     invalid_arg
531       (sprintf "incompatible types in subtraction: %s - %s"
532          (printable_string_of_variable value1)
533          (printable_string_of_variable value2))
534
535 and mul_values value1 value2 =
536   match value1, value2 with
537   | T_int i1, T_int i2 -> T_int (mult_big_int i1 i2)
538   | T_float i1, T_float i2 -> T_float (i1 *. i2)
539   | T_int i1, T_float i2 -> T_float (float_of_big_int i1 *. i2)
540   | T_float i1, T_int i2 -> T_float (i1 *. float_of_big_int i2)
541   | _ ->
542     invalid_arg
543       (sprintf "incompatible types in multiplication: %s * %s"
544          (printable_string_of_variable value1)
545          (printable_string_of_variable value2))
546
547 and div_values value1 value2 =
548   match value1, value2 with
549   | T_int i1, T_int i2 -> T_int (div_big_int i1 i2)
550   | T_float i1, T_float i2 -> T_float (i1 /. i2)
551   | T_int i1, T_float i2 -> T_float (float_of_big_int i1 /. i2)
552   | T_float i1, T_int i2 -> T_float (i1 /. float_of_big_int i2)
553   | _ ->
554     invalid_arg
555       (sprintf "incompatible types in division: %s / %s"
556          (printable_string_of_variable value1)
557          (printable_string_of_variable value2))
558
559 and mod_values value1 value2 =
560   match value1, value2 with
561   | T_int i1, T_int i2 -> T_int (mod_big_int i1 i2)
562   | T_float i1, T_float i2 -> T_float (mod_float i1 i2)
563   | T_int i1, T_float i2 -> T_float (mod_float (float_of_big_int i1) i2)
564   | T_float i1, T_int i2 -> T_float (mod_float i1 (float_of_big_int i2))
565   | _ ->
566     invalid_arg
567       (sprintf "incompatible types in modulo: %s mod %s"
568          (printable_string_of_variable value1)
569          (printable_string_of_variable value2))
570
571 and string_of_variable = function
572   | T_unit -> "" (* for string_of_variable, we don't want () here *)
573   | T_bool b -> string_of_bool b
574   | T_string s -> s
575   | T_int i -> string_of_big_int i
576   | T_float f -> string_of_float f
577
578 and printable_string_of_variable = function
579   | T_unit -> "()"
580   | T_bool b -> string_of_bool b
581   | T_string s -> sprintf "%S" s
582   | T_int i -> string_of_big_int i
583   | T_float f -> string_of_float f
584
585 let next_periodexpr =
586   (* Round up 'a' to the next multiple of 'i'. *)
587   let round_up_float a i =
588     let r = mod_float a i in
589     if r = 0. then a +. i else a +. (i -. r)
590   and round_up a i =
591     let r = a mod i in
592     if r = 0 then a + i else a + (i - r)
593   in
594
595   fun t -> function
596   | Every_seconds i ->
597     let i = float_of_int i in
598     round_up_float t i
599
600   | Every_years i ->
601     let tm = gmtime t in
602
603     (* Round 'tm' up to the first day of the next year. *)
604     let year = round_up tm.tm_year i in
605     let tm = { tm with tm_sec = 0; tm_min = 0; tm_hour = 0;
606                        tm_mday = 1; tm_mon = 0; tm_year = year } in
607     fst (mktime tm)
608
609   | Every_days i ->
610     let t = Date.from_unixfloat t in
611     let t0 = Date.make 1970 1 1 in
612
613     (* Number of whole days since Unix Epoch. *)
614     let nb_days = Date.Period.safe_nb_days (Date.sub t t0) in
615
616     let nb_days = round_up nb_days i in
617     let t' = Date.add t0 (Date.Period.day nb_days) in
618     Date.to_unixfloat t'
619
620   | Every_months i ->
621     (* Calculate number of whole months since Unix Epoch. *)
622     let tm = gmtime t in
623     let months = 12 * (tm.tm_year - 70) + tm.tm_mon in
624
625     let months = round_up months i in
626     let t0 = Date.make 1970 1 1 in
627     let t' = Date.add t0 (Date.Period.month months) in
628     Date.to_unixfloat t'
629
630 let check_valid_variable_name name =
631   (* Don't permit certain names. *)
632   if name = "JOBSERIAL" then
633     failwith "JOBSERIAL variable cannot be set";
634
635   let len = String.length name in
636   if len = 0 then
637     failwith "variable name is an empty string";
638   if name.[0] <> '_' && not (isalpha name.[0]) then
639     failwith "variable name must start with alphabetic character or underscore";
640
641   let rec loop i =
642     if i >= len then ()
643     else if name.[i] <> '_' && not (isalnum name.[i]) then
644       failwith "variable name contains non-alphanumeric non-underscore character"
645     else loop (i+1)
646   in
647   loop 1