Debian Codegraph @

crt/aws-c-common/source/command_line_parser.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5 #include <aws/common/byte_buf.h>
6 #include <aws/common/command_line_parser.h>
7
8 #include <ctype.h>
9
10 int aws_cli_optind = 1;
11 int aws_cli_opterr = -1;
12 int aws_cli_optopt = 0;
13 bool aws_cli_on_arg = false;
14
15 const char *aws_cli_optarg = NULL;
16 const char *aws_cli_positional_arg = NULL;
17
18 static const struct aws_cli_option *s_find_option_from_char(
19 const struct aws_cli_option *longopts,
20 char search_for,
21 int *longindex) {
22 int index = 0;
23 const struct aws_cli_option *option = &longopts[index];
24
25 while (option->val != 0 || option->name) {
26 if (option->val == search_for) {
27 if (longindex) {
28 *longindex = index;
29 }
30 return option;
31 }
32
33 option = &longopts[++index];
34 }
35
36 return NULL;
37 }
38
39 AWS_COMMON_API void aws_cli_reset_state(void) {
40 aws_cli_optind = 1;
41 aws_cli_opterr = -1;
42 aws_cli_optopt = 0;
43 aws_cli_on_arg = false;
44
45 aws_cli_optarg = NULL;
46 aws_cli_positional_arg = NULL;
47 }
48
49 static const struct aws_cli_option *s_find_option_from_c_str(
50 const struct aws_cli_option *longopts,
51 const char *search_for,
52 int *longindex) {
53 int index = 0;
54 const struct aws_cli_option *option = &longopts[index];
55
56 while (option->name || option->val != 0) {
57 if (option->name && !strcmp(search_for, option->name)) {
58 if (longindex) {
59 *longindex = index;
60 }
61 return option;
62 }
63
64 option = &longopts[++index];
65 }
66
67 return NULL;
68 }
69
70 int aws_cli_getopt_long(
71 int argc,
72 char *const argv[],
73 const char *optstring,
74 const struct aws_cli_option *longopts,
75 int *longindex) {
76 aws_cli_optarg = NULL;
77
78 if (aws_cli_optind >= argc) {
79 return -1;
80 }
81
82 char first_char = argv[aws_cli_optind][0];
83 char second_char = argv[aws_cli_optind][1];
84 char *option_start = NULL;
85 const struct aws_cli_option *option = NULL;
86 bool positional_arg_encountered = false;
87
88 if (first_char == '-' && second_char != '-') {
89 aws_cli_on_arg = true;
90 positional_arg_encountered = false;
91 option_start = &argv[aws_cli_optind][1];
92 option = s_find_option_from_char(longopts, *option_start, longindex);
93 } else if (first_char == '-' && second_char == '-') {
94 aws_cli_on_arg = true;
95 positional_arg_encountered = false;
96 option_start = &argv[aws_cli_optind][2];
97 option = s_find_option_from_c_str(longopts, option_start, longindex);
98 } else {
99 if (!aws_cli_on_arg) {
100 aws_cli_positional_arg = argv[aws_cli_optind];
101 positional_arg_encountered = true;
102 } else {
103 aws_cli_on_arg = false;
104 aws_cli_positional_arg = NULL;
105 }
106 }
107
108 aws_cli_optind++;
109 if (option) {
110 bool has_arg = false;
111 aws_cli_on_arg = false;
112 aws_cli_positional_arg = NULL;
113
114 char *opt_value = memchr(optstring, option->val, strlen(optstring) + 1);
115 if (!opt_value) {
116 return '?';
117 }
118
119 if (opt_value[1] == ':') {
120 has_arg = true;
121 }
122
123 if (has_arg) {
124 if (aws_cli_optind >= argc) {
125 return '?';
126 }
127
128 aws_cli_optarg = argv[aws_cli_optind++];
129 }
130
131 return option->val;
132 }
133
134 /* start of text to indicate we just have a text argument. */
135 return positional_arg_encountered ? 0x02 : '?';
136 }
137
138 int aws_cli_dispatch_on_subcommand(
139 int argc,
140 char *const argv[],
141 struct aws_cli_subcommand_dispatch *dispatch_table,
142 int table_length,
143 void *user_data) {
144 if (argc >= 2) {
145 struct aws_byte_cursor arg_name = aws_byte_cursor_from_c_str(argv[1]);
146 for (int i = 0; i < table_length; ++i) {
147 struct aws_byte_cursor cmd_name = aws_byte_cursor_from_c_str(dispatch_table[i].command_name);
148
149 if (aws_byte_cursor_eq_ignore_case(&arg_name, &cmd_name)) {
150 return dispatch_table[i].subcommand_fn(argc - 1, &argv[1], (const char *)arg_name.ptr, user_data);
151 }
152 }
153
154 return aws_raise_error(AWS_ERROR_UNIMPLEMENTED);
155 }
156
157 return aws_raise_error(AWS_ERROR_INVALID_ARGUMENT);
158 }