test-chase-manual: convert to the new option parser
--help now has help strings. --no_autofs is renamed to --no-autofs. Co-developed-by: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -1,84 +1,75 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
#include <getopt.h>
|
||||
|
||||
#include "chase.h"
|
||||
#include "fd-util.h"
|
||||
#include "format-table.h"
|
||||
#include "log.h"
|
||||
#include "main-func.h"
|
||||
#include "options.h"
|
||||
#include "strv.h"
|
||||
#include "tests.h"
|
||||
|
||||
static char *arg_root = NULL;
|
||||
static const char *arg_root = NULL;
|
||||
static int arg_flags = 0;
|
||||
static bool arg_open = false;
|
||||
|
||||
static int parse_argv(int argc, char *argv[]) {
|
||||
enum {
|
||||
ARG_ROOT = 0x1000,
|
||||
ARG_OPEN,
|
||||
};
|
||||
static int help(void) {
|
||||
_cleanup_(table_unrefp) Table *options = NULL;
|
||||
int r;
|
||||
|
||||
static const struct option options[] = {
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "root", required_argument, NULL, ARG_ROOT },
|
||||
{ "open", no_argument, NULL, ARG_OPEN },
|
||||
r = option_parser_get_help_table(&options);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
{ "prefix-root", no_argument, NULL, CHASE_PREFIX_ROOT },
|
||||
{ "nonexistent", no_argument, NULL, CHASE_NONEXISTENT },
|
||||
{ "no_autofs", no_argument, NULL, CHASE_NO_AUTOFS },
|
||||
{ "trigger-autofs", no_argument, NULL, CHASE_TRIGGER_AUTOFS },
|
||||
{ "safe", no_argument, NULL, CHASE_SAFE },
|
||||
{ "trail-slash", no_argument, NULL, CHASE_TRAIL_SLASH },
|
||||
{ "step", no_argument, NULL, CHASE_STEP },
|
||||
{ "nofollow", no_argument, NULL, CHASE_NOFOLLOW },
|
||||
{ "warn", no_argument, NULL, CHASE_WARN },
|
||||
{}
|
||||
};
|
||||
printf("%s [OPTIONS...] path...\n"
|
||||
"\nExercise chase() function on specified paths.\n\n",
|
||||
program_invocation_short_name);
|
||||
|
||||
int c;
|
||||
r = table_print_or_warn(options);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
assert_se(argc >= 0);
|
||||
assert_se(argv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while ((c = getopt_long(argc, argv, "", options, NULL)) >= 0)
|
||||
static int parse_argv(int argc, char *argv[], char ***ret_args) {
|
||||
assert(argc >= 0);
|
||||
assert(argv);
|
||||
assert(ret_args);
|
||||
|
||||
OptionParser state = { argc, argv };
|
||||
const Option *opt;
|
||||
const char *arg;
|
||||
|
||||
FOREACH_OPTION_FULL(&state, c, &opt, &arg, /* on_error= */ return c)
|
||||
switch (c) {
|
||||
|
||||
case 'h':
|
||||
printf("Syntax:\n"
|
||||
" %s [OPTION...] path...\n"
|
||||
"Options:\n"
|
||||
, argv[0]);
|
||||
FOREACH_ARRAY(option, options, ELEMENTSOF(options) - 1)
|
||||
printf(" --%s\n", option->name);
|
||||
return 0;
|
||||
OPTION_COMMON_HELP:
|
||||
return help();
|
||||
|
||||
case ARG_ROOT:
|
||||
arg_root = optarg;
|
||||
OPTION_LONG("root", "PATH", "Operate below specified root directory"):
|
||||
arg_root = arg;
|
||||
break;
|
||||
|
||||
case ARG_OPEN:
|
||||
OPTION_LONG("open", NULL, "Open the resolved path"):
|
||||
arg_open = true;
|
||||
break;
|
||||
|
||||
case CHASE_PREFIX_ROOT:
|
||||
case CHASE_NONEXISTENT:
|
||||
case CHASE_NO_AUTOFS:
|
||||
case CHASE_TRIGGER_AUTOFS:
|
||||
case CHASE_SAFE:
|
||||
case CHASE_TRAIL_SLASH:
|
||||
case CHASE_STEP:
|
||||
case CHASE_NOFOLLOW:
|
||||
case CHASE_WARN:
|
||||
arg_flags |= c;
|
||||
OPTION_LONG_DATA("prefix-root", NULL, CHASE_PREFIX_ROOT, "Prefix path with --root"): {}
|
||||
OPTION_LONG_DATA("nonexistent", NULL, CHASE_NONEXISTENT, "Allow path to not exist"): {}
|
||||
OPTION_LONG_DATA("no-autofs", NULL, CHASE_NO_AUTOFS, "Return -EREMOTE if autofs mount point found"): {}
|
||||
OPTION_LONG_DATA("trigger-autofs", NULL, CHASE_TRIGGER_AUTOFS, "Trigger autofs mounts"): {}
|
||||
OPTION_LONG_DATA("safe", NULL, CHASE_SAFE, "Refuse privilege boundary crossings"): {}
|
||||
OPTION_LONG_DATA("trail-slash", NULL, CHASE_TRAIL_SLASH, "Preserve trailing slash"): {}
|
||||
OPTION_LONG_DATA("step", NULL, CHASE_STEP, "Execute a single normalization step"): {}
|
||||
OPTION_LONG_DATA("nofollow", NULL, CHASE_NOFOLLOW, "Do not follow the path's right-most component"): {}
|
||||
OPTION_LONG_DATA("warn", NULL, CHASE_WARN, "Emit a warning on error"):
|
||||
arg_flags |= opt->data;
|
||||
break;
|
||||
|
||||
case '?':
|
||||
return -EINVAL;
|
||||
|
||||
default:
|
||||
assert_not_reached();
|
||||
}
|
||||
|
||||
if (optind == argc)
|
||||
*ret_args = option_parser_get_args(&state);
|
||||
if (strv_isempty(*ret_args))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "At least one argument is required.");
|
||||
|
||||
return 1;
|
||||
@@ -89,18 +80,19 @@ static int run(int argc, char **argv) {
|
||||
|
||||
test_setup_logging(LOG_DEBUG);
|
||||
|
||||
r = parse_argv(argc, argv);
|
||||
char **args = NULL;
|
||||
r = parse_argv(argc, argv, &args);
|
||||
if (r <= 0)
|
||||
return r;
|
||||
|
||||
for (int i = optind; i < argc; i++) {
|
||||
STRV_FOREACH(a, args) {
|
||||
_cleanup_free_ char *p = NULL;
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
|
||||
printf("%s ", argv[i]);
|
||||
printf("%s ", *a);
|
||||
fflush(stdout);
|
||||
|
||||
r = chase(argv[i], arg_root, arg_flags, &p, arg_open ? &fd : NULL);
|
||||
r = chase(*a, arg_root, arg_flags, &p, arg_open ? &fd : NULL);
|
||||
if (r < 0)
|
||||
log_error_errno(r, "failed: %m");
|
||||
else {
|
||||
|
||||
Reference in New Issue
Block a user