ui/ncurses: Update config screen help text
[petitboot] / discover / pb-discover.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5
6 #include <assert.h>
7 #include <getopt.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <string.h>
11
12 #include <waiter/waiter.h>
13 #include <log/log.h>
14 #include <process/process.h>
15 #include <talloc/talloc.h>
16 #include <i18n/i18n.h>
17
18 #include "discover-server.h"
19 #include "device-handler.h"
20 #include "sysinfo.h"
21 #include "platform.h"
22
23 static void print_version(void)
24 {
25         printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
26 }
27
28 static void print_usage(void)
29 {
30         print_version();
31         printf(
32 "Usage: pb-discover [-a, --no-autoboot] [-h, --help] [-l, --log log-file]\n"
33 "                   [-n, --dry-run] [-v, --verbose] [-V, --version]\n");
34 }
35
36 /**
37  * enum opt_value - Tri-state options variables.
38  */
39
40 enum opt_value {opt_undef = 0, opt_yes, opt_no};
41
42 /**
43  * struct opts - Values from command line options.
44  */
45
46 struct opts {
47         enum opt_value no_autoboot;
48         enum opt_value show_help;
49         const char *log_file;
50         enum opt_value dry_run;
51         enum opt_value show_version;
52         enum opt_value verbose;
53 };
54
55 /**
56  * opts_parse - Parse the command line options.
57  */
58
59 static int opts_parse(struct opts *opts, int argc, char *argv[])
60 {
61         static const struct option long_options[] = {
62                 {"no-autoboot",    no_argument,       NULL, 'a'},
63                 {"help",           no_argument,       NULL, 'h'},
64                 {"log",            required_argument, NULL, 'l'},
65                 {"dry-run",        no_argument,       NULL, 'n'},
66                 {"verbose",        no_argument,       NULL, 'v'},
67                 {"version",        no_argument,       NULL, 'V'},
68                 { NULL, 0, NULL, 0},
69         };
70         static const char short_options[] = "ahl:nvV";
71         static const struct opts default_values = {
72                 .no_autoboot = opt_no,
73                 .log_file = "/var/log/petitboot/pb-discover.log",
74                 .dry_run = opt_no,
75                 .verbose = opt_no,
76         };
77
78         *opts = default_values;
79
80         while (1) {
81                 int c = getopt_long(argc, argv, short_options, long_options,
82                         NULL);
83
84                 if (c == EOF)
85                         break;
86
87                 switch (c) {
88                 case 'a':
89                         opts->no_autoboot = opt_yes;
90                         break;
91                 case 'h':
92                         opts->show_help = opt_yes;
93                         break;
94                 case 'l':
95                         opts->log_file = optarg;
96                         break;
97                 case 'n':
98                         opts->dry_run = opt_yes;
99                         break;
100                 case 'v':
101                         opts->verbose = opt_yes;
102                         break;
103                 case 'V':
104                         opts->show_version = opt_yes;
105                         break;
106                 default:
107                         opts->show_help = opt_yes;
108                         return -1;
109                 }
110         }
111
112         return optind != argc;
113 }
114
115 static int running;
116
117 static void sigint_handler(int __attribute__((unused)) signum)
118 {
119         running = 0;
120 }
121
122 int main(int argc, char *argv[])
123 {
124         struct device_handler *handler;
125         struct discover_server *server;
126         struct waitset *waitset;
127         struct procset *procset;
128         struct opts opts;
129         FILE *log;
130
131         setlocale(LC_ALL, "");
132         bindtextdomain(PACKAGE, LOCALEDIR);
133         textdomain(PACKAGE);
134
135         if (opts_parse(&opts, argc, argv)) {
136                 print_usage();
137                 return EXIT_FAILURE;
138         }
139
140         if (opts.show_help == opt_yes) {
141                 print_usage();
142                 return EXIT_SUCCESS;
143         }
144
145         if (opts.show_version == opt_yes) {
146                 print_version();
147                 return EXIT_SUCCESS;
148         }
149
150         log = stderr;
151         if (strcmp(opts.log_file, "-")) {
152                 log = fopen(opts.log_file, "a");
153                 if (!log) {
154                         fprintf(stderr, "can't open log file %s, logging to "
155                                         "stderr\n", opts.log_file);
156                         log = stderr;
157                 }
158         }
159         pb_log_init(log);
160
161         if (opts.verbose == opt_yes)
162                 pb_log_set_debug(true);
163
164         pb_log("--- pb-discover ---\n");
165
166         /* we look for closed sockets when we write, so ignore SIGPIPE */
167         signal(SIGPIPE, SIG_IGN);
168
169         signal(SIGINT, sigint_handler);
170
171         waitset = waitset_create(NULL);
172
173         server = discover_server_init(waitset);
174         if (!server)
175                 return EXIT_FAILURE;
176
177         procset = process_init(server, waitset, opts.dry_run == opt_yes);
178         if (!procset)
179                 return EXIT_FAILURE;
180
181         platform_init(NULL);
182         if (opts.no_autoboot == opt_yes)
183                 config_set_autoboot(false);
184
185         if (config_get()->lang)
186                 setlocale(LC_ALL, config_get()->lang);
187
188         if (config_get()->debug)
189                 pb_log_set_debug(true);
190
191         system_info_init(server);
192
193         handler = device_handler_init(server, waitset, opts.dry_run == opt_yes);
194         if (!handler)
195                 return EXIT_FAILURE;
196
197         discover_server_set_device_source(server, handler);
198
199         for (running = 1; running;) {
200                 if (waiter_poll(waitset))
201                         break;
202         }
203
204         device_handler_destroy(handler);
205         discover_server_destroy(server);
206         platform_fini();
207         talloc_free(waitset);
208
209         pb_log("--- end ---\n");
210
211         if (log != stderr)
212                 fclose(log);
213
214         return EXIT_SUCCESS;
215 }