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