Add new argument type CRYPT_ARG_ALIAS.

It can be used to easily define option
aliases for command line utilities.
This commit is contained in:
Ondrej Kozina
2022-03-29 12:51:53 +02:00
parent e4ed545cbf
commit e2a5af9e64
4 changed files with 28 additions and 1 deletions

View File

@@ -3105,6 +3105,15 @@ static void basic_options_cb(poptContext popt_context,
}
}
static void cryptsetup_init_arg_aliases(void)
{
unsigned i;
for (i = 1; i < ARRAY_SIZE(tool_core_args); i++)
if (tool_core_args[i].type == CRYPT_ARG_ALIAS)
ARG_INIT_ALIAS(i);
}
int main(int argc, const char **argv)
{
static struct poptOption popt_help_options[] = {
@@ -3131,6 +3140,9 @@ int main(int argc, const char **argv)
const char *aname, *error_message;
int r;
/* initialize aliases */
cryptsetup_init_arg_aliases();
crypt_set_log_callback(NULL, tool_log, &log_parms);
setlocale(LC_ALL, "");

View File

@@ -132,7 +132,8 @@ typedef enum {
CRYPT_ARG_INT32,
CRYPT_ARG_UINT32,
CRYPT_ARG_INT64,
CRYPT_ARG_UINT64
CRYPT_ARG_UINT64,
CRYPT_ARG_ALIAS
} crypt_arg_type_info;
struct tools_arg {
@@ -145,6 +146,10 @@ struct tools_arg {
uint32_t u32_value;
int32_t i32_value;
int64_t i64_value;
union {
unsigned id;
struct tools_arg *ptr;
} o;
} u;
const char *actions_array[MAX_ACTIONS];
};

View File

@@ -93,4 +93,11 @@ do { \
tool_core_args[(X)].set = true; \
} while (0)
#define ARG_INIT_ALIAS(X) \
do { \
assert(tool_core_args[(X)].type == CRYPT_ARG_ALIAS); \
tool_core_args[(X)].u.o.ptr = &tool_core_args[tool_core_args[(X)].u.o.id]; \
} while (0)
#endif

View File

@@ -75,6 +75,9 @@ void tools_parse_arg_value(poptContext popt_context, crypt_arg_type_info type, s
arg->u.u64_value = ull;
}
break;
case CRYPT_ARG_ALIAS:
tools_parse_arg_value(popt_context, arg->u.o.ptr->type, arg->u.o.ptr, popt_arg, arg->u.o.id, needs_size_conv_fn);
break;
default:
/* this signals internal tools coding mistake */
abort();