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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <stdarg.h>
#include "auth/Auth.h"
#include "common/ceph_argparse.h"
#include "common/config.h"
#include "common/version.h"
#include "include/str_list.h"
/*
* Ceph argument parsing library
*
* We probably should eventually replace this with something standard like popt.
* Until we do that, though, this file is the place for argv parsing
* stuff to live.
*/
#undef dout
#undef pdout
#undef derr
#undef generic_dout
#undef dendl
struct strict_str_convert {
const char *str;
std::string *err;
strict_str_convert(const char *str, std::string *err)
: str(str), err(err) {}
inline operator float() const
{
return strict_strtof(str, err);
}
inline operator int() const
{
return strict_strtol(str, 10, err);
}
inline operator long long() const
{
return strict_strtoll(str, 10, err);
}
};
void string_to_vec(std::vector<std::string>& args, std::string argstr)
{
std::istringstream iss(argstr);
while(iss) {
std::string sub;
iss >> sub;
if (sub == "") break;
args.push_back(sub);
}
}
std::pair<std::vector<const char*>, std::vector<const char*>>
split_dashdash(const std::vector<const char*>& args) {
auto dashdash = std::find_if(args.begin(), args.end(),
[](const char* arg) {
return strcmp(arg, "--") == 0;
});
std::vector<const char*> options{args.begin(), dashdash};
if (dashdash != args.end()) {
++dashdash;
}
std::vector<const char*> arguments{dashdash, args.end()};
return {std::move(options), std::move(arguments)};
}
static std::mutex g_str_vec_lock;
static std::vector<std::string> g_str_vec;
void clear_g_str_vec()
{
g_str_vec_lock.lock();
g_str_vec.clear();
g_str_vec_lock.unlock();
}
void env_to_vec(std::vector<const char*>& args, const char *name)
{
if (!name)
name = "CEPH_ARGS";
/*
* We can only populate str_vec once. Other threads could hold pointers into
* it, so clearing it out and replacing it is not currently safe.
*/
g_str_vec_lock.lock();
if (g_str_vec.empty()) {
char *p = getenv(name);
if (!p) {
g_str_vec_lock.unlock();
return;
}
get_str_vec(p, " ", g_str_vec);
}
std::vector<const char*> env;
for (const auto& s : g_str_vec) {
env.push_back(s.c_str());
}
g_str_vec_lock.unlock();
auto [env_options, env_arguments] = split_dashdash(env);
auto [options, arguments] = split_dashdash(args);
args.clear();
args.insert(args.end(), env_options.begin(), env_options.end());
args.insert(args.end(), options.begin(), options.end());
if (arguments.empty() && env_arguments.empty()) {
return;
}
args.push_back("--");
args.insert(args.end(), env_arguments.begin(), env_arguments.end());
args.insert(args.end(), arguments.begin(), arguments.end());
}
std::vector<const char*> argv_to_vec(int argc, const char* const * argv)
{
assert(argc > 0);
return {argv + 1, argv + argc};
}
void vec_to_argv(const char *argv0, std::vector<const char*>& args,
int *argc, const char ***argv)
{
*argv = (const char**)malloc(sizeof(char*) * (args.size() + 1));
if (!*argv)
throw std::bad_alloc();
*argc = 1;
(*argv)[0] = argv0;
for (unsigned i=0; i<args.size(); i++)
(*argv)[(*argc)++] = args[i];
}
void ceph_arg_value_type(const char * nextargstr, bool *bool_option, bool *bool_numeric)
{
bool is_numeric = true;
bool is_float = false;
bool is_option;
if (nextargstr == NULL) {
return;
}
if (strlen(nextargstr) < 2) {
is_option = false;
} else {
is_option = (nextargstr[0] == '-') && (nextargstr[1] == '-');
}
for (unsigned int i = 0; i < strlen(nextargstr); i++) {
if (!(nextargstr[i] >= '0' && nextargstr[i] <= '9')) {
// May be negative numeral value
if ((i == 0) && (strlen(nextargstr) >= 2)) {
if (nextargstr[0] == '-')
continue;
}
if ( (nextargstr[i] == '.') && (is_float == false) ) {
is_float = true;
continue;
}
is_numeric = false;
break;
}
}
// -<option>
if (nextargstr[0] == '-' && is_numeric == false) {
is_option = true;
}
*bool_option = is_option;
*bool_numeric = is_numeric;
return;
}
bool parse_ip_port_vec(const char *s, std::vector<entity_addrvec_t>& vec, int type)
{
// first split by [ ;], which are not valid for an addrvec
std::list<std::string> items;
get_str_list(s, " ;", items);
for (auto& i : items) {
const char *s = i.c_str();
while (*s) {
const char *end;
// try parsing as an addr
entity_addr_t a;
if (a.parse(s, &end, type)) {
vec.push_back(entity_addrvec_t(a));
s = end;
if (*s == ',') {
++s;
}
continue;
}
// ok, try parsing as an addrvec
entity_addrvec_t av;
if (!av.parse(s, &end)) {
return false;
}
vec.push_back(av);
s = end;
if (*s == ',') {
++s;
}
}
}
return true;
}
// The defaults for CephInitParameters
CephInitParameters::CephInitParameters(uint32_t module_type_)
: module_type(module_type_)
{
name.set(module_type, "admin");
}
static void dashes_to_underscores(const char *input, char *output)
{
char c = 0;
char *o = output;
const char *i = input;
// first two characters are copied as-is
*o = *i++;
if (*o++ == '\0')
return;
*o = *i++;
if (*o++ == '\0')
return;
for (; ((c = *i)); ++i) {
if (c == '=') {
strcpy(o, i);
return;
}
if (c == '-')
*o++ = '_';
else
*o++ = c;
}
*o++ = '\0';
}
/** Once we see a standalone double dash, '--', we should remove it and stop
* looking for any other options and flags. */
bool ceph_argparse_double_dash(std::vector<const char*> &args,
std::vector<const char*>::iterator &i)
{
if (strcmp(*i, "--") == 0) {
i = args.erase(i);
return true;
}
return false;
}
bool ceph_argparse_flag(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, ...)
{
const char *first = *i;
char tmp[strlen(first)+1];
dashes_to_underscores(first, tmp);
first = tmp;
va_list ap;
va_start(ap, i);
while (1) {
const char *a = va_arg(ap, char*);
if (a == NULL) {
va_end(ap);
return false;
}
char a2[strlen(a)+1];
dashes_to_underscores(a, a2);
if (strcmp(a2, first) == 0) {
i = args.erase(i);
va_end(ap);
return true;
}
}
}
static bool check_bool_str(const char *val, int *ret)
{
if ((strcmp(val, "true") == 0) || (strcmp(val, "1") == 0)) {
*ret = 1;
return true;
} else if ((strcmp(val, "false") == 0) || (strcmp(val, "0") == 0)) {
*ret = 0;
return true;
}
return false;
}
static bool va_ceph_argparse_binary_flag(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, int *ret,
std::ostream *oss, va_list ap)
{
const char *first = *i;
char tmp[strlen(first)+1];
dashes_to_underscores(first, tmp);
first = tmp;
// does this argument match any of the possibilities?
while (1) {
const char *a = va_arg(ap, char*);
if (a == NULL)
return false;
int strlen_a = strlen(a);
char a2[strlen_a+1];
dashes_to_underscores(a, a2);
if (strncmp(a2, first, strlen(a2)) == 0) {
if (first[strlen_a] == '=') {
i = args.erase(i);
const char *val = first + strlen_a + 1;
if (check_bool_str(val, ret)) {
return true;
}
if (oss) {
(*oss) << "Parse error parsing binary flag " << a
<< ". Expected true or false, but got '" << val << "'\n";
}
*ret = -EINVAL;
return true;
}
else if (first[strlen_a] == '\0') {
auto next = i+1;
if (next != args.end() &&
*next &&
(*next)[0] != '-') {
if (check_bool_str(*next, ret)) {
i = args.erase(i);
i = args.erase(i);
return true;
}
}
i = args.erase(i);
*ret = 1;
return true;
}
}
}
}
bool ceph_argparse_binary_flag(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, int *ret,
std::ostream *oss, ...)
{
bool r;
va_list ap;
va_start(ap, oss);
r = va_ceph_argparse_binary_flag(args, i, ret, oss, ap);
va_end(ap);
return r;
}
static int va_ceph_argparse_witharg(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, std::string *ret,
std::ostream &oss, va_list ap)
{
const char *first = *i;
char tmp[strlen(first)+1];
dashes_to_underscores(first, tmp);
first = tmp;
// does this argument match any of the possibilities?
while (1) {
const char *a = va_arg(ap, char*);
if (a == NULL)
return 0;
int strlen_a = strlen(a);
char a2[strlen_a+1];
dashes_to_underscores(a, a2);
if (strncmp(a2, first, strlen(a2)) == 0) {
if (first[strlen_a] == '=') {
*ret = first + strlen_a + 1;
i = args.erase(i);
return 1;
}
else if (first[strlen_a] == '\0') {
// find second part (or not)
if (i+1 == args.end()) {
oss << "Option " << *i << " requires an argument." << std::endl;
i = args.erase(i);
return -EINVAL;
}
i = args.erase(i);
*ret = *i;
i = args.erase(i);
return 1;
}
}
}
}
template<class T>
bool ceph_argparse_witharg(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, T *ret,
std::ostream &oss, ...)
{
int r;
va_list ap;
bool is_option = false;
bool is_numeric = true;
std::string str;
va_start(ap, oss);
r = va_ceph_argparse_witharg(args, i, &str, oss, ap);
va_end(ap);
if (r == 0) {
return false;
} else if (r < 0) {
return true;
}
ceph_arg_value_type(str.c_str(), &is_option, &is_numeric);
if ((is_option == true) || (is_numeric == false)) {
*ret = EXIT_FAILURE;
if (is_option == true) {
oss << "Missing option value";
} else {
oss << "The option value '" << str << "' is invalid";
}
return true;
}
std::string err;
T myret = strict_str_convert(str.c_str(), &err);
*ret = myret;
if (!err.empty()) {
oss << err;
}
return true;
}
template bool ceph_argparse_witharg<int>(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, int *ret,
std::ostream &oss, ...);
template bool ceph_argparse_witharg<long long>(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, long long *ret,
std::ostream &oss, ...);
template bool ceph_argparse_witharg<float>(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, float *ret,
std::ostream &oss, ...);
bool ceph_argparse_witharg(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, std::string *ret,
std::ostream &oss, ...)
{
int r;
va_list ap;
va_start(ap, oss);
r = va_ceph_argparse_witharg(args, i, ret, oss, ap);
va_end(ap);
return r != 0;
}
bool ceph_argparse_witharg(std::vector<const char*> &args,
std::vector<const char*>::iterator &i, std::string *ret, ...)
{
int r;
va_list ap;
va_start(ap, ret);
r = va_ceph_argparse_witharg(args, i, ret, std::cerr, ap);
va_end(ap);
if (r < 0)
_exit(1);
return r != 0;
}
CephInitParameters ceph_argparse_early_args
(std::vector<const char*>& args, uint32_t module_type,
std::string *cluster, std::string *conf_file_list)
{
CephInitParameters iparams(module_type);
std::string val;
auto orig_args = args;
for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
if (strcmp(*i, "--") == 0) {
/* Normally we would use ceph_argparse_double_dash. However, in this
* function we *don't* want to remove the double dash, because later
* argument parses will still need to see it. */
break;
}
else if (ceph_argparse_flag(args, i, "--version", "-v", (char*)NULL)) {
std::cout << pretty_version_to_str() << std::endl;
_exit(0);
}
else if (ceph_argparse_witharg(args, i, &val, "--conf", "-c", (char*)NULL)) {
*conf_file_list = val;
}
else if (ceph_argparse_flag(args, i, "--no-config-file", (char*)NULL)) {
iparams.no_config_file = true;
}
else if (ceph_argparse_witharg(args, i, &val, "--cluster", (char*)NULL)) {
*cluster = val;
}
else if ((module_type != CEPH_ENTITY_TYPE_CLIENT) &&
(ceph_argparse_witharg(args, i, &val, "-i", (char*)NULL))) {
iparams.name.set_id(val);
}
else if (ceph_argparse_witharg(args, i, &val, "--id", "--user", (char*)NULL)) {
iparams.name.set_id(val);
}
else if (ceph_argparse_witharg(args, i, &val, "--name", "-n", (char*)NULL)) {
if (!iparams.name.from_str(val)) {
std::cerr << "error parsing '" << val << "': expected string of the form TYPE.ID, "
<< "valid types are: " << EntityName::get_valid_types_as_str()
<< std::endl;
_exit(1);
}
}
else if (ceph_argparse_flag(args, i, "--show_args", (char*)NULL)) {
std::cout << "args: ";
for (std::vector<const char *>::iterator ci = orig_args.begin(); ci != orig_args.end(); ++ci) {
if (ci != orig_args.begin())
std::cout << " ";
std::cout << *ci;
}
std::cout << std::endl;
}
else {
// ignore
++i;
}
}
return iparams;
}
static void generic_usage(bool is_server)
{
std::cout <<
" --conf/-c FILE read configuration from the given configuration file" << std::endl <<
(is_server ?
" --id/-i ID set ID portion of my name" :
" --id ID set ID portion of my name") << std::endl <<
" --name/-n TYPE.ID set name" << std::endl <<
" --cluster NAME set cluster name (default: ceph)" << std::endl <<
" --setuser USER set uid to user or uid (and gid to user's gid)" << std::endl <<
" --setgroup GROUP set gid to group or gid" << std::endl <<
" --version show version and quit" << std::endl
<< std::endl;
if (is_server) {
std::cout <<
" -d run in foreground, log to stderr" << std::endl <<
" -f run in foreground, log to usual location" << std::endl <<
std::endl <<
" --debug_ms N set message debug level (e.g. 1)" << std::endl;
}
std::cout.flush();
}
bool ceph_argparse_need_usage(const std::vector<const char*>& args)
{
if (args.empty()) {
return true;
}
for (auto a : args) {
if (strcmp(a, "-h") == 0 ||
strcmp(a, "--help") == 0) {
return true;
}
}
return false;
}
void generic_server_usage()
{
generic_usage(true);
}
void generic_client_usage()
{
generic_usage(false);
}
|