(root)/
util-linux-2.39/
libsmartcols/
samples/
fromfile.c
       1  /*
       2   * Copyright (C) 2016 Karel Zak <kzak@redhat.com>
       3   *
       4   * This file may be redistributed under the terms of the
       5   * GNU Lesser General Public License.
       6   */
       7  #include <stdlib.h>
       8  #include <unistd.h>
       9  #include <string.h>
      10  #include <errno.h>
      11  #include <sys/types.h>
      12  #include <sys/stat.h>
      13  #include <dirent.h>
      14  #include <getopt.h>
      15  
      16  #include "c.h"
      17  #include "nls.h"
      18  #include "strutils.h"
      19  #include "xalloc.h"
      20  #include "optutils.h"
      21  
      22  #include "libsmartcols.h"
      23  
      24  struct column_flag {
      25  	const char *name;
      26  	int mask;
      27  };
      28  
      29  static const struct column_flag flags[] = {
      30  	{ "trunc",	SCOLS_FL_TRUNC },
      31  	{ "tree",	SCOLS_FL_TREE },
      32  	{ "right",	SCOLS_FL_RIGHT },
      33  	{ "strictwidth",SCOLS_FL_STRICTWIDTH },
      34  	{ "noextremes", SCOLS_FL_NOEXTREMES },
      35  	{ "hidden",	SCOLS_FL_HIDDEN },
      36  	{ "wrap",	SCOLS_FL_WRAP },
      37  	{ "wrapnl",	SCOLS_FL_WRAP },
      38  	{ "none",	0 }
      39  };
      40  
      41  static long name_to_flag(const char *name, size_t namesz)
      42  {
      43  	size_t i;
      44  
      45  	for (i = 0; i < ARRAY_SIZE(flags); i++) {
      46  		const char *cn = flags[i].name;
      47  
      48  		if (!strncasecmp(name, cn, namesz) && !*(cn + namesz))
      49  			return flags[i].mask;
      50  	}
      51  	warnx("unknown flag: %s", name);
      52  	return -1;
      53  }
      54  
      55  static int parse_column_flags(char *str)
      56  {
      57  	unsigned long num_flags = 0;
      58  
      59  	if (string_to_bitmask(str, &num_flags, name_to_flag))
      60  		err(EXIT_FAILURE, "failed to parse column flags");
      61  
      62  	return num_flags;
      63  }
      64  
      65  static struct libscols_column *parse_column(FILE *f)
      66  {
      67  	size_t len = 0;
      68  	char *line = NULL;
      69  	int nlines = 0;
      70  
      71  	struct libscols_column *cl = NULL;
      72  
      73  	while (getline(&line, &len, f) != -1) {
      74  
      75  		char *p = strrchr(line, '\n');
      76  		if (p)
      77  			*p = '\0';
      78  
      79  		switch (nlines) {
      80  		case 0: /* NAME */
      81  			cl = scols_new_column();
      82  			if (!cl)
      83  				goto fail;
      84  			if (scols_column_set_name(cl, line) != 0)
      85  				goto fail;
      86  			break;
      87  
      88  		case 1: /* WIDTH-HINT */
      89  		{
      90  			double whint = strtod_or_err(line, "failed to parse column whint");
      91  			if (scols_column_set_whint(cl, whint))
      92  				goto fail;
      93  			break;
      94  		}
      95  		case 2: /* FLAGS */
      96  		{
      97  			int num_flags = parse_column_flags(line);
      98  			if (scols_column_set_flags(cl, num_flags))
      99  				goto fail;
     100  			if (strcmp(line, "wrapnl") == 0) {
     101  				scols_column_set_wrapfunc(cl,
     102  						scols_wrapnl_chunksize,
     103  						scols_wrapnl_nextchunk,
     104  						NULL);
     105  				scols_column_set_safechars(cl, "\n");
     106  			}
     107  			break;
     108  		}
     109  		case 3: /* COLOR */
     110  			if (scols_column_set_color(cl, line))
     111  				goto fail;
     112  			break;
     113  		default:
     114  			break;
     115  		}
     116  
     117  		nlines++;
     118  	}
     119  
     120  	free(line);
     121  	return cl;
     122  fail:
     123  	free(line);
     124  	scols_unref_column(cl);
     125  	return NULL;
     126  }
     127  
     128  static int parse_column_data(FILE *f, struct libscols_table *tb, int col)
     129  {
     130  	size_t len = 0, nlines = 0;
     131  	int i;
     132  	char *str = NULL;
     133  
     134  	while ((i = getline(&str, &len, f)) != -1) {
     135  
     136  		struct libscols_line *ln;
     137  		char *p = strrchr(str, '\n');
     138  		if (p)
     139  			*p = '\0';
     140  
     141  		while ((p = strrchr(str, '\\')) && *(p + 1) == 'n') {
     142  			*p = '\n';
     143  			memmove(p + 1, p + 2, i - (p + 2 - str));
     144  		}
     145  
     146  		ln = scols_table_get_line(tb, nlines++);
     147  		if (!ln)
     148  			break;
     149  
     150  		if (*str && scols_line_set_data(ln, col, str) != 0)
     151  			err(EXIT_FAILURE, "failed to add output data");
     152  	}
     153  
     154  	free(str);
     155  	return 0;
     156  
     157  }
     158  
     159  static struct libscols_line *get_line_with_id(struct libscols_table *tb,
     160  						int col_id, const char *id)
     161  {
     162  	struct libscols_line *ln;
     163  	struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
     164  
     165  	while (scols_table_next_line(tb, itr, &ln) == 0) {
     166  		struct libscols_cell *ce = scols_line_get_cell(ln, col_id);
     167  		const char *data = ce ? scols_cell_get_data(ce) : NULL;
     168  
     169  		if (data && strcmp(data, id) == 0)
     170  			break;
     171  	}
     172  
     173  	scols_free_iter(itr);
     174  	return ln;
     175  }
     176  
     177  static void compose_tree(struct libscols_table *tb, int parent_col, int id_col)
     178  {
     179  	struct libscols_line *ln;
     180  	struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
     181  
     182  	while (scols_table_next_line(tb, itr, &ln) == 0) {
     183  		struct libscols_line *parent = NULL;
     184  		struct libscols_cell *ce = scols_line_get_cell(ln, parent_col);
     185  		const char *data = ce ? scols_cell_get_data(ce) : NULL;
     186  
     187  		if (data)
     188  			parent = get_line_with_id(tb, id_col, data);
     189  		if (parent)
     190  			scols_line_add_child(parent, ln);
     191  	}
     192  
     193  	scols_free_iter(itr);
     194  }
     195  
     196  
     197  static void __attribute__((__noreturn__)) usage(void)
     198  {
     199  	FILE *out = stdout;
     200  	fprintf(out,
     201  		"\n %s [options] <column-data-file> ...\n\n", program_invocation_short_name);
     202  
     203  	fputs(" -m, --maxout                   fill all terminal width\n", out);
     204  	fputs(" -M, --minout                   minimize tailing padding\n", out);
     205  	fputs(" -c, --column <file>            column definition\n", out);
     206  	fputs(" -n, --nlines <num>             number of lines\n", out);
     207  	fputs(" -J, --json                     JSON output format\n", out);
     208  	fputs(" -r, --raw                      RAW output format\n", out);
     209  	fputs(" -E, --export                   use key=\"value\" output format\n", out);
     210  	fputs(" -C, --colsep <str>             set columns separator\n", out);
     211  	fputs(" -w, --width <num>              hardcode terminal width\n", out);
     212  	fputs(" -p, --tree-parent-column <n>   parent column\n", out);
     213  	fputs(" -i, --tree-id-column <n>       id column\n", out);
     214  	fputs(" -h, --help                     this help\n", out);
     215  	fputs("\n", out);
     216  
     217  	exit(EXIT_SUCCESS);
     218  }
     219  
     220  int main(int argc, char *argv[])
     221  {
     222  	struct libscols_table *tb;
     223  	int c, n, nlines = 0;
     224  	int parent_col = -1, id_col = -1;
     225  
     226  	static const struct option longopts[] = {
     227  		{ "maxout", 0, NULL, 'm' },
     228  		{ "minout", 0, NULL, 'M' },
     229  		{ "column", 1, NULL, 'c' },
     230  		{ "nlines", 1, NULL, 'n' },
     231  		{ "width",  1, NULL, 'w' },
     232  		{ "tree-parent-column", 1, NULL, 'p' },
     233  		{ "tree-id-column",	1, NULL, 'i' },
     234  		{ "json",   0, NULL, 'J' },
     235  		{ "raw",    0, NULL, 'r' },
     236  		{ "export", 0, NULL, 'E' },
     237  		{ "colsep",  1, NULL, 'C' },
     238  		{ "help",   0, NULL, 'h' },
     239  		{ NULL, 0, NULL, 0 },
     240  	};
     241  
     242  	static const ul_excl_t excl[] = {       /* rows and cols in ASCII order */
     243  		{ 'E', 'J', 'r' },
     244  		{ 'M', 'm' },
     245  		{ 0 }
     246  	};
     247  	int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
     248  
     249  	setlocale(LC_ALL, "");	/* just to have enable UTF8 chars */
     250  	scols_init_debug(0);
     251  
     252  	tb = scols_new_table();
     253  	if (!tb)
     254  		err(EXIT_FAILURE, "failed to create output table");
     255  
     256  	while((c = getopt_long(argc, argv, "hCc:Ei:JMmn:p:rw:", longopts, NULL)) != -1) {
     257  
     258  		err_exclusive_options(c, longopts, excl, excl_st);
     259  
     260  		switch(c) {
     261  		case 'c': /* add column from file */
     262  		{
     263  			struct libscols_column *cl;
     264  			FILE *f = fopen(optarg, "r");
     265  
     266  			if (!f)
     267  				err(EXIT_FAILURE, "%s: open failed", optarg);
     268  			cl = parse_column(f);
     269  			if (cl && scols_table_add_column(tb, cl))
     270  				err(EXIT_FAILURE, "%s: failed to add column", optarg);
     271  			scols_unref_column(cl);
     272  			fclose(f);
     273  			break;
     274  		}
     275  		case 'p':
     276  			parent_col = strtou32_or_err(optarg, "failed to parse tree PARENT column");
     277  			break;
     278  		case 'i':
     279  			id_col = strtou32_or_err(optarg, "failed to parse tree ID column");
     280  			break;
     281  		case 'J':
     282  			scols_table_enable_json(tb, 1);
     283  			scols_table_set_name(tb, "testtable");
     284  			break;
     285  		case 'm':
     286  			scols_table_enable_maxout(tb, TRUE);
     287  			break;
     288  		case 'M':
     289  			scols_table_enable_minout(tb, TRUE);
     290  			break;
     291  		case 'r':
     292  			scols_table_enable_raw(tb, TRUE);
     293  			break;
     294  		case 'E':
     295  			scols_table_enable_export(tb, TRUE);
     296  			break;
     297  		case 'C':
     298  			scols_table_set_column_separator(tb, optarg);
     299  			break;
     300  		case 'n':
     301  			nlines = strtou32_or_err(optarg, "failed to parse number of lines");
     302  			break;
     303  		case 'w':
     304  			scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS);
     305  			scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width"));
     306  			break;
     307  		case 'h':
     308  			usage();
     309  		default:
     310  			errtryhelp(EXIT_FAILURE);
     311  		}
     312  	}
     313  
     314  	if (nlines <= 0)
     315  		errx(EXIT_FAILURE, "--nlines not set");
     316  
     317  	for (n = 0; n < nlines; n++) {
     318  		struct libscols_line *ln = scols_new_line();
     319  
     320  		if (!ln || scols_table_add_line(tb, ln))
     321  			err(EXIT_FAILURE, "failed to add a new line");
     322  
     323  		scols_unref_line(ln);
     324  	}
     325  
     326  	n = 0;
     327  
     328  	while (optind < argc) {
     329  		FILE *f = fopen(argv[optind], "r");
     330  
     331  		if (!f)
     332  			err(EXIT_FAILURE, "%s: open failed", argv[optind]);
     333  
     334  		parse_column_data(f, tb, n);
     335  		optind++;
     336  		n++;
     337  	}
     338  
     339  	if (scols_table_is_tree(tb) && parent_col >= 0 && id_col >= 0)
     340  		compose_tree(tb, parent_col, id_col);
     341  
     342  	scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
     343  
     344  	scols_print_table(tb);
     345  	scols_unref_table(tb);
     346  	return EXIT_SUCCESS;
     347  }