| 1 |
/* $NetBSD: getopt.c,v 1.1 2009/03/22 22:33:13 joerg Exp $*/ |
| 2 |
/* Modified by David Anderson to work with GNU/Linux and freebsd. |
| 3 |
Added {} for clarity. |
| 4 |
Switched to standard dwarfdump formatting. |
| 5 |
Treatment of : modified so that :: gets dwoptarg NULL |
| 6 |
if space follows the letter |
| 7 |
(the dwoptarg is set to null). |
| 8 |
renamed to make it clear this is a private version. |
| 9 |
Oct 17 2017: Created dwgetopt_long(). See dwgetopt.h |
| 10 |
*/ |
| 11 |
/* |
| 12 |
* Copyright (c) 1987, 1993, 1994 |
| 13 |
* The Regents of the University of California. All rights reserved. |
| 14 |
* |
| 15 |
* Redistribution and use in source and binary forms, with or without |
| 16 |
* modification, are permitted provided that the following conditions |
| 17 |
* are met: |
| 18 |
* 1. Redistributions of source code must retain the above copyright |
| 19 |
* notice, this list of conditions and the following disclaimer. |
| 20 |
* 2. Redistributions in binary form must reproduce the above |
| 21 |
* copyright notice, this list of conditions and the following |
| 22 |
* disclaimer in the documentation and/or other materials |
| 23 |
* provided with the distribution. |
| 24 |
* 3. Neither the name of the University nor the names of its |
| 25 |
* contributors may be used to endorse or promote products |
| 26 |
* derived from this software without specific prior written |
| 27 |
* permission. |
| 28 |
* |
| 29 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' |
| 30 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 31 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 32 |
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 33 |
* SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 34 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 35 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 36 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 37 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 38 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 39 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 40 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 41 |
* POSSIBILITY OF SUCH DAMAGE. |
| 42 |
*/ |
| 43 |
|
| 44 |
/* This does not presently handle the option string |
| 45 |
leading + or leading - features. Such are not used |
| 46 |
by by libdwarfdump. Nor does it understand the |
| 47 |
GNU Env var POSIXLY_CORRECT . |
| 48 |
It does know of the leading ":" in the option string. |
| 49 |
See BADCH below. |
| 50 |
*/ |
| 51 |
|
| 52 |
#include "config.h" |
| 53 |
#include <stdio.h> |
| 54 |
#include <string.h> /* for strchr etc */ |
| 55 |
#include <stdlib.h> /* for exit() */ |
| 56 |
#include "dg_getopt.h" |
| 57 |
|
| 58 |
#define STRIP_OFF_CONSTNESS(a) ((char *)(a)) |
| 59 |
|
| 60 |
int dwopterr = 1, /* if error message should be printed */ |
| 61 |
dwoptind = 1, /* index into parent argv vector */ |
| 62 |
dwoptopt, /* character checked for validity */ |
| 63 |
dwoptreset; /* reset getopt */ |
| 64 |
char *dwoptarg; /* argument associated with option */ |
| 65 |
|
| 66 |
#define BADCH (int)'?' |
| 67 |
#define BADARG (int)':' |
| 68 |
#define EMSG "" |
| 69 |
|
| 70 |
#define TRUE 1 |
| 71 |
#define FALSE 0 |
| 72 |
|
| 73 |
#if 0 /* FOR DEBUGGING ONLY */ |
| 74 |
/* Use for testing dwgetopt only. |
| 75 |
Not a standard function. */ |
| 76 |
void |
| 77 |
dwgetoptresetfortestingonly(void) |
| 78 |
{ |
| 79 |
dwopterr = 1; |
| 80 |
dwoptind = 1; |
| 81 |
dwoptopt = 0; |
| 82 |
dwoptreset = 0; |
| 83 |
dwoptarg = 0; |
| 84 |
} |
| 85 |
#endif /* FOR DEBUGGING ONLY */ |
| 86 |
|
| 87 |
static const char *place = EMSG;/* option letter processing */ |
| 88 |
|
| 89 |
/* Post Condition: |
| 90 |
if return FALSE then *argerr is set false. */ |
| 91 |
static int |
| 92 |
dwoptnamematches( |
| 93 |
const struct dwoption *dwlopt, |
| 94 |
const char *iplace, |
| 95 |
const char **argloc, |
| 96 |
int *argerr) |
| 97 |
{ |
| 98 |
|
| 99 |
const char *eq = 0; |
| 100 |
unsigned namelen = 0; |
| 101 |
unsigned arglen = 0; |
| 102 |
int d = 0; |
| 103 |
|
| 104 |
for (eq = iplace; *eq; ++eq) { |
| 105 |
if (*eq != '=') { |
| 106 |
continue; |
| 107 |
} |
| 108 |
/* Found =, arg should follow */ |
| 109 |
namelen = (eq - iplace); |
| 110 |
if (namelen != (unsigned)strlen(dwlopt->name)) { |
| 111 |
return FALSE; |
| 112 |
} |
| 113 |
eq++; |
| 114 |
arglen = strlen(eq); |
| 115 |
break; |
| 116 |
} |
| 117 |
if (namelen) { |
| 118 |
d = strncmp(iplace,dwlopt->name,namelen); |
| 119 |
if (d) { |
| 120 |
return FALSE; |
| 121 |
} |
| 122 |
if (dwlopt->has_arg == 0) { |
| 123 |
*argerr = TRUE; |
| 124 |
return TRUE; |
| 125 |
} |
| 126 |
if (arglen) { |
| 127 |
/* Discarding const, avoiding warning. |
| 128 |
Data is in user space, so this is ok. */ |
| 129 |
dwoptarg = (char *)eq; |
| 130 |
*argloc = (const char *)eq; |
| 131 |
} else { |
| 132 |
/* Has arg = but arg is empty. */ |
| 133 |
dwoptarg = 0; |
| 134 |
} |
| 135 |
return TRUE; |
| 136 |
} else { |
| 137 |
d = strcmp(iplace,dwlopt->name); |
| 138 |
if (d) { |
| 139 |
return FALSE; |
| 140 |
} |
| 141 |
if (dwlopt->has_arg == 1) { |
| 142 |
*argerr = TRUE; |
| 143 |
return TRUE; |
| 144 |
} |
| 145 |
dwoptarg = 0; |
| 146 |
return TRUE; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
/* dwgetopt_long |
| 151 |
A reimplemention of a portion of |
| 152 |
the getopt(3) GNU/Linux getopt_long(). |
| 153 |
See dwgetopt.h for more details. |
| 154 |
*/ |
| 155 |
int dwgetopt_long(int nargc, char *const nargv[], |
| 156 |
const char *ostr, |
| 157 |
const struct dwoption* longopts, |
| 158 |
int *longindex) |
| 159 |
{ |
| 160 |
char *lplace = 0; |
| 161 |
if (dwoptreset) { |
| 162 |
/* Not really supported. */ |
| 163 |
place = EMSG; |
| 164 |
return (-1); |
| 165 |
} |
| 166 |
if (*place) { |
| 167 |
int v = dwgetopt(nargc,nargv,ostr); |
| 168 |
return v; |
| 169 |
} |
| 170 |
/* Use local lplace in case we need to call getopt() |
| 171 |
just below. */ |
| 172 |
lplace = nargv[dwoptind]; |
| 173 |
if (dwoptind >= nargc || *lplace++ != '-') { |
| 174 |
/* Argument is absent or is not an option */ |
| 175 |
place = EMSG; |
| 176 |
return (-1); |
| 177 |
} |
| 178 |
if (*lplace != '-') { |
| 179 |
/* Notice place not disturbed. */ |
| 180 |
int v = dwgetopt(nargc,nargv,ostr); |
| 181 |
return v; |
| 182 |
} |
| 183 |
/* Starts with two dashes. |
| 184 |
Now we set the global place */ |
| 185 |
place = lplace+1; |
| 186 |
if (!*place) { |
| 187 |
/* "--" => end of options */ |
| 188 |
++dwoptind; |
| 189 |
place = EMSG; |
| 190 |
return (-1); |
| 191 |
} |
| 192 |
|
| 193 |
/* We think this is a longopt. */ |
| 194 |
{ |
| 195 |
int lo_num = 0; |
| 196 |
int dwlopt_val = 0; |
| 197 |
|
| 198 |
for (;;lo_num++) { |
| 199 |
const struct dwoption *dwlopt = longopts +lo_num; |
| 200 |
const char * argloc = 0; |
| 201 |
int argerr = 0; |
| 202 |
int resmatch = 0; |
| 203 |
|
| 204 |
if (!dwlopt->name) { |
| 205 |
dwoptind++; |
| 206 |
(void)fprintf(stderr, |
| 207 |
"%s: invalid long option '--%s'\n", |
| 208 |
nargv[0]?nargv[0]:"", |
| 209 |
place); |
| 210 |
/* Leave longindex unchanged. */ |
| 211 |
place = EMSG; |
| 212 |
return (BADCH); |
| 213 |
} |
| 214 |
resmatch= dwoptnamematches(dwlopt,place, |
| 215 |
&argloc,&argerr); |
| 216 |
if (resmatch) { |
| 217 |
dwoptarg = 0; |
| 218 |
if (argloc) { |
| 219 |
/* Must drop const here. Ugh. */ |
| 220 |
dwoptarg = (char *)argloc; |
| 221 |
} |
| 222 |
} |
| 223 |
if (argerr) { |
| 224 |
/* resmatch == TRUE |
| 225 |
|
| 226 |
arg option missing if required, present |
| 227 |
but not allowed. |
| 228 |
GNU Behavior not well documented. |
| 229 |
Had to experiment. |
| 230 |
|
| 231 |
if argument-not-allowed, and we have one, |
| 232 |
do ??? |
| 233 |
|
| 234 |
If argument-required, |
| 235 |
then here GNU |
| 236 |
would take the next argv as the argument. |
| 237 |
we are not currently doing that. */ |
| 238 |
/**longindex = lo_num; */ |
| 239 |
if (dwlopt->has_arg) { |
| 240 |
/* Missing required arg, this does not |
| 241 |
match GNU getopt_long behavior |
| 242 |
of taking next argv as the arg value. |
| 243 |
and thus making getopt_long succeed. */ |
| 244 |
(void)fprintf(stderr, |
| 245 |
"%s: missing required long option " |
| 246 |
"argument '--%s'\n", |
| 247 |
nargv[0]?nargv[0]:"", |
| 248 |
place); |
| 249 |
} else { |
| 250 |
/* has arg but should not */ |
| 251 |
(void)fprintf(stderr, |
| 252 |
"%s: option '--%s' does not allow an argument\n", |
| 253 |
nargv[0]?nargv[0]:"", |
| 254 |
dwlopt->name); |
| 255 |
} |
| 256 |
dwoptind++; |
| 257 |
place = EMSG; |
| 258 |
return (BADCH); |
| 259 |
} |
| 260 |
if (resmatch) { |
| 261 |
dwlopt_val = dwlopt->val; |
| 262 |
break; |
| 263 |
} |
| 264 |
} |
| 265 |
*longindex = lo_num; |
| 266 |
place = EMSG; |
| 267 |
dwoptind++; |
| 268 |
return dwlopt_val; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
/* |
| 273 |
* getopt -- |
| 274 |
* Parse argc/argv argument vector. |
| 275 |
* a: means |
| 276 |
* -afoo |
| 277 |
* -a foo |
| 278 |
* and 'foo' is returned in dwoptarg |
| 279 |
* b:: means |
| 280 |
* -b |
| 281 |
* and dwoptarg is null |
| 282 |
* -bother |
| 283 |
* and dwoptarg is 'other' |
| 284 |
*/ |
| 285 |
int |
| 286 |
dwgetopt(int nargc, char * const nargv[], const char *ostr) |
| 287 |
{ |
| 288 |
char *oli; /* option letter list index */ |
| 289 |
|
| 290 |
if (dwoptreset || *place == 0) { /* update scanning pointer */ |
| 291 |
dwoptreset = 0; |
| 292 |
place = nargv[dwoptind]; |
| 293 |
|
| 294 |
if (dwoptind >= nargc || *place++ != '-') { |
| 295 |
/* Argument is absent or is not an option */ |
| 296 |
place = EMSG; |
| 297 |
return (-1); |
| 298 |
} |
| 299 |
dwoptopt = *place++; |
| 300 |
if (dwoptopt == '-' && *place == 0) { |
| 301 |
/* "--" => end of options */ |
| 302 |
++dwoptind; |
| 303 |
place = EMSG; |
| 304 |
return (-1); |
| 305 |
} |
| 306 |
if (dwoptopt == 0) { |
| 307 |
/* Solitary '-', treat as a '-' option |
| 308 |
if the program (eg su) is looking for it. */ |
| 309 |
place = EMSG; |
| 310 |
if (strchr(ostr, '-') == NULL) { |
| 311 |
return -1; |
| 312 |
} |
| 313 |
dwoptopt = '-'; |
| 314 |
} |
| 315 |
} else { |
| 316 |
dwoptopt = *place++; |
| 317 |
} |
| 318 |
/* See if option letter is one the caller wanted... */ |
| 319 |
if (dwoptopt == ':' || |
| 320 |
(oli = strchr((char *)ostr, dwoptopt)) == NULL) { |
| 321 |
if (*place == 0) { |
| 322 |
++dwoptind; |
| 323 |
} |
| 324 |
if (dwopterr && *ostr != ':') { |
| 325 |
(void)fprintf(stderr, |
| 326 |
"%s: invalid option -- '%c'\n", |
| 327 |
nargv[0]?nargv[0]:"", |
| 328 |
dwoptopt); |
| 329 |
} |
| 330 |
return (BADCH); |
| 331 |
} |
| 332 |
|
| 333 |
/* Does this option need an argument? */ |
| 334 |
if (oli[1] != ':') { |
| 335 |
/* don't need argument */ |
| 336 |
dwoptarg = NULL; |
| 337 |
if (*place == 0) { |
| 338 |
++dwoptind; |
| 339 |
} |
| 340 |
} else { |
| 341 |
int reqnextarg = 1; |
| 342 |
if (oli[1] && (oli[2] == ':')) { |
| 343 |
/* Pair of :: means special treatment of dwoptarg */ |
| 344 |
reqnextarg = 0; |
| 345 |
} |
| 346 |
/* Option-argument is either the rest of this argument or the |
| 347 |
entire next argument. */ |
| 348 |
if (*place ) { |
| 349 |
/* Whether : or :: */ |
| 350 |
dwoptarg = STRIP_OFF_CONSTNESS(place); |
| 351 |
} else if (reqnextarg) { |
| 352 |
/* ! *place */ |
| 353 |
if (nargc > (++dwoptind)) { |
| 354 |
dwoptarg = nargv[dwoptind]; |
| 355 |
} else { |
| 356 |
place=EMSG; |
| 357 |
/* Next arg required, but is missing */ |
| 358 |
if (*ostr == ':') { |
| 359 |
/* Leading : in ostr calls for BADARG return. */ |
| 360 |
return (BADARG); |
| 361 |
} |
| 362 |
if (dwopterr) { |
| 363 |
(void)fprintf(stderr, |
| 364 |
"%s: option requires an argument. -- '%c'\n", |
| 365 |
nargv[0]?nargv[0]:"", |
| 366 |
dwoptopt); |
| 367 |
} |
| 368 |
return (BADCH); |
| 369 |
} |
| 370 |
} else { |
| 371 |
/* ! *place */ |
| 372 |
/* The key part of :: treatment. */ |
| 373 |
dwoptarg = NULL; |
| 374 |
} |
| 375 |
place = EMSG; |
| 376 |
++dwoptind; |
| 377 |
} |
| 378 |
return (dwoptopt); /* return option letter */ |
| 379 |
} |