]> git.ozlabs.org Git - petitboot/blob - utils/pb-config.c
ui/ncurses: Update config screen help text
[petitboot] / utils / pb-config.c
1
2 #define _GNU_SOURCE
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <stdio.h>
7 #include <getopt.h>
8 #include <string.h>
9
10 #include <log/log.h>
11 #include <pb-config/pb-config.h>
12 #include <types/types.h>
13 #include <waiter/waiter.h>
14 #include <process/process.h>
15 #include <talloc/talloc.h>
16
17 extern struct config *config_get(void);
18 extern int platform_init(void);
19
20 static const struct option options[] = {
21         {
22                 .name = "list",
23                 .val = 'l',
24         },
25         { 0 },
26 };
27
28 static void usage(const char *progname)
29 {
30         fprintf(stderr, "Usage:\t%1$s <var>\n"
31                               "\t%1$s --list\n", progname);
32 }
33
34 static void print_one_config(void *ctx, const char *req, const char *name,
35                 const char *fmt, ...)
36 {
37         bool use_prefix = !req;
38         char *val, *sep;
39         va_list ap;
40
41         if (req && strcmp(req, name))
42                 return;
43
44         va_start(ap, fmt);
45         val = talloc_vasprintf(ctx, fmt, ap);
46         va_end(ap);
47
48         if (!strcmp(val, "(null)")) {
49                 talloc_free(val);
50                 if (!use_prefix)
51                         return;
52                 val = talloc_strdup(ctx, "");
53         }
54
55         sep = use_prefix ? ": " : "";
56
57         printf("%s%s%s\n", use_prefix ? name : "", sep, val);
58
59         talloc_free(val);
60 }
61
62 static void print_config(void *ctx, struct config *config, const char *var)
63 {
64         unsigned int i;
65
66         for (i = 0; i < config->n_autoboot_opts; i++) {
67                 if (config->autoboot_opts[i].boot_type == BOOT_DEVICE_TYPE)
68                         print_one_config(ctx, var, "bootdev", "%s",
69                          device_type_name(config->autoboot_opts[i].type));
70                 else
71                         print_one_config(ctx, var, "bootdev", "%s",
72                                          config->autoboot_opts[i].uuid);
73         }
74         print_one_config(ctx, var, "autoboot", "%s",
75                         config->autoboot_enabled ? "enabled" : "disabled");
76         print_one_config(ctx, var, "timeout", "%d",
77                         config->autoboot_timeout_sec);
78         print_one_config(ctx, var, "safe-mode", "%s",
79                         config->safe_mode ? "enabled" : "disabled");
80         print_one_config(ctx, var, "debug", "%s",
81                         config->debug ? "enabled" : "disabled");
82 }
83
84 int main(int argc, char **argv)
85 {
86         struct waitset *waitset;
87         struct config *config;
88         void *ctx;
89         bool list;
90
91         list = false;
92
93         for (;;) {
94                 int opt = getopt_long(argc, argv, "l", options, NULL);
95                 if (opt == -1)
96                         break;
97
98                 switch (opt) {
99                 case 'l':
100                         list = true;
101                         break;
102                 default:
103                         usage(argv[0]);
104                         return EXIT_FAILURE;
105                 }
106         }
107
108         ctx = talloc_new(NULL);
109
110         waitset = waitset_create(ctx);
111
112         process_init(ctx, waitset, false);
113
114         platform_init();
115
116         pb_log_init(stderr);
117
118         config = config_get();
119
120         if (list) {
121                 print_config(ctx, config, NULL);
122         } else {
123                 int i;
124
125                 for (i = optind; i < argc; i++)
126                         print_config(ctx, config, argv[i]);
127         }
128
129         talloc_free(ctx);
130
131         return EXIT_SUCCESS;
132 }