︙ | | | ︙ | |
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
struct lc_varhandler_st *varhandlers = NULL;
lc_err_t lc_errno = LC_ERR_NONE;
static int lc_process_var_string(void *data, const char *value) {
char **dataval;
dataval = data;
*dataval = strdup(value);
|
>
|
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
struct lc_varhandler_st *varhandlers = NULL;
lc_err_t lc_errno = LC_ERR_NONE;
int lc_optind = 0;
static int lc_process_var_string(void *data, const char *value) {
char **dataval;
dataval = data;
*dataval = strdup(value);
|
︙ | | | ︙ | |
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
|
return(0);
}
static int lc_process_cmdline(int argc, char **argv) {
struct lc_varhandler_st *handler = NULL;
char *cmdarg = NULL, *cmdoptarg = NULL;
char *lastcomponent_handler = NULL;
int cmdargidx = 0;
int retval = 0, chkretval = 0;
int ch = 0;
for (cmdargidx = 1; cmdargidx < argc; cmdargidx++) {
cmdarg = argv[cmdargidx];
/* Make sure we have an argument here. */
if (cmdarg == NULL) {
break;
}
/* If the argument isn't an option, abort. */
if (cmdarg[0] != '-') {
continue;
}
*cmdarg++;
/* Handle long options. */
if (cmdarg[0] == '-') {
*cmdarg++;
/* Don't process arguments after the '--' option. */
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
return(0);
}
static int lc_process_cmdline(int argc, char **argv) {
struct lc_varhandler_st *handler = NULL;
char *cmdarg = NULL, *cmdoptarg = NULL;
char *lastcomponent_handler = NULL;
char **newargv = NULL;
char *usedargv = NULL;
int cmdargidx = 0;
int newargvidx = 0;
int retval = 0, chkretval = 0;
int ch = 0;
/* Allocate "argc + 1" (+1 for the NULL terminator) elements. */
newargv = malloc((argc + 1) * sizeof(*newargv));
if (newargv == NULL) {
lc_errno = LC_ERR_ENOMEM;
return(-1);
}
newargv[newargvidx++] = argv[0];
newargv[argc] = NULL;
/* Allocate space to indicate which arguments have been used. */
usedargv = malloc(argc * sizeof(*usedargv));
if (usedargv == NULL) {
lc_errno = LC_ERR_ENOMEM;
return(-1);
}
for (cmdargidx = 0; cmdargidx < argc; cmdargidx++) {
usedargv[cmdargidx] = 0;
}
for (cmdargidx = 1; cmdargidx < argc; cmdargidx++) {
cmdarg = argv[cmdargidx];
/* Make sure we have an argument here. */
if (cmdarg == NULL) {
break;
}
/* If the argument isn't an option, abort. */
if (cmdarg[0] != '-') {
continue;
}
/* Setup a pointer in the new array for the actual argument. */
newargv[newargvidx++] = cmdarg;
usedargv[cmdargidx] = 1;
/* Then shift the argument past the '-' so we can ignore it. */
*cmdarg++;
/* Handle long options. */
if (cmdarg[0] == '-') {
*cmdarg++;
/* Don't process arguments after the '--' option. */
|
︙ | | | ︙ | |
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
|
handler->type == LC_VAR_SECTIONSTART ||
handler->type == LC_VAR_SECTIONEND ||
handler->type == LC_VAR_UNKNOWN) {
continue;
}
/* Find the last part of the variable and compare it with
the option being processed. */
lastcomponent_handler = strrchr(handler->var, '.');
if (lastcomponent_handler == NULL) {
lastcomponent_handler = handler->var;
} else {
*lastcomponent_handler++;
}
/* Ignore this handler if they don't match. */
if (strcasecmp(lastcomponent_handler, cmdarg) != 0) {
continue;
}
if (handler->type == LC_VAR_NONE) {
cmdoptarg = NULL;
} else {
cmdargidx++;
if (cmdargidx >= argc) {
PRINTERR("Argument required.");
lc_errno = LC_ERR_BADFORMAT;
return(-1);
}
cmdoptarg = argv[cmdargidx];
}
chkretval = lc_handle(handler, handler->var, NULL, cmdoptarg, LC_FLAGS_CMDLINE);
if (chkretval < 0) {
retval = -1;
}
|
|
>
|
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
|
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
|
handler->type == LC_VAR_SECTIONSTART ||
handler->type == LC_VAR_SECTIONEND ||
handler->type == LC_VAR_UNKNOWN) {
continue;
}
/* Find the last part of the variable and compare it with
the option being processed, if a wildcard is given. */
if (handler->var[0] == '*' && handler->var[1] == '.') {
lastcomponent_handler = strrchr(handler->var, '.');
if (lastcomponent_handler == NULL) {
lastcomponent_handler = handler->var;
} else {
*lastcomponent_handler++;
}
} else {
/* Disallow use of the fully qualified name
since there was no sectionstart portion
we cannot allow it to handle children of it. */
if (strchr(cmdarg, '.') != NULL) {
continue;
}
lastcomponent_handler = handler->var;
}
/* Ignore this handler if they don't match. */
if (strcasecmp(lastcomponent_handler, cmdarg) != 0) {
continue;
}
if (handler->type == LC_VAR_NONE) {
cmdoptarg = NULL;
} else {
cmdargidx++;
if (cmdargidx >= argc) {
PRINTERR("Argument required.");
lc_errno = LC_ERR_BADFORMAT;
return(-1);
}
cmdoptarg = argv[cmdargidx];
newargv[newargvidx++] = cmdoptarg;
usedargv[cmdargidx] = 1;
}
chkretval = lc_handle(handler, handler->var, NULL, cmdoptarg, LC_FLAGS_CMDLINE);
if (chkretval < 0) {
retval = -1;
}
|
︙ | | | ︙ | |
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
|
cmdargidx++;
if (cmdargidx >= argc) {
PRINTERR("Argument required.");
lc_errno = LC_ERR_BADFORMAT;
return(-1);
}
cmdoptarg = argv[cmdargidx];
}
chkretval = lc_handle(handler, handler->var, NULL, cmdoptarg, LC_FLAGS_CMDLINE);
if (chkretval < 0) {
retval = -1;
}
break;
}
if (handler == NULL) {
PRINTERR("Unknown option: -%c", ch);
lc_errno = LC_ERR_INVCMD;
return(-1);
}
}
}
}
return(retval);
}
int lc_process_var(const char *var, const char *varargs, const char *value, lc_flags_t flags) {
struct lc_varhandler_st *handler = NULL;
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
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
|
cmdargidx++;
if (cmdargidx >= argc) {
PRINTERR("Argument required.");
lc_errno = LC_ERR_BADFORMAT;
return(-1);
}
cmdoptarg = argv[cmdargidx];
newargv[newargvidx++] = cmdoptarg;
usedargv[cmdargidx] = 1;
}
chkretval = lc_handle(handler, handler->var, NULL, cmdoptarg, LC_FLAGS_CMDLINE);
if (chkretval < 0) {
retval = -1;
}
break;
}
if (handler == NULL) {
PRINTERR("Unknown option: -%c", ch);
lc_errno = LC_ERR_INVCMD;
return(-1);
}
}
}
}
if (retval >= 0) {
lc_optind = newargvidx;
for (cmdargidx = 1; cmdargidx < argc; cmdargidx++) {
if (usedargv[cmdargidx] != 0) {
continue;
}
cmdarg = argv[cmdargidx];
newargv[newargvidx++] = cmdarg;
}
for (cmdargidx = 1; cmdargidx < argc; cmdargidx++) {
argv[cmdargidx] = newargv[cmdargidx];
}
}
return(retval);
}
int lc_process_var(const char *var, const char *varargs, const char *value, lc_flags_t flags) {
struct lc_varhandler_st *handler = NULL;
|
︙ | | | ︙ | |