| 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 |
*/ |
| 9 |
/* |
| 10 |
* Copyright (c) 1987, 1993, 1994 |
| 11 |
* The Regents of the University of California. All rights reserved. |
| 12 |
* |
| 13 |
* Redistribution and use in source and binary forms, with or without |
| 14 |
* modification, are permitted provided that the following conditions |
| 15 |
* are met: |
| 16 |
* 1. Redistributions of source code must retain the above copyright |
| 17 |
* notice, this list of conditions and the following disclaimer. |
| 18 |
* 2. Redistributions in binary form must reproduce the above |
| 19 |
* copyright notice, this list of conditions and the following |
| 20 |
* disclaimer in the documentation and/or other materials |
| 21 |
* provided with the distribution. |
| 22 |
* 3. Neither the name of the University nor the names of its |
| 23 |
* contributors may be used to endorse or promote products |
| 24 |
* derived from this software without specific prior written |
| 25 |
* permission. |
| 26 |
* |
| 27 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' |
| 28 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 29 |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 30 |
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 31 |
* REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 32 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 33 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 34 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 35 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 36 |
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 37 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 38 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 39 |
* POSSIBILITY OF SUCH DAMAGE. |
| 40 |
*/ |
| 41 |
|
| 42 |
#ifdef __cplusplus |
| 43 |
extern "C" { |
| 44 |
#endif /* __cplusplus */ |
| 45 |
extern int dwopterr; |
| 46 |
extern int dwoptind; |
| 47 |
extern int dwoptopt; |
| 48 |
extern int dwoptreset; |
| 49 |
extern char *dwoptarg; |
| 50 |
|
| 51 |
int dwgetopt(int nargc, char * const nargv[], const char *ostr); |
| 52 |
|
| 53 |
/* As of October 2017 it seems advisable to allow |
| 54 |
long option names. So based on a reading of |
| 55 |
'man 3 getopt' we reimplement a portion of GNU getopt_long(). |
| 56 |
It's a wonderfully sensible design and all the credit |
| 57 |
should go to the original designers. |
| 58 |
We are not implementing all the features of GNU/Linux |
| 59 |
getopt_long(), just the features we wish to use. |
| 60 |
Specifically, we require val be 0 and flag |
| 61 |
be NULL and ignore those fields. |
| 62 |
We do not implement GNU digit_optind at all. |
| 63 |
Within these restrictions the interface behaves the same |
| 64 |
as GNU getopt_long() (or so it appears from the |
| 65 |
getopt documentation: |
| 66 |
release 4.04 of the Linux man-pages project, |
| 67 |
GETOPT(3), |
| 68 |
http://www.kernel.org/doc/man-pages/). |
| 69 |
*/ |
| 70 |
|
| 71 |
struct dwoption { |
| 72 |
const char *name; |
| 73 |
int has_arg; |
| 74 |
int *flag; |
| 75 |
int val; |
| 76 |
}; |
| 77 |
#define dwno_argument 0 |
| 78 |
#define dwrequired_argument 1 |
| 79 |
#define dwoptional_argument 2 |
| 80 |
|
| 81 |
int dwgetopt_long(int nargc, char *const nargv[], |
| 82 |
const char *ostr, |
| 83 |
const struct dwoption* longopts, |
| 84 |
int *longindex); |
| 85 |
|
| 86 |
#ifdef __cplusplus |
| 87 |
} |
| 88 |
#endif /* __cplusplus */ |