1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include "options.h"
typedef enum opt_type {
OT_INVALID = 0, OT_NOARG, OT_L, OT_LL, OT_STR,
OT_PATH
} opt_type;
typedef union opt_ptr {
long int l;
long long int ll;
const char *str;
char *str_dup;
} opt_ptr;
struct opt {
opt_type type;
opt_ptr value;
opt_ptr def_value;
int used;
int is_list;
const char *arg_name;
const char *short_help;
const char *help;
};
#define OPT(type, def_value, is_list, arg, short_help, help) \
{ type, {0}, {def_value}, 0, is_list, arg, short_help, help }
#define OPT_NOARG(arg, short_help, help) \
OPT(OT_NOARG, .ll = 0, 0, arg, short_help, help)
static struct opt options[OPT_MAX+1] = {
OPT(OT_PATH, .str = POTD_LOGFILE, 0, "log-file", "short help", "help"),
OPT(OT_STR, .ll = 0, 0, "log-level", "short help", "help"),
OPT_NOARG("daemon", "short help", "help"),
OPT(OT_INVALID, .ll = 0, 0, NULL, NULL, NULL)
};
static int setopt(struct opt *o, const char *optarg);
static int setopt(struct opt *o, const char *optarg)
{
char *endptr = NULL;
assert(o && o->type != OT_INVALID);
if (o->used && !o->is_list)
return 1;
if (!optarg || o->type == OT_NOARG)
goto noarg;
switch (o->type) {
case OT_L:
o->value.l = strtol(optarg, &endptr, 10);
break;
case OT_LL:
o->value.ll = strtoll(optarg, &endptr, 10);
break;
case OT_STR:
o->value.str_dup = strdup(optarg);
break;
case OT_PATH:
o->value.str_dup = realpath(optarg, NULL);
break;
case OT_NOARG:
case OT_INVALID:
return 1;
}
if (endptr && *endptr != 0)
return 1;
noarg:
o->used = 1;
return 0;
}
int options_cmdline(int argc, char **argv)
{
int rc, i, option, option_index;
struct option *o = (struct option *) calloc(OPT_MAX+1, sizeof *o);
assert(o);
for (i = 0; i < OPT_MAX; ++i) {
o[i].name = options[i].arg_name;
if (options[i].def_value.ll)
o[i].has_arg = optional_argument;
else
o[i].has_arg =
(options[i].type == OT_NOARG ? no_argument : required_argument);
}
rc = 0;
while (1) {
option_index = -1;
option = getopt_long_only(argc, argv, "", o, &option_index);
if (option_index == -1 && option != -1) {
rc = 1;
continue;
}
if (option == -1)
break;
if (!option) {
if (setopt(&options[option_index], optarg)) {
rc = 1;
goto error;
}
} else {
fprintf(stderr, "%s: unknown option '%c' [0x%X]\n",
argv[0], option, option);
}
}
error:
free(o);
return rc;
}
int getopt_used(opt_name on)
{
return options[on].used;
}
char *
getopt_str(opt_name on)
{
char *str;
assert(options[on].type == OT_STR ||
options[on].type == OT_PATH);
assert(getopt_used(on) || options[on].def_value.str);
str = options[on].value.str_dup;
if (!str)
str = options[on].def_value.str_dup;
assert(str);
return str;
}
|