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>
|
|
|
|
|
|
|
|
#include <winpr/cmdline.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-11-07 08:07:03 +04:00
|
|
|
int CommandLineParseArgumentsA(int argc, LPCSTR* 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;
|
2012-11-02 08:20:46 +04:00
|
|
|
int length;
|
2012-11-27 07:42:40 +04:00
|
|
|
int index;
|
2012-11-02 22:16:37 +04:00
|
|
|
BOOL match;
|
2012-11-02 08:20:46 +04:00
|
|
|
char* sigil;
|
|
|
|
int sigil_length;
|
|
|
|
int sigil_index;
|
|
|
|
char* keyword;
|
|
|
|
int keyword_length;
|
|
|
|
int keyword_index;
|
|
|
|
char* separator;
|
|
|
|
int separator_length;
|
|
|
|
int separator_index;
|
|
|
|
char* value;
|
|
|
|
int value_length;
|
|
|
|
int value_index;
|
2012-12-02 22:34:00 +04:00
|
|
|
int toggle;
|
2012-11-02 08:20:46 +04:00
|
|
|
|
2013-06-15 23:13:38 +04:00
|
|
|
status = 0;
|
|
|
|
|
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
|
|
|
{
|
|
|
|
status = COMMAND_LINE_STATUS_PRINT_HELP;
|
|
|
|
return status;
|
|
|
|
}
|
2012-11-22 18:36:09 +04:00
|
|
|
|
2012-11-02 08:20:46 +04:00
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
{
|
2012-11-27 07:42:40 +04:00
|
|
|
index = i;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (count < 0)
|
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)
|
|
|
|
{
|
|
|
|
i += count;
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-05 21:12:18 +04:00
|
|
|
}
|
2012-11-07 07:08:09 +04:00
|
|
|
|
2012-11-02 08:20:46 +04:00
|
|
|
sigil_index = 0;
|
2012-11-02 08:58:32 +04:00
|
|
|
sigil_length = 0;
|
2012-11-02 08:20:46 +04:00
|
|
|
sigil = (char*) &argv[i][sigil_index];
|
|
|
|
length = strlen(argv[i]);
|
|
|
|
|
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;
|
|
|
|
}
|
2012-11-02 22:16:37 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2012-11-02 08:20:46 +04:00
|
|
|
|
2012-11-19 02:32:18 +04:00
|
|
|
if ((sigil_length > 0) || (flags & COMMAND_LINE_SIGIL_NONE))
|
2012-11-02 08:58:32 +04:00
|
|
|
{
|
2012-11-02 08:20:46 +04:00
|
|
|
if (length < (sigil_length + 1))
|
2012-11-02 22:16:37 +04:00
|
|
|
return COMMAND_LINE_ERROR_NO_KEYWORD;
|
2012-11-02 08:20:46 +04:00
|
|
|
|
|
|
|
keyword_index = sigil_index + sigil_length;
|
|
|
|
keyword = (char*) &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;
|
|
|
|
keyword = (char*) &argv[i][keyword_index];
|
|
|
|
}
|
|
|
|
else if (strncmp(keyword, "disable-", 8) == 0)
|
|
|
|
{
|
|
|
|
toggle = FALSE;
|
|
|
|
keyword_index += 8;
|
|
|
|
keyword = (char*) &argv[i][keyword_index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
separator_length = 1;
|
|
|
|
separator_index = (separator - argv[i]);
|
|
|
|
|
|
|
|
keyword_length = (separator - keyword);
|
|
|
|
|
|
|
|
value_index = separator_index + separator_length;
|
|
|
|
value = (char*) &argv[i][value_index];
|
|
|
|
value_length = (length - value_index);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
separator_length = 0;
|
|
|
|
separator_index = -1;
|
|
|
|
keyword_length = (length - keyword_index);
|
|
|
|
|
|
|
|
value_index = -1;
|
|
|
|
value = NULL;
|
|
|
|
value_length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; options[j].Name != NULL; j++)
|
|
|
|
{
|
2012-11-02 22:16:37 +04:00
|
|
|
match = FALSE;
|
|
|
|
|
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;
|
|
|
|
|
2012-11-27 07:42:40 +04:00
|
|
|
options[j].Index = index;
|
|
|
|
|
|
|
|
if ((flags & COMMAND_LINE_SEPARATOR_SPACE) && ((i + 1) < argc))
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (value_present)
|
2012-12-02 22:34:00 +04:00
|
|
|
{
|
|
|
|
i++;
|
|
|
|
value_index = 0;
|
|
|
|
length = strlen(argv[i]);
|
2012-11-07 07:08:09 +04:00
|
|
|
|
2012-12-02 22:34:00 +04:00
|
|
|
value = (char*) &argv[i][value_index];
|
|
|
|
value_length = (length - value_index);
|
|
|
|
}
|
2012-11-27 07:42:40 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(flags & COMMAND_LINE_SEPARATOR_SPACE))
|
|
|
|
{
|
|
|
|
if (value && (options[j].Flags & COMMAND_LINE_VALUE_FLAG))
|
|
|
|
return COMMAND_LINE_ERROR_UNEXPECTED_VALUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (value && (options[j].Flags & COMMAND_LINE_VALUE_FLAG))
|
|
|
|
{
|
|
|
|
i--;
|
|
|
|
value_index = -1;
|
|
|
|
value = NULL;
|
|
|
|
value_length = 0;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
options[j].Value = value;
|
|
|
|
options[j].Flags |= COMMAND_LINE_VALUE_PRESENT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (options[j].Flags & COMMAND_LINE_VALUE_FLAG)
|
|
|
|
{
|
|
|
|
options[j].Value = (LPSTR) 1;
|
|
|
|
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)
|
|
|
|
postFilter(context, &options[j]);
|
|
|
|
|
2012-11-07 03:49:02 +04:00
|
|
|
if (options[j].Flags & COMMAND_LINE_PRINT)
|
|
|
|
return COMMAND_LINE_STATUS_PRINT;
|
|
|
|
else if (options[j].Flags & COMMAND_LINE_PRINT_HELP)
|
|
|
|
return COMMAND_LINE_STATUS_PRINT_HELP;
|
|
|
|
else if (options[j].Flags & COMMAND_LINE_PRINT_VERSION)
|
|
|
|
return COMMAND_LINE_STATUS_PRINT_VERSION;
|
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
|
|
|
}
|
|
|
|
|
2012-11-07 08:07:03 +04:00
|
|
|
int CommandLineParseArgumentsW(int argc, LPCWSTR* argv, COMMAND_LINE_ARGUMENT_W* options, 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)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMMAND_LINE_ARGUMENT_A* CommandLineFindArgumentA(COMMAND_LINE_ARGUMENT_A* options, LPCSTR Name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
COMMAND_LINE_ARGUMENT_W* CommandLineFindArgumentW(COMMAND_LINE_ARGUMENT_W* options, LPCWSTR Name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
COMMAND_LINE_ARGUMENT_A* CommandLineFindNextArgumentA(COMMAND_LINE_ARGUMENT_A* argument)
|
|
|
|
{
|
|
|
|
COMMAND_LINE_ARGUMENT_A* nextArgument;
|
|
|
|
|
|
|
|
nextArgument = &argument[1];
|
|
|
|
|
|
|
|
if (nextArgument->Name == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return nextArgument;
|
|
|
|
}
|