2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
33 * Parse command-line options
40 * This will point to the argument for the most recently returned
41 * option, if applicable.
46 * Current option index
48 * This is an index into the argv[] array. When getopt() returns -1,
49 * @c optind is the index to the first element that is not an option.
54 * Current option character index
56 * This is an index into the current element of argv[].
63 * When an unrecognised option is encountered, the actual option
64 * character is stored in @c optopt.
69 * Get option argument from argv[] array
71 * @v argc Argument count
72 * @v argv Argument list
73 * @ret argument Option argument, or NULL
75 * Grab the next element of argv[], if it exists and is not an option.
77 static const char * get_argv_argument ( int argc, char * const argv[] ) {
80 /* Don't overrun argv[] */
85 /* If next argv element is an option, then it's not usable as
91 /** Consume this argv element, and return it */
99 * @v argc Argument count
100 * @v argv Argument list
101 * @v opttext Option text within current argv[] element
102 * @v longopt Long option specification
103 * @ret option Option to return from getopt()
104 * @ret matched Found a match for this long option
106 static int match_long_option ( int argc, char * const argv[],
108 const struct option *longopt, int *option ) {
110 const char *argument = NULL;
112 /* Compare option name */
113 optlen = strlen ( longopt->name );
114 if ( strncmp ( opttext, longopt->name, optlen ) != 0 )
117 /* Check for inline argument */
118 if ( opttext[optlen] == '=' ) {
119 argument = &opttext[ optlen + 1 ];
120 } else if ( opttext[optlen] ) {
121 /* Long option with trailing garbage - no match */
125 /* Consume this argv element */
128 /* If we want an argument but don't have one yet, try to grab
129 * the next argv element
131 if ( ( longopt->has_arg != no_argument ) && ( ! argument ) )
132 argument = get_argv_argument ( argc, argv );
134 /* If we need an argument but don't have one, sulk */
135 if ( ( longopt->has_arg == required_argument ) && ( ! argument ) ) {
136 printf ( "Option \"%s\" requires an argument\n",
142 /* If we have an argument where we shouldn't have one, sulk */
143 if ( ( longopt->has_arg == no_argument ) && argument ) {
144 printf ( "Option \"%s\" takes no argument\n", longopt->name );
149 /* Store values and return success */
150 optarg = ( char * ) argument;
151 if ( longopt->flag ) {
152 *(longopt->flag) = longopt->val;
155 *option = longopt->val;
163 * @v argc Argument count
164 * @v argv Argument list
165 * @v opttext Option text within current argv[] element
166 * @v shortopt Option character from option specification
167 * @ret option Option to return from getopt()
168 * @ret matched Found a match for this short option
170 static int match_short_option ( int argc, char * const argv[],
171 const char *opttext, int shortopt,
172 enum getopt_argument_requirement has_arg,
174 const char *argument = NULL;
176 /* Compare option character */
177 if ( *opttext != shortopt )
180 /* Consume option character */
184 if ( has_arg != no_argument ) {
185 /* Consume remainder of element as inline argument */
191 /* Reached end of argv element */
196 /* If we want an argument but don't have one yet, try to grab
197 * the next argv element
199 if ( ( has_arg != no_argument ) && ( ! argument ) )
200 argument = get_argv_argument ( argc, argv );
202 /* If we need an argument but don't have one, sulk */
203 if ( ( has_arg == required_argument ) && ( ! argument ) ) {
204 printf ( "Option \"%c\" requires an argument\n", shortopt );
209 /* Store values and return success */
210 optarg = ( char * ) argument;
216 * Parse command-line options
218 * @v argc Argument count
219 * @v argv Argument list
220 * @v optstring Option specification string
221 * @v longopts Long option specification table
222 * @ret longindex Index of long option (or NULL)
223 * @ret option Option found, or -1 for no more options
225 * Note that the caller must arrange for reset_getopt() to be called
226 * before each set of calls to getopt_long(). In Etherboot, this is
227 * done automatically by execv().
229 int getopt_long ( int argc, char * const argv[], const char *optstring,
230 const struct option *longopts, int *longindex ) {
231 const char *opttext = argv[optind];
232 const struct option *longopt;
234 enum getopt_argument_requirement has_arg;
237 /* Check for end of argv array */
238 if ( optind >= argc )
241 /* Check for end of options */
242 if ( *(opttext++) != '-' )
245 /* Check for long options */
246 if ( *(opttext++) == '-' ) {
247 /* "--" indicates end of options */
248 if ( *opttext == '\0' ) {
252 for ( longopt = longopts ; longopt->name ; longopt++ ) {
253 if ( ! match_long_option ( argc, argv, opttext,
257 *longindex = ( longopt - longopts );
261 printf ( "Unrecognised option \"--%s\"\n", opttext );
265 /* Check for short options */
268 opttext = ( argv[optind] + nextchar );
269 while ( ( shortopt = *(optstring++) ) ) {
270 has_arg = no_argument;
271 while ( *optstring == ':' ) {
275 if ( match_short_option ( argc, argv, opttext, shortopt,
276 has_arg, &option ) ) {
281 printf ( "Unrecognised option \"-%c\"\n", optopt );