Debian Codegraph @

src/analyze/analyze-architectures.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "analyze.h"
4 #include "analyze-architectures.h"
5 #include "ansi-color.h"
6 #include "architecture.h"
7 #include "format-table.h"
8 #include "log.h"
9 #include "string-util.h"
10 #include "strv.h"
11
12 static int add_arch(Table *t, Architecture a) {
13 const char *c, *color;
14 int r;
15
16 assert(t);
17
18 if (a == native_architecture()) {
19 c = "native";
20 color = ansi_highlight_green();
21 } else if (a == uname_architecture()) {
22 c = "uname";
23 color = ansi_highlight();
24 #ifdef ARCHITECTURE_SECONDARY
25 } else if (a == ARCHITECTURE_SECONDARY) {
26 c = "secondary";
27 color = NULL;
28 #endif
29 } else {
30 c = "foreign";
31 color = ansi_grey();
32 }
33
34 r = table_add_many(t,
35 TABLE_INT, (int) a,
36 TABLE_STRING, architecture_to_string(a),
37 TABLE_STRING, c,
38 TABLE_SET_COLOR, color);
39 if (r < 0)
40 return table_log_add_error(r);
41
42 return 0;
43 }
44
45 int verb_architectures(int argc, char *argv[], uintptr_t _data, void *userdata) {
46 _cleanup_(table_unrefp) Table *table = NULL;
47 int r;
48
49 table = table_new("id", "name", "support");
50 if (!table)
51 return log_oom();
52
53 (void) table_hide_column_from_display(table, (size_t) 0);
54
55 char **args = strv_skip(argv, 1);
56 if (args) {
57 STRV_FOREACH(arg, args) {
58 Architecture a;
59
60 if (streq(*arg, "native"))
61 a = native_architecture();
62 else if (streq(*arg, "uname"))
63 a = uname_architecture();
64 else if (streq(*arg, "secondary")) {
65 #ifdef ARCHITECTURE_SECONDARY
66 a = ARCHITECTURE_SECONDARY;
67 #else
68 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No secondary architecture.");
69 #endif
70 } else
71 a = architecture_from_string(*arg);
72 if (a < 0)
73 return log_error_errno(a, "Architecture \"%s\" not known.", *arg);
74
75 r = add_arch(table, a);
76 if (r < 0)
77 return r;
78 }
79
80 (void) table_set_sort(table, (size_t) 0);
81 } else
82 for (Architecture a = 0; a < _ARCHITECTURE_MAX; a++) {
83 r = add_arch(table, a);
84 if (r < 0)
85 return r;
86 }
87
88 r = table_print_with_pager(table, arg_json_format_flags, arg_pager_flags, arg_legend);
89 if (r < 0)
90 return r;
91
92 return EXIT_SUCCESS;
93 }