#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include <getopt.h>
Go to the source code of this file.
Functions | |
| FILE_LICENCE (GPL2_OR_LATER) | |
| static const char * | get_argv_argument (int argc, char *const argv[]) |
| Get option argument from argv[] array. | |
| static int | match_long_option (int argc, char *const argv[], const char *opttext, const struct option *longopt, int *option) |
| Match long option. | |
| static int | match_short_option (int argc, char *const argv[], const char *opttext, int shortopt, enum getopt_argument_requirement has_arg, int *option) |
| Match short option. | |
| int | getopt_long (int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex) |
| Parse command-line options. | |
Variables | |
| char * | optarg |
| Option argument. | |
| int | optind |
| Current option index. | |
| int | nextchar |
| Current option character index. | |
| int | optopt |
| Unrecognised option. | |
Definition in file getopt.c.
| FILE_LICENCE | ( | GPL2_OR_LATER | ) |
| static const char* get_argv_argument | ( | int | argc, | |
| char *const | argv[] | |||
| ) | [static] |
Get option argument from argv[] array.
| argc | Argument count | |
| argv | Argument list |
| argument | Option argument, or NULL |
Consume this argv element, and return it
Definition at line 72 of file getopt.c.
Referenced by match_long_option(), and match_short_option().
00072 { 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 }
| static int match_long_option | ( | int | argc, | |
| char *const | argv[], | |||
| const char * | opttext, | |||
| const struct option * | longopt, | |||
| int * | option | |||
| ) | [static] |
Match long option.
| argc | Argument count | |
| argv | Argument list | |
| opttext | Option text within current argv[] element | |
| longopt | Long option specification |
Definition at line 101 of file getopt.c.
References option::flag, get_argv_argument(), option::has_arg, option::name, no_argument, NULL, optarg, optind, printf(), required_argument, strlen(), strncmp(), and option::val.
Referenced by getopt_long().
00103 { 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 }
| static int match_short_option | ( | int | argc, | |
| char *const | argv[], | |||
| const char * | opttext, | |||
| int | shortopt, | |||
| enum getopt_argument_requirement | has_arg, | |||
| int * | option | |||
| ) | [static] |
Match short option.
| argc | Argument count | |
| argv | Argument list | |
| opttext | Option text within current argv[] element | |
| shortopt | Option character from option specification |
Definition at line 165 of file getopt.c.
References get_argv_argument(), nextchar, no_argument, NULL, optarg, optind, printf(), and required_argument.
Referenced by getopt_long().
00168 { 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 }
| int getopt_long | ( | int | argc, | |
| char *const | argv[], | |||
| const char * | optstring, | |||
| const struct option * | longopts, | |||
| int * | longindex | |||
| ) |
Parse command-line options.
| argc | Argument count | |
| argv | Argument list | |
| optstring | Option specification string | |
| longopts | Long option specification table |
| longindex | Index of long option (or NULL) | |
| option | Option found, or -1 for no more options |
Definition at line 224 of file getopt.c.
References option::has_arg, match_long_option(), match_short_option(), option::name, nextchar, no_argument, optind, optopt, and printf().
Referenced by dhcp_exec(), gdbstub_exec(), getopt(), ifcommon_exec(), imgargs_exec(), imgexec_exec(), imgfetch_core_exec(), imgfree_exec(), imgload_exec(), imgstat_exec(), pxebs_exec(), route_exec(), and sanboot_exec().
00225 { 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 }
| char* optarg |
Option argument.
This will point to the argument for the most recently returned option, if applicable.
Definition at line 38 of file getopt.c.
Referenced by imgfetch_core_exec(), match_long_option(), and match_short_option().
| int optind |
| int nextchar |
| int optopt |
1.5.7.1