2 * Copyright (c) 2013, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Confizery Expression
35 * This module provides functions to parse an expression string and
36 * convert it in a logical expression tree, that can be evaluated,
37 * according to the variables defined in the configuration tree.
39 * It is used in configuration tree file, for instance in the "if"
40 * node or the "default" attribute.
43 #include <sys/queue.h>
45 #include "cfzy_list.h"
48 * Type of operand in expression tree (from low to high priority)
62 * Structure describing an expression tree
64 * An expression tree is a binary tree. Each node contains an
65 * operator, or a variable name. If it's a unary operator, only the
66 * right child is used, else both left and right children are set.
69 TAILQ_ENTRY(expr) next; /**< next entry in list */
70 enum cfzy_expr_op optype; /**< operator type */
71 char *varname; /**< var name, only valid if type is OP_VAR */
72 struct cfzy_expr *left; /**< left child */
73 struct cfzy_expr *right; /**< right child */
77 * Parse a string and convert it in an expression tree
80 * String containing an expression
82 * Expression tree, or NULL on error
84 struct cfzy_expr *cfzy_expr_parse(const char *buf);
87 * Free an expression tree previously allocated with cfzy_expr_parse()
92 void cfzy_expr_free(struct cfzy_expr *exp);
95 * Write an expression tree in a string buffer
97 * Dump the expression 'exp' as a string into the buffer 'buf' of
98 * length 'len'. The output string is always nul-terminated.
101 * Expression tree to dump
105 * Len of the destination buffer
107 * Number of written bytes on success (not including '\0'), else
110 int cfzy_expr_to_str(const struct cfzy_expr *exp, char *buf, int len);
113 * Evaluate an expression tree
115 * Evaluate the given expression tree according to variables defined
119 * Expression tree to evaluate
121 * Function used to evaluate a variable. This function should
122 * return the value of the variable (positive), or a negative value
123 * on error (variable invalid or not found).
124 * This argument can be NULL, in this case only "true" and "false" are
127 * Second argument given as-is to getvalue() function.
129 * The value of the expression (greater or equal to 0), or -1 on
132 int cfzy_expr_eval(const struct cfzy_expr *exp,
133 int (*getvalue)(const char *, void *), void *opaque_arg);
136 * Dump the expression graph in a file
138 * Useful for debug purpose only. The output can be parsed with
139 * cfzy_expr_graph.py.
144 * Expression tree to dump
146 * 0 on succes, -1 on error
148 int cfzy_expr_graph_dump(const char *filename, const struct cfzy_expr *exp);
151 * Return a list containing all variables on which expression depend
153 * Allocate a list head, and for each variable of the expression,
154 * allocate an element and add it in the list. Each element points to
155 * a string which is allocated and should be freed by the user. Each
156 * variable is present only once in the list.
161 * List containing the variables (it can be empty), or NULL on error.
163 struct cfzy_list_head *cfzy_expr_get_vars(const struct cfzy_expr *exp);
165 #endif /* _CFZY_EXPR_H_ */