| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "analyze.h" |
| 4 |
#include "analyze-exit-status.h" |
| 5 |
#include "exit-status.h" |
| 6 |
#include "format-table.h" |
| 7 |
#include "log.h" |
| 8 |
#include "strv.h" |
| 9 |
|
| 10 |
int verb_exit_status(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 11 |
_cleanup_(table_unrefp) Table *table = NULL; |
| 12 |
int r; |
| 13 |
|
| 14 |
table = table_new("name", "status", "class"); |
| 15 |
if (!table) |
| 16 |
return log_oom(); |
| 17 |
|
| 18 |
r = table_set_align_percent(table, table_get_cell(table, 0, 1), 100); |
| 19 |
if (r < 0) |
| 20 |
return log_error_errno(r, "Failed to right-align status: %m"); |
| 21 |
|
| 22 |
char **args = strv_skip(argv, 1); |
| 23 |
if (args) |
| 24 |
STRV_FOREACH(arg, args) { |
| 25 |
int status; |
| 26 |
|
| 27 |
status = exit_status_from_string(*arg); |
| 28 |
if (status < 0) |
| 29 |
return log_error_errno(status, "Invalid exit status \"%s\".", *arg); |
| 30 |
|
| 31 |
assert(status >= 0 && (size_t) status < ELEMENTSOF(exit_status_mappings)); |
| 32 |
r = table_add_many(table, |
| 33 |
TABLE_STRING, exit_status_mappings[status].name ?: "-", |
| 34 |
TABLE_INT, status, |
| 35 |
TABLE_STRING, exit_status_class(status) ?: "-"); |
| 36 |
if (r < 0) |
| 37 |
return table_log_add_error(r); |
| 38 |
} |
| 39 |
else |
| 40 |
for (size_t i = 0; i < ELEMENTSOF(exit_status_mappings); i++) { |
| 41 |
if (!exit_status_mappings[i].name) |
| 42 |
continue; |
| 43 |
|
| 44 |
r = table_add_many(table, |
| 45 |
TABLE_STRING, exit_status_mappings[i].name, |
| 46 |
TABLE_INT, (int) i, |
| 47 |
TABLE_STRING, exit_status_class(i)); |
| 48 |
if (r < 0) |
| 49 |
return table_log_add_error(r); |
| 50 |
} |
| 51 |
|
| 52 |
r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend); |
| 53 |
if (r < 0) |
| 54 |
return r; |
| 55 |
|
| 56 |
return 0; |
| 57 |
} |