| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- /**
- * @file BPredicate.h
- * @author Ambroz Bizjak <ambrop7@gmail.com>
- *
- * @section LICENSE
- *
- * This file is part of BadVPN.
- *
- * BadVPN is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2
- * as published by the Free Software Foundation.
- *
- * BadVPN is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
- *
- * @section DESCRIPTION
- *
- * Object that parses and evaluates a logical expression.
- * Allows the user to define custom functions than can be
- * used in the expression.
- *
- * Syntax and semantics for logical expressions:
- *
- * - true
- * Logical true constant. Evaluates to 1.
- *
- * - false
- * Logical false constant. Evaluates to 0.
- *
- * - NOT expression
- * Logical negation. If the expression evaluates to error, the
- * negation evaluates to error.
- *
- * - expression OR expression
- * Logical disjunction. The second expression is only evaluated
- * if the first expression evaluates to false. If a sub-expression
- * evaluates to error, the disjunction evaluates to error.
- *
- * - expression AND expression
- * Logical conjunction. The second expression is only evaluated
- * if the first expression evaluates to true. If a sub-expression
- * evaluates to error, the conjunction evaluates to error.
- *
- * - function(arg, ..., arg)
- * Evaluation of a user-provided function (function is the name of the
- * function, [a-zA-Z0-9_]+).
- * If the function with the given name does not exist, it evaluates to
- * error.
- * Arguments are evaluated from left to right. Each argument can either
- * be a logical expression or a string (characters enclosed in double
- * quotes, without any double quote).
- * If an argument is encountered, but all needed arguments have already
- * been evaluated, the function evaluates to error.
- * If an argument is of wrong type, it is not evaluated and the function
- * evaluates to error.
- * If an argument evaluates to error, the function evaluates to error.
- * If after all arguments have been evaluated, the function needs more
- * arguments, it evaluates to error.
- * Then the handler function is called. If it returns anything other
- * than 1 and 0, the function evaluates to error. Otherwise it evaluates
- * to what the handler function returned.
- */
- #ifndef BADVPN_PREDICATE_BPREDICATE_H
- #define BADVPN_PREDICATE_BPREDICATE_H
- #include <misc/debug.h>
- #include <structure/BAVL.h>
- #include <base/DebugObject.h>
- #define PREDICATE_TYPE_BOOL 1
- #define PREDICATE_TYPE_STRING 2
- #define PREDICATE_MAX_NAME 16
- #define PREDICATE_MAX_ARGS 16
- /**
- * Handler function called when evaluating a custom function in the predicate.
- *
- * @param user value passed to {@link BPredicateFunction_Init}
- * @param args arguments to the function. Points to an array of pointers (as many as the
- * function has arguments), where each pointer points to either to an int or
- * a zero-terminated string (depending on the type of the argument).
- * @return 1 for true, 0 for false, -1 for error
- */
- typedef int (*BPredicate_callback) (void *user, void **args);
- /**
- * Object that parses and evaluates a logical expression.
- * Allows the user to define custom functions than can be
- * used in the expression.
- */
- typedef struct {
- DebugObject d_obj;
- void *root;
- BAVL functions_tree;
- #ifndef NDEBUG
- int in_function;
- #endif
- } BPredicate;
- /**
- * Object that represents a custom function in {@link BPredicate}.
- */
- typedef struct {
- DebugObject d_obj;
- BPredicate *p;
- char name[PREDICATE_MAX_NAME + 1];
- int args[PREDICATE_MAX_ARGS];
- int num_args;
- BPredicate_callback callback;
- void *user;
- BAVLNode tree_node;
- } BPredicateFunction;
- /**
- * Initializes the object.
- *
- * @param p the object
- * @param str logical expression
- * @return 1 on success, 0 on failure
- */
- int BPredicate_Init (BPredicate *p, char *str) WARN_UNUSED;
- /**
- * Frees the object.
- * Must have no custom functions.
- * Must not be called from function handlers.
- *
- * @param p the object
- */
- void BPredicate_Free (BPredicate *p);
- /**
- * Evaluates the logical expression.
- * Must not be called from function handlers.
- *
- * @param p the object
- * @return 1 for true, 0 for false, -1 for error
- */
- int BPredicate_Eval (BPredicate *p);
- /**
- * Registers a custom function for {@link BPredicate}.
- * Must not be called from function handlers.
- *
- * @param o the object
- * @param p predicate to register the function for
- * @param args array of argument types. Each type is either PREDICATE_TYPE_BOOL or PREDICATE_TYPE_STRING.
- * @param num_args number of arguments for the function. Must be >=0 and <=PREDICATE_MAX_ARGS.
- * @param callback handler to call to evaluate the function
- * @param user value to pass to handler
- */
- void BPredicateFunction_Init (BPredicateFunction *o, BPredicate *p, char *name, int *args, int num_args, BPredicate_callback callback, void *user);
- /**
- * Removes a custom function for {@link BPredicate}.
- * Must not be called from function handlers.
- *
- * @param o the object
- */
- void BPredicateFunction_Free (BPredicateFunction *o);
- #endif
|