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 
verb_exit_status(int argc,char * argv[],void * userdata)8 int verb_exit_status(int argc, char *argv[], void *userdata) {
9         _cleanup_(table_unrefp) Table *table = NULL;
10         int r;
11 
12         table = table_new("name", "status", "class");
13         if (!table)
14                 return log_oom();
15 
16         r = table_set_align_percent(table, table_get_cell(table, 0, 1), 100);
17         if (r < 0)
18                 return log_error_errno(r, "Failed to right-align status: %m");
19 
20         if (strv_isempty(strv_skip(argv, 1)))
21                 for (size_t i = 0; i < ELEMENTSOF(exit_status_mappings); i++) {
22                         if (!exit_status_mappings[i].name)
23                                 continue;
24 
25                         r = table_add_many(table,
26                                            TABLE_STRING, exit_status_mappings[i].name,
27                                            TABLE_INT, (int) i,
28                                            TABLE_STRING, exit_status_class(i));
29                         if (r < 0)
30                                 return table_log_add_error(r);
31                 }
32         else
33                 for (int i = 1; i < argc; i++) {
34                         int status;
35 
36                         status = exit_status_from_string(argv[i]);
37                         if (status < 0)
38                                 return log_error_errno(status, "Invalid exit status \"%s\".", argv[i]);
39 
40                         assert(status >= 0 && (size_t) status < ELEMENTSOF(exit_status_mappings));
41                         r = table_add_many(table,
42                                            TABLE_STRING, exit_status_mappings[status].name ?: "-",
43                                            TABLE_INT, status,
44                                            TABLE_STRING, exit_status_class(status) ?: "-");
45                         if (r < 0)
46                                 return table_log_add_error(r);
47                 }
48 
49         pager_open(arg_pager_flags);
50 
51         return table_print(table, NULL);
52 }
53