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