]>
Commit | Line | Data |
---|---|---|
abccd63d MY |
1 | /* |
2 | * column-list-table.h - helper functions for implementing -H/--list-columns option | |
3 | * | |
4 | * Copyright (C) 2023 Red Hat, Inc. All rights reserved. | |
5 | * Written by Masatake YAMATO <yamato@redhat.com> | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it would be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software Foundation, | |
19 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | #ifndef UTIL_LINUX_COLUMN_LIST_TABLE_H | |
23 | #define UTIL_LINUX_COLUMN_LIST_TABLE_H | |
24 | ||
25 | #include "libsmartcols.h" | |
26 | #include <stdio.h> | |
27 | ||
28 | enum { CLT_COL_HOLDER, CLT_COL_TYPE, CLT_COL_DESC }; | |
29 | ||
30 | static inline struct libscols_table *xcolumn_list_table_new(const char *table_name, | |
31 | FILE *out, | |
32 | int raw, | |
33 | int json) | |
34 | { | |
35 | struct clt_colinfo { | |
36 | const char *name; | |
37 | int flags; | |
38 | }; | |
39 | struct libscols_table *tb; | |
40 | ||
41 | scols_init_debug(0); | |
42 | ||
43 | tb = scols_new_table(); | |
44 | if (!tb) | |
45 | err(EXIT_FAILURE, _("failed to allocate output table")); | |
46 | ||
47 | scols_table_set_name(tb, table_name); | |
48 | scols_table_set_stream(tb, out); | |
49 | scols_table_enable_noheadings(tb, 1); | |
50 | scols_table_enable_raw(tb, raw); | |
51 | scols_table_enable_json(tb, json); | |
52 | ||
53 | if (!scols_table_new_column(tb, "HOLDER", 0, SCOLS_FL_RIGHT)) | |
54 | goto failed; | |
55 | if (!scols_table_new_column(tb, "TYPE", 0, 0)) | |
56 | goto failed; | |
57 | if (!scols_table_new_column(tb, "DESCRIPTION", 0, 0)) | |
58 | goto failed; | |
59 | return tb; | |
60 | ||
61 | failed: | |
62 | err(EXIT_FAILURE, _("failed to allocate output column")); | |
63 | } | |
64 | ||
65 | static inline void xcolumn_list_table_append_line(struct libscols_table *tb, | |
66 | const char *name, | |
67 | int json_type, const char *fallback_typename, | |
68 | const char *desc) | |
69 | { | |
70 | struct libscols_line *ln = scols_table_new_line(tb, NULL); | |
71 | if (!ln) | |
72 | err(EXIT_FAILURE, _("failed to allocate output line")); | |
73 | ||
74 | if (scols_line_set_data(ln, CLT_COL_HOLDER, name)) | |
75 | err(EXIT_FAILURE, _("failed to add output data")); | |
76 | if (scols_line_set_data(ln, CLT_COL_TYPE, (json_type == SCOLS_JSON_STRING ? "<string>": | |
77 | json_type == SCOLS_JSON_ARRAY_STRING ? "<string>": | |
78 | json_type == SCOLS_JSON_ARRAY_NUMBER ? "<string>": | |
79 | json_type == SCOLS_JSON_NUMBER ? "<integer>": | |
80 | json_type == SCOLS_JSON_FLOAT ? "<float>": | |
81 | json_type == SCOLS_JSON_BOOLEAN ? "<boolean>": | |
82 | fallback_typename ?: "<string>"))) | |
83 | err(EXIT_FAILURE, _("failed to add output data")); | |
84 | if (scols_line_set_data(ln, CLT_COL_DESC, desc)) | |
85 | err(EXIT_FAILURE, _("failed to add output data")); | |
86 | } | |
87 | ||
88 | #endif |