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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stdint.h>
00022 #include <string.h>
00023 #include <stdio.h>
00024 #include <getopt.h>
00025 
00026 /** @file
00027  *
00028  * Parse command-line options
00029  *
00030  */
00031 
00032 /**
00033  * Option argument
00034  *
00035  * This will point to the argument for the most recently returned
00036  * option, if applicable.
00037  */
00038 char *optarg;
00039 
00040 /**
00041  * Current option index
00042  *
00043  * This is an index into the argv[] array.  When getopt() returns -1,
00044  * @c optind is the index to the first element that is not an option.
00045  */
00046 int optind;
00047 
00048 /**
00049  * Current option character index
00050  *
00051  * This is an index into the current element of argv[].
00052  */
00053 int nextchar;
00054 
00055 /**
00056  * Unrecognised option
00057  *
00058  * When an unrecognised option is encountered, the actual option
00059  * character is stored in @c optopt.
00060  */
00061 int optopt;
00062 
00063 /**
00064  * Get option argument from argv[] array
00065  *
00066  * @v argc              Argument count
00067  * @v argv              Argument list
00068  * @ret argument        Option argument, or NULL
00069  *
00070  * Grab the next element of argv[], if it exists and is not an option.
00071  */
00072 static const char * get_argv_argument ( int argc, char * const argv[] ) {
00073         char *arg;
00074 
00075         /* Don't overrun argv[] */
00076         if ( optind >= argc )
00077                 return NULL;
00078         arg = argv[optind];
00079 
00080         /* If next argv element is an option, then it's not usable as
00081          * an argument.
00082          */
00083         if ( *arg == '-' )
00084                 return NULL;
00085 
00086         /** Consume this argv element, and return it */
00087         optind++;
00088         return arg;
00089 }
00090 
00091 /**
00092  * Match long option
00093  *
00094  * @v argc              Argument count
00095  * @v argv              Argument list
00096  * @v opttext           Option text within current argv[] element
00097  * @v longopt           Long option specification
00098  * @ret option          Option to return from getopt()
00099  * @ret matched         Found a match for this long option
00100  */
00101 static int match_long_option ( int argc, char * const argv[],
00102                                const char *opttext,
00103                                const struct option *longopt, int *option ) {
00104         size_t optlen;
00105         const char *argument = NULL;
00106 
00107         /* Compare option name */
00108         optlen = strlen ( longopt->name );
00109         if ( strncmp ( opttext, longopt->name, optlen ) != 0 )
00110                 return 0;
00111 
00112         /* Check for inline argument */
00113         if ( opttext[optlen] == '=' ) {
00114                 argument = &opttext[ optlen + 1 ];
00115         } else if ( opttext[optlen] ) {
00116                 /* Long option with trailing garbage - no match */
00117                 return 0;
00118         }
00119 
00120         /* Consume this argv element */
00121         optind++;
00122 
00123         /* If we want an argument but don't have one yet, try to grab
00124          * the next argv element
00125          */
00126         if ( ( longopt->has_arg != no_argument ) && ( ! argument ) )
00127                 argument = get_argv_argument ( argc, argv );
00128 
00129         /* If we need an argument but don't have one, sulk */
00130         if ( ( longopt->has_arg == required_argument ) && ( ! argument ) ) {
00131                 printf ( "Option \"%s\" requires an argument\n",
00132                          longopt->name );
00133                 *option = ':';
00134                 return 1;
00135         }
00136 
00137         /* If we have an argument where we shouldn't have one, sulk */
00138         if ( ( longopt->has_arg == no_argument ) && argument ) {
00139                 printf ( "Option \"%s\" takes no argument\n", longopt->name );
00140                 *option = ':';
00141                 return 1;
00142         }
00143 
00144         /* Store values and return success */
00145         optarg = ( char * ) argument;
00146         if ( longopt->flag ) {
00147                 *(longopt->flag) = longopt->val;
00148                 *option = 0;
00149         } else {
00150                 *option = longopt->val;
00151         }
00152         return 1;
00153 }
00154 
00155 /**
00156  * Match short option
00157  *
00158  * @v argc              Argument count
00159  * @v argv              Argument list
00160  * @v opttext           Option text within current argv[] element
00161  * @v shortopt          Option character from option specification
00162  * @ret option          Option to return from getopt()
00163  * @ret matched         Found a match for this short option
00164  */
00165 static int match_short_option ( int argc, char * const argv[],
00166                                 const char *opttext, int shortopt,
00167                                 enum getopt_argument_requirement has_arg,
00168                                 int *option ) {
00169         const char *argument = NULL;
00170 
00171         /* Compare option character */
00172         if ( *opttext != shortopt )
00173                 return 0;
00174 
00175         /* Consume option character */
00176         opttext++;
00177         nextchar++;
00178         if ( *opttext ) {
00179                 if ( has_arg != no_argument ) {
00180                         /* Consume remainder of element as inline argument */
00181                         argument = opttext;
00182                         optind++;
00183                         nextchar = 0;
00184                 }
00185         } else {
00186                 /* Reached end of argv element */
00187                 optind++;
00188                 nextchar = 0;
00189         }
00190 
00191         /* If we want an argument but don't have one yet, try to grab
00192          * the next argv element
00193          */
00194         if ( ( has_arg != no_argument ) && ( ! argument ) )
00195                 argument = get_argv_argument ( argc, argv );
00196 
00197         /* If we need an argument but don't have one, sulk */
00198         if ( ( has_arg == required_argument ) && ( ! argument ) ) {
00199                 printf ( "Option \"%c\" requires an argument\n", shortopt );
00200                 *option = ':';
00201                 return 1;
00202         }
00203 
00204         /* Store values and return success */
00205         optarg = ( char * ) argument;
00206         *option = shortopt;
00207         return 1;
00208 }
00209 
00210 /**
00211  * Parse command-line options
00212  *
00213  * @v argc              Argument count
00214  * @v argv              Argument list
00215  * @v optstring         Option specification string
00216  * @v longopts          Long option specification table
00217  * @ret longindex       Index of long option (or NULL)
00218  * @ret option          Option found, or -1 for no more options
00219  *
00220  * Note that the caller must arrange for reset_getopt() to be called
00221  * before each set of calls to getopt_long().  In Etherboot, this is
00222  * done automatically by execv().
00223  */
00224 int getopt_long ( int argc, char * const argv[], const char *optstring,
00225                   const struct option *longopts, int *longindex ) {
00226         const char *opttext = argv[optind];
00227         const struct option *longopt;
00228         int shortopt;
00229         enum getopt_argument_requirement has_arg;
00230         int option;
00231 
00232         /* Check for end of argv array */
00233         if ( optind >= argc )
00234                 return -1;
00235 
00236         /* Check for end of options */
00237         if ( *(opttext++) != '-' )
00238                 return -1;
00239 
00240         /* Check for long options */
00241         if ( *(opttext++) == '-' ) {
00242                 for ( longopt = longopts ; longopt->name ; longopt++ ) {
00243                         if ( ! match_long_option ( argc, argv, opttext,
00244                                                    longopt, &option ) )
00245                                 continue;
00246                         if ( longindex )
00247                                 *longindex = ( longopt - longopts );
00248                         return option;
00249                 }
00250                 optopt = '?';
00251                 printf ( "Unrecognised option \"--%s\"\n", opttext );
00252                 return '?';
00253         }
00254 
00255         /* Check for short options */
00256         if ( nextchar < 1 )
00257                 nextchar = 1;
00258         opttext = ( argv[optind] + nextchar );
00259         while ( ( shortopt = *(optstring++) ) ) {
00260                 has_arg = no_argument;
00261                 while ( *optstring == ':' ) {
00262                         has_arg++;
00263                         optstring++;
00264                 }
00265                 if ( match_short_option ( argc, argv, opttext, shortopt,
00266                                           has_arg, &option ) ) {
00267                         return option;
00268                 }
00269         }
00270         optopt = *opttext;
00271         printf ( "Unrecognised option \"-%c\"\n", optopt );
00272         return '?';
00273 }

Generated on Tue Apr 6 20:00:51 2010 for gPXE by  doxygen 1.5.7.1