1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
|
/** @file getopt_long.c
** @brief getopt_long - Definition
** @author Andrea Vedaldi
**/
/*
Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
All rights reserved.
This file is part of the VLFeat library and is made available under
the terms of the BSD license (see the COPYING file).
*/
/**
@file getopt_long.h
@brief getopt_long
@author Andrea Vedaldi
This is a drop-in replacament of GNU getopt_long meant to be used
on platforms that do not support such functionality.
**/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "generic.h"
#include "getopt_long.h"
int opterr = 1 ;
int optind = 1 ;
int optopt ;
char * optarg ;
int optreset ;
#define BADCH '?'
#define BADARG ':'
#define EEND -1
#define EMSG ""
/** @brief Parse long options (BSD style)
** @param argc number of arguments.
** @param argv pointer to the vector of arguments.
** @param optstring list of abbreviated options
** @param longopts list of long options.
** @param longindex index of current option in @a longopts.
** @return the code of the next option.
**
** This function extract long and short options from the argument
** list @a argv of @a argc entries.
**
** A short options sequence is introduced by a single dash character
** @c -. Each short option is described by a single character in the
** string @a optstring, possibly followed by a @c : character to
** denote a (mandatory) argument of the short option. A short option
** with an argument cannot appear in the middle of a short option
** sequence, but only at the end.
**
** A long option is introduced by a double dash @c --. Each long
** option is described by an instance of the ::option structure in
** the @a longopts table (the last entry must be filled with zeroes
** to denote the end).
**
** Illegal options and missing arguments cause the function to skip
** the option and return '?'. If ::opterr is @c true (default), the
** function prints an error message to @a stderr. Finally, if @a
** optstring has a leading @c :, then error messages are suppressed
** and a missing argument causes @a : to be returned.
**
** @remark The function is currently <em>not</em> thread safe.
**/
VL_EXPORT int
getopt_long(int argc, char *const argv[],
const char *optstring,
const struct option * longopts,
int *longindex)
{
static char *place = EMSG; /* option letter processing */
static int optbegin = 0 ;
static int optend = 0 ;
char *oli; /* option letter list index */
int has_colon = 0 ;
int ret_val = 0 ;
/*
A semicolon at the beginning of optstring has a special meaning.
If we find one, we annote and remove it.
*/
has_colon = optstring && optstring[0] == ':' ;
if (has_colon) ++ optstring ;
/*
Here we are either processing a short option sequence or
we start processing a new option. This is indicated by optreset.
*/
if (optreset || *place == '\0') {
/* ---------------------------------------------------------------
* Look for next short/long option
* ------------------------------------------------------------ */
optreset = 0 ;
/* no more arguments ? */
if (optind >= argc) {
place = EMSG ;
return -1 ;
}
/* next argument that may hold an option */
optbegin = optind ;
/* ---------------------------------------------------------------
* Look for an option to parse
* ------------------------------------------------------------ */
parse_option_at_optbegin :
/* place points to the candidate option */
place = argv [optbegin] ;
/* an option is introduced by '-' */
if (place [0] != '-') {
/* this argument is not an option: try next argument */
++ optbegin ;
if (optbegin >= argc) {
/* no more arguments to look for options */
place = EMSG ;
return -1 ;
}
goto parse_option_at_optbegin ;
}
/* consume leading `-' */
++ place ;
/* assume the option is composed of one argument only */
optend = optbegin + 1 ;
/* assume no argument */
optarg = 0 ;
/* ---------------------------------------------------------------
* option `--'
* ------------------------------------------------------------ */
/* this special option (void long option) ends the option processing */
if (place[0] &&
place[0] == '-' &&
place[1] == '\0') {
optind = optend ;
place = EMSG ;
ret_val = -1 ;
goto done_option ;
}
/* ---------------------------------------------------------------
* long option
* ------------------------------------------------------------ */
if (place[0] &&
place[0] == '-' &&
place[1] ) {
size_t namelen ;
int i ;
/* consume second `-' */
++ place ;
/* count characters before `=' */
namelen = strcspn(place, "=") ;
/* scan longopts for this option */
for (i = 0 ; longopts[i].name != NULL ; ++ i) {
if (strlen ( longopts[i].name) == namelen &&
strncmp (place, longopts[i].name, namelen) == 0 ) {
/* save back long option index */
if (longindex) *longindex = i ;
/* process long option argument */
if (longopts[i].has_arg == required_argument ||
longopts[i].has_arg == optional_argument) {
/* --option=value style */
if (place[namelen] == '=') {
optarg = place + namelen + 1 ;
}
/* --option value style (only required_argument) */
else if (longopts[i].has_arg == required_argument) {
/* missing argument ? */
if (optbegin >= argc - 1) {
if (! has_colon && opterr)
fprintf(stderr,
"%s: option requires an argument -- %s\n",
argv[0], place);
place = EMSG ;
ret_val = has_colon ? BADARG : BADCH ;
goto done_option ;
}
optarg = argv [optend] ;
++ optend ;
}
}
/* determine return value */
if (longopts[i].flag == NULL) {
ret_val = longopts[i].val ;
}
else {
*longopts[i].flag = longopts[i].val;
ret_val = 0 ;
}
/* mark sequence closed */
place = EMSG ;
goto done_option ;
} /* if match */
} /* scan longoptions */
/* no matching option found */
if (! has_colon && opterr)
fprintf(stderr,
"%s: illegal option -- %s\n", argv[0], place) ;
place = EMSG ;
ret_val = BADCH ;
goto done_option ;
}
} /* end new option */
/* -----------------------------------------------------------------
* Finish short option sequence
* -------------------------------------------------------------- */
optopt = (int) *place++ ;
/* search charcater in option list */
oli = strchr(optstring, optopt);
/* short option not found */
if (!oli) {
if (! has_colon && opterr)
fprintf(stderr,
"%s: illegal option -- %c\n",
argv[0], optopt);
if (*place) {
/* more short options in the list */
return BADCH ;
}
else {
/* error occured as last option in the list */
place = EMSG ;
ret_val = BADCH ;
goto done_option ;
}
} /* end short option not found */
if (oli[1] != ':') {
/* short option with no argument */
if (*place) {
/* more short options in the list */
return optopt ;
}
else {
/* last option in the list */
place = EMSG ;
ret_val = optopt ;
goto done_option ;
}
} else {
/* short option with argument */
/* -ovalue style */
if (*place) {
optarg = place ;
place = EMSG ;
ret_val = optopt ;
goto done_option ;
}
/* -o value style: missing argument */
else if (optbegin >= argc - 1) {
if (! has_colon && opterr)
fprintf(stderr,
"%s: option requires an argument -- %c\n",
argv[0], optopt);
place = EMSG ;
ret_val = has_colon ? BADARG : BADCH ;
goto done_option ;
}
/* -o value style: process argument */
optarg = argv [optend] ;
++ optend ;
place = EMSG ;
ret_val = optopt ;
goto done_option ;
} /* short with argument */
done_option :
{
int pos = optend - optbegin ; /* n of circular shifts */
int c = pos ;
while (c --) {
int i ;
char *tmp = argv [optend - 1] ;
for (i = optend - 1 ; i > optind ; -- i) {
((char**)argv) [i] = argv [i-1] ;
}
((char**)argv) [optind] = tmp ;
}
optind += pos ;
}
return ret_val ;
}
|