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.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 12:08:00 +03:00
|
|
|
#include <winpr/config.h>
|
2012-11-02 08:20:46 +04:00
|
|
|
|
|
|
|
#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++)
|
|
|
|
{
|
2023-04-27 10:04:13 +03:00
|
|
|
COMMAND_LINE_ARGUMENT_A* cur = &options[j];
|
2017-11-14 15:54:05 +03:00
|
|
|
BOOL match = FALSE;
|
2012-11-02 22:16:37 +04:00
|
|
|
|
2023-04-27 10:04:13 +03:00
|
|
|
if (strncmp(cur->Name, keyword, keyword_length) == 0)
|
2012-11-02 08:20:46 +04:00
|
|
|
{
|
2023-04-27 10:04:13 +03:00
|
|
|
if (strlen(cur->Name) == keyword_length)
|
2012-11-02 22:16:37 +04:00
|
|
|
match = TRUE;
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:04:13 +03:00
|
|
|
if ((!match) && (cur->Alias != NULL))
|
2012-11-02 22:16:37 +04:00
|
|
|
{
|
2023-04-27 10:04:13 +03:00
|
|
|
if (strncmp(cur->Alias, keyword, keyword_length) == 0)
|
2012-11-02 08:20:46 +04:00
|
|
|
{
|
2023-04-27 10:04:13 +03:00
|
|
|
if (strlen(cur->Alias) == keyword_length)
|
2012-11-02 22:16:37 +04:00
|
|
|
match = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!match)
|
|
|
|
continue;
|
|
|
|
|
2013-09-09 18:07:14 +04:00
|
|
|
found = match;
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->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;
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:04:13 +03:00
|
|
|
if ((cur->Flags & COMMAND_LINE_VALUE_REQUIRED) ||
|
|
|
|
(cur->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
|
|
|
}
|
2023-04-27 10:04:13 +03:00
|
|
|
else if (!value_present && (cur->Flags & COMMAND_LINE_VALUE_OPTIONAL))
|
2013-09-09 19:21:15 +04:00
|
|
|
{
|
|
|
|
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))
|
|
|
|
{
|
2023-04-27 10:04:13 +03:00
|
|
|
if (value && (cur->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
|
|
|
|
{
|
2023-04-27 10:04:13 +03:00
|
|
|
if (value && (cur->Flags & COMMAND_LINE_VALUE_FLAG))
|
2012-11-27 07:42:40 +04:00
|
|
|
{
|
|
|
|
i--;
|
|
|
|
value = NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-11-02 22:16:37 +04:00
|
|
|
|
2023-04-27 10:04:13 +03:00
|
|
|
if (!value && (cur->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
|
|
|
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->Flags |= COMMAND_LINE_ARGUMENT_PRESENT;
|
2012-11-27 11:49:44 +04:00
|
|
|
|
2012-11-02 22:16:37 +04:00
|
|
|
if (value)
|
|
|
|
{
|
2023-04-27 10:04:13 +03:00
|
|
|
if (!(cur->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
|
|
|
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->Value = value;
|
|
|
|
cur->Flags |= COMMAND_LINE_VALUE_PRESENT;
|
2012-11-02 22:16:37 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-27 10:04:13 +03:00
|
|
|
if (cur->Flags & COMMAND_LINE_VALUE_FLAG)
|
2012-11-02 22:16:37 +04:00
|
|
|
{
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->Value = (LPSTR)1;
|
|
|
|
cur->Flags |= COMMAND_LINE_VALUE_PRESENT;
|
2012-11-02 08:20:46 +04:00
|
|
|
}
|
2023-04-27 10:04:13 +03:00
|
|
|
else if (cur->Flags & COMMAND_LINE_VALUE_BOOL)
|
2012-11-05 01:09:43 +04:00
|
|
|
{
|
2012-12-02 22:34:00 +04:00
|
|
|
if (flags & COMMAND_LINE_SIGIL_ENABLE_DISABLE)
|
|
|
|
{
|
|
|
|
if (toggle == -1)
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->Value = BoolValueTrue;
|
2012-12-02 22:34:00 +04:00
|
|
|
else if (!toggle)
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->Value = BoolValueFalse;
|
2012-12-02 22:34:00 +04:00
|
|
|
else
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->Value = BoolValueTrue;
|
2012-12-02 22:34:00 +04:00
|
|
|
}
|
2012-11-05 01:09:43 +04:00
|
|
|
else
|
2012-12-02 22:34:00 +04:00
|
|
|
{
|
|
|
|
if (sigil[0] == '+')
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->Value = BoolValueTrue;
|
2012-12-02 22:34:00 +04:00
|
|
|
else if (sigil[0] == '-')
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->Value = BoolValueFalse;
|
2012-12-02 22:34:00 +04:00
|
|
|
else
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->Value = BoolValueTrue;
|
2012-12-02 22:34:00 +04:00
|
|
|
}
|
2012-11-05 01:09:43 +04:00
|
|
|
|
2023-04-27 10:04:13 +03:00
|
|
|
cur->Flags |= COMMAND_LINE_VALUE_PRESENT;
|
2012-11-05 01:09:43 +04:00
|
|
|
}
|
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
|
|
|
|
2023-04-27 10:04:13 +03:00
|
|
|
if (cur->Flags & COMMAND_LINE_PRINT)
|
2017-05-04 15:46:58 +03:00
|
|
|
return COMMAND_LINE_STATUS_PRINT;
|
2023-04-27 10:04:13 +03:00
|
|
|
else if (cur->Flags & COMMAND_LINE_PRINT_HELP)
|
2017-05-04 15:46:58 +03:00
|
|
|
return COMMAND_LINE_STATUS_PRINT_HELP;
|
2023-04-27 10:04:13 +03:00
|
|
|
else if (cur->Flags & COMMAND_LINE_PRINT_VERSION)
|
2017-05-04 15:46:58 +03:00
|
|
|
return COMMAND_LINE_STATUS_PRINT_VERSION;
|
2023-04-27 10:04:13 +03:00
|
|
|
else if (cur->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
|
|
|
|
2022-02-24 10:46:18 +03:00
|
|
|
static int is_quoted(char c)
|
|
|
|
{
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '"':
|
|
|
|
return 1;
|
|
|
|
case '\'':
|
|
|
|
return -1;
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_element_count(const char* list, BOOL* failed, BOOL fullquoted)
|
2022-02-23 16:49:03 +03:00
|
|
|
{
|
|
|
|
size_t count = 0;
|
2022-02-24 10:46:18 +03:00
|
|
|
int quoted = 0;
|
2022-02-23 16:49:03 +03:00
|
|
|
BOOL finished = FALSE;
|
|
|
|
BOOL first = TRUE;
|
|
|
|
const char* it = list;
|
|
|
|
|
|
|
|
if (!list)
|
|
|
|
return 0;
|
|
|
|
if (strlen(list) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
while (!finished)
|
|
|
|
{
|
|
|
|
BOOL nextFirst = FALSE;
|
|
|
|
switch (*it)
|
|
|
|
{
|
|
|
|
case '\0':
|
2022-02-24 10:46:18 +03:00
|
|
|
if (quoted != 0)
|
2022-02-23 16:49:03 +03:00
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Invalid argument (missing closing quote) '%s'", list);
|
|
|
|
*failed = TRUE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
finished = TRUE;
|
|
|
|
break;
|
2022-02-24 10:46:18 +03:00
|
|
|
case '\'':
|
2022-02-23 16:49:03 +03:00
|
|
|
case '"':
|
2022-02-24 10:46:18 +03:00
|
|
|
if (!fullquoted)
|
2022-02-23 16:49:03 +03:00
|
|
|
{
|
2022-02-24 10:46:18 +03:00
|
|
|
int now = is_quoted(*it);
|
|
|
|
if ((quoted == 0) && !first)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Invalid argument (misplaced quote) '%s'", list);
|
|
|
|
*failed = TRUE;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (now == quoted)
|
|
|
|
quoted = 0;
|
|
|
|
else if (quoted == 0)
|
|
|
|
quoted = now;
|
2022-02-23 16:49:03 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ',':
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Invalid argument (empty list elements) '%s'", list);
|
|
|
|
*failed = TRUE;
|
|
|
|
return 0;
|
|
|
|
}
|
2022-02-24 10:46:18 +03:00
|
|
|
if (quoted == 0)
|
2022-02-23 16:49:03 +03:00
|
|
|
{
|
|
|
|
nextFirst = TRUE;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
first = nextFirst;
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
return count + 1;
|
|
|
|
}
|
|
|
|
|
2022-02-24 10:46:18 +03:00
|
|
|
static char* get_next_comma(char* string, BOOL fullquoted)
|
2022-02-23 16:49:03 +03:00
|
|
|
{
|
|
|
|
const char* log = string;
|
2022-02-24 10:46:18 +03:00
|
|
|
int quoted = 0;
|
2022-02-23 16:49:03 +03:00
|
|
|
BOOL first = TRUE;
|
|
|
|
|
|
|
|
WINPR_ASSERT(string);
|
|
|
|
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
switch (*string)
|
|
|
|
{
|
|
|
|
case '\0':
|
2022-02-24 10:46:18 +03:00
|
|
|
if (quoted != 0)
|
2022-02-23 16:49:03 +03:00
|
|
|
WLog_ERR(TAG, "Invalid quoted argument '%s'", log);
|
|
|
|
return NULL;
|
|
|
|
|
2022-02-24 10:46:18 +03:00
|
|
|
case '\'':
|
2022-02-23 16:49:03 +03:00
|
|
|
case '"':
|
2022-02-24 10:46:18 +03:00
|
|
|
if (!fullquoted)
|
2022-02-23 16:49:03 +03:00
|
|
|
{
|
2022-02-24 10:46:18 +03:00
|
|
|
int now = is_quoted(*string);
|
|
|
|
if ((quoted == 0) && !first)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Invalid quoted argument '%s'", log);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (now == quoted)
|
|
|
|
quoted = 0;
|
|
|
|
else if (quoted == 0)
|
|
|
|
quoted = now;
|
2022-02-23 16:49:03 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ',':
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "Invalid argument (empty list elements) '%s'", log);
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-02-24 10:46:18 +03:00
|
|
|
if (quoted == 0)
|
2022-02-23 16:49:03 +03:00
|
|
|
return string;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
first = FALSE;
|
|
|
|
string++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-02-24 10:46:18 +03:00
|
|
|
static BOOL is_valid_fullquoted(const char* string)
|
|
|
|
{
|
|
|
|
char cur = '\0';
|
|
|
|
char last = '\0';
|
|
|
|
const char quote = *string++;
|
|
|
|
|
|
|
|
/* We did not start with a quote. */
|
|
|
|
if (is_quoted(quote) == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
while ((cur = *string++) != '\0')
|
|
|
|
{
|
|
|
|
/* A quote is found. */
|
|
|
|
if (cur == quote)
|
|
|
|
{
|
|
|
|
/* If the quote was escaped, it is valid. */
|
|
|
|
if (last != '\\')
|
|
|
|
{
|
|
|
|
/* Only allow unescaped quote as last character in string. */
|
|
|
|
if (*string != '\0')
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
/* If the last quote in the string is escaped, it is wrong. */
|
|
|
|
else if (*string != '\0')
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
last = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The string did not terminate with the same quote as it started. */
|
|
|
|
if (last != quote)
|
|
|
|
return FALSE;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-01-21 15:55:55 +03:00
|
|
|
char** CommandLineParseCommaSeparatedValuesEx(const char* name, const char* list, size_t* count)
|
|
|
|
{
|
2022-02-23 16:49:03 +03:00
|
|
|
char** p = NULL;
|
|
|
|
char* str = NULL;
|
|
|
|
size_t nArgs = 0;
|
|
|
|
size_t index = 0;
|
|
|
|
size_t prefix = 0;
|
|
|
|
size_t len = 0;
|
|
|
|
size_t namelen = 0;
|
|
|
|
BOOL failed = FALSE;
|
|
|
|
char* copy = NULL;
|
|
|
|
char* unquoted = NULL;
|
2022-02-24 10:46:18 +03:00
|
|
|
BOOL fullquoted = FALSE;
|
2020-01-21 15:55:55 +03:00
|
|
|
|
|
|
|
if (count == NULL)
|
2022-02-23 16:49:03 +03:00
|
|
|
goto fail;
|
2020-01-21 15:55:55 +03:00
|
|
|
|
|
|
|
*count = 0;
|
2022-02-23 16:49:03 +03:00
|
|
|
if (list)
|
|
|
|
{
|
2022-02-24 10:46:18 +03:00
|
|
|
int start, end;
|
2022-02-23 16:49:03 +03:00
|
|
|
unquoted = copy = _strdup(list);
|
|
|
|
if (!copy)
|
|
|
|
goto fail;
|
2020-01-21 15:55:55 +03:00
|
|
|
|
2022-02-23 16:49:03 +03:00
|
|
|
len = strlen(unquoted);
|
2022-02-24 10:46:18 +03:00
|
|
|
if (len > 0)
|
2022-02-23 16:49:03 +03:00
|
|
|
{
|
2022-02-24 10:46:18 +03:00
|
|
|
start = is_quoted(unquoted[0]);
|
|
|
|
end = is_quoted(unquoted[len - 1]);
|
|
|
|
|
|
|
|
if ((start != 0) && (end != 0))
|
|
|
|
{
|
|
|
|
if (start != end)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "invalid argument (quote mismatch) '%s'", list);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
if (!is_valid_fullquoted(unquoted))
|
|
|
|
goto fail;
|
|
|
|
unquoted[len - 1] = '\0';
|
|
|
|
unquoted++;
|
|
|
|
len -= 2;
|
|
|
|
fullquoted = TRUE;
|
|
|
|
}
|
2022-02-23 16:49:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-24 10:46:18 +03:00
|
|
|
*count = get_element_count(unquoted, &failed, fullquoted);
|
2022-02-23 16:49:03 +03:00
|
|
|
if (failed)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (*count == 0)
|
2020-01-21 15:55:55 +03:00
|
|
|
{
|
2022-02-23 16:49:03 +03:00
|
|
|
if (!name)
|
|
|
|
goto fail;
|
|
|
|
else
|
2020-01-21 15:55:55 +03:00
|
|
|
{
|
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;
|
2022-02-23 16:49:03 +03:00
|
|
|
goto fail;
|
2020-01-21 15:55:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-23 16:49:03 +03:00
|
|
|
nArgs = *count;
|
2020-01-21 15:55:55 +03:00
|
|
|
|
|
|
|
if (name)
|
|
|
|
nArgs++;
|
|
|
|
|
|
|
|
prefix = (nArgs + 1UL) * sizeof(char*);
|
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)
|
2022-02-23 16:49:03 +03:00
|
|
|
goto fail;
|
2020-01-21 15:55:55 +03:00
|
|
|
|
|
|
|
str = &((char*)p)[prefix];
|
2022-02-23 16:49:03 +03:00
|
|
|
memcpy(str, unquoted, len);
|
2020-01-21 15:55:55 +03:00
|
|
|
|
|
|
|
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++)
|
|
|
|
{
|
2022-02-23 16:49:03 +03:00
|
|
|
char* ptr = str;
|
2022-02-24 10:46:18 +03:00
|
|
|
const int quote = is_quoted(*ptr);
|
|
|
|
char* comma = get_next_comma(str, fullquoted);
|
2022-02-23 16:49:03 +03:00
|
|
|
|
2022-02-24 10:46:18 +03:00
|
|
|
if ((quote != 0) && !fullquoted)
|
2022-02-23 16:49:03 +03:00
|
|
|
ptr++;
|
|
|
|
|
|
|
|
p[index] = ptr;
|
2020-01-21 15:55:55 +03:00
|
|
|
|
|
|
|
if (comma)
|
|
|
|
{
|
2022-02-23 16:49:03 +03:00
|
|
|
char* last = comma - 1;
|
2022-02-24 10:46:18 +03:00
|
|
|
const int lastQuote = is_quoted(*last);
|
|
|
|
|
|
|
|
if (!fullquoted)
|
|
|
|
{
|
|
|
|
if (lastQuote != quote)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "invalid argument (quote mismatch) '%s'", list);
|
|
|
|
free(p);
|
|
|
|
p = NULL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else if (lastQuote != 0)
|
|
|
|
*last = '\0';
|
|
|
|
}
|
2020-01-21 15:55:55 +03:00
|
|
|
*comma = '\0';
|
2022-02-23 16:49:03 +03:00
|
|
|
|
|
|
|
str = comma + 1;
|
2020-01-21 15:55:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*count = nArgs;
|
2022-02-23 16:49:03 +03:00
|
|
|
fail:
|
|
|
|
free(copy);
|
2020-01-21 15:55:55 +03:00
|
|
|
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;
|
|
|
|
}
|