Debian Codegraph @

libsmartcols/samples/fromfile.c

raw
1 /*
2 * Copyright (C) 2016 Karel Zak <[email protected]>
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 #include "mangle.h"
22 #include "path.h"
23
24 #include "libsmartcols.h"
25
26 static struct libscols_column *parse_column(const char *path)
27 {
28 char buf[BUFSIZ];
29 struct libscols_column *cl;
30
31 if (ul_path_read_buffer(NULL, buf, sizeof(buf), path) < 0)
32 err(EXIT_FAILURE, "failed to read column: %s", path);
33
34 cl = scols_new_column();
35 if (!cl)
36 err(EXIT_FAILURE, "failed to allocate column");
37
38 if (scols_column_set_properties(cl, buf) != 0)
39 err(EXIT_FAILURE, "failed to set column properties");
40
41 return cl;
42 }
43
44 static int parse_column_data(FILE *f, struct libscols_table *tb, int col)
45 {
46 size_t len = 0, nlines = 0;
47 ssize_t i;
48 char *str = NULL, *p;
49
50 while ((i = getline(&str, &len, f)) != -1) {
51 struct libscols_line *ln;
52 int rc = 0;
53
54 ln = scols_table_get_line(tb, nlines++);
55 if (!ln)
56 break;
57
58 p = strrchr(str, '\n');
59 if (p)
60 *p = '\0';
61 if (!*str)
62 continue;
63
64 /* convert \x?? to real bytes */
65 if (strstr(str, "\\x")) {
66 struct libscols_cell *ce = scols_line_get_cell(ln, col);
67 size_t sz = i + 1;
68 char *buf = xcalloc(1, sz);
69
70 sz = unhexmangle_to_buffer(str, buf, sz);
71 if (sz)
72 rc = scols_cell_refer_memory(ce, buf, sz);
73 else
74 free(buf);
75 } else
76 rc = scols_line_set_data(ln, col, str);
77 if (rc)
78 err(EXIT_FAILURE, "failed to add output data");
79 }
80
81 free(str);
82 return 0;
83
84 }
85
86 static struct libscols_line *get_line_with_id(struct libscols_table *tb,
87 int col_id, const char *id)
88 {
89 struct libscols_line *ln;
90 struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
91
92 while (scols_table_next_line(tb, itr, &ln) == 0) {
93 struct libscols_cell *ce = scols_line_get_cell(ln, col_id);
94 const char *data = ce ? scols_cell_get_data(ce) : NULL;
95
96 if (data && strcmp(data, id) == 0)
97 break;
98 }
99
100 scols_free_iter(itr);
101 return ln;
102 }
103
104 static void compose_tree(struct libscols_table *tb, int parent_col, int id_col)
105 {
106 struct libscols_line *ln;
107 struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
108
109 while (scols_table_next_line(tb, itr, &ln) == 0) {
110 struct libscols_line *parent = NULL;
111 struct libscols_cell *ce = scols_line_get_cell(ln, parent_col);
112 const char *data = ce ? scols_cell_get_data(ce) : NULL;
113
114 if (data)
115 parent = get_line_with_id(tb, id_col, data);
116 if (parent)
117 scols_line_add_child(parent, ln);
118 }
119
120 scols_free_iter(itr);
121 }
122
123 static struct libscols_filter *init_filter(
124 struct libscols_table *tb,
125 const char *query, int dump)
126 {
127 struct libscols_iter *itr;
128 struct libscols_filter *f = scols_new_filter(NULL);
129 const char *name = NULL;
130 int rc = 0;
131
132 if (!f)
133 err(EXIT_FAILURE, "failed to allocate filter");
134 if (scols_filter_parse_string(f, query) != 0) {
135 warnx("failed to parse filter: %s", scols_filter_get_errmsg(f));
136 scols_unref_filter(f);
137 return NULL;
138 }
139
140 itr = scols_new_iter(SCOLS_ITER_FORWARD);
141 if (!itr)
142 err(EXIT_FAILURE, "failed to allocate iterator");
143
144 while (scols_filter_next_holder(f, itr, &name, 0) == 0) {
145 struct libscols_column *col;
146
147 col = scols_table_get_column_by_name(tb, name);
148 if (!col) {
149 warnx("unknown column '%s' in filter", name);
150 rc++;
151 continue;
152 }
153 scols_filter_assign_column(f, itr, name, col);
154 }
155
156 scols_free_iter(itr);
157 if (dump && f)
158 scols_dump_filter(f, stdout);
159 if (rc) {
160 scols_unref_filter(f);
161 errx(EXIT_FAILURE, "failed to initialize filter");
162 }
163
164 return f;
165 }
166
167 /* Note: This is a simple (naive) way to use the filter, employed here for
168 * testing functionality.
169 *
170 * A more effective approach to using the filter is demonstrated in lsblk.c,
171 * where data manipulation is divided into two steps. The initial step prepares
172 * only the data necessary for evaluating the filter, and the remaining data is
173 * gathered later, only if necessary.
174 */
175 static void apply_filter(struct libscols_table *tb, struct libscols_filter *fltr)
176 {
177 struct libscols_iter *itr = scols_new_iter(SCOLS_ITER_FORWARD);
178 struct libscols_line *ln;
179
180 if (!itr)
181 err(EXIT_FAILURE, "failed to allocate iterator");
182
183 while (scols_table_next_line(tb, itr, &ln) == 0) {
184 int status = 0;
185
186 if (scols_line_apply_filter(ln, fltr, &status) != 0)
187 err(EXIT_FAILURE, "failed to apply filter");
188 if (status == 0) {
189 struct libscols_line *x = scols_line_get_parent(ln);
190
191 if (x)
192 scols_line_remove_child(x, ln);
193 scols_table_remove_line(tb, ln);
194 ln = NULL;
195 }
196 }
197
198 scols_free_iter(itr);
199 }
200
201 static void __attribute__((__noreturn__)) usage(void)
202 {
203 FILE *out = stdout;
204 fprintf(out,
205 "\n %s [options] <column-data-file> ...\n\n", program_invocation_short_name);
206
207 fputs(" -m, --maxout fill all terminal width\n", out);
208 fputs(" -M, --minout minimize trailing padding\n", out);
209 fputs(" -c, --column <file> column definition\n", out);
210 fputs(" -n, --nlines <num> number of lines\n", out);
211 fputs(" -J, --json JSON output format\n", out);
212 fputs(" -r, --raw RAW output format\n", out);
213 fputs(" -E, --export use key=\"value\" output format\n", out);
214 fputs(" -C, --colsep <str> set columns separator\n", out);
215 fputs(" -w, --width <num> hardcode terminal width\n", out);
216 fputs(" -p, --tree-parent-column <n> parent column\n", out);
217 fputs(" -i, --tree-id-column <n> id column\n", out);
218 fputs(" -Q, --filter <expr> filter\n", out);
219 fputs(" -h, --help this help\n", out);
220 fputs("\n", out);
221
222 exit(EXIT_SUCCESS);
223 }
224
225 int main(int argc, char *argv[])
226 {
227 struct libscols_table *tb;
228 int c, n, nlines = 0, rc;
229 int parent_col = -1, id_col = -1;
230 int fltr_dump = 0;
231 const char *fltr_str = NULL;
232 struct libscols_filter *fltr = NULL;
233
234 static const struct option longopts[] = {
235 { "maxout", 0, NULL, 'm' },
236 { "minout", 0, NULL, 'M' },
237 { "column", 1, NULL, 'c' },
238 { "nlines", 1, NULL, 'n' },
239 { "width", 1, NULL, 'w' },
240 { "tree-parent-column", 1, NULL, 'p' },
241 { "tree-id-column", 1, NULL, 'i' },
242 { "json", 0, NULL, 'J' },
243 { "raw", 0, NULL, 'r' },
244 { "export", 0, NULL, 'E' },
245 { "colsep", 1, NULL, 'C' },
246 { "filter", 1, NULL, 'Q' },
247 { "filter-dump", 0, NULL, 'd' },
248 { "help", 0, NULL, 'h' },
249 { NULL, 0, NULL, 0 },
250 };
251
252 static const ul_excl_t excl[] = { /* rows and cols in ASCII order */
253 { 'E', 'J', 'r' },
254 { 'M', 'm' },
255 { 0 }
256 };
257 int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT;
258
259 setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */
260 scols_init_debug(0);
261
262 tb = scols_new_table();
263 if (!tb)
264 err(EXIT_FAILURE, "failed to create output table");
265
266 while((c = getopt_long(argc, argv, "hCc:dEi:JMmn:p:Q:rw:", longopts, NULL)) != -1) {
267
268 err_exclusive_options(c, longopts, excl, excl_st);
269
270 switch(c) {
271 case 'c': /* add column from file */
272 {
273 struct libscols_column *cl = parse_column(optarg);
274
275 if (cl && scols_table_add_column(tb, cl))
276 err(EXIT_FAILURE, "%s: failed to add column", optarg);
277 scols_unref_column(cl);
278 break;
279 }
280 case 'd':
281 fltr_dump = 1;
282 break;
283 case 'p':
284 parent_col = strtou32_or_err(optarg, "failed to parse tree PARENT column");
285 break;
286 case 'i':
287 id_col = strtou32_or_err(optarg, "failed to parse tree ID column");
288 break;
289 case 'J':
290 scols_table_enable_json(tb, 1);
291 scols_table_set_name(tb, "testtable");
292 break;
293 case 'm':
294 scols_table_enable_maxout(tb, TRUE);
295 break;
296 case 'M':
297 scols_table_enable_minout(tb, TRUE);
298 break;
299 case 'r':
300 scols_table_enable_raw(tb, TRUE);
301 break;
302 case 'E':
303 scols_table_enable_export(tb, TRUE);
304 break;
305 case 'C':
306 scols_table_set_column_separator(tb, optarg);
307 break;
308 case 'n':
309 nlines = strtou32_or_err(optarg, "failed to parse number of lines");
310 break;
311 case 'Q':
312 fltr_str = optarg;
313 break;
314 case 'w':
315 scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS);
316 scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width"));
317 break;
318 case 'h':
319 usage();
320 default:
321 errtryhelp(EXIT_FAILURE);
322 }
323 }
324
325 if (nlines <= 0)
326 errx(EXIT_FAILURE, "--nlines not set");
327
328 for (n = 0; n < nlines; n++) {
329 struct libscols_line *ln = scols_new_line();
330
331 if (!ln || scols_table_add_line(tb, ln))
332 err(EXIT_FAILURE, "failed to add a new line");
333
334 scols_unref_line(ln);
335 }
336
337 if (fltr_str) {
338 fltr = init_filter(tb, fltr_str, fltr_dump);
339 if (!fltr) {
340 rc = EXIT_FAILURE;
341 goto done;
342 }
343 }
344
345 n = 0;
346
347 while (optind < argc) {
348 FILE *f = fopen(argv[optind], "r");
349
350 if (!f)
351 err(EXIT_FAILURE, "%s: open failed", argv[optind]);
352
353 parse_column_data(f, tb, n);
354 optind++;
355 n++;
356 }
357
358 if (scols_table_is_tree(tb) && parent_col >= 0 && id_col >= 0)
359 compose_tree(tb, parent_col, id_col);
360
361 scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
362
363 if (fltr)
364 apply_filter(tb, fltr);
365
366 scols_print_table(tb);
367 rc = EXIT_SUCCESS;
368 done:
369 scols_unref_filter(fltr);
370 scols_unref_table(tb);
371 return rc;
372 }