Imported Upstream version 2.4.3
[platform/upstream/audit.git] / auparse / expression.h
1 /*
2 * expression.h - Expression parsing and handling
3 * Copyright (C) 2008,2014 Red Hat Inc., Durham, North Carolina.
4 * All Rights Reserved.
5 *
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.
10 *
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.
15 *
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
19 *
20 * Authors:
21 *   Miloslav Trmač <mitr@redhat.com>
22 *   Steve Grubb <sgrubb@redhat.com>  extended timestamp
23 */
24
25 #ifndef EXPRESSION_H__
26 #define EXPRESSION_H__
27
28 #include <regex.h>
29 #include <sys/types.h>
30
31 #include "internal.h"
32
33 enum {
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,
39         EO_VALUE_GE,
40         /* Uses v.p.field.  Cannot be specified by an expression. */
41         EO_FIELD_EXISTS,
42         EO_REGEXP_MATCHES,      /* Uses v.regexp */
43         NUM_EO_VALUES,
44 };
45
46 enum field_id {
47         EF_TIMESTAMP, EF_RECORD_TYPE, EF_TIMESTAMP_EX
48 };
49
50 struct expr {
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;
55         union {
56                 struct expr *sub[2];
57                 struct {
58                         union {
59                                 char *name;
60                                 enum field_id id; /* If virtual_field != 0 */
61                         } field;
62                         union {
63                                 char *string;
64                                 /* A member from the following is selected
65                                    implicitly by field.id. */
66                                 struct {
67                                         time_t sec;
68                                         unsigned int milli;
69                                 } timestamp; /* EF_TIMESTAMP */
70                                 struct {
71                                         time_t sec;
72                                         unsigned milli;
73                                         unsigned serial;
74                                 } timestamp_ex; /* EF_TIMESTAMP_EX */
75                                 int int_value; /* EF_RECORD_TYPE */
76                         } value;
77                 } p;
78                 regex_t *regexp;
79         } v;
80 };
81
82 /* Free EXPR and all its subexpressions. */
83 void expr_free(struct expr *expr) hidden;
84
85 /* Parse STRING.
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;
90
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;
96
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;
102
103 /* Create an extended timestamp comparison-expression for with OP, SEC, 
104    MILLI, and SERIAL.
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;
109
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;
114
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;
119
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)
124         hidden;
125
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)
131         hidden;
132
133 #endif