1 /*******************************************************************************
2 * argtable3: Declares the main interfaces of the library
4 * This file is part of the argtable3 library.
6 * Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
7 * <sheitmann@users.sourceforge.net>
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * * Neither the name of STEWART HEITMANN nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 ******************************************************************************/
36 #include <stdio.h> /* FILE */
37 #include <time.h> /* struct tm */
44 #define ARG_REX_ICASE 1
45 #define ARG_DSTR_SIZE 200
46 #define ARG_CMD_NAME_LEN 100
47 #define ARG_CMD_DESCRIPTION_LEN 256
49 #ifndef ARG_REPLACE_GETOPT
50 #define ARG_REPLACE_GETOPT 1 /* use the embedded getopt as the system getopt(3) */
51 #endif /* ARG_REPLACE_GETOPT */
53 /* bit masks for arg_hdr.flag */
60 typedef void (arg_resetfn
)(void *parent
);
61 typedef int (arg_scanfn
)(void *parent
, const char *argval
);
62 typedef int (arg_checkfn
)(void *parent
);
63 typedef void (arg_errorfn
)(void *parent
, FILE *fp
, int error
, const char *argval
, const char *progname
);
67 * The arg_hdr struct defines properties that are common to all arg_xxx structs.
68 * The argtable library requires each arg_xxx struct to have an arg_hdr
69 * struct as its first data member.
70 * The argtable library functions then use this data to identify the
71 * properties of the command line option, such as its option tags,
72 * datatype string, and glossary strings, and so on.
73 * Moreover, the arg_hdr struct contains pointers to custom functions that
74 * are provided by each arg_xxx struct which perform the tasks of parsing
75 * that particular arg_xxx arguments, performing post-parse checks, and
77 * These functions are private to the individual arg_xxx source code
78 * and are the pointer to them are initiliased by that arg_xxx struct's
79 * constructor function. The user could alter them after construction
80 * if desired, but the original intention is for them to be set by the
81 * constructor and left unaltered.
84 char flag
; /* Modifier flags: ARG_TERMINATOR, ARG_HASVALUE. */
85 const char *shortopts
; /* String defining the short options */
86 const char *longopts
; /* String defiing the long options */
87 const char *datatype
; /* Description of the argument data type */
88 const char *glossary
; /* Description of the option as shown by arg_print_glossary function */
89 int mincount
; /* Minimum number of occurences of this option accepted */
90 int maxcount
; /* Maximum number of occurences if this option accepted */
91 void *parent
; /* Pointer to parent arg_xxx struct */
92 arg_resetfn
*resetfn
; /* Pointer to parent arg_xxx reset function */
93 arg_scanfn
*scanfn
; /* Pointer to parent arg_xxx scan function */
94 arg_checkfn
*checkfn
; /* Pointer to parent arg_xxx check function */
95 arg_errorfn
*errorfn
; /* Pointer to parent arg_xxx error function */
96 void *priv
; /* Pointer to private header data for use by arg_xxx functions */
100 struct arg_hdr hdr
; /* The mandatory argtable header struct */
104 struct arg_hdr hdr
; /* The mandatory argtable header struct */
105 int count
; /* Number of matching command line args */
109 struct arg_hdr hdr
; /* The mandatory argtable header struct */
110 int count
; /* Number of matching command line args */
111 int *ival
; /* Array of parsed argument values */
115 struct arg_hdr hdr
; /* The mandatory argtable header struct */
116 int count
; /* Number of matching command line args */
117 uint64_t *uval
; /* Array of parsed argument values */
121 struct arg_hdr hdr
; /* The mandatory argtable header struct */
122 int count
; /* Number of matching command line args */
123 double *dval
; /* Array of parsed argument values */
127 struct arg_hdr hdr
; /* The mandatory argtable header struct */
128 int count
; /* Number of matching command line args */
129 const char **sval
; /* Array of parsed argument values */
133 struct arg_hdr hdr
; /* The mandatory argtable header struct */
134 int count
; /* Number of matching command line args */
135 const char **sval
; /* Array of parsed argument values */
139 struct arg_hdr hdr
; /* The mandatory argtable header struct */
140 int count
; /* Number of matching command line args*/
141 const char **filename
; /* Array of parsed filenames (eg: /home/foo.bar) */
142 const char **basename
; /* Array of parsed basenames (eg: foo.bar) */
143 const char **extension
; /* Array of parsed extensions (eg: .bar) */
147 struct arg_hdr hdr
; /* The mandatory argtable header struct */
148 const char *format
; /* strptime format string used to parse the date */
149 int count
; /* Number of matching command line args */
150 struct tm
*tmval
; /* Array of parsed time values */
153 enum {ARG_ELIMIT
= 1, ARG_EMALLOC
, ARG_ENOMATCH
, ARG_ELONGOPT
, ARG_EMISSARG
};
155 struct arg_hdr hdr
; /* The mandatory argtable header struct */
156 int count
; /* Number of errors encountered */
157 int *error
; /* Array of error codes */
158 void **parent
; /* Array of pointers to offending arg_xxx struct */
159 const char **argval
; /* Array of pointers to offending argv[] string */
163 typedef struct arg_cmd_info {
164 char name[ARG_CMD_NAME_LEN];
165 char description[ARG_CMD_DESCRIPTION_LEN];
170 /**** arg_xxx constructor functions *********************************/
172 struct arg_rem
*arg_rem(const char *datatype
, const char *glossary
);
174 struct arg_lit
*arg_lit0(const char *shortopts
,
175 const char *longopts
,
176 const char *glossary
);
177 struct arg_lit
*arg_lit1(const char *shortopts
,
178 const char *longopts
,
179 const char *glossary
);
180 struct arg_lit
*arg_litn(const char *shortopts
,
181 const char *longopts
,
184 const char *glossary
);
186 struct arg_key
*arg_key0(const char *keyword
, int flags
, const char *glossary
);
187 struct arg_key
*arg_key1(const char *keyword
, int flags
, const char *glossary
);
188 struct arg_key
*arg_keyn(const char *keyword
, int flags
, int mincount
, int maxcount
, const char *glossary
);
190 struct arg_int
*arg_int0(const char *shortopts
, const char *longopts
, const char *datatype
, const char *glossary
);
191 struct arg_int
*arg_int1(const char *shortopts
, const char *longopts
, const char *datatype
, const char *glossary
);
192 struct arg_int
*arg_intn(const char *shortopts
, const char *longopts
, const char *datatype
, int mincount
, int maxcount
, const char *glossary
);
194 struct arg_u64
*arg_u64_0(const char *shortopts
,
195 const char *longopts
,
196 const char *datatype
,
197 const char *glossary
);
198 struct arg_u64
*arg_u64_1(const char *shortopts
,
199 const char *longopts
,
200 const char *datatype
,
201 const char *glossary
);
202 struct arg_u64
*arg_u64_n(const char *shortopts
,
203 const char *longopts
,
204 const char *datatype
,
207 const char *glossary
);
210 struct arg_dbl
*arg_dbl0(const char *shortopts
,
211 const char *longopts
,
212 const char *datatype
,
213 const char *glossary
);
214 struct arg_dbl
*arg_dbl1(const char *shortopts
,
215 const char *longopts
,
216 const char *datatype
,
217 const char *glossary
);
218 struct arg_dbl
*arg_dbln(const char *shortopts
,
219 const char *longopts
,
220 const char *datatype
,
223 const char *glossary
);
225 struct arg_str
*arg_str0(const char *shortopts
,
226 const char *longopts
,
227 const char *datatype
,
228 const char *glossary
);
229 struct arg_str
*arg_str1(const char *shortopts
,
230 const char *longopts
,
231 const char *datatype
,
232 const char *glossary
);
233 struct arg_str
*arg_strn(const char *shortopts
,
234 const char *longopts
,
235 const char *datatype
,
238 const char *glossary
);
240 struct arg_rex
*arg_rex0(const char *shortopts
,
241 const char *longopts
,
243 const char *datatype
,
245 const char *glossary
);
246 struct arg_rex
*arg_rex1(const char *shortopts
,
247 const char *longopts
,
249 const char *datatype
,
251 const char *glossary
);
252 struct arg_rex
*arg_rexn(const char *shortopts
,
253 const char *longopts
,
255 const char *datatype
,
259 const char *glossary
);
261 struct arg_file
*arg_file0(const char *shortopts
,
262 const char *longopts
,
263 const char *datatype
,
264 const char *glossary
);
265 struct arg_file
*arg_file1(const char *shortopts
,
266 const char *longopts
,
267 const char *datatype
,
268 const char *glossary
);
269 struct arg_file
*arg_filen(const char *shortopts
,
270 const char *longopts
,
271 const char *datatype
,
274 const char *glossary
);
276 struct arg_date
*arg_date0(const char *shortopts
,
277 const char *longopts
,
279 const char *datatype
,
280 const char *glossary
);
281 struct arg_date
*arg_date1(const char *shortopts
,
282 const char *longopts
,
284 const char *datatype
,
285 const char *glossary
);
286 struct arg_date
*arg_daten(const char *shortopts
,
287 const char *longopts
,
289 const char *datatype
,
292 const char *glossary
);
294 struct arg_end
*arg_end(int maxcount
);
297 /**** other functions *******************************************/
298 int arg_nullcheck(void **argtable
);
299 int arg_parse(int argc
, char **argv
, void **argtable
);
300 void arg_print_option(FILE *fp
, const char *shortopts
, const char *longopts
, const char *datatype
, const char *suffix
);
301 void arg_print_syntax(FILE *fp
, void **argtable
, const char *suffix
);
302 void arg_print_syntaxv(FILE *fp
, void **argtable
, const char *suffix
);
303 void arg_print_glossary(FILE *fp
, void **argtable
, const char *format
);
304 void arg_print_glossary_gnu(FILE *fp
, void **argtable
);
305 void arg_print_errors(FILE *fp
, struct arg_end
*end
, const char *progname
);
306 void arg_freetable(void **argtable
, size_t n
);
308 /**** deprecated functions, for back-compatibility only ********/
309 void arg_free(void **argtable
);