-
Notifications
You must be signed in to change notification settings - Fork 0
/
copts.h
249 lines (222 loc) · 10.4 KB
/
copts.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*********************************************************************
######################################################################
##
## Created by: Denis Filatov
## Date : 10.11.2005
##
## C command line arguments and simple config file parser
##
## Copyleft (c) 2003 - 2007
## This code is provided under the MIT license agreement.
######################################################################
*********************************************************************/
#ifndef copts_h
#define copts_h
#include <stdio.h>
#ifdef __cplusplus
extern "C" {
#endif
/** @defgroup COPTS copts - Programm line option processing.
* @{ */
#ifndef DOXYGEN
typedef struct copt_t copt_t;
typedef enum coptflag_t coptflag_t;
typedef enum coptype_t coptype_t;
typedef enum copterr_t copterr_t;
#endif
/** @enum coptype_t
* Option types definitions.
*/
enum coptype_t{
COPT_BOOL , /**< Boolean option. Doesn't require arguments.*/
COPT_BOOLI, /**< Inverted Boolean option. Doesn't require arguments. */
COPT_INT , /**< Requires for int argument */
COPT_UINT, /**< Requires for unsigned int argument */
COPT_LONG , /**< Requires for long argument */
COPT_ULONG, /**< Requires for unsigned long argument */
COPT_SHORT, /**< Requires for short (16 bit) argument */
COPT_USHORT, /**< Requires for unsigned short argument */
COPT_FLOAT, /**< Requires for float argument */
COPT_DOUBLE, /**< Requires for double float argument */
COPT_CHAR , /**< Requires for char or unsigned char argument */
COPT_STR , /**< Requires for string (const char *) arguments */
COPT_URL, /**< Requires for string (const char *) arguments. Checks url syntax. */
COPT_SOCKADDR,/**< Requires for sockaddr_in argument. */
COPT_PATH, /**< Requires for string (const pchar_t *) arguments. */
COPT_STRLIST, /**< Requires for string list argument (const char *[])
* Every time when this opion will be occuren in argv
* the value will be assigned to given pointer and
* this pointer will be incremented. */
COPT_STRENUM, /**< Requires for one of the string in the array given by vptr)
* Array of strings must be terminated by NULL pointer.
* After option found the vptr pointer will point
* to the element corresponding to the argument */
COPT_CFGFILE, /**< Requires for string (const pchar_t *) arguments.
* Treat it as config file name and load if found.
* If one or more config file options are exists in copt_t list
* this options will be executed before any other options parsing */
COPT_HELP, /**< Does't require argument.
* If this option is occured in command line parsing will be
* terminated imediate and COPT_EHELP will be returned */
COPT_TYPE_MASK = 0x00FF, /**< Option type mask. @internal. */
COPT_NOHELP = 0x2000, /**< Mask. Can be or-ed with any other option.
* Do not include in the help message generation */
COPT_CALLBACK = 0x4000, /**< Mask. Can be or-ed with any other option.
* That's mean treat vptr as a callback addres to call
* when option is occured */
COPT_CONFIG = 0x8000, /**< Mask. Can be or-ed with any other option.
* That's mean this option can be reached from config file
* and have to be writen to.*/
COPT_END = 0xFFFF, /**< End of options.
* If vptr is not NULL, treat it as callback to call for unknown
* options and non-option values */
};
#define COPT_IBOOL COPT_BOOLI
/** Main options item.
* Have to be used to define options items.
* Short and long options can be defined.
* Possible options notations:
* - Boolean options:
* - -o
* - --option
* - Other types except of boolean:
* - -o value
* - -o=value
* - --option=value
*/
struct copt_t
{
const char* sopts; /**< Short options. */
const char* lopt; /**< Long option. */
const coptype_t type; /**< Option type ( see @ref coptype_t ). */
void* vptr; /**< Option variable pointer. */
const char* helpstr; /**< Option help string. */
};
/**
* Execute option parser.
* @param argc Command line parameters count (from arguments of main() for example).
* @param argv Array of command line parameters.
* @param flags Configuration flags ( @ref coptflag_t ).
* @param opts Array of possible options. Must be finished by item with COPT_END type.
* @return <ul><li>On success returns the index of the first option argument in the arguments array.<li>On error returns negative index of the invalid option in the arguments array.</ul>
*/
int coptions(int argc, char* argv[], int flags, copt_t* opts);
/** Get enum index from the option variable.
* @param opts @ref copt_t array.
* @param idx Index of the enum option in the array.
* @param ptr The initial value of the @a vptr field of the opion array item.
* @return the integer enum value of the selected item.
*/
#define copts_enum_valid(opts,idx) \
(NULL != *((const char **)((opts)[idx]).vptr))
#define copts_enum_value(opts,idx,ptr) \
((int)(((const char **)((opts)[idx]).vptr) - ((const char **)(ptr))))
/**
* Load options config file.
* @param filename File path to load.
* @param section If not NULL then try to find the last occurance of the
* given section or load the file complet.
* @param flags Configuration flags ( see @ref coptflag_t ).
* @param opts The Array of possible option records. Must be finished
* by the item with COPT_END type.
* @return
<ul><li>On success returns 0.<li>On error returns negative line number of the invalid expression.</ul>
*/
int coptions_load(const char* filename, const char * section, int flags, copt_t* const opts);
/**
* Save current options to the file
*/
int coptions_save(const char* filename, const copt_t* const opts);
/**
* Save current options to already opened file
*/
int coptions_fsave(FILE * fd, const copt_t* const opts);
/**
* Generate and print the help page.
* @param fd File descriptor to print the resulting help page.
* @param prgname Application name. Can be taken from argv[0].
* @param opt Options array.
* @param usagestr The string to print before option list.
* @param header Help page header.
* @param footer Help page footer.
*/
void coptions_help_ex(FILE * fd, const char * prgname, int flags, copt_t* opt, const char* usagestr,
const char* header, const char* footer);
/** The lite version of the @ref coptions_help_ex.
* @param fd File descriptor to print the resulting help page.
* @param prgname Application name. Can be taken from argv[0].
* @param opt Options array.
* @param usagestr The string to print before option list.
*/
#define coptions_help(fd,prgname,flags,opt,usagestr) \
coptions_help_ex(fd,prgname,flags,opt,usagestr,NULL,NULL)
/** Wild value definition */
typedef struct copts_sockaddr {
unsigned long inaddr;
short port;
}copts_sockaddr;
typedef union{
int v_boolean;
signed short v_short;
unsigned short v_ushort;
signed int v_int;
unsigned long v_uint;
signed long v_long;
unsigned long v_ulong;
float v_float;
double v_double;
char v_char;
char * v_str;
struct {
unsigned long inaddr;
short port;
}v_sockaddr_in;
}copt_value_t;
/** The type of callback function to be called for the option having
@ref COPT_CALLBACK bit set in the @e type field of the @ref copt_t structure.
These functions must return zero if option was successfully processed,
@ref COPT_EHELP to generate option help string or negative value when
some error was occured.
@param opt The current item of the options array.
@param option String option given.
@param value Pointer to the option value.
*/
typedef int copt_callback(const copt_t * opt, const char * option, const copt_value_t * value);
/** Inverted Boolean True. */
#define IBOOL_YES ((void*)-1)
/** @enum coptflag_t
Option flag mask values.
*/
enum coptflag_t
{
COPT_DEFAULT = 0x0000, /**< No special flags given. */
COPT_NOAUTOHELP = 0x0001, /**< Does not provide automatic help messages. */
COPT_NOCONFIG = 0x0002, /**< Does not search for config files. */
COPT_NOREORDER = 0x0004, /**< Does not reorder command line array. */
COPT_NOERR_MSG = 0x0010, /**< Be silent. */
COPT_NOERR_UNKNOWN = 0x0020, /**< Treat unknown options as non-option args.*/
COPT_NOERR_ARG = 0x0040, /**< Does not produce an error if the required
option's argument is omited or have
incompatible type. */
COPT_NOERR = 0x0070, /**< Does not produce any errors. */
COPT_ERR_CONFIG = 0x0080, /**< Does not produce config errors. */
COPT_NOHELP_MSG = 0x0100, /**< Does not print help messages. */
COPT_HELP_NOVALUES = 0x0200, /**< Does not print default values. */
};
/** @{
@ref coptions return codes.
*/
/** Help option (-h or --help) vaw invoked. Need to print help page.*/
#define COPT_EHELP ((int)(0x80000001))
/** Some error was occured.*/
#define COPT_ERROR ((int)(0x80000002))
#define COPT_ERC(rc) (rc < 0 && 0==(rc & 0x8000000))
/**@}*/
/** @} */
/**
* @example test_copts.c
*/
#ifdef __cplusplus
}
#endif
#endif