"Fossies" - the Fresh Open Source Software Archive 
Member "cryptsetup-2.4.3/src/utils_args.c" (13 Jan 2022, 3927 Bytes) of package /linux/misc/cryptsetup-2.4.3.tar.xz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "utils_args.c" see the
Fossies "Dox" file reference documentation.
1 /*
2 * Command line arguments parsing helpers
3 *
4 * Copyright (C) 2020-2021 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2020-2021 Ondrej Kozina
6 *
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.
11 *
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.
16 *
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.
20 */
21
22 #include "cryptsetup.h"
23
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))
25 {
26 char *end, msg[128];
27 long long int ll;
28 long long unsigned int ull;
29
30 errno = 0;
31
32 switch (type) {
33 case CRYPT_ARG_BOOL:
34 break;
35 case CRYPT_ARG_STRING:
36 if (arg->set)
37 free(arg->u.str_value);
38 arg->u.str_value = poptGetOptArg(popt_context);
39 break;
40 case CRYPT_ARG_INT32:
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;
46 break;
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;
53 break;
54 case CRYPT_ARG_INT64:
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;
60 break;
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 snprintf(msg, sizeof(msg), _("Invalid size specification in parameter --%s."), arg->name);
66 usage(popt_context, EXIT_FAILURE, msg,
67 poptGetInvocationName(popt_context));
68 }
69 } else {
70 ull = strtoull(popt_arg, &end, 10);
71 if (*end || !*popt_arg || errno == ERANGE)
72 usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
73 poptGetInvocationName(popt_context));
74 arg->u.u64_value = ull;
75 }
76 break;
77 default:
78 /* this signals internal tools coding mistake */
79 abort();
80 }
81
82 arg->set = true;
83 }
84
85 void tools_args_free(struct tools_arg *args, size_t args_size)
86 {
87 size_t i;
88
89 for (i = 0; i < args_size; i++) {
90 if (args[i].set && args[i].type == CRYPT_ARG_STRING)
91 free(args[i].u.str_value);
92 args[i].set = false;
93 }
94 }
95
96 static bool action_allowed(const char *action, const char * const* list, size_t list_size)
97 {
98 size_t i;
99
100 if (!list[0])
101 return true;
102
103 for (i = 0; i < list_size && list[i]; i++) {
104 if (!strcmp(action, list[i]))
105 return true;
106 }
107
108 return false;
109 }
110
111 void tools_check_args(const char *action, const struct tools_arg *args, size_t args_size, poptContext popt_context)
112 {
113 size_t i;
114 char msg[256];
115
116 for (i = 1; i < args_size; i++) {
117 if (args[i].set) {
118 if (action_allowed(action, args[i].actions_array, MAX_ACTIONS)) {
119 continue;
120 } else {
121 (void)snprintf(msg, sizeof(msg), _("Option --%s is not allowed with %s action."), args[i].name, action);
122 usage(popt_context, EXIT_FAILURE, msg, poptGetInvocationName(popt_context));
123 }
124 }
125 }
126 }