From 063910c54bc1e944de7483d75eb665cdbdf4f0e2 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Sun, 28 Apr 2024 17:59:42 +0200 Subject: [PATCH] command: refactor common code to command_enum.h Move shared code to command_enum.h and avoid duplicating code. --- tools/command.c | 2 - tools/command_enums.h | 85 +++++++++++++++++++++++++++++++++++++++++++ tools/man-generator.c | 59 +----------------------------- tools/tools.h | 68 +--------------------------------- 4 files changed, 87 insertions(+), 127 deletions(-) create mode 100644 tools/command_enums.h diff --git a/tools/command.c b/tools/command.c index 0a90b5ebb..e484906fb 100644 --- a/tools/command.c +++ b/tools/command.c @@ -28,8 +28,6 @@ #include "tools.h" #endif /* MAN_PAGE_GENERATOR */ -#include "command.h" /* defines struct command */ -#include "command-count.h" /* defines COMMAND_COUNT */ /* see cmd_names[] below, one for each unique "ID" in command-lines.in */ diff --git a/tools/command_enums.h b/tools/command_enums.h new file mode 100644 index 000000000..2331b2772 --- /dev/null +++ b/tools/command_enums.h @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2024 Red Hat, Inc. All rights reserved. + * + * This file is part of LVM2. + * + * This copyrighted material is made available to anyone wishing to use, + * modify, copy, or redistribute it subject to the terms and conditions + * of the GNU Lesser General Public License v.2.1. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef _LVM_COMMAND_ENUMS_H +#define _LVM_COMMAND_ENUMS_H + +/* + * cmd_enum.h uses the generated cmds.h to create the enum with an ID + * for each command definition in command-lines.in. + */ +#include "lib/commands/cmd_enum.h" + +/* define the enums for the command line --options, foo_ARG */ +enum { +#define arg(a, b, c, d, e, f, g) a , +#include "args.h" +#undef arg +}; + +/* define the enums for the values accepted by command line --options, foo_VAL */ +enum { +#define val(a, b, c, d) a , +#include "vals.h" +#undef val +}; + +/* define enums for LV properties, foo_LVP */ +enum { +#define lvp(a, b, c) a , +#include "lv_props.h" +#undef lvp +}; + +/* define enums for LV types, foo_LVT */ +enum { +#define lvt(a, b, c) a , +#include "lv_types.h" +#undef lvt +}; + +#define PERMITTED_READ_ONLY 0x00000002 +/* Process all VGs if none specified on the command line. */ +#define ALL_VGS_IS_DEFAULT 0x00000004 +/* Process all devices with --all if none are specified on the command line. */ +#define ENABLE_ALL_DEVS 0x00000008 +/* Command may try to interpret a vgname arg as a uuid. */ +#define ALLOW_UUID_AS_NAME 0x00000010 +/* Command needs a shared lock on a VG; it only reads the VG. */ +#define LOCKD_VG_SH 0x00000020 +/* Command does not process any metadata. */ +#define NO_METADATA_PROCESSING 0x00000040 +/* Command must use all specified arg names and fail if all cannot be used. */ +#define MUST_USE_ALL_ARGS 0x00000100 +/* Command should process unused duplicate devices. */ +#define ENABLE_DUPLICATE_DEVS 0x00000400 +/* Command does not accept tags as args. */ +#define DISALLOW_TAG_ARGS 0x00000800 +/* Command may need to find VG name in an option value. */ +#define GET_VGNAME_FROM_OPTIONS 0x00001000 +/* The data read from disk by label scan can be used for vg_read. */ +#define CAN_USE_ONE_SCAN 0x00002000 +/* Command can use hints file */ +#define ALLOW_HINTS 0x00004000 +/* Command can access exported vg. */ +#define ALLOW_EXPORTED 0x00008000 +/* Command checks and reports warning if devs used by LV are incorrect. */ +#define CHECK_DEVS_USED 0x00010000 +/* Command prints devices file entries that were not found. */ +#define DEVICE_ID_NOT_FOUND 0x00020000 + +#include "command.h" /* defines struct command */ +#include "command-count.h" /* defines COMMAND_COUNT */ + +#endif /* _LVM_COMMAND_ENUMS_H */ diff --git a/tools/man-generator.c b/tools/man-generator.c index a0b4dde18..efe2a56f7 100644 --- a/tools/man-generator.c +++ b/tools/man-generator.c @@ -108,65 +108,8 @@ static inline int repairtype_arg(struct cmd_context *cmd __attribute__((unused)) static inline int dumptype_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av) { return 0; } static inline int headings_arg(struct cmd_context *cmd __attribute__((unused)), struct arg_values *av) { return 0; } -/* needed to include commands.h when building man page generator */ -#define CACHE_VGMETADATA 0x00000001 -#define PERMITTED_READ_ONLY 0x00000002 -#define ALL_VGS_IS_DEFAULT 0x00000004 -#define ENABLE_ALL_DEVS 0x00000008 -#define ALLOW_UUID_AS_NAME 0x00000010 -#define LOCKD_VG_SH 0x00000020 -#define NO_METADATA_PROCESSING 0x00000040 -#define MUST_USE_ALL_ARGS 0x00000100 -#define ENABLE_DUPLICATE_DEVS 0x00000400 -#define DISALLOW_TAG_ARGS 0x00000800 -#define GET_VGNAME_FROM_OPTIONS 0x00001000 -#define CAN_USE_ONE_SCAN 0x00002000 -#define ALLOW_HINTS 0x00004000 -#define ALLOW_EXPORTED 0x00008000 -#define CHECK_DEVS_USED 0x00010000 -#define DEVICE_ID_NOT_FOUND 0x00020000 - -/* create foo_CMD enums for command def ID's in command-lines.in */ - -enum { -#define cmd(a, b) a , -#include "../include/cmds.h" -#undef cmd -}; - -/* create foo_VAL enums for option and position values */ - -enum { -#define val(a, b, c, d) a , -#include "vals.h" -#undef val -}; - -/* create foo_ARG enums for --option's */ - -enum { -#define arg(a, b, c, d, e, f, g) a , -#include "args.h" -#undef arg -}; - -/* create foo_LVP enums for LV properties */ - -enum { -#define lvp(a, b, c) a, -#include "lv_props.h" -#undef lvp -}; - -/* create foo_LVT enums for LV types */ - -enum { -#define lvt(a, b, c) a, -#include "lv_types.h" -#undef lvt -}; - #define MAN_PAGE_GENERATOR +#include "command_enums.h" #include "command.c" static const size_t _LONG_LINE = 42; /* length of line that neededs .nh .. .hy */ diff --git a/tools/tools.h b/tools/tools.h index 805f4ec78..dae814284 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -45,54 +45,19 @@ #include "lib/notify/lvmnotify.h" #include "lib/label/hints.h" -/* - * cmd_enum.h uses the generated cmds.h to create the enum with an ID - * for each command definition in command-lines.in. - */ -#include "lib/commands/cmd_enum.h" - #include #include #define CMD_LEN 256 #define MAX_ARGS 64 -/* define the enums for the values accepted by command line --options, foo_VAL */ -enum { -#define val(a, b, c, d) a , -#include "vals.h" -#undef val -}; - -/* define the enums for the command line --options, foo_ARG */ -enum { -#define arg(a, b, c, d, e, f, g) a , -#include "args.h" -#undef arg -}; +#include "command_enums.h" /* command functions */ #define xx(a, b...) int a(struct cmd_context *cmd, int argc, char **argv); #include "commands.h" #undef xx -/* define enums for LV properties, foo_LVP */ -enum { -#define lvp(a, b, c) a , -#include "lv_props.h" -#undef lvp -}; - -/* define enums for LV types, foo_LVT */ -enum { -#define lvt(a, b, c) a , -#include "lv_types.h" -#undef lvt -}; - -#include "command.h" -#include "command-count.h" - #define ARG_COUNTABLE 0x00000001 /* E.g. -vvvv */ #define ARG_GROUPABLE 0x00000002 /* E.g. --addtag */ #define ARG_NONINTERACTIVE 0x00000004 /* only for use in noninteractive mode */ @@ -114,37 +79,6 @@ struct arg_value_group_list { struct arg_values arg_values[]; }; -#define PERMITTED_READ_ONLY 0x00000002 -/* Process all VGs if none specified on the command line. */ -#define ALL_VGS_IS_DEFAULT 0x00000004 -/* Process all devices with --all if none are specified on the command line. */ -#define ENABLE_ALL_DEVS 0x00000008 -/* Command may try to interpret a vgname arg as a uuid. */ -#define ALLOW_UUID_AS_NAME 0x00000010 -/* Command needs a shared lock on a VG; it only reads the VG. */ -#define LOCKD_VG_SH 0x00000020 -/* Command does not process any metadata. */ -#define NO_METADATA_PROCESSING 0x00000040 -/* Command must use all specified arg names and fail if all cannot be used. */ -#define MUST_USE_ALL_ARGS 0x00000100 -/* Command should process unused duplicate devices. */ -#define ENABLE_DUPLICATE_DEVS 0x00000400 -/* Command does not accept tags as args. */ -#define DISALLOW_TAG_ARGS 0x00000800 -/* Command may need to find VG name in an option value. */ -#define GET_VGNAME_FROM_OPTIONS 0x00001000 -/* The data read from disk by label scan can be used for vg_read. */ -#define CAN_USE_ONE_SCAN 0x00002000 -/* Command can use hints file */ -#define ALLOW_HINTS 0x00004000 -/* Command can access exported vg. */ -#define ALLOW_EXPORTED 0x00008000 -/* Command checks and reports warning if devs used by LV are incorrect. */ -#define CHECK_DEVS_USED 0x00010000 -/* Command prints devices file entries that were not found. */ -#define DEVICE_ID_NOT_FOUND 0x00020000 - - void usage(const char *name); /* the argument verify/normalise functions */