2012-11-02 08:20:46 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Command-Line Utils
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2021-09-07 09:31:07 +03:00
|
|
|
#include <winpr/assert.h>
|
2012-11-02 08:20:46 +04:00
|
|
|
#include <winpr/cmdline.h>
|
|
|
|
|
2018-12-11 11:27:32 +03:00
|
|
|
#include "../log.h"
|
|
|
|
|
|
|
|
#define TAG WINPR_TAG("commandline")
|
|
|
|
|
2012-11-02 08:20:46 +04:00
|
|
|
/**
|
|
|
|
* Command-line syntax: some basic concepts:
|
|
|
|
* https://pythonconquerstheuniverse.wordpress.com/2010/07/25/command-line-syntax-some-basic-concepts/
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Command-Line Syntax:
|
|
|
|
*
|
|
|
|
* <sigil><keyword><separator><value>
|
|
|
|
*
|
2012-11-02 22:16:37 +04:00
|
|
|
* <sigil>: '/' or '-' or ('+' | '-')
|
2012-11-02 08:20:46 +04:00
|
|
|
*
|
|
|
|
* <keyword>: option, named argument, flag
|
|
|
|
*
|
|
|
|
* <separator>: ':' or '='
|
|
|
|
*
|
|
|
|
* <value>: argument value
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2018-12-11 12:25:44 +03:00
|
|
|
static void log_error(DWORD flags, LPCSTR message, int index, LPCSTR argv)
|
|
|
|
{
|
|
|
|
if ((flags & COMMAND_LINE_SILENCE_PARSER) == 0)
|
|
|
|
WLog_ERR(TAG, message, index, argv);
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
int CommandLineParseArgumentsA(int argc, LPSTR* argv, COMMAND_LINE_ARGUMENT_A* options, DWORD flags,
|
|
|
|
void* context, COMMAND_LINE_PRE_FILTER_FN_A preFilter,
|
|
|
|
COMMAND_LINE_POST_FILTER_FN_A postFilter)
|
2012-11-02 08:20:46 +04:00
|
|
|
{
|
|
|
|
int i, j;
|
2013-06-15 23:13:38 +04:00
|
|
|
int status;
|
2012-12-05 23:00:49 +04:00
|
|
|
int count;
|
2017-11-14 15:54:05 +03:00
|
|
|
size_t length;
|
2013-09-18 01:03:35 +04:00
|
|
|
BOOL notescaped;
|
2017-11-30 18:56:45 +03:00
|
|
|
const char* sigil;
|
2017-11-14 15:54:05 +03:00
|
|
|
size_t sigil_length;
|
2018-06-06 17:43:09 +03:00
|
|
|
char* keyword;
|
2019-01-30 11:38:19 +03:00
|
|
|
size_t keyword_length;
|
2017-11-14 15:54:05 +03:00
|
|
|
SSIZE_T keyword_index;
|
2012-11-02 08:20:46 +04:00
|
|
|
char* separator;
|
2018-06-06 17:43:09 +03:00
|
|
|
char* value;
|
2012-12-02 22:34:00 +04:00
|
|
|
int toggle;
|
2013-06-15 23:13:38 +04:00
|
|
|
status = 0;
|
2013-09-18 01:03:35 +04:00
|
|
|
notescaped = FALSE;
|
|
|
|
|
2012-11-19 22:26:56 +04:00
|
|
|
if (!argv)
|
2013-06-15 23:13:38 +04:00
|
|
|
return status;
|
2012-11-19 22:26:56 +04:00
|
|
|
|
2012-11-22 18:36:09 +04:00
|
|
|
if (argc == 1)
|
2013-06-15 23:13:38 +04:00
|
|
|
{
|
2017-06-12 13:23:10 +03:00
|
|
|
if (flags & COMMAND_LINE_IGN_UNKNOWN_KEYWORD)
|
|
|
|
status = 0;
|
|
|
|
else
|
|
|
|
status = COMMAND_LINE_STATUS_PRINT_HELP;
|
2017-11-30 18:56:45 +03:00
|
|
|
|
2013-06-15 23:13:38 +04:00
|
|
|
return status;
|
|
|
|
}
|
2012-11-22 18:36:09 +04:00
|
|
|
|
2012-11-02 08:20:46 +04:00
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
2017-11-14 15:54:05 +03:00
|
|
|
BOOL found = FALSE;
|
|
|
|
BOOL escaped = TRUE;
|
2013-09-18 01:03:35 +04:00
|
|
|
|
2012-11-07 08:07:03 +04:00
|
|
|
if (preFilter)
|
2012-12-05 21:12:18 +04:00
|
|
|
{
|
2012-12-05 23:00:49 +04:00
|
|
|
count = preFilter(context, i, argc, argv);
|
2013-09-18 01:03:35 +04:00
|
|
|
|
2012-12-05 23:00:49 +04:00
|
|
|
if (count < 0)
|
2013-06-15 23:13:38 +04:00
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
log_error(flags, "Failed for index %d [%s]: PreFilter rule could not be applied", i,
|
|
|
|
argv[i]);
|
2013-06-15 23:13:38 +04:00
|
|
|
status = COMMAND_LINE_ERROR;
|
|
|
|
return status;
|
|
|
|
}
|
2012-12-05 23:00:49 +04:00
|
|
|
|
|
|
|
if (count > 0)
|
|
|
|
{
|
2013-09-18 01:03:35 +04:00
|
|
|
i += (count - 1);
|
2012-12-05 23:00:49 +04:00
|
|
|
continue;
|
|
|
|
}
|
2012-12-05 21:12:18 +04:00
|
|
|
}
|
2012-11-07 07:08:09 +04:00
|
|
|
|
2017-11-30 18:56:45 +03:00
|
|
|
sigil = argv[i];
|
2017-11-14 15:54:05 +03:00
|
|
|
length = strlen(argv[i]);
|
2012-11-02 08:20:46 +04:00
|
|
|
|
2012-11-05 01:09:43 +04:00
|
|
|
if ((sigil[0] == '/') && (flags & COMMAND_LINE_SIGIL_SLASH))
|
2012-11-02 08:20:46 +04:00
|
|
|
{
|
|
|
|
sigil_length = 1;
|
2012-11-02 08:58:32 +04:00
|
|
|
}
|
2012-11-05 01:09:43 +04:00
|
|
|
else if ((sigil[0] == '-') && (flags & COMMAND_LINE_SIGIL_DASH))
|
|
|
|
{
|
|
|
|
sigil_length = 1;
|
2012-12-02 22:34:00 +04:00
|
|
|
|
|
|
|
if (length > 2)
|
|
|
|
{
|
|
|
|
if ((sigil[1] == '-') && (flags & COMMAND_LINE_SIGIL_DOUBLE_DASH))
|
|
|
|
sigil_length = 2;
|
|
|
|
}
|
2012-11-05 01:09:43 +04:00
|
|
|
}
|
|
|
|
else if ((sigil[0] == '+') && (flags & COMMAND_LINE_SIGIL_PLUS_MINUS))
|
|
|
|
{
|
|
|
|
sigil_length = 1;
|
|
|
|
}
|
|
|
|
else if ((sigil[0] == '-') && (flags & COMMAND_LINE_SIGIL_PLUS_MINUS))
|
2012-11-02 08:58:32 +04:00
|
|
|
{
|
|
|
|
sigil_length = 1;
|
|
|
|
}
|
2012-11-19 02:32:18 +04:00
|
|
|
else if (flags & COMMAND_LINE_SIGIL_NONE)
|
|
|
|
{
|
|
|
|
sigil_length = 0;
|
|
|
|
}
|
2013-09-10 12:29:45 +04:00
|
|
|
else if (flags & COMMAND_LINE_SIGIL_NOT_ESCAPED)
|
|
|
|
{
|
|
|
|
if (notescaped)
|
2018-12-11 11:27:32 +03:00
|
|
|
{
|
2018-12-11 12:25:44 +03:00
|
|
|
log_error(flags, "Failed at index %d [%s]: Unescaped sigil", i, argv[i]);
|
2017-05-04 15:46:58 +03:00
|
|
|
return COMMAND_LINE_ERROR;
|
2018-12-11 11:27:32 +03:00
|
|
|
}
|
2013-09-18 01:03:35 +04:00
|
|
|
|
2013-09-10 12:29:45 +04:00
|
|
|
sigil_length = 0;
|
|
|
|
escaped = FALSE;
|
|
|
|
notescaped = TRUE;
|
|
|
|
}
|
2012-11-02 22:16:37 +04:00
|
|
|
else
|
|
|
|
{
|
2018-12-11 12:25:44 +03:00
|
|
|
log_error(flags, "Failed at index %d [%s]: Invalid sigil", i, argv[i]);
|
2013-09-09 18:07:14 +04:00
|
|
|
return COMMAND_LINE_ERROR;
|
2012-11-02 22:16:37 +04:00
|
|
|
}
|
2013-09-18 01:03:35 +04:00
|
|
|
|
2013-09-10 12:29:45 +04:00
|
|
|
if ((sigil_length > 0) || (flags & COMMAND_LINE_SIGIL_NONE) ||
|
2017-05-04 15:46:58 +03:00
|
|
|
(flags & COMMAND_LINE_SIGIL_NOT_ESCAPED))
|
2012-11-02 08:58:32 +04:00
|
|
|
{
|
2017-05-04 15:46:58 +03:00
|
|
|
if (length < (sigil_length + 1))
|
|
|
|
{
|
2015-03-13 01:28:44 +03:00
|
|
|
if ((flags & COMMAND_LINE_IGN_UNKNOWN_KEYWORD))
|
|
|
|
continue;
|
2017-11-30 18:56:45 +03:00
|
|
|
|
2012-11-02 22:16:37 +04:00
|
|
|
return COMMAND_LINE_ERROR_NO_KEYWORD;
|
2015-03-13 01:28:44 +03:00
|
|
|
}
|
2012-11-02 08:20:46 +04:00
|
|
|
|
2017-11-14 15:54:05 +03:00
|
|
|
keyword_index = sigil_length;
|
2017-11-30 18:56:45 +03:00
|
|
|
keyword = &argv[i][keyword_index];
|
2012-12-02 22:34:00 +04:00
|
|
|
toggle = -1;
|
|
|
|
|
|
|
|
if (flags & COMMAND_LINE_SIGIL_ENABLE_DISABLE)
|
|
|
|
{
|
|
|
|
if (strncmp(keyword, "enable-", 7) == 0)
|
|
|
|
{
|
|
|
|
toggle = TRUE;
|
|
|
|
keyword_index += 7;
|
2017-11-30 18:56:45 +03:00
|
|
|
keyword = &argv[i][keyword_index];
|
2012-12-02 22:34:00 +04:00
|
|
|
}
|
|
|
|
else if (strncmp(keyword, "disable-", 8) == 0)
|
|
|
|
{
|
|
|
|
toggle = FALSE;
|
|
|
|
keyword_index += 8;
|
2017-11-30 18:56:45 +03:00
|
|
|
keyword = &argv[i][keyword_index];
|
2012-12-02 22:34:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 08:58:32 +04:00
|
|
|
separator = NULL;
|
|
|
|
|
|
|
|
if ((flags & COMMAND_LINE_SEPARATOR_COLON) && (!separator))
|
|
|
|
separator = strchr(keyword, ':');
|
|
|
|
|
|
|
|
if ((flags & COMMAND_LINE_SEPARATOR_EQUAL) && (!separator))
|
|
|
|
separator = strchr(keyword, '=');
|
2012-11-02 08:20:46 +04:00
|
|
|
|
|
|
|
if (separator)
|
|
|
|
{
|
2017-11-14 15:54:05 +03:00
|
|
|
SSIZE_T separator_index = (separator - argv[i]);
|
|
|
|
SSIZE_T value_index = separator_index + 1;
|
|
|
|
keyword_length = (separator - keyword);
|
2017-11-30 18:56:45 +03:00
|
|
|
value = &argv[i][value_index];
|
2012-11-02 08:20:46 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
keyword_length = (length - keyword_index);
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
|
2013-09-10 12:29:45 +04:00
|
|
|
if (!escaped)
|
|
|
|
continue;
|
|
|
|
|
2012-11-02 08:20:46 +04:00
|
|
|
for (j = 0; options[j].Name != NULL; j++)
|
|
|
|
{
|
2017-11-14 15:54:05 +03:00
|
|
|
BOOL match = FALSE;
|
2012-11-02 22:16:37 +04:00
|
|
|
|
2012-11-02 08:20:46 +04:00
|
|
|
if (strncmp(options[j].Name, keyword, keyword_length) == 0)
|
|
|
|
{
|
|
|
|
if (strlen(options[j].Name) == keyword_length)
|
2012-11-02 22:16:37 +04:00
|
|
|
match = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((!match) && (options[j].Alias != NULL))
|
|
|
|
{
|
|
|
|
if (strncmp(options[j].Alias, keyword, keyword_length) == 0)
|
2012-11-02 08:20:46 +04:00
|
|
|
{
|
2012-11-02 22:16:37 +04:00
|
|
|
if (strlen(options[j].Alias) == keyword_length)
|
|
|
|
match = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match)
|
|
|
|
continue;
|
|
|
|
|
2013-09-09 18:07:14 +04:00
|
|
|
found = match;
|
2017-11-14 15:54:05 +03:00
|
|
|
options[j].Index = i;
|
2012-11-27 07:42:40 +04:00
|
|
|
|
|
|
|
if ((flags & COMMAND_LINE_SEPARATOR_SPACE) && ((i + 1) < argc))
|
|
|
|
{
|
2017-11-14 15:54:05 +03:00
|
|
|
BOOL argument;
|
2013-02-20 03:05:20 +04:00
|
|
|
int value_present = 1;
|
|
|
|
|
|
|
|
if (flags & COMMAND_LINE_SIGIL_DASH)
|
|
|
|
{
|
|
|
|
if (strncmp(argv[i + 1], "-", 1) == 0)
|
|
|
|
value_present = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & COMMAND_LINE_SIGIL_DOUBLE_DASH)
|
|
|
|
{
|
|
|
|
if (strncmp(argv[i + 1], "--", 2) == 0)
|
|
|
|
value_present = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & COMMAND_LINE_SIGIL_SLASH)
|
|
|
|
{
|
|
|
|
if (strncmp(argv[i + 1], "/", 1) == 0)
|
|
|
|
value_present = 0;
|
|
|
|
}
|
|
|
|
|
2013-09-09 19:21:15 +04:00
|
|
|
if ((options[j].Flags & COMMAND_LINE_VALUE_REQUIRED) ||
|
2017-05-04 15:46:58 +03:00
|
|
|
(options[j].Flags & COMMAND_LINE_VALUE_OPTIONAL))
|
2013-09-09 18:07:14 +04:00
|
|
|
argument = TRUE;
|
|
|
|
else
|
|
|
|
argument = FALSE;
|
2017-11-30 18:56:45 +03:00
|
|
|
|
2013-09-09 18:07:14 +04:00
|
|
|
if (value_present && argument)
|
2012-12-02 22:34:00 +04:00
|
|
|
{
|
|
|
|
i++;
|
2017-11-30 18:56:45 +03:00
|
|
|
value = argv[i];
|
2012-12-02 22:34:00 +04:00
|
|
|
}
|
2013-09-09 19:21:15 +04:00
|
|
|
else if (!value_present && (options[j].Flags & COMMAND_LINE_VALUE_OPTIONAL))
|
|
|
|
{
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
else if (!value_present && argument)
|
2018-12-11 11:27:32 +03:00
|
|
|
{
|
2018-12-11 12:25:44 +03:00
|
|
|
log_error(flags, "Failed at index %d [%s]: Argument required", i, argv[i]);
|
2013-09-09 18:07:14 +04:00
|
|
|
return COMMAND_LINE_ERROR;
|
2018-12-11 11:27:32 +03:00
|
|
|
}
|
2012-11-27 07:42:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & COMMAND_LINE_SEPARATOR_SPACE))
|
|
|
|
{
|
|
|
|
if (value && (options[j].Flags & COMMAND_LINE_VALUE_FLAG))
|
2018-12-11 11:27:32 +03:00
|
|
|
{
|
2018-12-11 12:25:44 +03:00
|
|
|
log_error(flags, "Failed at index %d [%s]: Unexpected value", i, argv[i]);
|
2012-11-27 07:42:40 +04:00
|
|
|
return COMMAND_LINE_ERROR_UNEXPECTED_VALUE;
|
2018-12-11 11:27:32 +03:00
|
|
|
}
|
2012-11-27 07:42:40 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value && (options[j].Flags & COMMAND_LINE_VALUE_FLAG))
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 22:16:37 +04:00
|
|
|
|
|
|
|
if (!value && (options[j].Flags & COMMAND_LINE_VALUE_REQUIRED))
|
2013-06-15 23:13:38 +04:00
|
|
|
{
|
2018-12-11 12:25:44 +03:00
|
|
|
log_error(flags, "Failed at index %d [%s]: Missing value", i, argv[i]);
|
2013-06-15 23:13:38 +04:00
|
|
|
status = COMMAND_LINE_ERROR_MISSING_VALUE;
|
|
|
|
return status;
|
|
|
|
}
|
2012-11-02 22:16:37 +04:00
|
|
|
|
2012-11-27 11:49:44 +04:00
|
|
|
options[j].Flags |= COMMAND_LINE_ARGUMENT_PRESENT;
|
|
|
|
|
2012-11-02 22:16:37 +04:00
|
|
|
if (value)
|
|
|
|
{
|
2020-03-19 00:00:06 +03:00
|
|
|
if (!(options[j].Flags &
|
|
|
|
(COMMAND_LINE_VALUE_OPTIONAL | COMMAND_LINE_VALUE_REQUIRED)))
|
2018-12-11 11:27:32 +03:00
|
|
|
{
|
2018-12-11 12:25:44 +03:00
|
|
|
log_error(flags, "Failed at index %d [%s]: Unexpected value", i, argv[i]);
|
2018-09-28 13:59:20 +03:00
|
|
|
return COMMAND_LINE_ERROR_UNEXPECTED_VALUE;
|
2018-12-11 11:27:32 +03:00
|
|
|
}
|
2018-09-28 13:59:20 +03:00
|
|
|
|
2012-11-02 22:16:37 +04:00
|
|
|
options[j].Value = value;
|
|
|
|
options[j].Flags |= COMMAND_LINE_VALUE_PRESENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (options[j].Flags & COMMAND_LINE_VALUE_FLAG)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
options[j].Value = (LPSTR)1;
|
2012-11-02 22:16:37 +04:00
|
|
|
options[j].Flags |= COMMAND_LINE_VALUE_PRESENT;
|
2012-11-02 08:20:46 +04:00
|
|
|
}
|
2012-11-05 01:09:43 +04:00
|
|
|
else if (options[j].Flags & COMMAND_LINE_VALUE_BOOL)
|
|
|
|
{
|
2012-12-02 22:34:00 +04:00
|
|
|
if (flags & COMMAND_LINE_SIGIL_ENABLE_DISABLE)
|
|
|
|
{
|
|
|
|
if (toggle == -1)
|
|
|
|
options[j].Value = BoolValueTrue;
|
|
|
|
else if (!toggle)
|
|
|
|
options[j].Value = BoolValueFalse;
|
|
|
|
else
|
|
|
|
options[j].Value = BoolValueTrue;
|
|
|
|
}
|
2012-11-05 01:09:43 +04:00
|
|
|
else
|
2012-12-02 22:34:00 +04:00
|
|
|
{
|
|
|
|
if (sigil[0] == '+')
|
|
|
|
options[j].Value = BoolValueTrue;
|
|
|
|
else if (sigil[0] == '-')
|
|
|
|
options[j].Value = BoolValueFalse;
|
|
|
|
else
|
|
|
|
options[j].Value = BoolValueTrue;
|
|
|
|
}
|
2012-11-05 01:09:43 +04:00
|
|
|
|
|
|
|
options[j].Flags |= COMMAND_LINE_VALUE_PRESENT;
|
|
|
|
}
|
2012-11-02 08:20:46 +04:00
|
|
|
}
|
2012-11-07 03:49:02 +04:00
|
|
|
|
2012-11-07 08:07:03 +04:00
|
|
|
if (postFilter)
|
2017-11-30 18:56:45 +03:00
|
|
|
{
|
|
|
|
count = postFilter(context, &options[j]);
|
|
|
|
|
|
|
|
if (count < 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
log_error(flags,
|
|
|
|
"Failed at index %d [%s]: PostFilter rule could not be applied",
|
|
|
|
i, argv[i]);
|
2017-11-30 18:56:45 +03:00
|
|
|
status = COMMAND_LINE_ERROR;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
}
|
2012-11-07 08:07:03 +04:00
|
|
|
|
2012-11-07 03:49:02 +04:00
|
|
|
if (options[j].Flags & COMMAND_LINE_PRINT)
|
2017-05-04 15:46:58 +03:00
|
|
|
return COMMAND_LINE_STATUS_PRINT;
|
2012-11-07 03:49:02 +04:00
|
|
|
else if (options[j].Flags & COMMAND_LINE_PRINT_HELP)
|
2017-05-04 15:46:58 +03:00
|
|
|
return COMMAND_LINE_STATUS_PRINT_HELP;
|
2012-11-07 03:49:02 +04:00
|
|
|
else if (options[j].Flags & COMMAND_LINE_PRINT_VERSION)
|
2017-05-04 15:46:58 +03:00
|
|
|
return COMMAND_LINE_STATUS_PRINT_VERSION;
|
2016-01-11 18:18:40 +03:00
|
|
|
else if (options[j].Flags & COMMAND_LINE_PRINT_BUILDCONFIG)
|
2017-05-04 15:46:58 +03:00
|
|
|
return COMMAND_LINE_STATUS_PRINT_BUILDCONFIG;
|
2012-11-02 08:20:46 +04:00
|
|
|
}
|
2017-11-30 18:56:45 +03:00
|
|
|
|
2015-03-13 01:28:44 +03:00
|
|
|
if (!found && (flags & COMMAND_LINE_IGN_UNKNOWN_KEYWORD) == 0)
|
2018-12-11 11:27:32 +03:00
|
|
|
{
|
2018-12-11 12:25:44 +03:00
|
|
|
log_error(flags, "Failed at index %d [%s]: Unexpected keyword", i, argv[i]);
|
2013-09-09 18:07:14 +04:00
|
|
|
return COMMAND_LINE_ERROR_NO_KEYWORD;
|
2018-12-11 11:27:32 +03:00
|
|
|
}
|
2012-11-02 08:20:46 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 23:13:38 +04:00
|
|
|
return status;
|
2012-11-02 08:20:46 +04:00
|
|
|
}
|
|
|
|
|
2018-06-06 17:43:09 +03:00
|
|
|
int CommandLineParseArgumentsW(int argc, LPWSTR* argv, COMMAND_LINE_ARGUMENT_W* options,
|
2019-11-06 17:24:51 +03:00
|
|
|
DWORD flags, void* context, COMMAND_LINE_PRE_FILTER_FN_W preFilter,
|
|
|
|
COMMAND_LINE_POST_FILTER_FN_W postFilter)
|
2012-11-02 08:20:46 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2012-11-02 08:58:32 +04:00
|
|
|
|
|
|
|
int CommandLineClearArgumentsA(COMMAND_LINE_ARGUMENT_A* options)
|
|
|
|
{
|
2017-11-30 18:56:45 +03:00
|
|
|
size_t i;
|
2012-11-02 08:58:32 +04:00
|
|
|
|
|
|
|
for (i = 0; options[i].Name != NULL; i++)
|
|
|
|
{
|
|
|
|
options[i].Flags &= COMMAND_LINE_INPUT_FLAG_MASK;
|
|
|
|
options[i].Value = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CommandLineClearArgumentsW(COMMAND_LINE_ARGUMENT_W* options)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; options[i].Name != NULL; i++)
|
|
|
|
{
|
|
|
|
options[i].Flags &= COMMAND_LINE_INPUT_FLAG_MASK;
|
|
|
|
options[i].Value = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:31:07 +03:00
|
|
|
const COMMAND_LINE_ARGUMENT_A* CommandLineFindArgumentA(const COMMAND_LINE_ARGUMENT_A* options,
|
|
|
|
LPCSTR Name)
|
2012-11-02 08:58:32 +04:00
|
|
|
{
|
2021-09-07 09:31:07 +03:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
WINPR_ASSERT(options);
|
|
|
|
WINPR_ASSERT(Name);
|
2012-11-02 08:58:32 +04:00
|
|
|
|
|
|
|
for (i = 0; options[i].Name != NULL; i++)
|
|
|
|
{
|
|
|
|
if (strcmp(options[i].Name, Name) == 0)
|
|
|
|
return &options[i];
|
2012-11-02 22:16:37 +04:00
|
|
|
|
|
|
|
if (options[i].Alias != NULL)
|
|
|
|
{
|
|
|
|
if (strcmp(options[i].Alias, Name) == 0)
|
|
|
|
return &options[i];
|
|
|
|
}
|
2012-11-02 08:58:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:31:07 +03:00
|
|
|
const COMMAND_LINE_ARGUMENT_W* CommandLineFindArgumentW(const COMMAND_LINE_ARGUMENT_W* options,
|
|
|
|
LPCWSTR Name)
|
2012-11-02 08:58:32 +04:00
|
|
|
{
|
2021-09-07 09:31:07 +03:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
WINPR_ASSERT(options);
|
|
|
|
WINPR_ASSERT(Name);
|
2012-11-02 08:58:32 +04:00
|
|
|
|
|
|
|
for (i = 0; options[i].Name != NULL; i++)
|
|
|
|
{
|
|
|
|
if (_wcscmp(options[i].Name, Name) == 0)
|
|
|
|
return &options[i];
|
2012-11-02 22:16:37 +04:00
|
|
|
|
|
|
|
if (options[i].Alias != NULL)
|
|
|
|
{
|
|
|
|
if (_wcscmp(options[i].Alias, Name) == 0)
|
|
|
|
return &options[i];
|
|
|
|
}
|
2012-11-02 08:58:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
2012-11-05 01:09:43 +04:00
|
|
|
|
2021-09-07 09:31:07 +03:00
|
|
|
const COMMAND_LINE_ARGUMENT_A* CommandLineFindNextArgumentA(const COMMAND_LINE_ARGUMENT_A* argument)
|
2012-11-05 01:09:43 +04:00
|
|
|
{
|
2021-09-07 09:31:07 +03:00
|
|
|
const COMMAND_LINE_ARGUMENT_A* nextArgument;
|
2018-05-06 11:49:05 +03:00
|
|
|
|
|
|
|
if (!argument || !argument->Name)
|
|
|
|
return NULL;
|
|
|
|
|
2012-11-05 01:09:43 +04:00
|
|
|
nextArgument = &argument[1];
|
|
|
|
|
|
|
|
if (nextArgument->Name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return nextArgument;
|
|
|
|
}
|
2020-01-21 15:55:55 +03:00
|
|
|
|
|
|
|
char** CommandLineParseCommaSeparatedValuesEx(const char* name, const char* list, size_t* count)
|
|
|
|
{
|
|
|
|
char** p;
|
|
|
|
char* str;
|
|
|
|
size_t nArgs;
|
|
|
|
size_t index;
|
|
|
|
size_t nCommas;
|
2021-10-07 14:43:12 +03:00
|
|
|
size_t prefix, len, namelen = 0;
|
2020-01-21 15:55:55 +03:00
|
|
|
nCommas = 0;
|
|
|
|
|
|
|
|
if (count == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*count = 0;
|
|
|
|
|
2021-07-06 12:08:47 +03:00
|
|
|
if (!list || strlen(list) == 0)
|
2020-01-21 15:55:55 +03:00
|
|
|
{
|
|
|
|
if (name)
|
|
|
|
{
|
2021-07-29 11:07:04 +03:00
|
|
|
size_t clen = strlen(name);
|
|
|
|
p = (char**)calloc(2UL + clen, sizeof(char*));
|
2020-01-21 15:55:55 +03:00
|
|
|
|
|
|
|
if (p)
|
|
|
|
{
|
|
|
|
char* dst = (char*)&p[1];
|
|
|
|
p[0] = dst;
|
2021-07-29 11:07:04 +03:00
|
|
|
sprintf_s(dst, clen + 1, "%s", name);
|
2020-01-21 15:55:55 +03:00
|
|
|
*count = 1;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const char* it = list;
|
|
|
|
|
|
|
|
while ((it = strchr(it, ',')) != NULL)
|
|
|
|
{
|
|
|
|
it++;
|
|
|
|
nCommas++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nArgs = nCommas + 1;
|
|
|
|
|
|
|
|
if (name)
|
|
|
|
nArgs++;
|
|
|
|
|
|
|
|
prefix = (nArgs + 1UL) * sizeof(char*);
|
|
|
|
len = strlen(list);
|
2021-10-07 14:43:12 +03:00
|
|
|
if (name)
|
|
|
|
namelen = strlen(name);
|
|
|
|
p = (char**)calloc(len + prefix + 1 + namelen + 1, sizeof(char*));
|
2020-01-21 15:55:55 +03:00
|
|
|
|
|
|
|
if (!p)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
str = &((char*)p)[prefix];
|
|
|
|
memcpy(str, list, len);
|
|
|
|
|
|
|
|
if (name)
|
2021-10-07 14:43:12 +03:00
|
|
|
{
|
|
|
|
char* namestr = &((char*)p)[prefix + len + 1];
|
|
|
|
memcpy(namestr, name, namelen);
|
|
|
|
|
|
|
|
p[0] = namestr;
|
|
|
|
}
|
2020-01-21 15:55:55 +03:00
|
|
|
|
|
|
|
for (index = name ? 1 : 0; index < nArgs; index++)
|
|
|
|
{
|
|
|
|
char* comma = strchr(str, ',');
|
|
|
|
p[index] = str;
|
|
|
|
|
|
|
|
if (comma)
|
|
|
|
{
|
|
|
|
str = comma + 1;
|
|
|
|
*comma = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*count = nArgs;
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
char** CommandLineParseCommaSeparatedValues(const char* list, size_t* count)
|
|
|
|
{
|
|
|
|
return CommandLineParseCommaSeparatedValuesEx(NULL, list, count);
|
|
|
|
}
|
2021-04-12 11:38:40 +03:00
|
|
|
|
|
|
|
char* CommandLineToCommaSeparatedValues(int argc, char* argv[])
|
2021-04-12 12:06:45 +03:00
|
|
|
{
|
|
|
|
return CommandLineToCommaSeparatedValuesEx(argc, argv, NULL, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* filtered(const char* arg, const char* filters[], size_t number)
|
|
|
|
{
|
|
|
|
size_t x;
|
|
|
|
if (number == 0)
|
|
|
|
return arg;
|
|
|
|
for (x = 0; x < number; x++)
|
|
|
|
{
|
|
|
|
const char* filter = filters[x];
|
|
|
|
size_t len = strlen(filter);
|
|
|
|
if (_strnicmp(arg, filter, len) == 0)
|
|
|
|
return &arg[len];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* CommandLineToCommaSeparatedValuesEx(int argc, char* argv[], const char* filters[],
|
|
|
|
size_t number)
|
2021-04-12 11:38:40 +03:00
|
|
|
{
|
|
|
|
int x;
|
|
|
|
char* str = NULL;
|
|
|
|
size_t offset = 0;
|
|
|
|
size_t size = argc + 1;
|
|
|
|
if ((argc <= 0) || !argv)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (x = 0; x < argc; x++)
|
|
|
|
size += strlen(argv[x]);
|
|
|
|
|
|
|
|
str = calloc(size, sizeof(char));
|
|
|
|
if (!str)
|
|
|
|
return NULL;
|
|
|
|
for (x = 0; x < argc; x++)
|
|
|
|
{
|
2021-04-12 12:06:45 +03:00
|
|
|
int rc;
|
|
|
|
const char* arg = filtered(argv[x], filters, number);
|
|
|
|
if (!arg)
|
|
|
|
continue;
|
|
|
|
rc = _snprintf(&str[offset], size - offset, "%s,", arg);
|
2021-04-12 11:38:40 +03:00
|
|
|
if (rc <= 0)
|
|
|
|
{
|
|
|
|
free(str);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
offset += (size_t)rc;
|
|
|
|
}
|
|
|
|
if (offset > 0)
|
|
|
|
str[offset - 1] = '\0';
|
|
|
|
return str;
|
|
|
|
}
|