Debian Codegraph @

libsmartcols/src/smartcolsP.h

raw
1 /*
2 * smartcolsP.h - private library header file
3 *
4 * Copyright (C) 2014 Ondrej Oprala <[email protected]>
5 * Copyright (C) 2014 Karel Zak <[email protected]>
6 *
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
9 */
10
11 #ifndef _LIBSMARTCOLS_PRIVATE_H
12 #define _LIBSMARTCOLS_PRIVATE_H
13
14 #include "c.h"
15 #include "list.h"
16 #include "strutils.h"
17 #include "color-names.h"
18 #include "jsonwrt.h"
19 #include "debug.h"
20 #include "buffer.h"
21
22 #include <stdbool.h>
23
24 #include "libsmartcols.h"
25
26 /*
27 * Debug
28 */
29 #define SCOLS_DEBUG_HELP (1 << 0)
30 #define SCOLS_DEBUG_INIT (1 << 1)
31 #define SCOLS_DEBUG_CELL (1 << 2)
32 #define SCOLS_DEBUG_LINE (1 << 3)
33 #define SCOLS_DEBUG_TAB (1 << 4)
34 #define SCOLS_DEBUG_COL (1 << 5)
35 #define SCOLS_DEBUG_BUFF (1 << 6)
36 #define SCOLS_DEBUG_GROUP (1 << 7)
37 #define SCOLS_DEBUG_FLTR (1 << 8)
38 #define SCOLS_DEBUG_FPARAM (1 << 9)
39 #define SCOLS_DEBUG_ALL 0xFFFF
40
41 UL_DEBUG_DECLARE_MASK(libsmartcols);
42 #define DBG(m, x) __UL_DBG(libsmartcols, SCOLS_DEBUG_, m, x)
43 #define ON_DBG(m, x) __UL_DBG_CALL(libsmartcols, SCOLS_DEBUG_, m, x)
44 #define DBG_FLUSH __UL_DBG_FLUSH(libsmartcols, SCOLS_DEBUG_)
45
46 #define UL_DEBUG_CURRENT_MASK UL_DEBUG_MASK(libsmartcols)
47 #include "debugobj.h"
48
49 #define SCOLS_BUFPTR_TREEEND 0
50
51 /*
52 * Generic iterator
53 */
54 struct libscols_iter {
55 struct list_head *p; /* current position */
56 struct list_head *head; /* start position */
57 int direction; /* SCOLS_ITER_{FOR,BACK}WARD */
58 };
59
60 /*
61 * Tree symbols
62 */
63 struct libscols_symbols {
64 int refcount;
65
66 char *tree_branch;
67 char *tree_vert;
68 char *tree_right;
69
70 char *group_vert;
71 char *group_horz;
72 char *group_first_member;
73 char *group_last_member;
74 char *group_middle_member;
75 char *group_last_child;
76 char *group_middle_child;
77
78 char *title_padding;
79 char *cell_padding;
80 };
81
82 /*
83 * Table cells
84 */
85 struct libscols_cell {
86 char *data;
87 size_t datasiz;
88 char *color;
89 char *uri;
90 void *userdata;
91 int flags;
92 size_t width;
93
94 unsigned int is_filled : 1,
95 no_uri : 1;
96 };
97
98 extern int scols_line_move_cells(struct libscols_line *ln, size_t newn, size_t oldn);
99
100 struct libscols_wstat {
101 size_t width_min;
102 size_t width_max;
103 double width_avg;
104 double width_sqr_sum;
105 double width_deviation;
106
107 size_t width_treeart;
108 };
109
110 /*
111 * Table column
112 */
113 struct libscols_column {
114 int refcount; /* reference counter */
115 size_t seqnum; /* column index */
116
117 size_t width; /* expected column width */
118 size_t width_treeart;
119 double width_hint; /* hint (N < 1 is in percent of termwidth) */
120
121 struct libscols_wstat wstat; /* private __scols_calculate() data */
122
123 int json_type; /* SCOLS_JSON_* */
124 int data_type; /* SCOLS_DATA_* */
125
126 int flags;
127 char *color; /* default column color */
128 char *annotation; /* column annotation */
129 char *uri; /* default column URI prefix */
130 struct ul_buffer uri_buf; /* temporary buffer to compose URIs */
131 char *safechars; /* do not encode this bytes */
132
133 int (*cmpfunc)(struct libscols_cell *,
134 struct libscols_cell *,
135 void *); /* cells comparison function */
136 void *cmpfunc_data;
137
138 /* multi-line cell data wrapping */
139 char *(*wrap_nextchunk)(const struct libscols_column *, char *, void *);
140 void *wrapfunc_data;
141
142 size_t wrap_datasz;
143 size_t wrap_datamax;
144 char *wrap_data;
145 char *wrap_cur;
146 char *wrap_next;
147 struct libscols_cell *wrap_cell;
148
149 void *(*datafunc)(const struct libscols_column *,
150 struct libscols_cell *,
151 void *);
152 void *datafunc_data;
153
154 struct libscols_cell header; /* column name with color etc. */
155 char *shellvar; /* raw column name in shell compatible format */
156
157 struct list_head cl_columns; /* member of table->tb_columns */
158
159 struct libscols_table *table;
160
161 unsigned int is_groups : 1; /* print group chart */
162
163 };
164
165 #define colsep(tb) ((tb)->colsep ? (tb)->colsep : " ")
166 #define linesep(tb) ((tb)->linesep ? (tb)->linesep : "\n")
167
168 enum {
169 SCOLS_GSTATE_NONE = 0, /* not activate yet */
170 SCOLS_GSTATE_FIRST_MEMBER,
171 SCOLS_GSTATE_MIDDLE_MEMBER,
172 SCOLS_GSTATE_LAST_MEMBER,
173 SCOLS_GSTATE_MIDDLE_CHILD,
174 SCOLS_GSTATE_LAST_CHILD,
175 SCOLS_GSTATE_CONT_MEMBERS,
176 SCOLS_GSTATE_CONT_CHILDREN
177 };
178
179 /*
180 * Every group needs at least 3 columns
181 */
182 #define SCOLS_GRPSET_CHUNKSIZ 3
183
184 struct libscols_group {
185 int refcount;
186
187 size_t nmembers;
188
189 struct list_head gr_members; /* head of line->ln_group */
190 struct list_head gr_children; /* head of line->ln_children */
191 struct list_head gr_groups; /* member of table->tb_groups */
192
193 int state; /* SCOLS_GSTATE_* */
194 };
195
196 /*
197 * Table line
198 */
199 struct libscols_line {
200 int refcount;
201 size_t seqnum;
202
203 void *userdata;
204 char *color; /* default line color */
205
206 struct libscols_cell *cells; /* array with data */
207 size_t ncells; /* number of cells */
208
209 struct list_head ln_lines; /* member of table->tb_lines */
210 struct list_head ln_branch; /* head of line->ln_children */
211 struct list_head ln_children; /* member of line->ln_children or group->gr_children */
212 struct list_head ln_groups; /* member of group->gr_groups */
213
214 struct libscols_line *parent;
215 struct libscols_group *parent_group; /* for group childs */
216 struct libscols_group *group; /* for group members */
217 };
218
219 enum {
220 SCOLS_FMT_HUMAN = 0, /* default, human readable */
221 SCOLS_FMT_RAW, /* space separated */
222 SCOLS_FMT_EXPORT, /* COLNAME="data" ... */
223 SCOLS_FMT_JSON /* http://en.wikipedia.org/wiki/JSON */
224 };
225
226 /*
227 * The table
228 */
229 struct libscols_table {
230 int refcount;
231 char *name; /* optional table name (for JSON) */
232 size_t ncols; /* number of columns */
233 size_t ntreecols; /* number of columns with SCOLS_FL_TREE */
234 size_t nlines; /* number of lines */
235 size_t termwidth; /* terminal width (number of columns) */
236 size_t termheight; /* terminal height (number of lines) */
237 size_t termreduce; /* extra blank space */
238 size_t outwidth; /* effective output width (calculated internally) */
239 int termforce; /* SCOLS_TERMFORCE_* */
240 FILE *out; /* output stream */
241
242 char *colsep; /* column separator */
243 char *linesep; /* line separator */
244
245 struct list_head tb_columns; /* list of columns, items: column->cl_columns */
246 struct list_head tb_lines; /* list of lines; items: line->ln_lines */
247
248 struct list_head tb_groups; /* all defined groups */
249 struct libscols_group **grpset;
250 size_t grpset_size;
251
252 size_t ngrpchlds_pending; /* groups with not yet printed children */
253 struct libscols_line *walk_last_tree_root; /* last root, used by scols_walk_() */
254
255 struct libscols_column *dflt_sort_column; /* default sort column, set by scols_sort_table() */
256
257 struct libscols_symbols *symbols;
258 struct libscols_cell title; /* optional table title (for humans) */
259
260 struct ul_jsonwrt json; /* JSON formatting */
261
262 int format; /* SCOLS_FMT_* */
263
264 size_t termlines_used; /* printed line counter */
265 size_t header_next; /* where repeat header */
266
267 const char *cur_color; /* current active color when printing */
268
269 struct libscols_cell *cur_cell; /* currently used cell */
270 struct libscols_line *cur_line; /* currently used line */
271 struct libscols_column *cur_column; /* currently used column */
272
273 enum ul_json_format json_format; /* JSON output format */
274
275 /* flags */
276 bool ascii , /* don't use unicode */
277 colors_wanted , /* enable colors */
278 is_term , /* isatty() */
279 padding_debug , /* output visible padding chars */
280 is_dummy_print, /* printing used for width calculation only */
281 is_shellvar , /* shell compatible column names */
282 maxout , /* maximize output */
283 minout , /* minimize output (mutually exclusive to maxout) */
284 header_repeat , /* print header after libscols_table->termheight */
285 header_printed, /* header already printed */
286 priv_symbols , /* default private symbols */
287 walk_last_done, /* last tree root walked */
288 no_headings , /* don't print header */
289 no_encode , /* don't care about control and non-printable chars */
290 no_linesep , /* don't print line separator */
291 no_wrap ; /* never wrap lines */
292 };
293
294 #define IS_ITER_FORWARD(_i) ((_i)->direction == SCOLS_ITER_FORWARD)
295 #define IS_ITER_BACKWARD(_i) ((_i)->direction == SCOLS_ITER_BACKWARD)
296
297 #define SCOLS_ITER_INIT(itr, list) \
298 do { \
299 (itr)->p = IS_ITER_FORWARD(itr) ? \
300 (list)->next : (list)->prev; \
301 (itr)->head = (list); \
302 } while(0)
303
304 #define SCOLS_ITER_ITERATE(itr, res, restype, member) \
305 do { \
306 res = list_entry((itr)->p, restype, member); \
307 (itr)->p = IS_ITER_FORWARD(itr) ? \
308 (itr)->p->next : (itr)->p->prev; \
309 } while(0)
310
311
312 static inline int scols_iter_is_last(const struct libscols_iter *itr)
313 {
314 if (!itr || !itr->head || !itr->p)
315 return 0;
316
317 return itr->p == itr->head;
318 }
319
320 /*
321 * line.c
322 */
323 int scols_line_next_group_child(struct libscols_line *ln,
324 struct libscols_iter *itr,
325 struct libscols_line **chld);
326
327 /*
328 * column.c
329 */
330 void scols_column_reset_wrap(struct libscols_column *cl);
331 int scols_column_next_wrap( struct libscols_column *cl,
332 struct libscols_cell *ce,
333 char **data);
334 int scols_column_greatest_wrap( struct libscols_column *cl,
335 struct libscols_cell *ce,
336 char **data);
337 int scols_column_has_pending_wrap(struct libscols_column *cl);
338 int scols_column_move_wrap(struct libscols_column *cl, size_t bytes);
339
340 /*
341 * table.c
342 */
343 int scols_table_next_group(struct libscols_table *tb,
344 struct libscols_iter *itr,
345 struct libscols_group **gr);
346 int scols_table_set_cursor(struct libscols_table *tb,
347 struct libscols_line *ln,
348 struct libscols_column *cl,
349 struct libscols_cell *ce);
350
351 #define scols_table_reset_cursor(_t) scols_table_set_cursor((_t), NULL, NULL, NULL)
352
353
354 /*
355 * grouping.c
356 */
357 void scols_ref_group(struct libscols_group *gr);
358 void scols_group_remove_children(struct libscols_group *gr);
359 void scols_group_remove_members(struct libscols_group *gr);
360 void scols_unref_group(struct libscols_group *gr);
361 void scols_groups_fix_members_order(struct libscols_table *tb);
362 int scols_groups_update_grpset(struct libscols_table *tb, struct libscols_line *ln);
363 void scols_groups_reset_state(struct libscols_table *tb);
364 struct libscols_group *scols_grpset_get_printable_children(struct libscols_table *tb);
365
366 /*
367 * walk.c
368 */
369 extern int scols_walk_tree(struct libscols_table *tb,
370 struct libscols_column *cl,
371 int (*callback)(struct libscols_table *,
372 struct libscols_line *,
373 struct libscols_column *,
374 void *),
375 void *data);
376 extern int scols_walk_is_last(struct libscols_table *tb, struct libscols_line *ln);
377
378 /*
379 * calculate.c
380 */
381 extern int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf);
382
383 /*
384 * print.c
385 */
386 int __cursor_to_buffer(struct libscols_table *tb,
387 struct ul_buffer *buf,
388 int cal);
389
390 void __scols_cleanup_printing(struct libscols_table *tb, struct ul_buffer *buf);
391 int __scols_initialize_printing(struct libscols_table *tb, struct ul_buffer *buf);
392 int __scols_print_tree(struct libscols_table *tb, struct ul_buffer *buf);
393 int __scols_print_table(struct libscols_table *tb, struct ul_buffer *buf);
394 int __scols_print_header(struct libscols_table *tb, struct ul_buffer *buf);
395 int __scols_print_title(struct libscols_table *tb);
396 int __scols_print_range(struct libscols_table *tb,
397 struct ul_buffer *buf,
398 struct libscols_iter *itr,
399 struct libscols_line *end);
400
401 static inline int is_tree_root(struct libscols_line *ln)
402 {
403 return ln && !ln->parent && !ln->parent_group;
404 }
405
406 static inline int is_last_tree_root(struct libscols_table *tb, struct libscols_line *ln)
407 {
408 if (!ln || !tb || tb->walk_last_tree_root != ln)
409 return 0;
410
411 return 1;
412 }
413
414 static inline int is_child(struct libscols_line *ln)
415 {
416 return ln && ln->parent;
417 }
418
419 static inline int is_last_child(struct libscols_line *ln)
420 {
421 if (!ln || !ln->parent)
422 return 0;
423
424 return list_entry_is_last(&ln->ln_children, &ln->parent->ln_branch);
425 }
426
427 static inline int is_first_child(struct libscols_line *ln)
428 {
429 if (!ln || !ln->parent)
430 return 0;
431
432 return list_entry_is_first(&ln->ln_children, &ln->parent->ln_branch);
433 }
434
435
436 static inline int is_last_column(struct libscols_column *cl)
437 {
438 struct libscols_column *next;
439
440 if (list_entry_is_last(&cl->cl_columns, &cl->table->tb_columns))
441 return 1;
442
443 next = list_entry(cl->cl_columns.next, struct libscols_column, cl_columns);
444 if (next && scols_column_is_hidden(next) && is_last_column(next))
445 return 1;
446 return 0;
447 }
448
449 static inline int is_last_group_member(struct libscols_line *ln)
450 {
451 if (!ln || !ln->group)
452 return 0;
453
454 return list_entry_is_last(&ln->ln_groups, &ln->group->gr_members);
455 }
456
457 static inline int is_first_group_member(struct libscols_line *ln)
458 {
459 if (!ln || !ln->group)
460 return 0;
461
462 return list_entry_is_first(&ln->ln_groups, &ln->group->gr_members);
463 }
464
465 static inline int is_group_member(struct libscols_line *ln)
466 {
467 return ln && ln->group;
468 }
469
470 static inline int is_last_group_child(struct libscols_line *ln)
471 {
472 if (!ln || !ln->parent_group)
473 return 0;
474
475 return list_entry_is_last(&ln->ln_children, &ln->parent_group->gr_children);
476 }
477
478 static inline int is_group_child(struct libscols_line *ln)
479 {
480 return ln && ln->parent_group;
481 }
482
483 static inline int has_groups(struct libscols_table *tb)
484 {
485 return tb && !list_empty(&tb->tb_groups);
486 }
487
488 static inline int has_children(struct libscols_line *ln)
489 {
490 return ln && !list_empty(&ln->ln_branch);
491 }
492
493 static inline int has_group_children(struct libscols_line *ln)
494 {
495 return ln && ln->group && !list_empty(&ln->group->gr_children);
496 }
497
498 /*
499 * Filter stuff
500 */
501 enum filter_holder {
502 F_HOLDER_NONE,
503 F_HOLDER_COLUMN /* column name */
504 };
505
506 /* node types */
507 enum filter_ntype {
508 F_NODE_PARAM,
509 F_NODE_EXPR
510 };
511
512 /* expression types */
513 enum filter_etype {
514 F_EXPR_AND,
515 F_EXPR_OR,
516 F_EXPR_NEG,
517
518 F_EXPR_EQ,
519 F_EXPR_NE,
520
521 F_EXPR_LT,
522 F_EXPR_LE,
523 F_EXPR_GT,
524 F_EXPR_GE,
525
526 F_EXPR_REG,
527 F_EXPR_NREG,
528 };
529
530 struct filter_node {
531 enum filter_ntype type;
532 int refcount;
533 };
534
535 #define filter_node_get_type(n) (((struct filter_node *)(n))->type)
536
537 struct filter_param;
538 struct filter_expr;
539
540 struct libscols_counter {
541 char *name;
542 struct list_head counters;
543 struct filter_param *param;
544 struct libscols_filter *filter;
545
546 int func;
547 unsigned long long result;
548
549 unsigned int neg : 1,
550 has_result : 1;
551 };
552
553 struct libscols_filter {
554 int refcount;
555 char *errmsg;
556 struct filter_node *root;
557 FILE *src;
558
559 int (*filler_cb)(struct libscols_filter *, struct libscols_line *, size_t, void *);
560 void *filler_data;
561
562 struct list_head params;
563 struct list_head counters;
564 };
565
566 struct filter_node *__filter_new_node(enum filter_ntype type, size_t sz);
567 void filter_ref_node(struct filter_node *n);
568 void filter_unref_node(struct filter_node *n);
569
570 void filter_dump_node(struct ul_jsonwrt *json, struct filter_node *n);
571 int filter_eval_node(struct libscols_filter *fltr, struct libscols_line *ln,
572 struct filter_node *n, int *status);
573 /* param */
574 int filter_compile_param(struct libscols_filter *fltr, struct filter_param *n);
575 void filter_dump_param(struct ul_jsonwrt *json, struct filter_param *n);
576 int filter_eval_param(struct libscols_filter *fltr, struct libscols_line *ln,
577 struct filter_param *n, int *status);
578 void filter_free_param(struct filter_param *n);
579 int filter_param_reset_holder(struct filter_param *n);
580 int filter_param_get_datatype(struct filter_param *n);
581
582 int filter_next_param(struct libscols_filter *fltr,
583 struct libscols_iter *itr, struct filter_param **prm);
584
585 int filter_compare_params(struct libscols_filter *fltr,
586 enum filter_etype oper,
587 struct filter_param *l,
588 struct filter_param *r,
589 int *status);
590 int filter_cast_param(struct libscols_filter *fltr,
591 struct libscols_line *ln,
592 int type,
593 struct filter_param *n,
594 struct filter_param **result);
595
596 int is_filter_holder_node(struct filter_node *n);
597
598 int filter_count_param(struct libscols_filter *fltr,
599 struct libscols_line *ln,
600 struct libscols_counter *ct);
601
602 /* expr */
603 void filter_free_expr(struct filter_expr *n);
604 void filter_dump_expr(struct ul_jsonwrt *json, struct filter_expr *n);
605 int filter_eval_expr(struct libscols_filter *fltr, struct libscols_line *ln,
606 struct filter_expr *n, int *status);
607
608 /* required by parser */
609 struct filter_node *filter_new_param(struct libscols_filter *filter,
610 int type,
611 enum filter_holder holder,
612 void *data);
613 struct filter_node *filter_new_expr(struct libscols_filter *filter,
614 enum filter_etype type,
615 struct filter_node *left,
616 struct filter_node *right);
617
618 #endif /* _LIBSMARTCOLS_PRIVATE_H */