]> git.ozlabs.org Git - petitboot/blob - utils/pb-config.c
utils: Add pb-config utility
[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         print_one_config(ctx, var, "bootdev", "%s", config->boot_device);
65         print_one_config(ctx, var, "autoboot", "%s",
66                         config->autoboot_enabled ? "enabled" : "disabled");
67         print_one_config(ctx, var, "timeout", "%d",
68                         config->autoboot_timeout_sec);
69         print_one_config(ctx, var, "safe-mode", "%s",
70                         config->safe_mode ? "enabled" : "disabled");
71 }
72
73 int main(int argc, char **argv)
74 {
75         struct waitset *waitset;
76         struct config *config;
77         void *ctx;
78         bool list;
79
80         list = false;
81
82         for (;;) {
83                 int opt = getopt_long(argc, argv, "l", options, NULL);
84                 if (opt == -1)
85                         break;
86
87                 switch (opt) {
88                 case 'l':
89                         list = true;
90                         break;
91                 default:
92                         usage(argv[0]);
93                         return EXIT_FAILURE;
94                 }
95         }
96
97         ctx = talloc_new(NULL);
98
99         waitset = waitset_create(ctx);
100
101         process_init(ctx, waitset, false);
102
103         platform_init();
104
105         pb_log_init(stderr);
106
107         config = config_get();
108
109         if (list) {
110                 print_config(ctx, config, NULL);
111         } else {
112                 int i;
113
114                 for (i = optind; i < argc; i++)
115                         print_config(ctx, config, argv[i]);
116         }
117
118         talloc_free(ctx);
119
120         return EXIT_SUCCESS;
121 }