Debian Codegraph @

libsmartcols/src/print.c

raw
1 /* print.c - functions to print table
2 *
3 * Copyright (C) 2010-2014 Karel Zak <[email protected]>
4 * Copyright (C) 2016 Igor Gnatenko <[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: table_print
12 * @title: Table print
13 * @short_description: output functions
14 *
15 * Table output API.
16 */
17
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <string.h>
21 #include <termios.h>
22 #include <ctype.h>
23
24 #include "mbsalign.h"
25 #include "carefulputc.h"
26 #include "smartcolsP.h"
27 #include "ttyutils.h"
28
29 /* Fallback for symbols
30 *
31 * Note that by default library define all the symbols, but in case user does
32 * not define all symbols or if we extended the symbols struct then we need
33 * fallback to be more robust and backwardly compatible.
34 */
35 #define titlepadding_symbol(tb) ((tb)->symbols->title_padding ? (tb)->symbols->title_padding : " ")
36 #define branch_symbol(tb) ((tb)->symbols->tree_branch ? (tb)->symbols->tree_branch : "|-")
37 #define vertical_symbol(tb) ((tb)->symbols->tree_vert ? (tb)->symbols->tree_vert : "| ")
38 #define right_symbol(tb) ((tb)->symbols->tree_right ? (tb)->symbols->tree_right : "`-")
39
40 #define grp_vertical_symbol(tb) ((tb)->symbols->group_vert ? (tb)->symbols->group_vert : "|")
41 #define grp_horizontal_symbol(tb) ((tb)->symbols->group_horz ? (tb)->symbols->group_horz : "-")
42 #define grp_m_first_symbol(tb) ((tb)->symbols->group_first_member ? (tb)->symbols->group_first_member : ",->")
43 #define grp_m_last_symbol(tb) ((tb)->symbols->group_last_member ? (tb)->symbols->group_last_member : "\\->")
44 #define grp_m_middle_symbol(tb) ((tb)->symbols->group_middle_member ? (tb)->symbols->group_middle_member : "|->")
45 #define grp_c_middle_symbol(tb) ((tb)->symbols->group_middle_child ? (tb)->symbols->group_middle_child : "|-")
46 #define grp_c_last_symbol(tb) ((tb)->symbols->group_last_child ? (tb)->symbols->group_last_child : "`-")
47
48 #define cellpadding_symbol(tb) ((tb)->padding_debug ? "." : \
49 ((tb)->symbols->cell_padding ? (tb)->symbols->cell_padding: " "))
50
51 #define want_repeat_header(tb) (!(tb)->header_repeat || (tb)->header_next <= (tb)->termlines_used)
52
53 static int is_next_columns_empty(
54 struct libscols_table *tb,
55 struct libscols_column *cl,
56 struct libscols_line *ln)
57 {
58 struct libscols_iter itr;
59
60 if (!tb || !cl)
61 return 0;
62 if (is_last_column(cl))
63 return 1;
64 if (!ln)
65 return 0;
66
67 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
68 scols_table_set_columns_iter(tb, &itr, cl);
69
70 /* skip current column */
71 scols_table_next_column(tb, &itr, &cl);
72
73 while (scols_table_next_column(tb, &itr, &cl) == 0) {
74 struct libscols_cell *ce;
75 const char *data = NULL;
76
77 if (scols_column_is_hidden(cl))
78 continue;
79 if (scols_column_is_tree(cl))
80 return 0;
81
82 ce = scols_line_get_cell(ln, cl->seqnum);
83 if (ce)
84 data = scols_cell_get_data(ce);
85 if (data && *data)
86 return 0;
87 }
88 return 1;
89 }
90
91 /* returns pointer to the end of used data */
92 static int tree_ascii_art_to_buffer(struct libscols_table *tb,
93 struct libscols_line *ln,
94 struct ul_buffer *buf)
95 {
96 const char *art;
97 int rc;
98
99 assert(ln);
100 assert(buf);
101
102 if (!ln->parent)
103 return 0;
104
105 rc = tree_ascii_art_to_buffer(tb, ln->parent, buf);
106 if (rc)
107 return rc;
108
109 if (is_last_child(ln))
110 art = " ";
111 else
112 art = vertical_symbol(tb);
113
114 return ul_buffer_append_string(buf, art);
115 }
116
117 static int grpset_is_empty( struct libscols_table *tb,
118 size_t idx,
119 size_t *rest)
120 {
121 size_t i;
122
123 for (i = idx; i < tb->grpset_size; i++) {
124 if (tb->grpset[i] == NULL) {
125 if (rest)
126 (*rest)++;
127 } else
128 return 0;
129 }
130 return 1;
131 }
132
133 static int groups_ascii_art_to_buffer( struct libscols_table *tb,
134 struct libscols_line *ln,
135 struct ul_buffer *buf,
136 int empty)
137 {
138 int filled = 0;
139 size_t i, rest = 0;
140 const char *filler = cellpadding_symbol(tb);
141
142 if (!has_groups(tb))
143 return 0;
144
145 DBG(LINE, ul_debugobj(ln, "printing groups chart"));
146
147 if (tb->is_dummy_print)
148 return 0; /* allocate grpset[] only */
149
150 for (i = 0; i < tb->grpset_size; i += SCOLS_GRPSET_CHUNKSIZ) {
151 struct libscols_group *gr = tb->grpset[i];
152
153 if (!gr) {
154 ul_buffer_append_ntimes(buf, SCOLS_GRPSET_CHUNKSIZ, cellpadding_symbol(tb));
155 continue;
156 }
157
158 /*
159 * Empty cells (multi-line entries, etc.), print vertical symbols only
160 * to show that the group continues.
161 */
162 if (empty) {
163 switch (gr->state) {
164 case SCOLS_GSTATE_FIRST_MEMBER:
165 case SCOLS_GSTATE_MIDDLE_MEMBER:
166 case SCOLS_GSTATE_CONT_MEMBERS:
167 ul_buffer_append_string(buf, grp_vertical_symbol(tb));
168 ul_buffer_append_ntimes(buf, 2, filler);
169 break;
170
171 case SCOLS_GSTATE_LAST_MEMBER:
172 case SCOLS_GSTATE_MIDDLE_CHILD:
173 case SCOLS_GSTATE_CONT_CHILDREN:
174 ul_buffer_append_string(buf, filler);
175 ul_buffer_append_string(buf, grp_vertical_symbol(tb));
176 ul_buffer_append_string(buf, filler);
177 break;
178 case SCOLS_GSTATE_LAST_CHILD:
179 ul_buffer_append_ntimes(buf, 3, filler);
180 break;
181 }
182 continue;
183 }
184
185 /*
186 * Regular cell
187 */
188 switch (gr->state) {
189 case SCOLS_GSTATE_FIRST_MEMBER:
190 ul_buffer_append_string(buf, grp_m_first_symbol(tb));
191 break;
192 case SCOLS_GSTATE_MIDDLE_MEMBER:
193 ul_buffer_append_string(buf, grp_m_middle_symbol(tb));
194 break;
195 case SCOLS_GSTATE_LAST_MEMBER:
196 ul_buffer_append_string(buf, grp_m_last_symbol(tb));
197 break;
198 case SCOLS_GSTATE_CONT_MEMBERS:
199 ul_buffer_append_string(buf, grp_vertical_symbol(tb));
200 ul_buffer_append_ntimes(buf, 2, filler);
201 break;
202 case SCOLS_GSTATE_MIDDLE_CHILD:
203 ul_buffer_append_string(buf, filler);
204 ul_buffer_append_string(buf, grp_c_middle_symbol(tb));
205 if (grpset_is_empty(tb, i + SCOLS_GRPSET_CHUNKSIZ, &rest)) {
206 ul_buffer_append_ntimes(buf, rest+1, grp_horizontal_symbol(tb));
207 filled = 1;
208 }
209 filler = grp_horizontal_symbol(tb);
210 break;
211 case SCOLS_GSTATE_LAST_CHILD:
212 ul_buffer_append_string(buf, cellpadding_symbol(tb));
213 ul_buffer_append_string(buf, grp_c_last_symbol(tb));
214 if (grpset_is_empty(tb, i + SCOLS_GRPSET_CHUNKSIZ, &rest)) {
215 ul_buffer_append_ntimes(buf, rest+1, grp_horizontal_symbol(tb));
216 filled = 1;
217 }
218 filler = grp_horizontal_symbol(tb);
219 break;
220 case SCOLS_GSTATE_CONT_CHILDREN:
221 ul_buffer_append_string(buf, filler);
222 ul_buffer_append_string(buf, grp_vertical_symbol(tb));
223 ul_buffer_append_string(buf, filler);
224 break;
225 }
226
227 if (filled)
228 break;
229 }
230
231 if (!filled)
232 ul_buffer_append_string(buf, filler);
233 return 0;
234 }
235
236 static void fputs_color_reset(struct libscols_table *tb)
237 {
238 if (tb->cur_color) {
239 fputs(UL_COLOR_RESET, tb->out);
240 tb->cur_color = NULL;
241 }
242 }
243
244 static void fputs_color(struct libscols_table *tb, const char *color)
245 {
246 if (tb->cur_color)
247 fputs_color_reset(tb);
248
249 tb->cur_color = color;
250 if (color)
251 fputs(color, tb->out);
252 }
253
254 static const char *get_cell_color(struct libscols_table *tb,
255 struct libscols_column *cl,
256 struct libscols_line *ln,
257 struct libscols_cell *ce)
258 {
259 const char *color = NULL;
260
261 if (!tb || !tb->colors_wanted || tb->format != SCOLS_FMT_HUMAN)
262 return NULL;
263 if (ce)
264 color = ce->color;
265 if (!color && (!ln || !ln->color) && cl)
266 color = cl->color;
267 return color;
268 }
269
270 /* switch from line color to cell/column color */
271 static void fputs_color_cell_open(struct libscols_table *tb,
272 struct libscols_column *cl,
273 struct libscols_line *ln,
274 struct libscols_cell *ce)
275 {
276 const char *color = get_cell_color(tb, cl, ln, ce);
277
278 if (color)
279 fputs_color(tb, color);
280 }
281
282 /* switch from cell/column color to line color or reset */
283 static void fputs_color_cell_close(struct libscols_table *tb,
284 struct libscols_column *cl,
285 struct libscols_line *ln,
286 struct libscols_cell *ce)
287 {
288 const char *color = get_cell_color(tb, cl, ln, ce);
289
290 if (color)
291 fputs_color(tb, ln ? ln->color : NULL);
292 }
293
294 /* switch to line color */
295 static void fputs_color_line_open(struct libscols_table *tb,
296 struct libscols_line *ln)
297 {
298 if (!tb || !tb->colors_wanted || tb->format != SCOLS_FMT_HUMAN)
299 return;
300 fputs_color(tb, ln ? ln->color : NULL);
301 }
302
303 /* switch off all colors */
304 static void fputs_color_line_close(struct libscols_table *tb)
305 {
306 if (!tb || !tb->colors_wanted || tb->format != SCOLS_FMT_HUMAN)
307 return;
308 fputs_color_reset(tb);
309 }
310
311 /* @buf is the cell data generated by __cursor_to_buffer(). We cannot use
312 * scols_cell_get_data() directly because there may be a defined wrap function
313 * and we need the URI for the segment of the data. For example, when the cell
314 * contains multiple filenames.
315 *
316 * The number of URIs can be enormous (due to the number of lines in the table).
317 * Therefore, the goal is to avoid allocation, and a buffer specific to each
318 * column is used and shared for all lines.
319 */
320 static const char *mk_cell_uri(struct libscols_column *cl,
321 struct libscols_cell *ce,
322 struct ul_buffer *buf)
323 {
324 const char *path;
325
326 /* URI disabled at all */
327 if (ce->no_uri)
328 return NULL;
329
330 /* No column prefix, return cell's URI (or NULL if undefined) */
331 if (!scols_column_get_uri(cl))
332 return scols_cell_get_uri(ce);
333
334 /* Compose URI from column's URI + path. The path is cell's URI or cell data. */
335 path = scols_cell_get_uri(ce);
336
337 if (!path && buf) {
338 /* The buffer may already contain tree data, so we need to skip it. */
339 path = ul_buffer_get_pointer(buf, SCOLS_BUFPTR_TREEEND);
340 if (!path)
341 path = ul_buffer_get_string(buf, NULL, NULL);
342 }
343
344 if (!path)
345 return NULL;
346
347 ul_buffer_reset_data(&cl->uri_buf);
348 ul_buffer_append_string(&cl->uri_buf, cl->uri);
349
350 ul_buffer_append_string(&cl->uri_buf, path);
351
352 return ul_buffer_get_string(&cl->uri_buf, NULL, NULL);
353 }
354
355 /* print padding or ASCII-art instead of data of @cl */
356 static void print_empty_cell(struct libscols_table *tb,
357 struct libscols_column *cl,
358 struct libscols_line *ln, /* optional */
359 struct libscols_cell *ce,
360 size_t bufsz)
361 {
362 size_t len_pad = 0; /* in screen cells as opposed to bytes */
363
364 DBG(COL, ul_debugobj(cl, " printing empty cell"));
365
366 fputs_color_cell_open(tb, cl, ln, ce);
367
368 /* generate tree/group ASCII-art rather than padding
369 */
370 if (ln && scols_column_is_tree(cl)) {
371 struct ul_buffer art = UL_INIT_BUFFER;
372 char *data;
373
374 if (ul_buffer_alloc_data(&art, bufsz) != 0)
375 goto done;
376
377 if (cl->is_groups)
378 groups_ascii_art_to_buffer(tb, ln, &art, 1);
379
380 tree_ascii_art_to_buffer(tb, ln, &art);
381
382 if (!list_empty(&ln->ln_branch))
383 ul_buffer_append_string(&art, vertical_symbol(tb));
384
385 if (scols_table_is_noencoding(tb))
386 data = ul_buffer_get_data(&art, NULL, &len_pad);
387 else
388 data = ul_buffer_get_safe_data(&art, NULL, &len_pad, NULL);
389
390 if (data && len_pad)
391 fputs(data, tb->out);
392 ul_buffer_free_data(&art);
393 }
394
395 done:
396 /* minout -- don't fill */
397 if (scols_table_is_minout(tb) && is_next_columns_empty(tb, cl, ln)) {
398 fputs_color_cell_close(tb, cl, ln, ce);
399 return;
400 }
401
402 /* default -- fill except last column */
403 if (!scols_table_is_maxout(tb) && is_last_column(cl)) {
404 fputs_color_cell_close(tb, cl, ln, ce);
405 return;
406 }
407
408 /* fill rest of cell with space */
409 for(; len_pad < cl->width; ++len_pad)
410 fputs(cellpadding_symbol(tb), tb->out);
411
412 fputs_color_cell_close(tb, cl, ln, ce);
413
414 if (!is_last_column(cl))
415 fputs(colsep(tb), tb->out);
416 }
417
418
419
420 /* Fill the start of a line with padding (or with tree ascii-art).
421 *
422 * This is necessary after a long non-truncated column, as this requires the
423 * next column to be printed on the next line. For example (see 'DDD'):
424 *
425 * aaa bbb ccc ddd eee
426 * AAA BBB CCCCCCC
427 * DDD EEE
428 * ^^^^^^^^^^^^
429 * new line padding
430 */
431 static void print_newline_padding(struct libscols_table *tb,
432 struct libscols_column *cl,
433 struct libscols_line *ln, /* optional */
434 struct libscols_cell *ce,
435 size_t bufsz)
436 {
437 size_t i;
438
439 assert(tb);
440 assert(cl);
441
442 DBG(LINE, ul_debugobj(ln, "printing newline padding"));
443
444 fputs(linesep(tb), tb->out); /* line break */
445 tb->termlines_used++;
446
447 fputs_color_line_open(tb, ln);
448
449 /* fill cells after line break */
450 for (i = 0; i <= (size_t) cl->seqnum; i++)
451 print_empty_cell(tb, scols_table_get_column(tb, i), ln, ce, bufsz);
452
453 fputs_color_line_close(tb);
454 }
455
456 static int print_pending_data(struct libscols_table *tb, struct ul_buffer *buf)
457 {
458 struct libscols_line *ln;
459 struct libscols_column *cl;
460 struct libscols_cell *ce;
461 char *data;
462 const char *uri = NULL;
463 size_t i, width = 0, len = 0, bytes = 0;
464
465 scols_table_get_cursor(tb, &ln, &cl, &ce);
466
467 width = cl->width;
468 if (!width)
469 return -EINVAL;
470
471 DBG(COL, ul_debugobj(cl, "printing pending data"));
472
473 if (scols_column_get_uri(cl) || scols_cell_get_uri(ce))
474 uri = mk_cell_uri(cl, ce, buf);
475
476 if (scols_table_is_noencoding(tb))
477 data = ul_buffer_get_data(buf, &bytes, &len);
478 else
479 data = ul_buffer_get_safe_data(buf, &bytes, &len, scols_column_get_safechars(cl));
480
481 if (!data)
482 return 0;
483
484 /* standard multi-line cell */
485 if (len > width && scols_column_is_wrap(cl)
486 && !scols_column_is_customwrap(cl)) {
487
488 len = width;
489 bytes = mbs_truncate(data, &len);
490
491 if (bytes != (size_t) -1 && bytes > 0)
492 scols_column_move_wrap(cl, mbs_safe_decode_size(data));
493 }
494
495 fputs_color_cell_open(tb, cl, ln, ce);
496
497 if (uri)
498 ul_fputs_hyperlink(uri, data, tb->out);
499 else
500 fputs(data, tb->out);
501
502 /* minout -- don't fill */
503 if (scols_table_is_minout(tb) && is_next_columns_empty(tb, cl, ln)) {
504 fputs_color_cell_close(tb, cl, ln, ce);
505 return 0;
506 }
507
508 /* default -- fill except last column */
509 if (!scols_table_is_maxout(tb) && is_last_column(cl)) {
510 fputs_color_cell_close(tb, cl, ln, ce);
511 return 0;
512 }
513
514 /* fill rest of cell with space */
515 for(i = len; i < width; i++)
516 fputs(cellpadding_symbol(tb), tb->out);
517
518 fputs_color_cell_close(tb, cl, ln, ce);
519
520 if (!is_last_column(cl))
521 fputs(colsep(tb), tb->out);
522
523 return 0;
524 }
525
526 static void print_json_data(struct libscols_table *tb,
527 struct libscols_column *cl,
528 const char *name,
529 char *data)
530 {
531 switch (cl->json_type) {
532 case SCOLS_JSON_STRING:
533 /* name: "aaa" */
534 ul_jsonwrt_value_s(&tb->json, name, data);
535 break;
536 case SCOLS_JSON_NUMBER:
537 case SCOLS_JSON_FLOAT:
538 /* name: 123 */
539 ul_jsonwrt_value_raw(&tb->json, name, data);
540 break;
541 case SCOLS_JSON_BOOLEAN:
542 case SCOLS_JSON_BOOLEAN_OPTIONAL:
543 /* name: true|false|null */
544 if (cl->json_type == SCOLS_JSON_BOOLEAN_OPTIONAL && (!*data || !strcmp(data, "-"))) {
545 ul_jsonwrt_value_null(&tb->json, name);
546 } else {
547 ul_jsonwrt_value_boolean(&tb->json, name,
548 !*data ? 0 :
549 *data == '0' ? 0 :
550 *data == 'N' || *data == 'n' ? 0 : 1);
551 }
552 break;
553 case SCOLS_JSON_ARRAY_STRING:
554 case SCOLS_JSON_ARRAY_NUMBER:
555 {
556 /* name: [ "aaa", "bbb", "ccc" ] */
557 int items = 0;
558
559 if (!scols_column_is_customwrap(cl)) {
560 if (data && *data) {
561 ul_jsonwrt_array_open(&tb->json, name);
562 ul_jsonwrt_value_s(&tb->json, NULL, data);
563 items++;
564 }
565 } else do {
566 if (!data || !*data)
567 continue;
568 if (!items)
569 ul_jsonwrt_array_open(&tb->json, name);
570 if (cl->json_type == SCOLS_JSON_ARRAY_STRING)
571 ul_jsonwrt_value_s(&tb->json, NULL, data);
572 else
573 ul_jsonwrt_value_raw(&tb->json, NULL, data);
574 items++;
575 } while (scols_column_next_wrap(cl, NULL, &data) == 0);
576
577 if (!items)
578 ul_jsonwrt_array_empty(&tb->json, name);
579 else
580 ul_jsonwrt_array_close(&tb->json);
581 break;
582 }
583 }
584 }
585
586 static int print_data(struct libscols_table *tb, struct ul_buffer *buf)
587 {
588 struct libscols_line *ln; /* NULL for header line! */
589 struct libscols_column *cl;
590 struct libscols_cell *ce;
591 size_t len = 0, i, width, bytes;
592 char *data = NULL;
593 const char *name = NULL, *annot = NULL, *uri = NULL;
594 int is_last;
595
596 assert(tb);
597
598 scols_table_get_cursor(tb, &ln, &cl, &ce);
599 assert(cl);
600
601 if (tb->format != SCOLS_FMT_HUMAN) {
602 name = scols_table_is_shellvar(tb) ?
603 scols_column_get_name_as_shellvar(cl) :
604 scols_column_get_name(cl);
605
606 data = ul_buffer_get_data(buf, NULL, NULL);
607 if (!data)
608 data = "";
609 }
610
611 is_last = is_last_column(cl);
612
613 if (ln && is_last && scols_table_is_json(tb) &&
614 scols_table_is_tree(tb) && has_children(ln))
615 /* "children": [] is the real last value */
616 is_last = 0;
617
618 switch (tb->format) {
619 case SCOLS_FMT_RAW:
620 fputs_nonblank(data, tb->out);
621 if (!is_last)
622 fputs(colsep(tb), tb->out);
623 return 0;
624
625 case SCOLS_FMT_EXPORT:
626 fputs(name ?: "", tb->out);
627 fputc('=', tb->out);
628 fputs_quoted(data, tb->out);
629 if (!is_last)
630 fputs(colsep(tb), tb->out);
631 return 0;
632
633 case SCOLS_FMT_JSON:
634 print_json_data(tb, cl, name, data);
635 return 0;
636
637 case SCOLS_FMT_HUMAN:
638 break; /* continue below */
639 }
640
641 if (!ln) {
642 /* column header */
643 annot = scols_column_get_annotation(cl);
644 } else if (scols_column_get_uri(cl) || scols_cell_get_uri(ce)) {
645 /* column data */
646 uri = mk_cell_uri(cl, ce, buf);
647 }
648
649
650 /* Encode. Note that 'len' and 'width' are number of glyphs not bytes.
651 */
652 if (scols_table_is_noencoding(tb))
653 data = ul_buffer_get_data(buf, &bytes, &len);
654 else
655 data = ul_buffer_get_safe_data(buf, &bytes, &len, scols_column_get_safechars(cl));
656
657 if (!data)
658 data = "";
659 width = cl->width;
660
661 if (is_last
662 && len < width
663 && !scols_table_is_maxout(tb)
664 && !scols_column_is_right(cl))
665 width = len;
666
667 /* truncate data */
668 if (len > width && scols_column_is_trunc(cl)) {
669 len = width;
670 bytes = mbs_truncate(data, &len); /* updates 'len' */
671 }
672
673 /* standard multi-line cell */
674 if (len > width && scols_column_is_wrap(cl)
675 && !scols_column_is_customwrap(cl)) {
676
677 len = width;
678 bytes = mbs_truncate(data, &len);
679
680 if (bytes != (size_t) -1 && bytes > 0)
681 scols_column_move_wrap(cl, mbs_safe_decode_size(data));
682 }
683
684 if (bytes == (size_t) -1) {
685 bytes = len = 0;
686 data = NULL;
687 }
688
689 fputs_color_cell_open(tb, cl, ln, ce);
690
691 if (data && *data) {
692 if (scols_column_is_right(cl)) {
693 for (i = len; i < width; i++)
694 fputs(cellpadding_symbol(tb), tb->out);
695 len = width;
696 }
697
698 if (uri) {
699 char *link = data;
700 size_t skip = ul_buffer_get_pointer_length(buf, SCOLS_BUFPTR_TREEEND);
701
702 /* Print hyperlink after tree lines */
703 if (skip) {
704 link = data + skip;
705 for (i = 0; i < skip; i++)
706 fputc(data[i], tb->out);
707 }
708 ul_fputs_hyperlink(uri, link, tb->out);
709
710 } else if (annot && *annot) {
711 ul_fputs_hyperlink(annot, data, tb->out);
712 } else {
713 fputs(data, tb->out);
714 }
715 }
716
717 /* minout -- don't fill */
718 if (scols_table_is_minout(tb) && is_next_columns_empty(tb, cl, ln)) {
719 fputs_color_cell_close(tb, cl, ln, ce);
720 return 0;
721 }
722
723 /* default -- fill except last column */
724 if (!scols_table_is_maxout(tb) && is_last) {
725 fputs_color_cell_close(tb, cl, ln, ce);
726 return 0;
727 }
728
729 /* fill rest of cell with space */
730 for(i = len; i < width; i++)
731 fputs(cellpadding_symbol(tb), tb->out);
732
733 fputs_color_cell_close(tb, cl, ln, ce);
734
735 if (len > width && !scols_column_is_trunc(cl)) {
736 DBG(COL, ul_debugobj(cl, "*** data len=%zu > column width=%zu", len, width));
737 print_newline_padding(tb, cl, ln, ce, ul_buffer_get_bufsiz(buf)); /* next column starts on next line */
738
739 } else if (!is_last)
740 fputs(colsep(tb), tb->out); /* columns separator */
741
742 return 0;
743 }
744
745 /*
746 * Copy current cell data to buffer. The @cal means "calculation" phase.
747 */
748 int __cursor_to_buffer(struct libscols_table *tb,
749 struct ul_buffer *buf,
750 int cal)
751 {
752 const char *data = NULL;
753 size_t datasiz = 0;
754 struct libscols_cell *ce;
755 struct libscols_line *ln;
756 struct libscols_column *cl;
757 int rc = 0;
758
759 assert(tb);
760
761 scols_table_get_cursor(tb, &ln, &cl, &ce);
762
763 assert(ln);
764 assert(cl);
765 assert(buf);
766 assert(cl->seqnum <= tb->ncols);
767
768 ul_buffer_reset_data(buf);
769
770 if (!scols_column_is_tree(cl))
771 goto notree;
772
773 /*
774 * Group stuff
775 */
776 if (!scols_table_is_json(tb) && cl->is_groups)
777 rc = groups_ascii_art_to_buffer(tb, ln, buf, 0);
778
779 /*
780 * Tree stuff
781 */
782 if (!rc && ln->parent && !scols_table_is_json(tb)) {
783 rc = tree_ascii_art_to_buffer(tb, ln->parent, buf);
784
785 if (!rc && is_last_child(ln))
786 rc = ul_buffer_append_string(buf, right_symbol(tb));
787 else if (!rc)
788 rc = ul_buffer_append_string(buf, branch_symbol(tb));
789 }
790
791 if (!rc && (ln->parent || cl->is_groups) && !scols_table_is_json(tb))
792 ul_buffer_save_pointer(buf, SCOLS_BUFPTR_TREEEND);
793 notree:
794 if (!rc && ce) {
795 int do_wrap = scols_column_is_wrap(cl);
796
797 /* Disable multi-line cells for "raw" and "export" formats.
798 * JSON uses data wrapping to generate arrays */
799 if (do_wrap && (tb->format == SCOLS_FMT_RAW ||
800 tb->format == SCOLS_FMT_EXPORT))
801 do_wrap = 0;
802
803 /* Wrapping enabled; append the next chunk if cell data */
804 if (do_wrap) {
805 char *x = NULL;
806
807 rc = cal ? scols_column_greatest_wrap(cl, ce, &x) :
808 scols_column_next_wrap(cl, ce, &x);
809 /* rc: error: <0; nodata: 1; success: 0 */
810 if (rc < 0)
811 goto done;
812 data = x;
813 rc = 0;
814 if (data && *data)
815 datasiz = strlen(data);
816 if (data && datasiz)
817 rc = ul_buffer_append_data(buf, data, datasiz);
818
819 /* Wrapping disabled, but data maintained by custom wrapping
820 * callback. Try to use data as a string, if not possible,
821 * append all chunks separated by \n (backward compatibility).
822 * */
823 } else if (scols_column_is_customwrap(cl)) {
824 size_t len;
825 int i = 0;
826 char *x = NULL;
827
828 data = scols_cell_get_data(ce);
829 datasiz = scols_cell_get_datasiz(ce);
830 len = data ? strnlen(data, datasiz) : 0;
831
832 if (len && len + 1 == datasiz)
833 rc = ul_buffer_append_data(buf, data, datasiz);
834
835 else while (scols_column_next_wrap(cl, ce, &x) == 0) {
836 /* non-string data in cell, use a nextchunk callback */
837 if (!x)
838 continue;
839 datasiz = strlen(x);
840 if (i)
841 rc = ul_buffer_append_data(buf, "\n", 1);
842 if (!rc)
843 rc = ul_buffer_append_data(buf, x, datasiz);
844 i++;
845 }
846
847 /* Wrapping disabled; let's use data as a classic string. */
848 } else {
849 data = scols_cell_get_data(ce);
850 datasiz = scols_cell_get_datasiz(ce);
851
852 if (data && *data && !datasiz)
853 datasiz = strlen(data); /* cell content may be updated */
854
855 if (data && datasiz)
856 rc = ul_buffer_append_data(buf, data, datasiz);
857 }
858 }
859
860 done:
861 /* reset wrapping after greatest chunk calculation */
862 if (cal && scols_column_is_wrap(cl))
863 scols_column_reset_wrap(cl);
864
865 DBG(COL, ul_debugobj(cl, "__cursor_to_buffer rc=%d len=%zu", rc,
866 ul_buffer_get_datasiz(buf)));
867 return rc;
868 }
869
870 /*
871 * Prints data. Data can be printed in more formats (raw, NAME=xxx pairs), and
872 * control and non-printable characters can be encoded in the \x?? encoding.
873 */
874 static int print_line(struct libscols_table *tb,
875 struct libscols_line *ln,
876 struct ul_buffer *buf)
877 {
878 int rc = 0, pending = 0;
879 struct libscols_column *cl;
880 struct libscols_iter itr;
881
882 assert(ln);
883
884 DBG(LINE, ul_debugobj(ln, " printing line"));
885
886 fputs_color_line_open(tb, ln);
887
888 /* regular line */
889 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
890
891 while (rc == 0 && scols_table_next_column(tb, &itr, &cl) == 0) {
892 if (scols_column_is_hidden(cl))
893 continue;
894
895 scols_table_set_cursor(tb, ln, cl, scols_line_get_cell(ln, cl->seqnum));
896 rc = __cursor_to_buffer(tb, buf, 0);
897 if (!rc)
898 rc = print_data(tb, buf);
899 if (!rc) {
900 if (scols_column_has_pending_wrap(cl))
901 pending = 1;
902 else
903 scols_column_reset_wrap(cl);
904 }
905 scols_table_reset_cursor(tb);
906 }
907 fputs_color_line_close(tb);
908
909 /* extra lines of the multi-line cells */
910 while (rc == 0 && pending) {
911 DBG(LINE, ul_debugobj(ln, "printing pending data"));
912 pending = 0;
913 fputs(linesep(tb), tb->out);
914 fputs_color_line_open(tb, ln);
915 tb->termlines_used++;
916
917 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
918
919 while (rc == 0 && scols_table_next_column(tb, &itr, &cl) == 0) {
920 if (scols_column_is_hidden(cl))
921 continue;
922
923 scols_table_set_cursor(tb, ln, cl, scols_line_get_cell(ln, cl->seqnum));
924 if (scols_column_has_pending_wrap(cl)) {
925 rc = __cursor_to_buffer(tb, buf, 0);
926 if (!rc)
927 rc = print_pending_data(tb, buf);
928 if (!rc) {
929 if (scols_column_has_pending_wrap(cl))
930 pending = 1;
931 else
932 scols_column_reset_wrap(cl);
933 }
934 } else
935 print_empty_cell(tb, cl, ln, NULL, ul_buffer_get_bufsiz(buf));
936 scols_table_reset_cursor(tb);
937 }
938 fputs_color_line_close(tb);
939 }
940
941 return 0;
942 }
943
944 int __scols_print_title(struct libscols_table *tb)
945 {
946 int rc;
947 mbs_align_t align;
948 size_t width, len = 0, bufsz, titlesz;
949 char *title = NULL, *buf = NULL;
950
951 assert(tb);
952
953 if (!tb->title.data)
954 return 0;
955
956 DBG(TAB, ul_debugobj(tb, "printing title"));
957
958 /* encode data */
959 if (tb->no_encode) {
960 len = bufsz = strlen(tb->title.data) + 1;
961 buf = strdup(tb->title.data);
962 if (!buf) {
963 rc = -ENOMEM;
964 goto done;
965 }
966 } else {
967 bufsz = mbs_safe_encode_size(strlen(tb->title.data)) + 1;
968 if (bufsz == 1) {
969 DBG(TAB, ul_debugobj(tb, "title is empty string -- ignore"));
970 return 0;
971 }
972 buf = malloc(bufsz);
973 if (!buf) {
974 rc = -ENOMEM;
975 goto done;
976 }
977
978 if (!mbs_safe_encode_to_buffer(tb->title.data, &len, buf, bufsz, NULL) ||
979 !len || len == (size_t) -1) {
980 rc = -EINVAL;
981 goto done;
982 }
983 }
984
985 /* truncate and align */
986 width = tb->is_term ? tb->termwidth : 80;
987 titlesz = width + bufsz;
988
989 title = malloc(titlesz);
990 if (!title) {
991 rc = -EINVAL;
992 goto done;
993 }
994
995 switch (scols_cell_get_alignment(&tb->title)) {
996 case SCOLS_CELL_FL_RIGHT:
997 align = MBS_ALIGN_RIGHT;
998 break;
999 case SCOLS_CELL_FL_CENTER:
1000 align = MBS_ALIGN_CENTER;
1001 break;
1002 case SCOLS_CELL_FL_LEFT:
1003 default:
1004 align = MBS_ALIGN_LEFT;
1005 /*
1006 * Don't print extra blank chars after the title if on left
1007 * (that's same as we use for the last column in the table).
1008 */
1009 if (len < width
1010 && !scols_table_is_maxout(tb)
1011 && isblank(*titlepadding_symbol(tb)))
1012 width = len;
1013 break;
1014
1015 }
1016
1017 /* copy from buf to title and align to width with title_padding */
1018 rc = mbsalign_with_padding(buf, title, titlesz,
1019 &width, align,
1020 0, (int) *titlepadding_symbol(tb));
1021
1022 if (rc == -1) {
1023 rc = -EINVAL;
1024 goto done;
1025 }
1026
1027
1028 if (tb->colors_wanted)
1029 fputs_color(tb, tb->title.color);
1030
1031 fputs(title, tb->out);
1032
1033 if (tb->colors_wanted)
1034 fputs_color_reset(tb);
1035
1036 fputc('\n', tb->out);
1037 rc = 0;
1038 done:
1039 free(buf);
1040 free(title);
1041 DBG(TAB, ul_debugobj(tb, "printing title done [rc=%d]", rc));
1042 return rc;
1043 }
1044
1045 int __scols_print_header(struct libscols_table *tb, struct ul_buffer *buf)
1046 {
1047 int rc = 0;
1048 struct libscols_column *cl;
1049 struct libscols_iter itr;
1050
1051 assert(tb);
1052
1053 if ((tb->header_printed == 1 && tb->header_repeat == 0) ||
1054 scols_table_is_noheadings(tb) ||
1055 scols_table_is_export(tb) ||
1056 scols_table_is_json(tb) ||
1057 list_empty(&tb->tb_lines))
1058 return 0;
1059
1060 DBG(TAB, ul_debugobj(tb, "printing header"));
1061
1062 /* set the width according to the size of the data */
1063 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
1064 while (rc == 0 && scols_table_next_column(tb, &itr, &cl) == 0) {
1065 if (scols_column_is_hidden(cl))
1066 continue;
1067
1068 ul_buffer_reset_data(buf);
1069 if (scols_column_get_uri(cl))
1070 scols_cell_disable_uri(&cl->header, 1);
1071 scols_table_set_cursor(tb, NULL, cl, &cl->header);
1072
1073 if (cl->is_groups
1074 && scols_table_is_tree(tb) && scols_column_is_tree(cl)) {
1075 size_t i;
1076 for (i = 0; i < tb->grpset_size + 1; i++) {
1077 rc = ul_buffer_append_data(buf, " ", 1);
1078 if (rc)
1079 break;
1080 }
1081 }
1082 if (!rc)
1083 rc = ul_buffer_append_string(buf,
1084 scols_table_is_shellvar(tb) ?
1085 scols_column_get_name_as_shellvar(cl) :
1086 scols_column_get_name(cl));
1087 if (!rc)
1088 rc = print_data(tb, buf);
1089 scols_table_reset_cursor(tb);
1090 }
1091
1092 if (rc == 0) {
1093 fputs(linesep(tb), tb->out);
1094 tb->termlines_used++;
1095 }
1096
1097 tb->header_printed = 1;
1098 tb->header_next = tb->termlines_used + tb->termheight;
1099 if (tb->header_repeat)
1100 DBG(TAB, ul_debugobj(tb, "\tnext header: %zu [current=%zu, rc=%d]",
1101 tb->header_next, tb->termlines_used, rc));
1102 return rc;
1103 }
1104
1105
1106 int __scols_print_range(struct libscols_table *tb,
1107 struct ul_buffer *buf,
1108 struct libscols_iter *itr,
1109 struct libscols_line *end)
1110 {
1111 int rc = 0;
1112 struct libscols_line *ln;
1113
1114 assert(tb);
1115 DBG(TAB, ul_debugobj(tb, "printing range"));
1116
1117 while (rc == 0 && scols_table_next_line(tb, itr, &ln) == 0) {
1118
1119 int last = scols_iter_is_last(itr);
1120
1121 if (scols_table_is_json(tb))
1122 ul_jsonwrt_object_open(&tb->json, NULL);
1123
1124 rc = print_line(tb, ln, buf);
1125
1126 if (scols_table_is_json(tb))
1127 ul_jsonwrt_object_close(&tb->json);
1128 else if (last == 0 && tb->no_linesep == 0) {
1129 fputs(linesep(tb), tb->out);
1130 tb->termlines_used++;
1131 }
1132
1133 if (end && ln == end)
1134 break;
1135
1136 if (!last && want_repeat_header(tb))
1137 __scols_print_header(tb, buf);
1138 }
1139
1140 return rc;
1141
1142 }
1143
1144 int __scols_print_table(struct libscols_table *tb, struct ul_buffer *buf)
1145 {
1146 struct libscols_iter itr;
1147
1148 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
1149 return __scols_print_range(tb, buf, &itr, NULL);
1150 }
1151
1152 /* scols_walk_tree() callback to print tree line */
1153 static int print_tree_line(struct libscols_table *tb,
1154 struct libscols_line *ln,
1155 struct libscols_column *cl __attribute__((__unused__)),
1156 void *data)
1157 {
1158 struct ul_buffer *buf = (struct ul_buffer *) data;
1159 int rc;
1160
1161 DBG(LINE, ul_debugobj(ln, " printing tree line"));
1162
1163 if (scols_table_is_json(tb))
1164 ul_jsonwrt_object_open(&tb->json, NULL);
1165
1166 rc = print_line(tb, ln, buf);
1167 if (rc)
1168 return rc;
1169
1170 if (has_children(ln)) {
1171 if (scols_table_is_json(tb))
1172 ul_jsonwrt_array_open(&tb->json, "children");
1173 else {
1174 /* between parent and child is separator */
1175 fputs(linesep(tb), tb->out);
1176 tb->termlines_used++;
1177 }
1178 } else {
1179 int last;
1180
1181 /* terminate all open last children for JSON */
1182 if (scols_table_is_json(tb)) {
1183 do {
1184 last = (is_child(ln) && is_last_child(ln)) ||
1185 (is_tree_root(ln) && is_last_tree_root(tb, ln));
1186
1187 ul_jsonwrt_object_close(&tb->json);
1188 if (last && is_child(ln))
1189 ul_jsonwrt_array_close(&tb->json);
1190 ln = ln->parent;
1191 } while(ln && last);
1192
1193 } else if (tb->no_linesep == 0) {
1194 int last_in_tree = scols_walk_is_last(tb, ln);
1195
1196 if (last_in_tree == 0) {
1197 /* standard output */
1198 fputs(linesep(tb), tb->out);
1199 tb->termlines_used++;
1200 }
1201 }
1202 }
1203
1204 return 0;
1205 }
1206
1207 int __scols_print_tree(struct libscols_table *tb, struct ul_buffer *buf)
1208 {
1209 assert(tb);
1210 DBG(TAB, ul_debugobj(tb, "----printing-tree-----"));
1211
1212 return scols_walk_tree(tb, NULL, print_tree_line, (void *) buf);
1213 }
1214
1215 static size_t strlen_line(struct libscols_line *ln)
1216 {
1217 size_t i, sz = 0;
1218
1219 assert(ln);
1220
1221 for (i = 0; i < ln->ncells; i++) {
1222 struct libscols_cell *ce = scols_line_get_cell(ln, i);
1223 const char *data = ce ? scols_cell_get_data(ce) : NULL;
1224
1225 sz += data ? strlen(data) : 0;
1226 }
1227
1228 return sz;
1229 }
1230
1231 void __scols_cleanup_printing(struct libscols_table *tb, struct ul_buffer *buf)
1232 {
1233 if (!tb)
1234 return;
1235
1236 ul_buffer_free_data(buf);
1237
1238 if (tb->priv_symbols) {
1239 scols_table_set_symbols(tb, NULL);
1240 tb->priv_symbols = 0;
1241 }
1242 }
1243
1244 int __scols_initialize_printing(struct libscols_table *tb, struct ul_buffer *buf)
1245 {
1246 size_t bufsz, extra_bufsz = 0;
1247 struct libscols_line *ln;
1248 struct libscols_iter itr;
1249 int rc;
1250
1251 DBG(TAB, ul_debugobj(tb, "initialize printing"));
1252
1253 if (!tb->symbols) {
1254 rc = scols_table_set_default_symbols(tb);
1255 if (rc)
1256 goto err;
1257 tb->priv_symbols = 1;
1258 } else
1259 tb->priv_symbols = 0;
1260
1261 if (tb->format == SCOLS_FMT_HUMAN)
1262 tb->is_term = tb->termforce == SCOLS_TERMFORCE_NEVER ? 0 :
1263 tb->termforce == SCOLS_TERMFORCE_ALWAYS ? 1 :
1264 isatty(STDOUT_FILENO);
1265
1266 if (tb->is_term) {
1267 size_t width = (size_t) scols_table_get_termwidth(tb);
1268 tb->outwidth = width > tb->termreduce ? width - tb->termreduce : width;
1269 bufsz = tb->outwidth;
1270 } else {
1271 bufsz = BUFSIZ;
1272 tb->outwidth = bufsz;
1273 }
1274
1275 if (!tb->is_term || tb->format != SCOLS_FMT_HUMAN || scols_table_is_tree(tb))
1276 tb->header_repeat = 0;
1277
1278 /*
1279 * Estimate extra space necessary for tree, JSON or another output
1280 * decoration.
1281 */
1282 if (scols_table_is_tree(tb))
1283 extra_bufsz += tb->nlines * strlen(vertical_symbol(tb));
1284
1285 switch (tb->format) {
1286 case SCOLS_FMT_RAW:
1287 extra_bufsz += tb->ncols; /* separator between columns */
1288 break;
1289 case SCOLS_FMT_JSON:
1290 ul_jsonwrt_init(&tb->json, tb->out, 0, tb->json_format);
1291 extra_bufsz += tb->nlines * 3; /* indentation */
1292 FALLTHROUGH;
1293 case SCOLS_FMT_EXPORT:
1294 {
1295 struct libscols_column *cl;
1296
1297 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
1298
1299 while (scols_table_next_column(tb, &itr, &cl) == 0) {
1300 if (scols_column_is_hidden(cl))
1301 continue;
1302
1303 if (scols_column_get_name(cl))
1304 extra_bufsz += strlen(scols_column_get_name(cl)); /* data */
1305 extra_bufsz += 2; /* separators */
1306 }
1307 break;
1308 }
1309 case SCOLS_FMT_HUMAN:
1310 break;
1311 }
1312
1313 /*
1314 * Enlarge buffer if necessary, the buffer should be large enough to
1315 * store line data and tree ascii art (or another decoration).
1316 */
1317 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
1318 while (scols_table_next_line(tb, &itr, &ln) == 0) {
1319 size_t sz;
1320
1321 sz = strlen_line(ln) + extra_bufsz;
1322 if (sz > bufsz)
1323 bufsz = sz;
1324 }
1325
1326 /* pre-allocate space for data */
1327 rc = ul_buffer_alloc_data(buf, bufsz + 1); /* data + space for \0 */
1328 if (rc)
1329 goto err;
1330
1331 /*
1332 * Make sure groups members are in the same orders as the tree
1333 */
1334 if (has_groups(tb) && scols_table_is_tree(tb))
1335 scols_groups_fix_members_order(tb);
1336
1337 if (tb->format == SCOLS_FMT_HUMAN) {
1338 rc = __scols_calculate(tb, buf);
1339 if (rc != 0)
1340 goto err;
1341 }
1342
1343 return 0;
1344 err:
1345 __scols_cleanup_printing(tb, buf);
1346 return rc;
1347 }
1348