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 
41 #ifdef _GETOPT_API
42 #undef _GETOPT_API
43 #endif
44 
45 #if defined(EXPORTS_GETOPT) && defined(STATIC_GETOPT)
46 #error "The preprocessor definitions of EXPORTS_GETOPT and STATIC_GETOPT can only be used individually"
47 #elif defined(STATIC_GETOPT)
48 #pragma message("Warning static builds of getopt violate the Lesser GNU Public License")
49 #define _GETOPT_API
50 #elif defined(EXPORTS_GETOPT)
51 #pragma message("Exporting getopt library")
52 #define _GETOPT_API __declspec(dllexport)
53 #else
54 #pragma message("Importing getopt library")
55 #define _GETOPT_API __declspec(dllimport)
56 #endif
57 
58 // Change behavior for C\C++
59 #ifdef __cplusplus
60 #define _BEGIN_EXTERN_C extern "C" {
61 #define _END_EXTERN_C }
62 #define _GETOPT_THROW throw()
63 #else
64 #define _BEGIN_EXTERN_C
65 #define _END_EXTERN_C
66 #define _GETOPT_THROW
67 #endif
68 
69 // Standard GNU options
70 #define null_argument 0
71 #define no_argument 0
72 #define required_argument 1
73 #define optional_argument 2
74 
75 // Shorter Options
76 #define ARG_NULL 0
77 #define ARG_NONE 0
78 #define ARG_REQ 1
79 #define ARG_OPT 2
80 
81 #include <string.h>
82 #include <wchar.h>
83 
84 _BEGIN_EXTERN_C
85 
86 extern _GETOPT_API int optind;
87 extern _GETOPT_API int opterr;
88 extern _GETOPT_API int optopt;
89 
90 // Ansi
91 struct option_a
92 {
93     const char* name;
94     int has_arg;
95     int *flag;
96     int val;
97 };
98 extern _GETOPT_API char *optarg_a;
99 extern _GETOPT_API int getopt_a(int argc, char *const *argv, const char *optstring) _GETOPT_THROW;
100 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;
101 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;
102 
103 // Unicode
104 struct option_w
105 {
106     const wchar_t* name;
107     int has_arg;
108     int *flag;
109     int val;
110 };
111 extern _GETOPT_API wchar_t *optarg_w;
112 extern _GETOPT_API int getopt_w(int argc, wchar_t *const *argv, const wchar_t *optstring) _GETOPT_THROW;
113 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;
114 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;
115 
116 _END_EXTERN_C
117 
118 #undef _BEGIN_EXTERN_C
119 #undef _END_EXTERN_C
120 #undef _GETOPT_THROW
121 #undef _GETOPT_API
122 
123 #ifdef _UNICODE
124 #define getopt getopt_w
125 #define getopt_long getopt_long_w
126 #define getopt_long_only getopt_long_only_w
127 #define option option_w
128 #define optarg optarg_w
129 #else
130 #define getopt getopt_a
131 #define getopt_long getopt_long_a
132 #define getopt_long_only getopt_long_only_a
133 #define option option_a
134 #define optarg optarg_a
135 #endif
136 #endif  // __GETOPT_H_
137