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"
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 static void permute(const char *argv[], int *first_nonopt, int *first_opt, int after_opt);
26
27 static int parse_short_opt(int argc, const char *const argv[], const Options *options, Option *option);
28 static int parse_long_opt(int argc, const char *const argv[], const Options *options, 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_strchrnul(const char *s, int c);
39
40 Option parse_args(int argc, const char *argv[], const Options *options)
41 {
42 static int argn = 1;
43 static bool no_more_options = false;
44
45 static int first_nonopt = 1;
46
47 Option option = { NOT_OPTION, NULL, 0 };
48 option.argn = first_nonopt;
49
50 while (argn < argc)
51 {
52 size_t arg_len = strlen(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.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 = true;
66 }
67 else
68 {
69 argn = parse_long_opt(argc, argv, options, &option);
70 }
71 }
72 else
73 {
74 argn = parse_short_opt(argc, argv, options, &option);
75 }
76
77 // shift option in front of non-options
78 permute(argv, &first_nonopt, &option.argn, argn);
79
80 // don't include "--" in non-options
81 if (no_more_options)
82 ++option.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 permute(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_opt(int argc, const char *const argv[], const Options *options, Option *option)
119 {
120 static size_t offset = 1; // ignore the "-"
121
122 int argn = option->argn;
123
124 const char *arg = argv[argn];
125
126 const size_t arg_len = strlen(arg);
127
128 const bool arg_attached = (offset + 1 < arg_len), // possible argument attached?
129 last_in_argv = (argn == argc - 1);
130
131 option->value = INVALID_OPTION;
132
133 for (; !(options->short_opt == 0 &&
134 options->long_opt == NULL); ++options)
135 {
136 if (options->short_opt != 0 &&
137 options->short_opt == arg[offset])
138 {
139 option->value = options->value;
140
141 if (options->has_arg)
142 {
143 if (arg_attached) // arg directly follows option
144 {
145 option->arg = arg + offset + 1;
146
147 offset = arg_len;
148 }
149 else if (!last_in_argv) // arg is next in argv
150 {
151 option->arg = argv[++argn];
152
153 offset = arg_len;
154 }
155 else
156 {
157 option->value = OPTION_MISSING_ARG;
158 break;
159 }
160 }
161
162 break;
163 }
164 }
165
166 switch (option->value)
167 {
168 case INVALID_OPTION:
169 fprintf(stderr, "%s: invalid option -- '%c'\n", argv[0], argv[option->argn][offset]);
170 break;
171 case OPTION_MISSING_ARG:
172 fprintf(stderr, "%s: option requires an argument -- '%c'\n", argv[0], argv[option->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_opt(int argc, const char *const argv[], const Options *options, Option *option)
186 {
187 int argn = option->argn;
188
189 const char *arg = argv[argn] + 2; // ignore the "--"
190
191 const size_t arg_len = strlen(arg),
192 arg_opt_len = ot_strchrnul(arg, '=') - arg; // length before "="
193
194 const bool arg_attached = (arg_opt_len < arg_len), // argument attached using "="?
195 last_in_argv = (argn == argc - 1);
196
197 option->value = INVALID_OPTION;
198
199 for (; !(options->short_opt == 0 &&
200 options->long_opt == NULL); ++options)
201 {
202 if (options->long_opt != NULL &&
203 strncmp(options->long_opt, arg, arg_opt_len) == 0) // matches (partially, at least)
204 {
205 if (option->value != INVALID_OPTION) // other match already found
206 {
207 option->value = AMBIGUOUS_OPTION;
208 break;
209 }
210
211 option->value = options->value;
212
213 if (options->has_arg)
214 {
215 if (arg_attached) // arg is after "="
216 {
217 option->arg = arg + arg_opt_len + 1;
218 }
219 else if (!last_in_argv) // arg is next in argv
220 {
221 option->arg = argv[++argn];
222 }
223 else // arg is missing
224 {
225 option->value = OPTION_MISSING_ARG;
226 // can't break, gotta check for ambiguity
227 }
228 }
229
230 if (arg_opt_len == strlen(options->long_opt)) // exact match
231 break;
232 // can't break for partial match, gotta check for ambiguity
233 }
234 }
235
236 switch (option->value)
237 {
238 case INVALID_OPTION:
239 fprintf(stderr, "%s: unrecognized option '%s'\n", argv[0], argv[option->argn]);
240 break;
241 case AMBIGUOUS_OPTION:
242 fprintf(stderr, "%s: option '%s' is ambiguous\n", argv[0], argv[option->argn]);
243 break;
244 case OPTION_MISSING_ARG:
245 fprintf(stderr, "%s: option '%s' requires an argument\n", argv[0], argv[option->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_strchrnul(const char *s, int c)
255 {
256 for (; *s != c && *s != '\0'; ++s)
257 ;
258 return (char *)s;
259 }