2 * Copyright (C) 2012 Red Hat Inc.
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.
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.
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.
25 module StringMap = struct
26 include Map.Make (String)
27 let keys m = fold (fun k _ ks -> k :: ks) m []
28 let values m = fold (fun _ v vs -> v :: vs) m []
31 module StringSet = Set.Make (String)
36 | Expr_int of Big_int.big_int
39 | Expr_and of whenexpr * whenexpr
40 | Expr_or of whenexpr * whenexpr
41 | Expr_eq of whenexpr * whenexpr
42 | Expr_not of whenexpr
43 | Expr_changes of string
45 (* This internal type is used during conversion of the OCaml AST
46 * to the whenexpr type.
51 | IExpr_int of Big_int.big_int
52 | IExpr_float of float
54 | IExpr_app of string * whenexpr_int list
56 (* Note that days are not necessarily expressible in seconds (because
57 * of leap seconds), months are not expressible in days (because months
58 * have different lengths), and years are not expressible in days
59 * (because of leap days) although we could save a case here by
60 * expressing years in months.
63 | Every_seconds of int
79 let variable_of_rpc = function
80 | `bool_t b -> T_bool b
81 | `string_t s -> T_string s
82 | `int_t i -> T_int (big_int_of_string i)
83 | `float_t f -> T_float f
85 let rpc_of_variable = function
86 | T_bool b -> `bool_t b
87 | T_string s -> `string_t s
88 | T_int i -> `int_t (string_of_big_int i)
89 | T_float f -> `float_t f
91 type variables = variable StringMap.t
94 (* The result of the previous evaluation. This is used for
95 * implementing edge-triggering, since we only trigger the job to run
96 * when the state changes from false -> true.
98 job_prev_eval_state : bool;
100 (* When the job {i ran} last time, we take a copy of the variables.
101 * This allows us to implement the 'changes' operator.
103 job_prev_variables : variables;
107 { job_prev_eval_state = false; job_prev_variables = StringMap.empty }
110 | When_job of whenexpr
111 | Every_job of periodexpr
117 job_script : shell_script;
118 job_private : job_private;
121 let rec expr_of_ast _loc ast =
122 expr_of_iexpr _loc (iexpr_of_ast _loc ast)
124 and iexpr_of_ast _loc = function
125 | ExId (_, IdLid (_, "true")) -> IExpr_bool true
126 | ExId (_, IdLid (_, "false")) -> IExpr_bool false
127 | ExStr (_, str) -> IExpr_str str
128 | ExInt (_, i) -> IExpr_int (big_int_of_string i) (* XXX too large? *)
129 | ExFlo (_, f) -> IExpr_float (float_of_string f)
130 | ExId (_, IdLid (_, id)) -> IExpr_var id
132 (* In the OCaml AST, functions are curried right to left, so we
133 * must uncurry to get the list of arguments.
135 | ExApp (_, left_tree, right_arg) ->
136 let f, left_args = uncurry_app_tree _loc left_tree in
137 IExpr_app (f, List.rev_map (iexpr_of_ast _loc) (right_arg :: left_args))
140 (* https://groups.google.com/group/fa.caml/browse_thread/thread/f35452d085654bd6 *)
141 eprintf "expr_of_ast: invalid expression: %!";
142 let e = Ast.StExp (_loc, e) in
143 Printers.OCaml.print_implem ~output_file:"/dev/stderr" e;
145 invalid_arg (sprintf "%s: invalid expression" (Loc.to_string _loc))
147 and uncurry_app_tree _loc = function
148 | ExId (_, IdLid (_, f)) -> f, []
149 | ExApp (_, left_tree, right_arg) ->
150 let f, left_args = uncurry_app_tree _loc left_tree in
151 f, (right_arg :: left_args)
153 eprintf "uncurry_app_tree: invalid expression: %!";
154 let e = Ast.StExp (_loc, e) in
155 Printers.OCaml.print_implem ~output_file:"/dev/stderr" e;
157 invalid_arg (sprintf "%s: invalid expression" (Loc.to_string _loc))
159 and expr_of_iexpr _loc = function
160 | IExpr_bool b -> Expr_bool b
161 | IExpr_str s -> Expr_str s
162 | IExpr_int i -> Expr_int i
163 | IExpr_float f -> Expr_float f
164 | IExpr_var v -> Expr_var v
166 | IExpr_app ("&&", exprs) ->
168 | [e1; e2] -> Expr_and (expr_of_iexpr _loc e1, expr_of_iexpr _loc e2)
170 invalid_arg (sprintf "%s: && operator must be applied to two parameters"
171 (Loc.to_string _loc))
174 | IExpr_app ("||", exprs) ->
176 | [e1; e2] -> Expr_or (expr_of_iexpr _loc e1, expr_of_iexpr _loc e2)
178 invalid_arg (sprintf "%s: || operator must be applied to two parameters"
179 (Loc.to_string _loc))
182 | IExpr_app (("="|"=="), exprs) ->
184 | [e1; e2] -> Expr_eq (expr_of_iexpr _loc e1, expr_of_iexpr _loc e2)
186 invalid_arg (sprintf "%s: = operator must be applied to two parameters"
187 (Loc.to_string _loc))
190 | IExpr_app ("!", exprs) ->
192 | [e1] -> Expr_not (expr_of_iexpr _loc e1)
194 invalid_arg (sprintf "%s: ! operator must be applied to one parameter"
195 (Loc.to_string _loc))
198 | IExpr_app (("change"|"changes"|"changed"), [IExpr_var v]) ->
201 | IExpr_app (("change"|"changes"|"changed"), _) ->
202 invalid_arg (sprintf "%s: 'changes' operator must be followed by a variable name"
203 (Loc.to_string _loc))
205 | IExpr_app (op, _) ->
206 invalid_arg (sprintf "%s: unknown operator in expression: %s"
207 (Loc.to_string _loc) op)
209 let rec string_of_whenexpr = function
210 | Expr_bool b -> sprintf "%b" b
211 | Expr_str s -> sprintf "%S" s
212 | Expr_int i -> sprintf "%s" (string_of_big_int i)
213 | Expr_float f -> sprintf "%f" f
214 | Expr_var v -> sprintf "%s" v
215 | Expr_and (e1, e2) ->
216 sprintf "%s && %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
217 | Expr_or (e1, e2) ->
218 sprintf "%s || %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
219 | Expr_eq (e1, e2) ->
220 sprintf "%s == %s" (string_of_whenexpr e1) (string_of_whenexpr e2)
221 | Expr_not e -> sprintf "! %s" (string_of_whenexpr e)
222 | Expr_changes v -> sprintf "changes %s" v
224 let string_of_periodexpr = function
225 | Every_seconds 1 -> "1 second"
226 | Every_seconds i -> sprintf "%d seconds" i
227 | Every_days 1 -> "1 day"
228 | Every_days i -> sprintf "%d days" i
229 | Every_months 1 -> "1 month"
230 | Every_months i -> sprintf "%d months" i
231 | Every_years 1 -> "1 year"
232 | Every_years i -> sprintf "%d years" i
234 let rec dependencies_of_whenexpr = function
240 | Expr_and (e1, e2) ->
241 dependencies_of_whenexpr e1 @ dependencies_of_whenexpr e2
242 | Expr_or (e1, e2) ->
243 dependencies_of_whenexpr e1 @ dependencies_of_whenexpr e2
244 | Expr_eq (e1, e2) ->
245 dependencies_of_whenexpr e1 @ dependencies_of_whenexpr e2
246 | Expr_not e -> dependencies_of_whenexpr e
247 | Expr_changes v -> [v]
249 let dependencies_of_job = function
250 | { job_cond = When_job whenexpr } -> dependencies_of_whenexpr whenexpr
251 | { job_cond = Every_job _ } -> []
253 let rec eval_whenexpr job variables = function
254 | Expr_bool b -> T_bool b
255 | Expr_str s -> T_string s
256 | Expr_int i -> T_int i
257 | Expr_float f -> T_float f
260 (try StringMap.find v variables with Not_found -> T_string "")
262 | Expr_and (e1, e2) ->
263 if eval_whenexpr_as_bool job variables e1 &&
264 eval_whenexpr_as_bool job variables e2 then
269 | Expr_or (e1, e2) ->
270 if eval_whenexpr_as_bool job variables e1 ||
271 eval_whenexpr_as_bool job variables e2 then
276 | Expr_eq (e1, e2) ->
277 let e1 = eval_whenexpr job variables e1
278 and e2 = eval_whenexpr job variables e2 in
279 if 0 = compare_values e1 e2 then
285 if not (eval_whenexpr_as_bool job variables e) then
292 try StringMap.find v job.job_private.job_prev_variables
293 with Not_found -> T_string "" in
295 try StringMap.find v variables
296 with Not_found -> T_string "" in
298 if 0 <> compare_values prev_value curr_value then
303 (* Call {!eval_whenexpr} and cast the result to a boolean. *)
304 and eval_whenexpr_as_bool job variables expr =
305 match eval_whenexpr job variables expr with
307 | T_string s -> s <> ""
308 | T_int i -> sign_big_int i <> 0
309 | T_float f -> f <> 0.
311 (* Do a comparison on two typed values and return -1/0/+1. If the
312 * types are different then we compare the values as strings. The user
313 * can avoid this by specifying types.
315 and compare_values value1 value2 =
316 match value1, value2 with
317 | T_bool b1, T_bool b2 -> compare b1 b2
318 | T_string s1, T_string s2 -> compare s1 s2
319 | T_int i1, T_int i2 -> compare_big_int i1 i2
320 | T_float f1, T_float f2 -> compare f1 f2
322 let value1 = value_as_string value1
323 and value2 = value_as_string value2 in
324 compare value1 value2
326 and value_as_string = function
327 | T_bool b -> string_of_bool b
329 | T_int i -> string_of_big_int i
330 | T_float f -> string_of_float f
332 let job_evaluate job variables =
334 | { job_cond = Every_job _ } -> false, job
335 | { job_cond = When_job whenexpr } ->
336 let state = eval_whenexpr_as_bool job variables whenexpr in
338 (* Because jobs are edge-triggered, we're only interested in the
339 * case where the evaluation state changes from false -> true.
341 match job.job_private.job_prev_eval_state, state with
345 let jobp = { job.job_private with job_prev_eval_state = state } in
346 let job = { job with job_private = jobp } in
350 let jobp = { job_prev_eval_state = true;
351 job_prev_variables = variables } in
352 let job = { job with job_private = jobp } in