2 * expression.h - Expression parsing and handling
3 * Copyright (C) 2008,2014 Red Hat Inc., Durham, North Carolina.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Miloslav Trmač <mitr@redhat.com>
22 * Steve Grubb <sgrubb@redhat.com> extended timestamp
25 #ifndef EXPRESSION_H__
26 #define EXPRESSION_H__
29 #include <sys/types.h>
34 EO_NOT, /* Uses v.sub[0] */
35 EO_AND, EO_OR, /* Uses v.sub[0] and v.sub[1] */
36 /* All of the following use v.p */
37 EO_RAW_EQ, EO_RAW_NE, EO_INTERPRETED_EQ, EO_INTERPRETED_NE,
38 EO_VALUE_EQ, EO_VALUE_NE, EO_VALUE_LT, EO_VALUE_LE, EO_VALUE_GT,
40 /* Uses v.p.field. Cannot be specified by an expression. */
42 EO_REGEXP_MATCHES, /* Uses v.regexp */
47 EF_TIMESTAMP, EF_RECORD_TYPE, EF_TIMESTAMP_EX
51 unsigned op : 8; /* EO_* */
52 unsigned virtual_field : 1;
53 /* Can be non-zero only if virtual_field != 0 */
54 unsigned precomputed_value : 1;
60 enum field_id id; /* If virtual_field != 0 */
64 /* A member from the following is selected
65 implicitly by field.id. */
69 } timestamp; /* EF_TIMESTAMP */
74 } timestamp_ex; /* EF_TIMESTAMP_EX */
75 int int_value; /* EF_RECORD_TYPE */
82 /* Free EXPR and all its subexpressions. */
83 void expr_free(struct expr *expr) hidden;
86 On success, return the parsed expression tree.
87 On error, set *ERROR to an error string (for free()) or NULL, and return
88 NULL. (*ERROR == NULL is allowed to handle out-of-memory errors) */
89 struct expr *expr_parse(const char *string, char **error) hidden;
91 /* Create a comparison-expression for FIELD, OP and VALUE.
92 On success, return the created expression.
93 On error, set errno and return NULL. */
94 struct expr *expr_create_comparison(const char *field, unsigned op,
95 const char *value) hidden;
97 /* Create a timestamp comparison-expression for with OP, SEC, MILLI.
98 On success, return the created expression.
99 On error, set errno and return NULL. */
100 struct expr *expr_create_timestamp_comparison(unsigned op, time_t sec,
101 unsigned milli) hidden;
103 /* Create an extended timestamp comparison-expression for with OP, SEC,
105 On success, return the created expression.
106 On error, set errno and return NULL. */
107 struct expr *expr_create_timestamp_comparison_ex(unsigned op, time_t sec,
108 unsigned milli, unsigned serial) hidden;
110 /* Create an EO_FIELD_EXISTS-expression for FIELD.
111 On success, return the created expression.
112 On error, set errno and return NULL. */
113 struct expr *expr_create_field_exists(const char *field) hidden;
115 /* Create a \regexp expression for regexp comparison.
116 On success, return the created expression.
117 On error, set errno and return NULL. */
118 struct expr *expr_create_regexp_expression(const char *regexp) hidden;
120 /* Create a binary expresion for OP and subexpressions E1 and E2.
121 On success, return the created expresion.
122 On error, set errno and return NULL. */
123 struct expr *expr_create_binary(unsigned op, struct expr *e1, struct expr *e2)
126 /* Evaluate EXPR on RECORD in AU->le.
127 Return 1 if EXPR is true, 0 if it false or if it fails.
128 (No error reporting facility is provided; an invalid term is considered to
129 be false; e.g. !invalid is true.) */
130 int expr_eval(auparse_state_t *au, rnode *record, const struct expr *expr)