util-linux (2.39)
1 /*
2 * Prints table or tree.
3 *
4 * Copyright (C) 2014 Ondrej Oprala <ooprala@redhat.com>
5 * Copyright (C) 2014 Karel Zak <kzak@redhat.com>
6 *
7 * This file may be redistributed under the terms of the
8 * GNU Lesser General Public License.
9 */
10 #ifndef _LIBSMARTCOLS_H
11 #define _LIBSMARTCOLS_H
12
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <sys/types.h>
20
21 /**
22 * LIBSMARTCOLS_VERSION:
23 *
24 * Library version string
25 */
26 #define LIBSMARTCOLS_VERSION "2.39.0"
27
28 /**
29 * libscols_iter:
30 *
31 * Generic iterator
32 */
33 struct libscols_iter;
34
35 /**
36 * libscols_symbols:
37 *
38 * Symbol groups for printing tree hierarchies
39 */
40 struct libscols_symbols;
41
42 /**
43 * libscols_cell:
44 *
45 * A cell - the smallest library object
46 */
47 struct libscols_cell;
48
49 /**
50 * libscols_line:
51 *
52 * A line - an array of cells
53 */
54 struct libscols_line;
55
56 /**
57 * libscols_table:
58 *
59 * A table - The most abstract object, encapsulating lines, columns, symbols and cells
60 */
61 struct libscols_table;
62
63 /**
64 * libscols_column:
65 *
66 * A column - defines the number of columns and column names
67 */
68 struct libscols_column;
69
70 /* iter.c */
71 enum {
72
73 SCOLS_ITER_FORWARD = 0,
74 SCOLS_ITER_BACKWARD
75 };
76
77 /*
78 * Column flags
79 */
80 enum {
81 SCOLS_FL_TRUNC = (1 << 0), /* truncate fields data if necessary */
82 SCOLS_FL_TREE = (1 << 1), /* use tree "ascii art" */
83 SCOLS_FL_RIGHT = (1 << 2), /* align to the right */
84 SCOLS_FL_STRICTWIDTH = (1 << 3), /* don't reduce width if column is empty */
85 SCOLS_FL_NOEXTREMES = (1 << 4), /* ignore extreme fields when count column width*/
86 SCOLS_FL_HIDDEN = (1 << 5), /* maintain data, but don't print */
87 SCOLS_FL_WRAP = (1 << 6) /* wrap long lines to multi-line cells */
88 };
89
90 /*
91 * Column JSON types
92 */
93 enum {
94 SCOLS_JSON_STRING = 0, /* default */
95 SCOLS_JSON_NUMBER = 1,
96 SCOLS_JSON_BOOLEAN = 2,
97 SCOLS_JSON_ARRAY_STRING = 3, /* e.g. for multi-line (SCOLS_FL_WRAP) cells */
98 SCOLS_JSON_ARRAY_NUMBER = 4,
99 SCOLS_JSON_BOOLEAN_OPTIONAL = 5,
100 };
101
102 /*
103 * Cell flags, see scols_cell_set_flags() before use
104 */
105 enum {
106 /* alignment evaluated in order: right,center,left */
107 SCOLS_CELL_FL_LEFT = 0,
108 SCOLS_CELL_FL_CENTER = (1 << 0),
109 SCOLS_CELL_FL_RIGHT = (1 << 1)
110 };
111
112 extern struct libscols_iter *scols_new_iter(int direction);
113 extern void scols_free_iter(struct libscols_iter *itr);
114 extern void scols_reset_iter(struct libscols_iter *itr, int direction);
115 extern int scols_iter_get_direction(const struct libscols_iter *itr);
116
117 /* init.c */
118 extern void scols_init_debug(int mask);
119
120 /* version.c */
121 extern int scols_parse_version_string(const char *ver_string);
122 extern int scols_get_library_version(const char **ver_string);
123
124 /* symbols.c */
125 extern struct libscols_symbols *scols_new_symbols(void);
126 extern void scols_ref_symbols(struct libscols_symbols *sy);
127 extern void scols_unref_symbols(struct libscols_symbols *sy);
128 extern struct libscols_symbols *scols_copy_symbols(const struct libscols_symbols *sy);
129 extern int scols_symbols_set_branch(struct libscols_symbols *sy, const char *str);
130 extern int scols_symbols_set_vertical(struct libscols_symbols *sy, const char *str);
131 extern int scols_symbols_set_right(struct libscols_symbols *sy, const char *str);
132 extern int scols_symbols_set_title_padding(struct libscols_symbols *sy, const char *str);
133 extern int scols_symbols_set_cell_padding(struct libscols_symbols *sy, const char *str);
134
135 extern int scols_symbols_set_group_vertical(struct libscols_symbols *sy, const char *str);
136 extern int scols_symbols_set_group_horizontal(struct libscols_symbols *sy, const char *str);
137 extern int scols_symbols_set_group_first_member(struct libscols_symbols *sy, const char *str);
138 extern int scols_symbols_set_group_last_member(struct libscols_symbols *sy, const char *str);
139 extern int scols_symbols_set_group_middle_member(struct libscols_symbols *sy, const char *str);
140 extern int scols_symbols_set_group_last_child(struct libscols_symbols *sy, const char *str);
141 extern int scols_symbols_set_group_middle_child(struct libscols_symbols *sy, const char *str);
142
143 /* cell.c */
144 extern int scols_reset_cell(struct libscols_cell *ce);
145 extern int scols_cell_copy_content(struct libscols_cell *dest,
146 const struct libscols_cell *src);
147 extern int scols_cell_set_data(struct libscols_cell *ce, const char *data);
148 extern int scols_cell_refer_data(struct libscols_cell *ce, char *data);
149 extern const char *scols_cell_get_data(const struct libscols_cell *ce);
150 extern int scols_cell_set_color(struct libscols_cell *ce, const char *color);
151 extern const char *scols_cell_get_color(const struct libscols_cell *ce);
152
153 extern int scols_cell_set_flags(struct libscols_cell *ce, int flags);
154 extern int scols_cell_get_flags(const struct libscols_cell *ce);
155 extern int scols_cell_get_alignment(const struct libscols_cell *ce);
156
157 extern void *scols_cell_get_userdata(struct libscols_cell *ce);
158 extern int scols_cell_set_userdata(struct libscols_cell *ce, void *data);
159
160 extern int scols_cmpstr_cells(struct libscols_cell *a,
161 struct libscols_cell *b, void *data);
162 /* column.c */
163 extern int scols_column_is_tree(const struct libscols_column *cl);
164 extern int scols_column_is_trunc(const struct libscols_column *cl);
165 extern int scols_column_is_right(const struct libscols_column *cl);
166 extern int scols_column_is_strict_width(const struct libscols_column *cl);
167 extern int scols_column_is_hidden(const struct libscols_column *cl);
168 extern int scols_column_is_noextremes(const struct libscols_column *cl);
169 extern int scols_column_is_wrap(const struct libscols_column *cl);
170 extern int scols_column_is_customwrap(const struct libscols_column *cl);
171
172 extern size_t scols_column_get_width(const struct libscols_column *cl);
173
174 extern int scols_column_set_safechars(struct libscols_column *cl, const char *safe);
175 extern const char *scols_column_get_safechars(const struct libscols_column *cl);
176
177 extern int scols_column_set_json_type(struct libscols_column *cl, int type);
178 extern int scols_column_get_json_type(const struct libscols_column *cl);
179
180 extern int scols_column_set_flags(struct libscols_column *cl, int flags);
181 extern int scols_column_get_flags(const struct libscols_column *cl);
182 extern struct libscols_column *scols_new_column(void);
183 extern void scols_ref_column(struct libscols_column *cl);
184 extern void scols_unref_column(struct libscols_column *cl);
185 extern struct libscols_column *scols_copy_column(const struct libscols_column *cl);
186 extern int scols_column_set_whint(struct libscols_column *cl, double whint);
187 extern double scols_column_get_whint(const struct libscols_column *cl);
188 extern struct libscols_cell *scols_column_get_header(struct libscols_column *cl);
189 extern int scols_column_set_color(struct libscols_column *cl, const char *color);
190 extern const char *scols_column_get_color(const struct libscols_column *cl);
191 extern struct libscols_table *scols_column_get_table(const struct libscols_column *cl);
192
193 extern int scols_column_set_name(struct libscols_column *cl, const char *name);
194 extern const char *scols_column_get_name(struct libscols_column *cl);
195 extern const char *scols_column_get_name_as_shellvar(struct libscols_column *cl);
196
197 extern int scols_column_set_properties(struct libscols_column *cl, const char *opts);
198
199 extern int scols_column_set_cmpfunc(struct libscols_column *cl,
200 int (*cmp)(struct libscols_cell *a,
201 struct libscols_cell *b, void *),
202 void *data);
203
204 extern int scols_column_set_wrapfunc(struct libscols_column *cl,
205 size_t (*wrap_chunksize)(const struct libscols_column *,
206 const char *, void *),
207 char * (*wrap_nextchunk)(const struct libscols_column *,
208 char *, void *),
209 void *userdata);
210
211 extern char *scols_wrapnl_nextchunk(const struct libscols_column *cl, char *data, void *userdata);
212 extern size_t scols_wrapnl_chunksize(const struct libscols_column *cl, const char *data, void *userdata);
213
214 /* line.c */
215 extern struct libscols_line *scols_new_line(void);
216 extern void scols_ref_line(struct libscols_line *ln);
217 extern void scols_unref_line(struct libscols_line *ln);
218 extern int scols_line_alloc_cells(struct libscols_line *ln, size_t n);
219 extern void scols_line_free_cells(struct libscols_line *ln);
220 extern int scols_line_set_userdata(struct libscols_line *ln, void *data);
221 extern void *scols_line_get_userdata(struct libscols_line *ln);
222 extern int scols_line_remove_child(struct libscols_line *ln, struct libscols_line *child);
223 extern int scols_line_add_child(struct libscols_line *ln, struct libscols_line *child);
224 extern int scols_line_has_children(struct libscols_line *ln);
225 extern int scols_line_is_ancestor(struct libscols_line *ln, struct libscols_line *parent);
226 extern int scols_line_next_child(struct libscols_line *ln,
227 struct libscols_iter *itr, struct libscols_line **chld);
228 extern struct libscols_line *scols_line_get_parent(const struct libscols_line *ln);
229 extern int scols_line_set_color(struct libscols_line *ln, const char *color);
230 extern const char *scols_line_get_color(const struct libscols_line *ln);
231 extern size_t scols_line_get_ncells(const struct libscols_line *ln);
232 extern struct libscols_cell *scols_line_get_cell(struct libscols_line *ln, size_t n);
233 extern struct libscols_cell *scols_line_get_column_cell(
234 struct libscols_line *ln,
235 struct libscols_column *cl);
236 extern int scols_line_set_data(struct libscols_line *ln, size_t n, const char *data);
237 extern int scols_line_refer_data(struct libscols_line *ln, size_t n, char *data);
238 extern int scols_line_set_column_data(struct libscols_line *ln, struct libscols_column *cl, const char *data);
239 extern const char *scols_line_get_column_data(struct libscols_line *ln, struct libscols_column *cl);
240 extern int scols_line_refer_column_data(struct libscols_line *ln, struct libscols_column *cl, char *data);
241 extern struct libscols_line *scols_copy_line(const struct libscols_line *ln);
242
243 /* table */
244 extern int scols_table_colors_wanted(const struct libscols_table *tb);
245 extern int scols_table_set_name(struct libscols_table *tb, const char *name);
246 extern const char *scols_table_get_name(const struct libscols_table *tb);
247 extern struct libscols_cell *scols_table_get_title(struct libscols_table *tb);
248 extern int scols_table_is_raw(const struct libscols_table *tb);
249 extern int scols_table_is_ascii(const struct libscols_table *tb);
250 extern int scols_table_is_json(const struct libscols_table *tb);
251 extern int scols_table_is_noheadings(const struct libscols_table *tb);
252 extern int scols_table_is_header_repeat(const struct libscols_table *tb);
253 extern int scols_table_is_empty(const struct libscols_table *tb);
254 extern int scols_table_is_export(const struct libscols_table *tb);
255 extern int scols_table_is_shellvar(const struct libscols_table *tb);
256 extern int scols_table_is_maxout(const struct libscols_table *tb);
257 extern int scols_table_is_minout(const struct libscols_table *tb);
258 extern int scols_table_is_nowrap(const struct libscols_table *tb);
259 extern int scols_table_is_nolinesep(const struct libscols_table *tb);
260 extern int scols_table_is_tree(const struct libscols_table *tb);
261 extern int scols_table_is_noencoding(const struct libscols_table *tb);
262
263 extern int scols_table_enable_colors(struct libscols_table *tb, int enable);
264 extern int scols_table_enable_raw(struct libscols_table *tb, int enable);
265 extern int scols_table_enable_ascii(struct libscols_table *tb, int enable);
266 extern int scols_table_enable_json(struct libscols_table *tb, int enable);
267 extern int scols_table_enable_noheadings(struct libscols_table *tb, int enable);
268 extern int scols_table_enable_header_repeat(struct libscols_table *tb, int enable);
269 extern int scols_table_enable_export(struct libscols_table *tb, int enable);
270 extern int scols_table_enable_shellvar(struct libscols_table *tb, int enable);
271 extern int scols_table_enable_maxout(struct libscols_table *tb, int enable);
272 extern int scols_table_enable_minout(struct libscols_table *tb, int enable);
273 extern int scols_table_enable_nowrap(struct libscols_table *tb, int enable);
274 extern int scols_table_enable_nolinesep(struct libscols_table *tb, int enable);
275 extern int scols_table_enable_noencoding(struct libscols_table *tb, int enable);
276
277 extern int scols_table_set_column_separator(struct libscols_table *tb, const char *sep);
278 extern int scols_table_set_line_separator(struct libscols_table *tb, const char *sep);
279
280 extern struct libscols_table *scols_new_table(void);
281 extern void scols_ref_table(struct libscols_table *tb);
282 extern void scols_unref_table(struct libscols_table *tb);
283 extern int scols_table_add_column(struct libscols_table *tb, struct libscols_column *cl);
284 extern int scols_table_remove_column(struct libscols_table *tb, struct libscols_column *cl);
285 extern int scols_table_remove_columns(struct libscols_table *tb);
286 extern int scols_table_move_column(struct libscols_table *tb, struct libscols_column *pre, struct libscols_column *cl);
287 extern struct libscols_column *scols_table_new_column(struct libscols_table *tb, const char *name, double whint, int flags);
288 extern int scols_table_next_column(struct libscols_table *tb, struct libscols_iter *itr, struct libscols_column **cl);
289 extern int scols_table_set_columns_iter(struct libscols_table *tb, struct libscols_iter *itr, struct libscols_column *cl);
290 extern const char *scols_table_get_column_separator(const struct libscols_table *tb);
291 extern const char *scols_table_get_line_separator(const struct libscols_table *tb);
292 extern size_t scols_table_get_ncols(const struct libscols_table *tb);
293 extern size_t scols_table_get_nlines(const struct libscols_table *tb);
294 extern struct libscols_column *scols_table_get_column(struct libscols_table *tb, size_t n);
295 struct libscols_column *scols_table_get_column_by_name(struct libscols_table *tb, const char *name);
296 extern int scols_table_add_line(struct libscols_table *tb, struct libscols_line *ln);
297 extern int scols_table_remove_line(struct libscols_table *tb, struct libscols_line *ln);
298 extern void scols_table_remove_lines(struct libscols_table *tb);
299 extern int scols_table_next_line(struct libscols_table *tb, struct libscols_iter *itr, struct libscols_line **ln);
300 extern struct libscols_line *scols_table_new_line(struct libscols_table *tb, struct libscols_line *parent);
301 extern struct libscols_line *scols_table_get_line(struct libscols_table *tb, size_t n);
302 extern struct libscols_table *scols_copy_table(struct libscols_table *tb);
303 extern int scols_table_set_symbols(struct libscols_table *tb, struct libscols_symbols *sy);
304 extern int scols_table_set_default_symbols(struct libscols_table *tb);
305 extern struct libscols_symbols *scols_table_get_symbols(const struct libscols_table *tb);
306
307 extern int scols_table_set_stream(struct libscols_table *tb, FILE *stream);
308 extern FILE *scols_table_get_stream(const struct libscols_table *tb);
309 extern int scols_table_reduce_termwidth(struct libscols_table *tb, size_t reduce);
310
311 extern int scols_sort_table(struct libscols_table *tb, struct libscols_column *cl);
312 extern int scols_sort_table_by_tree(struct libscols_table *tb);
313 /*
314 *
315 */
316 enum {
317 SCOLS_TERMFORCE_AUTO = 0,
318 SCOLS_TERMFORCE_NEVER,
319 SCOLS_TERMFORCE_ALWAYS
320 };
321 extern int scols_table_set_termforce(struct libscols_table *tb, int force);
322 extern int scols_table_get_termforce(const struct libscols_table *tb);
323 extern int scols_table_set_termwidth(struct libscols_table *tb, size_t width);
324 extern size_t scols_table_get_termwidth(const struct libscols_table *tb);
325 extern int scols_table_set_termheight(struct libscols_table *tb, size_t height);
326 extern size_t scols_table_get_termheight(const struct libscols_table *tb);
327
328
329 /* table_print.c */
330 extern int scols_print_table(struct libscols_table *tb);
331 extern int scols_print_table_to_string(struct libscols_table *tb, char **data);
332
333 extern int scols_table_print_range( struct libscols_table *tb,
334 struct libscols_line *start,
335 struct libscols_line *end);
336 extern int scols_table_print_range_to_string( struct libscols_table *tb,
337 struct libscols_line *start,
338 struct libscols_line *end,
339 char **data);
340
341 /* grouping.c */
342 int scols_line_link_group(struct libscols_line *ln, struct libscols_line *member, int id);
343 int scols_table_group_lines(struct libscols_table *tb, struct libscols_line *ln,
344 struct libscols_line *member, int id);
345 #ifdef __cplusplus
346 }
347 #endif
348
349 #endif /* _LIBSMARTCOLS_H */