1 /* 2 Copyright 2021 Northern.tech AS 3 4 This file is part of CFEngine 3 - written and maintained by Northern.tech AS. 5 6 This program is free software; you can redistribute it and/or modify it 7 under the terms of the GNU General Public License as published by the 8 Free Software Foundation; version 3. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA 18 19 To the extent this program is licensed as part of the Enterprise 20 versions of CFEngine, the applicable Commercial Open Source License 21 (COSL) may apply to this file if you as a licensee so wish it. See 22 included file COSL.txt. 23 */ 24 25 #ifndef CFENGINE_LOGIC_EXPRESSIONS_H 26 #define CFENGINE_LOGIC_EXPRESSIONS_H 27 28 #include <stdbool.h> 29 #include <string_expressions.h> 30 31 /* 32 Logic expressions grammar: 33 34 <expr> ::= <or-expr> 35 36 <or-expr> ::= <and-expr> 37 <and-expr> | <and-expr> 38 <and-expr> || <and-expr> 39 40 <and-expr> ::= <not-expr> 41 <not-expr> . <not-expr> 42 <not-expr> & <not-expr> 43 44 <not-expr> ::= ! <primary> 45 <primary> 46 47 <primary> ::= ( <expr> ) 48 <name> 49 50 Basis of logic evaluation is <name> values which are provided by 51 StringExpression and suitable string->bool evaluator. 52 */ 53 54 typedef enum 55 { 56 LOGICAL_OP_OR, 57 LOGICAL_OP_AND, 58 LOGICAL_OP_NOT, 59 LOGICAL_OP_EVAL 60 } LogicalOp; 61 62 typedef struct Expression_ Expression; 63 64 struct Expression_ 65 { 66 LogicalOp op; 67 union 68 { 69 struct 70 { 71 Expression *lhs; 72 Expression *rhs; 73 } andor; 74 75 struct 76 { 77 Expression *arg; 78 } not; 79 80 struct 81 { 82 StringExpression *name; 83 } eval; 84 } val; 85 }; 86 87 /* Parsing and evaluation */ 88 89 /* 90 * Result of parsing. 91 * 92 * if succeeded, then result is the result of parsing and position is last 93 * character consumed. 94 * 95 * if not succeeded, then result is NULL and position is last character consumed 96 * before the error. 97 */ 98 typedef struct 99 { 100 Expression *result; 101 int position; 102 } ParseResult; 103 104 ParseResult ParseExpression(const char *expr, int start, int end); 105 106 typedef enum ExpressionValue 107 { 108 EXPRESSION_VALUE_ERROR = -1, 109 EXPRESSION_VALUE_FALSE = false, 110 EXPRESSION_VALUE_TRUE = true, 111 } ExpressionValue; 112 113 /* 114 * Evaluator should return FALSE, TRUE or ERROR if unable to parse result. In 115 * later case evaluation will be aborted and ERROR will be returned from 116 * EvalExpression. 117 */ 118 typedef ExpressionValue(*NameEvaluator) (const char *name, void *param); 119 120 /* 121 * Result is heap-allocated. In case evalfn() returns ERROR whole 122 * EvalExpression returns ERROR as well. 123 */ 124 ExpressionValue EvalExpression(const Expression *expr, 125 NameEvaluator nameevalfn, VarRefEvaluator varrefevalfn, void *param); 126 127 /* 128 * Frees Expression produced by ParseExpression. NULL-safe. 129 */ 130 void FreeExpression(Expression *expr); 131 132 #endif 133