-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathoptions.c
More file actions
357 lines (307 loc) · 8.27 KB
/
options.c
File metadata and controls
357 lines (307 loc) · 8.27 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project
#include "options.h"
#include "config.h"
#include "Compiler.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#define MAX_LONGOPT_LENGTH 32
#define ERROR_UNKNOWN_OPTION 0x01
#define ERROR_BAD_ARGUMENT 0x02
#define ERROR_GOT_ARGUMENT 0x03
#define ERROR_MISSING_ARGUMENT 0x04
enum ShortOption {
OPTION_NONE,
OPTION_WITH_PRIO,
};
struct OptionDef {
int shortopt;
const char *longopt;
const char *argument;
const char *description;
};
struct Options options = {
.verbosity = V_DEFAULT,
.password = NULL,
.port_str = NULL,
.format = NULL,
.range = { .start = 0, .end = UINT_MAX },
};
static const struct OptionDef option_table[] = {
{ 'v', "verbose", NULL, "Give verbose output" },
{ 'q', "quiet", NULL, "Suppress status message" },
{ 'q', "no-status", NULL, "synonym for --quiet" },
{ 'h', "host", "<host>", "Connect to server on <host>" },
{ 'P', "password", "<password>", "Connect to server using password <password>" },
{ 'p', "port", "<port>", "Connect to server port <port>" },
{ 'f', "format", "<format>", "Print status with format <format>" },
{ 'w', "wait", NULL, "Wait for operation to finish (e.g. database update)" },
{ 'r', "range", "[<start>]:[<end>]", "Operate on a range (e.g. when loading a playlist)" },
{ 'a', "partition", "<name>", "Operate on partition <name> instead" },
{ 'F', "force", NULL, "Force some operations (like saving playlist)" },
{ OPTION_WITH_PRIO, "with-prio", NULL, "Show only songs that have a non-zero priority" },
};
static const unsigned option_table_size = sizeof(option_table) / sizeof(option_table[0]);
gcc_noreturn
static void
option_error(int error, const char *option, const char *arg)
{
switch (error) {
case ERROR_UNKNOWN_OPTION:
fprintf(stderr, PACKAGE ": invalid option %s\n", option);
break;
case ERROR_BAD_ARGUMENT:
fprintf(stderr, PACKAGE ": bad argument: %s\n", option);
break;
case ERROR_GOT_ARGUMENT:
fprintf(stderr, PACKAGE ": invalid option %s=%s\n", option, arg);
break;
case ERROR_MISSING_ARGUMENT:
fprintf(stderr, PACKAGE ": missing value for %s option\n", option);
break;
default:
fprintf(stderr, PACKAGE ": internal error %d\n", error);
break;
}
exit(EXIT_FAILURE);
}
gcc_pure
static const struct OptionDef *
lookup_long_option(const char *l, size_t len)
{
for (unsigned i = 0; i < option_table_size; ++i) {
if (strncmp(l, option_table[i].longopt, len) == 0)
return &option_table[i];
}
return NULL;
}
gcc_const
static const struct OptionDef *
lookup_short_option(int s)
{
for (unsigned i = 0; i < option_table_size; ++i) {
if (s == option_table[i].shortopt)
return &option_table[i];
}
return NULL;
}
static void
ParseRange(struct Range *r, const char *s)
{
char *endptr;
r->start = strtoul(s, &endptr, 10);
if (endptr == s)
r->start = 0;
s = endptr;
if (*s == '\0') {
r->end = r->start + 1;
return;
}
if (*s != ':') {
fprintf(stderr, "Failed to parse range '%s'\n", s);
exit(EXIT_FAILURE);
}
++s;
r->end = strtoul(s, &endptr, 10);
if (*endptr != 0) {
fprintf(stderr, "Failed to parse range end '%s'\n", s);
exit(EXIT_FAILURE);
}
if (endptr == s)
r->end = UINT_MAX;
}
static void
handle_option(int c, const char *arg)
{
switch (c) {
case 'v':
options.verbosity = 2;
break;
case 'q':
options.verbosity = 0;
break;
case 'h':
options.host = arg;
break;
case 'P':
options.password = arg;
break;
case 'p':
options.port_str = arg;
break;
case 'a':
options.partition = arg;
break;
case 'f':
options.format = arg;
options.custom_format = true;
break;
case 'w':
options.wait = true;
break;
case 'F':
options.force = true;
break;
case 'r':
ParseRange(&options.range, arg);
break;
case OPTION_WITH_PRIO:
options.with_prio = true;
break;
default: // Should never be reached, due to lookup_*_option functions
fprintf(stderr, "Unknown option %c = %s\n", c, arg);
exit(EXIT_FAILURE);
break;
}
}
void
print_option_help(void)
{
for (unsigned i = 0; i < option_table_size; i++) {
int remaining = 28;
if (option_table[i].shortopt > 0x20) {
printf(" -%c, ", option_table[i].shortopt);
remaining -= 4;
} else
printf(" ");
if (option_table[i].argument)
printf("--%s=%-*s",
option_table[i].longopt,
remaining - (int) strlen(option_table[i].longopt),
option_table[i].argument);
else
printf("--%-*s ", remaining, option_table[i].longopt);
printf("%s\n", option_table[i].description);
}
}
void
parse_options(int * argc_p, char ** argv)
{
int i;
const struct OptionDef *opt = NULL;
char * tmp;
int cmdind = 0;
int optind = 0;
for (i = 1; i < *argc_p; i++) {
const char *arg = argv[i];
size_t len = strlen(arg);
if (arg[0] == '-' && (arg[1] < '0' || arg[1] > '9')) {
if (arg[1] == '-') {
/* arg is a long option */
size_t name_len = len - 2;
/* if arg is "--", there can be no more options */
if (len == 2) {
optind = i + 1;
if (cmdind == 0) {
cmdind = optind;
}
break;
}
/* make sure we got an argument for the previous option */
if( opt && opt->argument)
option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
/* retrieve a option argument */
char *value;
if ((value = strchr(arg + 2, '=')) != NULL) {
name_len = value - arg - 2;
value++;
}
/* check if the option exists */
if ((opt=lookup_long_option(arg+2, name_len)) == NULL) {
option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
}
/* abort if we got an argument to the option and don't want one */
if( value && opt->argument == NULL)
option_error(ERROR_GOT_ARGUMENT, arg, value);
/* execute option callback */
if (value || opt->argument == NULL) {
handle_option(opt->shortopt, value);
opt = NULL;
}
} else {
/* arg is a short option (or several) */
size_t j;
if (len == 1)
option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
for (j=1; j<len; j++) {
/* make sure we got an argument for the previous option */
if (opt && opt->argument)
option_error(ERROR_MISSING_ARGUMENT,
opt->longopt, opt->argument);
/* check if the option exists */
if ((opt = lookup_short_option(arg[j])) == NULL)
option_error(ERROR_UNKNOWN_OPTION, arg, NULL);
/* if no option argument is needed execute callback */
if (opt->argument == NULL) {
handle_option(opt->shortopt, NULL);
opt = NULL;
}
}
}
} else {
/* No '-' */
if (opt && opt->argument) {
/* arg is an option argument */
handle_option(opt->shortopt, arg);
opt = NULL;
} else {
/* arg may the command; note it and read for more options */
if (cmdind == 0)
cmdind = i;
/* otherwise, it is the first command argument and we are done */
else {
optind = i;
break;
}
}
}
}
if (optind == 0)
optind = i;
if (opt && opt->argument)
option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument);
/* Parse the password from the host */
if (options.host != NULL &&
(tmp = strchr(options.host, '@')) != NULL &&
/* if the host begins with a '@' then it's not an empty
password but an abstract socket */
tmp > options.host) {
size_t password_length = tmp - options.host;
char *password = malloc(password_length + 1);
memcpy(password, options.host, password_length);
password[password_length] = 0;
options.password = password;
options.host = tmp + 1;
}
/* Convert port to an integer */
if (options.port_str) {
options.port = strtol(options.port_str, &tmp, 10);
if (options.port < 0 || *tmp != '\0') {
fprintf(stderr, "Port \"%s\" is not a positive integer\n", options.port_str);
exit(EXIT_FAILURE);
}
}
if (options.format == NULL) {
if ((options.format = getenv("MPC_FORMAT")) == NULL)
options.format = F_DEFAULT;
else
options.custom_format = true;
}
/* Fix argv for command processing, which wants
argv[1] to be the command, and so on. */
if (cmdind != 0)
argv[1] = argv[cmdind];
if (optind > 1) {
if ( optind == cmdind || cmdind == 0 ) {
for (i = optind + 1; i < *argc_p; i++)
argv[i-optind+2] = argv[i];
*argc_p -= optind - 1;
} else {
for (i = optind; i < *argc_p; i++)
argv[i-optind+2] = argv[i];
*argc_p -= optind - 2;
}
}
}