2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
5 * Copyright (C) 2017 Karel Zak <kzak@redhat.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
52 #include "closestream.h"
58 #include "libsmartcols.h"
60 #define TABCHAR_CELLS 8
63 COLUMN_MODE_FILLCOLS
= 0,
69 struct column_control
{
70 int mode
; /* COLUMN_MODE_* */
71 size_t termwidth
; /* -1 uninilialized, 0 unlimited, >0 width (default is 80) */
73 struct libscols_table
*tab
;
75 char **tab_colnames
; /* array with column names */
76 const char *tab_name
; /* table name */
77 const char *tab_order
; /* --table-order */
79 char **tab_columns
; /* array from --table-column */
81 const char *tab_colright
; /* --table-right */
82 const char *tab_coltrunc
; /* --table-trunc */
83 const char *tab_colnoextrem
; /* --table-noextreme */
84 const char *tab_colwrap
; /* --table-wrap */
85 const char *tab_colhide
; /* --table-hide */
89 const char *tree_parent
;
91 wchar_t *input_separator
;
92 const char *output_separator
;
94 wchar_t **ents
; /* input entries */
95 size_t nents
; /* number of entries */
96 size_t maxlength
; /* longest input record (line) */
97 size_t maxncols
; /* maximal number of input columns */
98 size_t mincolsep
; /* minimal spaces between columns */
105 keep_empty_lines
, /* --keep-empty-lines */
123 * Count how many characters are non-printable due to ANSI X3.41 escape codes.
125 * It detects and count Fe Escape and OSC 8 links sequences. These sequences contains
126 * characters that normally are printable, but due to being part of a escape sequence
127 * are ignored when displayed in console terminals.
129 static inline size_t ansi_esc_width(ansi_esc_states
*state
, size_t *found
, const wchar_t *str
, int chw
)
133 // ANSI X3.41 escape sequences begin with ESC ( written as \x1b \033 or ^[ )
136 // Ignore 1 byte C1 control codes (0x80–0x9F) due to conflict with UTF-8 and CP-1252
139 // Fe escape sequences allows the range 0x40 to 0x5f
141 case '[': // CSI - Control Sequence Introducer
144 case ']': // OSC - Operating System Command
147 case '_': // APC - Application Program Command
148 case 'P': // DCS - Device Control String
149 case '^': // PM - Privacy Message
160 // Fe escape sequences allows the range 0x30-0x3f
161 // However SGR (Select Graphic Rendition) only uses: 0-9 ';' ':'
162 if (*str
>= '0' && *str
<= '?')
164 // Fe ends with the range 0x40-0x7e but SGR ends with 'm'
165 if (*str
<= '@' && *str
>= '~')
170 if (*str
== ANSI_LNK
) // OSC8-Link
173 *state
= ANSI_END
; // other command sequences are ignored
175 case ANSI_LNK
: // OSC8 Terminal Hiperlink Sequence
177 case 0x7: // Separated by BEL
178 *state
= ANSI_LBL
; //# \e]8;;LINK\aTEXT\e]8;;\a #
180 case 0x1b: // OSC8-Link separated by ESC-BACKSLASH
182 *state
= ANSI_LBL
; //# \e]8;;LINK\e\\TEXT\e]8;;\e\\ #
187 return 0; // ignore link width
189 if (*str
== 0x1b) { // Link label goes until ESC BACKSLASH
196 if (*str
== '[') // SGR FG/BG colors nested inside OSC8-Link sequence
199 *state
= ANSI_END
; //# Link label ends with \e[8;;\e\\ #
201 case ANSI_LSG
: //# \e]8;;LINK\e\\\e[1;34mTEXT\e[0m\e]8;;\e\\ #
203 if (*str
< '0' || *str
> '?') // SGR color sequence ends with 'm'
208 case 0x1b: // APC/OSC8-Links ends with ESC-BACKSLASH
211 case 0x7: // APC/OSC/OSC8-Links ends with BEL
213 case 0x9c: // APC/DCS/DM ends with ST (String Terminator)
227 static size_t width(const wchar_t *str
)
231 ansi_esc_states state
= ANSI_CHR
;
233 for (; *str
!= '\0'; str
++) {
235 int x
= wcwidth(*str
); /* don't use wcswidth(), need to ignore non-printable */
237 int x
= isprint(*str
) ? 1 : 0;
239 int chw
= x
> 0 ? x
: 0;
240 size_t nonpr
= ansi_esc_width(&state
, &found
, str
, chw
);
241 count
+= chw
- nonpr
;
246 static wchar_t *mbs_to_wcs(const char *s
)
252 n
= mbstowcs((wchar_t *)0, s
, 0);
255 wcs
= xcalloc((n
+ 1) * sizeof(wchar_t), 1);
256 n
= mbstowcs(wcs
, s
, n
+ 1);
267 static char *wcs_to_mbs(const wchar_t *s
)
273 n
= wcstombs(NULL
, s
, 0);
274 if (n
== (size_t) -1)
277 str
= xcalloc(n
+ 1, 1);
278 if (wcstombs(str
, s
, n
) == (size_t) -1) {
288 static wchar_t *local_wcstok(struct column_control
const *const ctl
, wchar_t *p
,
291 wchar_t *result
= NULL
;
295 return wcstok(p
, ctl
->input_separator
, state
);
297 return strtok_r(p
, ctl
->input_separator
, state
);
306 p
= wcspbrk(result
, ctl
->input_separator
);
308 p
= strpbrk(result
, ctl
->input_separator
);
319 static char **split_or_error(const char *str
, const char *errmsg
)
321 char **res
= ul_strv_split(str
, ",");
326 errx(EXIT_FAILURE
, "%s: '%s'", errmsg
, str
);
333 static void init_table(struct column_control
*ctl
)
337 ctl
->tab
= scols_new_table();
339 err(EXIT_FAILURE
, _("failed to allocate output table"));
341 scols_table_set_column_separator(ctl
->tab
, ctl
->output_separator
);
343 scols_table_enable_json(ctl
->tab
, 1);
344 scols_table_set_name(ctl
->tab
, ctl
->tab_name
? : "table");
346 scols_table_enable_noencoding(ctl
->tab
, 1);
348 scols_table_enable_maxout(ctl
->tab
, ctl
->maxout
? 1 : 0);
350 if (ctl
->tab_columns
) {
353 UL_STRV_FOREACH(opts
, ctl
->tab_columns
) {
354 struct libscols_column
*cl
;
356 cl
= scols_table_new_column(ctl
->tab
, NULL
, 0, 0);
357 scols_column_set_properties(cl
, *opts
);
360 } else if (ctl
->tab_colnames
) {
363 UL_STRV_FOREACH(name
, ctl
->tab_colnames
)
364 scols_table_new_column(ctl
->tab
, *name
, 0, 0);
366 scols_table_enable_noheadings(ctl
->tab
, 1);
368 if (ctl
->tab_colnames
|| ctl
->tab_columns
) {
369 if (ctl
->header_repeat
)
370 scols_table_enable_header_repeat(ctl
->tab
, 1);
371 scols_table_enable_noheadings(ctl
->tab
, !!ctl
->tab_noheadings
);
376 static struct libscols_column
*get_last_visible_column(struct column_control
*ctl
, int n
)
378 struct libscols_iter
*itr
;
379 struct libscols_column
*cl
, *res
= NULL
;
381 itr
= scols_new_iter(SCOLS_ITER_BACKWARD
);
385 while (scols_table_next_column(ctl
->tab
, itr
, &cl
) == 0) {
386 if (scols_column_get_flags(cl
) & SCOLS_FL_HIDDEN
)
395 scols_free_iter(itr
);
399 static struct libscols_column
*string_to_column(struct column_control
*ctl
, const char *str
)
401 struct libscols_column
*cl
;
403 if (isdigit_string(str
)) {
404 uint32_t n
= strtou32_or_err(str
, _("failed to parse column")) - 1;
406 cl
= scols_table_get_column(ctl
->tab
, n
);
407 } else if (strcmp(str
, "-1") == 0)
408 cl
= get_last_visible_column(ctl
, 0);
410 cl
= scols_table_get_column_by_name(ctl
->tab
, str
);
413 errx(EXIT_FAILURE
, _("undefined column name '%s'"), str
);
418 static int column_set_flag(struct libscols_column
*cl
, int fl
)
420 int cur
= scols_column_get_flags(cl
);
422 return scols_column_set_flags(cl
, cur
| fl
);
425 static int has_unnamed(const char *list
)
432 if (strcmp(list
, "-") == 0)
434 if (!strchr(list
, ','))
437 all
= split_or_error(list
, NULL
);
439 UL_STRV_FOREACH(one
, all
) {
440 if (strcmp(*one
, "-") == 0) {
451 static void apply_columnflag_from_list(struct column_control
*ctl
, const char *list
,
452 int flag
, const char *errmsg
)
457 struct libscols_column
*cl
;
460 if (list
&& strcmp(list
, "0") == 0) {
461 struct libscols_iter
*itr
;
463 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
467 while (scols_table_next_column(ctl
->tab
, itr
, &cl
) == 0)
468 column_set_flag(cl
, flag
);
469 scols_free_iter(itr
);
473 all
= split_or_error(list
, errmsg
);
475 /* apply to columns specified by name */
476 UL_STRV_FOREACH(one
, all
) {
479 if (strcmp(*one
, "-") == 0) {
484 /* parse range (N-M) */
485 if (strchr(*one
, '-') && ul_parse_range(*one
, &low
, &up
, 0) == 0) {
486 for (; low
<= up
; low
++) {
488 cl
= get_last_visible_column(ctl
, (low
* -1) -1);
490 cl
= scols_table_get_column(ctl
->tab
, low
-1);
492 column_set_flag(cl
, flag
);
497 /* one item in the list */
498 cl
= string_to_column(ctl
, *one
);
500 column_set_flag(cl
, flag
);
504 /* apply flag to all columns without name */
506 struct libscols_iter
*itr
;
508 itr
= scols_new_iter(SCOLS_ITER_FORWARD
);
512 while (scols_table_next_column(ctl
->tab
, itr
, &cl
) == 0) {
513 if (!scols_column_get_name(cl
))
514 column_set_flag(cl
, flag
);
516 scols_free_iter(itr
);
520 static void reorder_table(struct column_control
*ctl
)
522 struct libscols_column
**wanted
, *last
= NULL
;
524 size_t ncols
= scols_table_get_ncols(ctl
->tab
);
525 char **order
= split_or_error(ctl
->tab_order
, _("failed to parse --table-order list"));
528 wanted
= xcalloc(ncols
, sizeof(struct libscols_column
*));
530 UL_STRV_FOREACH(one
, order
) {
531 struct libscols_column
*cl
= string_to_column(ctl
, *one
);
533 wanted
[count
++] = cl
;
536 for (i
= 0; i
< count
; i
++) {
537 scols_table_move_column(ctl
->tab
, last
, wanted
[i
]);
545 static void create_tree(struct column_control
*ctl
)
547 struct libscols_column
*cl_tree
= string_to_column(ctl
, ctl
->tree
);
548 struct libscols_column
*cl_p
= string_to_column(ctl
, ctl
->tree_parent
);
549 struct libscols_column
*cl_i
= string_to_column(ctl
, ctl
->tree_id
);
550 struct libscols_iter
*itr_p
, *itr_i
;
551 struct libscols_line
*ln_i
;
553 if (!cl_p
|| !cl_i
|| !cl_tree
)
554 return; /* silently ignore the tree request */
556 column_set_flag(cl_tree
, SCOLS_FL_TREE
);
558 itr_p
= scols_new_iter(SCOLS_ITER_FORWARD
);
559 itr_i
= scols_new_iter(SCOLS_ITER_FORWARD
);
560 if (!itr_p
|| !itr_i
)
563 /* scan all lines for ID */
564 while (scols_table_next_line(ctl
->tab
, itr_i
, &ln_i
) == 0) {
565 struct libscols_line
*ln
;
566 struct libscols_cell
*ce
= scols_line_get_column_cell(ln_i
, cl_i
);
567 const char *id
= ce
? scols_cell_get_data(ce
) : NULL
;
572 /* see if the ID is somewhere used in parent column */
573 scols_reset_iter(itr_p
, SCOLS_ITER_FORWARD
);
574 while (scols_table_next_line(ctl
->tab
, itr_p
, &ln
) == 0) {
577 ce
= scols_line_get_column_cell(ln
, cl_p
);
578 parent
= ce
? scols_cell_get_data(ce
) : NULL
;
582 if (strcmp(id
, parent
) != 0)
584 if (scols_line_is_ancestor(ln
, ln_i
))
586 scols_line_add_child(ln_i
, ln
);
590 scols_free_iter(itr_p
);
591 scols_free_iter(itr_i
);
594 static void modify_table(struct column_control
*ctl
)
596 if (ctl
->termwidth
> 0) {
597 scols_table_set_termwidth(ctl
->tab
, ctl
->termwidth
);
598 scols_table_set_termforce(ctl
->tab
, SCOLS_TERMFORCE_ALWAYS
);
601 if (ctl
->tab_colhide
)
602 apply_columnflag_from_list(ctl
, ctl
->tab_colhide
,
603 SCOLS_FL_HIDDEN
, _("failed to parse --table-hide list"));
605 if (ctl
->tab_colright
)
606 apply_columnflag_from_list(ctl
, ctl
->tab_colright
,
607 SCOLS_FL_RIGHT
, _("failed to parse --table-right list"));
609 if (ctl
->tab_coltrunc
)
610 apply_columnflag_from_list(ctl
, ctl
->tab_coltrunc
,
611 SCOLS_FL_TRUNC
, _("failed to parse --table-trunc list"));
613 if (ctl
->tab_colnoextrem
)
614 apply_columnflag_from_list(ctl
, ctl
->tab_colnoextrem
,
615 SCOLS_FL_NOEXTREMES
, _("failed to parse --table-noextreme list"));
617 if (ctl
->tab_colwrap
)
618 apply_columnflag_from_list(ctl
, ctl
->tab_colwrap
,
619 SCOLS_FL_WRAP
, _("failed to parse --table-wrap list"));
621 if (!ctl
->tab_colnoextrem
) {
622 struct libscols_column
*cl
= get_last_visible_column(ctl
, 0);
624 column_set_flag(cl
, SCOLS_FL_NOEXTREMES
);
630 /* This must be the last step! */
636 static int add_line_to_table(struct column_control
*ctl
, wchar_t *wcs0
)
638 wchar_t *sv
= NULL
, *wcs
= wcs0
, *all
= NULL
;
640 struct libscols_line
*ln
= NULL
;
649 err(EXIT_FAILURE
, _("failed to allocate input line"));
654 wchar_t *wcdata
= local_wcstok(ctl
, wcs
, &sv
);
659 if (ctl
->maxncols
&& n
+ 1 == ctl
->maxncols
) {
660 /* Use rest of the string as column data */
661 size_t skip
= wcdata
- wcs0
;
665 if (scols_table_get_ncols(ctl
->tab
) < n
+ 1) {
666 if (scols_table_is_json(ctl
->tab
) && !ctl
->hide_unnamed
)
667 errx(EXIT_FAILURE
, _("line %zu: for JSON the name of the "
668 "column %zu is required"),
669 scols_table_get_nlines(ctl
->tab
) + 1,
671 scols_table_new_column(ctl
->tab
, NULL
, 0,
672 ctl
->hide_unnamed
? SCOLS_FL_HIDDEN
: 0);
675 ln
= scols_table_new_line(ctl
->tab
, NULL
);
677 err(EXIT_FAILURE
, _("failed to allocate output line"));
680 data
= wcs_to_mbs(wcdata
);
682 err(EXIT_FAILURE
, _("failed to allocate output data"));
683 if (scols_line_refer_data(ln
, n
, data
))
684 err(EXIT_FAILURE
, _("failed to add output data"));
687 if (ctl
->maxncols
&& n
== ctl
->maxncols
)
695 static int add_emptyline_to_table(struct column_control
*ctl
)
700 if (!scols_table_new_line(ctl
->tab
, NULL
))
701 err(EXIT_FAILURE
, _("failed to allocate output line"));
706 static void add_entry(struct column_control
*ctl
, size_t *maxents
, wchar_t *wcs
)
708 if (ctl
->nents
<= *maxents
) {
710 ctl
->ents
= xreallocarray(ctl
->ents
, *maxents
, sizeof(wchar_t *));
712 ctl
->ents
[ctl
->nents
] = wcs
;
716 static int read_input(struct column_control
*ctl
, FILE *fp
)
718 wchar_t *empty
= NULL
;
730 if (getline(&buf
, &bufsz
, fp
) < 0) {
733 err(EXIT_FAILURE
, _("read failed"));
735 str
= (char *) skip_space(buf
);
737 p
= strchr(str
, '\n');
742 if (ctl
->keep_empty_lines
) {
743 if (ctl
->mode
== COLUMN_MODE_TABLE
) {
744 add_emptyline_to_table(ctl
);
747 empty
= mbs_to_wcs("");
748 add_entry(ctl
, &maxents
, empty
);
754 wcs
= mbs_to_wcs(buf
);
757 * Convert broken sequences to \x<hex> and continue.
760 char *tmp
= mbs_invalid_encode(buf
, &tmpsz
);
763 err(EXIT_FAILURE
, _("read failed"));
764 wcs
= mbs_to_wcs(tmp
);
769 case COLUMN_MODE_TABLE
:
770 rc
= add_line_to_table(ctl
, wcs
);
774 case COLUMN_MODE_FILLCOLS
:
775 case COLUMN_MODE_FILLROWS
:
776 add_entry(ctl
, &maxents
, wcs
);
778 if (ctl
->maxlength
< len
)
779 ctl
->maxlength
= len
;
793 static void columnate_fillrows(struct column_control
*ctl
)
795 size_t chcnt
, col
, cnt
, endcol
, numcols
, remains
;
799 ctl
->maxlength
+= ctl
->mincolsep
;
801 ctl
->maxlength
= (ctl
->maxlength
+ TABCHAR_CELLS
) & ~(TABCHAR_CELLS
- 1);
802 numcols
= ctl
->termwidth
/ ctl
->maxlength
;
803 remains
= ctl
->termwidth
% ctl
->maxlength
;
804 if (ctl
->use_spaces
&& remains
+ ctl
->mincolsep
>= ctl
->maxlength
)
806 endcol
= ctl
->maxlength
;
807 for (chcnt
= col
= 0, lp
= ctl
->ents
; /* nothing */; ++lp
) {
812 if (++col
== numcols
) {
814 endcol
= ctl
->maxlength
;
817 if (ctl
->use_spaces
) {
818 while (chcnt
< endcol
) {
823 while ((cnt
= ((chcnt
+ TABCHAR_CELLS
) & ~(TABCHAR_CELLS
- 1))) <= endcol
) {
828 endcol
+= ctl
->maxlength
;
835 static void columnate_fillcols(struct column_control
*ctl
)
837 size_t base
, chcnt
, cnt
, col
, endcol
, numcols
, numrows
, row
, remains
;
840 ctl
->maxlength
+= ctl
->mincolsep
;
842 ctl
->maxlength
= (ctl
->maxlength
+ TABCHAR_CELLS
) & ~(TABCHAR_CELLS
- 1);
843 numcols
= ctl
->termwidth
/ ctl
->maxlength
;
844 remains
= ctl
->termwidth
% ctl
->maxlength
;
847 if (ctl
->use_spaces
&& remains
+ ctl
->mincolsep
>= ctl
->maxlength
)
849 numrows
= ctl
->nents
/ numcols
;
850 if (ctl
->nents
% numcols
)
853 for (row
= 0; row
< numrows
; ++row
) {
854 endcol
= ctl
->maxlength
;
855 for (base
= row
, chcnt
= col
= 0; col
< numcols
; ++col
) {
856 fputws(ctl
->ents
[base
], stdout
);
857 chcnt
+= width(ctl
->ents
[base
]);
858 if ((base
+= numrows
) >= ctl
->nents
)
860 if (ctl
->use_spaces
) {
861 while (chcnt
< endcol
) {
866 while ((cnt
= ((chcnt
+ TABCHAR_CELLS
) & ~(TABCHAR_CELLS
- 1))) <= endcol
) {
871 endcol
+= ctl
->maxlength
;
877 static void simple_print(struct column_control
*ctl
)
882 for (cnt
= ctl
->nents
, lp
= ctl
->ents
; cnt
--; ++lp
) {
888 static void __attribute__((__noreturn__
)) usage(void)
892 fputs(USAGE_HEADER
, out
);
893 fprintf(out
, _(" %s [options] [<file>...]\n"), program_invocation_short_name
);
895 fputs(USAGE_SEPARATOR
, out
);
896 fputs(_("Columnate lists.\n"), out
);
898 fputs(USAGE_OPTIONS
, out
);
899 fputs(_(" -t, --table create a table\n"), out
);
900 fputs(_(" -n, --table-name <name> table name for JSON output\n"), out
);
901 fputs(_(" -O, --table-order <columns> specify order of output columns\n"), out
);
902 fputs(_(" -C, --table-column <properties> define column\n"), out
);
903 fputs(_(" -N, --table-columns <names> comma separated columns names\n"), out
);
904 fputs(_(" -l, --table-columns-limit <num> maximal number of input columns\n"), out
);
905 fputs(_(" -E, --table-noextreme <columns> don't count long text from the columns to column width\n"), out
);
906 fputs(_(" -d, --table-noheadings don't print header\n"), out
);
907 fputs(_(" -m, --table-maxout fill all available space\n"), out
);
908 fputs(_(" -e, --table-header-repeat repeat header for each page\n"), out
);
909 fputs(_(" -H, --table-hide <columns> don't print the columns\n"), out
);
910 fputs(_(" -R, --table-right <columns> right align text in these columns\n"), out
);
911 fputs(_(" -T, --table-truncate <columns> truncate text in the columns when necessary\n"), out
);
912 fputs(_(" -W, --table-wrap <columns> wrap text in the columns when necessary\n"), out
);
913 fputs(_(" -L, --keep-empty-lines don't ignore empty lines\n"), out
);
914 fputs(_(" -J, --json use JSON output format for table\n"), out
);
916 fputs(USAGE_SEPARATOR
, out
);
917 fputs(_(" -r, --tree <column> column to use tree-like output for the table\n"), out
);
918 fputs(_(" -i, --tree-id <column> line ID to specify child-parent relation\n"), out
);
919 fputs(_(" -p, --tree-parent <column> parent to specify child-parent relation\n"), out
);
921 fputs(USAGE_SEPARATOR
, out
);
922 fputs(_(" -c, --output-width <width> width of output in number of characters\n"), out
);
923 fputs(_(" -o, --output-separator <string> columns separator for table output (default is two spaces)\n"), out
);
924 fputs(_(" -s, --separator <string> possible table delimiters\n"), out
);
925 fputs(_(" -x, --fillrows fill rows before columns\n"), out
);
926 fputs(_(" -S, --use-spaces <number> minimal whitespaces between columns (no tabs)\n"), out
);
929 fputs(USAGE_SEPARATOR
, out
);
930 fprintf(out
, USAGE_HELP_OPTIONS(34));
931 fprintf(out
, USAGE_MAN_TAIL("column(1)"));
936 int main(int argc
, char **argv
)
938 struct column_control ctl
= {
939 .mode
= COLUMN_MODE_FILLCOLS
,
941 .termwidth
= (size_t) -1
945 unsigned int eval
= 0; /* exit value */
947 static const struct option longopts
[] =
949 { "columns", required_argument
, NULL
, 'c' }, /* deprecated */
950 { "fillrows", no_argument
, NULL
, 'x' },
951 { "help", no_argument
, NULL
, 'h' },
952 { "json", no_argument
, NULL
, 'J' },
953 { "keep-empty-lines", no_argument
, NULL
, 'L' },
954 { "output-separator", required_argument
, NULL
, 'o' },
955 { "output-width", required_argument
, NULL
, 'c' },
956 { "separator", required_argument
, NULL
, 's' },
957 { "table", no_argument
, NULL
, 't' },
958 { "table-columns", required_argument
, NULL
, 'N' },
959 { "table-column", required_argument
, NULL
, 'C' },
960 { "table-columns-limit", required_argument
, NULL
, 'l' },
961 { "table-hide", required_argument
, NULL
, 'H' },
962 { "table-name", required_argument
, NULL
, 'n' },
963 { "table-maxout", no_argument
, NULL
, 'm' },
964 { "table-noextreme", required_argument
, NULL
, 'E' },
965 { "table-noheadings", no_argument
, NULL
, 'd' },
966 { "table-order", required_argument
, NULL
, 'O' },
967 { "table-right", required_argument
, NULL
, 'R' },
968 { "table-truncate", required_argument
, NULL
, 'T' },
969 { "table-wrap", required_argument
, NULL
, 'W' },
970 { "table-empty-lines", no_argument
, NULL
, 'L' }, /* deprecated */
971 { "table-header-repeat", no_argument
, NULL
, 'e' },
972 { "tree", required_argument
, NULL
, 'r' },
973 { "tree-id", required_argument
, NULL
, 'i' },
974 { "tree-parent", required_argument
, NULL
, 'p' },
975 { "use-spaces", required_argument
, NULL
, 'S' },
976 { "version", no_argument
, NULL
, 'V' },
977 { NULL
, 0, NULL
, 0 },
979 static const ul_excl_t excl
[] = { /* rows and cols in ASCII order */
985 int excl_st
[ARRAY_SIZE(excl
)] = UL_EXCL_STATUS_INIT
;
987 setlocale(LC_ALL
, "");
988 bindtextdomain(PACKAGE
, LOCALEDIR
);
990 close_stdout_atexit();
992 ctl
.output_separator
= " ";
993 ctl
.input_separator
= mbs_to_wcs("\t ");
995 while ((c
= getopt_long(argc
, argv
, "C:c:dE:eH:hi:Jl:LN:n:mO:o:p:R:r:S:s:T:tVW:x", longopts
, NULL
)) != -1) {
997 err_exclusive_options(c
, longopts
, excl
, excl_st
);
1001 if (ul_strv_extend(&ctl
.tab_columns
, optarg
))
1005 if (strcmp(optarg
, "unlimited") == 0)
1008 ctl
.termwidth
= strtou32_or_err(optarg
, _("invalid columns argument"));
1011 ctl
.tab_noheadings
= 1;
1014 ctl
.tab_colnoextrem
= optarg
;
1017 ctl
.header_repeat
= 1;
1020 ctl
.tab_colhide
= optarg
;
1021 ctl
.hide_unnamed
= has_unnamed(ctl
.tab_colhide
);
1024 ctl
.tree_id
= optarg
;
1028 ctl
.mode
= COLUMN_MODE_TABLE
;
1031 ctl
.keep_empty_lines
= 1;
1034 ctl
.maxncols
= strtou32_or_err(optarg
, _("invalid columns limit argument"));
1035 if (ctl
.maxncols
== 0)
1036 errx(EXIT_FAILURE
, _("columns limit must be greater than zero"));
1039 ctl
.tab_colnames
= split_or_error(optarg
, _("failed to parse column names"));
1042 ctl
.tab_name
= optarg
;
1048 ctl
.tab_order
= optarg
;
1051 ctl
.output_separator
= optarg
;
1054 ctl
.tree_parent
= optarg
;
1057 ctl
.tab_colright
= optarg
;
1064 ctl
.mincolsep
= strtou32_or_err(optarg
, _("invalid spaces argument"));
1067 free(ctl
.input_separator
);
1068 ctl
.input_separator
= mbs_to_wcs(optarg
);
1069 if (!ctl
.input_separator
)
1070 err(EXIT_FAILURE
, _("failed to parse input separator"));
1074 ctl
.tab_coltrunc
= optarg
;
1077 ctl
.mode
= COLUMN_MODE_TABLE
;
1080 ctl
.tab_colwrap
= optarg
;
1083 ctl
.mode
= COLUMN_MODE_FILLROWS
;
1089 print_version(EXIT_SUCCESS
);
1091 errtryhelp(EXIT_FAILURE
);
1097 if (ctl
.termwidth
== (size_t) -1)
1098 ctl
.termwidth
= get_terminal_width(80);
1101 ctl
.mode
= COLUMN_MODE_TABLE
;
1102 if (!ctl
.tree_parent
|| !ctl
.tree_id
)
1103 errx(EXIT_FAILURE
, _("options --tree-id and --tree-parent are "
1104 "required for tree formatting"));
1107 if (ctl
.mode
!= COLUMN_MODE_TABLE
1108 && (ctl
.tab_order
|| ctl
.tab_name
|| ctl
.tab_colwrap
||
1109 ctl
.tab_colhide
|| ctl
.tab_coltrunc
|| ctl
.tab_colnoextrem
||
1110 ctl
.tab_colright
|| ctl
.tab_colnames
|| ctl
.tab_columns
))
1111 errx(EXIT_FAILURE
, _("option --table required for all --table-*"));
1113 if (!ctl
.tab_colnames
&& !ctl
.tab_columns
&& ctl
.json
)
1114 errx(EXIT_FAILURE
, _("option --table-columns or --table-column required for --json"));
1117 eval
+= read_input(&ctl
, stdin
);
1119 for (; *argv
; ++argv
) {
1122 if ((fp
= fopen(*argv
, "r")) != NULL
) {
1123 eval
+= read_input(&ctl
, fp
);
1127 eval
+= EXIT_FAILURE
;
1131 if (ctl
.mode
!= COLUMN_MODE_TABLE
) {
1134 if (ctl
.maxlength
>= ctl
.termwidth
)
1135 ctl
.mode
= COLUMN_MODE_SIMPLE
;
1139 case COLUMN_MODE_TABLE
:
1140 if (ctl
.tab
&& scols_table_get_nlines(ctl
.tab
)) {
1142 eval
= scols_print_table(ctl
.tab
);
1144 scols_unref_table(ctl
.tab
);
1145 if (ctl
.tab_colnames
)
1146 ul_strv_free(ctl
.tab_colnames
);
1147 if (ctl
.tab_columns
)
1148 ul_strv_free(ctl
.tab_columns
);
1151 case COLUMN_MODE_FILLCOLS
:
1152 columnate_fillcols(&ctl
);
1154 case COLUMN_MODE_FILLROWS
:
1155 columnate_fillrows(&ctl
);
1157 case COLUMN_MODE_SIMPLE
:
1162 free(ctl
.input_separator
);
1164 return eval
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;