1 /**
2  * WinPR: Windows Portable Runtime
3  * Command-Line Utils
4  *
5  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #ifndef WINPR_CMDLINE_H
21 #define WINPR_CMDLINE_H
22 
23 #include <winpr/winpr.h>
24 #include <winpr/wtypes.h>
25 
26 /* Command-Line Argument Flags */
27 
28 #define COMMAND_LINE_INPUT_FLAG_MASK 0x0000FFFF
29 #define COMMAND_LINE_OUTPUT_FLAG_MASK 0xFFFF0000
30 
31 /* Command-Line Argument Input Flags */
32 
33 #define COMMAND_LINE_VALUE_FLAG 0x00000001
34 #define COMMAND_LINE_VALUE_REQUIRED 0x00000002
35 #define COMMAND_LINE_VALUE_OPTIONAL 0x00000004
36 #define COMMAND_LINE_VALUE_BOOL 0x00000008
37 
38 #define COMMAND_LINE_ADVANCED 0x00000100
39 #define COMMAND_LINE_PRINT 0x00000200
40 #define COMMAND_LINE_PRINT_HELP 0x00000400
41 #define COMMAND_LINE_PRINT_VERSION 0x00000800
42 #define COMMAND_LINE_PRINT_BUILDCONFIG 0x00001000
43 
44 /* Command-Line Argument Output Flags */
45 
46 #define COMMAND_LINE_ARGUMENT_PRESENT 0x80000000
47 #define COMMAND_LINE_VALUE_PRESENT 0x40000000
48 
49 /* Command-Line Parsing Flags */
50 
51 #define COMMAND_LINE_SIGIL_NONE 0x00000001
52 #define COMMAND_LINE_SIGIL_SLASH 0x00000002
53 #define COMMAND_LINE_SIGIL_DASH 0x00000004
54 #define COMMAND_LINE_SIGIL_DOUBLE_DASH 0x00000008
55 #define COMMAND_LINE_SIGIL_PLUS_MINUS 0x00000010
56 #define COMMAND_LINE_SIGIL_ENABLE_DISABLE 0x00000020
57 #define COMMAND_LINE_SIGIL_NOT_ESCAPED 0x00000040
58 
59 #define COMMAND_LINE_SEPARATOR_COLON 0x00000100
60 #define COMMAND_LINE_SEPARATOR_EQUAL 0x00000200
61 #define COMMAND_LINE_SEPARATOR_SPACE 0x00000400
62 
63 /* Supress COMMAND_LINE_ERROR_NO_KEYWORD return. */
64 #define COMMAND_LINE_IGN_UNKNOWN_KEYWORD 0x00001000
65 #define COMMAND_LINE_SILENCE_PARSER 0x00002000
66 
67 /* Command-Line Parsing Error Codes */
68 
69 #define COMMAND_LINE_ERROR -1000
70 #define COMMAND_LINE_ERROR_NO_KEYWORD -1001
71 #define COMMAND_LINE_ERROR_UNEXPECTED_VALUE -1002
72 #define COMMAND_LINE_ERROR_MISSING_VALUE -1003
73 #define COMMAND_LINE_ERROR_MISSING_ARGUMENT -1004
74 #define COMMAND_LINE_ERROR_UNEXPECTED_SIGIL -1005
75 #define COMMAND_LINE_ERROR_MEMORY -1006
76 #define COMMAND_LINE_ERROR_LAST -1999
77 
78 /* Command-Line Parsing Status Codes */
79 
80 #define COMMAND_LINE_STATUS_PRINT -2001
81 #define COMMAND_LINE_STATUS_PRINT_HELP -2002
82 #define COMMAND_LINE_STATUS_PRINT_VERSION -2003
83 #define COMMAND_LINE_STATUS_PRINT_BUILDCONFIG -2004
84 #define COMMAND_LINE_STATUS_PRINT_LAST -2999
85 
86 /* Command-Line Macros */
87 
88 #define CommandLineSwitchStart(_arg) \
89 	if (0)                           \
90 	{                                \
91 	}
92 #define CommandLineSwitchCase(_arg, _name) else if (strcmp(_arg->Name, _name) == 0)
93 #define CommandLineSwitchDefault(_arg) else
94 #define CommandLineSwitchEnd(_arg)
95 
96 #define BoolValueTrue ((LPSTR)1)
97 #define BoolValueFalse ((LPSTR)0)
98 
99 typedef struct _COMMAND_LINE_ARGUMENT_A COMMAND_LINE_ARGUMENT_A;
100 typedef struct _COMMAND_LINE_ARGUMENT_W COMMAND_LINE_ARGUMENT_W;
101 
102 struct _COMMAND_LINE_ARGUMENT_A
103 {
104 	LPCSTR Name;
105 	DWORD Flags;
106 	LPCSTR Format;
107 	LPCSTR Default;
108 	LPSTR Value;
109 	LONG Index;
110 	LPCSTR Alias;
111 	LPCSTR Text;
112 };
113 
114 struct _COMMAND_LINE_ARGUMENT_W
115 {
116 	LPCWSTR Name;
117 	DWORD Flags;
118 	LPCSTR Format;
119 	LPWSTR Default;
120 	LPWSTR Value;
121 	LONG Index;
122 	LPCWSTR Alias;
123 	LPCWSTR Text;
124 };
125 
126 #ifdef UNICODE
127 #define COMMAND_LINE_ARGUMENT COMMAND_LINE_ARGUMENT_W
128 #else
129 #define COMMAND_LINE_ARGUMENT COMMAND_LINE_ARGUMENT_A
130 #endif
131 
132 typedef int (*COMMAND_LINE_PRE_FILTER_FN_A)(void* context, int index, int argc, LPSTR* argv);
133 typedef int (*COMMAND_LINE_PRE_FILTER_FN_W)(void* context, int index, int argc, LPWSTR* argv);
134 
135 typedef int (*COMMAND_LINE_POST_FILTER_FN_A)(void* context, COMMAND_LINE_ARGUMENT_A* arg);
136 typedef int (*COMMAND_LINE_POST_FILTER_FN_W)(void* context, COMMAND_LINE_ARGUMENT_W* arg);
137 
138 #ifdef __cplusplus
139 extern "C"
140 {
141 #endif
142 
143 	WINPR_API int CommandLineClearArgumentsA(COMMAND_LINE_ARGUMENT_A* options);
144 	WINPR_API int CommandLineClearArgumentsW(COMMAND_LINE_ARGUMENT_W* options);
145 
146 	WINPR_API int CommandLineParseArgumentsA(int argc, LPSTR* argv,
147 	                                         COMMAND_LINE_ARGUMENT_A* options, DWORD flags,
148 	                                         void* context, COMMAND_LINE_PRE_FILTER_FN_A preFilter,
149 	                                         COMMAND_LINE_POST_FILTER_FN_A postFilter);
150 	WINPR_API int CommandLineParseArgumentsW(int argc, LPWSTR* argv,
151 	                                         COMMAND_LINE_ARGUMENT_W* options, DWORD flags,
152 	                                         void* context, COMMAND_LINE_PRE_FILTER_FN_W preFilter,
153 	                                         COMMAND_LINE_POST_FILTER_FN_W postFilter);
154 
155 	WINPR_API COMMAND_LINE_ARGUMENT_A* CommandLineFindArgumentA(COMMAND_LINE_ARGUMENT_A* options,
156 	                                                            LPCSTR Name);
157 	WINPR_API COMMAND_LINE_ARGUMENT_W* CommandLineFindArgumentW(COMMAND_LINE_ARGUMENT_W* options,
158 	                                                            LPCWSTR Name);
159 
160 	WINPR_API COMMAND_LINE_ARGUMENT_A*
161 	CommandLineFindNextArgumentA(COMMAND_LINE_ARGUMENT_A* argument);
162 
163 	WINPR_API char** CommandLineParseCommaSeparatedValues(const char* list, size_t* count);
164 
165 	WINPR_API char** CommandLineParseCommaSeparatedValuesEx(const char* name, const char* list,
166 	                                                        size_t* count);
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #ifdef UNICODE
173 #define CommandLineClearArguments CommandLineClearArgumentsW
174 #define CommandLineParseArguments CommandLineParseArgumentsW
175 #define CommandLineFindArgument CommandLineFindArgumentW
176 #else
177 #define CommandLineClearArguments CommandLineClearArgumentsA
178 #define CommandLineParseArguments CommandLineParseArgumentsA
179 #define CommandLineFindArgument CommandLineFindArgumentA
180 #endif
181 
182 #endif /* WINPR_CMDLINE_H */
183