summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/aws-c-common/source/command_line_parser.c
diff options
context:
space:
mode:
authorthegeorg <[email protected]>2022-05-11 12:12:06 +0300
committerthegeorg <[email protected]>2022-05-11 12:12:06 +0300
commit62f93da087b2fec0f89979fd11ac4d754ca36253 (patch)
tree67bf8ceb55e2d079f3575f9a7373584ad407d2a5 /contrib/restricted/aws/aws-c-common/source/command_line_parser.c
parent8d55620139d4309265409767f873ba83fe046418 (diff)
Update aws-c-common and aws-c-io
* Update `contrib/restricted/aws/aws-c-io` to 0.11.0 * Backport cJSON symbol renaming logic from aws-sdk-cpp upstream ref:396829235a01ed34888651ee38ebd76c95510d6b
Diffstat (limited to 'contrib/restricted/aws/aws-c-common/source/command_line_parser.c')
-rw-r--r--contrib/restricted/aws/aws-c-common/source/command_line_parser.c57
1 files changed, 54 insertions, 3 deletions
diff --git a/contrib/restricted/aws/aws-c-common/source/command_line_parser.c b/contrib/restricted/aws/aws-c-common/source/command_line_parser.c
index ccbe6d18208..bf2db81e0a0 100644
--- a/contrib/restricted/aws/aws-c-common/source/command_line_parser.c
+++ b/contrib/restricted/aws/aws-c-common/source/command_line_parser.c
@@ -2,13 +2,18 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
+#include <aws/common/byte_buf.h>
#include <aws/common/command_line_parser.h>
+#include <ctype.h>
+
int aws_cli_optind = 1;
int aws_cli_opterr = -1;
int aws_cli_optopt = 0;
+bool aws_cli_on_arg = false;
const char *aws_cli_optarg = NULL;
+const char *aws_cli_positional_arg = NULL;
static const struct aws_cli_option *s_find_option_from_char(
const struct aws_cli_option *longopts,
@@ -31,6 +36,16 @@ static const struct aws_cli_option *s_find_option_from_char(
return NULL;
}
+AWS_COMMON_API void aws_cli_reset_state(void) {
+ aws_cli_optind = 1;
+ aws_cli_opterr = -1;
+ aws_cli_optopt = 0;
+ aws_cli_on_arg = false;
+
+ aws_cli_optarg = NULL;
+ aws_cli_positional_arg = NULL;
+}
+
static const struct aws_cli_option *s_find_option_from_c_str(
const struct aws_cli_option *longopts,
const char *search_for,
@@ -70,22 +85,35 @@ int aws_cli_getopt_long(
char second_char = argv[aws_cli_optind][1];
char *option_start = NULL;
const struct aws_cli_option *option = NULL;
+ bool positional_arg_encountered = false;
if (first_char == '-' && second_char != '-') {
+ aws_cli_on_arg = true;
+ positional_arg_encountered = false;
option_start = &argv[aws_cli_optind][1];
option = s_find_option_from_char(longopts, *option_start, longindex);
} else if (first_char == '-' && second_char == '-') {
+ aws_cli_on_arg = true;
+ positional_arg_encountered = false;
option_start = &argv[aws_cli_optind][2];
option = s_find_option_from_c_str(longopts, option_start, longindex);
} else {
- return -1;
+ if (!aws_cli_on_arg) {
+ aws_cli_positional_arg = argv[aws_cli_optind];
+ positional_arg_encountered = true;
+ } else {
+ aws_cli_on_arg = false;
+ aws_cli_positional_arg = NULL;
+ }
}
aws_cli_optind++;
if (option) {
bool has_arg = false;
+ aws_cli_on_arg = false;
+ aws_cli_positional_arg = NULL;
- char *opt_value = memchr(optstring, option->val, strlen(optstring));
+ char *opt_value = memchr(optstring, option->val, strlen(optstring) + 1);
if (!opt_value) {
return '?';
}
@@ -105,5 +133,28 @@ int aws_cli_getopt_long(
return option->val;
}
- return '?';
+ /* start of text to indicate we just have a text argument. */
+ return positional_arg_encountered ? 0x02 : '?';
+}
+
+int aws_cli_dispatch_on_subcommand(
+ int argc,
+ char *const argv[],
+ struct aws_cli_subcommand_dispatch *dispatch_table,
+ int table_length,
+ void *user_data) {
+ if (argc >= 2) {
+ struct aws_byte_cursor arg_name = aws_byte_cursor_from_c_str(argv[1]);
+ for (int i = 0; i < table_length; ++i) {
+ struct aws_byte_cursor cmd_name = aws_byte_cursor_from_c_str(dispatch_table[i].command_name);
+
+ if (aws_byte_cursor_eq_ignore_case(&arg_name, &cmd_name)) {
+ return dispatch_table[i].subcommand_fn(argc - 1, &argv[1], (const char *)arg_name.ptr, user_data);
+ }
+ }
+
+ return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
+ }
+
+ return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
}