Debian Codegraph @

src/arg_parse.c

raw symbols SCIP
1 /*
2 * OpenTyrian: A modern cross-platform port of Tyrian
3 * Copyright (C) 2007-2009 The OpenTyrian Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 #include "arg_parse.h"cxx . . $ `<file>/src/arg_parse.h`/
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 static void permutecxx . . $ permute(a9dfad950f64b0f2).(const char *argv[], int *first_nonopt, int *first_opt, int after_opt);
26
27 static int parse_short_optcxx . . $ parse_short_opt(923fcd9ec2aecb47).(int argc, const char *const argv[], const Optionscxx . . $ Options# *options, Optioncxx . . $ Option# *option);
28 static int parse_long_optcxx . . $ parse_long_opt(923fcd9ec2aecb47).(int argc, const char *const argv[], const Optionscxx . . $ Options# *options, Optioncxx . . $ Option# *option);
29
30 /*!
31 * \brief Locate a character in a a string.
32 *
33 * \param[in] s the string
34 * \param[in] c the character
35 * \return the pointer to the first occurrence of \p c in \p s if there is an occurrences;
36 * otherwise the pointer to the terminating NUL character of \p s
37 */
38 static char *ot_strchrnulcxx . . $ ot_strchrnul(2050848944e00d54).(const char *s, int c);
39
40 Optioncxx . . $ Option# parse_argscxx . . $ parse_args(bbba568bb979d141).(int argc, const char *argv[], const Optionscxx . . $ Options# *options)
41 {
42 static int argn = 1;
43 static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! no_more_options = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
44
45 static int first_nonopt = 1;
46
47 Optioncxx . . $ Option# option = { NOT_OPTIONcxx . . $ NOT_OPTION., NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, 0 };
48 option.argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn. = first_nonopt;
49
50 while (argn < argc)
51 {
52 size_t arg_len = strlencxx . . $ strlen(751346f8406fd082).(argv[argn]);
53
54 if (!no_more_options &&
55 argv[argn][0] == '-' && // first char is '-'
56 arg_len > 1) // option is not "-"
57 {
58 option.argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn. = argn;
59
60 if (argv[argn][1] == '-') // string begins with "--"
61 {
62 if (arg_len == 2) // "--" alone indicates end of options
63 {
64 ++argn;
65 no_more_options = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
66 }
67 else
68 {
69 argn = parse_long_optcxx . . $ parse_long_opt(923fcd9ec2aecb47).(argc, argv, options, &option);
70 }
71 }
72 else
73 {
74 argn = parse_short_optcxx . . $ parse_short_opt(923fcd9ec2aecb47).(argc, argv, options, &option);
75 }
76
77 // shift option in front of non-options
78 permutecxx . . $ permute(a9dfad950f64b0f2).(argv, &first_nonopt, &option.argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn., argn);
79
80 // don't include "--" in non-options
81 if (no_more_options)
82 ++option.argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn.;
83 break;
84 }
85 else
86 {
87 // skip non-options, permute later when option encountered
88 ++argn;
89 }
90 }
91
92 return option;
93 }
94
95 static void permutecxx . . $ permute(a9dfad950f64b0f2).(const char *argv[], int *first_nonopt, int *first_opt, int after_opt)
96 {
97 const int nonopts = *first_opt - *first_nonopt;
98
99 // slide each of the options in front of the non-options
100 for (int i = *first_opt; i < after_opt; ++i)
101 {
102 for (int j = i; j > *first_nonopt; --j)
103 {
104 // swap argv[j] and argv[j - 1]
105 const char *temp = argv[j];
106 argv[j] = argv[j - 1];
107 argv[j - 1] = temp;
108 }
109
110 // position of first non-option shifts right once for each option
111 ++(*first_nonopt);
112 }
113
114 // position of first option is initial position of first non-option
115 *first_opt -= nonopts;
116 }
117
118 static int parse_short_optcxx . . $ parse_short_opt(923fcd9ec2aecb47).(int argc, const char *const argv[], const Optionscxx . . $ Options# *options, Optioncxx . . $ Option# *option)
119 {
120 static size_t offset = 1; // ignore the "-"
121
122 int argn = option->argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn.;
123
124 const char *arg = argv[argn];
125
126 const size_t arg_len = strlencxx . . $ strlen(751346f8406fd082).(arg);
127
128 const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! arg_attached = (offset + 1 < arg_len), // possible argument attached?
129 last_in_argv = (argn == argc - 1);
130
131 option->valuecxx . . $ $anonymous_type_5f74074d111bf769_2#value. = INVALID_OPTIONcxx . . $ INVALID_OPTION.;
132
133 for (; !(options->short_optcxx . . $ $anonymous_type_5f74074d111bf769_0#short_opt. == 0 &&
134 options->long_optcxx . . $ $anonymous_type_5f74074d111bf769_0#long_opt. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); ++options)
135 {
136 if (options->short_optcxx . . $ $anonymous_type_5f74074d111bf769_0#short_opt. != 0 &&
137 options->short_optcxx . . $ $anonymous_type_5f74074d111bf769_0#short_opt. == arg[offset])
138 {
139 option->valuecxx . . $ $anonymous_type_5f74074d111bf769_2#value. = options->valuecxx . . $ $anonymous_type_5f74074d111bf769_0#value.;
140
141 if (options->has_argcxx . . $ $anonymous_type_5f74074d111bf769_0#has_arg.)
142 {
143 if (arg_attached) // arg directly follows option
144 {
145 option->argcxx . . $ $anonymous_type_5f74074d111bf769_2#arg. = arg + offset + 1;
146
147 offset = arg_len;
148 }
149 else if (!last_in_argv) // arg is next in argv
150 {
151 option->argcxx . . $ $anonymous_type_5f74074d111bf769_2#arg. = argv[++argn];
152
153 offset = arg_len;
154 }
155 else
156 {
157 option->valuecxx . . $ $anonymous_type_5f74074d111bf769_2#value. = OPTION_MISSING_ARGcxx . . $ OPTION_MISSING_ARG.;
158 break;
159 }
160 }
161
162 break;
163 }
164 }
165
166 switch (option->valuecxx . . $ $anonymous_type_5f74074d111bf769_2#value.)
167 {
168 case INVALID_OPTIONcxx . . $ INVALID_OPTION.:
169 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: invalid option -- '%c'\n", argv[0], argv[option->argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn.][offset]);
170 break;
171 case OPTION_MISSING_ARGcxx . . $ OPTION_MISSING_ARG.:
172 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: option requires an argument -- '%c'\n", argv[0], argv[option->argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn.][offset]);
173 break;
174 }
175
176 if (++offset >= arg_len)
177 {
178 ++argn;
179 offset = 1;
180 }
181
182 return argn; // which arg in argv that parse_args() should examine when called again
183 }
184
185 static int parse_long_optcxx . . $ parse_long_opt(923fcd9ec2aecb47).(int argc, const char *const argv[], const Optionscxx . . $ Options# *options, Optioncxx . . $ Option# *option)
186 {
187 int argn = option->argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn.;
188
189 const char *arg = argv[argn] + 2; // ignore the "--"
190
191 const size_t arg_len = strlencxx . . $ strlen(751346f8406fd082).(arg),
192 arg_opt_len = ot_strchrnulcxx . . $ ot_strchrnul(2050848944e00d54).(arg, '=') - arg; // length before "="
193
194 const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! arg_attached = (arg_opt_len < arg_len), // argument attached using "="?
195 last_in_argv = (argn == argc - 1);
196
197 option->valuecxx . . $ $anonymous_type_5f74074d111bf769_2#value. = INVALID_OPTIONcxx . . $ INVALID_OPTION.;
198
199 for (; !(options->short_optcxx . . $ $anonymous_type_5f74074d111bf769_0#short_opt. == 0 &&
200 options->long_optcxx . . $ $anonymous_type_5f74074d111bf769_0#long_opt. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); ++options)
201 {
202 if (options->long_optcxx . . $ $anonymous_type_5f74074d111bf769_0#long_opt. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! &&
203 strncmpcxx . . $ strncmp(fd278af2f295fa2e).(options->long_optcxx . . $ $anonymous_type_5f74074d111bf769_0#long_opt., arg, arg_opt_len) == 0) // matches (partially, at least)
204 {
205 if (option->valuecxx . . $ $anonymous_type_5f74074d111bf769_2#value. != INVALID_OPTIONcxx . . $ INVALID_OPTION.) // other match already found
206 {
207 option->valuecxx . . $ $anonymous_type_5f74074d111bf769_2#value. = AMBIGUOUS_OPTIONcxx . . $ AMBIGUOUS_OPTION.;
208 break;
209 }
210
211 option->valuecxx . . $ $anonymous_type_5f74074d111bf769_2#value. = options->valuecxx . . $ $anonymous_type_5f74074d111bf769_0#value.;
212
213 if (options->has_argcxx . . $ $anonymous_type_5f74074d111bf769_0#has_arg.)
214 {
215 if (arg_attached) // arg is after "="
216 {
217 option->argcxx . . $ $anonymous_type_5f74074d111bf769_2#arg. = arg + arg_opt_len + 1;
218 }
219 else if (!last_in_argv) // arg is next in argv
220 {
221 option->argcxx . . $ $anonymous_type_5f74074d111bf769_2#arg. = argv[++argn];
222 }
223 else // arg is missing
224 {
225 option->valuecxx . . $ $anonymous_type_5f74074d111bf769_2#value. = OPTION_MISSING_ARGcxx . . $ OPTION_MISSING_ARG.;
226 // can't break, gotta check for ambiguity
227 }
228 }
229
230 if (arg_opt_len == strlencxx . . $ strlen(751346f8406fd082).(options->long_optcxx . . $ $anonymous_type_5f74074d111bf769_0#long_opt.)) // exact match
231 break;
232 // can't break for partial match, gotta check for ambiguity
233 }
234 }
235
236 switch (option->valuecxx . . $ $anonymous_type_5f74074d111bf769_2#value.)
237 {
238 case INVALID_OPTIONcxx . . $ INVALID_OPTION.:
239 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: unrecognized option '%s'\n", argv[0], argv[option->argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn.]);
240 break;
241 case AMBIGUOUS_OPTIONcxx . . $ AMBIGUOUS_OPTION.:
242 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: option '%s' is ambiguous\n", argv[0], argv[option->argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn.]);
243 break;
244 case OPTION_MISSING_ARGcxx . . $ OPTION_MISSING_ARG.:
245 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "%s: option '%s' requires an argument\n", argv[0], argv[option->argncxx . . $ $anonymous_type_5f74074d111bf769_2#argn.]);
246 break;
247 }
248
249 ++argn;
250
251 return argn; // which arg in argv that parse_args() should examine when called again
252 }
253
254 static char *ot_strchrnulcxx . . $ ot_strchrnul(2050848944e00d54).(const char *s, int c)
255 {
256 for (; *s != c && *s != '\0'; ++s)
257 ;
258 return (char *)s;
259 }