Skip to content

Commit b06ff7b

Browse files
committed
src: add getopt option parser
1 parent 0d6d3dd commit b06ff7b

File tree

8 files changed

+1335
-279
lines changed

8 files changed

+1335
-279
lines changed

deps/getopt/getopt.c

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/* $NetBSD: getopt.c,v 1.29 2014/06/05 22:00:22 christos Exp $ */
2+
3+
/*
4+
* Copyright (c) 1987, 1993, 1994
5+
* The Regents of the University of California. All rights reserved.
6+
*
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions
9+
* are met:
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
* 3. Neither the name of the University nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software
17+
* without specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29+
* SUCH DAMAGE.
30+
*/
31+
32+
#if defined(LIBC_SCCS) && !defined(lint)
33+
static char sccsid[] = "@(#)getopt.c 8.3 (Berkeley) 4/27/95";
34+
#endif /* LIBC_SCCS and not lint */
35+
36+
#include <stdio.h>
37+
#include <stdlib.h>
38+
#include <string.h>
39+
40+
#ifdef WIN32
41+
42+
#ifdef _MSC_VER
43+
/* ignore MSVC++ warnings that are annoying and hard to remove:
44+
4702 unreachable code
45+
(there is an unreachable assert(0) in case somehow it is reached)
46+
*/
47+
#pragma warning( disable : 4702 )
48+
#endif
49+
50+
#else /* WIN32 */
51+
#include <sys/cdefs.h>
52+
#include <unistd.h>
53+
#endif
54+
55+
int opterr = 1, /* if error message should be printed */
56+
optind = 1, /* index into parent argv vector */
57+
optopt, /* character checked for validity */
58+
optreset; /* reset getopt */
59+
char *optarg; /* argument associated with option */
60+
61+
#define BADCH (int)'?'
62+
#define BADARG (int)':'
63+
#define EMSG ""
64+
65+
/*
66+
* getopt --
67+
* Parse argc/argv argument vector.
68+
*/
69+
int
70+
getopt(int nargc, char * const nargv[], const char *ostr)
71+
{
72+
static char *place = EMSG; /* option letter processing */
73+
char *oli; /* option letter list index */
74+
75+
if (optreset || *place == 0) { /* update scanning pointer */
76+
optreset = 0;
77+
place = nargv[optind];
78+
if (optind >= nargc || *place++ != '-') {
79+
/* Argument is absent or is not an option */
80+
place = EMSG;
81+
return (-1);
82+
}
83+
optopt = *place++;
84+
if (optopt == '-' && *place == 0) {
85+
/* "--" => end of options */
86+
++optind;
87+
place = EMSG;
88+
return (-1);
89+
}
90+
if (optopt == 0) {
91+
/* Solitary '-', treat as a '-' option
92+
if the program (eg su) is looking for it. */
93+
place = EMSG;
94+
if (strchr(ostr, '-') == NULL)
95+
return (-1);
96+
optopt = '-';
97+
}
98+
} else
99+
optopt = *place++;
100+
101+
/* See if option letter is one the caller wanted... */
102+
if (optopt == ':' || (oli = strchr(ostr, optopt)) == NULL) {
103+
if (*place == 0)
104+
++optind;
105+
if (opterr && *ostr != ':')
106+
(void)fprintf(stderr,
107+
"iojs: illegal option -- %c\n", optopt);
108+
return (BADCH);
109+
}
110+
111+
/* Does this option need an argument? */
112+
if (oli[1] != ':') {
113+
/* don't need argument */
114+
optarg = NULL;
115+
if (*place == 0)
116+
++optind;
117+
} else {
118+
/* Option-argument is either the rest of this argument or the
119+
entire next argument. */
120+
if (*place)
121+
optarg = place;
122+
else if (oli[2] == ':')
123+
/*
124+
* GNU Extension, for optional arguments if the rest of
125+
* the argument is empty, we return NULL
126+
*/
127+
optarg = NULL;
128+
else if (nargc > ++optind)
129+
optarg = nargv[optind];
130+
else {
131+
/* option-argument absent */
132+
place = EMSG;
133+
if (*ostr == ':')
134+
return (BADARG);
135+
if (opterr)
136+
(void)fprintf(stderr,
137+
"iojs: option requires an argument -- %c\n", optopt);
138+
return (BADCH);
139+
}
140+
place = EMSG;
141+
++optind;
142+
}
143+
return (optopt); /* return option letter */
144+
}

deps/getopt/getopt.gyp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
'targets': [
3+
{
4+
'target_name': 'getopt',
5+
'type': 'static_library',
6+
'sources': [
7+
'getopt.h',
8+
'getopt.c',
9+
'getopt_long.c'
10+
]
11+
}
12+
]
13+
}

deps/getopt/getopt.h

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* $NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $ */
2+
/* $FreeBSD$ */
3+
4+
/*-
5+
* Copyright (c) 2000 The NetBSD Foundation, Inc.
6+
* All rights reserved.
7+
*
8+
* This code is derived from software contributed to The NetBSD Foundation
9+
* by Dieter Baron and Thomas Klausner.
10+
*
11+
* Redistribution and use in source and binary forms, with or without
12+
* modification, are permitted provided that the following conditions
13+
* are met:
14+
* 1. Redistributions of source code must retain the above copyright
15+
* notice, this list of conditions and the following disclaimer.
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in the
18+
* documentation and/or other materials provided with the distribution.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21+
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23+
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24+
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30+
* POSSIBILITY OF SUCH DAMAGE.
31+
*/
32+
33+
#ifndef _GETOPT_H_
34+
#define _GETOPT_H_
35+
36+
#ifdef WIN32
37+
#ifdef __cplusplus
38+
#define __BEGIN_DECLS extern "C" {
39+
#define __END_DECLS }
40+
#else
41+
#define __BEGIN_DECLS
42+
#define __END_DECLS
43+
#endif
44+
45+
#ifdef _MSC_VER
46+
/* ignore MSVC++ warnings that are annoying and hard to remove:
47+
4702 unreachable code
48+
(there is an unreachable assert(0) in case somehow it is reached)
49+
*/
50+
#pragma warning( disable : 4702 )
51+
#endif
52+
53+
#else /* WIN32 */
54+
#include <sys/cdefs.h>
55+
#endif
56+
57+
/*
58+
* GNU-like getopt_long()/getopt_long_only() with 4.4BSD optreset extension.
59+
* getopt() is declared here too for GNU programs.
60+
*/
61+
#define no_argument 0
62+
#define required_argument 1
63+
#define optional_argument 2
64+
65+
struct option {
66+
/* name of long option */
67+
const char *name;
68+
/*
69+
* one of no_argument, required_argument, and optional_argument:
70+
* whether option takes an argument
71+
*/
72+
int has_arg;
73+
/* if not NULL, set *flag to val when option found */
74+
int *flag;
75+
/* if flag not NULL, value to set *flag to; else return value */
76+
int val;
77+
78+
/* add description for usage */
79+
const char *desc;
80+
};
81+
82+
__BEGIN_DECLS
83+
int getopt_long(int, char * const *, const char *,
84+
const struct option *, int *);
85+
int getopt_long_only(int, char * const *, const char *,
86+
const struct option *, int *);
87+
#ifndef _GETOPT_DECLARED
88+
#define _GETOPT_DECLARED
89+
int getopt(int, char * const [], const char *);
90+
91+
extern char *optarg; /* getopt(3) external variables */
92+
extern int optind, opterr, optopt;
93+
#endif
94+
#ifndef _OPTRESET_DECLARED
95+
#define _OPTRESET_DECLARED
96+
extern int optreset; /* getopt(3) external variable */
97+
#endif
98+
__END_DECLS
99+
100+
#endif /* !_GETOPT_H_ */

0 commit comments

Comments
 (0)