iPXE
getopt.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <string.h>
00028 #include <stdio.h>
00029 #include <getopt.h>
00030 
00031 /** @file
00032  *
00033  * Parse command-line options
00034  *
00035  */
00036 
00037 /**
00038  * Option argument
00039  *
00040  * This will point to the argument for the most recently returned
00041  * option, if applicable.
00042  */
00043 char *optarg;
00044 
00045 /**
00046  * Current option index
00047  *
00048  * This is an index into the argv[] array.  When getopt() returns -1,
00049  * @c optind is the index to the first element that is not an option.
00050  */
00051 int optind;
00052 
00053 /**
00054  * Current option character index
00055  *
00056  * This is an index into the current element of argv[].
00057  */
00058 int nextchar;
00059 
00060 /**
00061  * Unrecognised option
00062  *
00063  * When an unrecognised option is encountered, the actual option
00064  * character is stored in @c optopt.
00065  */
00066 int optopt;
00067 
00068 /**
00069  * Get option argument from argv[] array
00070  *
00071  * @v argc              Argument count
00072  * @v argv              Argument list
00073  * @ret argument        Option argument, or NULL
00074  *
00075  * Grab the next element of argv[], if it exists and is not an option.
00076  */
00077 static const char * get_argv_argument ( int argc, char * const argv[] ) {
00078         char *arg;
00079 
00080         /* Don't overrun argv[] */
00081         if ( optind >= argc )
00082                 return NULL;
00083         arg = argv[optind];
00084 
00085         /* If next argv element is an option, then it's not usable as
00086          * an argument.
00087          */
00088         if ( *arg == '-' )
00089                 return NULL;
00090 
00091         /** Consume this argv element, and return it */
00092         optind++;
00093         return arg;
00094 }
00095 
00096 /**
00097  * Match long option
00098  *
00099  * @v argc              Argument count
00100  * @v argv              Argument list
00101  * @v opttext           Option text within current argv[] element
00102  * @v longopt           Long option specification
00103  * @ret option          Option to return from getopt()
00104  * @ret matched         Found a match for this long option
00105  */
00106 static int match_long_option ( int argc, char * const argv[],
00107                                const char *opttext,
00108                                const struct option *longopt, int *option ) {
00109         size_t optlen;
00110         const char *argument = NULL;
00111 
00112         /* Compare option name */
00113         optlen = strlen ( longopt->name );
00114         if ( strncmp ( opttext, longopt->name, optlen ) != 0 )
00115                 return 0;
00116 
00117         /* Check for inline argument */
00118         if ( opttext[optlen] == '=' ) {
00119                 argument = &opttext[ optlen + 1 ];
00120         } else if ( opttext[optlen] ) {
00121                 /* Long option with trailing garbage - no match */
00122                 return 0;
00123         }
00124 
00125         /* Consume this argv element */
00126         optind++;
00127 
00128         /* If we want an argument but don't have one yet, try to grab
00129          * the next argv element
00130          */
00131         if ( ( longopt->has_arg != no_argument ) && ( ! argument ) )
00132                 argument = get_argv_argument ( argc, argv );
00133 
00134         /* If we need an argument but don't have one, sulk */
00135         if ( ( longopt->has_arg == required_argument ) && ( ! argument ) ) {
00136                 printf ( "Option \"%s\" requires an argument\n",
00137                          longopt->name );
00138                 *option = ':';
00139                 return 1;
00140         }
00141 
00142         /* If we have an argument where we shouldn't have one, sulk */
00143         if ( ( longopt->has_arg == no_argument ) && argument ) {
00144                 printf ( "Option \"%s\" takes no argument\n", longopt->name );
00145                 *option = ':';
00146                 return 1;
00147         }
00148 
00149         /* Store values and return success */
00150         optarg = ( char * ) argument;
00151         if ( longopt->flag ) {
00152                 *(longopt->flag) = longopt->val;
00153                 *option = 0;
00154         } else {
00155                 *option = longopt->val;
00156         }
00157         return 1;
00158 }
00159 
00160 /**
00161  * Match short option
00162  *
00163  * @v argc              Argument count
00164  * @v argv              Argument list
00165  * @v opttext           Option text within current argv[] element
00166  * @v shortopt          Option character from option specification
00167  * @ret option          Option to return from getopt()
00168  * @ret matched         Found a match for this short option
00169  */
00170 static int match_short_option ( int argc, char * const argv[],
00171                                 const char *opttext, int shortopt,
00172                                 enum getopt_argument_requirement has_arg,
00173                                 int *option ) {
00174         const char *argument = NULL;
00175 
00176         /* Compare option character */
00177         if ( *opttext != shortopt )
00178                 return 0;
00179 
00180         /* Consume option character */
00181         opttext++;
00182         nextchar++;
00183         if ( *opttext ) {
00184                 if ( has_arg != no_argument ) {
00185                         /* Consume remainder of element as inline argument */
00186                         argument = opttext;
00187                         optind++;
00188                         nextchar = 0;
00189                 }
00190         } else {
00191                 /* Reached end of argv element */
00192                 optind++;
00193                 nextchar = 0;
00194         }
00195 
00196         /* If we want an argument but don't have one yet, try to grab
00197          * the next argv element
00198          */
00199         if ( ( has_arg != no_argument ) && ( ! argument ) )
00200                 argument = get_argv_argument ( argc, argv );
00201 
00202         /* If we need an argument but don't have one, sulk */
00203         if ( ( has_arg == required_argument ) && ( ! argument ) ) {
00204                 printf ( "Option \"%c\" requires an argument\n", shortopt );
00205                 *option = ':';
00206                 return 1;
00207         }
00208 
00209         /* Store values and return success */
00210         optarg = ( char * ) argument;
00211         *option = shortopt;
00212         return 1;
00213 }
00214 
00215 /**
00216  * Parse command-line options
00217  *
00218  * @v argc              Argument count
00219  * @v argv              Argument list
00220  * @v optstring         Option specification string
00221  * @v longopts          Long option specification table
00222  * @ret longindex       Index of long option (or NULL)
00223  * @ret option          Option found, or -1 for no more options
00224  *
00225  * Note that the caller must arrange for reset_getopt() to be called
00226  * before each set of calls to getopt_long().  In Etherboot, this is
00227  * done automatically by execv().
00228  */
00229 int getopt_long ( int argc, char * const argv[], const char *optstring,
00230                   const struct option *longopts, int *longindex ) {
00231         const char *opttext = argv[optind];
00232         const struct option *longopt;
00233         int shortopt;
00234         enum getopt_argument_requirement has_arg;
00235         int option;
00236 
00237         /* Check for end of argv array */
00238         if ( optind >= argc )
00239                 return -1;
00240 
00241         /* Check for end of options */
00242         if ( *(opttext++) != '-' )
00243                 return -1;
00244 
00245         /* Check for long options */
00246         if ( *(opttext++) == '-' ) {
00247                 /* "--" indicates end of options */
00248                 if ( *opttext == '\0' ) {
00249                         optind++;
00250                         return -1;
00251                 }
00252                 for ( longopt = longopts ; longopt->name ; longopt++ ) {
00253                         if ( ! match_long_option ( argc, argv, opttext,
00254                                                    longopt, &option ) )
00255                                 continue;
00256                         if ( longindex )
00257                                 *longindex = ( longopt - longopts );
00258                         return option;
00259                 }
00260                 optopt = '?';
00261                 printf ( "Unrecognised option \"--%s\"\n", opttext );
00262                 return '?';
00263         }
00264 
00265         /* Check for short options */
00266         if ( nextchar < 1 )
00267                 nextchar = 1;
00268         opttext = ( argv[optind] + nextchar );
00269         while ( ( shortopt = *(optstring++) ) ) {
00270                 has_arg = no_argument;
00271                 while ( *optstring == ':' ) {
00272                         has_arg++;
00273                         optstring++;
00274                 }
00275                 if ( match_short_option ( argc, argv, opttext, shortopt,
00276                                           has_arg, &option ) ) {
00277                         return option;
00278                 }
00279         }
00280         optopt = *opttext;
00281         printf ( "Unrecognised option \"-%c\"\n", optopt );
00282         return '?';
00283 }