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