1 #ifdef _MSC_VER
2 #ifndef __GETOPT_H__
3 /**
4  * DISCLAIMER
5  * This file is part of the mingw-w64 runtime package.
6  *
7  * The mingw-w64 runtime package and its code is distributed in the hope that it
8  * will be useful but WITHOUT ANY WARRANTY.  ALL WARRANTIES, EXPRESSED OR
9  * IMPLIED ARE HEREBY DISCLAIMED.  This includes but is not limited to
10  * warranties of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  */
12 /*
13  * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com>
14  *
15  * Permission to use, copy, modify, and distribute this software for any
16  * purpose with or without fee is hereby granted, provided that the above
17  * copyright notice and this permission notice appear in all copies.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
20  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
21  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
22  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
23  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
24  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
25  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26  *
27  * Sponsored in part by the Defense Advanced Research Projects
28  * Agency (DARPA) and Air Force Research Laboratory, Air Force
29  * Materiel Command, USAF, under agreement number F39502-99-1-0512.
30  */
31 /*-
32  * Copyright (c) 2000 The NetBSD Foundation, Inc.
33  * All rights reserved.
34  *
35  * This code is derived from software contributed to The NetBSD Foundation
36  * by Dieter Baron and Thomas Klausner.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
41  * 1. Redistributions of source code must retain the above copyright
42  *    notice, this list of conditions and the following disclaimer.
43  * 2. Redistributions in binary form must reproduce the above copyright
44  *    notice, this list of conditions and the following disclaimer in the
45  *    documentation and/or other materials provided with the distribution.
46  *
47  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
48  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
49  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
50  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
51  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57  * POSSIBILITY OF SUCH DAMAGE.
58  */
59 
60 #define __GETOPT_H__
61 
62 /* All the headers include this file. */
63 #include <crtdefs.h>
64 #include <errno.h>
65 #include <stdarg.h>
66 #include <stdio.h>
67 #include <stdlib.h>
68 #include <string.h>
69 #define WIN32_LEAN_AND_MEAN
70 #include <windows.h>
71 
72 #ifdef __cplusplus
73 extern "C" {
74 #endif
75 
76 #define REPLACE_GETOPT /* use this getopt as the system getopt(3) */
77 
78 //extern int optind;		/* index of first non-option in argv      */
79 //extern int optopt;		/* single option character, as parsed     */
80 //extern int opterr;		/* flag to enable built-in diagnostics... */
81 //				/* (user may set to zero, to suppress)    */
82 //
83 //extern char *optarg;		/* pointer to argument of current option  */
84 
85 #define PRINT_ERROR ((opterr) && (*options != ':'))
86 
87 #define FLAG_PERMUTE 0x01 /* permute non-options to the end of argv */
88 #define FLAG_ALLARGS 0x02 /* treat non-options as args to option "-1" */
89 #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */
90 
91 /* return values */
92 #define BADCH (int)'?'
93 #define BADARG ((*options == ':') ? (int)':' : (int)'?')
94 #define INORDER (int)1
95 
96 #ifndef __CYGWIN__
97 #define __progname __argv[0]
98 #else
99 extern char __declspec(dllimport) * __progname;
100 #endif
101 
102 #ifdef __CYGWIN__
103 static char EMSG[] = "";
104 #else
105 #define EMSG ""
106 #endif
107 
108 static int getopt_internal(int, char* const*, const char*,
109     const struct option*, int*, int);
110 static int parse_long_options(char* const*, const char*,
111     const struct option*, int*, int);
112 static int gcd(int, int);
113 static void permute_args(int, int, int, char* const*);
114 
115 static char* place = EMSG; /* option letter processing */
116 
117 /* XXX: set optreset to 1 rather than these two */
118 static int nonopt_start = -1; /* first non option argument (for permute) */
119 static int nonopt_end = -1; /* first option after non options (for permute) */
120 
121 /* Error messages */
122 static const char recargchar[] = "option requires an argument -- %c";
123 static const char recargstring[] = "option requires an argument -- %s";
124 static const char ambig[] = "ambiguous option -- %.*s";
125 static const char noarg[] = "option doesn't take an argument -- %.*s";
126 static const char illoptchar[] = "unknown option -- %c";
127 static const char illoptstring[] = "unknown option -- %s";
128 
129 static void _vwarnx(const char* fmt, va_list ap);
130 
131 static void warnx(const char* fmt, ...);
132 
133 /*
134  * Compute the greatest common divisor of a and b.
135  */
136 static int gcd(int a, int b);
137 
138 /*
139  * Exchange the block from nonopt_start to nonopt_end with the block
140  * from nonopt_end to opt_end (keeping the same order of arguments
141  * in each block).
142  */
143 static void permute_args(int panonopt_start, int panonopt_end, int opt_end, char* const* nargv);
144 
145 #ifdef REPLACE_GETOPT
146 /*
147  * getopt --
148  *	Parse argc/argv argument vector.
149  *
150  * [eventually this will replace the BSD getopt]
151  */
152 int getopt(int nargc, char* const* nargv, const char* options);
153 #endif /* REPLACE_GETOPT */
154 
155 //extern int getopt(int nargc, char * const *nargv, const char *options);
156 
157 #ifdef _BSD_SOURCE
158 /*
159  * BSD adds the non-standard `optreset' feature, for reinitialisation
160  * of `getopt' parsing.  We support this feature, for applications which
161  * proclaim their BSD heritage, before including this header; however,
162  * to maintain portability, developers are advised to avoid it.
163  */
164 #define optreset __mingw_optreset
165 extern int optreset;
166 #endif
167 #ifdef __cplusplus
168 }
169 #endif
170 /*
171  * POSIX requires the `getopt' API to be specified in `unistd.h';
172  * thus, `unistd.h' includes this header.  However, we do not want
173  * to expose the `getopt_long' or `getopt_long_only' APIs, when
174  * included in this manner.  Thus, close the standard __GETOPT_H__
175  * declarations block, and open an additional __GETOPT_LONG_H__
176  * specific block, only when *not* __UNISTD_H_SOURCED__, in which
177  * to declare the extended API.
178  */
179 #endif /* !defined(__GETOPT_H__) */
180 
181 #if !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__)
182 #define __GETOPT_LONG_H__
183 
184 #ifdef __cplusplus
185 extern "C" {
186 #endif
187 
188 /*
189  * parse_long_options --
190  *	Parse long options in argc/argv argument vector.
191  * Returns -1 if short_too is set and the option does not match long_options.
192  */
193 /* static int parse_long_options(char* const* nargv, const char* options, const struct option* long_options, int* idx, int short_too); */
194 
195 /*
196  * getopt_internal --
197  *	Parse argc/argv argument vector.  Called by user level routines.
198  */
199 /* static int getopt_internal(int nargc, char* const* nargv, const char* options, const struct option* long_options, int* idx, int flags); */
200 
201 /*
202  * getopt_long --
203  *	Parse argc/argv argument vector.
204  */
205 int getopt_long(int nargc, char* const* nargv, const char* options, const struct option* long_options, int* idx);
206 
207 /*
208  * getopt_long_only --
209  *	Parse argc/argv argument vector.
210  */
211 int getopt_long_only(int nargc, char* const* nargv, const char* options, const struct option* long_options, int* idx);
212 
213 /*
214  * Previous MinGW implementation had...
215  */
216 #ifndef HAVE_DECL_GETOPT
217 /*
218  * ...for the long form API only; keep this for compatibility.
219  */
220 #define HAVE_DECL_GETOPT 1
221 #endif
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 
227 #endif /* !defined(__UNISTD_H_SOURCED__) && !defined(__GETOPT_LONG_H__) */
228 #endif
229