Line data Source code
1 : #include <minipot_argp.h>
2 : #include <minipot_config.h>
3 : #include <stdlib.h>
4 : #include <stdint.h>
5 :
6 : static const struct argp_option options[] = {
7 : {"config-file", 'c', "FILE", 0, "Configuration file", 0},
8 : {"user", 'u', "USER", 0, "User to drop priviledges", 0},
9 : {"topic", 't', "TOPIC", 0, "Topic for communication with Sentinel Proxy", 0},
10 : {"endpoint", 'e', "ENDPOINT", 0, "ZMQ endpoint for communication with Sentinel Proxy", 0},
11 : {"port", 'p', "PORT", 0, "Port to listen on", 0},
12 : {NULL},
13 : };
14 :
15 : static error_t parse_opt(int key, char *arg, struct argp_state *state);
16 :
17 : const struct argp minipot_argp = {
18 : .options = options,
19 : .parser = parse_opt,
20 : };
21 :
22 0 : static uint16_t parse_port(const char *str, struct argp_state *state) {
23 0 : char *end_ptr;
24 0 : errno = 0;
25 0 : long result = strtol(str, &end_ptr, 10);
26 0 : if (errno || // conversion error
27 0 : (result < 0 || result > 65535) || // port out of range
28 0 : end_ptr == str) // no digits
29 0 : argp_error(state, "Invalid port value! Must be integer in range <0, 65535> !");
30 0 : return result;
31 : }
32 :
33 0 : static error_t parse_opt(int key, char *arg, struct argp_state *state) {
34 0 : struct minipot_config *config = state->input;
35 0 : switch (key) {
36 0 : case 'c':
37 0 : config->config_file = arg;
38 0 : break;
39 0 : case 'u':
40 0 : config->user = arg;
41 0 : break;
42 0 : case 't':
43 0 : config->topic = arg;
44 0 : break;
45 0 : case 'e':
46 0 : config->zmq_endopint = arg;
47 0 : break;
48 0 : case 'p':
49 0 : config->ports[config->ports_cnt] = parse_port(arg, state);
50 0 : config->ports_cnt++;
51 0 : if (config->ports_cnt == config->ports_size) {
52 0 : config->ports_size *= 2;
53 0 : config->ports = realloc(config->ports,
54 : sizeof(*config->ports) * config->ports_size);
55 : }
56 : break;
57 : default:
58 : return ARGP_ERR_UNKNOWN;
59 : }
60 : return 0;
61 : }
|