Add usage comment to parse_user_event
[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
11 #include <waiter/waiter.h>
12 #include <log/log.h>
13
14 #include "udev.h"
15 #include "user-event.h"
16 #include "discover-server.h"
17 #include "device-handler.h"
18
19 static void print_version(void)
20 {
21         printf("pb-discover (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
22 }
23
24 static void print_usage(void)
25 {
26         print_version();
27         printf(
28 "Usage: pb-discover [-h, --help] [-l, --log log-file] [-V, --version]\n");
29 }
30
31 /**
32  * enum opt_value - Tri-state options variables.
33  */
34
35 enum opt_value {opt_undef = 0, opt_yes, opt_no};
36
37 /**
38  * struct opts - Values from command line options.
39  */
40
41 struct opts {
42         enum opt_value show_help;
43         const char *log_file;
44         enum opt_value show_version;
45 };
46
47 /**
48  * opts_parse - Parse the command line options.
49  */
50
51 static int opts_parse(struct opts *opts, int argc, char *argv[])
52 {
53         static const struct option long_options[] = {
54                 {"help",           no_argument,       NULL, 'h'},
55                 {"log",            required_argument, NULL, 'l'},
56                 {"version",        no_argument,       NULL, 'V'},
57                 { NULL, 0, NULL, 0},
58         };
59         static const char short_options[] = "hl:V";
60         static const struct opts default_values = {
61                 .log_file = "pb-discover.log",
62         };
63
64         *opts = default_values;
65
66         while (1) {
67                 int c = getopt_long(argc, argv, short_options, long_options,
68                         NULL);
69
70                 if (c == EOF)
71                         break;
72
73                 switch (c) {
74                 case 'h':
75                         opts->show_help = opt_yes;
76                         break;
77                 case 'l':
78                         opts->log_file = optarg;
79                         break;
80                 case 'V':
81                         opts->show_version = opt_yes;
82                         break;
83                 default:
84                         opts->show_help = opt_yes;
85                         return -1;
86                 }
87         }
88
89         return optind != argc;
90 }
91
92 static int running;
93
94 static void sigint_handler(int __attribute__((unused)) signum)
95 {
96         running = 0;
97 }
98
99 int main(int argc, char *argv[])
100 {
101         struct device_handler *handler;
102         struct discover_server *server;
103         struct opts opts;
104         struct udev *udev;
105         struct user_event *uev;
106         FILE *log;
107
108         if (opts_parse(&opts, argc, argv)) {
109                 print_usage();
110                 return EXIT_FAILURE;
111         }
112
113         if (opts.show_help == opt_yes) {
114                 print_usage();
115                 return EXIT_SUCCESS;
116         }
117
118         if (opts.show_version == opt_yes) {
119                 print_version();
120                 return EXIT_SUCCESS;
121         }
122
123         log = fopen(opts.log_file, "a");
124         assert(log);
125         pb_log_set_stream(log);
126
127 #if defined(DEBUG)
128         pb_log_always_flush(1);
129 #endif
130         pb_log("--- pb-discover ---\n");
131
132         /* we look for closed sockets when we write, so ignore SIGPIPE */
133         signal(SIGPIPE, SIG_IGN);
134
135         signal(SIGINT, sigint_handler);
136
137         server = discover_server_init();
138         if (!server)
139                 return EXIT_FAILURE;
140
141         handler = device_handler_init(server);
142         if (!handler)
143                 return EXIT_FAILURE;
144
145         discover_server_set_device_source(server, handler);
146
147         udev = udev_init(handler);
148         if (!udev)
149                 return EXIT_FAILURE;
150
151         uev = user_event_init(handler);
152         if (!uev)
153                 return EXIT_FAILURE;
154
155         udev_trigger(udev);
156         user_event_trigger(uev);
157
158         for (running = 1; running;) {
159                 if (waiter_poll())
160                         break;
161         }
162
163         device_handler_destroy(handler);
164
165         pb_log("--- end ---\n");
166
167         return EXIT_SUCCESS;
168 }