| 1 |
#ifndef __GETOPT_H_ |
| 2 |
#define __GETOPT_H_ |
| 3 |
#ifndef _MSC_VER |
| 4 |
/* $NetBSD: getopt.h,v 1.4 2000/07/07 10:43:54 ad Exp $ */ |
| 5 |
/* $FreeBSD: src/include/getopt.h,v 1.6 2004/02/24 08:09:20 ache Exp $ */ |
| 6 |
|
| 7 |
/*- |
| 8 |
* Copyright (c) 2000 The NetBSD Foundation, Inc. |
| 9 |
* All rights reserved. |
| 10 |
* |
| 11 |
* This code is derived from software contributed to The NetBSD Foundation |
| 12 |
* by Dieter Baron and Thomas Klausner. |
| 13 |
* |
| 14 |
* Redistribution and use in source and binary forms, with or without |
| 15 |
* modification, are permitted provided that the following conditions |
| 16 |
* are met: |
| 17 |
* 1. Redistributions of source code must retain the above copyright |
| 18 |
* notice, this list of conditions and the following disclaimer. |
| 19 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 20 |
* notice, this list of conditions and the following disclaimer in the |
| 21 |
* documentation and/or other materials provided with the distribution. |
| 22 |
* 3. All advertising materials mentioning features or use of this software |
| 23 |
* must display the following acknowledgement: |
| 24 |
* This product includes software developed by the NetBSD |
| 25 |
* Foundation, Inc. and its contributors. |
| 26 |
* 4. Neither the name of The NetBSD Foundation nor the names of its |
| 27 |
* contributors may be used to endorse or promote products derived |
| 28 |
* from this software without specific prior written permission. |
| 29 |
* |
| 30 |
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 31 |
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 32 |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 33 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 34 |
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 35 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 36 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 37 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 38 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 39 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 40 |
* POSSIBILITY OF SUCH DAMAGE. |
| 41 |
*/ |
| 42 |
|
| 43 |
#ifndef _GETOPT_H_ |
| 44 |
#define _GETOPT_H_ |
| 45 |
|
| 46 |
#include <sys/cdefs.h> |
| 47 |
#include <unistd.h> |
| 48 |
|
| 49 |
/* |
| 50 |
* GNU-like getopt_long()/getopt_long_only() with 4.4BSD optreset extension. |
| 51 |
* getopt() is declared here too for GNU programs. |
| 52 |
*/ |
| 53 |
#define no_argument 0 |
| 54 |
#define required_argument 1 |
| 55 |
#define optional_argument 2 |
| 56 |
|
| 57 |
struct option { |
| 58 |
/* name of long option */ |
| 59 |
const char *name; |
| 60 |
/* |
| 61 |
* one of no_argument, required_argument, and optional_argument: |
| 62 |
* whether option takes an argument |
| 63 |
*/ |
| 64 |
int has_arg; |
| 65 |
/* if not NULL, set *flag to val when option found */ |
| 66 |
int *flag; |
| 67 |
/* if flag not NULL, value to set *flag to; else return value */ |
| 68 |
int val; |
| 69 |
}; |
| 70 |
|
| 71 |
__BEGIN_DECLS |
| 72 |
int getopt_long(int, char * const *, const char *, |
| 73 |
const struct option *, int *); |
| 74 |
int getopt_long_only(int, char * const *, const char *, |
| 75 |
const struct option *, int *); |
| 76 |
#ifndef _GETOPT |
| 77 |
#define _GETOPT |
| 78 |
int getopt(int, char * const [], const char *) __DARWIN_ALIAS(getopt); |
| 79 |
|
| 80 |
extern char *optarg; /* getopt(3) external variables */ |
| 81 |
extern int optind, opterr, optopt; |
| 82 |
#endif |
| 83 |
#ifndef _OPTRESET |
| 84 |
#define _OPTRESET |
| 85 |
extern int optreset; /* getopt(3) external variable */ |
| 86 |
#endif |
| 87 |
__END_DECLS |
| 88 |
|
| 89 |
#endif /* !_GETOPT_H_ */ |
| 90 |
#else |
| 91 |
/* Getopt for Microsoft C |
| 92 |
This code is a modification of the Free Software Foundation, Inc. |
| 93 |
Getopt library for parsing command line argument the purpose was |
| 94 |
to provide a Microsoft Visual C friendly derivative. This code |
| 95 |
provides functionality for both Unicode and Multibyte builds. |
| 96 |
|
| 97 |
Date: 02/03/2011 - Ludvik Jerabek - Initial Release |
| 98 |
Version: 1.0 |
| 99 |
Comment: Supports getopt, getopt_long, and getopt_long_only |
| 100 |
and POSIXLY_CORRECT environment flag |
| 101 |
License: LGPL |
| 102 |
|
| 103 |
Revisions: |
| 104 |
|
| 105 |
02/03/2011 - Ludvik Jerabek - Initial Release |
| 106 |
02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4 |
| 107 |
07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs |
| 108 |
08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception |
| 109 |
08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB |
| 110 |
02/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file |
| 111 |
|
| 112 |
**DISCLAIMER** |
| 113 |
THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
| 114 |
EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE |
| 115 |
IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 116 |
PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE |
| 117 |
EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT |
| 118 |
APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY |
| 119 |
DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY |
| 120 |
USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST |
| 121 |
PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON |
| 122 |
YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE |
| 123 |
EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. |
| 124 |
*/ |
| 125 |
|
| 126 |
#ifdef _GETOPT_API |
| 127 |
#undef _GETOPT_API |
| 128 |
#endif |
| 129 |
|
| 130 |
#if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT) |
| 131 |
#error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually" |
| 132 |
#elif defined(STATIC_GETOPT) |
| 133 |
#pragma message("Warning static builds of getopt violate the Lesser GNU Public License") |
| 134 |
#define _GETOPT_API |
| 135 |
#elif defined(EXPORTS_GETOPT) |
| 136 |
#pragma message("Exporting getopt library") |
| 137 |
#define _GETOPT_API __declspec(dllexport) |
| 138 |
#else |
| 139 |
#pragma message("Importing getopt library") |
| 140 |
#define _GETOPT_API __declspec(dllimport) |
| 141 |
#endif |
| 142 |
|
| 143 |
|
| 144 |
#include <tchar.h> |
| 145 |
|
| 146 |
// Standard GNU options |
| 147 |
#define null_argument 0 /*Argument Null*/ |
| 148 |
#define no_argument 0 /*Argument Switch Only*/ |
| 149 |
#define required_argument 1 /*Argument Required*/ |
| 150 |
#define optional_argument 2 /*Argument Optional*/ |
| 151 |
|
| 152 |
// Shorter Versions of options |
| 153 |
#define ARG_NULL 0 /*Argument Null*/ |
| 154 |
#define ARG_NONE 0 /*Argument Switch Only*/ |
| 155 |
#define ARG_REQ 1 /*Argument Required*/ |
| 156 |
#define ARG_OPT 2 /*Argument Optional*/ |
| 157 |
|
| 158 |
// Change behavior for C\C++ |
| 159 |
#ifdef __cplusplus |
| 160 |
#define _BEGIN_EXTERN_C extern "C" { |
| 161 |
#define _END_EXTERN_C } |
| 162 |
#define _GETOPT_THROW throw() |
| 163 |
#else |
| 164 |
#define _BEGIN_EXTERN_C |
| 165 |
#define _END_EXTERN_C |
| 166 |
#define _GETOPT_THROW |
| 167 |
#endif |
| 168 |
|
| 169 |
_BEGIN_EXTERN_C |
| 170 |
|
| 171 |
extern _GETOPT_API TCHAR *optarg; |
| 172 |
extern _GETOPT_API int optind; |
| 173 |
extern _GETOPT_API int opterr; |
| 174 |
extern _GETOPT_API int optopt; |
| 175 |
|
| 176 |
struct option |
| 177 |
{ |
| 178 |
const TCHAR* name; |
| 179 |
int has_arg; |
| 180 |
int *flag; |
| 181 |
TCHAR val; |
| 182 |
}; |
| 183 |
|
| 184 |
extern _GETOPT_API int getopt(int argc, TCHAR *const *argv, const TCHAR *optstring) _GETOPT_THROW; |
| 185 |
extern _GETOPT_API int getopt_long(int ___argc, TCHAR *const *___argv, const TCHAR *__shortopts, const struct option *__longopts, int *__longind) _GETOPT_THROW; |
| 186 |
extern _GETOPT_API int getopt_long_only(int ___argc, TCHAR *const *___argv, const TCHAR *__shortopts, const struct option *__longopts, int *__longind) _GETOPT_THROW; |
| 187 |
_END_EXTERN_C |
| 188 |
|
| 189 |
// Undefine so the macros are not included |
| 190 |
#undef _BEGIN_EXTERN_C |
| 191 |
#undef _END_EXTERN_C |
| 192 |
#undef _GETOPT_THROW |
| 193 |
#undef _GETOPT_API |
| 194 |
|
| 195 |
#endif |
| 196 |
#endif // __GETOPT_H_ |