Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / src / utils_arg_macros.h
1 /*
2  * Command line arguments parsing helpers
3  *
4  * Copyright (C) 2020-2023 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2020-2023 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 #ifndef UTILS_ARG_MACROS_H
23 #define UTILS_ARG_MACROS_H
24
25 #include <assert.h>
26
27 #define ARG_SET(X) !!tool_core_args[(X)].set
28
29 #define ARG_STR(X) ({ \
30         assert(tool_core_args[(X)].type == CRYPT_ARG_STRING); \
31         tool_core_args[(X)].u.str_value; \
32 })
33
34 #define ARG_INT32(X) ({ \
35         assert(tool_core_args[(X)].type == CRYPT_ARG_INT32); \
36         tool_core_args[(X)].u.i32_value; \
37 })
38
39 #define ARG_UINT32(X) ({ \
40         assert(tool_core_args[(X)].type == CRYPT_ARG_UINT32); \
41         tool_core_args[(X)].u.u32_value; \
42 })
43
44 #define ARG_INT64(X) ({ \
45         assert(tool_core_args[(X)].type == CRYPT_ARG_INT64); \
46         tool_core_args[(X)].u.i64_value; \
47 })
48
49 #define ARG_UINT64(X) ({ \
50         assert(tool_core_args[(X)].type == CRYPT_ARG_UINT64); \
51         tool_core_args[(X)].u.u64_value; \
52 })
53
54 #define ARG_SET_TRUE(X) do { \
55         tool_core_args[(X)].set = true; \
56 } while (0)
57
58 #define ARG_SET_STR(X, Y) \
59 do { \
60         char *str; \
61         assert(tool_core_args[(X)].set == false && tool_core_args[(X)].type == CRYPT_ARG_STRING); \
62         str = (Y); \
63         assert(str != NULL); \
64         tool_core_args[(X)].u.str_value = str; \
65         tool_core_args[(X)].set = true; \
66 } while (0)
67
68 #define ARG_SET_INT32(X, Y) \
69 do { \
70         assert(tool_core_args[(X)].set == false && tool_core_args[(X)].type == CRYPT_ARG_INT32); \
71         tool_core_args[(X)].u.i32_value = (Y); \
72         tool_core_args[(X)].set = true; \
73 } while (0)
74
75 #define ARG_SET_UINT32(X, Y) \
76 do { \
77         assert(tool_core_args[(X)].set == false && tool_core_args[(X)].type == CRYPT_ARG_UINT32); \
78         tool_core_args[(X)].u.u32_value = (Y); \
79         tool_core_args[(X)].set = true; \
80 } while (0)
81
82 #define ARG_SET_INT64(X, Y) \
83 do { \
84         assert(tool_core_args[(X)].set == false && tool_core_args[(X)].type == CRYPT_ARG_INT64); \
85         tool_core_args[(X)].u.i64_value = (Y); \
86         tool_core_args[(X)].set = true; \
87 } while (0)
88
89 #define ARG_SET_UINT64(X, Y) \
90 do { \
91         assert(tool_core_args[(X)].set == false && tool_core_args[(X)].type == CRYPT_ARG_UINT64); \
92         tool_core_args[(X)].u.u64_value = (Y); \
93         tool_core_args[(X)].set = true; \
94 } while (0)
95
96
97 #define ARG_INIT_ALIAS(X) \
98 do { \
99         assert(tool_core_args[(X)].type == CRYPT_ARG_ALIAS); \
100         tool_core_args[(X)].u.o.ptr = &tool_core_args[tool_core_args[(X)].u.o.id]; \
101 } while (0)
102
103 #endif