summaryrefslogtreecommitdiffstats
path: root/src/utils_args.c
blob: fda2350661e42556472db49342ef8bc1af49eb6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
 * Command line arguments parsing helpers
 *
 * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved.
 * Copyright (C) 2020-2023 Ondrej Kozina
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU 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.
 */

#include "cryptsetup.h"

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))
{
	char *end, msg[128];
	long long int ll;
	long long unsigned int ull;

	errno = 0;

	switch (type) {
	case CRYPT_ARG_BOOL:
		break;
	case CRYPT_ARG_STRING:
		if (arg->set)
			free(arg->u.str_value);
		arg->u.str_value = poptGetOptArg(popt_context);
		break;
	case CRYPT_ARG_INT32:
		ll = strtoll(popt_arg, &end, 10);
		if (*end || !*popt_arg || ll > INT32_MAX || ll < INT32_MIN || errno == ERANGE)
			usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
			      poptGetInvocationName(popt_context));
		arg->u.i32_value = (int32_t)ll;
		break;
	case CRYPT_ARG_UINT32:
		ull = strtoull(popt_arg, &end, 10);
		if (*end || !*popt_arg || ull > UINT32_MAX || errno == ERANGE)
			usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
			      poptGetInvocationName(popt_context));
		arg->u.u32_value = (uint32_t)ull;
		break;
	case CRYPT_ARG_INT64:
		ll = strtoll(popt_arg, &end, 10);
		if (*end || !*popt_arg || errno == ERANGE)
			usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
			      poptGetInvocationName(popt_context));
		arg->u.i64_value = ll;
		break;
	case CRYPT_ARG_UINT64:
		/* special size strings with units converted to integers */
		if (needs_size_conv_fn && needs_size_conv_fn(popt_val)) {
			if (tools_string_to_size(popt_arg, &arg->u.u64_value)) {
				if (snprintf(msg, sizeof(msg), _("Invalid size specification in parameter --%s."), arg->name) < 0)
					msg[0] = '\0';
				usage(popt_context, EXIT_FAILURE, msg,
				      poptGetInvocationName(popt_context));
			}
		} else {
			ull = strtoull(popt_arg, &end, 10);
			if (*end || !*popt_arg || errno == ERANGE)
				usage(popt_context, EXIT_FAILURE, poptStrerror(POPT_ERROR_BADNUMBER),
				      poptGetInvocationName(popt_context));
			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();
	}

	arg->set = true;
}

void tools_args_free(struct tools_arg *args, size_t args_size)
{
	size_t i;

	for (i = 0; i < args_size; i++) {
		if (args[i].set && args[i].type == CRYPT_ARG_STRING)
			free(args[i].u.str_value);
		args[i].set = false;
	}
}

static bool action_allowed(const char *action, const char * const* list, size_t list_size)
{
	size_t i;

	if (!list[0])
		return true;

	for (i = 0; i < list_size && list[i]; i++) {
		if (!strcmp(action, list[i]))
			return true;
	}

	return false;
}

void tools_check_args(const char *action, const struct tools_arg *args, size_t args_size, poptContext popt_context)
{
	size_t i;
	char msg[256];

	for (i = 1; i < args_size; i++) {
		if (args[i].set) {
			if (action_allowed(action, args[i].actions_array, MAX_ACTIONS)) {
				continue;
			} else {
				if (snprintf(msg, sizeof(msg), _("Option --%s is not allowed with %s action."), args[i].name, action) < 0)
					msg[0] = '\0';
				usage(popt_context, EXIT_FAILURE, msg, poptGetInvocationName(popt_context));
			}
		}
	}
}