]> git.ipfire.org Git - thirdparty/util-linux.git/blob - text-utils/column.c
ab700ab0134f6a0d38041750b1bb75fa215d470d
[thirdparty/util-linux.git] / text-utils / column.c
1 /*
2 * Copyright (c) 1989, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Copyright (C) 2017 Karel Zak <kzak@redhat.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
22 *
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
33 * SUCH DAMAGE.
34 */
35 #include <sys/types.h>
36 #include <sys/ioctl.h>
37
38 #include <ctype.h>
39 #include <stdio.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <getopt.h>
45
46 #include "nls.h"
47 #include "c.h"
48 #include "widechar.h"
49 #include "xalloc.h"
50 #include "strutils.h"
51 #include "closestream.h"
52 #include "ttyutils.h"
53 #include "strv.h"
54 #include "optutils.h"
55
56 #include "libsmartcols.h"
57
58 #define TABCHAR_CELLS 8
59
60 enum {
61 COLUMN_MODE_FILLCOLS = 0,
62 COLUMN_MODE_FILLROWS,
63 COLUMN_MODE_TABLE,
64 COLUMN_MODE_SIMPLE
65 };
66
67 struct column_control {
68 int mode; /* COLUMN_MODE_* */
69 size_t termwidth;
70
71 struct libscols_table *tab;
72
73 char **tab_colnames; /* array with column names */
74 const char *tab_name; /* table name */
75 const char *tab_colright; /* non-parsed --table-right */
76
77 wchar_t *input_separator;
78 const char *output_separator;
79
80 wchar_t **ents; /* input entries */
81 size_t nents; /* number of entries */
82 size_t maxlength; /* longest input record (line) */
83
84 unsigned int greedy :1,
85 json :1;
86 };
87
88 static size_t width(const wchar_t *str)
89 {
90 size_t width = 0;
91
92 for (; *str != '\0'; str++) {
93 #ifdef HAVE_WIDECHAR
94 int x = wcwidth(*str); /* don't use wcswidth(), need to ignore non-printable */
95 if (x > 0)
96 width += x;
97 #else
98 if (isprint(*str))
99 width++;
100 #endif
101 }
102 return width;
103 }
104
105 static wchar_t *mbs_to_wcs(const char *s)
106 {
107 #ifdef HAVE_WIDECHAR
108 ssize_t n;
109 wchar_t *wcs;
110
111 n = mbstowcs((wchar_t *)0, s, 0);
112 if (n < 0)
113 return NULL;
114 wcs = xcalloc((n + 1) * sizeof(wchar_t), 1);
115 n = mbstowcs(wcs, s, n + 1);
116 if (n < 0) {
117 free(wcs);
118 return NULL;
119 }
120 return wcs;
121 #else
122 return xstrdup(s)
123 #endif
124 }
125
126 static char *wcs_to_mbs(const wchar_t *s)
127 {
128 #ifdef HAVE_WIDECHAR
129 size_t n;
130 char *str;
131
132 n = wcstombs(NULL, s, 0);
133 if (n == (size_t) -1)
134 return NULL;
135
136 str = xcalloc(n + 1, 1);
137 if (wcstombs(str, s, n) == (size_t) -1) {
138 free(str);
139 return NULL;
140 }
141 return str;
142 #else
143 return xstrdup(s)
144 #endif
145 }
146
147 static wchar_t *local_wcstok(wchar_t *p, const wchar_t *separator, int greedy, wchar_t **state)
148 {
149 wchar_t *result = NULL;
150
151 if (greedy)
152 return wcstok(p, separator, state);
153 if (!p) {
154 if (!*state || !**state)
155 return NULL;
156 p = *state;
157 }
158 result = p;
159 p = wcspbrk(result, separator);
160 if (!p)
161 *state = NULL;
162 else {
163 *p = '\0';
164 *state = p + 1;
165 }
166 return result;
167 }
168
169 static char **split_or_error(const char *str, const char *errmsg)
170 {
171 char **res = strv_split(str, ",");
172 if (!res) {
173 if (errno == ENOMEM)
174 err_oom();
175 errx(EXIT_FAILURE, "%s: '%s'", errmsg, str);
176 }
177 return res;
178 }
179
180 static void init_table(struct column_control *ctl)
181 {
182 scols_init_debug(0);
183
184 ctl->tab = scols_new_table();
185 if (!ctl->tab)
186 err(EXIT_FAILURE, _("failed to allocate output table"));
187
188 scols_table_set_column_separator(ctl->tab, ctl->output_separator);
189 if (ctl->json) {
190 scols_table_enable_json(ctl->tab, 1);
191 scols_table_set_name(ctl->tab, ctl->tab_name ? : "table");
192 }
193 if (ctl->tab_colnames) {
194 char **name;
195
196 STRV_FOREACH(name, ctl->tab_colnames)
197 scols_table_new_column(ctl->tab, *name, 0, 0);
198 } else
199 scols_table_enable_noheadings(ctl->tab, 1);
200 }
201
202 static struct libscols_column *string_to_column(struct column_control *ctl, const char *str)
203 {
204 uint32_t colnum = 0;
205
206 if (isdigit_string(str))
207 colnum = strtou32_or_err(str, _("failed to parse column")) - 1;
208 else {
209 char **name;
210
211 STRV_FOREACH(name, ctl->tab_colnames) {
212 if (strcasecmp(*name, str) == 0)
213 break;
214 colnum++;
215 }
216 if (!name || !*name)
217 errx(EXIT_FAILURE, _("undefined column name '%s'"), str);
218 }
219
220 return scols_table_get_column(ctl->tab, colnum);
221 }
222
223 static int column_set_flag(struct libscols_column *cl, int fl)
224 {
225 int cur = scols_column_get_flags(cl);
226
227 return scols_column_set_flags(cl, cur | fl);
228 }
229
230 static void modify_table(struct column_control *ctl)
231 {
232 /* align text in columns to right */
233 if (ctl->tab_colright) {
234 char **allright = split_or_error(ctl->tab_colright, _("failed to parse --table-colright list"));
235 char **right;
236
237 STRV_FOREACH(right, allright) {
238 struct libscols_column *cl = string_to_column(ctl, *right);
239 if (cl)
240 column_set_flag(cl, SCOLS_FL_RIGHT);
241 }
242 strv_free(allright);
243 }
244 }
245
246 static int add_line_to_table(struct column_control *ctl, wchar_t *wcs)
247 {
248 wchar_t *wcdata, *sv = NULL;
249 size_t n = 0;
250 struct libscols_line *ln = NULL;
251
252 if (!ctl->tab)
253 init_table(ctl);
254
255 while ((wcdata = local_wcstok(wcs, ctl->input_separator, ctl->greedy, &sv))) {
256 char *data;
257
258 if (scols_table_get_ncols(ctl->tab) < n + 1)
259 scols_table_new_column(ctl->tab, NULL, 0, 0);
260 if (!ln) {
261 ln = scols_table_new_line(ctl->tab, NULL);
262 if (!ln)
263 err(EXIT_FAILURE, _("failed to allocate output line"));
264 }
265 data = wcs_to_mbs(wcdata);
266 if (!data)
267 err(EXIT_FAILURE, _("failed to allocate output data"));
268 if (scols_line_refer_data(ln, n, data))
269 err(EXIT_FAILURE, _("failed to add output data"));
270 n++;
271 wcs = NULL;
272 }
273
274 return 0;
275 }
276
277 static int read_input(struct column_control *ctl, FILE *fp)
278 {
279 char *buf = NULL;
280 size_t bufsz = 0;
281 size_t maxents = 0;
282 int rc = 0;
283
284 do {
285 char *str, *p;
286 wchar_t *wcs = NULL;
287 size_t len;
288
289 if (getline(&buf, &bufsz, fp) < 0) {
290 if (feof(fp))
291 break;
292 err(EXIT_FAILURE, _("read failed"));
293 }
294 str = (char *) skip_space(buf);
295 if (str) {
296 p = strchr(str, '\n');
297 if (p)
298 *p = '\0';
299 }
300 if (!str || !*str)
301 continue;
302
303 wcs = mbs_to_wcs(str);
304 if (!wcs)
305 err(EXIT_FAILURE, _("read failed"));
306
307 switch (ctl->mode) {
308 case COLUMN_MODE_TABLE:
309 rc = add_line_to_table(ctl, wcs);
310 free(wcs);
311 break;
312
313 case COLUMN_MODE_FILLCOLS:
314 case COLUMN_MODE_FILLROWS:
315 if (ctl->nents <= maxents) {
316 maxents += 1000;
317 ctl->ents = xrealloc(ctl->ents,
318 maxents * sizeof(wchar_t *));
319 }
320 ctl->ents[ctl->nents] = wcs;
321 len = width(ctl->ents[ctl->nents]);
322 if (ctl->maxlength < len)
323 ctl->maxlength = len;
324 ctl->nents++;
325 break;
326 }
327 } while (rc == 0);
328
329 return rc;
330 }
331
332
333 static void columnate_fillrows(struct column_control *ctl)
334 {
335 size_t chcnt, col, cnt, endcol, numcols;
336 wchar_t **lp;
337
338 ctl->maxlength = (ctl->maxlength + TABCHAR_CELLS) & ~(TABCHAR_CELLS - 1);
339 numcols = ctl->termwidth / ctl->maxlength;
340 endcol = ctl->maxlength;
341 for (chcnt = col = 0, lp = ctl->ents;; ++lp) {
342 fputws(*lp, stdout);
343 chcnt += width(*lp);
344 if (!--ctl->nents)
345 break;
346 if (++col == numcols) {
347 chcnt = col = 0;
348 endcol = ctl->maxlength;
349 putwchar('\n');
350 } else {
351 while ((cnt = ((chcnt + TABCHAR_CELLS) & ~(TABCHAR_CELLS - 1))) <= endcol) {
352 putwchar('\t');
353 chcnt = cnt;
354 }
355 endcol += ctl->maxlength;
356 }
357 }
358 if (chcnt)
359 putwchar('\n');
360 }
361
362 static void columnate_fillcols(struct column_control *ctl)
363 {
364 size_t base, chcnt, cnt, col, endcol, numcols, numrows, row;
365
366 ctl->maxlength = (ctl->maxlength + TABCHAR_CELLS) & ~(TABCHAR_CELLS - 1);
367 numcols = ctl->termwidth / ctl->maxlength;
368 if (!numcols)
369 numcols = 1;
370 numrows = ctl->nents / numcols;
371 if (ctl->nents % numcols)
372 ++numrows;
373
374 for (row = 0; row < numrows; ++row) {
375 endcol = ctl->maxlength;
376 for (base = row, chcnt = col = 0; col < numcols; ++col) {
377 fputws(ctl->ents[base], stdout);
378 chcnt += width(ctl->ents[base]);
379 if ((base += numrows) >= ctl->nents)
380 break;
381 while ((cnt = ((chcnt + TABCHAR_CELLS) & ~(TABCHAR_CELLS - 1))) <= endcol) {
382 putwchar('\t');
383 chcnt = cnt;
384 }
385 endcol += ctl->maxlength;
386 }
387 putwchar('\n');
388 }
389 }
390
391 static void simple_print(struct column_control *ctl)
392 {
393 int cnt;
394 wchar_t **lp;
395
396 for (cnt = ctl->nents, lp = ctl->ents; cnt--; ++lp) {
397 fputws(*lp, stdout);
398 putwchar('\n');
399 }
400 }
401
402 static void __attribute__((__noreturn__)) usage(int rc)
403 {
404 FILE *out = rc == EXIT_FAILURE ? stderr : stdout;
405
406 fputs(USAGE_HEADER, out);
407 fprintf(out, _(" %s [options] [<file>...]\n"), program_invocation_short_name);
408
409 fputs(USAGE_SEPARATOR, out);
410 fputs(_("Columnate lists.\n"), out);
411
412 fputs(USAGE_OPTIONS, out);
413 fputs(_(" -J, --json use JSON output format for table\n"), out);
414 fputs(_(" -t, --table create a table\n"), out);
415 fputs(_(" -N, --table-colnames <names> comma separated columns names\n"), out);
416 fputs(_(" -R, --table-colright <columns> right align text in these columns\n"), out);
417 fputs(_(" -n, --table-name <name> table name for JSON output\n"), out);
418 fputs(_(" -s, --separator <string> possible table delimiters\n"), out);
419 fputs(_(" -o, --output-separator <string> columns separator for table output\n"
420 " (default is two spaces)\n"), out);
421 fputs(_(" -c, --output-width <width> width of output in number of characters\n"), out);
422 fputs(_(" -x, --fillrows fill rows before columns\n"), out);
423 fputs(USAGE_SEPARATOR, out);
424 fputs(USAGE_HELP, out);
425 fputs(USAGE_VERSION, out);
426 fprintf(out, USAGE_MAN_TAIL("column(1)"));
427
428 exit(rc);
429 }
430
431 int main(int argc, char **argv)
432 {
433 struct column_control ctl = {
434 .mode = COLUMN_MODE_FILLCOLS,
435 .termwidth = 80,
436 .greedy = 1
437 };
438
439 int c;
440 unsigned int eval = 0; /* exit value */
441
442 static const struct option longopts[] =
443 {
444 { "columns", required_argument, NULL, 'c' }, /* deprecated */
445 { "json", no_argument, NULL, 'J' },
446 { "fillrows", no_argument, NULL, 'x' },
447 { "help", no_argument, NULL, 'h' },
448 { "output-separator", required_argument, NULL, 'o' },
449 { "output-width", required_argument, NULL, 'c' },
450 { "separator", required_argument, NULL, 's' },
451 { "table", no_argument, NULL, 't' },
452 { "table-colnames", required_argument, NULL, 'N' },
453 { "table-colright", required_argument, NULL, 'R' },
454 { "table-name", required_argument, NULL, 'n' },
455 { "version", no_argument, NULL, 'V' },
456 { NULL, 0, NULL, 0 },
457 };
458 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
459 { 'J','x' },
460 { 't','x' },
461 { 0 }
462 };
463 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
464
465 setlocale(LC_ALL, "");
466 bindtextdomain(PACKAGE, LOCALEDIR);
467 textdomain(PACKAGE);
468 atexit(close_stdout);
469
470 ctl.termwidth = get_terminal_width(80);
471 ctl.output_separator = " ";
472 ctl.input_separator = mbs_to_wcs("\t ");
473
474 while ((c = getopt_long(argc, argv, "hVc:Jn:N:R:s:txo:", longopts, NULL)) != -1) {
475
476 err_exclusive_options(c, longopts, excl, excl_st);
477
478 switch(c) {
479 case 'J':
480 ctl.json = 1;
481 ctl.mode = COLUMN_MODE_TABLE;
482 break;
483 case 'n':
484 ctl.tab_name = optarg;
485 break;
486 case 'N':
487 ctl.tab_colnames = split_or_error(optarg, _("failed to parse column names"));
488 break;
489 case 'h':
490 usage(EXIT_SUCCESS);
491 break;
492 case 'V':
493 printf(UTIL_LINUX_VERSION);
494 return EXIT_SUCCESS;
495 case 'c':
496 ctl.termwidth = strtou32_or_err(optarg, _("invalid columns argument"));
497 break;
498 case 'R':
499 ctl.tab_colright = optarg;
500 break;
501 case 's':
502 free(ctl.input_separator);
503 ctl.input_separator = mbs_to_wcs(optarg);
504 ctl.greedy = 0;
505 break;
506 case 'o':
507 ctl.output_separator = optarg;
508 break;
509 case 't':
510 ctl.mode = COLUMN_MODE_TABLE;
511 break;
512 case 'x':
513 ctl.mode = COLUMN_MODE_FILLROWS;
514 break;
515 default:
516 errtryhelp(EXIT_FAILURE);
517 }
518 }
519 argc -= optind;
520 argv += optind;
521
522 if (ctl.tab_colnames == NULL && ctl.json)
523 errx(EXIT_FAILURE, _("option --table-colnames required for --json"));
524
525 if (!*argv)
526 eval += read_input(&ctl, stdin);
527 else
528 for (; *argv; ++argv) {
529 FILE *fp;
530
531 if ((fp = fopen(*argv, "r")) != NULL) {
532 eval += read_input(&ctl, fp);
533 fclose(fp);
534 } else {
535 warn("%s", *argv);
536 eval += EXIT_FAILURE;
537 }
538 }
539
540 if (ctl.mode != COLUMN_MODE_TABLE) {
541 if (!ctl.nents)
542 exit(eval);
543 if (ctl.maxlength >= ctl.termwidth)
544 ctl.mode = COLUMN_MODE_SIMPLE;
545 }
546
547 switch (ctl.mode) {
548 case COLUMN_MODE_TABLE:
549 modify_table(&ctl);
550 eval = scols_print_table(ctl.tab);
551 break;
552 case COLUMN_MODE_FILLCOLS:
553 columnate_fillcols(&ctl);
554 break;
555 case COLUMN_MODE_FILLROWS:
556 columnate_fillrows(&ctl);
557 break;
558 case COLUMN_MODE_SIMPLE:
559 simple_print(&ctl);
560 break;
561 }
562
563 return eval == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
564 }