2 * Command line arguments parsing helpers
4 * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2020-2023 Ondrej Kozina
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "cryptsetup.h"
24 void tools_parse_arg_value(poptContext popt_context, crypt_arg_type_info type, struct tools_arg *arg, const char *popt_arg, int popt_val, bool(*needs_size_conv_fn)(unsigned arg_id))
28 long long unsigned int ull;
35 case CRYPT_ARG_STRING:
37 free(arg->u.str_value);
38 arg->u.str_value = poptGetOptArg(popt_context);
41 ll = strtoll(popt_arg, &end, 10);
42 if (*end || !*popt_arg || ll > INT32_MAX || ll < INT32_MIN || errno == ERANGE)
43 usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
44 poptGetInvocationName(popt_context));
45 arg->u.i32_value = (int32_t)ll;
47 case CRYPT_ARG_UINT32:
48 ull = strtoull(popt_arg, &end, 10);
49 if (*end || !*popt_arg || ull > UINT32_MAX || errno == ERANGE)
50 usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
51 poptGetInvocationName(popt_context));
52 arg->u.u32_value = (uint32_t)ull;
55 ll = strtoll(popt_arg, &end, 10);
56 if (*end || !*popt_arg || errno == ERANGE)
57 usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
58 poptGetInvocationName(popt_context));
59 arg->u.i64_value = ll;
61 case CRYPT_ARG_UINT64:
62 /* special size strings with units converted to integers */
63 if (needs_size_conv_fn && needs_size_conv_fn(popt_val)) {
64 if (tools_string_to_size(popt_arg, &arg->u.u64_value)) {
65 if (snprintf(msg, sizeof(msg), _("Invalid size specification in parameter --%s."), arg->name) < 0)
67 usage(popt_context, EXIT_FAILURE, msg,
68 poptGetInvocationName(popt_context));
71 ull = strtoull(popt_arg, &end, 10);
72 if (*end || !*popt_arg || errno == ERANGE)
73 usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
74 poptGetInvocationName(popt_context));
75 arg->u.u64_value = ull;
79 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);
82 /* this signals internal tools coding mistake */
89 void tools_args_free(struct tools_arg *args, size_t args_size)
93 for (i = 0; i < args_size; i++) {
94 if (args[i].set && args[i].type == CRYPT_ARG_STRING)
95 free(args[i].u.str_value);
100 static bool action_allowed(const char *action, const char * const* list, size_t list_size)
107 for (i = 0; i < list_size && list[i]; i++) {
108 if (!strcmp(action, list[i]))
115 void tools_check_args(const char *action, const struct tools_arg *args, size_t args_size, poptContext popt_context)
120 for (i = 1; i < args_size; i++) {
122 if (action_allowed(action, args[i].actions_array, MAX_ACTIONS)) {
125 if (snprintf(msg, sizeof(msg), _("Option --%s is not allowed with %s action."), args[i].name, action) < 0)
127 usage(popt_context, EXIT_FAILURE, msg, poptGetInvocationName(popt_context));