Debian Codegraph @

libsmartcols/src/filter.c

raw
1 /*
2 * filter.c - functions for lines filtering
3 *
4 * Copyright (C) 2023 Karel Zak <[email protected]>
5 *
6 * This file may be redistributed under the terms of the
7 * GNU Lesser General Public License.
8 */
9
10 /**
11 * SECTION: filter
12 * @title: Filters and counters
13 * @short_description: defines lines filter and counter
14 *
15 * An API to define and use filter and counters.
16 */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22
23 #include "smartcolsP.h"
24
25 #include "filter-parser.h"
26 #include "filter-scanner.h"
27
28 /**
29 * scols_new_filter:
30 * @str: filter expression or NULL
31 *
32 * Allocated and optionally parses a new filter.
33 *
34 * Returns: new filter instance or NULL in case of error.
35 *
36 * Since: 2.40
37 */
38 struct libscols_filter *scols_new_filter(const char *str)
39 {
40 struct libscols_filter *fltr = calloc(1, sizeof(*fltr));
41
42 if (!fltr)
43 return NULL;
44
45 DBG(FLTR, ul_debugobj(fltr, "alloc"));
46 fltr->refcount = 1;
47 INIT_LIST_HEAD(&fltr->params);
48 INIT_LIST_HEAD(&fltr->counters);
49
50 if (str && scols_filter_parse_string(fltr, str) != 0) {
51 scols_unref_filter(fltr);
52 return NULL;
53 }
54
55 return fltr;
56 }
57
58 /**
59 * scols_ref_filter:
60 * @fltr: filter instance
61 *
62 * Increment filter reference counter.
63 *
64 * Since: 2.40
65 */
66 void scols_ref_filter(struct libscols_filter *fltr)
67 {
68 if (fltr)
69 fltr->refcount++;
70 }
71
72 static void reset_filter(struct libscols_filter *fltr)
73 {
74 if (!fltr)
75 return;
76 filter_unref_node(fltr->root);
77 fltr->root = NULL;
78
79 if (fltr->src)
80 fclose(fltr->src);
81 fltr->src = NULL;
82
83 free(fltr->errmsg);
84 fltr->errmsg = NULL;
85 }
86
87 static void remove_counters(struct libscols_filter *fltr)
88 {
89 if (!fltr)
90 return;
91
92 DBG(FLTR, ul_debugobj(fltr, "remove all counters"));
93 while (!list_empty(&fltr->counters)) {
94 struct libscols_counter *ct = list_entry(fltr->counters.next,
95 struct libscols_counter, counters);
96
97 filter_unref_node((struct filter_node *) ct->param);
98 list_del_init(&ct->counters);
99 free(ct->name);
100 free(ct);
101 }
102 }
103
104 /**
105 * scols_unref_filter:
106 * @fltr: filter instance
107 *
108 * Decrements reference counter, unallocates the filter for the last
109 * reference.
110 *
111 * Since: 2.40
112 */
113 void scols_unref_filter(struct libscols_filter *fltr)
114 {
115 if (fltr && --fltr->refcount <= 0) {
116 DBG(FLTR, ul_debugobj(fltr, "dealloc"));
117 reset_filter(fltr);
118 remove_counters(fltr);
119 free(fltr);
120 }
121 }
122
123 /* This is generic allocator for a new node, always use the node type specific
124 * functions (e.g. filter_new_param() */
125 struct filter_node *__filter_new_node(enum filter_ntype type, size_t sz)
126 {
127 struct filter_node *n = calloc(1, sz);
128
129 if (!n)
130 return NULL;
131
132 n->type = type;
133 n->refcount = 1;
134 return n;
135 }
136
137 void filter_unref_node(struct filter_node *n)
138 {
139 if (!n || --n->refcount > 0)
140 return;
141
142 switch (n->type) {
143 case F_NODE_EXPR:
144 filter_free_expr((struct filter_expr *) n);
145 break;
146 case F_NODE_PARAM:
147 filter_free_param((struct filter_param *) n);
148 break;
149 }
150 }
151
152 void filter_ref_node(struct filter_node *n)
153 {
154 if (n)
155 n->refcount++;
156 }
157
158 void filter_dump_node(struct ul_jsonwrt *json, struct filter_node *n)
159 {
160 if (!n)
161 return;
162
163 switch (n->type) {
164 case F_NODE_EXPR:
165 filter_dump_expr(json, (struct filter_expr *) n);
166 break;
167 case F_NODE_PARAM:
168 filter_dump_param(json, (struct filter_param *) n);
169 break;
170 }
171 }
172
173 /**
174 * scols_filter_parse_string:
175 * @fltr: filter instance
176 * @str: string with filter expression
177 *
178 * Parses filter, see scols_filter_get_errmsg() for errors.
179 *
180 * Returns: 0, a negative number in case of an error.
181 *
182 * Since: 2.40
183 */
184 int scols_filter_parse_string(struct libscols_filter *fltr, const char *str)
185 {
186 yyscan_t sc;
187 int rc;
188
189 reset_filter(fltr);
190
191 if (!str || !*str)
192 return 0; /* empty filter is not error */
193
194 fltr->src = fmemopen((void *) str, strlen(str), "r");
195 if (!fltr->src)
196 return -errno;
197
198 yylex_init_extra(fltr, &sc);
199 yyset_in(fltr->src, sc);
200
201 rc = yyparse(sc, fltr);
202 yylex_destroy(sc);
203
204 fclose(fltr->src);
205 fltr->src = NULL;
206
207 ON_DBG(FLTR, scols_dump_filter(fltr, stderr));
208
209 return rc;
210 }
211
212 /**
213 * scols_dump_filter:
214 * @fltr: filter instance
215 * @out: output stream
216 *
217 * Dumps internal filter nodes in JSON format. This function is mostly designed
218 * for debugging purpose. The fields in the output are subject to change.
219 *
220 * Returns: 0, a negative number in case of an error.
221 *
222 * Since: 2.40
223 */
224 int scols_dump_filter(struct libscols_filter *fltr, FILE *out)
225 {
226 struct ul_jsonwrt json;
227
228 if (!fltr || !out)
229 return -EINVAL;
230
231 ul_jsonwrt_init(&json, out, 0, UL_JSON_PRETTY);
232 ul_jsonwrt_root_open(&json);
233
234 filter_dump_node(&json, fltr->root);
235 ul_jsonwrt_root_close(&json);
236 return 0;
237 }
238
239 /**
240 * scols_filter_get_errmsg:
241 * @fltr: filter instance
242 *
243 * Returns: string with parse-error message of NULL (if no error)
244 *
245 * Since: 2.40
246 */
247 const char *scols_filter_get_errmsg(struct libscols_filter *fltr)
248 {
249 return fltr ? fltr->errmsg : NULL;
250 }
251
252 int filter_eval_node(struct libscols_filter *fltr, struct libscols_line *ln,
253 struct filter_node *n, int *status)
254 {
255 switch (n->type) {
256 case F_NODE_PARAM:
257 return filter_eval_param(fltr, ln, (struct filter_param *) n, status);
258 case F_NODE_EXPR:
259 return filter_eval_expr(fltr, ln, (struct filter_expr *) n, status);
260 default:
261 break;
262 }
263 return -EINVAL;
264 }
265
266 /**
267 * scols_line_apply_filter:
268 * @ln: apply filter to the line
269 * @fltr: filter instance
270 * @status: return 1 or 0 as result of the expression
271 *
272 * Applies filter (and also counters associated with the filter).
273 *
274 * Returns: 0, a negative number in case of an error.
275 *
276 * Since: 2.40
277 */
278 int scols_line_apply_filter(struct libscols_line *ln,
279 struct libscols_filter *fltr, int *status)
280 {
281 int rc, res = 0;
282 struct libscols_iter itr;
283 struct filter_param *prm = NULL;
284
285 if (!ln || !fltr)
286 return -EINVAL;
287
288 /* reset column data and types stored in the filter */
289 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
290 while (filter_next_param(fltr, &itr, &prm) == 0) {
291 filter_param_reset_holder(prm);
292 }
293
294 if (fltr->root)
295 rc = filter_eval_node(fltr, ln, fltr->root, &res);
296 else
297 rc = 0, res = 1; /* empty filter matches all lines */
298
299 if (rc == 0) {
300 struct libscols_counter *ct = NULL;
301
302 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
303 while (scols_filter_next_counter(fltr, &itr, &ct) == 0) {
304 if ((ct->neg && res == 0) || res == 1)
305 filter_count_param(fltr, ln, ct);
306 }
307 }
308
309 if (status)
310 *status = res;
311 DBG(FLTR, ul_debugobj(fltr, "filter done [rc=%d, status=%d]", rc, res));
312 return rc;
313 }
314
315 /**
316 * scols_filter_set_filler_cb:
317 * @fltr: filter instance
318 * @cb: application defined callback
319 * @userdata: pointer to private callback data
320 *
321 * The application can apply filter for empty lines to avoid filling the table
322 * with unnecessary data (for example if the line will be later removed from
323 * the table due to filter).
324 *
325 * This callback is used by filter to ask application to fill to the line data
326 * which are necessary to evaluate the filter expression. The callback
327 * arguments are filter, column number and userdata.
328 *
329 * <informalexample>
330 * <programlisting>
331 * ln = scols_table_new_line(tab, NULL);
332 *
333 * scols_filter_set_filler_cb(filter, my_filler, NULL);
334 *
335 * scols_line_apply_filter(line, filter, &status);
336 * if (status == 0)
337 * scols_table_remove_line(tab, line);
338 * else for (i = 0; i < ncolumns; i++) {
339 * if (scols_line_is_filled(line, i))
340 * continue;
341 * my_filler(NULL, ln, i, NULL);
342 * }
343 * </programlisting>
344 * </informalexample>
345 *
346 * Returns: 0, a negative number in case of an error.
347 *
348 * Since: 2.40
349 */
350 int scols_filter_set_filler_cb(struct libscols_filter *fltr,
351 int (*cb)(struct libscols_filter *,
352 struct libscols_line *, size_t, void *),
353 void *userdata)
354 {
355 if (!fltr)
356 return -EINVAL;
357 fltr->filler_cb = cb;
358 fltr->filler_data = userdata;
359
360 return 0;
361 }
362
363 /**
364 * scols_filter_new_counter:
365 * @fltr: filter instance
366 *
367 * Allocates a new counter instance into the filter.
368 *
369 * Returns: new counter or NULL in case of an error.
370 *
371 * Since: 2.40
372 */
373 struct libscols_counter *scols_filter_new_counter(struct libscols_filter *fltr)
374 {
375 struct libscols_counter *ct;
376
377 if (!fltr)
378 return NULL;
379
380 ct = calloc(1, sizeof(*ct));
381 if (!ct)
382 return NULL;
383
384 DBG(FLTR, ul_debugobj(fltr, "alloc counter"));
385
386 ct->filter = fltr; /* don't use ref.counting here */
387 INIT_LIST_HEAD(&ct->counters);
388 list_add_tail(&ct->counters, &fltr->counters);
389
390
391 return ct;
392 }
393
394 /**
395 * scols_counter_set_name:
396 * @ct: counter instance
397 * @name: something for humans
398 *
399 * The name is not use by library, it's just description usable for application
400 * when prints results from counters.
401 *
402 * Returns: 0, a negative number in case of an error.
403 *
404 * Since: 2.40
405 */
406 int scols_counter_set_name(struct libscols_counter *ct, const char *name)
407 {
408 if (!ct)
409 return -EINVAL;
410 return strdup_to_struct_member(ct, name, name);
411 }
412
413 /**
414 * scols_counter_set_param:
415 * @ct: counter instance
416 * @name: holder (column) name
417 *
418 * Assigns a counter to the column. The name is used in the same way as names
419 * in the filter expression. This is usable for counter that calculate with data
420 * from table cells (e.g. max, sum, etc.)
421 *
422 * Returns: 0, a negative number in case of an error.
423 *
424 * Since: 2.40
425 */
426 int scols_counter_set_param(struct libscols_counter *ct, const char *name)
427 {
428 if (!ct)
429 return -EINVAL;
430
431 if (ct->param) {
432 filter_unref_node((struct filter_node *) ct->param);
433 ct->param = NULL;
434 }
435 if (name) {
436 ct->param = (struct filter_param *)
437 filter_new_param(ct->filter, SCOLS_DATA_U64,
438 F_HOLDER_COLUMN, (void *) name);
439 if (!ct->param)
440 return -ENOMEM;
441 }
442 return 0;
443 }
444
445 /**
446 * scols_counter_set_func:
447 * @ct: counter instance
448 * @func: SCOLS_COUNTER_{COUNT,MAX,MIN,SUM}
449 *
450 * Defines function to calculate data.
451 *
452 * Returns: 0, a negative number in case of an error.
453 *
454 * Since: 2.40
455 */
456 int scols_counter_set_func(struct libscols_counter *ct, int func)
457 {
458 if (!ct || func < 0 || func >= __SCOLS_NCOUNTES)
459 return -EINVAL;
460
461 ct->func = func;
462 return 0;
463 }
464
465 /**
466 * scols_counter_get_result:
467 * @ct: counter instance
468 *
469 * Returns: result from the counter
470 *
471 * Since: 2.40
472 */
473 unsigned long long scols_counter_get_result(struct libscols_counter *ct)
474 {
475 return ct ? ct->result : 0;
476 }
477
478 /**
479 * scols_counter_get_name:
480 * @ct: counter instance
481 *
482 * Returns: name of the counter.
483 *
484 * Since: 2.40
485 */
486 const char *scols_counter_get_name(struct libscols_counter *ct)
487 {
488 return ct ? ct->name : NULL;
489 }
490
491 /**
492 * scols_filter_next_counter:
493 * @fltr: filter instance
494 * @itr: a pointer to a struct libscols_iter instance
495 * @ct: returns the next counter
496 *
497 * Finds the next counter and returns a pointer to it via @ct.
498 *
499 * Returns: 0, a negative value in case of an error, and 1 at the end.
500 *
501 * Since: 2.40
502 */
503 int scols_filter_next_counter(struct libscols_filter *fltr,
504 struct libscols_iter *itr, struct libscols_counter **ct)
505 {
506 int rc = 1;
507
508 if (!fltr || !itr || !ct)
509 return -EINVAL;
510 *ct = NULL;
511
512 if (!itr->head)
513 SCOLS_ITER_INIT(itr, &fltr->counters);
514 if (itr->p != itr->head) {
515 SCOLS_ITER_ITERATE(itr, *ct, struct libscols_counter, counters);
516 rc = 0;
517 }
518
519 return rc;
520 }