]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/format-table.h
cgroup-util: allow cg_read_pid() to skip unmapped (zero) pids
[thirdparty/systemd.git] / src / shared / format-table.h
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <sys/types.h>
7
8 #include "json.h"
9 #include "macro.h"
10 #include "pager.h"
11
12 typedef enum TableDataType {
13 TABLE_EMPTY,
14 TABLE_STRING,
15 TABLE_HEADER, /* in regular mode: the cells in the first row, that carry the column names */
16 TABLE_FIELD, /* in vertical mode: the cells in the first column, that carry the field names */
17 TABLE_STRV,
18 TABLE_STRV_WRAPPED,
19 TABLE_PATH,
20 TABLE_PATH_BASENAME, /* like TABLE_PATH, but display only last path element (i.e. the "basename") in regular output */
21 TABLE_BOOLEAN,
22 TABLE_BOOLEAN_CHECKMARK,
23 TABLE_TIMESTAMP,
24 TABLE_TIMESTAMP_UTC,
25 TABLE_TIMESTAMP_RELATIVE,
26 TABLE_TIMESTAMP_RELATIVE_MONOTONIC,
27 TABLE_TIMESTAMP_LEFT,
28 TABLE_TIMESTAMP_DATE,
29 TABLE_TIMESPAN,
30 TABLE_TIMESPAN_MSEC,
31 TABLE_TIMESPAN_DAY,
32 TABLE_SIZE,
33 TABLE_BPS,
34 TABLE_INT,
35 TABLE_INT8,
36 TABLE_INT16,
37 TABLE_INT32,
38 TABLE_INT64,
39 TABLE_UINT,
40 TABLE_UINT8,
41 TABLE_UINT16,
42 TABLE_UINT32,
43 TABLE_UINT32_HEX,
44 TABLE_UINT64,
45 TABLE_UINT64_HEX,
46 TABLE_PERCENT,
47 TABLE_IFINDEX,
48 TABLE_IN_ADDR, /* Takes a union in_addr_union (or a struct in_addr) */
49 TABLE_IN6_ADDR, /* Takes a union in_addr_union (or a struct in6_addr) */
50 TABLE_ID128,
51 TABLE_UUID,
52 TABLE_UID,
53 TABLE_GID,
54 TABLE_PID,
55 TABLE_SIGNAL,
56 TABLE_MODE, /* as in UNIX file mode (mode_t), in typical octal output */
57 TABLE_MODE_INODE_TYPE, /* also mode_t, but displays only the inode type as string */
58 TABLE_DEVNUM, /* a dev_t, displayed in the usual major:minor way */
59 _TABLE_DATA_TYPE_MAX,
60
61 /* The following are not really data types, but commands for table_add_cell_many() to make changes to
62 * a cell just added. */
63 TABLE_SET_MINIMUM_WIDTH,
64 TABLE_SET_MAXIMUM_WIDTH,
65 TABLE_SET_WEIGHT,
66 TABLE_SET_ALIGN_PERCENT,
67 TABLE_SET_ELLIPSIZE_PERCENT,
68 TABLE_SET_COLOR,
69 TABLE_SET_RGAP_COLOR,
70 TABLE_SET_BOTH_COLORS,
71 TABLE_SET_UNDERLINE,
72 TABLE_SET_RGAP_UNDERLINE,
73 TABLE_SET_BOTH_UNDERLINES,
74 TABLE_SET_URL,
75 TABLE_SET_UPPERCASE,
76
77 _TABLE_DATA_TYPE_INVALID = -EINVAL,
78 } TableDataType;
79
80 typedef enum TableErsatz {
81 TABLE_ERSATZ_EMPTY,
82 TABLE_ERSATZ_DASH,
83 TABLE_ERSATZ_UNSET,
84 TABLE_ERSATZ_NA,
85 _TABLE_ERSATZ_MAX,
86 } TableErsatz;
87
88 typedef struct Table Table;
89 typedef struct TableCell TableCell;
90
91 Table *table_new_internal(const char *first_header, ...) _sentinel_;
92 #define table_new(...) table_new_internal(__VA_ARGS__, NULL)
93 Table *table_new_raw(size_t n_columns);
94 Table *table_new_vertical(void);
95 Table *table_unref(Table *t);
96
97 DEFINE_TRIVIAL_CLEANUP_FUNC(Table*, table_unref);
98
99 int table_add_cell_full(Table *t, TableCell **ret_cell, TableDataType type, const void *data, size_t minimum_width, size_t maximum_width, unsigned weight, unsigned align_percent, unsigned ellipsize_percent);
100 static inline int table_add_cell(Table *t, TableCell **ret_cell, TableDataType type, const void *data) {
101 return table_add_cell_full(t, ret_cell, type, data, SIZE_MAX, SIZE_MAX, UINT_MAX, UINT_MAX, UINT_MAX);
102 }
103 int table_add_cell_stringf_full(Table *t, TableCell **ret_cell, TableDataType type, const char *format, ...) _printf_(4, 5);
104 #define table_add_cell_stringf(t, ret_cell, format, ...) table_add_cell_stringf_full(t, ret_cell, TABLE_STRING, format, __VA_ARGS__)
105
106 int table_fill_empty(Table *t, size_t until_column);
107
108 int table_dup_cell(Table *t, TableCell *cell);
109
110 int table_set_minimum_width(Table *t, TableCell *cell, size_t minimum_width);
111 int table_set_maximum_width(Table *t, TableCell *cell, size_t maximum_width);
112 int table_set_weight(Table *t, TableCell *cell, unsigned weight);
113 int table_set_align_percent(Table *t, TableCell *cell, unsigned percent);
114 int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent);
115 int table_set_color(Table *t, TableCell *cell, const char *color);
116 int table_set_rgap_color(Table *t, TableCell *cell, const char *color);
117 int table_set_underline(Table *t, TableCell *cell, bool b);
118 int table_set_rgap_underline(Table *t, TableCell *cell, bool b);
119 int table_set_url(Table *t, TableCell *cell, const char *url);
120 int table_set_uppercase(Table *t, TableCell *cell, bool b);
121
122 int table_update(Table *t, TableCell *cell, TableDataType type, const void *data);
123
124 int table_add_many_internal(Table *t, TableDataType first_type, ...);
125 #define table_add_many(t, ...) table_add_many_internal(t, __VA_ARGS__, _TABLE_DATA_TYPE_MAX)
126
127 void table_set_header(Table *table, bool b);
128 void table_set_width(Table *t, size_t width);
129 void table_set_cell_height_max(Table *t, size_t height);
130 void table_set_ersatz_string(Table *t, TableErsatz ersatz);
131 int table_set_display_internal(Table *t, size_t first_column, ...);
132 #define table_set_display(...) table_set_display_internal(__VA_ARGS__, SIZE_MAX)
133 int table_set_sort_internal(Table *t, size_t first_column, ...);
134 #define table_set_sort(...) table_set_sort_internal(__VA_ARGS__, SIZE_MAX)
135 int table_set_reverse(Table *t, size_t column, bool b);
136 int table_hide_column_from_display_internal(Table *t, ...);
137 #define table_hide_column_from_display(t, ...) table_hide_column_from_display_internal(t, __VA_ARGS__, SIZE_MAX)
138
139 int table_print(Table *t, FILE *f);
140 int table_format(Table *t, char **ret);
141
142 static inline TableCell* TABLE_HEADER_CELL(size_t i) {
143 return SIZE_TO_PTR(i + 1);
144 }
145
146 size_t table_get_rows(Table *t);
147 static inline bool table_isempty(Table *t) {
148 if (!t)
149 return true;
150
151 return table_get_rows(t) <= 1;
152 }
153 size_t table_get_columns(Table *t);
154
155 size_t table_get_current_column(Table *t);
156
157 TableCell *table_get_cell(Table *t, size_t row, size_t column);
158
159 const void *table_get(Table *t, TableCell *cell);
160 const void *table_get_at(Table *t, size_t row, size_t column);
161
162 int table_to_json(Table *t, JsonVariant **ret);
163 int table_print_json(Table *t, FILE *f, JsonFormatFlags json_flags);
164
165 int table_print_with_pager(Table *t, JsonFormatFlags json_format_flags, PagerFlags pager_flags, bool show_header);
166
167 int table_set_json_field_name(Table *t, size_t idx, const char *name);
168
169 #define table_log_add_error(r) \
170 log_error_errno(r, "Failed to add cells to table: %m")
171
172 #define table_log_print_error(r) \
173 log_error_errno(r, "Failed to print table: %m")
174
175 #define table_log_sort_error(r) \
176 log_error_errno(r, "Failed to sort table: %m")