| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <ctype.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-id128.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "devnum-util.h" |
| 10 |
#include "fileio.h" |
| 11 |
#include "format-ifname.h" |
| 12 |
#include "format-table.h" |
| 13 |
#include "format-util.h" |
| 14 |
#include "glyph-util.h" |
| 15 |
#include "gunicode.h" |
| 16 |
#include "in-addr-util.h" |
| 17 |
#include "json-util.h" |
| 18 |
#include "memory-util.h" |
| 19 |
#include "memstream-util.h" |
| 20 |
#include "pager.h" |
| 21 |
#include "path-util.h" |
| 22 |
#include "pretty-print.h" |
| 23 |
#include "process-util.h" |
| 24 |
#include "signal-util.h" |
| 25 |
#include "sort-util.h" |
| 26 |
#include "stat-util.h" |
| 27 |
#include "string-util.h" |
| 28 |
#include "strv.h" |
| 29 |
#include "strxcpyx.h" |
| 30 |
#include "terminal-util.h" |
| 31 |
#include "time-util.h" |
| 32 |
#include "user-util.h" |
| 33 |
#include "utf8.h" |
| 34 |
|
| 35 |
#define DEFAULT_WEIGHT 100 |
| 36 |
|
| 37 |
/* |
| 38 |
A few notes on implementation details: |
| 39 |
|
| 40 |
- TableCell is a 'fake' structure, it's just used as data type to pass references to specific cell positions in the |
| 41 |
table. It can be easily converted to an index number and back. |
| 42 |
|
| 43 |
- TableData is where the actual data is stored: it encapsulates the data and formatting for a specific cell. It's |
| 44 |
'pseudo-immutable' and ref-counted. When a cell's data's formatting is to be changed, we duplicate the object if the |
| 45 |
ref-counting is larger than 1. Note that TableData and its ref-counting is mostly not visible to the outside. The |
| 46 |
outside only sees Table and TableCell. |
| 47 |
|
| 48 |
- The Table object stores a simple one-dimensional array of references to TableData objects, one row after the |
| 49 |
previous one. |
| 50 |
|
| 51 |
- There's no special concept of a "row" or "column" in the table, and no special concept of the "header" row. It's all |
| 52 |
derived from the cell index: we know how many cells are to be stored in a row, and can determine the rest from |
| 53 |
that. The first row is always the header row. If header display is turned off we simply skip outputting the first |
| 54 |
row. Also, when sorting rows we always leave the first row where it is, as the header shouldn't move. |
| 55 |
|
| 56 |
- Note because there's no row and no column object some properties that might be appropriate as row/column properties |
| 57 |
are exposed as cell properties instead. For example, the "weight" of a column (which is used to determine where to |
| 58 |
add/remove space preferable when expanding/compressing tables horizontally) is actually made the "weight" of a |
| 59 |
cell. Given that we usually need it per-column though we will calculate the average across every cell of the column |
| 60 |
instead. |
| 61 |
|
| 62 |
- To make things easy, when cells are added without any explicit configured formatting, then we'll copy the formatting |
| 63 |
from the same cell in the previous cell. This is particularly useful for the "weight" of the cell (see above), as |
| 64 |
this means setting the weight of the cells of the header row will nicely propagate to all cells in the other rows. |
| 65 |
*/ |
| 66 |
|
| 67 |
typedef struct TableData { |
| 68 |
unsigned n_ref; |
| 69 |
TableDataType type; |
| 70 |
|
| 71 |
size_t minimum_width; /* minimum width for the column */ |
| 72 |
size_t maximum_width; /* maximum width for the column */ |
| 73 |
size_t formatted_for_width; /* the width we tried to format for */ |
| 74 |
unsigned weight; /* the horizontal weight for this column, in case the table is expanded/compressed */ |
| 75 |
unsigned ellipsize_percent; /* 0 … 100, where to place the ellipsis when compression is needed */ |
| 76 |
unsigned align_percent; /* 0 … 100, where to pad with spaces when expanding is needed. 0: left-aligned, 100: right-aligned */ |
| 77 |
|
| 78 |
bool uppercase:1; /* Uppercase string on display */ |
| 79 |
bool underline:1; |
| 80 |
bool rgap_underline:1; |
| 81 |
|
| 82 |
const char *color; /* ANSI color string to use for this cell. When written to terminal should not move cursor. Will automatically be reset after the cell */ |
| 83 |
const char *rgap_color; /* The ANSI color to use for the gap right of this cell. Usually used to underline entire rows in a gapless fashion */ |
| 84 |
char *url; /* A URL to use for a clickable hyperlink */ |
| 85 |
char *formatted; /* A cached textual representation of the cell data, before ellipsation/alignment */ |
| 86 |
|
| 87 |
union { |
| 88 |
uint8_t data[0]; /* data is generic array */ |
| 89 |
bool boolean; |
| 90 |
int tristate; |
| 91 |
usec_t timestamp; |
| 92 |
usec_t timespan; |
| 93 |
uint64_t size; |
| 94 |
char string[0]; |
| 95 |
char **strv; |
| 96 |
int int_val; |
| 97 |
int8_t int8; |
| 98 |
int16_t int16; |
| 99 |
int32_t int32; |
| 100 |
int64_t int64; |
| 101 |
unsigned uint_val; |
| 102 |
uint8_t uint8; |
| 103 |
uint16_t uint16; |
| 104 |
uint32_t uint32; |
| 105 |
uint64_t uint64; |
| 106 |
int percent; /* we use 'int' as datatype for percent values in order to match the result of parse_percent() */ |
| 107 |
int ifindex; |
| 108 |
union in_addr_union address; |
| 109 |
sd_id128_t id128; |
| 110 |
uid_t uid; |
| 111 |
gid_t gid; |
| 112 |
pid_t pid; |
| 113 |
mode_t mode; |
| 114 |
dev_t devnum; |
| 115 |
sd_json_variant *json; |
| 116 |
/* … add more here as we start supporting more cell data types … */ |
| 117 |
}; |
| 118 |
} TableData; |
| 119 |
|
| 120 |
static size_t TABLE_CELL_TO_INDEX(TableCell *cell) { |
| 121 |
size_t i; |
| 122 |
|
| 123 |
assert(cell); |
| 124 |
|
| 125 |
i = PTR_TO_SIZE(cell); |
| 126 |
assert(i > 0); |
| 127 |
|
| 128 |
return i-1; |
| 129 |
} |
| 130 |
|
| 131 |
static TableCell* TABLE_INDEX_TO_CELL(size_t index) { |
| 132 |
assert(index != SIZE_MAX); |
| 133 |
return SIZE_TO_PTR(index + 1); |
| 134 |
} |
| 135 |
|
| 136 |
struct Table { |
| 137 |
size_t n_columns; |
| 138 |
size_t n_cells; |
| 139 |
|
| 140 |
bool header; /* Whether to show the header row? */ |
| 141 |
bool vertical; /* Whether to field names are on the left rather than the first line */ |
| 142 |
|
| 143 |
TableErsatz ersatz; /* What to show when we have an empty cell or an invalid value that cannot be rendered. */ |
| 144 |
|
| 145 |
size_t width; /* If == 0 format this as wide as necessary. If SIZE_MAX format this to console |
| 146 |
* width or less wide, but not wider. Otherwise the width to format this table in. */ |
| 147 |
size_t cell_height_max; /* Maximum number of lines per cell. (If there are more, ellipsis is shown. If SIZE_MAX then no limit is set, the default. == 0 is not allowed.) */ |
| 148 |
|
| 149 |
TableData **data; |
| 150 |
|
| 151 |
size_t *display_map; /* List of columns to show (by their index). It's fine if columns are listed multiple times or not at all */ |
| 152 |
size_t n_display_map; |
| 153 |
|
| 154 |
size_t *sort_map; /* The columns to order rows by, in order of preference. */ |
| 155 |
size_t n_sort_map; |
| 156 |
|
| 157 |
char **json_fields; |
| 158 |
size_t n_json_fields; |
| 159 |
|
| 160 |
bool *reverse_map; |
| 161 |
}; |
| 162 |
|
| 163 |
Table* table_new_raw(size_t n_columns) { |
| 164 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 165 |
|
| 166 |
assert(n_columns > 0); |
| 167 |
|
| 168 |
t = new(Table, 1); |
| 169 |
if (!t) |
| 170 |
return NULL; |
| 171 |
|
| 172 |
*t = (Table) { |
| 173 |
.n_columns = n_columns, |
| 174 |
.header = true, |
| 175 |
.width = SIZE_MAX, |
| 176 |
.cell_height_max = SIZE_MAX, |
| 177 |
.ersatz = TABLE_ERSATZ_EMPTY, |
| 178 |
}; |
| 179 |
|
| 180 |
return TAKE_PTR(t); |
| 181 |
} |
| 182 |
|
| 183 |
Table* table_new_internal(const char *first_header, ...) { |
| 184 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 185 |
size_t n_columns = 1; |
| 186 |
va_list ap; |
| 187 |
int r; |
| 188 |
|
| 189 |
assert(first_header); |
| 190 |
|
| 191 |
va_start(ap, first_header); |
| 192 |
for (;;) { |
| 193 |
if (!va_arg(ap, const char*)) |
| 194 |
break; |
| 195 |
|
| 196 |
n_columns++; |
| 197 |
} |
| 198 |
va_end(ap); |
| 199 |
|
| 200 |
t = table_new_raw(n_columns); |
| 201 |
if (!t) |
| 202 |
return NULL; |
| 203 |
|
| 204 |
va_start(ap, first_header); |
| 205 |
for (const char *h = first_header; h; h = va_arg(ap, const char*)) { |
| 206 |
TableCell *cell; |
| 207 |
|
| 208 |
r = table_add_cell(t, &cell, TABLE_HEADER, h); |
| 209 |
if (r < 0) { |
| 210 |
va_end(ap); |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
} |
| 214 |
va_end(ap); |
| 215 |
|
| 216 |
assert(t->n_columns == t->n_cells); |
| 217 |
return TAKE_PTR(t); |
| 218 |
} |
| 219 |
|
| 220 |
Table* table_new_vertical(void) { |
| 221 |
_cleanup_(table_unrefp) Table *t = NULL; |
| 222 |
TableCell *cell; |
| 223 |
|
| 224 |
t = table_new_raw(2); |
| 225 |
if (!t) |
| 226 |
return NULL; |
| 227 |
|
| 228 |
t->vertical = true; |
| 229 |
t->header = false; |
| 230 |
|
| 231 |
if (table_add_cell(t, &cell, TABLE_HEADER, "key") < 0) |
| 232 |
return NULL; |
| 233 |
|
| 234 |
if (table_set_align_percent(t, cell, 100) < 0) |
| 235 |
return NULL; |
| 236 |
|
| 237 |
if (table_add_cell(t, &cell, TABLE_HEADER, "value") < 0) |
| 238 |
return NULL; |
| 239 |
|
| 240 |
if (table_set_align_percent(t, cell, 0) < 0) |
| 241 |
return NULL; |
| 242 |
|
| 243 |
return TAKE_PTR(t); |
| 244 |
} |
| 245 |
|
| 246 |
static TableData* table_data_free(TableData *d) { |
| 247 |
assert(d); |
| 248 |
|
| 249 |
free(d->formatted); |
| 250 |
free(d->url); |
| 251 |
|
| 252 |
if (IN_SET(d->type, TABLE_STRV, TABLE_STRV_WRAPPED)) |
| 253 |
strv_free(d->strv); |
| 254 |
|
| 255 |
if (d->type == TABLE_JSON) |
| 256 |
sd_json_variant_unref(d->json); |
| 257 |
|
| 258 |
return mfree(d); |
| 259 |
} |
| 260 |
|
| 261 |
DEFINE_PRIVATE_TRIVIAL_REF_UNREF_FUNC(TableData, table_data, table_data_free); |
| 262 |
DEFINE_TRIVIAL_CLEANUP_FUNC(TableData*, table_data_unref); |
| 263 |
|
| 264 |
Table* table_unref(Table *t) { |
| 265 |
if (!t) |
| 266 |
return NULL; |
| 267 |
|
| 268 |
for (size_t i = 0; i < t->n_cells; i++) |
| 269 |
table_data_unref(t->data[i]); |
| 270 |
|
| 271 |
free(t->data); |
| 272 |
free(t->display_map); |
| 273 |
free(t->sort_map); |
| 274 |
free(t->reverse_map); |
| 275 |
|
| 276 |
for (size_t i = 0; i < t->n_json_fields; i++) |
| 277 |
free(t->json_fields[i]); |
| 278 |
|
| 279 |
free(t->json_fields); |
| 280 |
|
| 281 |
return mfree(t); |
| 282 |
} |
| 283 |
|
| 284 |
static size_t table_data_size(TableDataType type, const void *data) { |
| 285 |
|
| 286 |
switch (type) { |
| 287 |
|
| 288 |
case TABLE_EMPTY: |
| 289 |
return 0; |
| 290 |
|
| 291 |
case TABLE_STRING: |
| 292 |
case TABLE_STRING_WITH_ANSI: |
| 293 |
case TABLE_PATH: |
| 294 |
case TABLE_PATH_BASENAME: |
| 295 |
case TABLE_FIELD: |
| 296 |
case TABLE_HEADER: |
| 297 |
case TABLE_VERSION: |
| 298 |
return strlen(data) + 1; |
| 299 |
|
| 300 |
case TABLE_STRV: |
| 301 |
case TABLE_STRV_WRAPPED: |
| 302 |
return sizeof(char **); |
| 303 |
|
| 304 |
case TABLE_BOOLEAN_CHECKMARK: |
| 305 |
case TABLE_BOOLEAN: |
| 306 |
return sizeof(bool); |
| 307 |
|
| 308 |
case TABLE_TIMESTAMP: |
| 309 |
case TABLE_TIMESTAMP_UTC: |
| 310 |
case TABLE_TIMESTAMP_RELATIVE: |
| 311 |
case TABLE_TIMESTAMP_RELATIVE_MONOTONIC: |
| 312 |
case TABLE_TIMESTAMP_LEFT: |
| 313 |
case TABLE_TIMESTAMP_DATE: |
| 314 |
case TABLE_TIMESPAN: |
| 315 |
case TABLE_TIMESPAN_MSEC: |
| 316 |
case TABLE_TIMESPAN_DAY: |
| 317 |
return sizeof(usec_t); |
| 318 |
|
| 319 |
case TABLE_SIZE: |
| 320 |
case TABLE_INT64: |
| 321 |
case TABLE_UINT64: |
| 322 |
case TABLE_UINT64_HEX: |
| 323 |
case TABLE_UINT64_HEX_0x: |
| 324 |
case TABLE_BPS: |
| 325 |
return sizeof(uint64_t); |
| 326 |
|
| 327 |
case TABLE_INT32: |
| 328 |
case TABLE_UINT32: |
| 329 |
case TABLE_UINT32_HEX: |
| 330 |
case TABLE_UINT32_HEX_0x: |
| 331 |
return sizeof(uint32_t); |
| 332 |
|
| 333 |
case TABLE_INT16: |
| 334 |
case TABLE_UINT16: |
| 335 |
return sizeof(uint16_t); |
| 336 |
|
| 337 |
case TABLE_INT8: |
| 338 |
case TABLE_UINT8: |
| 339 |
return sizeof(uint8_t); |
| 340 |
|
| 341 |
case TABLE_INT: |
| 342 |
case TABLE_UINT: |
| 343 |
case TABLE_PERCENT: |
| 344 |
case TABLE_IFINDEX: |
| 345 |
case TABLE_SIGNAL: |
| 346 |
case TABLE_TRISTATE: |
| 347 |
return sizeof(int); |
| 348 |
|
| 349 |
case TABLE_IN_ADDR: |
| 350 |
return sizeof(struct in_addr); |
| 351 |
|
| 352 |
case TABLE_IN6_ADDR: |
| 353 |
return sizeof(struct in6_addr); |
| 354 |
|
| 355 |
case TABLE_UUID: |
| 356 |
case TABLE_ID128: |
| 357 |
return sizeof(sd_id128_t); |
| 358 |
|
| 359 |
case TABLE_UID: |
| 360 |
return sizeof(uid_t); |
| 361 |
case TABLE_GID: |
| 362 |
return sizeof(gid_t); |
| 363 |
case TABLE_PID: |
| 364 |
return sizeof(pid_t); |
| 365 |
|
| 366 |
case TABLE_MODE: |
| 367 |
case TABLE_MODE_INODE_TYPE: |
| 368 |
return sizeof(mode_t); |
| 369 |
|
| 370 |
case TABLE_DEVNUM: |
| 371 |
return sizeof(dev_t); |
| 372 |
|
| 373 |
case TABLE_JSON: |
| 374 |
return sizeof(sd_json_variant*); |
| 375 |
|
| 376 |
default: |
| 377 |
assert_not_reached(); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
static bool table_data_matches( |
| 382 |
TableData *d, |
| 383 |
TableDataType type, |
| 384 |
const void *data, |
| 385 |
size_t minimum_width, |
| 386 |
size_t maximum_width, |
| 387 |
unsigned weight, |
| 388 |
unsigned align_percent, |
| 389 |
unsigned ellipsize_percent, |
| 390 |
bool uppercase) { |
| 391 |
|
| 392 |
size_t k, l; |
| 393 |
assert(d); |
| 394 |
|
| 395 |
if (d->type != type) |
| 396 |
return false; |
| 397 |
|
| 398 |
if (d->minimum_width != minimum_width) |
| 399 |
return false; |
| 400 |
|
| 401 |
if (d->maximum_width != maximum_width) |
| 402 |
return false; |
| 403 |
|
| 404 |
if (d->weight != weight) |
| 405 |
return false; |
| 406 |
|
| 407 |
if (d->align_percent != align_percent) |
| 408 |
return false; |
| 409 |
|
| 410 |
if (d->ellipsize_percent != ellipsize_percent) |
| 411 |
return false; |
| 412 |
|
| 413 |
if (d->uppercase != uppercase) |
| 414 |
return false; |
| 415 |
|
| 416 |
/* If a color/url is set, refuse to merge */ |
| 417 |
if (d->color || d->rgap_color || d->underline || d->rgap_underline) |
| 418 |
return false; |
| 419 |
if (d->url) |
| 420 |
return false; |
| 421 |
|
| 422 |
k = table_data_size(type, data); |
| 423 |
l = table_data_size(d->type, d->data); |
| 424 |
if (k != l) |
| 425 |
return false; |
| 426 |
|
| 427 |
return memcmp_safe(data, d->data, l) == 0; |
| 428 |
} |
| 429 |
|
| 430 |
static TableData* table_data_new( |
| 431 |
TableDataType type, |
| 432 |
const void *data, |
| 433 |
size_t minimum_width, |
| 434 |
size_t maximum_width, |
| 435 |
unsigned weight, |
| 436 |
unsigned align_percent, |
| 437 |
unsigned ellipsize_percent, |
| 438 |
bool uppercase) { |
| 439 |
|
| 440 |
_cleanup_free_ TableData *d = NULL; |
| 441 |
size_t data_size; |
| 442 |
|
| 443 |
data_size = table_data_size(type, data); |
| 444 |
|
| 445 |
d = malloc0(offsetof(TableData, data) + data_size); |
| 446 |
if (!d) |
| 447 |
return NULL; |
| 448 |
|
| 449 |
d->n_ref = 1; |
| 450 |
d->type = type; |
| 451 |
d->minimum_width = minimum_width; |
| 452 |
d->maximum_width = maximum_width; |
| 453 |
d->weight = weight; |
| 454 |
d->align_percent = align_percent; |
| 455 |
d->ellipsize_percent = ellipsize_percent; |
| 456 |
d->uppercase = uppercase; |
| 457 |
|
| 458 |
switch (type) { |
| 459 |
|
| 460 |
case TABLE_STRV: |
| 461 |
case TABLE_STRV_WRAPPED: |
| 462 |
d->strv = strv_copy(data); |
| 463 |
if (!d->strv) |
| 464 |
return NULL; |
| 465 |
break; |
| 466 |
|
| 467 |
case TABLE_JSON: |
| 468 |
d->json = sd_json_variant_ref((sd_json_variant*) data); |
| 469 |
break; |
| 470 |
|
| 471 |
default: |
| 472 |
memcpy_safe(d->data, data, data_size); |
| 473 |
} |
| 474 |
|
| 475 |
return TAKE_PTR(d); |
| 476 |
} |
| 477 |
|
| 478 |
int table_add_cell_full( |
| 479 |
Table *t, |
| 480 |
TableCell **ret_cell, |
| 481 |
TableDataType dt, |
| 482 |
const void *data, |
| 483 |
size_t minimum_width, |
| 484 |
size_t maximum_width, |
| 485 |
unsigned weight, |
| 486 |
unsigned align_percent, |
| 487 |
unsigned ellipsize_percent) { |
| 488 |
|
| 489 |
_cleanup_(table_data_unrefp) TableData *d = NULL; |
| 490 |
bool uppercase; |
| 491 |
TableData *p; |
| 492 |
|
| 493 |
assert(t); |
| 494 |
assert(dt >= 0); |
| 495 |
assert(dt < _TABLE_DATA_TYPE_MAX); |
| 496 |
|
| 497 |
/* Special rule: patch NULL data fields to the empty field */ |
| 498 |
if (!data) |
| 499 |
dt = TABLE_EMPTY; |
| 500 |
|
| 501 |
/* Determine the cell adjacent to the current one, but one row up */ |
| 502 |
if (t->n_cells >= t->n_columns) |
| 503 |
assert_se(p = t->data[t->n_cells - t->n_columns]); |
| 504 |
else |
| 505 |
p = NULL; |
| 506 |
|
| 507 |
/* If formatting parameters are left unspecified, copy from the previous row */ |
| 508 |
if (minimum_width == SIZE_MAX) |
| 509 |
minimum_width = p ? p->minimum_width : 1; |
| 510 |
|
| 511 |
if (weight == UINT_MAX) |
| 512 |
weight = p ? p->weight : DEFAULT_WEIGHT; |
| 513 |
|
| 514 |
if (align_percent == UINT_MAX) |
| 515 |
align_percent = p ? p->align_percent : 0; |
| 516 |
|
| 517 |
if (ellipsize_percent == UINT_MAX) |
| 518 |
ellipsize_percent = p ? p->ellipsize_percent : 100; |
| 519 |
|
| 520 |
assert(align_percent <= 100); |
| 521 |
assert(ellipsize_percent <= 100); |
| 522 |
|
| 523 |
uppercase = dt == TABLE_HEADER; |
| 524 |
|
| 525 |
/* Small optimization: Pretty often adjacent cells in two subsequent lines have the same data and |
| 526 |
* formatting. Let's see if we can reuse the cell data and ref it once more. */ |
| 527 |
|
| 528 |
if (p && table_data_matches(p, dt, data, minimum_width, maximum_width, weight, align_percent, ellipsize_percent, uppercase)) |
| 529 |
d = table_data_ref(p); |
| 530 |
else { |
| 531 |
d = table_data_new(dt, data, minimum_width, maximum_width, weight, align_percent, ellipsize_percent, uppercase); |
| 532 |
if (!d) |
| 533 |
return -ENOMEM; |
| 534 |
} |
| 535 |
|
| 536 |
if (!GREEDY_REALLOC(t->data, MAX(t->n_cells + 1, t->n_columns))) |
| 537 |
return -ENOMEM; |
| 538 |
|
| 539 |
if (ret_cell) |
| 540 |
*ret_cell = TABLE_INDEX_TO_CELL(t->n_cells); |
| 541 |
|
| 542 |
t->data[t->n_cells++] = TAKE_PTR(d); |
| 543 |
|
| 544 |
return 0; |
| 545 |
} |
| 546 |
|
| 547 |
int table_add_cell_stringf_full(Table *t, TableCell **ret_cell, TableDataType dt, const char *format, ...) { |
| 548 |
_cleanup_free_ char *buffer = NULL; |
| 549 |
va_list ap; |
| 550 |
int r; |
| 551 |
|
| 552 |
assert(t); |
| 553 |
assert(IN_SET(dt, TABLE_STRING, TABLE_STRING_WITH_ANSI, TABLE_PATH, TABLE_PATH_BASENAME, TABLE_FIELD, TABLE_HEADER, TABLE_VERSION)); |
| 554 |
|
| 555 |
va_start(ap, format); |
| 556 |
r = vasprintf(&buffer, format, ap); |
| 557 |
va_end(ap); |
| 558 |
if (r < 0) |
| 559 |
return -ENOMEM; |
| 560 |
|
| 561 |
return table_add_cell(t, ret_cell, dt, buffer); |
| 562 |
} |
| 563 |
|
| 564 |
int table_fill_empty(Table *t, size_t until_column) { |
| 565 |
int r; |
| 566 |
|
| 567 |
assert(t); |
| 568 |
|
| 569 |
/* Fill the rest of the current line with empty cells until we reach the specified column. Will add |
| 570 |
* at least one cell. Pass 0 in order to fill a line to the end or insert an empty line. */ |
| 571 |
|
| 572 |
if (until_column >= t->n_columns) |
| 573 |
return -EINVAL; |
| 574 |
|
| 575 |
do { |
| 576 |
r = table_add_cell(t, NULL, TABLE_EMPTY, NULL); |
| 577 |
if (r < 0) |
| 578 |
return r; |
| 579 |
|
| 580 |
} while ((t->n_cells % t->n_columns) != until_column); |
| 581 |
|
| 582 |
return 0; |
| 583 |
} |
| 584 |
|
| 585 |
int table_dup_cell(Table *t, TableCell *cell) { |
| 586 |
size_t i; |
| 587 |
|
| 588 |
assert(t); |
| 589 |
|
| 590 |
/* Add the data of the specified cell a second time as a new cell to the end. */ |
| 591 |
|
| 592 |
i = TABLE_CELL_TO_INDEX(cell); |
| 593 |
if (i >= t->n_cells) |
| 594 |
return -ENXIO; |
| 595 |
|
| 596 |
if (!GREEDY_REALLOC(t->data, MAX(t->n_cells + 1, t->n_columns))) |
| 597 |
return -ENOMEM; |
| 598 |
|
| 599 |
t->data[t->n_cells++] = table_data_ref(t->data[i]); |
| 600 |
return 0; |
| 601 |
} |
| 602 |
|
| 603 |
static int table_dedup_cell(Table *t, TableCell *cell) { |
| 604 |
_cleanup_free_ char *curl = NULL; |
| 605 |
TableData *nd, *od; |
| 606 |
size_t i; |
| 607 |
|
| 608 |
assert(t); |
| 609 |
|
| 610 |
/* Helper call that ensures the specified cell's data object has a ref count of 1, which we can use before |
| 611 |
* changing a cell's formatting without effecting every other cell's formatting that shares the same data */ |
| 612 |
|
| 613 |
i = TABLE_CELL_TO_INDEX(cell); |
| 614 |
if (i >= t->n_cells) |
| 615 |
return -ENXIO; |
| 616 |
|
| 617 |
assert_se(od = t->data[i]); |
| 618 |
if (od->n_ref == 1) |
| 619 |
return 0; |
| 620 |
|
| 621 |
assert(od->n_ref > 1); |
| 622 |
|
| 623 |
if (od->url) { |
| 624 |
curl = strdup(od->url); |
| 625 |
if (!curl) |
| 626 |
return -ENOMEM; |
| 627 |
} |
| 628 |
|
| 629 |
nd = table_data_new( |
| 630 |
od->type, |
| 631 |
od->data, |
| 632 |
od->minimum_width, |
| 633 |
od->maximum_width, |
| 634 |
od->weight, |
| 635 |
od->align_percent, |
| 636 |
od->ellipsize_percent, |
| 637 |
od->uppercase); |
| 638 |
if (!nd) |
| 639 |
return -ENOMEM; |
| 640 |
|
| 641 |
nd->color = od->color; |
| 642 |
nd->rgap_color = od->rgap_color; |
| 643 |
nd->underline = od->underline; |
| 644 |
nd->rgap_underline = od->rgap_underline; |
| 645 |
nd->url = TAKE_PTR(curl); |
| 646 |
|
| 647 |
table_data_unref(od); |
| 648 |
t->data[i] = nd; |
| 649 |
|
| 650 |
assert(nd->n_ref == 1); |
| 651 |
|
| 652 |
return 1; |
| 653 |
} |
| 654 |
|
| 655 |
static TableData *table_get_data(Table *t, TableCell *cell) { |
| 656 |
size_t i; |
| 657 |
|
| 658 |
assert(t); |
| 659 |
assert(cell); |
| 660 |
|
| 661 |
/* Get the data object of the specified cell, or NULL if it doesn't exist */ |
| 662 |
|
| 663 |
i = TABLE_CELL_TO_INDEX(cell); |
| 664 |
if (i >= t->n_cells) |
| 665 |
return NULL; |
| 666 |
|
| 667 |
assert(t->data[i]); |
| 668 |
assert(t->data[i]->n_ref > 0); |
| 669 |
|
| 670 |
return t->data[i]; |
| 671 |
} |
| 672 |
|
| 673 |
int table_set_minimum_width(Table *t, TableCell *cell, size_t minimum_width) { |
| 674 |
int r; |
| 675 |
|
| 676 |
assert(t); |
| 677 |
assert(cell); |
| 678 |
|
| 679 |
if (minimum_width == SIZE_MAX) |
| 680 |
minimum_width = 1; |
| 681 |
|
| 682 |
r = table_dedup_cell(t, cell); |
| 683 |
if (r < 0) |
| 684 |
return r; |
| 685 |
|
| 686 |
table_get_data(t, cell)->minimum_width = minimum_width; |
| 687 |
return 0; |
| 688 |
} |
| 689 |
|
| 690 |
int table_set_maximum_width(Table *t, TableCell *cell, size_t maximum_width) { |
| 691 |
int r; |
| 692 |
|
| 693 |
assert(t); |
| 694 |
assert(cell); |
| 695 |
|
| 696 |
r = table_dedup_cell(t, cell); |
| 697 |
if (r < 0) |
| 698 |
return r; |
| 699 |
|
| 700 |
table_get_data(t, cell)->maximum_width = maximum_width; |
| 701 |
return 0; |
| 702 |
} |
| 703 |
|
| 704 |
int table_set_weight(Table *t, TableCell *cell, unsigned weight) { |
| 705 |
int r; |
| 706 |
|
| 707 |
assert(t); |
| 708 |
assert(cell); |
| 709 |
|
| 710 |
if (weight == UINT_MAX) |
| 711 |
weight = DEFAULT_WEIGHT; |
| 712 |
|
| 713 |
r = table_dedup_cell(t, cell); |
| 714 |
if (r < 0) |
| 715 |
return r; |
| 716 |
|
| 717 |
table_get_data(t, cell)->weight = weight; |
| 718 |
return 0; |
| 719 |
} |
| 720 |
|
| 721 |
int table_set_align_percent(Table *t, TableCell *cell, unsigned percent) { |
| 722 |
int r; |
| 723 |
|
| 724 |
assert(t); |
| 725 |
assert(cell); |
| 726 |
|
| 727 |
if (percent == UINT_MAX) |
| 728 |
percent = 0; |
| 729 |
|
| 730 |
assert(percent <= 100); |
| 731 |
|
| 732 |
r = table_dedup_cell(t, cell); |
| 733 |
if (r < 0) |
| 734 |
return r; |
| 735 |
|
| 736 |
table_get_data(t, cell)->align_percent = percent; |
| 737 |
return 0; |
| 738 |
} |
| 739 |
|
| 740 |
int table_set_ellipsize_percent(Table *t, TableCell *cell, unsigned percent) { |
| 741 |
int r; |
| 742 |
|
| 743 |
assert(t); |
| 744 |
assert(cell); |
| 745 |
|
| 746 |
if (percent == UINT_MAX) |
| 747 |
percent = 100; |
| 748 |
|
| 749 |
assert(percent <= 100); |
| 750 |
|
| 751 |
r = table_dedup_cell(t, cell); |
| 752 |
if (r < 0) |
| 753 |
return r; |
| 754 |
|
| 755 |
table_get_data(t, cell)->ellipsize_percent = percent; |
| 756 |
return 0; |
| 757 |
} |
| 758 |
|
| 759 |
int table_set_color(Table *t, TableCell *cell, const char *color) { |
| 760 |
int r; |
| 761 |
|
| 762 |
assert(t); |
| 763 |
assert(cell); |
| 764 |
|
| 765 |
r = table_dedup_cell(t, cell); |
| 766 |
if (r < 0) |
| 767 |
return r; |
| 768 |
|
| 769 |
table_get_data(t, cell)->color = empty_to_null(color); |
| 770 |
return 0; |
| 771 |
} |
| 772 |
|
| 773 |
int table_set_rgap_color(Table *t, TableCell *cell, const char *color) { |
| 774 |
int r; |
| 775 |
|
| 776 |
assert(t); |
| 777 |
assert(cell); |
| 778 |
|
| 779 |
r = table_dedup_cell(t, cell); |
| 780 |
if (r < 0) |
| 781 |
return r; |
| 782 |
|
| 783 |
table_get_data(t, cell)->rgap_color = empty_to_null(color); |
| 784 |
return 0; |
| 785 |
} |
| 786 |
|
| 787 |
int table_set_underline(Table *t, TableCell *cell, bool b) { |
| 788 |
TableData *d; |
| 789 |
int r; |
| 790 |
|
| 791 |
assert(t); |
| 792 |
assert(cell); |
| 793 |
|
| 794 |
r = table_dedup_cell(t, cell); |
| 795 |
if (r < 0) |
| 796 |
return r; |
| 797 |
|
| 798 |
assert_se(d = table_get_data(t, cell)); |
| 799 |
|
| 800 |
if (d->underline == b) |
| 801 |
return 0; |
| 802 |
|
| 803 |
d->underline = b; |
| 804 |
return 1; |
| 805 |
} |
| 806 |
|
| 807 |
int table_set_rgap_underline(Table *t, TableCell *cell, bool b) { |
| 808 |
TableData *d; |
| 809 |
int r; |
| 810 |
|
| 811 |
assert(t); |
| 812 |
assert(cell); |
| 813 |
|
| 814 |
r = table_dedup_cell(t, cell); |
| 815 |
if (r < 0) |
| 816 |
return r; |
| 817 |
|
| 818 |
assert_se(d = table_get_data(t, cell)); |
| 819 |
|
| 820 |
if (d->rgap_underline == b) |
| 821 |
return 0; |
| 822 |
|
| 823 |
d->rgap_underline = b; |
| 824 |
return 1; |
| 825 |
} |
| 826 |
|
| 827 |
int table_set_url(Table *t, TableCell *cell, const char *url) { |
| 828 |
_cleanup_free_ char *copy = NULL; |
| 829 |
int r; |
| 830 |
|
| 831 |
assert(t); |
| 832 |
assert(cell); |
| 833 |
|
| 834 |
if (url) { |
| 835 |
copy = strdup(url); |
| 836 |
if (!copy) |
| 837 |
return -ENOMEM; |
| 838 |
} |
| 839 |
|
| 840 |
r = table_dedup_cell(t, cell); |
| 841 |
if (r < 0) |
| 842 |
return r; |
| 843 |
|
| 844 |
return free_and_replace(table_get_data(t, cell)->url, copy); |
| 845 |
} |
| 846 |
|
| 847 |
int table_set_uppercase(Table *t, TableCell *cell, bool b) { |
| 848 |
TableData *d; |
| 849 |
int r; |
| 850 |
|
| 851 |
assert(t); |
| 852 |
assert(cell); |
| 853 |
|
| 854 |
r = table_dedup_cell(t, cell); |
| 855 |
if (r < 0) |
| 856 |
return r; |
| 857 |
|
| 858 |
assert_se(d = table_get_data(t, cell)); |
| 859 |
|
| 860 |
if (d->uppercase == b) |
| 861 |
return 0; |
| 862 |
|
| 863 |
d->formatted = mfree(d->formatted); |
| 864 |
d->uppercase = b; |
| 865 |
return 1; |
| 866 |
} |
| 867 |
|
| 868 |
int table_update(Table *t, TableCell *cell, TableDataType type, const void *data) { |
| 869 |
_cleanup_free_ char *curl = NULL; |
| 870 |
TableData *nd, *od; |
| 871 |
size_t i; |
| 872 |
|
| 873 |
assert(t); |
| 874 |
assert(cell); |
| 875 |
|
| 876 |
i = TABLE_CELL_TO_INDEX(cell); |
| 877 |
if (i >= t->n_cells) |
| 878 |
return -ENXIO; |
| 879 |
|
| 880 |
assert_se(od = t->data[i]); |
| 881 |
|
| 882 |
if (od->url) { |
| 883 |
curl = strdup(od->url); |
| 884 |
if (!curl) |
| 885 |
return -ENOMEM; |
| 886 |
} |
| 887 |
|
| 888 |
nd = table_data_new( |
| 889 |
type, |
| 890 |
data, |
| 891 |
od->minimum_width, |
| 892 |
od->maximum_width, |
| 893 |
od->weight, |
| 894 |
od->align_percent, |
| 895 |
od->ellipsize_percent, |
| 896 |
od->uppercase); |
| 897 |
if (!nd) |
| 898 |
return -ENOMEM; |
| 899 |
|
| 900 |
nd->color = od->color; |
| 901 |
nd->rgap_color = od->rgap_color; |
| 902 |
nd->underline = od->underline; |
| 903 |
nd->rgap_underline = od->rgap_underline; |
| 904 |
nd->url = TAKE_PTR(curl); |
| 905 |
|
| 906 |
table_data_unref(od); |
| 907 |
t->data[i] = nd; |
| 908 |
|
| 909 |
return 0; |
| 910 |
} |
| 911 |
|
| 912 |
int table_add_many_internal(Table *t, TableDataType first_type, ...) { |
| 913 |
TableCell *last_cell = NULL; |
| 914 |
va_list ap; |
| 915 |
int r; |
| 916 |
|
| 917 |
assert(t); |
| 918 |
assert(first_type >= 0); |
| 919 |
assert(first_type < _TABLE_DATA_TYPE_MAX); |
| 920 |
|
| 921 |
va_start(ap, first_type); |
| 922 |
|
| 923 |
for (TableDataType type = first_type;; type = va_arg(ap, TableDataType)) { |
| 924 |
const void *data; |
| 925 |
union { |
| 926 |
uint64_t size; |
| 927 |
usec_t usec; |
| 928 |
int int_val; |
| 929 |
int8_t int8; |
| 930 |
int16_t int16; |
| 931 |
int32_t int32; |
| 932 |
int64_t int64; |
| 933 |
unsigned uint_val; |
| 934 |
uint8_t uint8; |
| 935 |
uint16_t uint16; |
| 936 |
uint32_t uint32; |
| 937 |
uint64_t uint64; |
| 938 |
int percent; |
| 939 |
int ifindex; |
| 940 |
int tristate; |
| 941 |
bool b; |
| 942 |
union in_addr_union address; |
| 943 |
sd_id128_t id128; |
| 944 |
uid_t uid; |
| 945 |
gid_t gid; |
| 946 |
pid_t pid; |
| 947 |
mode_t mode; |
| 948 |
dev_t devnum; |
| 949 |
} buffer; |
| 950 |
|
| 951 |
switch (type) { |
| 952 |
|
| 953 |
case TABLE_EMPTY: |
| 954 |
data = NULL; |
| 955 |
break; |
| 956 |
|
| 957 |
case TABLE_STRING: |
| 958 |
case TABLE_STRING_WITH_ANSI: |
| 959 |
case TABLE_PATH: |
| 960 |
case TABLE_PATH_BASENAME: |
| 961 |
case TABLE_FIELD: |
| 962 |
case TABLE_HEADER: |
| 963 |
case TABLE_VERSION: |
| 964 |
data = va_arg(ap, const char *); |
| 965 |
break; |
| 966 |
|
| 967 |
case TABLE_STRV: |
| 968 |
case TABLE_STRV_WRAPPED: |
| 969 |
data = va_arg(ap, char * const *); |
| 970 |
break; |
| 971 |
|
| 972 |
case TABLE_BOOLEAN_CHECKMARK: |
| 973 |
case TABLE_BOOLEAN: |
| 974 |
buffer.b = va_arg(ap, int); |
| 975 |
data = &buffer.b; |
| 976 |
break; |
| 977 |
|
| 978 |
case TABLE_TRISTATE: |
| 979 |
buffer.tristate = va_arg(ap, int); |
| 980 |
data = &buffer.tristate; |
| 981 |
break; |
| 982 |
|
| 983 |
case TABLE_TIMESTAMP: |
| 984 |
case TABLE_TIMESTAMP_UTC: |
| 985 |
case TABLE_TIMESTAMP_RELATIVE: |
| 986 |
case TABLE_TIMESTAMP_RELATIVE_MONOTONIC: |
| 987 |
case TABLE_TIMESTAMP_LEFT: |
| 988 |
case TABLE_TIMESTAMP_DATE: |
| 989 |
case TABLE_TIMESPAN: |
| 990 |
case TABLE_TIMESPAN_MSEC: |
| 991 |
case TABLE_TIMESPAN_DAY: |
| 992 |
buffer.usec = va_arg(ap, usec_t); |
| 993 |
data = &buffer.usec; |
| 994 |
break; |
| 995 |
|
| 996 |
case TABLE_SIZE: |
| 997 |
case TABLE_BPS: |
| 998 |
buffer.size = va_arg(ap, uint64_t); |
| 999 |
data = &buffer.size; |
| 1000 |
break; |
| 1001 |
|
| 1002 |
case TABLE_INT: |
| 1003 |
case TABLE_SIGNAL: |
| 1004 |
buffer.int_val = va_arg(ap, int); |
| 1005 |
data = &buffer.int_val; |
| 1006 |
break; |
| 1007 |
|
| 1008 |
case TABLE_INT8: { |
| 1009 |
int x = va_arg(ap, int); |
| 1010 |
assert(x >= INT8_MIN && x <= INT8_MAX); |
| 1011 |
|
| 1012 |
buffer.int8 = x; |
| 1013 |
data = &buffer.int8; |
| 1014 |
break; |
| 1015 |
} |
| 1016 |
|
| 1017 |
case TABLE_INT16: { |
| 1018 |
int x = va_arg(ap, int); |
| 1019 |
assert(x >= INT16_MIN && x <= INT16_MAX); |
| 1020 |
|
| 1021 |
buffer.int16 = x; |
| 1022 |
data = &buffer.int16; |
| 1023 |
break; |
| 1024 |
} |
| 1025 |
|
| 1026 |
case TABLE_INT32: |
| 1027 |
buffer.int32 = va_arg(ap, int32_t); |
| 1028 |
data = &buffer.int32; |
| 1029 |
break; |
| 1030 |
|
| 1031 |
case TABLE_INT64: |
| 1032 |
buffer.int64 = va_arg(ap, int64_t); |
| 1033 |
data = &buffer.int64; |
| 1034 |
break; |
| 1035 |
|
| 1036 |
case TABLE_UINT: |
| 1037 |
buffer.uint_val = va_arg(ap, unsigned); |
| 1038 |
data = &buffer.uint_val; |
| 1039 |
break; |
| 1040 |
|
| 1041 |
case TABLE_UINT8: { |
| 1042 |
unsigned x = va_arg(ap, unsigned); |
| 1043 |
assert(x <= UINT8_MAX); |
| 1044 |
|
| 1045 |
buffer.uint8 = x; |
| 1046 |
data = &buffer.uint8; |
| 1047 |
break; |
| 1048 |
} |
| 1049 |
|
| 1050 |
case TABLE_UINT16: { |
| 1051 |
unsigned x = va_arg(ap, unsigned); |
| 1052 |
assert(x <= UINT16_MAX); |
| 1053 |
|
| 1054 |
buffer.uint16 = x; |
| 1055 |
data = &buffer.uint16; |
| 1056 |
break; |
| 1057 |
} |
| 1058 |
|
| 1059 |
case TABLE_UINT32: |
| 1060 |
case TABLE_UINT32_HEX: |
| 1061 |
case TABLE_UINT32_HEX_0x: |
| 1062 |
buffer.uint32 = va_arg(ap, uint32_t); |
| 1063 |
data = &buffer.uint32; |
| 1064 |
break; |
| 1065 |
|
| 1066 |
case TABLE_UINT64: |
| 1067 |
case TABLE_UINT64_HEX: |
| 1068 |
case TABLE_UINT64_HEX_0x: |
| 1069 |
buffer.uint64 = va_arg(ap, uint64_t); |
| 1070 |
data = &buffer.uint64; |
| 1071 |
break; |
| 1072 |
|
| 1073 |
case TABLE_PERCENT: |
| 1074 |
buffer.percent = va_arg(ap, int); |
| 1075 |
data = &buffer.percent; |
| 1076 |
break; |
| 1077 |
|
| 1078 |
case TABLE_IFINDEX: |
| 1079 |
buffer.ifindex = va_arg(ap, int); |
| 1080 |
data = &buffer.ifindex; |
| 1081 |
break; |
| 1082 |
|
| 1083 |
case TABLE_IN_ADDR: |
| 1084 |
buffer.address.in = *va_arg(ap, struct in_addr *); |
| 1085 |
data = &buffer.address.in; |
| 1086 |
break; |
| 1087 |
|
| 1088 |
case TABLE_IN6_ADDR: |
| 1089 |
buffer.address.in6 = *va_arg(ap, struct in6_addr *); |
| 1090 |
data = &buffer.address.in6; |
| 1091 |
break; |
| 1092 |
|
| 1093 |
case TABLE_UUID: |
| 1094 |
case TABLE_ID128: |
| 1095 |
buffer.id128 = va_arg(ap, sd_id128_t); |
| 1096 |
data = &buffer.id128; |
| 1097 |
break; |
| 1098 |
|
| 1099 |
case TABLE_UID: |
| 1100 |
buffer.uid = va_arg(ap, uid_t); |
| 1101 |
data = &buffer.uid; |
| 1102 |
break; |
| 1103 |
|
| 1104 |
case TABLE_GID: |
| 1105 |
buffer.gid = va_arg(ap, gid_t); |
| 1106 |
data = &buffer.gid; |
| 1107 |
break; |
| 1108 |
|
| 1109 |
case TABLE_PID: |
| 1110 |
buffer.pid = va_arg(ap, pid_t); |
| 1111 |
data = &buffer.pid; |
| 1112 |
break; |
| 1113 |
|
| 1114 |
case TABLE_MODE: |
| 1115 |
case TABLE_MODE_INODE_TYPE: |
| 1116 |
buffer.mode = va_arg(ap, mode_t); |
| 1117 |
data = &buffer.mode; |
| 1118 |
break; |
| 1119 |
|
| 1120 |
case TABLE_DEVNUM: |
| 1121 |
buffer.devnum = va_arg(ap, dev_t); |
| 1122 |
data = &buffer.devnum; |
| 1123 |
break; |
| 1124 |
|
| 1125 |
case TABLE_JSON: |
| 1126 |
data = va_arg(ap, sd_json_variant*); |
| 1127 |
break; |
| 1128 |
|
| 1129 |
case TABLE_SET_MINIMUM_WIDTH: { |
| 1130 |
size_t w = va_arg(ap, size_t); |
| 1131 |
|
| 1132 |
r = table_set_minimum_width(t, last_cell, w); |
| 1133 |
goto check; |
| 1134 |
} |
| 1135 |
|
| 1136 |
case TABLE_SET_MAXIMUM_WIDTH: { |
| 1137 |
size_t w = va_arg(ap, size_t); |
| 1138 |
r = table_set_maximum_width(t, last_cell, w); |
| 1139 |
goto check; |
| 1140 |
} |
| 1141 |
|
| 1142 |
case TABLE_SET_WEIGHT: { |
| 1143 |
unsigned w = va_arg(ap, unsigned); |
| 1144 |
r = table_set_weight(t, last_cell, w); |
| 1145 |
goto check; |
| 1146 |
} |
| 1147 |
|
| 1148 |
case TABLE_SET_ALIGN_PERCENT: { |
| 1149 |
unsigned p = va_arg(ap, unsigned); |
| 1150 |
r = table_set_align_percent(t, last_cell, p); |
| 1151 |
goto check; |
| 1152 |
} |
| 1153 |
|
| 1154 |
case TABLE_SET_ELLIPSIZE_PERCENT: { |
| 1155 |
unsigned p = va_arg(ap, unsigned); |
| 1156 |
r = table_set_ellipsize_percent(t, last_cell, p); |
| 1157 |
goto check; |
| 1158 |
} |
| 1159 |
|
| 1160 |
case TABLE_SET_COLOR: { |
| 1161 |
const char *c = va_arg(ap, const char*); |
| 1162 |
r = table_set_color(t, last_cell, c); |
| 1163 |
goto check; |
| 1164 |
} |
| 1165 |
|
| 1166 |
case TABLE_SET_RGAP_COLOR: { |
| 1167 |
const char *c = va_arg(ap, const char*); |
| 1168 |
r = table_set_rgap_color(t, last_cell, c); |
| 1169 |
goto check; |
| 1170 |
} |
| 1171 |
|
| 1172 |
case TABLE_SET_BOTH_COLORS: { |
| 1173 |
const char *c = va_arg(ap, const char*); |
| 1174 |
|
| 1175 |
r = table_set_color(t, last_cell, c); |
| 1176 |
if (r < 0) { |
| 1177 |
va_end(ap); |
| 1178 |
return r; |
| 1179 |
} |
| 1180 |
|
| 1181 |
r = table_set_rgap_color(t, last_cell, c); |
| 1182 |
goto check; |
| 1183 |
} |
| 1184 |
|
| 1185 |
case TABLE_SET_UNDERLINE: { |
| 1186 |
int u = va_arg(ap, int); |
| 1187 |
r = table_set_underline(t, last_cell, u); |
| 1188 |
goto check; |
| 1189 |
} |
| 1190 |
|
| 1191 |
case TABLE_SET_RGAP_UNDERLINE: { |
| 1192 |
int u = va_arg(ap, int); |
| 1193 |
r = table_set_rgap_underline(t, last_cell, u); |
| 1194 |
goto check; |
| 1195 |
} |
| 1196 |
|
| 1197 |
case TABLE_SET_BOTH_UNDERLINES: { |
| 1198 |
int u = va_arg(ap, int); |
| 1199 |
|
| 1200 |
r = table_set_underline(t, last_cell, u); |
| 1201 |
if (r < 0) { |
| 1202 |
va_end(ap); |
| 1203 |
return r; |
| 1204 |
} |
| 1205 |
|
| 1206 |
r = table_set_rgap_underline(t, last_cell, u); |
| 1207 |
goto check; |
| 1208 |
} |
| 1209 |
|
| 1210 |
case TABLE_SET_URL: { |
| 1211 |
const char *u = va_arg(ap, const char*); |
| 1212 |
r = table_set_url(t, last_cell, u); |
| 1213 |
goto check; |
| 1214 |
} |
| 1215 |
|
| 1216 |
case TABLE_SET_UPPERCASE: { |
| 1217 |
int u = va_arg(ap, int); |
| 1218 |
r = table_set_uppercase(t, last_cell, u); |
| 1219 |
goto check; |
| 1220 |
} |
| 1221 |
|
| 1222 |
case TABLE_SET_JSON_FIELD_NAME: { |
| 1223 |
const char *n = va_arg(ap, const char*); |
| 1224 |
size_t idx; |
| 1225 |
if (t->vertical) { |
| 1226 |
assert(TABLE_CELL_TO_INDEX(last_cell) >= t->n_columns); |
| 1227 |
idx = TABLE_CELL_TO_INDEX(last_cell) / t->n_columns - 1; |
| 1228 |
} else { |
| 1229 |
idx = TABLE_CELL_TO_INDEX(last_cell); |
| 1230 |
assert(idx < t->n_columns); |
| 1231 |
} |
| 1232 |
r = table_set_json_field_name(t, idx, n); |
| 1233 |
goto check; |
| 1234 |
} |
| 1235 |
|
| 1236 |
case _TABLE_DATA_TYPE_MAX: |
| 1237 |
/* Used as end marker */ |
| 1238 |
va_end(ap); |
| 1239 |
return 0; |
| 1240 |
|
| 1241 |
default: |
| 1242 |
assert_not_reached(); |
| 1243 |
} |
| 1244 |
|
| 1245 |
r = table_add_cell(t, &last_cell, type, data); |
| 1246 |
check: |
| 1247 |
if (r < 0) { |
| 1248 |
va_end(ap); |
| 1249 |
return r; |
| 1250 |
} |
| 1251 |
} |
| 1252 |
} |
| 1253 |
|
| 1254 |
void table_set_header(Table *t, bool b) { |
| 1255 |
assert(t); |
| 1256 |
|
| 1257 |
t->header = b; |
| 1258 |
} |
| 1259 |
|
| 1260 |
void table_set_width(Table *t, size_t width) { |
| 1261 |
assert(t); |
| 1262 |
|
| 1263 |
t->width = width; |
| 1264 |
} |
| 1265 |
|
| 1266 |
void table_set_cell_height_max(Table *t, size_t height) { |
| 1267 |
assert(t); |
| 1268 |
assert(height >= 1 || height == SIZE_MAX); |
| 1269 |
|
| 1270 |
t->cell_height_max = height; |
| 1271 |
} |
| 1272 |
|
| 1273 |
void table_set_ersatz_string(Table *t, TableErsatz ersatz) { |
| 1274 |
assert(t); |
| 1275 |
assert(ersatz >= 0 && ersatz < _TABLE_ERSATZ_MAX); |
| 1276 |
|
| 1277 |
t->ersatz = ersatz; |
| 1278 |
} |
| 1279 |
|
| 1280 |
static const char* table_ersatz_string(const Table *t) { |
| 1281 |
switch (t->ersatz) { |
| 1282 |
case TABLE_ERSATZ_EMPTY: |
| 1283 |
return ""; |
| 1284 |
case TABLE_ERSATZ_DASH: |
| 1285 |
return "-"; |
| 1286 |
case TABLE_ERSATZ_UNSET: |
| 1287 |
return "(unset)"; |
| 1288 |
case TABLE_ERSATZ_NA: |
| 1289 |
return "n/a"; |
| 1290 |
default: |
| 1291 |
assert_not_reached(); |
| 1292 |
} |
| 1293 |
} |
| 1294 |
|
| 1295 |
static int table_set_display_all(Table *t) { |
| 1296 |
size_t *d; |
| 1297 |
|
| 1298 |
assert(t); |
| 1299 |
|
| 1300 |
/* Initialize the display map to the identity */ |
| 1301 |
|
| 1302 |
d = reallocarray(t->display_map, t->n_columns, sizeof(size_t)); |
| 1303 |
if (!d) |
| 1304 |
return -ENOMEM; |
| 1305 |
|
| 1306 |
for (size_t i = 0; i < t->n_columns; i++) |
| 1307 |
d[i] = i; |
| 1308 |
|
| 1309 |
t->display_map = d; |
| 1310 |
t->n_display_map = t->n_columns; |
| 1311 |
|
| 1312 |
return 0; |
| 1313 |
} |
| 1314 |
|
| 1315 |
int table_set_display_internal(Table *t, size_t first_column, ...) { |
| 1316 |
size_t column; |
| 1317 |
va_list ap; |
| 1318 |
|
| 1319 |
assert(t); |
| 1320 |
|
| 1321 |
column = first_column; |
| 1322 |
|
| 1323 |
va_start(ap, first_column); |
| 1324 |
for (;;) { |
| 1325 |
assert(column < t->n_columns); |
| 1326 |
|
| 1327 |
if (!GREEDY_REALLOC(t->display_map, MAX(t->n_columns, t->n_display_map+1))) { |
| 1328 |
va_end(ap); |
| 1329 |
return -ENOMEM; |
| 1330 |
} |
| 1331 |
|
| 1332 |
t->display_map[t->n_display_map++] = column; |
| 1333 |
|
| 1334 |
column = va_arg(ap, size_t); |
| 1335 |
if (column == SIZE_MAX) |
| 1336 |
break; |
| 1337 |
|
| 1338 |
} |
| 1339 |
va_end(ap); |
| 1340 |
|
| 1341 |
return 0; |
| 1342 |
} |
| 1343 |
|
| 1344 |
int table_set_sort_internal(Table *t, size_t first_column, ...) { |
| 1345 |
size_t column; |
| 1346 |
va_list ap; |
| 1347 |
|
| 1348 |
assert(t); |
| 1349 |
|
| 1350 |
column = first_column; |
| 1351 |
|
| 1352 |
va_start(ap, first_column); |
| 1353 |
for (;;) { |
| 1354 |
assert(column < t->n_columns); |
| 1355 |
|
| 1356 |
if (!GREEDY_REALLOC(t->sort_map, MAX(t->n_columns, t->n_sort_map+1))) { |
| 1357 |
va_end(ap); |
| 1358 |
return -ENOMEM; |
| 1359 |
} |
| 1360 |
|
| 1361 |
t->sort_map[t->n_sort_map++] = column; |
| 1362 |
|
| 1363 |
column = va_arg(ap, size_t); |
| 1364 |
if (column == SIZE_MAX) |
| 1365 |
break; |
| 1366 |
} |
| 1367 |
va_end(ap); |
| 1368 |
|
| 1369 |
return 0; |
| 1370 |
} |
| 1371 |
|
| 1372 |
int table_hide_column_from_display_internal(Table *t, ...) { |
| 1373 |
size_t cur = 0; |
| 1374 |
int r; |
| 1375 |
|
| 1376 |
assert(t); |
| 1377 |
|
| 1378 |
/* If the display map is empty, initialize it with all available columns */ |
| 1379 |
if (!t->display_map) { |
| 1380 |
r = table_set_display_all(t); |
| 1381 |
if (r < 0) |
| 1382 |
return r; |
| 1383 |
} |
| 1384 |
|
| 1385 |
FOREACH_ARRAY(i, t->display_map, t->n_display_map) { |
| 1386 |
bool listed = false; |
| 1387 |
va_list ap; |
| 1388 |
|
| 1389 |
va_start(ap, t); |
| 1390 |
for (;;) { |
| 1391 |
size_t column; |
| 1392 |
|
| 1393 |
column = va_arg(ap, size_t); |
| 1394 |
if (column == SIZE_MAX) |
| 1395 |
break; |
| 1396 |
if (column == *i) { |
| 1397 |
listed = true; |
| 1398 |
break; |
| 1399 |
} |
| 1400 |
} |
| 1401 |
va_end(ap); |
| 1402 |
|
| 1403 |
if (listed) |
| 1404 |
continue; |
| 1405 |
|
| 1406 |
t->display_map[cur++] = *i; |
| 1407 |
} |
| 1408 |
|
| 1409 |
t->n_display_map = cur; |
| 1410 |
|
| 1411 |
return 0; |
| 1412 |
} |
| 1413 |
|
| 1414 |
static int cell_data_compare(TableData *a, size_t index_a, TableData *b, size_t index_b) { |
| 1415 |
int r; |
| 1416 |
|
| 1417 |
assert(a); |
| 1418 |
assert(b); |
| 1419 |
|
| 1420 |
if (a->type == b->type) { |
| 1421 |
|
| 1422 |
/* We only define ordering for cells of the same data type. If cells with different data types are |
| 1423 |
* compared we follow the order the cells were originally added in */ |
| 1424 |
|
| 1425 |
switch (a->type) { |
| 1426 |
|
| 1427 |
case TABLE_STRING: |
| 1428 |
case TABLE_STRING_WITH_ANSI: |
| 1429 |
case TABLE_FIELD: |
| 1430 |
case TABLE_HEADER: |
| 1431 |
return strcmp(a->string, b->string); |
| 1432 |
|
| 1433 |
case TABLE_PATH: |
| 1434 |
case TABLE_PATH_BASENAME: |
| 1435 |
return path_compare(a->string, b->string); |
| 1436 |
|
| 1437 |
case TABLE_VERSION: |
| 1438 |
return strverscmp_improved(a->string, b->string); |
| 1439 |
|
| 1440 |
case TABLE_STRV: |
| 1441 |
case TABLE_STRV_WRAPPED: |
| 1442 |
return strv_compare(a->strv, b->strv); |
| 1443 |
|
| 1444 |
case TABLE_BOOLEAN: |
| 1445 |
case TABLE_BOOLEAN_CHECKMARK: |
| 1446 |
if (!a->boolean && b->boolean) |
| 1447 |
return -1; |
| 1448 |
if (a->boolean && !b->boolean) |
| 1449 |
return 1; |
| 1450 |
return 0; |
| 1451 |
|
| 1452 |
case TABLE_TRISTATE: |
| 1453 |
/* NB: we do not use CMP() here, since we want to collapse all negative and all |
| 1454 |
* positive into one bucket each. */ |
| 1455 |
if ((a->tristate < 0 && b->tristate >= 0) || |
| 1456 |
(a->tristate == 0 && b->tristate > 0)) |
| 1457 |
return -1; |
| 1458 |
|
| 1459 |
if ((b->tristate < 0 && a->tristate >= 0) || |
| 1460 |
(b->tristate == 0 && a->tristate > 0)) |
| 1461 |
return 1; |
| 1462 |
return 0; |
| 1463 |
|
| 1464 |
case TABLE_TIMESTAMP: |
| 1465 |
case TABLE_TIMESTAMP_UTC: |
| 1466 |
case TABLE_TIMESTAMP_RELATIVE: |
| 1467 |
case TABLE_TIMESTAMP_RELATIVE_MONOTONIC: |
| 1468 |
case TABLE_TIMESTAMP_LEFT: |
| 1469 |
case TABLE_TIMESTAMP_DATE: |
| 1470 |
return CMP(a->timestamp, b->timestamp); |
| 1471 |
|
| 1472 |
case TABLE_TIMESPAN: |
| 1473 |
case TABLE_TIMESPAN_MSEC: |
| 1474 |
case TABLE_TIMESPAN_DAY: |
| 1475 |
return CMP(a->timespan, b->timespan); |
| 1476 |
|
| 1477 |
case TABLE_SIZE: |
| 1478 |
case TABLE_BPS: |
| 1479 |
return CMP(a->size, b->size); |
| 1480 |
|
| 1481 |
case TABLE_INT: |
| 1482 |
case TABLE_SIGNAL: |
| 1483 |
return CMP(a->int_val, b->int_val); |
| 1484 |
|
| 1485 |
case TABLE_INT8: |
| 1486 |
return CMP(a->int8, b->int8); |
| 1487 |
|
| 1488 |
case TABLE_INT16: |
| 1489 |
return CMP(a->int16, b->int16); |
| 1490 |
|
| 1491 |
case TABLE_INT32: |
| 1492 |
return CMP(a->int32, b->int32); |
| 1493 |
|
| 1494 |
case TABLE_INT64: |
| 1495 |
return CMP(a->int64, b->int64); |
| 1496 |
|
| 1497 |
case TABLE_UINT: |
| 1498 |
return CMP(a->uint_val, b->uint_val); |
| 1499 |
|
| 1500 |
case TABLE_UINT8: |
| 1501 |
return CMP(a->uint8, b->uint8); |
| 1502 |
|
| 1503 |
case TABLE_UINT16: |
| 1504 |
return CMP(a->uint16, b->uint16); |
| 1505 |
|
| 1506 |
case TABLE_UINT32: |
| 1507 |
case TABLE_UINT32_HEX: |
| 1508 |
case TABLE_UINT32_HEX_0x: |
| 1509 |
return CMP(a->uint32, b->uint32); |
| 1510 |
|
| 1511 |
case TABLE_UINT64: |
| 1512 |
case TABLE_UINT64_HEX: |
| 1513 |
case TABLE_UINT64_HEX_0x: |
| 1514 |
return CMP(a->uint64, b->uint64); |
| 1515 |
|
| 1516 |
case TABLE_PERCENT: |
| 1517 |
return CMP(a->percent, b->percent); |
| 1518 |
|
| 1519 |
case TABLE_IFINDEX: |
| 1520 |
return CMP(a->ifindex, b->ifindex); |
| 1521 |
|
| 1522 |
case TABLE_IN_ADDR: |
| 1523 |
return CMP(a->address.in.s_addr, b->address.in.s_addr); |
| 1524 |
|
| 1525 |
case TABLE_IN6_ADDR: |
| 1526 |
return memcmp(&a->address.in6, &b->address.in6, FAMILY_ADDRESS_SIZE(AF_INET6)); |
| 1527 |
|
| 1528 |
case TABLE_UUID: |
| 1529 |
case TABLE_ID128: |
| 1530 |
return memcmp(&a->id128, &b->id128, sizeof(sd_id128_t)); |
| 1531 |
|
| 1532 |
case TABLE_UID: |
| 1533 |
return CMP(a->uid, b->uid); |
| 1534 |
|
| 1535 |
case TABLE_GID: |
| 1536 |
return CMP(a->gid, b->gid); |
| 1537 |
|
| 1538 |
case TABLE_PID: |
| 1539 |
return CMP(a->pid, b->pid); |
| 1540 |
|
| 1541 |
case TABLE_MODE: |
| 1542 |
case TABLE_MODE_INODE_TYPE: |
| 1543 |
return CMP(a->mode, b->mode); |
| 1544 |
|
| 1545 |
case TABLE_DEVNUM: |
| 1546 |
r = CMP(major(a->devnum), major(b->devnum)); |
| 1547 |
if (r != 0) |
| 1548 |
return r; |
| 1549 |
|
| 1550 |
return CMP(minor(a->devnum), minor(b->devnum)); |
| 1551 |
|
| 1552 |
case TABLE_JSON: |
| 1553 |
return json_variant_compare(a->json, b->json); |
| 1554 |
|
| 1555 |
default: |
| 1556 |
; |
| 1557 |
} |
| 1558 |
} |
| 1559 |
|
| 1560 |
/* Generic fallback using the original order in which the cells where added. */ |
| 1561 |
return CMP(index_a, index_b); |
| 1562 |
} |
| 1563 |
|
| 1564 |
static int table_data_compare(const size_t *a, const size_t *b, Table *t) { |
| 1565 |
int r; |
| 1566 |
|
| 1567 |
/* This is called from qsort()s inner loops. Correctly implemented qsort will never pass NULL so we |
| 1568 |
just suppress the check via POINTER_MAY_BE_NULL instead of assert() to avoid the runtime cost. */ |
| 1569 |
POINTER_MAY_BE_NULL(a); |
| 1570 |
POINTER_MAY_BE_NULL(b); |
| 1571 |
assert(t); |
| 1572 |
assert(t->sort_map); |
| 1573 |
|
| 1574 |
/* Make sure the header stays at the beginning */ |
| 1575 |
if (*a < t->n_columns && *b < t->n_columns) |
| 1576 |
return 0; |
| 1577 |
if (*a < t->n_columns) |
| 1578 |
return -1; |
| 1579 |
if (*b < t->n_columns) |
| 1580 |
return 1; |
| 1581 |
|
| 1582 |
/* Order other lines by the sorting map */ |
| 1583 |
for (size_t i = 0; i < t->n_sort_map; i++) { |
| 1584 |
TableData *d, *dd; |
| 1585 |
|
| 1586 |
d = t->data[*a + t->sort_map[i]]; |
| 1587 |
dd = t->data[*b + t->sort_map[i]]; |
| 1588 |
|
| 1589 |
r = cell_data_compare(d, *a, dd, *b); |
| 1590 |
if (r != 0) |
| 1591 |
return t->reverse_map && t->reverse_map[t->sort_map[i]] ? -r : r; |
| 1592 |
} |
| 1593 |
|
| 1594 |
/* Order identical lines by the order there were originally added in */ |
| 1595 |
return CMP(*a, *b); |
| 1596 |
} |
| 1597 |
|
| 1598 |
static char* format_strv_width(char **strv, size_t column_width) { |
| 1599 |
_cleanup_(memstream_done) MemStream m = {}; |
| 1600 |
FILE *f; |
| 1601 |
|
| 1602 |
f = memstream_init(&m); |
| 1603 |
if (!f) |
| 1604 |
return NULL; |
| 1605 |
|
| 1606 |
size_t position = 0; |
| 1607 |
STRV_FOREACH(p, strv) { |
| 1608 |
size_t our_len = utf8_console_width(*p); /* This returns -1 on invalid utf-8 (which shouldn't happen). |
| 1609 |
* If that happens, we'll just print one item per line. */ |
| 1610 |
|
| 1611 |
if (position == 0) { |
| 1612 |
fputs(*p, f); |
| 1613 |
position = our_len; |
| 1614 |
} else if (size_add(size_add(position, 1), our_len) <= column_width) { |
| 1615 |
fprintf(f, " %s", *p); |
| 1616 |
position = size_add(size_add(position, 1), our_len); |
| 1617 |
} else { |
| 1618 |
fprintf(f, "\n%s", *p); |
| 1619 |
position = our_len; |
| 1620 |
} |
| 1621 |
} |
| 1622 |
|
| 1623 |
char *buf; |
| 1624 |
if (memstream_finalize(&m, &buf, NULL) < 0) |
| 1625 |
return NULL; |
| 1626 |
|
| 1627 |
return buf; |
| 1628 |
} |
| 1629 |
|
| 1630 |
static const char* table_data_format( |
| 1631 |
Table *t, |
| 1632 |
TableData *d, |
| 1633 |
bool avoid_uppercasing, |
| 1634 |
size_t column_width, |
| 1635 |
bool *have_soft) { |
| 1636 |
|
| 1637 |
assert(d); |
| 1638 |
|
| 1639 |
if (d->formatted && |
| 1640 |
/* Only TABLE_STRV_WRAPPED adjust based on column_width so far… */ |
| 1641 |
(d->type != TABLE_STRV_WRAPPED || d->formatted_for_width == column_width)) |
| 1642 |
return d->formatted; |
| 1643 |
|
| 1644 |
d->formatted = mfree(d->formatted); |
| 1645 |
|
| 1646 |
switch (d->type) { |
| 1647 |
case TABLE_EMPTY: |
| 1648 |
return table_ersatz_string(t); |
| 1649 |
|
| 1650 |
case TABLE_STRING: |
| 1651 |
case TABLE_STRING_WITH_ANSI: |
| 1652 |
case TABLE_PATH: |
| 1653 |
case TABLE_PATH_BASENAME: |
| 1654 |
case TABLE_FIELD: |
| 1655 |
case TABLE_HEADER: |
| 1656 |
case TABLE_VERSION: { |
| 1657 |
_cleanup_free_ char *bn = NULL; |
| 1658 |
const char *s; |
| 1659 |
|
| 1660 |
if (d->type == TABLE_PATH_BASENAME) |
| 1661 |
s = path_extract_filename(d->string, &bn) < 0 ? d->string : bn; |
| 1662 |
else |
| 1663 |
s = d->string; |
| 1664 |
|
| 1665 |
if (d->uppercase && !avoid_uppercasing) { |
| 1666 |
d->formatted = new(char, strlen(s) + (d->type == TABLE_FIELD) + 1); |
| 1667 |
if (!d->formatted) |
| 1668 |
return NULL; |
| 1669 |
|
| 1670 |
char *q = d->formatted; |
| 1671 |
for (const char *p = s; *p; p++) |
| 1672 |
*(q++) = (char) toupper((unsigned char) *p); |
| 1673 |
|
| 1674 |
if (d->type == TABLE_FIELD) |
| 1675 |
*(q++) = ':'; |
| 1676 |
|
| 1677 |
*q = 0; |
| 1678 |
return d->formatted; |
| 1679 |
} |
| 1680 |
|
| 1681 |
if (d->type == TABLE_FIELD) |
| 1682 |
return (d->formatted = strjoin(s, ":")); |
| 1683 |
|
| 1684 |
if (bn) |
| 1685 |
return (d->formatted = TAKE_PTR(bn)); |
| 1686 |
|
| 1687 |
return d->string; |
| 1688 |
} |
| 1689 |
|
| 1690 |
case TABLE_STRV: |
| 1691 |
if (strv_isempty(d->strv)) |
| 1692 |
return table_ersatz_string(t); |
| 1693 |
|
| 1694 |
return (d->formatted = strv_join(d->strv, "\n")); |
| 1695 |
|
| 1696 |
case TABLE_STRV_WRAPPED: |
| 1697 |
if (strv_isempty(d->strv)) |
| 1698 |
return table_ersatz_string(t); |
| 1699 |
|
| 1700 |
d->formatted = format_strv_width(d->strv, column_width); |
| 1701 |
if (!d->formatted) |
| 1702 |
return NULL; |
| 1703 |
|
| 1704 |
d->formatted_for_width = column_width; |
| 1705 |
if (have_soft) |
| 1706 |
*have_soft = true; |
| 1707 |
return d->formatted; |
| 1708 |
|
| 1709 |
case TABLE_BOOLEAN: |
| 1710 |
return yes_no(d->boolean); |
| 1711 |
|
| 1712 |
case TABLE_BOOLEAN_CHECKMARK: |
| 1713 |
return glyph(d->boolean ? GLYPH_CHECK_MARK : GLYPH_CROSS_MARK); |
| 1714 |
|
| 1715 |
case TABLE_TRISTATE: |
| 1716 |
if (d->tristate < 0) |
| 1717 |
return table_ersatz_string(t); |
| 1718 |
|
| 1719 |
return yes_no(d->tristate); |
| 1720 |
|
| 1721 |
case TABLE_TIMESTAMP: |
| 1722 |
case TABLE_TIMESTAMP_UTC: |
| 1723 |
case TABLE_TIMESTAMP_RELATIVE: |
| 1724 |
case TABLE_TIMESTAMP_RELATIVE_MONOTONIC: |
| 1725 |
case TABLE_TIMESTAMP_LEFT: |
| 1726 |
case TABLE_TIMESTAMP_DATE: { |
| 1727 |
char *ret; |
| 1728 |
|
| 1729 |
_cleanup_free_ char *p = new( |
| 1730 |
char, |
| 1731 |
IN_SET(d->type, TABLE_TIMESTAMP_RELATIVE, TABLE_TIMESTAMP_RELATIVE_MONOTONIC, TABLE_TIMESTAMP_LEFT) ? |
| 1732 |
FORMAT_TIMESTAMP_RELATIVE_MAX : FORMAT_TIMESTAMP_MAX); |
| 1733 |
if (!p) |
| 1734 |
return NULL; |
| 1735 |
|
| 1736 |
if (d->type == TABLE_TIMESTAMP) |
| 1737 |
ret = format_timestamp(p, FORMAT_TIMESTAMP_MAX, d->timestamp); |
| 1738 |
else if (d->type == TABLE_TIMESTAMP_UTC) |
| 1739 |
ret = format_timestamp_style(p, FORMAT_TIMESTAMP_MAX, d->timestamp, TIMESTAMP_UTC); |
| 1740 |
else if (d->type == TABLE_TIMESTAMP_DATE) |
| 1741 |
ret = format_timestamp_style(p, FORMAT_TIMESTAMP_MAX, d->timestamp, TIMESTAMP_DATE); |
| 1742 |
else if (d->type == TABLE_TIMESTAMP_RELATIVE_MONOTONIC) |
| 1743 |
ret = format_timestamp_relative_monotonic(p, FORMAT_TIMESTAMP_RELATIVE_MAX, d->timestamp); |
| 1744 |
else |
| 1745 |
ret = format_timestamp_relative_full(p, FORMAT_TIMESTAMP_RELATIVE_MAX, |
| 1746 |
d->timestamp, CLOCK_REALTIME, |
| 1747 |
/* implicit_left= */ d->type == TABLE_TIMESTAMP_LEFT); |
| 1748 |
if (!ret) |
| 1749 |
return "-"; |
| 1750 |
|
| 1751 |
return (d->formatted = TAKE_PTR(p)); |
| 1752 |
} |
| 1753 |
|
| 1754 |
case TABLE_TIMESPAN: |
| 1755 |
case TABLE_TIMESPAN_MSEC: |
| 1756 |
case TABLE_TIMESPAN_DAY: { |
| 1757 |
_cleanup_free_ char *p = new(char, FORMAT_TIMESPAN_MAX); |
| 1758 |
if (!p) |
| 1759 |
return NULL; |
| 1760 |
|
| 1761 |
if (!format_timespan(p, FORMAT_TIMESPAN_MAX, d->timespan, |
| 1762 |
d->type == TABLE_TIMESPAN ? 0 : |
| 1763 |
d->type == TABLE_TIMESPAN_MSEC ? USEC_PER_MSEC : USEC_PER_DAY)) |
| 1764 |
return "-"; |
| 1765 |
|
| 1766 |
return (d->formatted = TAKE_PTR(p)); |
| 1767 |
} |
| 1768 |
|
| 1769 |
case TABLE_SIZE: { |
| 1770 |
_cleanup_free_ char *p = new(char, FORMAT_BYTES_MAX); |
| 1771 |
if (!p) |
| 1772 |
return NULL; |
| 1773 |
|
| 1774 |
if (!format_bytes(p, FORMAT_BYTES_MAX, d->size)) |
| 1775 |
return table_ersatz_string(t); |
| 1776 |
|
| 1777 |
return (d->formatted = TAKE_PTR(p)); |
| 1778 |
} |
| 1779 |
|
| 1780 |
case TABLE_BPS: { |
| 1781 |
_cleanup_free_ char *p = new(char, FORMAT_BYTES_MAX+2); |
| 1782 |
if (!p) |
| 1783 |
return NULL; |
| 1784 |
|
| 1785 |
if (!format_bytes_full(p, FORMAT_BYTES_MAX, d->size, FORMAT_BYTES_BELOW_POINT)) |
| 1786 |
return table_ersatz_string(t); |
| 1787 |
|
| 1788 |
size_t n = strlen(p); |
| 1789 |
strscpy(p + n, FORMAT_BYTES_MAX + 2 - n, "bps"); |
| 1790 |
|
| 1791 |
return (d->formatted = TAKE_PTR(p)); |
| 1792 |
} |
| 1793 |
|
| 1794 |
case TABLE_INT: |
| 1795 |
return (d->formatted = asprintf_safe("%i", d->int_val)); |
| 1796 |
|
| 1797 |
case TABLE_INT8: |
| 1798 |
return (d->formatted = asprintf_safe("%" PRIi8, d->int8)); |
| 1799 |
|
| 1800 |
case TABLE_INT16: |
| 1801 |
return (d->formatted = asprintf_safe("%" PRIi16, d->int16)); |
| 1802 |
|
| 1803 |
case TABLE_INT32: |
| 1804 |
return (d->formatted = asprintf_safe("%" PRIi32, d->int32)); |
| 1805 |
|
| 1806 |
case TABLE_INT64: |
| 1807 |
return (d->formatted = asprintf_safe("%" PRIi64, d->int64)); |
| 1808 |
|
| 1809 |
case TABLE_UINT: |
| 1810 |
return (d->formatted = asprintf_safe("%u", d->uint_val)); |
| 1811 |
|
| 1812 |
case TABLE_UINT8: |
| 1813 |
return (d->formatted = asprintf_safe("%" PRIu8, d->uint8)); |
| 1814 |
|
| 1815 |
case TABLE_UINT16: |
| 1816 |
return (d->formatted = asprintf_safe("%" PRIu16, d->uint16)); |
| 1817 |
|
| 1818 |
case TABLE_UINT32: |
| 1819 |
return (d->formatted = asprintf_safe("%" PRIu32, d->uint32)); |
| 1820 |
|
| 1821 |
case TABLE_UINT32_HEX: |
| 1822 |
return (d->formatted = asprintf_safe("%" PRIx32, d->uint32)); |
| 1823 |
|
| 1824 |
case TABLE_UINT32_HEX_0x: |
| 1825 |
return (d->formatted = asprintf_safe("0x%" PRIx32, d->uint32)); |
| 1826 |
|
| 1827 |
case TABLE_UINT64: |
| 1828 |
return (d->formatted = asprintf_safe("%" PRIu64, d->uint64)); |
| 1829 |
|
| 1830 |
case TABLE_UINT64_HEX: |
| 1831 |
return (d->formatted = asprintf_safe("%" PRIx64, d->uint64)); |
| 1832 |
|
| 1833 |
case TABLE_UINT64_HEX_0x: |
| 1834 |
return (d->formatted = asprintf_safe("0x%" PRIx64, d->uint64)); |
| 1835 |
|
| 1836 |
case TABLE_PERCENT: |
| 1837 |
return (d->formatted = asprintf_safe("%i%%" , d->percent)); |
| 1838 |
|
| 1839 |
case TABLE_IFINDEX: |
| 1840 |
(void) format_ifname_full_alloc(d->ifindex, FORMAT_IFNAME_IFINDEX, &d->formatted); |
| 1841 |
return d->formatted; |
| 1842 |
|
| 1843 |
case TABLE_IN_ADDR: |
| 1844 |
case TABLE_IN6_ADDR: |
| 1845 |
(void) in_addr_to_string(d->type == TABLE_IN_ADDR ? AF_INET : AF_INET6, |
| 1846 |
&d->address, |
| 1847 |
&d->formatted); |
| 1848 |
return d->formatted; |
| 1849 |
|
| 1850 |
case TABLE_ID128: |
| 1851 |
d->formatted = new(char, SD_ID128_STRING_MAX); |
| 1852 |
if (!d->formatted) |
| 1853 |
return NULL; |
| 1854 |
|
| 1855 |
return sd_id128_to_string(d->id128, d->formatted); |
| 1856 |
|
| 1857 |
case TABLE_UUID: |
| 1858 |
d->formatted = new(char, SD_ID128_UUID_STRING_MAX); |
| 1859 |
if (!d->formatted) |
| 1860 |
return NULL; |
| 1861 |
|
| 1862 |
return sd_id128_to_uuid_string(d->id128, d->formatted); |
| 1863 |
|
| 1864 |
case TABLE_UID: |
| 1865 |
if (!uid_is_valid(d->uid)) |
| 1866 |
return table_ersatz_string(t); |
| 1867 |
|
| 1868 |
return (d->formatted = asprintf_safe(UID_FMT, d->uid)); |
| 1869 |
|
| 1870 |
case TABLE_GID: |
| 1871 |
if (!gid_is_valid(d->gid)) |
| 1872 |
return table_ersatz_string(t); |
| 1873 |
|
| 1874 |
return (d->formatted = asprintf_safe(GID_FMT, d->gid)); |
| 1875 |
|
| 1876 |
case TABLE_PID: |
| 1877 |
if (!pid_is_valid(d->pid)) |
| 1878 |
return table_ersatz_string(t); |
| 1879 |
|
| 1880 |
return (d->formatted = asprintf_safe(PID_FMT, d->pid)); |
| 1881 |
|
| 1882 |
case TABLE_SIGNAL: { |
| 1883 |
const char *suffix = signal_to_string(d->int_val); |
| 1884 |
if (!suffix) |
| 1885 |
return table_ersatz_string(t); |
| 1886 |
|
| 1887 |
return (d->formatted = strjoin("SIG", suffix)); |
| 1888 |
} |
| 1889 |
|
| 1890 |
case TABLE_MODE: |
| 1891 |
if (d->mode == MODE_INVALID) |
| 1892 |
return table_ersatz_string(t); |
| 1893 |
|
| 1894 |
return (d->formatted = asprintf_safe("%04o", d->mode & 07777)); |
| 1895 |
|
| 1896 |
case TABLE_MODE_INODE_TYPE: |
| 1897 |
if (d->mode == MODE_INVALID) |
| 1898 |
return table_ersatz_string(t); |
| 1899 |
|
| 1900 |
return inode_type_to_string(d->mode) ?: table_ersatz_string(t); |
| 1901 |
|
| 1902 |
case TABLE_DEVNUM: |
| 1903 |
if (devnum_is_zero(d->devnum)) |
| 1904 |
return table_ersatz_string(t); |
| 1905 |
|
| 1906 |
return (d->formatted = asprintf_safe(DEVNUM_FORMAT_STR, DEVNUM_FORMAT_VAL(d->devnum))); |
| 1907 |
|
| 1908 |
case TABLE_JSON: |
| 1909 |
if (!d->json) |
| 1910 |
return table_ersatz_string(t); |
| 1911 |
|
| 1912 |
(void) sd_json_variant_format(d->json, /* flags= */ 0, &d->formatted); |
| 1913 |
return d->formatted; |
| 1914 |
|
| 1915 |
default: |
| 1916 |
assert_not_reached(); |
| 1917 |
} |
| 1918 |
} |
| 1919 |
|
| 1920 |
static const char* table_data_format_strip_ansi( |
| 1921 |
Table *t, |
| 1922 |
TableData *d, |
| 1923 |
bool avoid_uppercasing, |
| 1924 |
size_t column_width, |
| 1925 |
bool *have_soft, |
| 1926 |
char **ret_buffer) { |
| 1927 |
|
| 1928 |
/* Just like table_data_format() but strips ANSI sequences for ANSI fields. */ |
| 1929 |
|
| 1930 |
assert(ret_buffer); |
| 1931 |
|
| 1932 |
const char *c; |
| 1933 |
|
| 1934 |
c = table_data_format(t, d, avoid_uppercasing, column_width, have_soft); |
| 1935 |
if (!c) |
| 1936 |
return NULL; |
| 1937 |
|
| 1938 |
if (d->type != TABLE_STRING_WITH_ANSI) { |
| 1939 |
/* Shortcut: we do not consider ANSI sequences for all other column types, hence return the |
| 1940 |
* original string as-is */ |
| 1941 |
*ret_buffer = NULL; |
| 1942 |
return c; |
| 1943 |
} |
| 1944 |
|
| 1945 |
_cleanup_free_ char *s = strdup(c); |
| 1946 |
if (!s) |
| 1947 |
return NULL; |
| 1948 |
|
| 1949 |
if (!strip_tab_ansi(&s, /* isz= */ NULL, /* highlight= */ NULL)) |
| 1950 |
return NULL; |
| 1951 |
|
| 1952 |
*ret_buffer = TAKE_PTR(s); |
| 1953 |
return *ret_buffer; |
| 1954 |
} |
| 1955 |
|
| 1956 |
static int console_width_height( |
| 1957 |
const char *s, |
| 1958 |
size_t *ret_width, |
| 1959 |
size_t *ret_height) { |
| 1960 |
|
| 1961 |
size_t max_width = 0, height = 0; |
| 1962 |
const char *p; |
| 1963 |
|
| 1964 |
assert(s); |
| 1965 |
|
| 1966 |
/* Determine the width and height in console character cells the specified string needs. */ |
| 1967 |
|
| 1968 |
do { |
| 1969 |
size_t k; |
| 1970 |
|
| 1971 |
p = strchr(s, '\n'); |
| 1972 |
if (p) { |
| 1973 |
_cleanup_free_ char *c = NULL; |
| 1974 |
|
| 1975 |
c = strndup(s, p - s); |
| 1976 |
if (!c) |
| 1977 |
return -ENOMEM; |
| 1978 |
|
| 1979 |
k = utf8_console_width(c); |
| 1980 |
s = p + 1; |
| 1981 |
} else { |
| 1982 |
k = utf8_console_width(s); |
| 1983 |
s = NULL; |
| 1984 |
} |
| 1985 |
if (k == SIZE_MAX) |
| 1986 |
return -EINVAL; |
| 1987 |
if (k > max_width) |
| 1988 |
max_width = k; |
| 1989 |
|
| 1990 |
height++; |
| 1991 |
} while (!isempty(s)); |
| 1992 |
|
| 1993 |
if (ret_width) |
| 1994 |
*ret_width = max_width; |
| 1995 |
|
| 1996 |
if (ret_height) |
| 1997 |
*ret_height = height; |
| 1998 |
|
| 1999 |
return 0; |
| 2000 |
} |
| 2001 |
|
| 2002 |
static int table_data_requested_width_height( |
| 2003 |
Table *table, |
| 2004 |
TableData *d, |
| 2005 |
size_t available_width, |
| 2006 |
size_t *ret_width, |
| 2007 |
size_t *ret_height, |
| 2008 |
bool *have_soft) { |
| 2009 |
|
| 2010 |
_cleanup_free_ char *truncated = NULL, *buffer = NULL; |
| 2011 |
bool truncation_applied = false; |
| 2012 |
size_t width, height; |
| 2013 |
bool soft = false; |
| 2014 |
const char *t; |
| 2015 |
int r; |
| 2016 |
|
| 2017 |
t = table_data_format_strip_ansi( |
| 2018 |
table, |
| 2019 |
d, |
| 2020 |
/* avoid_uppercasing= */ false, |
| 2021 |
available_width, |
| 2022 |
&soft, |
| 2023 |
&buffer); |
| 2024 |
if (!t) |
| 2025 |
return -ENOMEM; |
| 2026 |
|
| 2027 |
if (table->cell_height_max != SIZE_MAX) { |
| 2028 |
r = string_truncate_lines(t, table->cell_height_max, &truncated); |
| 2029 |
if (r < 0) |
| 2030 |
return r; |
| 2031 |
if (r > 0) |
| 2032 |
truncation_applied = true; |
| 2033 |
|
| 2034 |
t = truncated; |
| 2035 |
} |
| 2036 |
|
| 2037 |
r = console_width_height(t, &width, &height); |
| 2038 |
if (r < 0) |
| 2039 |
return r; |
| 2040 |
|
| 2041 |
if (d->maximum_width != SIZE_MAX && width > d->maximum_width) |
| 2042 |
width = d->maximum_width; |
| 2043 |
|
| 2044 |
if (width < d->minimum_width) |
| 2045 |
width = d->minimum_width; |
| 2046 |
|
| 2047 |
if (ret_width) |
| 2048 |
*ret_width = width; |
| 2049 |
if (ret_height) |
| 2050 |
*ret_height = height; |
| 2051 |
if (have_soft && soft) |
| 2052 |
*have_soft = true; |
| 2053 |
|
| 2054 |
return truncation_applied; |
| 2055 |
} |
| 2056 |
|
| 2057 |
static char* align_string_mem(const char *str, const char *url, size_t new_length, unsigned percent) { |
| 2058 |
size_t w = 0, space, lspace, old_length, clickable_length; |
| 2059 |
_cleanup_free_ char *clickable = NULL; |
| 2060 |
const char *p; |
| 2061 |
char *ret; |
| 2062 |
int r; |
| 2063 |
|
| 2064 |
/* As with ellipsize_mem(), 'old_length' is a byte size while 'new_length' is a width in character cells */ |
| 2065 |
|
| 2066 |
assert(str); |
| 2067 |
assert(percent <= 100); |
| 2068 |
|
| 2069 |
old_length = strlen(str); |
| 2070 |
|
| 2071 |
if (url) { |
| 2072 |
r = terminal_urlify(url, str, &clickable); |
| 2073 |
if (r < 0) |
| 2074 |
return NULL; |
| 2075 |
|
| 2076 |
clickable_length = strlen(clickable); |
| 2077 |
} else |
| 2078 |
clickable_length = old_length; |
| 2079 |
|
| 2080 |
/* Determine current width on screen */ |
| 2081 |
p = str; |
| 2082 |
while (p < str + old_length) { |
| 2083 |
char32_t c; |
| 2084 |
|
| 2085 |
if (utf8_encoded_to_unichar(p, &c) < 0) { |
| 2086 |
p++, w++; /* count invalid chars as 1 */ |
| 2087 |
continue; |
| 2088 |
} |
| 2089 |
|
| 2090 |
p = utf8_next_char(p); |
| 2091 |
w += unichar_iswide(c) ? 2 : 1; |
| 2092 |
} |
| 2093 |
|
| 2094 |
/* Already wider than the target, if so, don't do anything */ |
| 2095 |
if (w >= new_length) |
| 2096 |
return clickable ? TAKE_PTR(clickable) : strdup(str); |
| 2097 |
|
| 2098 |
/* How much spaces shall we add? An how much on the left side? */ |
| 2099 |
space = new_length - w; |
| 2100 |
lspace = space * percent / 100U; |
| 2101 |
|
| 2102 |
ret = new(char, space + clickable_length + 1); |
| 2103 |
if (!ret) |
| 2104 |
return NULL; |
| 2105 |
|
| 2106 |
for (size_t i = 0; i < lspace; i++) |
| 2107 |
ret[i] = ' '; |
| 2108 |
memcpy(ret + lspace, clickable ?: str, clickable_length); |
| 2109 |
for (size_t i = lspace + clickable_length; i < space + clickable_length; i++) |
| 2110 |
ret[i] = ' '; |
| 2111 |
|
| 2112 |
ret[space + clickable_length] = 0; |
| 2113 |
return ret; |
| 2114 |
} |
| 2115 |
|
| 2116 |
static bool table_data_isempty(const TableData *d) { |
| 2117 |
assert(d); |
| 2118 |
|
| 2119 |
if (d->type == TABLE_EMPTY) |
| 2120 |
return true; |
| 2121 |
|
| 2122 |
/* Let's also consider an empty strv as truly empty. */ |
| 2123 |
if (IN_SET(d->type, TABLE_STRV, TABLE_STRV_WRAPPED)) |
| 2124 |
return strv_isempty(d->strv); |
| 2125 |
|
| 2126 |
if (d->type == TABLE_JSON) |
| 2127 |
return sd_json_variant_is_null(d->json); |
| 2128 |
|
| 2129 |
/* Note that an empty string we do not consider empty here! */ |
| 2130 |
return false; |
| 2131 |
} |
| 2132 |
|
| 2133 |
static const char* table_data_color(const TableData *d) { |
| 2134 |
assert(d); |
| 2135 |
|
| 2136 |
if (d->color) |
| 2137 |
return d->color; |
| 2138 |
|
| 2139 |
/* Let's implicitly color all "empty" cells in grey, in case an "empty_string" is set that is not empty */ |
| 2140 |
if (table_data_isempty(d)) |
| 2141 |
return ansi_grey(); |
| 2142 |
|
| 2143 |
if (d->type == TABLE_FIELD) |
| 2144 |
return ansi_bright_blue(); |
| 2145 |
|
| 2146 |
return NULL; |
| 2147 |
} |
| 2148 |
|
| 2149 |
static const char* table_data_underline(const TableData *d) { |
| 2150 |
assert(d); |
| 2151 |
|
| 2152 |
if (d->underline) |
| 2153 |
return ansi_add_underline_grey(); |
| 2154 |
|
| 2155 |
if (d->type == TABLE_HEADER) |
| 2156 |
return ansi_add_underline(); |
| 2157 |
|
| 2158 |
return NULL; |
| 2159 |
} |
| 2160 |
|
| 2161 |
static const char* table_data_rgap_underline(const TableData *d) { |
| 2162 |
assert(d); |
| 2163 |
|
| 2164 |
if (d->rgap_underline) |
| 2165 |
return ansi_add_underline_grey(); |
| 2166 |
|
| 2167 |
if (d->type == TABLE_HEADER) |
| 2168 |
return ansi_add_underline(); |
| 2169 |
|
| 2170 |
return NULL; |
| 2171 |
} |
| 2172 |
|
| 2173 |
int table_data_requested_width(Table *table, size_t column, size_t *ret) { |
| 2174 |
size_t width = 0; |
| 2175 |
int r; |
| 2176 |
|
| 2177 |
assert(table); |
| 2178 |
assert(ret); |
| 2179 |
|
| 2180 |
for (size_t row = 0; row < table_get_rows(table); row++) { |
| 2181 |
TableCell *cell = table_get_cell(table, row, column); |
| 2182 |
if (!cell) |
| 2183 |
continue; |
| 2184 |
|
| 2185 |
TableData *data = table_get_data(table, cell); |
| 2186 |
if (!data) |
| 2187 |
continue; |
| 2188 |
|
| 2189 |
size_t w; |
| 2190 |
|
| 2191 |
r = table_data_requested_width_height( |
| 2192 |
table, data, SIZE_MAX, &w, /* ret_height= */ NULL, /* have_soft= */ NULL); |
| 2193 |
if (r < 0) |
| 2194 |
return r; |
| 2195 |
|
| 2196 |
width = MAX(width, w); |
| 2197 |
} |
| 2198 |
|
| 2199 |
*ret = width; |
| 2200 |
return 0; |
| 2201 |
} |
| 2202 |
|
| 2203 |
int table_set_column_width(Table *t, size_t column, size_t width) { |
| 2204 |
int r = 0; |
| 2205 |
|
| 2206 |
assert(t); |
| 2207 |
|
| 2208 |
for (size_t row = 0; row < table_get_rows(t); row++) { |
| 2209 |
TableCell *cell = table_get_cell(t, row, column); |
| 2210 |
if (!cell) |
| 2211 |
continue; |
| 2212 |
|
| 2213 |
RET_GATHER(r, table_set_minimum_width(t, cell, width)); |
| 2214 |
} |
| 2215 |
|
| 2216 |
return r; |
| 2217 |
} |
| 2218 |
|
| 2219 |
int _table_sync_column_widths(size_t column, Table *a, ...) { |
| 2220 |
size_t max = 0; |
| 2221 |
va_list ap; |
| 2222 |
int r = 0; |
| 2223 |
|
| 2224 |
assert(a); |
| 2225 |
|
| 2226 |
/* Make the specified column have the same width in the tables. */ |
| 2227 |
|
| 2228 |
va_start(ap, a); |
| 2229 |
for (Table *t = a; t; t = va_arg(ap, Table*)) { |
| 2230 |
size_t w; |
| 2231 |
|
| 2232 |
r = table_data_requested_width(t, column, &w); |
| 2233 |
if (r < 0) |
| 2234 |
break; |
| 2235 |
|
| 2236 |
max = MAX(max, w); |
| 2237 |
} |
| 2238 |
va_end(ap); |
| 2239 |
if (r < 0) |
| 2240 |
return log_error_errno(r, "Failed to query table column width: %m"); |
| 2241 |
|
| 2242 |
r = 0; |
| 2243 |
va_start(ap, a); |
| 2244 |
for (Table *t = a; t; t = va_arg(ap, Table*)) |
| 2245 |
RET_GATHER(r, table_set_column_width(t, column, max)); |
| 2246 |
va_end(ap); |
| 2247 |
|
| 2248 |
return r; |
| 2249 |
} |
| 2250 |
|
| 2251 |
int table_print_full(Table *t, FILE *f, bool flush) { |
| 2252 |
_cleanup_free_ size_t *sorted = NULL; |
| 2253 |
int r; |
| 2254 |
|
| 2255 |
assert(t); |
| 2256 |
|
| 2257 |
if (!f) |
| 2258 |
f = stdout; |
| 2259 |
|
| 2260 |
/* Ensure we have no incomplete rows */ |
| 2261 |
assert(t->n_cells % t->n_columns == 0); |
| 2262 |
|
| 2263 |
size_t n_rows = t->n_cells / t->n_columns; |
| 2264 |
assert(n_rows > 0); /* at least the header row must be complete */ |
| 2265 |
|
| 2266 |
if (t->sort_map) { |
| 2267 |
/* If sorting is requested, let's calculate an index table we use to lookup the actual index to display with. */ |
| 2268 |
|
| 2269 |
sorted = new(size_t, n_rows); |
| 2270 |
if (!sorted) |
| 2271 |
return -ENOMEM; |
| 2272 |
|
| 2273 |
for (size_t i = 0; i < n_rows; i++) |
| 2274 |
sorted[i] = i * t->n_columns; |
| 2275 |
|
| 2276 |
typesafe_qsort_r(sorted, n_rows, table_data_compare, t); |
| 2277 |
} |
| 2278 |
|
| 2279 |
size_t display_columns = t->display_map ? t->n_display_map : t->n_columns; |
| 2280 |
assert(display_columns > 0); |
| 2281 |
|
| 2282 |
size_t *minimum_width = newa(size_t, display_columns), |
| 2283 |
*maximum_width = newa(size_t, display_columns), |
| 2284 |
*requested_width = newa(size_t, display_columns), |
| 2285 |
*width = NULL; |
| 2286 |
uint64_t *column_weight = newa0(uint64_t, display_columns); |
| 2287 |
|
| 2288 |
for (size_t j = 0; j < display_columns; j++) { |
| 2289 |
minimum_width[j] = 1; |
| 2290 |
maximum_width[j] = SIZE_MAX; |
| 2291 |
} |
| 2292 |
|
| 2293 |
for (unsigned pass = 0; pass < 2; pass++) { |
| 2294 |
/* First pass: determine column sizes */ |
| 2295 |
|
| 2296 |
for (size_t j = 0; j < display_columns; j++) |
| 2297 |
requested_width[j] = SIZE_MAX; |
| 2298 |
|
| 2299 |
bool any_soft = false; |
| 2300 |
|
| 2301 |
for (size_t i = t->header ? 0 : 1; i < n_rows; i++) { |
| 2302 |
TableData **row; |
| 2303 |
|
| 2304 |
/* Note that we don't care about ordering at this time, as we just want to determine column sizes, |
| 2305 |
* hence we don't care for sorted[] during the first pass. */ |
| 2306 |
row = t->data + i * t->n_columns; |
| 2307 |
|
| 2308 |
for (size_t j = 0; j < display_columns; j++) { |
| 2309 |
TableData *d; |
| 2310 |
size_t req_width, req_height; |
| 2311 |
|
| 2312 |
assert_se(d = row[t->display_map ? t->display_map[j] : j]); |
| 2313 |
|
| 2314 |
r = table_data_requested_width_height(t, d, |
| 2315 |
width ? width[j] : SIZE_MAX, |
| 2316 |
&req_width, &req_height, &any_soft); |
| 2317 |
if (r < 0) |
| 2318 |
return r; |
| 2319 |
if (r > 0) { /* Truncated because too many lines? */ |
| 2320 |
_cleanup_free_ char *last = NULL, *buffer = NULL; |
| 2321 |
const char *field; |
| 2322 |
|
| 2323 |
/* If we are going to show only the first few lines of a cell that has |
| 2324 |
* multiple make sure that we have enough space horizontally to show an |
| 2325 |
* ellipsis. Hence, let's figure out the last line, and account for its |
| 2326 |
* length plus ellipsis. */ |
| 2327 |
|
| 2328 |
field = table_data_format_strip_ansi( |
| 2329 |
t, |
| 2330 |
d, |
| 2331 |
/* avoid_uppercasing= */ false, |
| 2332 |
width ? width[j] : SIZE_MAX, |
| 2333 |
&any_soft, |
| 2334 |
&buffer); |
| 2335 |
if (!field) |
| 2336 |
return -ENOMEM; |
| 2337 |
|
| 2338 |
assert_se(t->cell_height_max > 0); |
| 2339 |
r = string_extract_line(field, t->cell_height_max-1, &last); |
| 2340 |
if (r < 0) |
| 2341 |
return r; |
| 2342 |
|
| 2343 |
req_width = MAX(req_width, |
| 2344 |
utf8_console_width(last) + |
| 2345 |
utf8_console_width(glyph(GLYPH_ELLIPSIS))); |
| 2346 |
} |
| 2347 |
|
| 2348 |
/* Determine the biggest width that any cell in this column would like to have */ |
| 2349 |
if (requested_width[j] == SIZE_MAX || |
| 2350 |
requested_width[j] < req_width) |
| 2351 |
requested_width[j] = req_width; |
| 2352 |
|
| 2353 |
/* Determine the minimum width any cell in this column needs */ |
| 2354 |
if (minimum_width[j] < d->minimum_width) |
| 2355 |
minimum_width[j] = d->minimum_width; |
| 2356 |
|
| 2357 |
/* Determine the maximum width any cell in this column needs */ |
| 2358 |
if (d->maximum_width != SIZE_MAX && |
| 2359 |
(maximum_width[j] == SIZE_MAX || |
| 2360 |
maximum_width[j] > d->maximum_width)) |
| 2361 |
maximum_width[j] = d->maximum_width; |
| 2362 |
|
| 2363 |
/* Determine the full columns weight */ |
| 2364 |
column_weight[j] += d->weight; |
| 2365 |
} |
| 2366 |
} |
| 2367 |
|
| 2368 |
/* One space between each column */ |
| 2369 |
size_t table_requested_width, table_minimum_width, table_maximum_width, table_effective_width; |
| 2370 |
table_requested_width = table_minimum_width = table_maximum_width = display_columns - 1; |
| 2371 |
|
| 2372 |
/* Calculate the total weight for all columns, plus the minimum, maximum and requested width for the table. */ |
| 2373 |
uint64_t weight_sum = 0; |
| 2374 |
for (size_t j = 0; j < display_columns; j++) { |
| 2375 |
weight_sum += column_weight[j]; |
| 2376 |
|
| 2377 |
table_minimum_width += minimum_width[j]; |
| 2378 |
|
| 2379 |
if (maximum_width[j] == SIZE_MAX) |
| 2380 |
table_maximum_width = SIZE_MAX; |
| 2381 |
else |
| 2382 |
table_maximum_width += maximum_width[j]; |
| 2383 |
|
| 2384 |
table_requested_width += requested_width[j]; |
| 2385 |
} |
| 2386 |
|
| 2387 |
/* Calculate effective table width */ |
| 2388 |
if (t->width != 0 && t->width != SIZE_MAX) |
| 2389 |
table_effective_width = t->width; |
| 2390 |
else if (t->width == 0 || |
| 2391 |
((pass > 0 || !any_soft) && (pager_have() || !isatty_safe(STDOUT_FILENO)))) |
| 2392 |
table_effective_width = table_requested_width; |
| 2393 |
else |
| 2394 |
table_effective_width = MIN(table_requested_width, columns()); |
| 2395 |
|
| 2396 |
if (table_maximum_width != SIZE_MAX && table_effective_width > table_maximum_width) |
| 2397 |
table_effective_width = table_maximum_width; |
| 2398 |
|
| 2399 |
if (table_effective_width < table_minimum_width) |
| 2400 |
table_effective_width = table_minimum_width; |
| 2401 |
|
| 2402 |
if (!width) |
| 2403 |
width = newa(size_t, display_columns); |
| 2404 |
|
| 2405 |
if (table_effective_width >= table_requested_width) { |
| 2406 |
size_t extra; |
| 2407 |
|
| 2408 |
/* We have extra room, let's distribute it among columns according to their weights. We first provide |
| 2409 |
* each column with what it asked for and the distribute the rest. */ |
| 2410 |
|
| 2411 |
extra = table_effective_width - table_requested_width; |
| 2412 |
|
| 2413 |
for (size_t j = 0; j < display_columns; j++) { |
| 2414 |
size_t delta; |
| 2415 |
|
| 2416 |
if (weight_sum == 0) |
| 2417 |
width[j] = requested_width[j] + extra / (display_columns - j); /* Avoid division by zero */ |
| 2418 |
else |
| 2419 |
width[j] = requested_width[j] + (extra * column_weight[j]) / weight_sum; |
| 2420 |
|
| 2421 |
if (maximum_width[j] != SIZE_MAX && width[j] > maximum_width[j]) |
| 2422 |
width[j] = maximum_width[j]; |
| 2423 |
|
| 2424 |
if (width[j] < minimum_width[j]) |
| 2425 |
width[j] = minimum_width[j]; |
| 2426 |
|
| 2427 |
delta = LESS_BY(width[j], requested_width[j]); |
| 2428 |
|
| 2429 |
/* Subtract what we just added from the rest */ |
| 2430 |
if (extra > delta) |
| 2431 |
extra -= delta; |
| 2432 |
else |
| 2433 |
extra = 0; |
| 2434 |
|
| 2435 |
assert(weight_sum >= column_weight[j]); |
| 2436 |
weight_sum -= column_weight[j]; |
| 2437 |
} |
| 2438 |
|
| 2439 |
break; /* Every column should be happy, no need to repeat calculations. */ |
| 2440 |
} else { |
| 2441 |
/* We need to compress the table, columns can't get what they asked for. We first provide each column |
| 2442 |
* with the minimum they need, and then distribute anything left. */ |
| 2443 |
bool finalize = false; |
| 2444 |
size_t extra; |
| 2445 |
|
| 2446 |
extra = table_effective_width - table_minimum_width; |
| 2447 |
|
| 2448 |
for (size_t j = 0; j < display_columns; j++) |
| 2449 |
width[j] = SIZE_MAX; |
| 2450 |
|
| 2451 |
for (;;) { |
| 2452 |
bool restart = false; |
| 2453 |
|
| 2454 |
for (size_t j = 0; j < display_columns; j++) { |
| 2455 |
size_t delta, w; |
| 2456 |
|
| 2457 |
/* Did this column already get something assigned? If so, let's skip to the next */ |
| 2458 |
if (width[j] != SIZE_MAX) |
| 2459 |
continue; |
| 2460 |
|
| 2461 |
if (weight_sum == 0) |
| 2462 |
w = minimum_width[j] + extra / (display_columns - j); /* avoid division by zero */ |
| 2463 |
else |
| 2464 |
w = minimum_width[j] + (extra * column_weight[j]) / weight_sum; |
| 2465 |
|
| 2466 |
if (w >= requested_width[j]) { |
| 2467 |
/* Never give more than requested. If we hit a column like this, there's more |
| 2468 |
* space to allocate to other columns which means we need to restart the |
| 2469 |
* iteration. However, if we hit a column like this, let's assign it the space |
| 2470 |
* it wanted for good early. */ |
| 2471 |
|
| 2472 |
w = requested_width[j]; |
| 2473 |
restart = true; |
| 2474 |
|
| 2475 |
} else if (!finalize) |
| 2476 |
continue; |
| 2477 |
|
| 2478 |
width[j] = w; |
| 2479 |
|
| 2480 |
assert(w >= minimum_width[j]); |
| 2481 |
delta = w - minimum_width[j]; |
| 2482 |
|
| 2483 |
assert(delta <= extra); |
| 2484 |
extra -= delta; |
| 2485 |
|
| 2486 |
assert(weight_sum >= column_weight[j]); |
| 2487 |
weight_sum -= column_weight[j]; |
| 2488 |
|
| 2489 |
if (restart && !finalize) |
| 2490 |
break; |
| 2491 |
} |
| 2492 |
|
| 2493 |
if (finalize) |
| 2494 |
break; |
| 2495 |
|
| 2496 |
if (!restart) |
| 2497 |
finalize = true; |
| 2498 |
} |
| 2499 |
|
| 2500 |
if (!any_soft) /* Some columns got less than requested. If some cells were "soft", |
| 2501 |
* let's try to reformat them with the new widths. Otherwise, let's |
| 2502 |
* move on. */ |
| 2503 |
break; |
| 2504 |
} |
| 2505 |
} |
| 2506 |
|
| 2507 |
/* Second pass: show output */ |
| 2508 |
for (size_t i = t->header ? 0 : 1; i < n_rows; i++) { |
| 2509 |
size_t n_subline = 0; |
| 2510 |
bool more_sublines; |
| 2511 |
TableData **row; |
| 2512 |
|
| 2513 |
if (sorted) |
| 2514 |
row = t->data + sorted[i]; |
| 2515 |
else |
| 2516 |
row = t->data + i * t->n_columns; |
| 2517 |
|
| 2518 |
do { |
| 2519 |
const char *gap_color = NULL, *gap_underline = NULL; |
| 2520 |
more_sublines = false; |
| 2521 |
|
| 2522 |
for (size_t j = 0; j < display_columns; j++) { |
| 2523 |
_cleanup_free_ char *buffer = NULL, *stripped_ansi_buffer = NULL, *extracted = NULL; |
| 2524 |
bool lines_truncated = false; |
| 2525 |
const char *field, *color = NULL, *underline = NULL; |
| 2526 |
TableData *d; |
| 2527 |
size_t l; |
| 2528 |
|
| 2529 |
assert_se(d = row[t->display_map ? t->display_map[j] : j]); |
| 2530 |
|
| 2531 |
if (colors_enabled()) |
| 2532 |
field = table_data_format( |
| 2533 |
t, |
| 2534 |
d, |
| 2535 |
/* avoid_uppercasing= */ false, |
| 2536 |
width[j], |
| 2537 |
/* have_soft= */ NULL); |
| 2538 |
else |
| 2539 |
field = table_data_format_strip_ansi( |
| 2540 |
t, |
| 2541 |
d, |
| 2542 |
/* avoid_uppercasing= */ false, |
| 2543 |
width[j], |
| 2544 |
/* have_soft= */ NULL, |
| 2545 |
&stripped_ansi_buffer); |
| 2546 |
if (!field) |
| 2547 |
return -ENOMEM; |
| 2548 |
|
| 2549 |
r = string_extract_line(field, n_subline, &extracted); |
| 2550 |
if (r < 0) |
| 2551 |
return r; |
| 2552 |
if (r > 0) { |
| 2553 |
/* There are more lines to come */ |
| 2554 |
if ((t->cell_height_max == SIZE_MAX || n_subline + 1 < t->cell_height_max)) |
| 2555 |
more_sublines = true; /* There are more lines to come */ |
| 2556 |
else |
| 2557 |
lines_truncated = true; |
| 2558 |
} |
| 2559 |
if (extracted) |
| 2560 |
field = extracted; |
| 2561 |
|
| 2562 |
l = utf8_console_width(field); |
| 2563 |
if (l > width[j]) { |
| 2564 |
/* Field is wider than allocated space. Let's ellipsize */ |
| 2565 |
|
| 2566 |
buffer = ellipsize(field, width[j], /* ellipsize at the end if we truncated coming lines, otherwise honour configuration */ |
| 2567 |
lines_truncated ? 100 : d->ellipsize_percent); |
| 2568 |
if (!buffer) |
| 2569 |
return -ENOMEM; |
| 2570 |
|
| 2571 |
field = buffer; |
| 2572 |
} else { |
| 2573 |
if (lines_truncated) { |
| 2574 |
_cleanup_free_ char *padded = NULL; |
| 2575 |
|
| 2576 |
/* We truncated more lines of this cell, let's add an |
| 2577 |
* ellipsis. We first append it, but that might make our |
| 2578 |
* string grow above what we have space for, hence ellipsize |
| 2579 |
* right after. This will truncate the ellipsis and add a new |
| 2580 |
* one. */ |
| 2581 |
|
| 2582 |
padded = strjoin(field, glyph(GLYPH_ELLIPSIS)); |
| 2583 |
if (!padded) |
| 2584 |
return -ENOMEM; |
| 2585 |
|
| 2586 |
buffer = ellipsize(padded, width[j], 100); |
| 2587 |
if (!buffer) |
| 2588 |
return -ENOMEM; |
| 2589 |
|
| 2590 |
field = buffer; |
| 2591 |
l = utf8_console_width(field); |
| 2592 |
} |
| 2593 |
|
| 2594 |
if (l < width[j]) { |
| 2595 |
_cleanup_free_ char *aligned = NULL; |
| 2596 |
/* Field is shorter than allocated space. Let's align with spaces */ |
| 2597 |
|
| 2598 |
aligned = align_string_mem(field, d->url, width[j], d->align_percent); |
| 2599 |
if (!aligned) |
| 2600 |
return -ENOMEM; |
| 2601 |
|
| 2602 |
/* Drop trailing white spaces of last column when no cosmetics is set. */ |
| 2603 |
if (j == display_columns - 1 && |
| 2604 |
(!colors_enabled() || !table_data_color(d)) && |
| 2605 |
(!underline_enabled() || !table_data_underline(d)) && |
| 2606 |
(!urlify_enabled() || !d->url)) |
| 2607 |
delete_trailing_chars(aligned, NULL); |
| 2608 |
|
| 2609 |
free_and_replace(buffer, aligned); |
| 2610 |
field = buffer; |
| 2611 |
} |
| 2612 |
} |
| 2613 |
|
| 2614 |
if (l >= width[j] && d->url) { |
| 2615 |
_cleanup_free_ char *clickable = NULL; |
| 2616 |
|
| 2617 |
r = terminal_urlify(d->url, field, &clickable); |
| 2618 |
if (r < 0) |
| 2619 |
return r; |
| 2620 |
|
| 2621 |
free_and_replace(buffer, clickable); |
| 2622 |
field = buffer; |
| 2623 |
} |
| 2624 |
|
| 2625 |
if (colors_enabled() && gap_color) |
| 2626 |
fputs(gap_color, f); |
| 2627 |
if (underline_enabled() && gap_underline) |
| 2628 |
fputs(gap_underline, f); |
| 2629 |
|
| 2630 |
if (j > 0) |
| 2631 |
fputc(' ', f); /* column separator left of cell */ |
| 2632 |
|
| 2633 |
/* Undo gap color/underline */ |
| 2634 |
if ((colors_enabled() && gap_color) || |
| 2635 |
(underline_enabled() && gap_underline)) |
| 2636 |
fputs(ANSI_NORMAL, f); |
| 2637 |
|
| 2638 |
if (colors_enabled()) { |
| 2639 |
color = table_data_color(d); |
| 2640 |
if (color) |
| 2641 |
fputs(color, f); |
| 2642 |
} |
| 2643 |
|
| 2644 |
if (underline_enabled()) { |
| 2645 |
underline = table_data_underline(d); |
| 2646 |
if (underline) |
| 2647 |
fputs(underline, f); |
| 2648 |
} |
| 2649 |
|
| 2650 |
fputs(field, f); |
| 2651 |
|
| 2652 |
/* Reset color afterwards if colors was set or the string to output contained ANSI sequences. */ |
| 2653 |
if (color || underline || (d->type == TABLE_STRING_WITH_ANSI && colors_enabled())) |
| 2654 |
fputs(ANSI_NORMAL, f); |
| 2655 |
|
| 2656 |
gap_color = d->rgap_color; |
| 2657 |
gap_underline = table_data_rgap_underline(d); |
| 2658 |
} |
| 2659 |
|
| 2660 |
fputc('\n', f); |
| 2661 |
n_subline++; |
| 2662 |
} while (more_sublines); |
| 2663 |
} |
| 2664 |
|
| 2665 |
if (!flush) |
| 2666 |
return 0; |
| 2667 |
|
| 2668 |
return fflush_and_check(f); |
| 2669 |
} |
| 2670 |
|
| 2671 |
int table_print_or_warn(Table *t) { |
| 2672 |
int r; |
| 2673 |
|
| 2674 |
r = table_print(t); |
| 2675 |
if (r < 0) |
| 2676 |
return table_log_print_error(r); |
| 2677 |
return 0; |
| 2678 |
} |
| 2679 |
|
| 2680 |
int table_format(Table *t, char **ret) { |
| 2681 |
_cleanup_(memstream_done) MemStream m = {}; |
| 2682 |
FILE *f; |
| 2683 |
int r; |
| 2684 |
|
| 2685 |
assert(t); |
| 2686 |
assert(ret); |
| 2687 |
|
| 2688 |
f = memstream_init(&m); |
| 2689 |
if (!f) |
| 2690 |
return -ENOMEM; |
| 2691 |
|
| 2692 |
r = table_print_full(t, f, /* flush= */ true); |
| 2693 |
if (r < 0) |
| 2694 |
return r; |
| 2695 |
|
| 2696 |
return memstream_finalize(&m, ret, NULL); |
| 2697 |
} |
| 2698 |
|
| 2699 |
size_t table_get_rows(Table *t) { |
| 2700 |
if (!t) |
| 2701 |
return 0; |
| 2702 |
|
| 2703 |
assert(t->n_columns > 0); |
| 2704 |
return t->n_cells / t->n_columns; |
| 2705 |
} |
| 2706 |
|
| 2707 |
size_t table_get_columns(Table *t) { |
| 2708 |
if (!t) |
| 2709 |
return 0; |
| 2710 |
|
| 2711 |
assert(t->n_columns > 0); |
| 2712 |
return t->n_columns; |
| 2713 |
} |
| 2714 |
|
| 2715 |
size_t table_get_current_column(Table *t) { |
| 2716 |
if (!t) |
| 2717 |
return 0; |
| 2718 |
|
| 2719 |
assert(t->n_columns > 0); |
| 2720 |
return t->n_cells % t->n_columns; |
| 2721 |
} |
| 2722 |
|
| 2723 |
int table_set_reverse(Table *t, size_t column, bool b) { |
| 2724 |
assert(t); |
| 2725 |
assert(column < t->n_columns); |
| 2726 |
|
| 2727 |
if (!t->reverse_map) { |
| 2728 |
if (!b) |
| 2729 |
return 0; |
| 2730 |
|
| 2731 |
t->reverse_map = new0(bool, t->n_columns); |
| 2732 |
if (!t->reverse_map) |
| 2733 |
return -ENOMEM; |
| 2734 |
} |
| 2735 |
|
| 2736 |
t->reverse_map[column] = b; |
| 2737 |
return 0; |
| 2738 |
} |
| 2739 |
|
| 2740 |
TableCell* table_get_cell(Table *t, size_t row, size_t column) { |
| 2741 |
size_t i; |
| 2742 |
|
| 2743 |
assert(t); |
| 2744 |
|
| 2745 |
if (column >= t->n_columns) |
| 2746 |
return NULL; |
| 2747 |
|
| 2748 |
i = row * t->n_columns + column; |
| 2749 |
if (i >= t->n_cells) |
| 2750 |
return NULL; |
| 2751 |
|
| 2752 |
return TABLE_INDEX_TO_CELL(i); |
| 2753 |
} |
| 2754 |
|
| 2755 |
const void* table_get(Table *t, TableCell *cell) { |
| 2756 |
TableData *d; |
| 2757 |
|
| 2758 |
assert(t); |
| 2759 |
|
| 2760 |
d = table_get_data(t, cell); |
| 2761 |
if (!d) |
| 2762 |
return NULL; |
| 2763 |
|
| 2764 |
return d->data; |
| 2765 |
} |
| 2766 |
|
| 2767 |
const void* table_get_at(Table *t, size_t row, size_t column) { |
| 2768 |
TableCell *cell; |
| 2769 |
|
| 2770 |
cell = table_get_cell(t, row, column); |
| 2771 |
if (!cell) |
| 2772 |
return NULL; |
| 2773 |
|
| 2774 |
return table_get(t, cell); |
| 2775 |
} |
| 2776 |
|
| 2777 |
static int table_data_to_json(TableData *d, sd_json_variant **ret) { |
| 2778 |
|
| 2779 |
switch (d->type) { |
| 2780 |
|
| 2781 |
case TABLE_EMPTY: |
| 2782 |
return sd_json_variant_new_null(ret); |
| 2783 |
|
| 2784 |
case TABLE_STRING: |
| 2785 |
case TABLE_PATH: |
| 2786 |
case TABLE_PATH_BASENAME: |
| 2787 |
case TABLE_FIELD: |
| 2788 |
case TABLE_HEADER: |
| 2789 |
case TABLE_VERSION: |
| 2790 |
return sd_json_variant_new_string(ret, d->string); |
| 2791 |
|
| 2792 |
case TABLE_STRV: |
| 2793 |
case TABLE_STRV_WRAPPED: |
| 2794 |
return sd_json_variant_new_array_strv(ret, d->strv); |
| 2795 |
|
| 2796 |
case TABLE_BOOLEAN_CHECKMARK: |
| 2797 |
case TABLE_BOOLEAN: |
| 2798 |
return sd_json_variant_new_boolean(ret, d->boolean); |
| 2799 |
|
| 2800 |
case TABLE_TRISTATE: |
| 2801 |
if (d->tristate < 0) |
| 2802 |
return sd_json_variant_new_null(ret); |
| 2803 |
|
| 2804 |
return sd_json_variant_new_boolean(ret, d->tristate); |
| 2805 |
|
| 2806 |
case TABLE_TIMESTAMP: |
| 2807 |
case TABLE_TIMESTAMP_UTC: |
| 2808 |
case TABLE_TIMESTAMP_RELATIVE: |
| 2809 |
case TABLE_TIMESTAMP_RELATIVE_MONOTONIC: |
| 2810 |
case TABLE_TIMESTAMP_LEFT: |
| 2811 |
case TABLE_TIMESTAMP_DATE: |
| 2812 |
if (d->timestamp == USEC_INFINITY) |
| 2813 |
return sd_json_variant_new_null(ret); |
| 2814 |
|
| 2815 |
return sd_json_variant_new_unsigned(ret, d->timestamp); |
| 2816 |
|
| 2817 |
case TABLE_TIMESPAN: |
| 2818 |
case TABLE_TIMESPAN_MSEC: |
| 2819 |
case TABLE_TIMESPAN_DAY: |
| 2820 |
if (d->timespan == USEC_INFINITY) |
| 2821 |
return sd_json_variant_new_null(ret); |
| 2822 |
|
| 2823 |
return sd_json_variant_new_unsigned(ret, d->timespan); |
| 2824 |
|
| 2825 |
case TABLE_SIZE: |
| 2826 |
case TABLE_BPS: |
| 2827 |
if (d->size == UINT64_MAX) |
| 2828 |
return sd_json_variant_new_null(ret); |
| 2829 |
|
| 2830 |
return sd_json_variant_new_unsigned(ret, d->size); |
| 2831 |
|
| 2832 |
case TABLE_INT: |
| 2833 |
return sd_json_variant_new_integer(ret, d->int_val); |
| 2834 |
|
| 2835 |
case TABLE_INT8: |
| 2836 |
return sd_json_variant_new_integer(ret, d->int8); |
| 2837 |
|
| 2838 |
case TABLE_INT16: |
| 2839 |
return sd_json_variant_new_integer(ret, d->int16); |
| 2840 |
|
| 2841 |
case TABLE_INT32: |
| 2842 |
return sd_json_variant_new_integer(ret, d->int32); |
| 2843 |
|
| 2844 |
case TABLE_INT64: |
| 2845 |
return sd_json_variant_new_integer(ret, d->int64); |
| 2846 |
|
| 2847 |
case TABLE_UINT: |
| 2848 |
return sd_json_variant_new_unsigned(ret, d->uint_val); |
| 2849 |
|
| 2850 |
case TABLE_UINT8: |
| 2851 |
return sd_json_variant_new_unsigned(ret, d->uint8); |
| 2852 |
|
| 2853 |
case TABLE_UINT16: |
| 2854 |
return sd_json_variant_new_unsigned(ret, d->uint16); |
| 2855 |
|
| 2856 |
case TABLE_UINT32: |
| 2857 |
case TABLE_UINT32_HEX: |
| 2858 |
case TABLE_UINT32_HEX_0x: |
| 2859 |
return sd_json_variant_new_unsigned(ret, d->uint32); |
| 2860 |
|
| 2861 |
case TABLE_UINT64: |
| 2862 |
case TABLE_UINT64_HEX: |
| 2863 |
case TABLE_UINT64_HEX_0x: |
| 2864 |
return sd_json_variant_new_unsigned(ret, d->uint64); |
| 2865 |
|
| 2866 |
case TABLE_PERCENT: |
| 2867 |
return sd_json_variant_new_integer(ret, d->percent); |
| 2868 |
|
| 2869 |
case TABLE_IFINDEX: |
| 2870 |
if (d->ifindex <= 0) |
| 2871 |
return sd_json_variant_new_null(ret); |
| 2872 |
|
| 2873 |
return sd_json_variant_new_integer(ret, d->ifindex); |
| 2874 |
|
| 2875 |
case TABLE_IN_ADDR: |
| 2876 |
return sd_json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET)); |
| 2877 |
|
| 2878 |
case TABLE_IN6_ADDR: |
| 2879 |
return sd_json_variant_new_array_bytes(ret, &d->address, FAMILY_ADDRESS_SIZE(AF_INET6)); |
| 2880 |
|
| 2881 |
case TABLE_ID128: |
| 2882 |
return sd_json_variant_new_id128(ret, d->id128); |
| 2883 |
|
| 2884 |
case TABLE_UUID: |
| 2885 |
return sd_json_variant_new_uuid(ret, d->id128); |
| 2886 |
|
| 2887 |
case TABLE_UID: |
| 2888 |
if (!uid_is_valid(d->uid)) |
| 2889 |
return sd_json_variant_new_null(ret); |
| 2890 |
|
| 2891 |
return sd_json_variant_new_integer(ret, d->uid); |
| 2892 |
|
| 2893 |
case TABLE_GID: |
| 2894 |
if (!gid_is_valid(d->gid)) |
| 2895 |
return sd_json_variant_new_null(ret); |
| 2896 |
|
| 2897 |
return sd_json_variant_new_integer(ret, d->gid); |
| 2898 |
|
| 2899 |
case TABLE_PID: |
| 2900 |
if (!pid_is_valid(d->pid)) |
| 2901 |
return sd_json_variant_new_null(ret); |
| 2902 |
|
| 2903 |
return sd_json_variant_new_integer(ret, d->pid); |
| 2904 |
|
| 2905 |
case TABLE_SIGNAL: |
| 2906 |
if (!SIGNAL_VALID(d->int_val)) |
| 2907 |
return sd_json_variant_new_null(ret); |
| 2908 |
|
| 2909 |
return sd_json_variant_new_integer(ret, d->int_val); |
| 2910 |
|
| 2911 |
case TABLE_MODE: |
| 2912 |
case TABLE_MODE_INODE_TYPE: |
| 2913 |
if (d->mode == MODE_INVALID) |
| 2914 |
return sd_json_variant_new_null(ret); |
| 2915 |
|
| 2916 |
return sd_json_variant_new_unsigned(ret, d->mode); |
| 2917 |
|
| 2918 |
case TABLE_DEVNUM: |
| 2919 |
if (devnum_is_zero(d->devnum)) |
| 2920 |
return sd_json_variant_new_null(ret); |
| 2921 |
|
| 2922 |
return sd_json_build(ret, SD_JSON_BUILD_ARRAY( |
| 2923 |
SD_JSON_BUILD_UNSIGNED(major(d->devnum)), |
| 2924 |
SD_JSON_BUILD_UNSIGNED(minor(d->devnum)))); |
| 2925 |
|
| 2926 |
case TABLE_JSON: |
| 2927 |
if (!d->json) |
| 2928 |
return sd_json_variant_new_null(ret); |
| 2929 |
|
| 2930 |
if (ret) |
| 2931 |
*ret = sd_json_variant_ref(d->json); |
| 2932 |
|
| 2933 |
return 0; |
| 2934 |
|
| 2935 |
case TABLE_STRING_WITH_ANSI: { |
| 2936 |
_cleanup_free_ char *s = strdup(d->string); |
| 2937 |
if (!s) |
| 2938 |
return -ENOMEM; |
| 2939 |
|
| 2940 |
/* We strip the ANSI data when outputting to JSON */ |
| 2941 |
if (!strip_tab_ansi(&s, /* isz= */ NULL, /* highlight= */ NULL)) |
| 2942 |
return -ENOMEM; |
| 2943 |
|
| 2944 |
return sd_json_variant_new_string(ret, s); |
| 2945 |
} |
| 2946 |
|
| 2947 |
default: |
| 2948 |
return -EINVAL; |
| 2949 |
} |
| 2950 |
} |
| 2951 |
|
| 2952 |
char* table_mangle_to_json_field_name(const char *str) { |
| 2953 |
/* Tries to make a string more suitable as JSON field name. There are no strict rules defined what a |
| 2954 |
* field name can be hence this is a bit vague and black magic. Here's what we do: |
| 2955 |
* - Convert spaces to underscores |
| 2956 |
* - Convert dashes to underscores (some JSON parsers don't like dealing with dashes) |
| 2957 |
* - Convert most other symbols to underscores (for similar reasons) |
| 2958 |
* - Make the first letter of each word lowercase (unless it looks like the whole word is uppercase) |
| 2959 |
*/ |
| 2960 |
|
| 2961 |
bool new_word = true; |
| 2962 |
char *c; |
| 2963 |
|
| 2964 |
assert(str); |
| 2965 |
|
| 2966 |
c = strdup(str); |
| 2967 |
if (!c) |
| 2968 |
return NULL; |
| 2969 |
|
| 2970 |
for (char *x = c; *x; x++) { |
| 2971 |
if (!strchr(ALPHANUMERICAL, *x)) { |
| 2972 |
*x = '_'; |
| 2973 |
new_word = true; |
| 2974 |
continue; |
| 2975 |
} |
| 2976 |
|
| 2977 |
if (new_word) { |
| 2978 |
if (ascii_tolower(*(x + 1)) == *(x + 1)) /* Heuristic: if next char is upper-case |
| 2979 |
* then we assume the whole word is all-caps |
| 2980 |
* and avoid lowercasing it. */ |
| 2981 |
*x = ascii_tolower(*x); |
| 2982 |
new_word = false; |
| 2983 |
} |
| 2984 |
} |
| 2985 |
|
| 2986 |
return c; |
| 2987 |
} |
| 2988 |
|
| 2989 |
static int table_make_json_field_name(Table *t, TableData *d, char **ret) { |
| 2990 |
_cleanup_free_ char *mangled = NULL, *buffer = NULL; |
| 2991 |
const char *n; |
| 2992 |
|
| 2993 |
assert(t); |
| 2994 |
assert(d); |
| 2995 |
assert(ret); |
| 2996 |
|
| 2997 |
if (IN_SET(d->type, TABLE_HEADER, TABLE_FIELD)) |
| 2998 |
n = d->string; |
| 2999 |
else { |
| 3000 |
n = table_data_format_strip_ansi( |
| 3001 |
t, |
| 3002 |
d, |
| 3003 |
/* avoid_uppercasing= */ true, |
| 3004 |
/* column_width= */ SIZE_MAX, |
| 3005 |
/* have_soft= */ NULL, |
| 3006 |
&buffer); |
| 3007 |
if (!n) |
| 3008 |
return -ENOMEM; |
| 3009 |
} |
| 3010 |
|
| 3011 |
mangled = table_mangle_to_json_field_name(n); |
| 3012 |
if (!mangled) |
| 3013 |
return -ENOMEM; |
| 3014 |
|
| 3015 |
*ret = TAKE_PTR(mangled); |
| 3016 |
return 0; |
| 3017 |
} |
| 3018 |
|
| 3019 |
static const char* table_get_json_field_name(Table *t, size_t idx) { |
| 3020 |
assert(t); |
| 3021 |
|
| 3022 |
return idx < t->n_json_fields ? t->json_fields[idx] : NULL; |
| 3023 |
} |
| 3024 |
|
| 3025 |
static int table_to_json_regular(Table *t, sd_json_variant **ret) { |
| 3026 |
sd_json_variant **rows = NULL, **elements = NULL; |
| 3027 |
_cleanup_free_ size_t *sorted = NULL; |
| 3028 |
size_t n_rows, display_columns; |
| 3029 |
int r; |
| 3030 |
|
| 3031 |
assert(t); |
| 3032 |
assert(!t->vertical); |
| 3033 |
|
| 3034 |
/* Ensure we have no incomplete rows */ |
| 3035 |
assert(t->n_columns > 0); |
| 3036 |
assert(t->n_cells % t->n_columns == 0); |
| 3037 |
|
| 3038 |
n_rows = t->n_cells / t->n_columns; |
| 3039 |
assert(n_rows > 0); /* at least the header row must be complete */ |
| 3040 |
|
| 3041 |
if (t->sort_map) { |
| 3042 |
/* If sorting is requested, let's calculate an index table we use to lookup the actual index to display with. */ |
| 3043 |
|
| 3044 |
sorted = new(size_t, n_rows); |
| 3045 |
if (!sorted) |
| 3046 |
return -ENOMEM; |
| 3047 |
|
| 3048 |
for (size_t i = 0; i < n_rows; i++) |
| 3049 |
sorted[i] = i * t->n_columns; |
| 3050 |
|
| 3051 |
typesafe_qsort_r(sorted, n_rows, table_data_compare, t); |
| 3052 |
} |
| 3053 |
|
| 3054 |
if (t->display_map) |
| 3055 |
display_columns = t->n_display_map; |
| 3056 |
else |
| 3057 |
display_columns = t->n_columns; |
| 3058 |
assert(display_columns > 0); |
| 3059 |
|
| 3060 |
elements = new0(sd_json_variant*, display_columns * 2); |
| 3061 |
if (!elements) |
| 3062 |
return -ENOMEM; |
| 3063 |
|
| 3064 |
CLEANUP_ARRAY(elements, (size_t) { display_columns * 2 }, sd_json_variant_unref_many); |
| 3065 |
|
| 3066 |
for (size_t j = 0; j < display_columns; j++) { |
| 3067 |
_cleanup_free_ char *mangled = NULL; |
| 3068 |
const char *n; |
| 3069 |
size_t c; |
| 3070 |
|
| 3071 |
c = t->display_map ? t->display_map[j] : j; |
| 3072 |
|
| 3073 |
/* Use explicitly set JSON field name, if we have one. Otherwise mangle the column field value. */ |
| 3074 |
n = table_get_json_field_name(t, c); |
| 3075 |
if (!n) { |
| 3076 |
r = table_make_json_field_name(t, ASSERT_PTR(t->data[c]), &mangled); |
| 3077 |
if (r < 0) |
| 3078 |
return r; |
| 3079 |
|
| 3080 |
n = mangled; |
| 3081 |
} |
| 3082 |
|
| 3083 |
r = sd_json_variant_new_string(elements + j*2, n); |
| 3084 |
if (r < 0) |
| 3085 |
return r; |
| 3086 |
} |
| 3087 |
|
| 3088 |
rows = new0(sd_json_variant*, n_rows-1); |
| 3089 |
if (!rows) |
| 3090 |
return -ENOMEM; |
| 3091 |
|
| 3092 |
CLEANUP_ARRAY(rows, (size_t) { n_rows - 1 }, sd_json_variant_unref_many); |
| 3093 |
|
| 3094 |
for (size_t i = 1; i < n_rows; i++) { |
| 3095 |
TableData **row; |
| 3096 |
|
| 3097 |
if (sorted) |
| 3098 |
row = t->data + sorted[i]; |
| 3099 |
else |
| 3100 |
row = t->data + i * t->n_columns; |
| 3101 |
|
| 3102 |
for (size_t j = 0; j < display_columns; j++) { |
| 3103 |
TableData *d; |
| 3104 |
size_t k; |
| 3105 |
|
| 3106 |
assert_se(d = row[t->display_map ? t->display_map[j] : j]); |
| 3107 |
|
| 3108 |
k = j*2+1; |
| 3109 |
elements[k] = sd_json_variant_unref(elements[k]); |
| 3110 |
|
| 3111 |
r = table_data_to_json(d, elements + k); |
| 3112 |
if (r < 0) |
| 3113 |
return r; |
| 3114 |
} |
| 3115 |
|
| 3116 |
r = sd_json_variant_new_object(rows + i - 1, elements, display_columns * 2); |
| 3117 |
if (r < 0) |
| 3118 |
return r; |
| 3119 |
} |
| 3120 |
|
| 3121 |
return sd_json_variant_new_array(ret, rows, n_rows - 1); |
| 3122 |
} |
| 3123 |
|
| 3124 |
static int table_to_json_vertical(Table *t, sd_json_variant **ret) { |
| 3125 |
sd_json_variant **elements = NULL; |
| 3126 |
size_t n_elements = 0; |
| 3127 |
int r; |
| 3128 |
|
| 3129 |
assert(t); |
| 3130 |
assert(t->vertical); |
| 3131 |
|
| 3132 |
if (t->n_columns != 2) |
| 3133 |
return -EINVAL; |
| 3134 |
|
| 3135 |
/* Ensure we have no incomplete rows */ |
| 3136 |
assert(t->n_cells % t->n_columns == 0); |
| 3137 |
|
| 3138 |
elements = new0(sd_json_variant *, t->n_cells); |
| 3139 |
if (!elements) |
| 3140 |
return -ENOMEM; |
| 3141 |
|
| 3142 |
CLEANUP_ARRAY(elements, n_elements, sd_json_variant_unref_many); |
| 3143 |
|
| 3144 |
for (size_t i = t->n_columns; i < t->n_cells; i++) { |
| 3145 |
|
| 3146 |
if (i % t->n_columns == 0) { |
| 3147 |
_cleanup_free_ char *mangled = NULL; |
| 3148 |
const char *n; |
| 3149 |
|
| 3150 |
n = table_get_json_field_name(t, i / t->n_columns - 1); |
| 3151 |
if (!n) { |
| 3152 |
r = table_make_json_field_name(t, ASSERT_PTR(t->data[i]), &mangled); |
| 3153 |
if (r < 0) |
| 3154 |
return r; |
| 3155 |
|
| 3156 |
n = mangled; |
| 3157 |
} |
| 3158 |
|
| 3159 |
r = sd_json_variant_new_string(elements + n_elements, n); |
| 3160 |
} else |
| 3161 |
r = table_data_to_json(t->data[i], elements + n_elements); |
| 3162 |
if (r < 0) |
| 3163 |
return r; |
| 3164 |
|
| 3165 |
n_elements++; |
| 3166 |
} |
| 3167 |
|
| 3168 |
return sd_json_variant_new_object(ret, elements, n_elements); |
| 3169 |
} |
| 3170 |
|
| 3171 |
int table_to_json(Table *t, sd_json_variant **ret) { |
| 3172 |
assert(t); |
| 3173 |
|
| 3174 |
if (t->vertical) |
| 3175 |
return table_to_json_vertical(t, ret); |
| 3176 |
|
| 3177 |
return table_to_json_regular(t, ret); |
| 3178 |
} |
| 3179 |
|
| 3180 |
int table_print_json(Table *t, FILE *f, sd_json_format_flags_t flags) { |
| 3181 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 3182 |
int r; |
| 3183 |
|
| 3184 |
assert(t); |
| 3185 |
|
| 3186 |
if (!sd_json_format_enabled(flags)) /* If JSON output is turned off, use regular output */ |
| 3187 |
return table_print_full(t, f, /* flush= */ true); |
| 3188 |
|
| 3189 |
if (!f) |
| 3190 |
f = stdout; |
| 3191 |
|
| 3192 |
r = table_to_json(t, &v); |
| 3193 |
if (r < 0) |
| 3194 |
return r; |
| 3195 |
|
| 3196 |
sd_json_variant_dump(v, flags, f, NULL); |
| 3197 |
|
| 3198 |
return fflush_and_check(f); |
| 3199 |
} |
| 3200 |
|
| 3201 |
int table_print_with_pager( |
| 3202 |
Table *t, |
| 3203 |
sd_json_format_flags_t json_format_flags, |
| 3204 |
PagerFlags pager_flags, |
| 3205 |
bool show_header) { |
| 3206 |
|
| 3207 |
bool saved_header; |
| 3208 |
int r; |
| 3209 |
|
| 3210 |
assert(t); |
| 3211 |
|
| 3212 |
/* An all-in-one solution for showing tables, and turning on a pager first. Also optionally suppresses |
| 3213 |
* the table header and logs about any error. */ |
| 3214 |
|
| 3215 |
if (json_format_flags & (SD_JSON_FORMAT_OFF|SD_JSON_FORMAT_PRETTY|SD_JSON_FORMAT_PRETTY_AUTO)) |
| 3216 |
pager_open(pager_flags); |
| 3217 |
|
| 3218 |
saved_header = t->header; |
| 3219 |
t->header = show_header; |
| 3220 |
r = table_print_json(t, stdout, json_format_flags); |
| 3221 |
t->header = saved_header; |
| 3222 |
if (r < 0) |
| 3223 |
return table_log_print_error(r); |
| 3224 |
|
| 3225 |
return 0; |
| 3226 |
} |
| 3227 |
|
| 3228 |
int table_set_json_field_name(Table *t, size_t idx, const char *name) { |
| 3229 |
int r; |
| 3230 |
|
| 3231 |
assert(t); |
| 3232 |
|
| 3233 |
if (name) { |
| 3234 |
size_t m; |
| 3235 |
|
| 3236 |
m = MAX(idx + 1, t->n_json_fields); |
| 3237 |
if (!GREEDY_REALLOC0(t->json_fields, m)) |
| 3238 |
return -ENOMEM; |
| 3239 |
|
| 3240 |
r = free_and_strdup(t->json_fields + idx, name); |
| 3241 |
if (r < 0) |
| 3242 |
return r; |
| 3243 |
|
| 3244 |
t->n_json_fields = m; |
| 3245 |
return r; |
| 3246 |
} else { |
| 3247 |
if (idx >= t->n_json_fields) |
| 3248 |
return 0; |
| 3249 |
|
| 3250 |
t->json_fields[idx] = mfree(t->json_fields[idx]); |
| 3251 |
return 1; |
| 3252 |
} |
| 3253 |
} |