/src/FreeRDP/winpr/libwinpr/utils/cmdline.c
Line | Count | Source (jump to first uncovered line) |
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 | | #include <winpr/config.h> |
21 | | |
22 | | #include <winpr/crt.h> |
23 | | #include <winpr/assert.h> |
24 | | #include <winpr/cmdline.h> |
25 | | |
26 | | #include "../log.h" |
27 | | |
28 | 0 | #define TAG WINPR_TAG("commandline") |
29 | | |
30 | | /** |
31 | | * Command-line syntax: some basic concepts: |
32 | | * https://pythonconquerstheuniverse.wordpress.com/2010/07/25/command-line-syntax-some-basic-concepts/ |
33 | | */ |
34 | | |
35 | | /** |
36 | | * Command-Line Syntax: |
37 | | * |
38 | | * <sigil><keyword><separator><value> |
39 | | * |
40 | | * <sigil>: '/' or '-' or ('+' | '-') |
41 | | * |
42 | | * <keyword>: option, named argument, flag |
43 | | * |
44 | | * <separator>: ':' or '=' |
45 | | * |
46 | | * <value>: argument value |
47 | | * |
48 | | */ |
49 | | |
50 | | #if !defined(WITH_DEBUG_UTILS_CMDLINE_DUMP) |
51 | | static const char censoredmessage[] = |
52 | | "<censored: build with -DWITH_DEBUG_UTILS_CMDLINE_DUMP=ON for details>"; |
53 | | #endif |
54 | | |
55 | | #define log_error(flags, msg, index, arg) \ |
56 | 0 | log_error_((flags), (msg), (index), (arg), __FILE__, __func__, __LINE__) |
57 | | static void log_error_(DWORD flags, LPCSTR message, int index, WINPR_ATTR_UNUSED LPCSTR argv, |
58 | | const char* file, const char* fkt, size_t line) |
59 | 0 | { |
60 | 0 | if ((flags & COMMAND_LINE_SILENCE_PARSER) == 0) |
61 | 0 | { |
62 | 0 | const DWORD level = WLOG_ERROR; |
63 | 0 | static wLog* log = NULL; |
64 | 0 | if (!WLog_IsLevelActive(log, level)) |
65 | 0 | return; |
66 | | |
67 | 0 | WLog_PrintTextMessage(log, level, line, file, fkt, "Failed at index %d [%s]: %s", index, |
68 | | #if defined(WITH_DEBUG_UTILS_CMDLINE_DUMP) |
69 | | argv |
70 | | #else |
71 | 0 | censoredmessage |
72 | 0 | #endif |
73 | 0 | , |
74 | 0 | message); |
75 | 0 | } |
76 | 0 | } |
77 | | |
78 | 0 | #define log_comma_error(msg, arg) log_comma_error_((msg), (arg), __FILE__, __func__, __LINE__) |
79 | | static void log_comma_error_(const char* message, WINPR_ATTR_UNUSED const char* argument, |
80 | | const char* file, const char* fkt, size_t line) |
81 | 0 | { |
82 | 0 | const DWORD level = WLOG_ERROR; |
83 | 0 | static wLog* log = NULL; |
84 | 0 | if (!log) |
85 | 0 | log = WLog_Get(TAG); |
86 | |
|
87 | 0 | if (!WLog_IsLevelActive(log, level)) |
88 | 0 | return; |
89 | | |
90 | 0 | WLog_PrintTextMessage(log, level, line, file, fkt, "%s [%s]", message, |
91 | | #if defined(WITH_DEBUG_UTILS_CMDLINE_DUMP) |
92 | | argument |
93 | | #else |
94 | 0 | censoredmessage |
95 | 0 | #endif |
96 | 0 | ); |
97 | 0 | } |
98 | | |
99 | | int CommandLineParseArgumentsA(int argc, LPSTR* argv, COMMAND_LINE_ARGUMENT_A* options, DWORD flags, |
100 | | void* context, COMMAND_LINE_PRE_FILTER_FN_A preFilter, |
101 | | COMMAND_LINE_POST_FILTER_FN_A postFilter) |
102 | 0 | { |
103 | 0 | int status = 0; |
104 | 0 | int count = 0; |
105 | 0 | BOOL notescaped = FALSE; |
106 | 0 | const char* sigil = NULL; |
107 | 0 | size_t sigil_length = 0; |
108 | 0 | char* keyword = NULL; |
109 | 0 | size_t keyword_index = 0; |
110 | 0 | char* separator = NULL; |
111 | 0 | char* value = NULL; |
112 | 0 | int toggle = 0; |
113 | |
|
114 | 0 | if (!argv) |
115 | 0 | return status; |
116 | | |
117 | 0 | if (argc == 1) |
118 | 0 | { |
119 | 0 | if (flags & COMMAND_LINE_IGN_UNKNOWN_KEYWORD) |
120 | 0 | status = 0; |
121 | 0 | else |
122 | 0 | status = COMMAND_LINE_STATUS_PRINT_HELP; |
123 | |
|
124 | 0 | return status; |
125 | 0 | } |
126 | | |
127 | 0 | for (int i = 1; i < argc; i++) |
128 | 0 | { |
129 | 0 | size_t keyword_length = 0; |
130 | 0 | BOOL found = FALSE; |
131 | 0 | BOOL escaped = TRUE; |
132 | |
|
133 | 0 | if (preFilter) |
134 | 0 | { |
135 | 0 | count = preFilter(context, i, argc, argv); |
136 | |
|
137 | 0 | if (count < 0) |
138 | 0 | { |
139 | 0 | log_error(flags, "Failed for index %d [%s]: PreFilter rule could not be applied", i, |
140 | 0 | argv[i]); |
141 | 0 | status = COMMAND_LINE_ERROR; |
142 | 0 | return status; |
143 | 0 | } |
144 | | |
145 | 0 | if (count > 0) |
146 | 0 | { |
147 | 0 | i += (count - 1); |
148 | 0 | continue; |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | 0 | sigil = argv[i]; |
153 | 0 | size_t length = strlen(argv[i]); |
154 | |
|
155 | 0 | if ((sigil[0] == '/') && (flags & COMMAND_LINE_SIGIL_SLASH)) |
156 | 0 | { |
157 | 0 | sigil_length = 1; |
158 | 0 | } |
159 | 0 | else if ((sigil[0] == '-') && (flags & COMMAND_LINE_SIGIL_DASH)) |
160 | 0 | { |
161 | 0 | sigil_length = 1; |
162 | |
|
163 | 0 | if (length > 2) |
164 | 0 | { |
165 | 0 | if ((sigil[1] == '-') && (flags & COMMAND_LINE_SIGIL_DOUBLE_DASH)) |
166 | 0 | sigil_length = 2; |
167 | 0 | } |
168 | 0 | } |
169 | 0 | else if ((sigil[0] == '+') && (flags & COMMAND_LINE_SIGIL_PLUS_MINUS)) |
170 | 0 | { |
171 | 0 | sigil_length = 1; |
172 | 0 | } |
173 | 0 | else if ((sigil[0] == '-') && (flags & COMMAND_LINE_SIGIL_PLUS_MINUS)) |
174 | 0 | { |
175 | 0 | sigil_length = 1; |
176 | 0 | } |
177 | 0 | else if (flags & COMMAND_LINE_SIGIL_NONE) |
178 | 0 | { |
179 | 0 | sigil_length = 0; |
180 | 0 | } |
181 | 0 | else if (flags & COMMAND_LINE_SIGIL_NOT_ESCAPED) |
182 | 0 | { |
183 | 0 | if (notescaped) |
184 | 0 | { |
185 | 0 | log_error(flags, "Failed at index %d [%s]: Unescaped sigil", i, argv[i]); |
186 | 0 | return COMMAND_LINE_ERROR; |
187 | 0 | } |
188 | | |
189 | 0 | sigil_length = 0; |
190 | 0 | escaped = FALSE; |
191 | 0 | notescaped = TRUE; |
192 | 0 | } |
193 | 0 | else |
194 | 0 | { |
195 | 0 | log_error(flags, "Failed at index %d [%s]: Invalid sigil", i, argv[i]); |
196 | 0 | return COMMAND_LINE_ERROR; |
197 | 0 | } |
198 | | |
199 | 0 | if ((sigil_length > 0) || (flags & COMMAND_LINE_SIGIL_NONE) || |
200 | 0 | (flags & COMMAND_LINE_SIGIL_NOT_ESCAPED)) |
201 | 0 | { |
202 | 0 | if (length < (sigil_length + 1)) |
203 | 0 | { |
204 | 0 | if ((flags & COMMAND_LINE_IGN_UNKNOWN_KEYWORD)) |
205 | 0 | continue; |
206 | | |
207 | 0 | return COMMAND_LINE_ERROR_NO_KEYWORD; |
208 | 0 | } |
209 | | |
210 | 0 | keyword_index = sigil_length; |
211 | 0 | keyword = &argv[i][keyword_index]; |
212 | 0 | toggle = -1; |
213 | |
|
214 | 0 | if (flags & COMMAND_LINE_SIGIL_ENABLE_DISABLE) |
215 | 0 | { |
216 | 0 | if (strncmp(keyword, "enable-", 7) == 0) |
217 | 0 | { |
218 | 0 | toggle = TRUE; |
219 | 0 | keyword_index += 7; |
220 | 0 | keyword = &argv[i][keyword_index]; |
221 | 0 | } |
222 | 0 | else if (strncmp(keyword, "disable-", 8) == 0) |
223 | 0 | { |
224 | 0 | toggle = FALSE; |
225 | 0 | keyword_index += 8; |
226 | 0 | keyword = &argv[i][keyword_index]; |
227 | 0 | } |
228 | 0 | } |
229 | |
|
230 | 0 | separator = NULL; |
231 | |
|
232 | 0 | if ((flags & COMMAND_LINE_SEPARATOR_COLON) && (!separator)) |
233 | 0 | separator = strchr(keyword, ':'); |
234 | |
|
235 | 0 | if ((flags & COMMAND_LINE_SEPARATOR_EQUAL) && (!separator)) |
236 | 0 | separator = strchr(keyword, '='); |
237 | |
|
238 | 0 | if (separator) |
239 | 0 | { |
240 | 0 | SSIZE_T separator_index = (separator - argv[i]); |
241 | 0 | SSIZE_T value_index = separator_index + 1; |
242 | 0 | keyword_length = WINPR_ASSERTING_INT_CAST(size_t, (separator - keyword)); |
243 | 0 | value = &argv[i][value_index]; |
244 | 0 | } |
245 | 0 | else |
246 | 0 | { |
247 | 0 | if (length < keyword_index) |
248 | 0 | { |
249 | 0 | log_error(flags, "Failed at index %d [%s]: Argument required", i, argv[i]); |
250 | 0 | return COMMAND_LINE_ERROR; |
251 | 0 | } |
252 | | |
253 | 0 | keyword_length = length - keyword_index; |
254 | 0 | value = NULL; |
255 | 0 | } |
256 | | |
257 | 0 | if (!escaped) |
258 | 0 | continue; |
259 | | |
260 | 0 | for (size_t j = 0; options[j].Name != NULL; j++) |
261 | 0 | { |
262 | 0 | COMMAND_LINE_ARGUMENT_A* cur = &options[j]; |
263 | 0 | BOOL match = FALSE; |
264 | |
|
265 | 0 | if (strncmp(cur->Name, keyword, keyword_length) == 0) |
266 | 0 | { |
267 | 0 | if (strlen(cur->Name) == keyword_length) |
268 | 0 | match = TRUE; |
269 | 0 | } |
270 | |
|
271 | 0 | if ((!match) && (cur->Alias != NULL)) |
272 | 0 | { |
273 | 0 | if (strncmp(cur->Alias, keyword, keyword_length) == 0) |
274 | 0 | { |
275 | 0 | if (strlen(cur->Alias) == keyword_length) |
276 | 0 | match = TRUE; |
277 | 0 | } |
278 | 0 | } |
279 | |
|
280 | 0 | if (!match) |
281 | 0 | continue; |
282 | | |
283 | 0 | found = match; |
284 | 0 | cur->Index = i; |
285 | |
|
286 | 0 | if ((flags & COMMAND_LINE_SEPARATOR_SPACE) && ((i + 1) < argc)) |
287 | 0 | { |
288 | 0 | BOOL argument = 0; |
289 | 0 | int value_present = 1; |
290 | |
|
291 | 0 | if (flags & COMMAND_LINE_SIGIL_DASH) |
292 | 0 | { |
293 | 0 | if (strncmp(argv[i + 1], "-", 1) == 0) |
294 | 0 | value_present = 0; |
295 | 0 | } |
296 | |
|
297 | 0 | if (flags & COMMAND_LINE_SIGIL_DOUBLE_DASH) |
298 | 0 | { |
299 | 0 | if (strncmp(argv[i + 1], "--", 2) == 0) |
300 | 0 | value_present = 0; |
301 | 0 | } |
302 | |
|
303 | 0 | if (flags & COMMAND_LINE_SIGIL_SLASH) |
304 | 0 | { |
305 | 0 | if (strncmp(argv[i + 1], "/", 1) == 0) |
306 | 0 | value_present = 0; |
307 | 0 | } |
308 | |
|
309 | 0 | if ((cur->Flags & COMMAND_LINE_VALUE_REQUIRED) || |
310 | 0 | (cur->Flags & COMMAND_LINE_VALUE_OPTIONAL)) |
311 | 0 | argument = TRUE; |
312 | 0 | else |
313 | 0 | argument = FALSE; |
314 | |
|
315 | 0 | if (value_present && argument) |
316 | 0 | { |
317 | 0 | i++; |
318 | 0 | value = argv[i]; |
319 | 0 | } |
320 | 0 | else if (!value_present && (cur->Flags & COMMAND_LINE_VALUE_OPTIONAL)) |
321 | 0 | { |
322 | 0 | value = NULL; |
323 | 0 | } |
324 | 0 | else if (!value_present && argument) |
325 | 0 | { |
326 | 0 | log_error(flags, "Failed at index %d [%s]: Argument required", i, argv[i]); |
327 | 0 | return COMMAND_LINE_ERROR; |
328 | 0 | } |
329 | 0 | } |
330 | | |
331 | 0 | if (!(flags & COMMAND_LINE_SEPARATOR_SPACE)) |
332 | 0 | { |
333 | 0 | if (value && (cur->Flags & COMMAND_LINE_VALUE_FLAG)) |
334 | 0 | { |
335 | 0 | log_error(flags, "Failed at index %d [%s]: Unexpected value", i, argv[i]); |
336 | 0 | return COMMAND_LINE_ERROR_UNEXPECTED_VALUE; |
337 | 0 | } |
338 | 0 | } |
339 | 0 | else |
340 | 0 | { |
341 | 0 | if (value && (cur->Flags & COMMAND_LINE_VALUE_FLAG)) |
342 | 0 | { |
343 | 0 | i--; |
344 | 0 | value = NULL; |
345 | 0 | } |
346 | 0 | } |
347 | | |
348 | 0 | if (!value && (cur->Flags & COMMAND_LINE_VALUE_REQUIRED)) |
349 | 0 | { |
350 | 0 | log_error(flags, "Failed at index %d [%s]: Missing value", i, argv[i]); |
351 | 0 | status = COMMAND_LINE_ERROR_MISSING_VALUE; |
352 | 0 | return status; |
353 | 0 | } |
354 | | |
355 | 0 | cur->Flags |= COMMAND_LINE_ARGUMENT_PRESENT; |
356 | |
|
357 | 0 | if (value) |
358 | 0 | { |
359 | 0 | if (!(cur->Flags & (COMMAND_LINE_VALUE_OPTIONAL | COMMAND_LINE_VALUE_REQUIRED))) |
360 | 0 | { |
361 | 0 | log_error(flags, "Failed at index %d [%s]: Unexpected value", i, argv[i]); |
362 | 0 | return COMMAND_LINE_ERROR_UNEXPECTED_VALUE; |
363 | 0 | } |
364 | | |
365 | 0 | cur->Value = value; |
366 | 0 | cur->Flags |= COMMAND_LINE_VALUE_PRESENT; |
367 | 0 | } |
368 | 0 | else |
369 | 0 | { |
370 | 0 | if (cur->Flags & COMMAND_LINE_VALUE_FLAG) |
371 | 0 | { |
372 | 0 | cur->Value = (LPSTR)1; |
373 | 0 | cur->Flags |= COMMAND_LINE_VALUE_PRESENT; |
374 | 0 | } |
375 | 0 | else if (cur->Flags & COMMAND_LINE_VALUE_BOOL) |
376 | 0 | { |
377 | 0 | if (flags & COMMAND_LINE_SIGIL_ENABLE_DISABLE) |
378 | 0 | { |
379 | 0 | if (toggle == -1) |
380 | 0 | cur->Value = BoolValueTrue; |
381 | 0 | else if (!toggle) |
382 | 0 | cur->Value = BoolValueFalse; |
383 | 0 | else |
384 | 0 | cur->Value = BoolValueTrue; |
385 | 0 | } |
386 | 0 | else |
387 | 0 | { |
388 | 0 | if (sigil[0] == '+') |
389 | 0 | cur->Value = BoolValueTrue; |
390 | 0 | else if (sigil[0] == '-') |
391 | 0 | cur->Value = BoolValueFalse; |
392 | 0 | else |
393 | 0 | cur->Value = BoolValueTrue; |
394 | 0 | } |
395 | |
|
396 | 0 | cur->Flags |= COMMAND_LINE_VALUE_PRESENT; |
397 | 0 | } |
398 | 0 | } |
399 | | |
400 | 0 | if (postFilter) |
401 | 0 | { |
402 | 0 | count = postFilter(context, &options[j]); |
403 | |
|
404 | 0 | if (count < 0) |
405 | 0 | { |
406 | 0 | log_error(flags, |
407 | 0 | "Failed at index %d [%s]: PostFilter rule could not be applied", |
408 | 0 | i, argv[i]); |
409 | 0 | status = COMMAND_LINE_ERROR; |
410 | 0 | return status; |
411 | 0 | } |
412 | 0 | } |
413 | | |
414 | 0 | if (cur->Flags & COMMAND_LINE_PRINT) |
415 | 0 | return COMMAND_LINE_STATUS_PRINT; |
416 | 0 | else if (cur->Flags & COMMAND_LINE_PRINT_HELP) |
417 | 0 | return COMMAND_LINE_STATUS_PRINT_HELP; |
418 | 0 | else if (cur->Flags & COMMAND_LINE_PRINT_VERSION) |
419 | 0 | return COMMAND_LINE_STATUS_PRINT_VERSION; |
420 | 0 | else if (cur->Flags & COMMAND_LINE_PRINT_BUILDCONFIG) |
421 | 0 | return COMMAND_LINE_STATUS_PRINT_BUILDCONFIG; |
422 | 0 | } |
423 | | |
424 | 0 | if (!found && (flags & COMMAND_LINE_IGN_UNKNOWN_KEYWORD) == 0) |
425 | 0 | { |
426 | 0 | log_error(flags, "Failed at index %d [%s]: Unexpected keyword", i, argv[i]); |
427 | 0 | return COMMAND_LINE_ERROR_NO_KEYWORD; |
428 | 0 | } |
429 | 0 | } |
430 | 0 | } |
431 | | |
432 | 0 | return status; |
433 | 0 | } |
434 | | |
435 | | int CommandLineParseArgumentsW(WINPR_ATTR_UNUSED int argc, WINPR_ATTR_UNUSED LPWSTR* argv, |
436 | | WINPR_ATTR_UNUSED COMMAND_LINE_ARGUMENT_W* options, |
437 | | WINPR_ATTR_UNUSED DWORD flags, WINPR_ATTR_UNUSED void* context, |
438 | | WINPR_ATTR_UNUSED COMMAND_LINE_PRE_FILTER_FN_W preFilter, |
439 | | WINPR_ATTR_UNUSED COMMAND_LINE_POST_FILTER_FN_W postFilter) |
440 | 0 | { |
441 | 0 | WLog_ERR("TODO", "TODO: implement"); |
442 | 0 | return 0; |
443 | 0 | } |
444 | | |
445 | | int CommandLineClearArgumentsA(COMMAND_LINE_ARGUMENT_A* options) |
446 | 0 | { |
447 | 0 | for (size_t i = 0; options[i].Name != NULL; i++) |
448 | 0 | { |
449 | 0 | options[i].Flags &= COMMAND_LINE_INPUT_FLAG_MASK; |
450 | 0 | options[i].Value = NULL; |
451 | 0 | } |
452 | |
|
453 | 0 | return 0; |
454 | 0 | } |
455 | | |
456 | | int CommandLineClearArgumentsW(COMMAND_LINE_ARGUMENT_W* options) |
457 | 0 | { |
458 | 0 | for (int i = 0; options[i].Name != NULL; i++) |
459 | 0 | { |
460 | 0 | options[i].Flags &= COMMAND_LINE_INPUT_FLAG_MASK; |
461 | 0 | options[i].Value = NULL; |
462 | 0 | } |
463 | |
|
464 | 0 | return 0; |
465 | 0 | } |
466 | | |
467 | | const COMMAND_LINE_ARGUMENT_A* CommandLineFindArgumentA(const COMMAND_LINE_ARGUMENT_A* options, |
468 | | LPCSTR Name) |
469 | 0 | { |
470 | 0 | WINPR_ASSERT(options); |
471 | 0 | WINPR_ASSERT(Name); |
472 | | |
473 | 0 | for (size_t i = 0; options[i].Name != NULL; i++) |
474 | 0 | { |
475 | 0 | if (strcmp(options[i].Name, Name) == 0) |
476 | 0 | return &options[i]; |
477 | | |
478 | 0 | if (options[i].Alias != NULL) |
479 | 0 | { |
480 | 0 | if (strcmp(options[i].Alias, Name) == 0) |
481 | 0 | return &options[i]; |
482 | 0 | } |
483 | 0 | } |
484 | | |
485 | 0 | return NULL; |
486 | 0 | } |
487 | | |
488 | | const COMMAND_LINE_ARGUMENT_W* CommandLineFindArgumentW(const COMMAND_LINE_ARGUMENT_W* options, |
489 | | LPCWSTR Name) |
490 | 0 | { |
491 | 0 | WINPR_ASSERT(options); |
492 | 0 | WINPR_ASSERT(Name); |
493 | | |
494 | 0 | for (size_t i = 0; options[i].Name != NULL; i++) |
495 | 0 | { |
496 | 0 | if (_wcscmp(options[i].Name, Name) == 0) |
497 | 0 | return &options[i]; |
498 | | |
499 | 0 | if (options[i].Alias != NULL) |
500 | 0 | { |
501 | 0 | if (_wcscmp(options[i].Alias, Name) == 0) |
502 | 0 | return &options[i]; |
503 | 0 | } |
504 | 0 | } |
505 | | |
506 | 0 | return NULL; |
507 | 0 | } |
508 | | |
509 | | const COMMAND_LINE_ARGUMENT_A* CommandLineFindNextArgumentA(const COMMAND_LINE_ARGUMENT_A* argument) |
510 | 0 | { |
511 | 0 | const COMMAND_LINE_ARGUMENT_A* nextArgument = NULL; |
512 | |
|
513 | 0 | if (!argument || !argument->Name) |
514 | 0 | return NULL; |
515 | | |
516 | 0 | nextArgument = &argument[1]; |
517 | |
|
518 | 0 | if (nextArgument->Name == NULL) |
519 | 0 | return NULL; |
520 | | |
521 | 0 | return nextArgument; |
522 | 0 | } |
523 | | |
524 | | static int is_quoted(char c) |
525 | 0 | { |
526 | 0 | switch (c) |
527 | 0 | { |
528 | 0 | case '"': |
529 | 0 | return 1; |
530 | 0 | case '\'': |
531 | 0 | return -1; |
532 | 0 | default: |
533 | 0 | return 0; |
534 | 0 | } |
535 | 0 | } |
536 | | |
537 | | static size_t get_element_count(const char* list, BOOL* failed, BOOL fullquoted) |
538 | 0 | { |
539 | 0 | size_t count = 0; |
540 | 0 | int quoted = 0; |
541 | 0 | bool escaped = false; |
542 | 0 | BOOL finished = FALSE; |
543 | 0 | BOOL first = TRUE; |
544 | 0 | const char* it = list; |
545 | |
|
546 | 0 | if (!list) |
547 | 0 | return 0; |
548 | 0 | if (strlen(list) == 0) |
549 | 0 | return 0; |
550 | | |
551 | 0 | while (!finished) |
552 | 0 | { |
553 | 0 | BOOL nextFirst = FALSE; |
554 | |
|
555 | 0 | const char cur = *it++; |
556 | | |
557 | | /* Ignore the symbol that was escaped. */ |
558 | 0 | if (escaped) |
559 | 0 | { |
560 | 0 | escaped = false; |
561 | 0 | continue; |
562 | 0 | } |
563 | | |
564 | 0 | switch (cur) |
565 | 0 | { |
566 | 0 | case '\0': |
567 | 0 | if (quoted != 0) |
568 | 0 | { |
569 | 0 | log_comma_error("Invalid argument (missing closing quote)", list); |
570 | 0 | *failed = TRUE; |
571 | 0 | return 0; |
572 | 0 | } |
573 | 0 | finished = TRUE; |
574 | 0 | break; |
575 | 0 | case '\\': |
576 | 0 | if (!escaped) |
577 | 0 | { |
578 | 0 | escaped = true; |
579 | 0 | continue; |
580 | 0 | } |
581 | 0 | break; |
582 | 0 | case '\'': |
583 | 0 | case '"': |
584 | 0 | if (!fullquoted) |
585 | 0 | { |
586 | 0 | int now = is_quoted(cur) && !escaped; |
587 | 0 | if (now == quoted) |
588 | 0 | quoted = 0; |
589 | 0 | else if (quoted == 0) |
590 | 0 | quoted = now; |
591 | 0 | } |
592 | 0 | break; |
593 | 0 | case ',': |
594 | 0 | if (first) |
595 | 0 | { |
596 | 0 | log_comma_error("Invalid argument (empty list elements)", list); |
597 | 0 | *failed = TRUE; |
598 | 0 | return 0; |
599 | 0 | } |
600 | 0 | if (quoted == 0) |
601 | 0 | { |
602 | 0 | nextFirst = TRUE; |
603 | 0 | count++; |
604 | 0 | } |
605 | 0 | break; |
606 | 0 | default: |
607 | 0 | break; |
608 | 0 | } |
609 | | |
610 | 0 | first = nextFirst; |
611 | 0 | } |
612 | 0 | return count + 1; |
613 | 0 | } |
614 | | |
615 | | static char* get_next_comma(char* string, BOOL fullquoted) |
616 | 0 | { |
617 | 0 | const char* log = string; |
618 | 0 | int quoted = 0; |
619 | 0 | bool first = true; |
620 | 0 | bool escaped = false; |
621 | |
|
622 | 0 | WINPR_ASSERT(string); |
623 | | |
624 | 0 | while (TRUE) |
625 | 0 | { |
626 | 0 | char* last = string; |
627 | 0 | const char cur = *string++; |
628 | 0 | if (escaped) |
629 | 0 | { |
630 | 0 | escaped = false; |
631 | 0 | continue; |
632 | 0 | } |
633 | | |
634 | 0 | switch (cur) |
635 | 0 | { |
636 | 0 | case '\0': |
637 | 0 | if (quoted != 0) |
638 | 0 | log_comma_error("Invalid quoted argument", log); |
639 | 0 | return NULL; |
640 | | |
641 | 0 | case '\\': |
642 | 0 | if (!escaped) |
643 | 0 | { |
644 | 0 | escaped = true; |
645 | 0 | continue; |
646 | 0 | } |
647 | 0 | break; |
648 | 0 | case '\'': |
649 | 0 | case '"': |
650 | 0 | if (!fullquoted) |
651 | 0 | { |
652 | 0 | int now = is_quoted(cur); |
653 | 0 | if ((quoted == 0) && !first) |
654 | 0 | { |
655 | 0 | log_comma_error("Invalid quoted argument", log); |
656 | 0 | return NULL; |
657 | 0 | } |
658 | 0 | if (now == quoted) |
659 | 0 | quoted = 0; |
660 | 0 | else if (quoted == 0) |
661 | 0 | quoted = now; |
662 | 0 | } |
663 | 0 | break; |
664 | | |
665 | 0 | case ',': |
666 | 0 | if (first) |
667 | 0 | { |
668 | 0 | log_comma_error("Invalid argument (empty list elements)", log); |
669 | 0 | return NULL; |
670 | 0 | } |
671 | 0 | if (quoted == 0) |
672 | 0 | return last; |
673 | 0 | break; |
674 | | |
675 | 0 | default: |
676 | 0 | break; |
677 | 0 | } |
678 | 0 | first = FALSE; |
679 | 0 | } |
680 | | |
681 | 0 | return NULL; |
682 | 0 | } |
683 | | |
684 | | static BOOL is_valid_fullquoted(const char* string) |
685 | 0 | { |
686 | 0 | char cur = '\0'; |
687 | 0 | char last = '\0'; |
688 | 0 | const char quote = *string++; |
689 | | |
690 | | /* We did not start with a quote. */ |
691 | 0 | if (is_quoted(quote) == 0) |
692 | 0 | return FALSE; |
693 | | |
694 | 0 | while ((cur = *string++) != '\0') |
695 | 0 | { |
696 | | /* A quote is found. */ |
697 | 0 | if (cur == quote) |
698 | 0 | { |
699 | | /* If the quote was escaped, it is valid. */ |
700 | 0 | if (last != '\\') |
701 | 0 | { |
702 | | /* Only allow unescaped quote as last character in string. */ |
703 | 0 | if (*string != '\0') |
704 | 0 | return FALSE; |
705 | 0 | } |
706 | | /* If the last quote in the string is escaped, it is wrong. */ |
707 | 0 | else if (*string != '\0') |
708 | 0 | return FALSE; |
709 | 0 | } |
710 | 0 | last = cur; |
711 | 0 | } |
712 | | |
713 | | /* The string did not terminate with the same quote as it started. */ |
714 | 0 | if (last != quote) |
715 | 0 | return FALSE; |
716 | 0 | return TRUE; |
717 | 0 | } |
718 | | |
719 | | char** CommandLineParseCommaSeparatedValuesEx(const char* name, const char* list, size_t* count) |
720 | 0 | { |
721 | 0 | char** p = NULL; |
722 | 0 | char* str = NULL; |
723 | 0 | size_t nArgs = 0; |
724 | 0 | size_t prefix = 0; |
725 | 0 | size_t len = 0; |
726 | 0 | size_t namelen = 0; |
727 | 0 | BOOL failed = FALSE; |
728 | 0 | char* copy = NULL; |
729 | 0 | char* unquoted = NULL; |
730 | 0 | BOOL fullquoted = FALSE; |
731 | |
|
732 | 0 | BOOL success = FALSE; |
733 | 0 | if (count == NULL) |
734 | 0 | goto fail; |
735 | | |
736 | 0 | *count = 0; |
737 | 0 | if (list) |
738 | 0 | { |
739 | 0 | int start = 0; |
740 | 0 | int end = 0; |
741 | 0 | unquoted = copy = _strdup(list); |
742 | 0 | if (!copy) |
743 | 0 | goto fail; |
744 | | |
745 | 0 | len = strlen(unquoted); |
746 | 0 | if (len > 0) |
747 | 0 | { |
748 | 0 | start = is_quoted(unquoted[0]); |
749 | 0 | end = is_quoted(unquoted[len - 1]); |
750 | |
|
751 | 0 | if ((start != 0) && (end != 0)) |
752 | 0 | { |
753 | 0 | if (start != end) |
754 | 0 | { |
755 | 0 | log_comma_error("Invalid argument (quote mismatch)", list); |
756 | 0 | goto fail; |
757 | 0 | } |
758 | 0 | if (!is_valid_fullquoted(unquoted)) |
759 | 0 | goto fail; |
760 | 0 | unquoted[len - 1] = '\0'; |
761 | 0 | unquoted++; |
762 | 0 | len -= 2; |
763 | 0 | fullquoted = TRUE; |
764 | 0 | } |
765 | 0 | } |
766 | 0 | } |
767 | | |
768 | 0 | *count = get_element_count(unquoted, &failed, fullquoted); |
769 | 0 | if (failed) |
770 | 0 | goto fail; |
771 | | |
772 | 0 | if (*count == 0) |
773 | 0 | { |
774 | 0 | if (!name) |
775 | 0 | goto fail; |
776 | 0 | else |
777 | 0 | { |
778 | 0 | size_t clen = strlen(name); |
779 | 0 | p = (char**)calloc(2UL + clen, sizeof(char*)); |
780 | |
|
781 | 0 | if (p) |
782 | 0 | { |
783 | 0 | char* dst = (char*)&p[1]; |
784 | 0 | p[0] = dst; |
785 | 0 | (void)sprintf_s(dst, clen + 1, "%s", name); |
786 | 0 | *count = 1; |
787 | 0 | success = TRUE; |
788 | 0 | goto fail; |
789 | 0 | } |
790 | 0 | } |
791 | 0 | } |
792 | | |
793 | 0 | nArgs = *count; |
794 | |
|
795 | 0 | if (name) |
796 | 0 | nArgs++; |
797 | |
|
798 | 0 | prefix = (nArgs + 1UL) * sizeof(char*); |
799 | 0 | if (name) |
800 | 0 | namelen = strlen(name); |
801 | 0 | p = (char**)calloc(len + prefix + 1 + namelen + 1, sizeof(char*)); |
802 | |
|
803 | 0 | if (!p) |
804 | 0 | goto fail; |
805 | | |
806 | 0 | str = &((char*)p)[prefix]; |
807 | 0 | memcpy(str, unquoted, len); |
808 | |
|
809 | 0 | if (name) |
810 | 0 | { |
811 | 0 | char* namestr = &((char*)p)[prefix + len + 1]; |
812 | 0 | memcpy(namestr, name, namelen); |
813 | |
|
814 | 0 | p[0] = namestr; |
815 | 0 | } |
816 | |
|
817 | 0 | for (size_t index = name ? 1 : 0; index < nArgs; index++) |
818 | 0 | { |
819 | 0 | char* ptr = str; |
820 | 0 | const int quote = is_quoted(*ptr); |
821 | 0 | char* comma = get_next_comma(str, fullquoted); |
822 | |
|
823 | 0 | if ((quote != 0) && !fullquoted) |
824 | 0 | ptr++; |
825 | |
|
826 | 0 | p[index] = ptr; |
827 | |
|
828 | 0 | if (comma) |
829 | 0 | { |
830 | 0 | char* last = comma - 1; |
831 | 0 | const int lastQuote = is_quoted(*last); |
832 | |
|
833 | 0 | if (!fullquoted) |
834 | 0 | { |
835 | 0 | if (lastQuote != quote) |
836 | 0 | { |
837 | 0 | log_comma_error("Invalid argument (quote mismatch)", list); |
838 | 0 | goto fail; |
839 | 0 | } |
840 | 0 | else if (lastQuote != 0) |
841 | 0 | *last = '\0'; |
842 | 0 | } |
843 | 0 | *comma = '\0'; |
844 | |
|
845 | 0 | str = comma + 1; |
846 | 0 | } |
847 | 0 | else if (quote) |
848 | 0 | { |
849 | 0 | char* end = strrchr(ptr, '"'); |
850 | 0 | if (!end) |
851 | 0 | goto fail; |
852 | 0 | *end = '\0'; |
853 | 0 | } |
854 | 0 | } |
855 | | |
856 | 0 | *count = nArgs; |
857 | 0 | success = TRUE; |
858 | 0 | fail: |
859 | 0 | free(copy); |
860 | 0 | if (!success) |
861 | 0 | { |
862 | 0 | if (count) |
863 | 0 | *count = 0; |
864 | 0 | free((void*)p); |
865 | 0 | return NULL; |
866 | 0 | } |
867 | 0 | return p; |
868 | 0 | } |
869 | | |
870 | | char** CommandLineParseCommaSeparatedValues(const char* list, size_t* count) |
871 | 0 | { |
872 | 0 | return CommandLineParseCommaSeparatedValuesEx(NULL, list, count); |
873 | 0 | } |
874 | | |
875 | | char* CommandLineToCommaSeparatedValues(int argc, char* argv[]) |
876 | 0 | { |
877 | 0 | return CommandLineToCommaSeparatedValuesEx(argc, argv, NULL, 0); |
878 | 0 | } |
879 | | |
880 | | static const char* filtered(const char* arg, const char* filters[], size_t number) |
881 | 0 | { |
882 | 0 | if (number == 0) |
883 | 0 | return arg; |
884 | 0 | for (size_t x = 0; x < number; x++) |
885 | 0 | { |
886 | 0 | const char* filter = filters[x]; |
887 | 0 | size_t len = strlen(filter); |
888 | 0 | if (_strnicmp(arg, filter, len) == 0) |
889 | 0 | return &arg[len]; |
890 | 0 | } |
891 | 0 | return NULL; |
892 | 0 | } |
893 | | |
894 | | char* CommandLineToCommaSeparatedValuesEx(int argc, char* argv[], const char* filters[], |
895 | | size_t number) |
896 | 0 | { |
897 | 0 | char* str = NULL; |
898 | 0 | size_t offset = 0; |
899 | 0 | size_t size = WINPR_ASSERTING_INT_CAST(size_t, argc) + 1; |
900 | 0 | if ((argc <= 0) || !argv) |
901 | 0 | return NULL; |
902 | | |
903 | 0 | for (int x = 0; x < argc; x++) |
904 | 0 | size += strlen(argv[x]); |
905 | |
|
906 | 0 | str = calloc(size, sizeof(char)); |
907 | 0 | if (!str) |
908 | 0 | return NULL; |
909 | 0 | for (int x = 0; x < argc; x++) |
910 | 0 | { |
911 | 0 | int rc = 0; |
912 | 0 | const char* arg = filtered(argv[x], filters, number); |
913 | 0 | if (!arg) |
914 | 0 | continue; |
915 | 0 | rc = _snprintf(&str[offset], size - offset, "%s,", arg); |
916 | 0 | if (rc <= 0) |
917 | 0 | { |
918 | 0 | free(str); |
919 | 0 | return NULL; |
920 | 0 | } |
921 | 0 | offset += (size_t)rc; |
922 | 0 | } |
923 | 0 | if (offset > 0) |
924 | 0 | str[offset - 1] = '\0'; |
925 | 0 | return str; |
926 | 0 | } |
927 | | |
928 | | void CommandLineParserFree(char** ptr) |
929 | 0 | { |
930 | 0 | union |
931 | 0 | { |
932 | 0 | char* p; |
933 | 0 | char** pp; |
934 | 0 | } uptr; |
935 | 0 | uptr.pp = ptr; |
936 | 0 | free(uptr.p); |
937 | 0 | } |