1 /* Getopt for Microsoft C
2 This code is a modification of the Free Software Foundation, Inc.
3 Getopt library for parsing command line argument the purpose was
4 to provide a Microsoft Visual C friendly derivative. This code
5 provides functionality for both Unicode and Multibyte builds.
6 
7 Date: 02/03/2011 - Ludvik Jerabek - Initial Release
8 Version: 1.0
9 Comment: Supports getopt, getopt_long, and getopt_long_only
10 and POSIXLY_CORRECT environment flag
11 License: LGPL
12 
13 Revisions:
14 
15 02/03/2011 - Ludvik Jerabek - Initial Release
16 02/20/2011 - Ludvik Jerabek - Fixed compiler warnings at Level 4
17 07/05/2011 - Ludvik Jerabek - Added no_argument, required_argument, optional_argument defs
18 08/03/2011 - Ludvik Jerabek - Fixed non-argument runtime bug which caused runtime exception
19 08/09/2011 - Ludvik Jerabek - Added code to export functions for DLL and LIB
20 02/15/2012 - Ludvik Jerabek - Fixed _GETOPT_THROW definition missing in implementation file
21 08/01/2012 - Ludvik Jerabek - Created separate functions for char and wchar_t characters so single dll can do both unicode and ansi
22 10/15/2012 - Ludvik Jerabek - Modified to match latest GNU features
23 06/19/2015 - Ludvik Jerabek - Fixed maximum option limitation caused by option_a (255) and option_w (65535) structure val variable
24 
25 **DISCLAIMER**
26 THIS MATERIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
27 EITHER EXPRESS OR IMPLIED, INCLUDING, BUT Not LIMITED TO, THE
28 IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
29 PURPOSE, OR NON-INFRINGEMENT. SOME JURISDICTIONS DO NOT ALLOW THE
30 EXCLUSION OF IMPLIED WARRANTIES, SO THE ABOVE EXCLUSION MAY NOT
31 APPLY TO YOU. IN NO EVENT WILL I BE LIABLE TO ANY PARTY FOR ANY
32 DIRECT, INDIRECT, SPECIAL OR OTHER CONSEQUENTIAL DAMAGES FOR ANY
33 USE OF THIS MATERIAL INCLUDING, WITHOUT LIMITATION, ANY LOST
34 PROFITS, BUSINESS INTERRUPTION, LOSS OF PROGRAMS OR OTHER DATA ON
35 YOUR INFORMATION HANDLING SYSTEM OR OTHERWISE, EVEN If WE ARE
36 EXPRESSLY ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
37 */
38 #ifndef __GETOPT_H_
39 	#define __GETOPT_H_
40 	#include <mgl2/dllexport.h>
41 	#define _GETOPT_API MGL_EXPORT
42 
43 	// Change behavior for C\C++
44 	#ifdef __cplusplus
45 		#define _BEGIN_EXTERN_C extern "C" {
46 		#define _END_EXTERN_C }
47 		#define _GETOPT_THROW throw()
48 	#else
49 		#define _BEGIN_EXTERN_C
50 		#define _END_EXTERN_C
51 		#define _GETOPT_THROW
52 	#endif
53 
54 	// Standard GNU options
55 	#define	null_argument		0	/*Argument Null*/
56 	#define	no_argument			0	/*Argument Switch Only*/
57 	#define required_argument	1	/*Argument Required*/
58 	#define optional_argument	2	/*Argument Optional*/
59 
60 	// Shorter Options
61 	#define ARG_NULL	0	/*Argument Null*/
62 	#define ARG_NONE	0	/*Argument Switch Only*/
63 	#define ARG_REQ		1	/*Argument Required*/
64 	#define ARG_OPT		2	/*Argument Optional*/
65 
66 	#include <string.h>
67 	#include <wchar.h>
68 
69 _BEGIN_EXTERN_C
70 
71 	extern _GETOPT_API int optind;
72 	extern _GETOPT_API int opterr;
73 	extern _GETOPT_API int optopt;
74 
75 	// Ansi
76 	struct option_a
77 	{
78 		const char* name;
79 		int has_arg;
80 		int *flag;
81 		int val;
82 	};
83 	extern _GETOPT_API char *optarg_a;
84 	extern _GETOPT_API int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW;
85 	extern _GETOPT_API int getopt_long_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW;
86 	extern _GETOPT_API int getopt_long_only_a(int argc, char *const *argv, const char *options, const struct option_a *long_options, int *opt_index) _GETOPT_THROW;
87 
88 	// Unicode
89 	struct option_w
90 	{
91 		const wchar_t* name;
92 		int has_arg;
93 		int *flag;
94 		int val;
95 	};
96 	extern _GETOPT_API wchar_t *optarg_w;
97 	extern _GETOPT_API int getopt_w(int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW;
98 	extern _GETOPT_API int getopt_long_w(int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index) _GETOPT_THROW;
99 	extern _GETOPT_API int getopt_long_only_w(int argc, wchar_t *const *argv, const wchar_t *options, const struct option_w *long_options, int *opt_index) _GETOPT_THROW;
100 
101 _END_EXTERN_C
102 
103 	#undef _BEGIN_EXTERN_C
104 	#undef _END_EXTERN_C
105 	#undef _GETOPT_THROW
106 	#undef _GETOPT_API
107 
108 	#ifdef _UNICODE
109 		#define getopt getopt_w
110 		#define getopt_long getopt_long_w
111 		#define getopt_long_only getopt_long_only_w
112 		#define option option_w
113 		#define optarg optarg_w
114 	#else
115 		#define getopt getopt_a
116 		#define getopt_long getopt_long_a
117 		#define getopt_long_only getopt_long_only_a
118 		#define option option_a
119 		#define optarg optarg_a
120 	#endif
121 #endif  // __GETOPT_H_
122