]> Sergey Matveev's repositories - bfs.git/blob - src/expr.h
Skip mtab
[bfs.git] / src / expr.h
1 // Copyright © Tavian Barnes <tavianator@tavianator.com>
2 // SPDX-License-Identifier: 0BSD
3
4 /**
5  * The expression tree representation.
6  */
7
8 #ifndef BFS_EXPR_H
9 #define BFS_EXPR_H
10
11 #include "color.h"
12 #include "config.h"
13 #include "eval.h"
14 #include "stat.h"
15 #include <stddef.h>
16 #include <sys/types.h>
17 #include <time.h>
18
19 /**
20  * Integer comparison modes.
21  */
22 enum bfs_int_cmp {
23         /** Exactly N. */
24         BFS_INT_EQUAL,
25         /** Less than N (-N). */
26         BFS_INT_LESS,
27         /** Greater than N (+N). */
28         BFS_INT_GREATER,
29 };
30
31 /**
32  * Permission comparison modes.
33  */
34 enum bfs_mode_cmp {
35         /** Mode is an exact match (MODE). */
36         BFS_MODE_EQUAL,
37         /** Mode has all these bits (-MODE). */
38         BFS_MODE_ALL,
39         /** Mode has any of these bits (/MODE). */
40         BFS_MODE_ANY,
41 };
42
43 /**
44  * Possible time units.
45  */
46 enum bfs_time_unit {
47         /** Seconds. */
48         BFS_SECONDS,
49         /** Minutes. */
50         BFS_MINUTES,
51         /** Days. */
52         BFS_DAYS,
53 };
54
55 /**
56  * Possible file size units.
57  */
58 enum bfs_size_unit {
59         /** 512-byte blocks. */
60         BFS_BLOCKS,
61         /** Single bytes. */
62         BFS_BYTES,
63         /** Two-byte words. */
64         BFS_WORDS,
65         /** Kibibytes. */
66         BFS_KB,
67         /** Mebibytes. */
68         BFS_MB,
69         /** Gibibytes. */
70         BFS_GB,
71         /** Tebibytes. */
72         BFS_TB,
73         /** Pebibytes. */
74         BFS_PB,
75 };
76
77 /**
78  * A command line expression.
79  */
80 struct bfs_expr {
81         /** The function that evaluates this expression. */
82         bfs_eval_fn *eval_fn;
83
84         /** The number of command line arguments for this expression. */
85         size_t argc;
86         /** The command line arguments comprising this expression. */
87         char **argv;
88
89         /** The number of files this expression keeps open between evaluations. */
90         int persistent_fds;
91         /** The number of files this expression opens during evaluation. */
92         int ephemeral_fds;
93
94         /** Whether this expression has no side effects. */
95         bool pure;
96         /** Whether this expression always evaluates to true. */
97         bool always_true;
98         /** Whether this expression always evaluates to false. */
99         bool always_false;
100
101         /** Estimated cost. */
102         float cost;
103         /** Estimated probability of success. */
104         float probability;
105         /** Number of times this predicate was evaluated. */
106         size_t evaluations;
107         /** Number of times this predicate succeeded. */
108         size_t successes;
109         /** Total time spent running this predicate. */
110         struct timespec elapsed;
111
112         /** Auxilliary data for the evaluation function. */
113         union {
114                 /** Child expressions. */
115                 struct {
116                         /** The left hand side of the expression. */
117                         struct bfs_expr *lhs;
118                         /** The right hand side of the expression. */
119                         struct bfs_expr *rhs;
120                 };
121
122                 /** Integer comparisons. */
123                 struct {
124                         /** Integer for this comparison. */
125                         long long num;
126                         /** The comparison mode. */
127                         enum bfs_int_cmp int_cmp;
128
129                         /** Optional extra data. */
130                         union {
131                                 /** -size data. */
132                                 enum bfs_size_unit size_unit;
133
134                                 /** Timestamp comparison data. */
135                                 struct {
136                                         /** The stat field to look at. */
137                                         enum bfs_stat_field stat_field;
138                                         /** The reference time. */
139                                         struct timespec reftime;
140                                         /** The time unit. */
141                                         enum bfs_time_unit time_unit;
142                                 };
143                         };
144                 };
145
146                 /** String comparisons. */
147                 struct {
148                         /** String pattern. */
149                         const char *pattern;
150                         /** fnmatch() flags. */
151                         int fnm_flags;
152                         /** Whether strcmp() can be used instead of fnmatch(). */
153                         bool literal;
154                 };
155
156                 /** Printing actions. */
157                 struct {
158                         /** The output stream. */
159                         CFILE *cfile;
160                         /** Optional file path. */
161                         const char *path;
162                         /** Optional -printf format. */
163                         struct bfs_printf *printf;
164                 };
165
166                 /** -exec data. */
167                 struct bfs_exec *exec;
168
169                 /** -flags data. */
170                 struct {
171                         /** The comparison mode. */
172                         enum bfs_mode_cmp flags_cmp;
173                         /** Flags that should be set. */
174                         unsigned long long set_flags;
175                         /** Flags that should be cleared. */
176                         unsigned long long clear_flags;
177                 };
178
179                 /** -perm data. */
180                 struct {
181                         /** The comparison mode. */
182                         enum bfs_mode_cmp mode_cmp;
183                         /** Mode to use for files. */
184                         mode_t file_mode;
185                         /** Mode to use for directories (different due to X). */
186                         mode_t dir_mode;
187                 };
188
189                 /** -regex data. */
190                 struct bfs_regex *regex;
191
192                 /** -samefile data. */
193                 struct {
194                         /** Device number of the target file. */
195                         dev_t dev;
196                         /** Inode number of the target file. */
197                         ino_t ino;
198                 };
199         };
200 };
201
202 /**
203  * Create a new expression.
204  */
205 struct bfs_expr *bfs_expr_new(bfs_eval_fn *eval, size_t argc, char **argv);
206
207 /**
208  * @return Whether the expression has child expressions.
209  */
210 bool bfs_expr_is_parent(const struct bfs_expr *expr);
211
212 /**
213  * @return Whether expr is known to always quit.
214  */
215 bool bfs_expr_never_returns(const struct bfs_expr *expr);
216
217 /**
218  * @return The result of the integer comparison for this expression.
219  */
220 bool bfs_expr_cmp(const struct bfs_expr *expr, long long n);
221
222 /**
223  * Free an expression tree.
224  */
225 void bfs_expr_free(struct bfs_expr *expr);
226
227 #endif // BFS_EXPR_H