/src/FreeRDP/winpr/include/winpr/cmdline.h
Line | Count | Source |
1 | | /** |
2 | | * WinPR: Windows Portable Runtime |
3 | | * Command-Line Utils |
4 | | * |
5 | | * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #ifndef WINPR_CMDLINE_H |
21 | | #define WINPR_CMDLINE_H |
22 | | |
23 | | #include <winpr/winpr.h> |
24 | | #include <winpr/wtypes.h> |
25 | | |
26 | | /* Command-Line Argument Flags */ |
27 | | |
28 | 43 | #define COMMAND_LINE_INPUT_FLAG_MASK 0x0000FFFF |
29 | | #define COMMAND_LINE_OUTPUT_FLAG_MASK 0xFFFF0000 |
30 | | |
31 | | /* Command-Line Argument Input Flags */ |
32 | | |
33 | 13 | #define COMMAND_LINE_VALUE_FLAG 0x00000001 |
34 | 42 | #define COMMAND_LINE_VALUE_REQUIRED 0x00000002 |
35 | 7 | #define COMMAND_LINE_VALUE_OPTIONAL 0x00000004 |
36 | 0 | #define COMMAND_LINE_VALUE_BOOL 0x00000008 |
37 | | |
38 | | #define COMMAND_LINE_ADVANCED 0x00000100 |
39 | 4 | #define COMMAND_LINE_PRINT 0x00000200 |
40 | 6 | #define COMMAND_LINE_PRINT_HELP 0x00000400 |
41 | 4 | #define COMMAND_LINE_PRINT_VERSION 0x00000800 |
42 | 4 | #define COMMAND_LINE_PRINT_BUILDCONFIG 0x00001000 |
43 | | |
44 | | /* Command-Line Argument Output Flags */ |
45 | | |
46 | 48 | #define COMMAND_LINE_ARGUMENT_PRESENT 0x80000000 |
47 | 8 | #define COMMAND_LINE_VALUE_PRESENT 0x40000000 |
48 | | |
49 | | /* Command-Line Parsing Flags */ |
50 | | |
51 | 0 | #define COMMAND_LINE_SIGIL_NONE 0x00000001 |
52 | 4 | #define COMMAND_LINE_SIGIL_SLASH 0x00000002 |
53 | 9 | #define COMMAND_LINE_SIGIL_DASH 0x00000004 |
54 | 4 | #define COMMAND_LINE_SIGIL_DOUBLE_DASH 0x00000008 |
55 | 0 | #define COMMAND_LINE_SIGIL_PLUS_MINUS 0x00000010 |
56 | 4 | #define COMMAND_LINE_SIGIL_ENABLE_DISABLE 0x00000020 |
57 | 0 | #define COMMAND_LINE_SIGIL_NOT_ESCAPED 0x00000040 |
58 | | |
59 | 4 | #define COMMAND_LINE_SEPARATOR_COLON 0x00000100 |
60 | 4 | #define COMMAND_LINE_SEPARATOR_EQUAL 0x00000200 |
61 | 9 | #define COMMAND_LINE_SEPARATOR_SPACE 0x00000400 |
62 | | |
63 | | /* Suppress COMMAND_LINE_ERROR_NO_KEYWORD return. */ |
64 | 0 | #define COMMAND_LINE_IGN_UNKNOWN_KEYWORD 0x00001000 |
65 | 0 | #define COMMAND_LINE_SILENCE_PARSER 0x00002000 |
66 | | |
67 | | /* Command-Line Parsing Error Codes */ |
68 | | |
69 | 0 | #define COMMAND_LINE_ERROR -1000 |
70 | 0 | #define COMMAND_LINE_ERROR_NO_KEYWORD -1001 |
71 | 0 | #define COMMAND_LINE_ERROR_UNEXPECTED_VALUE -1002 |
72 | 0 | #define COMMAND_LINE_ERROR_MISSING_VALUE -1003 |
73 | | #define COMMAND_LINE_ERROR_MISSING_ARGUMENT -1004 |
74 | | #define COMMAND_LINE_ERROR_UNEXPECTED_SIGIL -1005 |
75 | | #define COMMAND_LINE_ERROR_MEMORY -1006 |
76 | | #define COMMAND_LINE_ERROR_LAST -1999 |
77 | | |
78 | | /* Command-Line Parsing Status Codes */ |
79 | | |
80 | 0 | #define COMMAND_LINE_STATUS_PRINT -2001 |
81 | 1 | #define COMMAND_LINE_STATUS_PRINT_HELP -2002 |
82 | 0 | #define COMMAND_LINE_STATUS_PRINT_VERSION -2003 |
83 | 0 | #define COMMAND_LINE_STATUS_PRINT_BUILDCONFIG -2004 |
84 | | #define COMMAND_LINE_STATUS_PRINT_LAST -2999 |
85 | | |
86 | | /* Command-Line Macros */ |
87 | | |
88 | | #define CommandLineSwitchStart(_arg) \ |
89 | 4 | if (0) \ |
90 | 4 | { \ |
91 | 0 | } |
92 | 21 | #define CommandLineSwitchCase(_arg, _name) else if (strcmp(_arg->Name, _name) == 0) |
93 | | #define CommandLineSwitchDefault(_arg) else |
94 | | #define CommandLineSwitchEnd(_arg) |
95 | | |
96 | 0 | #define BoolValueTrue WINPR_CXX_COMPAT_CAST(LPSTR, 1) |
97 | 0 | #define BoolValueFalse WINPR_CXX_COMPAT_CAST(LPSTR, nullptr) |
98 | | |
99 | | typedef struct |
100 | | { |
101 | | LPCSTR Name; |
102 | | DWORD Flags; |
103 | | LPCSTR Format; |
104 | | LPCSTR Default; |
105 | | LPSTR Value; |
106 | | LONG Index; |
107 | | LPCSTR Alias; |
108 | | LPCSTR Text; |
109 | | } COMMAND_LINE_ARGUMENT_A; |
110 | | |
111 | | typedef struct |
112 | | { |
113 | | LPCWSTR Name; |
114 | | DWORD Flags; |
115 | | LPCSTR Format; |
116 | | LPWSTR Default; |
117 | | LPWSTR Value; |
118 | | LONG Index; |
119 | | LPCWSTR Alias; |
120 | | LPCWSTR Text; |
121 | | } COMMAND_LINE_ARGUMENT_W; |
122 | | |
123 | | #ifdef UNICODE |
124 | | #define COMMAND_LINE_ARGUMENT COMMAND_LINE_ARGUMENT_W |
125 | | #else |
126 | | #define COMMAND_LINE_ARGUMENT COMMAND_LINE_ARGUMENT_A |
127 | | #endif |
128 | | |
129 | | typedef int (*COMMAND_LINE_PRE_FILTER_FN_A)(void* context, int index, int argc, LPSTR* argv); |
130 | | typedef int (*COMMAND_LINE_PRE_FILTER_FN_W)(void* context, int index, int argc, LPWSTR* argv); |
131 | | |
132 | | typedef int (*COMMAND_LINE_POST_FILTER_FN_A)(void* context, COMMAND_LINE_ARGUMENT_A* arg); |
133 | | typedef int (*COMMAND_LINE_POST_FILTER_FN_W)(void* context, COMMAND_LINE_ARGUMENT_W* arg); |
134 | | |
135 | | #ifdef __cplusplus |
136 | | extern "C" |
137 | | { |
138 | | #endif |
139 | | |
140 | | WINPR_API int CommandLineClearArgumentsA(COMMAND_LINE_ARGUMENT_A* options); |
141 | | WINPR_API int CommandLineClearArgumentsW(COMMAND_LINE_ARGUMENT_W* options); |
142 | | |
143 | | WINPR_ATTR_NODISCARD |
144 | | WINPR_API int CommandLineParseArgumentsA(int argc, LPSTR* argv, |
145 | | COMMAND_LINE_ARGUMENT_A* options, DWORD flags, |
146 | | void* context, COMMAND_LINE_PRE_FILTER_FN_A preFilter, |
147 | | COMMAND_LINE_POST_FILTER_FN_A postFilter); |
148 | | |
149 | | WINPR_ATTR_NODISCARD |
150 | | WINPR_API int CommandLineParseArgumentsW(int argc, LPWSTR* argv, |
151 | | COMMAND_LINE_ARGUMENT_W* options, DWORD flags, |
152 | | void* context, COMMAND_LINE_PRE_FILTER_FN_W preFilter, |
153 | | COMMAND_LINE_POST_FILTER_FN_W postFilter); |
154 | | |
155 | | WINPR_ATTR_NODISCARD |
156 | | WINPR_API const COMMAND_LINE_ARGUMENT_A* |
157 | | CommandLineFindArgumentA(const COMMAND_LINE_ARGUMENT_A* options, LPCSTR Name); |
158 | | |
159 | | WINPR_ATTR_NODISCARD |
160 | | WINPR_API const COMMAND_LINE_ARGUMENT_W* |
161 | | CommandLineFindArgumentW(const COMMAND_LINE_ARGUMENT_W* options, LPCWSTR Name); |
162 | | |
163 | | WINPR_ATTR_NODISCARD |
164 | | WINPR_API const COMMAND_LINE_ARGUMENT_A* |
165 | | CommandLineFindNextArgumentA(const COMMAND_LINE_ARGUMENT_A* argument); |
166 | | |
167 | | /** @brief free arrays allocated by CommandLineParseCommaSeparatedValues(Ex) |
168 | | * |
169 | | * @param ptr the pointer to free, may be \b nullptr |
170 | | * |
171 | | * @since version 3.10.0 |
172 | | */ |
173 | | WINPR_API void CommandLineParserFree(char** ptr); |
174 | | |
175 | | WINPR_ATTR_MALLOC(CommandLineParserFree, 1) |
176 | | WINPR_API char** CommandLineParseCommaSeparatedValues(const char* list, size_t* count); |
177 | | |
178 | | WINPR_ATTR_MALLOC(CommandLineParserFree, 1) |
179 | | WINPR_API char** CommandLineParseCommaSeparatedValuesEx(const char* name, const char* list, |
180 | | size_t* count); |
181 | | |
182 | | WINPR_ATTR_MALLOC(free, 1) |
183 | | WINPR_API char* CommandLineToCommaSeparatedValues(int argc, char* argv[]); |
184 | | |
185 | | WINPR_ATTR_MALLOC(free, 1) |
186 | | WINPR_API char* CommandLineToCommaSeparatedValuesEx(int argc, char* argv[], |
187 | | const char* filters[], size_t number); |
188 | | |
189 | | #ifdef __cplusplus |
190 | | } |
191 | | #endif |
192 | | |
193 | | #ifdef UNICODE |
194 | | #define CommandLineClearArguments CommandLineClearArgumentsW |
195 | | #define CommandLineParseArguments CommandLineParseArgumentsW |
196 | | #define CommandLineFindArgument CommandLineFindArgumentW |
197 | | #else |
198 | | #define CommandLineClearArguments CommandLineClearArgumentsA |
199 | | #define CommandLineParseArguments CommandLineParseArgumentsA |
200 | | #define CommandLineFindArgument CommandLineFindArgumentA |
201 | | #endif |
202 | | |
203 | | #endif /* WINPR_CMDLINE_H */ |