Debian Codegraph @

libsmartcols/src/table.c

raw
1 /*
2 * table.c - functions handling the data at the table level
3 *
4 * Copyright (C) 2010-2014 Karel Zak <[email protected]>
5 * Copyright (C) 2014 Ondrej Oprala <[email protected]>
6 * Copyright (C) 2016 Igor Gnatenko <[email protected]>
7 *
8 * This file may be redistributed under the terms of the
9 * GNU Lesser General Public License.
10 */
11
12 /**
13 * SECTION: table
14 * @title: Table
15 * @short_description: container for rows and columns
16 *
17 * Table data manipulation API.
18 */
19
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <string.h>
24 #include <termios.h>
25 #include <ctype.h>
26
27 #include "nls.h"
28 #include "ttyutils.h"
29 #include "smartcolsP.h"
30
31 #ifdef HAVE_WIDECHAR
32 #define UTF_V "\342\224\202" /* U+2502, Vertical line drawing char | */
33 #define UTF_VR "\342\224\234" /* U+251C, Vertical and right |- */
34 #define UTF_H "\342\224\200" /* U+2500, Horizontal - */
35 #define UTF_UR "\342\224\224" /* U+2514, Up and right '- */
36
37 #define UTF_V3 "\342\224\206" /* U+2506 Triple Dash Vertical | */
38 #define UTF_H3 "\342\224\210" /* U+2504 Triple Dash Horizontal - */
39 #define UTF_DR "\342\224\214" /* U+250C Down and Right ,- */
40 #define UTF_DH "\342\224\254" /* U+252C Down and Horizontal |' */
41
42 #define UTF_TR "\342\226\266" /* U+25B6 Black Right-Pointing Triangle > */
43 #endif /* !HAVE_WIDECHAR */
44
45 #define is_last_column(_tb, _cl) \
46 list_entry_is_last(&(_cl)->cl_columns, &(_tb)->tb_columns)
47
48
49 static void check_padding_debug(struct libscols_table *tb)
50 {
51 const char *str;
52
53 assert(libsmartcols_debug_mask); /* debug has to be already initialized! */
54
55 str = getenv("LIBSMARTCOLS_DEBUG_PADDING");
56 if (!str || (strcmp(str, "on") != 0 && strcmp(str, "1") != 0))
57 return;
58
59 DBG(INIT, ul_debugobj(tb, "padding debug: ENABLE"));
60 tb->padding_debug = 1;
61 }
62
63 static enum ul_json_format get_json_format(void)
64 {
65 const char *str;
66
67 str = getenv("LIBSMARTCOLS_JSON");
68 if (!str)
69 return UL_JSON_PRETTY;
70 else if (strcmp(str, "compact") == 0)
71 return UL_JSON_COMPACT;
72 else if (strcmp(str, "lines") == 0)
73 return UL_JSON_LINE;
74
75 /* default to pretty */
76 return UL_JSON_PRETTY;
77 }
78
79 /**
80 * scols_new_table:
81 *
82 * Returns: A newly allocated table.
83 */
84 struct libscols_table *scols_new_table(void)
85 {
86 struct libscols_table *tb;
87 int c, l;
88
89 tb = calloc(1, sizeof(struct libscols_table));
90 if (!tb)
91 return NULL;
92
93 tb->refcount = 1;
94 tb->out = stdout;
95
96 get_terminal_dimension(&c, &l);
97 tb->termwidth = c > 0 ? c : 80;
98 tb->termheight = l > 0 ? l : 24;
99
100 INIT_LIST_HEAD(&tb->tb_lines);
101 INIT_LIST_HEAD(&tb->tb_columns);
102 INIT_LIST_HEAD(&tb->tb_groups);
103
104 DBG(TAB, ul_debugobj(tb, "alloc"));
105 ON_DBG(INIT, check_padding_debug(tb));
106 tb->json_format = get_json_format();
107
108 return tb;
109 }
110
111 /**
112 * scols_ref_table:
113 * @tb: a pointer to a struct libscols_table instance
114 *
115 * Increases the refcount of @tb.
116 */
117 void scols_ref_table(struct libscols_table *tb)
118 {
119 if (tb)
120 tb->refcount++;
121 }
122
123 static void scols_table_remove_groups(struct libscols_table *tb)
124 {
125 while (!list_empty(&tb->tb_groups)) {
126 struct libscols_group *gr = list_entry(tb->tb_groups.next,
127 struct libscols_group, gr_groups);
128 scols_group_remove_children(gr);
129 scols_group_remove_members(gr);
130 scols_unref_group(gr);
131 }
132 }
133
134 /**
135 * scols_unref_table:
136 * @tb: a pointer to a struct libscols_table instance
137 *
138 * Decreases the refcount of @tb. When the count falls to zero, the instance
139 * is automatically deallocated.
140 */
141 void scols_unref_table(struct libscols_table *tb)
142 {
143 if (tb && (--tb->refcount <= 0)) {
144 DBG(TAB, ul_debugobj(tb, "dealloc <-"));
145 scols_table_remove_groups(tb);
146 scols_table_remove_lines(tb);
147 scols_table_remove_columns(tb);
148 scols_unref_symbols(tb->symbols);
149 scols_reset_cell(&tb->title);
150 free(tb->grpset);
151 free(tb->linesep);
152 free(tb->colsep);
153 free(tb->name);
154 free(tb);
155 DBG(TAB, ul_debug("<- done"));
156 }
157 }
158
159 /* Private API */
160 int scols_table_next_group(struct libscols_table *tb,
161 struct libscols_iter *itr,
162 struct libscols_group **gr)
163 {
164 int rc = 1;
165
166 if (!tb || !itr || !gr)
167 return -EINVAL;
168 *gr = NULL;
169
170 if (!itr->head)
171 SCOLS_ITER_INIT(itr, &tb->tb_groups);
172 if (itr->p != itr->head) {
173 SCOLS_ITER_ITERATE(itr, *gr, struct libscols_group, gr_groups);
174 rc = 0;
175 }
176
177 return rc;
178 }
179
180 /**
181 * scols_table_set_name:
182 * @tb: a pointer to a struct libscols_table instance
183 * @name: a name
184 *
185 * The table name is used for example for JSON top level object name.
186 *
187 * Returns: 0, a negative number in case of an error.
188 *
189 * Since: 2.27
190 */
191 int scols_table_set_name(struct libscols_table *tb, const char *name)
192 {
193 return strdup_to_struct_member(tb, name, name);
194 }
195
196 /**
197 * scols_table_get_name:
198 * @tb: a pointer to a struct libscols_table instance
199 *
200 * Returns: The current name setting of the table @tb
201 *
202 * Since: 2.29
203 */
204 const char *scols_table_get_name(const struct libscols_table *tb)
205 {
206 return tb->name;
207 }
208
209 /**
210 * scols_table_get_title:
211 * @tb: a pointer to a struct libscols_table instance
212 *
213 * The returned pointer is possible to modify by cell functions. Note that
214 * title output alignment on non-tty is hardcoded to 80 output chars. For the
215 * regular terminal it's based on terminal width.
216 *
217 * Returns: Title of the table, or NULL in case of blank title.
218 *
219 * Since: 2.28
220 */
221 struct libscols_cell *scols_table_get_title(struct libscols_table *tb)
222 {
223 return &tb->title;
224 }
225
226 /**
227 * scols_table_add_column:
228 * @tb: a pointer to a struct libscols_table instance
229 * @cl: a pointer to a struct libscols_column instance
230 *
231 * Adds @cl to @tb's column list. The column cannot be shared between more
232 * tables.
233 *
234 * Returns: 0, a negative number in case of an error.
235 */
236 int scols_table_add_column(struct libscols_table *tb, struct libscols_column *cl)
237 {
238 struct libscols_iter itr;
239 struct libscols_line *ln;
240 int rc = 0;
241
242 if (!tb || !cl || cl->table)
243 return -EINVAL;
244
245 if (!list_empty(&cl->cl_columns))
246 return -EINVAL;
247
248 if (cl->flags & SCOLS_FL_TREE)
249 tb->ntreecols++;
250
251 DBG(TAB, ul_debugobj(tb, "add column"));
252 list_add_tail(&cl->cl_columns, &tb->tb_columns);
253 cl->seqnum = tb->ncols++;
254 cl->table = tb;
255 scols_ref_column(cl);
256
257 if (list_empty(&tb->tb_lines))
258 return 0;
259
260 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
261
262 /* Realloc line cell arrays
263 */
264 while (scols_table_next_line(tb, &itr, &ln) == 0) {
265 rc = scols_line_alloc_cells(ln, tb->ncols);
266 if (rc)
267 break;
268 }
269
270 return rc;
271 }
272
273 /**
274 * scols_table_remove_column:
275 * @tb: a pointer to a struct libscols_table instance
276 * @cl: a pointer to a struct libscols_column instance
277 *
278 * Removes @cl from @tb.
279 *
280 * Returns: 0, a negative number in case of an error.
281 */
282 int scols_table_remove_column(struct libscols_table *tb,
283 struct libscols_column *cl)
284 {
285 if (!tb || !cl || !list_empty(&tb->tb_lines))
286 return -EINVAL;
287
288 if (cl->flags & SCOLS_FL_TREE)
289 tb->ntreecols--;
290 if (tb->dflt_sort_column == cl)
291 tb->dflt_sort_column = NULL;
292
293 DBG(TAB, ul_debugobj(tb, "remove column"));
294 list_del_init(&cl->cl_columns);
295 tb->ncols--;
296 cl->table = NULL;
297 scols_unref_column(cl);
298 return 0;
299 }
300
301 /**
302 * scols_table_remove_columns:
303 * @tb: a pointer to a struct libscols_table instance
304 *
305 * Removes all of @tb's columns.
306 *
307 * Returns: 0, a negative number in case of an error.
308 */
309 int scols_table_remove_columns(struct libscols_table *tb)
310 {
311 if (!tb || !list_empty(&tb->tb_lines))
312 return -EINVAL;
313
314 DBG(TAB, ul_debugobj(tb, "remove all columns"));
315 while (!list_empty(&tb->tb_columns)) {
316 struct libscols_column *cl = list_entry(tb->tb_columns.next,
317 struct libscols_column, cl_columns);
318 scols_table_remove_column(tb, cl);
319 }
320 return 0;
321 }
322
323 /**
324 * scols_table_move_column:
325 * @tb: table
326 * @pre: column before the column
327 * @cl: column to move
328 *
329 * Move the @cl behind @pre. If the @pre is NULL then the @col is the first
330 * column in the table.
331 *
332 * Since: 2.30
333 *
334 * Returns: 0, a negative number in case of an error.
335 */
336 int scols_table_move_column(struct libscols_table *tb,
337 struct libscols_column *pre,
338 struct libscols_column *cl)
339 {
340 struct list_head *head;
341 struct libscols_iter itr;
342 struct libscols_column *p;
343 struct libscols_line *ln;
344 size_t n = 0, oldseq;
345
346 if (!tb || !cl)
347 return -EINVAL;
348
349 if (pre && pre->seqnum + 1 == cl->seqnum)
350 return 0;
351 if (pre == NULL && cl->seqnum == 0)
352 return 0;
353
354 DBG(TAB, ul_debugobj(tb, "move column %zu behind %zu",
355 cl->seqnum, pre? pre->seqnum : 0));
356
357 list_del_init(&cl->cl_columns); /* remove from old position */
358
359 head = pre ? &pre->cl_columns : &tb->tb_columns;
360 list_add(&cl->cl_columns, head); /* add to the new place */
361
362 oldseq = cl->seqnum;
363
364 /* fix seq. numbers */
365 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
366 while (scols_table_next_column(tb, &itr, &p) == 0)
367 p->seqnum = n++;
368
369 /* move data in lines */
370 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
371 while (scols_table_next_line(tb, &itr, &ln) == 0)
372 scols_line_move_cells(ln, cl->seqnum, oldseq);
373 return 0;
374 }
375
376 /**
377 * scols_table_new_column:
378 * @tb: table
379 * @name: column header
380 * @whint: column width hint (absolute width: N > 1; relative width: 0 < N < 1)
381 * @flags: flags integer
382 *
383 * This is shortcut for
384 *
385 * <informalexample>
386 * <programlisting language="C">
387 * cl = scols_new_column();
388 * scols_column_set_....(cl, ...);
389 * scols_table_add_column(tb, cl);
390 * </programlisting>
391 * </informalexample>
392 *
393 * The column width is possible to define by:
394 *
395 * <informalexample>
396 * <programlisting>
397 * whint: 0 < N < 1 : relative width, percent of terminal width
398 *
399 * whint: N >= 1 : absolute width, empty column will be truncated to
400 * the column header width if no specified STRICTWIDTH flag
401 * </programlisting>
402 * </informalexample>
403 *
404 * Note that if table has disabled "maxout" flag (disabled by default) than
405 * relative width is used as a hint only. It's possible that column will be
406 * narrow if the specified size is too large for column data.
407 *
408 *
409 * If the width of all columns is greater than terminal width then library
410 * tries to reduce width of the individual columns. It's done in three stages:
411 *
412 * 1. reduce columns with SCOLS_FL_TRUNC flag and with relative width if the
413 * width is greater than width defined by @whint (@whint * terminal_width)
414 *
415 * 2. reduce all columns with SCOLS_FL_TRUNC flag
416 *
417 * 3. reduce all columns with relative width
418 *
419 * The next stage is always used if the previous stage is unsuccessful. Note
420 * that SCOLS_FL_WRAP is interpreted as SCOLS_FL_TRUNC when calculate column
421 * width (if custom wrap function is not specified), but the final text is not
422 * truncated, but wrapped to multi-line cell.
423 *
424 *
425 * The column is necessary to address by sequential number. The first defined
426 * column has the colnum = 0. For example:
427 *
428 * <informalexample>
429 * <programlisting language="C">
430 * scols_table_new_column(tab, "FOO", 0.5, 0); // colnum = 0
431 * scols_table_new_column(tab, "BAR", 0.5, 0); // colnum = 1
432 * .
433 * .
434 * scols_line_get_cell(line, 0); // FOO column
435 * scols_line_get_cell(line, 1); // BAR column
436 * </programlisting>
437 * </informalexample>
438 *
439 * Returns: newly allocated column
440 */
441 struct libscols_column *scols_table_new_column(struct libscols_table *tb,
442 const char *name,
443 double whint,
444 int flags)
445 {
446 struct libscols_column *cl;
447
448 if (!tb)
449 return NULL;
450
451 DBG(TAB, ul_debugobj(tb, "new column name=%s, whint=%g, flags=0x%04x",
452 name, whint, flags));
453 cl = scols_new_column();
454 if (!cl)
455 return NULL;
456
457 if (name && scols_column_set_name(cl, name))
458 goto err;
459 scols_column_set_whint(cl, whint);
460 scols_column_set_flags(cl, flags);
461
462 if (scols_table_add_column(tb, cl)) /* this increments column ref-counter */
463 goto err;
464
465 scols_unref_column(cl);
466 return cl;
467 err:
468 scols_unref_column(cl);
469 return NULL;
470 }
471
472 /**
473 * scols_table_next_column:
474 * @tb: a pointer to a struct libscols_table instance
475 * @itr: a pointer to a struct libscols_iter instance
476 * @cl: a pointer to a pointer to a struct libscols_column instance
477 *
478 * Returns the next column of @tb via @cl.
479 *
480 * Returns: 0, a negative value in case of an error.
481 */
482 int scols_table_next_column(struct libscols_table *tb,
483 struct libscols_iter *itr,
484 struct libscols_column **cl)
485 {
486 int rc = 1;
487
488 if (!tb || !itr || !cl)
489 return -EINVAL;
490 *cl = NULL;
491
492 if (!itr->head)
493 SCOLS_ITER_INIT(itr, &tb->tb_columns);
494 if (itr->p != itr->head) {
495 SCOLS_ITER_ITERATE(itr, *cl, struct libscols_column, cl_columns);
496 rc = 0;
497 }
498
499 return rc;
500 }
501
502 /**
503 * scols_table_set_columns_iter:
504 * @tb: tab pointer
505 * @itr: iterator
506 * @cl: tab entry
507 *
508 * Sets @iter to the position of @cl in the file @tb.
509 *
510 * Returns: 0 on success, negative number in case of error.
511 *
512 * Since: 2.35
513 */
514 int scols_table_set_columns_iter(
515 struct libscols_table *tb,
516 struct libscols_iter *itr,
517 struct libscols_column *cl)
518 {
519 if (!tb || !itr || !cl)
520 return -EINVAL;
521
522 if (cl->table != tb)
523 return -EINVAL;
524
525 SCOLS_ITER_INIT(itr, &tb->tb_columns);
526 itr->p = &cl->cl_columns;
527
528 return 0;
529 }
530
531 /**
532 * scols_table_get_ncols:
533 * @tb: table
534 *
535 * Returns: the ncols table member.
536 */
537 size_t scols_table_get_ncols(const struct libscols_table *tb)
538 {
539 return tb->ncols;
540 }
541
542 /**
543 * scols_table_get_nlines:
544 * @tb: table
545 *
546 * Returns: the nlines table member.
547 */
548 size_t scols_table_get_nlines(const struct libscols_table *tb)
549 {
550 return tb->nlines;
551 }
552
553
554 int scols_table_set_cursor(struct libscols_table *tb,
555 struct libscols_line *ln,
556 struct libscols_column *cl,
557 struct libscols_cell *ce)
558 {
559 if (!tb)
560 return -EINVAL;
561
562 tb->cur_line = ln;
563 tb->cur_column = cl;
564 tb->cur_cell = ce;
565
566 return 0;
567 }
568
569 /**
570 * scols_table_get_cursor:
571 * @tb: table
572 * @ln: returns current line (optional)
573 * @cl: returns current column (optional)
574 * @ce: returns current cell (optional)
575 *
576 * Returns: 0 on success, negative number in case of error.
577 *
578 * Since: 2.40
579 */
580 int scols_table_get_cursor(struct libscols_table *tb,
581 struct libscols_line **ln,
582 struct libscols_column **cl,
583 struct libscols_cell **ce)
584 {
585 if (!tb)
586 return -EINVAL;
587
588 if (ln)
589 *ln = tb->cur_line;
590 if (cl)
591 *cl = tb->cur_column;
592 if (ce)
593 *ce = tb->cur_cell;
594 return 0;
595 }
596
597 /**
598 * scols_table_set_stream:
599 * @tb: table
600 * @stream: output stream
601 *
602 * Sets the output stream for table @tb.
603 *
604 * Returns: 0, a negative number in case of an error.
605 */
606 int scols_table_set_stream(struct libscols_table *tb, FILE *stream)
607 {
608 assert(tb);
609 if (!tb)
610 return -EINVAL;
611
612 DBG(TAB, ul_debugobj(tb, "setting alternative stream"));
613 tb->out = stream;
614 return 0;
615 }
616
617 /**
618 * scols_table_get_stream:
619 * @tb: table
620 *
621 * Gets the output stream for table @tb.
622 *
623 * Returns: stream pointer, NULL in case of an error or an unset stream.
624 */
625 FILE *scols_table_get_stream(const struct libscols_table *tb)
626 {
627 return tb->out;
628 }
629
630 /**
631 * scols_table_reduce_termwidth:
632 * @tb: table
633 * @reduce: width
634 *
635 * If necessary then libsmartcols use all terminal width, the @reduce setting
636 * provides extra space (for example for borders in ncurses applications).
637 *
638 * The @reduce must be smaller than terminal width, otherwise it's silently
639 * ignored. The reduction is not applied when STDOUT_FILENO is not terminal.
640 *
641 * Note that after output initialization (scols_table_print_* calls) the width
642 * will be reduced, this behavior affects subsequenced scols_table_get_termwidth()
643 * calls.
644 *
645 * Returns: 0, a negative value in case of an error.
646 */
647 int scols_table_reduce_termwidth(struct libscols_table *tb, size_t reduce)
648 {
649 if (!tb)
650 return -EINVAL;
651
652 DBG(TAB, ul_debugobj(tb, "reduce terminal width: %zu", reduce));
653 tb->termreduce = reduce;
654 return 0;
655 }
656
657 /**
658 * scols_table_get_column:
659 * @tb: table
660 * @n: number of column (0..N)
661 *
662 * Returns: pointer to column or NULL
663 */
664 struct libscols_column *scols_table_get_column(struct libscols_table *tb,
665 size_t n)
666 {
667 struct libscols_iter itr;
668 struct libscols_column *cl;
669
670 if (!tb)
671 return NULL;
672 if (n >= tb->ncols)
673 return NULL;
674
675 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
676 while (scols_table_next_column(tb, &itr, &cl) == 0) {
677 if (cl->seqnum == n)
678 return cl;
679 }
680 return NULL;
681 }
682
683 /**
684 * scols_table_get_column_ny_name
685 * @tb: table
686 * @name: column name
687 *
688 * Returns: pointer to column or NULL
689 *
690 * Since: 2.39
691 */
692 struct libscols_column *scols_table_get_column_by_name(
693 struct libscols_table *tb, const char *name)
694 {
695 struct libscols_iter itr;
696 struct libscols_column *cl;
697
698 if (!tb || !name)
699 return NULL;
700
701 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
702 while (scols_table_next_column(tb, &itr, &cl) == 0) {
703 const char *cn = scols_column_get_name(cl);
704
705 if (cn && strcmp(cn, name) == 0)
706 return cl;
707 }
708
709 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
710 while (scols_table_next_column(tb, &itr, &cl) == 0) {
711 const char *cn = scols_column_get_name_as_shellvar(cl);
712
713 if (cn && strcmp(cn, name) == 0)
714 return cl;
715 }
716
717 return NULL;
718 }
719
720
721 /**
722 * scols_table_add_line:
723 * @tb: table
724 * @ln: line
725 *
726 * Note that this function calls scols_line_alloc_cells() if number
727 * of the cells in the line is too small for @tb.
728 *
729 * Returns: 0, a negative value in case of an error.
730 */
731 int scols_table_add_line(struct libscols_table *tb, struct libscols_line *ln)
732 {
733 if (!tb || !ln)
734 return -EINVAL;
735
736 if (!list_empty(&ln->ln_lines))
737 return -EINVAL;
738
739 if (tb->ncols > ln->ncells) {
740 int rc = scols_line_alloc_cells(ln, tb->ncols);
741 if (rc)
742 return rc;
743 }
744
745 DBG(TAB, ul_debugobj(tb, "add line"));
746 list_add_tail(&ln->ln_lines, &tb->tb_lines);
747 ln->seqnum = tb->nlines++;
748 scols_ref_line(ln);
749 return 0;
750 }
751
752 /**
753 * scols_table_remove_line:
754 * @tb: table
755 * @ln: line
756 *
757 * Note that this function does not destroy the parent<->child relationship between lines.
758 * You have to call scols_line_remove_child()
759 *
760 * Returns: 0, a negative value in case of an error.
761 */
762 int scols_table_remove_line(struct libscols_table *tb,
763 struct libscols_line *ln)
764 {
765 if (!tb || !ln)
766 return -EINVAL;
767
768 DBG(TAB, ul_debugobj(tb, "remove line"));
769 list_del_init(&ln->ln_lines);
770 tb->nlines--;
771 scols_unref_line(ln);
772 return 0;
773 }
774
775 /**
776 * scols_table_remove_lines:
777 * @tb: table
778 *
779 * This empties the table and also destroys all the parent<->child relationships.
780 */
781 void scols_table_remove_lines(struct libscols_table *tb)
782 {
783 if (!tb)
784 return;
785
786 DBG(TAB, ul_debugobj(tb, "remove all lines"));
787 while (!list_empty(&tb->tb_lines)) {
788 struct libscols_line *ln = list_entry(tb->tb_lines.next,
789 struct libscols_line, ln_lines);
790 if (ln->parent)
791 scols_line_remove_child(ln->parent, ln);
792 scols_table_remove_line(tb, ln);
793 }
794 }
795
796 /**
797 * scols_table_next_line:
798 * @tb: a pointer to a struct libscols_table instance
799 * @itr: a pointer to a struct libscols_iter instance
800 * @ln: a pointer to a pointer to a struct libscols_line instance
801 *
802 * Finds the next line and returns a pointer to it via @ln.
803 *
804 * Returns: 0, a negative value in case of an error.
805 */
806 int scols_table_next_line(struct libscols_table *tb,
807 struct libscols_iter *itr,
808 struct libscols_line **ln)
809 {
810 int rc = 1;
811
812 if (!tb || !itr || !ln)
813 return -EINVAL;
814 *ln = NULL;
815
816 if (!itr->head)
817 SCOLS_ITER_INIT(itr, &tb->tb_lines);
818 if (itr->p != itr->head) {
819 SCOLS_ITER_ITERATE(itr, *ln, struct libscols_line, ln_lines);
820 rc = 0;
821 }
822
823 return rc;
824 }
825
826 /**
827 * scols_table_new_line:
828 * @tb: table
829 * @parent: parental line or NULL
830 *
831 * This is shortcut for
832 *
833 * <informalexample>
834 * <programlisting language="C">
835 * ln = scols_new_line();
836 * scols_table_add_line(tb, ln);
837 * scols_line_add_child(parent, ln);
838 * </programlisting>
839 * </informalexample>
840 *
841 * Returns: newly allocate line
842 */
843 struct libscols_line *scols_table_new_line(struct libscols_table *tb,
844 struct libscols_line *parent)
845 {
846 struct libscols_line *ln;
847
848 if (!tb)
849 return NULL;
850
851 ln = scols_new_line();
852 if (!ln)
853 return NULL;
854
855 if (scols_table_add_line(tb, ln))
856 goto err;
857 if (parent)
858 scols_line_add_child(parent, ln);
859
860 scols_unref_line(ln); /* ref-counter incremented by scols_table_add_line() */
861 return ln;
862 err:
863 scols_unref_line(ln);
864 return NULL;
865 }
866
867 /**
868 * scols_table_get_line:
869 * @tb: table
870 * @n: column number (0..N)
871 *
872 * Returns: a line or NULL
873 */
874 struct libscols_line *scols_table_get_line(struct libscols_table *tb,
875 size_t n)
876 {
877 struct libscols_iter itr;
878 struct libscols_line *ln;
879
880 if (!tb)
881 return NULL;
882 if (n >= tb->nlines)
883 return NULL;
884
885 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
886 while (scols_table_next_line(tb, &itr, &ln) == 0) {
887 if (ln->seqnum == n)
888 return ln;
889 }
890 return NULL;
891 }
892
893 /**
894 * scols_copy_table:
895 * @tb: table
896 *
897 * Creates a new independent table copy, except struct libscols_symbols that
898 * are shared between the tables.
899 *
900 * Returns: a newly allocated copy of @tb
901 */
902 struct libscols_table *scols_copy_table(struct libscols_table *tb)
903 {
904 struct libscols_table *ret;
905 struct libscols_line *ln;
906 struct libscols_column *cl;
907 struct libscols_iter itr;
908
909 if (!tb)
910 return NULL;
911 ret = scols_new_table();
912 if (!ret)
913 return NULL;
914
915 DBG(TAB, ul_debugobj(tb, "copy"));
916
917 if (tb->symbols)
918 scols_table_set_symbols(ret, tb->symbols);
919
920 /* columns */
921 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
922 while (scols_table_next_column(tb, &itr, &cl) == 0) {
923 cl = scols_copy_column(cl);
924 if (!cl)
925 goto err;
926 if (scols_table_add_column(ret, cl))
927 goto err;
928 scols_unref_column(cl);
929 }
930
931 /* lines */
932 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
933 while (scols_table_next_line(tb, &itr, &ln) == 0) {
934 struct libscols_line *newln = scols_copy_line(ln);
935 if (!newln)
936 goto err;
937 if (scols_table_add_line(ret, newln))
938 goto err;
939 if (ln->parent) {
940 struct libscols_line *p =
941 scols_table_get_line(ret, ln->parent->seqnum);
942 if (p)
943 scols_line_add_child(p, newln);
944 }
945 scols_unref_line(newln);
946 }
947
948 /* separators */
949 if (scols_table_set_column_separator(ret, tb->colsep) ||
950 scols_table_set_line_separator(ret, tb->linesep))
951 goto err;
952
953 return ret;
954 err:
955 scols_unref_table(ret);
956 return NULL;
957 }
958
959 /**
960 * scols_table_set_default_symbols:
961 * @tb: table
962 *
963 * The library check the current environment to select ASCII or UTF8 symbols.
964 * This default behavior could be controlled by scols_table_enable_ascii().
965 *
966 * Use scols_table_set_symbols() to unset symbols or use your own setting.
967 *
968 * Returns: 0, a negative value in case of an error.
969 *
970 * Since: 2.29
971 */
972 int scols_table_set_default_symbols(struct libscols_table *tb)
973 {
974 struct libscols_symbols *sy;
975 int rc;
976
977 if (!tb)
978 return -EINVAL;
979
980 DBG(TAB, ul_debugobj(tb, "setting default symbols"));
981
982 sy = scols_new_symbols();
983 if (!sy)
984 return -ENOMEM;
985
986 #if defined(HAVE_WIDECHAR)
987 if (!scols_table_is_ascii(tb) &&
988 !strcmp(nl_langinfo(CODESET), "UTF-8")) {
989 /* tree chart */
990 scols_symbols_set_branch(sy, UTF_VR UTF_H);
991 scols_symbols_set_vertical(sy, UTF_V " ");
992 scols_symbols_set_right(sy, UTF_UR UTF_H);
993 /* groups chart */
994 scols_symbols_set_group_horizontal(sy, UTF_H3);
995 scols_symbols_set_group_vertical(sy, UTF_V3);
996
997 scols_symbols_set_group_first_member(sy, UTF_DR UTF_H3 UTF_TR);
998 scols_symbols_set_group_last_member(sy, UTF_UR UTF_DH UTF_TR);
999 scols_symbols_set_group_middle_member(sy, UTF_VR UTF_H3 UTF_TR);
1000 scols_symbols_set_group_last_child(sy, UTF_UR UTF_H3);
1001 scols_symbols_set_group_middle_child(sy, UTF_VR UTF_H3);
1002 } else
1003 #endif
1004 {
1005 /* tree chart */
1006 scols_symbols_set_branch(sy, "|-");
1007 scols_symbols_set_vertical(sy, "| ");
1008 scols_symbols_set_right(sy, "`-");
1009 /* groups chart */
1010 scols_symbols_set_group_horizontal(sy, "-");
1011 scols_symbols_set_group_vertical(sy, "|");
1012
1013 scols_symbols_set_group_first_member(sy, ",->");
1014 scols_symbols_set_group_last_member(sy, "'->");
1015 scols_symbols_set_group_middle_member(sy, "|->");
1016 scols_symbols_set_group_last_child(sy, "`-");
1017 scols_symbols_set_group_middle_child(sy, "|-");
1018 }
1019 scols_symbols_set_title_padding(sy, " ");
1020 scols_symbols_set_cell_padding(sy, " ");
1021
1022 rc = scols_table_set_symbols(tb, sy);
1023 scols_unref_symbols(sy);
1024 return rc;
1025 }
1026
1027
1028 /**
1029 * scols_table_set_symbols:
1030 * @tb: table
1031 * @sy: symbols or NULL
1032 *
1033 * Add a reference to @sy from the table. The symbols are used by library to
1034 * draw tree output. If no symbols are used for the table then library creates
1035 * default temporary symbols to draw output by scols_table_set_default_symbols().
1036 *
1037 * If @sy is NULL then remove reference from the currently used symbols.
1038 *
1039 * Returns: 0, a negative value in case of an error.
1040 */
1041 int scols_table_set_symbols(struct libscols_table *tb,
1042 struct libscols_symbols *sy)
1043 {
1044 if (!tb)
1045 return -EINVAL;
1046
1047 /* remove old */
1048 if (tb->symbols) {
1049 DBG(TAB, ul_debugobj(tb, "remove symbols reference"));
1050 scols_unref_symbols(tb->symbols);
1051 tb->symbols = NULL;
1052 }
1053
1054 /* set new */
1055 if (sy) { /* ref user defined */
1056 DBG(TAB, ul_debugobj(tb, "set symbols"));
1057 tb->symbols = sy;
1058 scols_ref_symbols(sy);
1059 }
1060 return 0;
1061 }
1062
1063 /**
1064 * scols_table_get_symbols:
1065 * @tb: table
1066 *
1067 * Returns: pointer to symbols table.
1068 *
1069 * Since: 2.29
1070 */
1071 struct libscols_symbols *scols_table_get_symbols(const struct libscols_table *tb)
1072 {
1073 return tb->symbols;
1074 }
1075
1076 /**
1077 * scols_table_enable_nolinesep:
1078 * @tb: table
1079 * @enable: 1 or 0
1080 *
1081 * Enable/disable line separator printing. This is useful if you want to
1082 * re-printing the same line more than once (e.g. progress bar). Don't use it
1083 * if you're not sure.
1084 *
1085 * Note that for the last line in the table the separator is disabled at all.
1086 * The library differentiate between table terminator and line terminator
1087 * (although for standard output \n byte is used in both cases).
1088 *
1089 * Returns: 0 on success, negative number in case of an error.
1090 */
1091 int scols_table_enable_nolinesep(struct libscols_table *tb, int enable)
1092 {
1093 if (!tb)
1094 return -EINVAL;
1095
1096 DBG(TAB, ul_debugobj(tb, "nolinesep: %s", enable ? "ENABLE" : "DISABLE"));
1097 tb->no_linesep = enable ? 1 : 0;
1098 return 0;
1099 }
1100
1101 /**
1102 * scols_table_is_nolinesep:
1103 * @tb: a pointer to a struct libscols_table instance
1104 *
1105 * Returns: 1 if line separator printing is disabled.
1106 *
1107 * Since: 2.29
1108 */
1109 int scols_table_is_nolinesep(const struct libscols_table *tb)
1110 {
1111 return tb->no_linesep;
1112 }
1113
1114 /**
1115 * scols_table_enable_colors:
1116 * @tb: table
1117 * @enable: 1 or 0
1118 *
1119 * Enable/disable colors.
1120 *
1121 * Returns: 0 on success, negative number in case of an error.
1122 */
1123 int scols_table_enable_colors(struct libscols_table *tb, int enable)
1124 {
1125 if (!tb)
1126 return -EINVAL;
1127
1128 DBG(TAB, ul_debugobj(tb, "colors: %s", enable ? "ENABLE" : "DISABLE"));
1129 tb->colors_wanted = enable;
1130 return 0;
1131 }
1132
1133 /**
1134 * scols_table_enable_raw:
1135 * @tb: table
1136 * @enable: 1 or 0
1137 *
1138 * Enable/disable raw output format. The parsable output formats
1139 * (export, raw, JSON, ...) are mutually exclusive.
1140 *
1141 * Returns: 0 on success, negative number in case of an error.
1142 */
1143 int scols_table_enable_raw(struct libscols_table *tb, int enable)
1144 {
1145 if (!tb)
1146 return -EINVAL;
1147
1148 DBG(TAB, ul_debugobj(tb, "raw: %s", enable ? "ENABLE" : "DISABLE"));
1149 if (enable)
1150 tb->format = SCOLS_FMT_RAW;
1151 else if (tb->format == SCOLS_FMT_RAW)
1152 tb->format = 0;
1153 return 0;
1154 }
1155
1156 /**
1157 * scols_table_enable_json:
1158 * @tb: table
1159 * @enable: 1 or 0
1160 *
1161 * Enable/disable JSON output format. The parsable output formats
1162 * (export, raw, JSON, ...) are mutually exclusive.
1163 *
1164 * Returns: 0 on success, negative number in case of an error.
1165 *
1166 * Since: 2.27
1167 */
1168 int scols_table_enable_json(struct libscols_table *tb, int enable)
1169 {
1170 if (!tb)
1171 return -EINVAL;
1172
1173 DBG(TAB, ul_debugobj(tb, "json: %s", enable ? "ENABLE" : "DISABLE"));
1174 if (enable)
1175 tb->format = SCOLS_FMT_JSON;
1176 else if (tb->format == SCOLS_FMT_JSON)
1177 tb->format = 0;
1178 return 0;
1179 }
1180
1181 /**
1182 * scols_table_enable_export:
1183 * @tb: table
1184 * @enable: 1 or 0
1185 *
1186 * Enable/disable export output format (COLUMNAME="value" ...).
1187 * The parsable output formats (export and raw) are mutually exclusive.
1188 *
1189 * See also scols_table_enable_shellvar(). Note that in version 2.37 (and only
1190 * in this version) scols_table_enable_shellvar() functionality has been
1191 * automatically enabled for "export" format. This behavior has been reverted
1192 * in version 2.38 due to backward compatibility issues. Now it's necessary to
1193 * explicitly call scols_table_enable_shellvar().
1194 *
1195 * Returns: 0 on success, negative number in case of an error.
1196 */
1197 int scols_table_enable_export(struct libscols_table *tb, int enable)
1198 {
1199 if (!tb)
1200 return -EINVAL;
1201
1202 DBG(TAB, ul_debugobj(tb, "export: %s", enable ? "ENABLE" : "DISABLE"));
1203 if (enable)
1204 tb->format = SCOLS_FMT_EXPORT;
1205 else if (tb->format == SCOLS_FMT_EXPORT)
1206 tb->format = 0;
1207 return 0;
1208 }
1209
1210 /**
1211 * scols_table_enable_shellvar:
1212 * @tb: table
1213 * @enable: 1 or 0
1214 *
1215 * Force library to print column names to be compatible with shell requirements
1216 * to variable names. For example "1FOO%" will be printed as "_1FOO_PCT".
1217 *
1218 * Returns: 0 on success, negative number in case of an error.
1219 *
1220 * Since: 2.38
1221 */
1222 int scols_table_enable_shellvar(struct libscols_table *tb, int enable)
1223 {
1224 if (!tb)
1225 return -EINVAL;
1226
1227 DBG(TAB, ul_debugobj(tb, "shellvar: %s", enable ? "ENABLE" : "DISABLE"));
1228 tb->is_shellvar = enable ? 1 : 0;
1229 return 0;
1230 }
1231
1232
1233 /**
1234 * scols_table_enable_ascii:
1235 * @tb: table
1236 * @enable: 1 or 0
1237 *
1238 * The ASCII-only output is relevant for tree-like outputs. The library
1239 * checks if the current environment is UTF8 compatible by default. This
1240 * function overrides this check and force the library to use ASCII chars
1241 * for the tree.
1242 *
1243 * If a custom libcols_symbols are specified (see scols_table_set_symbols()
1244 * then ASCII flag setting is ignored.
1245 *
1246 * Returns: 0 on success, negative number in case of an error.
1247 */
1248 int scols_table_enable_ascii(struct libscols_table *tb, int enable)
1249 {
1250 if (!tb)
1251 return -EINVAL;
1252
1253 DBG(TAB, ul_debugobj(tb, "ascii: %s", enable ? "ENABLE" : "DISABLE"));
1254 tb->ascii = enable ? 1 : 0;
1255 return 0;
1256 }
1257
1258 /**
1259 * scols_table_enable_noheadings:
1260 * @tb: table
1261 * @enable: 1 or 0
1262 *
1263 * Enable/disable header line.
1264 *
1265 * Returns: 0 on success, negative number in case of an error.
1266 */
1267 int scols_table_enable_noheadings(struct libscols_table *tb, int enable)
1268 {
1269 if (!tb)
1270 return -EINVAL;
1271 DBG(TAB, ul_debugobj(tb, "noheading: %s", enable ? "ENABLE" : "DISABLE"));
1272 tb->no_headings = enable ? 1 : 0;
1273 return 0;
1274 }
1275
1276 /**
1277 * scols_table_enable_header_repeat:
1278 * @tb: table
1279 * @enable: 1 or 0
1280 *
1281 * Enable/disable header line repeat. The header line is printed only once by
1282 * default. Note that the flag will be silently ignored and disabled if the
1283 * output is not on terminal or output format is JSON, raw, etc.
1284 *
1285 * Returns: 0 on success, negative number in case of an error.
1286 *
1287 * Since: 2.31
1288 */
1289 int scols_table_enable_header_repeat(struct libscols_table *tb, int enable)
1290 {
1291 if (!tb)
1292 return -EINVAL;
1293 DBG(TAB, ul_debugobj(tb, "header-repeat: %s", enable ? "ENABLE" : "DISABLE"));
1294 tb->header_repeat = enable ? 1 : 0;
1295 return 0;
1296 }
1297
1298 /**
1299 * scols_table_enable_maxout:
1300 * @tb: table
1301 * @enable: 1 or 0
1302 *
1303 * The extra space after last column is ignored by default. The output
1304 * maximization add padding for all columns.
1305 *
1306 * This setting is mutually exclusive to scols_table_enable_minout().
1307 *
1308 * Returns: 0 on success, negative number in case of an error.
1309 */
1310 int scols_table_enable_maxout(struct libscols_table *tb, int enable)
1311 {
1312 if (!tb || tb->minout)
1313 return -EINVAL;
1314
1315 DBG(TAB, ul_debugobj(tb, "maxout: %s", enable ? "ENABLE" : "DISABLE"));
1316 tb->maxout = enable ? 1 : 0;
1317 return 0;
1318 }
1319
1320 /**
1321 * scols_table_enable_minout:
1322 * @tb: table
1323 * @enable: 1 or 0
1324 *
1325 * Force library to terminate line after last column with data. The extra
1326 * padding is not added to the empty cells at the end of the line. The default is fill
1327 * trailing empty cells except the last line cell.
1328 *
1329 * This setting is mutually exclusive to scols_table_enable_maxout().
1330 *
1331 * Returns: 0 on success, negative number in case of an error.
1332 *
1333 * Since: 2.35
1334 */
1335 int scols_table_enable_minout(struct libscols_table *tb, int enable)
1336 {
1337 if (!tb || tb->maxout)
1338 return -EINVAL;
1339
1340 DBG(TAB, ul_debugobj(tb, "minout: %s", enable ? "ENABLE" : "DISABLE"));
1341 tb->minout = enable ? 1 : 0;
1342 return 0;
1343 }
1344
1345 /**
1346 * scols_table_enable_nowrap:
1347 * @tb: table
1348 * @enable: 1 or 0
1349 *
1350 * Never continue on next line, remove last column(s) when too large, truncate last column.
1351 *
1352 * Returns: 0 on success, negative number in case of an error.
1353 *
1354 * Since: 2.28
1355 */
1356 int scols_table_enable_nowrap(struct libscols_table *tb, int enable)
1357 {
1358 if (!tb)
1359 return -EINVAL;
1360 DBG(TAB, ul_debugobj(tb, "nowrap: %s", enable ? "ENABLE" : "DISABLE"));
1361 tb->no_wrap = enable ? 1 : 0;
1362 return 0;
1363 }
1364
1365 /**
1366 * scols_table_is_nowrap:
1367 * @tb: a pointer to a struct libscols_table instance
1368 *
1369 * Returns: 1 if nowrap is enabled.
1370 *
1371 * Since: 2.29
1372 */
1373 int scols_table_is_nowrap(const struct libscols_table *tb)
1374 {
1375 return tb->no_wrap;
1376 }
1377
1378 /**
1379 * scols_table_enable_noencoding:
1380 * @tb: table
1381 * @enable: 1 or 0
1382 *
1383 * The library encode non-printable and control chars by \xHEX by default.
1384 *
1385 * Returns: 0 on success, negative number in case of an error.
1386 *
1387 * Since: 2.31
1388 */
1389 int scols_table_enable_noencoding(struct libscols_table *tb, int enable)
1390 {
1391 if (!tb)
1392 return -EINVAL;
1393 DBG(TAB, ul_debugobj(tb, "encoding: %s", enable ? "ENABLE" : "DISABLE"));
1394 tb->no_encode = enable ? 1 : 0;
1395 return 0;
1396 }
1397
1398 /**
1399 * scols_table_is_noencoding:
1400 * @tb: a pointer to a struct libscols_table instance
1401 *
1402 * Returns: 1 if encoding is disabled.
1403 *
1404 * Since: 2.31
1405 */
1406 int scols_table_is_noencoding(const struct libscols_table *tb)
1407 {
1408 return tb->no_encode;
1409 }
1410
1411 /**
1412 * scols_table_colors_wanted:
1413 * @tb: table
1414 *
1415 * Returns: 1 if colors are enabled.
1416 */
1417 int scols_table_colors_wanted(const struct libscols_table *tb)
1418 {
1419 return tb->colors_wanted;
1420 }
1421
1422 /**
1423 * scols_table_is_empty:
1424 * @tb: table
1425 *
1426 * Returns: 1 if the table is empty.
1427 */
1428 int scols_table_is_empty(const struct libscols_table *tb)
1429 {
1430 return !tb->nlines;
1431 }
1432
1433 /**
1434 * scols_table_is_ascii:
1435 * @tb: table
1436 *
1437 * Returns: 1 if ASCII tree is enabled.
1438 */
1439 int scols_table_is_ascii(const struct libscols_table *tb)
1440 {
1441 return tb->ascii;
1442 }
1443
1444 /**
1445 * scols_table_is_noheadings:
1446 * @tb: table
1447 *
1448 * Returns: 1 if header output is disabled.
1449 */
1450 int scols_table_is_noheadings(const struct libscols_table *tb)
1451 {
1452 return tb->no_headings;
1453 }
1454
1455 /**
1456 * scols_table_is_header_repeat
1457 * @tb: table
1458 *
1459 * Returns: 1 if header repeat is enabled.
1460 *
1461 * Since: 2.31
1462 */
1463 int scols_table_is_header_repeat(const struct libscols_table *tb)
1464 {
1465 return tb->header_repeat;
1466 }
1467
1468 /**
1469 * scols_table_is_export:
1470 * @tb: table
1471 *
1472 * Returns: 1 if export output format is enabled.
1473 */
1474 int scols_table_is_export(const struct libscols_table *tb)
1475 {
1476 return tb->format == SCOLS_FMT_EXPORT;
1477 }
1478
1479 /**
1480 * scols_table_is_shellvar:
1481 * @tb: table
1482 *
1483 * Returns: 1 if column names has to be compatible with shell requirements
1484 * to variable names
1485 *
1486 * Since: 2.38
1487 */
1488 int scols_table_is_shellvar(const struct libscols_table *tb)
1489 {
1490 return tb->is_shellvar;
1491 }
1492
1493 /**
1494 * scols_table_is_raw:
1495 * @tb: table
1496 *
1497 * Returns: 1 if raw output format is enabled.
1498 */
1499 int scols_table_is_raw(const struct libscols_table *tb)
1500 {
1501 return tb->format == SCOLS_FMT_RAW;
1502 }
1503
1504 /**
1505 * scols_table_is_json:
1506 * @tb: table
1507 *
1508 * Returns: 1 if JSON output format is enabled.
1509 *
1510 * Since: 2.27
1511 */
1512 int scols_table_is_json(const struct libscols_table *tb)
1513 {
1514 return tb->format == SCOLS_FMT_JSON;
1515 }
1516
1517 /**
1518 * scols_table_is_maxout
1519 * @tb: table
1520 *
1521 * Returns: 1 if output maximization is enabled or 0
1522 */
1523 int scols_table_is_maxout(const struct libscols_table *tb)
1524 {
1525 return tb->maxout;
1526 }
1527
1528 /**
1529 * scols_table_is_minout
1530 * @tb: table
1531 *
1532 * Returns: 1 if output minimization is enabled or 0
1533 *
1534 * Since: 2.35
1535 */
1536 int scols_table_is_minout(const struct libscols_table *tb)
1537 {
1538 return tb->minout;
1539 }
1540
1541 /**
1542 * scols_table_is_tree:
1543 * @tb: table
1544 *
1545 * Returns: returns 1 tree-like output is expected.
1546 */
1547 int scols_table_is_tree(const struct libscols_table *tb)
1548 {
1549 return tb->ntreecols > 0;
1550 }
1551
1552 /**
1553 * scols_table_set_column_separator:
1554 * @tb: table
1555 * @sep: separator
1556 *
1557 * Sets the column separator of @tb to @sep.
1558 *
1559 * Returns: 0, a negative value in case of an error.
1560 */
1561 int scols_table_set_column_separator(struct libscols_table *tb, const char *sep)
1562 {
1563 return strdup_to_struct_member(tb, colsep, sep);
1564 }
1565
1566 /**
1567 * scols_table_set_line_separator:
1568 * @tb: table
1569 * @sep: separator
1570 *
1571 * Sets the line separator of @tb to @sep.
1572 *
1573 * Returns: 0, a negative value in case of an error.
1574 */
1575 int scols_table_set_line_separator(struct libscols_table *tb, const char *sep)
1576 {
1577 return strdup_to_struct_member(tb, linesep, sep);
1578 }
1579
1580 /**
1581 * scols_table_get_column_separator:
1582 * @tb: table
1583 *
1584 * Returns: @tb column separator, NULL in case of an error
1585 */
1586 const char *scols_table_get_column_separator(const struct libscols_table *tb)
1587 {
1588 return tb->colsep;
1589 }
1590
1591 /**
1592 * scols_table_get_line_separator:
1593 * @tb: table
1594 *
1595 * Returns: @tb line separator, NULL in case of an error
1596 */
1597 const char *scols_table_get_line_separator(const struct libscols_table *tb)
1598 {
1599 return tb->linesep;
1600 }
1601 /* for lines in the struct libscols_line->ln_lines list */
1602 static int cells_cmp_wrapper_lines(struct list_head *a, struct list_head *b, void *data)
1603 {
1604 struct libscols_column *cl = (struct libscols_column *) data;
1605 struct libscols_line *ra, *rb;
1606 struct libscols_cell *ca, *cb;
1607
1608 assert(a);
1609 assert(b);
1610 assert(cl);
1611
1612 ra = list_entry(a, struct libscols_line, ln_lines);
1613 rb = list_entry(b, struct libscols_line, ln_lines);
1614 ca = scols_line_get_cell(ra, cl->seqnum);
1615 cb = scols_line_get_cell(rb, cl->seqnum);
1616
1617 return cl->cmpfunc(ca, cb, cl->cmpfunc_data);
1618 }
1619
1620 /* for lines in the struct libscols_line->ln_children list */
1621 static int cells_cmp_wrapper_children(struct list_head *a, struct list_head *b, void *data)
1622 {
1623 struct libscols_column *cl = (struct libscols_column *) data;
1624 struct libscols_line *ra, *rb;
1625 struct libscols_cell *ca, *cb;
1626
1627 assert(a);
1628 assert(b);
1629 assert(cl);
1630
1631 ra = list_entry(a, struct libscols_line, ln_children);
1632 rb = list_entry(b, struct libscols_line, ln_children);
1633 ca = scols_line_get_cell(ra, cl->seqnum);
1634 cb = scols_line_get_cell(rb, cl->seqnum);
1635
1636 return cl->cmpfunc(ca, cb, cl->cmpfunc_data);
1637 }
1638
1639
1640 static int sort_line_children(struct libscols_line *ln, struct libscols_column *cl)
1641 {
1642 struct list_head *p;
1643
1644 if (!list_empty(&ln->ln_branch)) {
1645 list_for_each(p, &ln->ln_branch) {
1646 struct libscols_line *chld =
1647 list_entry(p, struct libscols_line, ln_children);
1648 sort_line_children(chld, cl);
1649 }
1650
1651 list_sort(&ln->ln_branch, cells_cmp_wrapper_children, cl);
1652 }
1653
1654 if (is_first_group_member(ln)) {
1655 list_for_each(p, &ln->group->gr_children) {
1656 struct libscols_line *chld =
1657 list_entry(p, struct libscols_line, ln_children);
1658 sort_line_children(chld, cl);
1659 }
1660
1661 list_sort(&ln->group->gr_children, cells_cmp_wrapper_children, cl);
1662 }
1663
1664 return 0;
1665 }
1666
1667 static int __scols_sort_tree(struct libscols_table *tb, struct libscols_column *cl)
1668 {
1669 struct libscols_line *ln;
1670 struct libscols_iter itr;
1671
1672 if (!tb || !cl || !cl->cmpfunc)
1673 return -EINVAL;
1674
1675 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
1676 while (scols_table_next_line(tb, &itr, &ln) == 0)
1677 sort_line_children(ln, cl);
1678 return 0;
1679 }
1680
1681 /**
1682 * scols_sort_table:
1683 * @tb: table
1684 * @cl: order by this column or NULL
1685 *
1686 * Orders the table by the column. See also scols_column_set_cmpfunc(). If the
1687 * tree output is enabled then children in the tree are recursively sorted too.
1688 *
1689 * The column @cl is saved as the default sort column to the @tb and the next time
1690 * is possible to call scols_sort_table(tb, NULL). The saved column is also used by
1691 * scols_sort_table_by_tree().
1692 *
1693 * Returns: 0, a negative value in case of an error.
1694 */
1695 int scols_sort_table(struct libscols_table *tb, struct libscols_column *cl)
1696 {
1697 if (!tb)
1698 return -EINVAL;
1699 if (!cl)
1700 cl = tb->dflt_sort_column;
1701 if (!cl || !cl->cmpfunc)
1702 return -EINVAL;
1703
1704 DBG(TAB, ul_debugobj(tb, "sorting table by %zu column", cl->seqnum));
1705 list_sort(&tb->tb_lines, cells_cmp_wrapper_lines, cl);
1706
1707 if (scols_table_is_tree(tb))
1708 __scols_sort_tree(tb, cl);
1709
1710 if (cl && cl != tb->dflt_sort_column)
1711 tb->dflt_sort_column = cl;
1712
1713 return 0;
1714 }
1715
1716 /*
1717 * Move all @ln's children after @ln in the table.
1718 */
1719 static struct libscols_line *move_line_and_children(struct libscols_line *ln, struct libscols_line *pre)
1720 {
1721 if (pre) {
1722 list_del_init(&ln->ln_lines); /* remove from old position */
1723 list_add(&ln->ln_lines, &pre->ln_lines); /* add to the new place (after @pre) */
1724 }
1725 pre = ln;
1726
1727 if (!list_empty(&ln->ln_branch)) {
1728 struct list_head *p;
1729
1730 list_for_each(p, &ln->ln_branch) {
1731 struct libscols_line *chld =
1732 list_entry(p, struct libscols_line, ln_children);
1733 pre = move_line_and_children(chld, pre);
1734 }
1735 }
1736
1737 return pre;
1738 }
1739
1740 /**
1741 * scols_sort_table_by_tree:
1742 * @tb: table
1743 *
1744 * Reorders lines in the table by parent->child relation. Note that order of
1745 * the lines in the table is independent on the tree hierarchy by default.
1746 *
1747 * The children of the lines are sorted according to the default sort column
1748 * if scols_sort_table() has been previously called.
1749 *
1750 * Since: 2.30
1751 *
1752 * Returns: 0, a negative value in case of an error.
1753 */
1754 int scols_sort_table_by_tree(struct libscols_table *tb)
1755 {
1756 struct libscols_line *ln;
1757 struct libscols_iter itr;
1758
1759 if (!tb)
1760 return -EINVAL;
1761
1762 DBG(TAB, ul_debugobj(tb, "sorting table by tree"));
1763
1764 if (tb->dflt_sort_column)
1765 __scols_sort_tree(tb, tb->dflt_sort_column);
1766
1767 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
1768 while (scols_table_next_line(tb, &itr, &ln) == 0)
1769 move_line_and_children(ln, NULL);
1770
1771 return 0;
1772 }
1773
1774
1775 /**
1776 * scols_table_set_termforce:
1777 * @tb: table
1778 * @force: SCOLS_TERMFORCE_{NEVER,ALWAYS,AUTO}
1779 *
1780 * Forces library to use stdout as terminal, non-terminal or use automatic
1781 * detection (default).
1782 *
1783 * Returns: 0, a negative value in case of an error.
1784 *
1785 * Since: 2.29
1786 */
1787 int scols_table_set_termforce(struct libscols_table *tb, int force)
1788 {
1789 if (!tb)
1790 return -EINVAL;
1791 tb->termforce = force;
1792 return 0;
1793 }
1794
1795 /**
1796 * scols_table_get_termforce:
1797 * @tb: table
1798 *
1799 * Returns: SCOLS_TERMFORCE_{NEVER,ALWAYS,AUTO} or a negative value in case of an error.
1800 *
1801 * Since: 2.29
1802 */
1803 int scols_table_get_termforce(const struct libscols_table *tb)
1804 {
1805 return tb->termforce;
1806 }
1807
1808 /**
1809 * scols_table_set_termwidth
1810 * @tb: table
1811 * @width: terminal width
1812 *
1813 * The library automatically detects terminal width or defaults to 80 chars if
1814 * detections is unsuccessful. This function override this behaviour.
1815 *
1816 * Returns: 0, a negative value in case of an error.
1817 *
1818 * Since: 2.29
1819 */
1820 int scols_table_set_termwidth(struct libscols_table *tb, size_t width)
1821 {
1822 DBG(TAB, ul_debugobj(tb, "set terminatl width: %zu", width));
1823 tb->termwidth = width;
1824 return 0;
1825 }
1826
1827 /**
1828 * scols_table_get_termwidth
1829 * @tb: table
1830 *
1831 * Returns: terminal width.
1832 */
1833 size_t scols_table_get_termwidth(const struct libscols_table *tb)
1834 {
1835 return tb->termwidth;
1836 }
1837
1838 /**
1839 * scols_table_set_termheight
1840 * @tb: table
1841 * @height: terminal height (number of lines)
1842 *
1843 * The library automatically detects terminal height or defaults to 24 lines if
1844 * detections is unsuccessful. This function override this behaviour.
1845 *
1846 * Returns: 0, a negative value in case of an error.
1847 *
1848 * Since: 2.31
1849 */
1850 int scols_table_set_termheight(struct libscols_table *tb, size_t height)
1851 {
1852 DBG(TAB, ul_debugobj(tb, "set terminatl height: %zu", height));
1853 tb->termheight = height;
1854 return 0;
1855 }
1856
1857 /**
1858 * scols_table_get_termheight
1859 * @tb: table
1860 *
1861 * Returns: terminal height (number of lines).
1862 *
1863 * Since: 2.31
1864 */
1865 size_t scols_table_get_termheight(const struct libscols_table *tb)
1866 {
1867 return tb->termheight;
1868 }