1 /* argparse.h - Argument parser for option handling.
2 * Copyright (C) 1998,1999,2000,2001,2006 Free Software Foundation, Inc.
4 * This file is part of GnuPG.
6 * GnuPG is free software; you can redistribute and/or modify this
7 * part of GnuPG under the terms of either
9 * - the GNU Lesser General Public License as published by the Free
10 * Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
15 * - the GNU General Public License as published by the Free
16 * Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * or both in parallel, as here.
21 * GnuPG is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * General Public License for more details.
26 * You should have received a copies of the GNU General Public License
27 * and the GNU Lesser General Public License along with this program;
28 * if not, see <https://www.gnu.org/licenses/>.
31 #ifndef GNUPG_COMMON_ARGPARSE_H
32 #define GNUPG_COMMON_ARGPARSE_H
38 int *argc; /* Pointer to ARGC (value subject to change). */
39 char ***argv; /* Pointer to ARGV (value subject to change). */
40 unsigned int flags; /* Global flags. May be set prior to calling the
41 parser. The parser may change the value. */
42 int err; /* Print error description for last option.
43 Either 0, ARGPARSE_PRINT_WARNING or
44 ARGPARSE_PRINT_ERROR. */
46 int r_opt; /* Returns option code. */
47 int r_type; /* Returns type of option value. */
51 unsigned long ret_ulong;
53 } r; /* Return values */
61 const void *cur_alias;
63 } internal; /* Private - do not change. */
71 const char *description; /* Optional option description. */
75 #define ARGPARSE_SHORTOPT_HELP 32768
76 #define ARGPARSE_SHORTOPT_VERSION 32769
77 #define ARGPARSE_SHORTOPT_WARRANTY 32770
78 #define ARGPARSE_SHORTOPT_DUMP_OPTIONS 32771
81 /* Global flags (ARGPARSE_ARGS). */
82 #define ARGPARSE_FLAG_KEEP 1 /* Do not remove options form argv. */
83 #define ARGPARSE_FLAG_ALL 2 /* Do not stop at last option but return
84 remaining args with R_OPT set to -1. */
85 #define ARGPARSE_FLAG_MIXED 4 /* Assume options and args are mixed. */
86 #define ARGPARSE_FLAG_NOSTOP 8 /* Do not stop processing at "--". */
87 #define ARGPARSE_FLAG_ARG0 16 /* Do not skip the first arg. */
88 #define ARGPARSE_FLAG_ONEDASH 32 /* Allow long options with one dash. */
89 #define ARGPARSE_FLAG_NOVERSION 64 /* No output for "--version". */
91 #define ARGPARSE_FLAG_STOP_SEEN 256 /* Set to true if a "--" has been seen. */
93 /* Flags for each option (ARGPARSE_OPTS). The type code may be
94 ORed with the OPT flags. */
95 #define ARGPARSE_TYPE_NONE 0 /* Does not take an argument. */
96 #define ARGPARSE_TYPE_INT 1 /* Takes an int argument. */
97 #define ARGPARSE_TYPE_STRING 2 /* Takes a string argument. */
98 #define ARGPARSE_TYPE_LONG 3 /* Takes a long argument. */
99 #define ARGPARSE_TYPE_ULONG 4 /* Takes an unsigned long argument. */
100 #define ARGPARSE_OPT_OPTIONAL (1<<3) /* Argument is optional. */
101 #define ARGPARSE_OPT_PREFIX (1<<4) /* Allow 0x etc. prefixed values. */
102 #define ARGPARSE_OPT_IGNORE (1<<6) /* Ignore command or option. */
103 #define ARGPARSE_OPT_COMMAND (1<<7) /* The argument is a command. */
105 #define ARGPARSE_TYPE_MASK 7 /* Mask for the type values (internal). */
107 /* A set of macros to make option definitions easier to read. */
108 #define ARGPARSE_x(s,l,t,f,d) \
109 { (s), (l), ARGPARSE_TYPE_ ## t | (f), (d) }
111 #define ARGPARSE_s(s,l,t,d) \
112 { (s), (l), ARGPARSE_TYPE_ ## t, (d) }
113 #define ARGPARSE_s_n(s,l,d) \
114 { (s), (l), ARGPARSE_TYPE_NONE, (d) }
115 #define ARGPARSE_s_i(s,l,d) \
116 { (s), (l), ARGPARSE_TYPE_INT, (d) }
117 #define ARGPARSE_s_s(s,l,d) \
118 { (s), (l), ARGPARSE_TYPE_STRING, (d) }
119 #define ARGPARSE_s_l(s,l,d) \
120 { (s), (l), ARGPARSE_TYPE_LONG, (d) }
121 #define ARGPARSE_s_u(s,l,d) \
122 { (s), (l), ARGPARSE_TYPE_ULONG, (d) }
124 #define ARGPARSE_o(s,l,t,d) \
125 { (s), (l), (ARGPARSE_TYPE_ ## t | ARGPARSE_OPT_OPTIONAL), (d) }
126 #define ARGPARSE_o_n(s,l,d) \
127 { (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_OPTIONAL), (d) }
128 #define ARGPARSE_o_i(s,l,d) \
129 { (s), (l), (ARGPARSE_TYPE_INT | ARGPARSE_OPT_OPTIONAL), (d) }
130 #define ARGPARSE_o_s(s,l,d) \
131 { (s), (l), (ARGPARSE_TYPE_STRING | ARGPARSE_OPT_OPTIONAL), (d) }
132 #define ARGPARSE_o_l(s,l,d) \
133 { (s), (l), (ARGPARSE_TYPE_LONG | ARGPARSE_OPT_OPTIONAL), (d) }
134 #define ARGPARSE_o_u(s,l,d) \
135 { (s), (l), (ARGPARSE_TYPE_ULONG | ARGPARSE_OPT_OPTIONAL), (d) }
137 #define ARGPARSE_p(s,l,t,d) \
138 { (s), (l), (ARGPARSE_TYPE_ ## t | ARGPARSE_OPT_PREFIX), (d) }
139 #define ARGPARSE_p_n(s,l,d) \
140 { (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_PREFIX), (d) }
141 #define ARGPARSE_p_i(s,l,d) \
142 { (s), (l), (ARGPARSE_TYPE_INT | ARGPARSE_OPT_PREFIX), (d) }
143 #define ARGPARSE_p_s(s,l,d) \
144 { (s), (l), (ARGPARSE_TYPE_STRING | ARGPARSE_OPT_PREFIX), (d) }
145 #define ARGPARSE_p_l(s,l,d) \
146 { (s), (l), (ARGPARSE_TYPE_LONG | ARGPARSE_OPT_PREFIX), (d) }
147 #define ARGPARSE_p_u(s,l,d) \
148 { (s), (l), (ARGPARSE_TYPE_ULONG | ARGPARSE_OPT_PREFIX), (d) }
150 #define ARGPARSE_op(s,l,t,d) \
151 { (s), (l), (ARGPARSE_TYPE_ ## t \
152 | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
153 #define ARGPARSE_op_n(s,l,d) \
154 { (s), (l), (ARGPARSE_TYPE_NONE \
155 | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
156 #define ARGPARSE_op_i(s,l,d) \
157 { (s), (l), (ARGPARSE_TYPE_INT \
158 | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
159 #define ARGPARSE_op_s(s,l,d) \
160 { (s), (l), (ARGPARSE_TYPE_STRING \
161 | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
162 #define ARGPARSE_op_l(s,l,d) \
163 { (s), (l), (ARGPARSE_TYPE_LONG \
164 | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
165 #define ARGPARSE_op_u(s,l,d) \
166 { (s), (l), (ARGPARSE_TYPE_ULONG \
167 | ARGPARSE_OPT_OPTIONAL | ARGPARSE_OPT_PREFIX), (d) }
169 #define ARGPARSE_c(s,l,d) \
170 { (s), (l), (ARGPARSE_TYPE_NONE | ARGPARSE_OPT_COMMAND), (d) }
172 #define ARGPARSE_ignore(s,l) \
173 { (s), (l), (ARGPARSE_OPT_IGNORE), "@" }
175 #define ARGPARSE_group(s,d) \
176 { (s), NULL, 0, (d) }
178 /* Placeholder options for help, version, warranty and dump-options. See arg_parse(). */
179 #define ARGPARSE_end() \
180 { 0, NULL, 0, NULL }, \
181 { 0, NULL, 0, NULL }, \
182 { 0, NULL, 0, NULL }, \
183 { 0, NULL, 0, NULL }, \
187 /* Other constants. */
188 #define ARGPARSE_PRINT_WARNING 1
189 #define ARGPARSE_PRINT_ERROR 2
193 #define ARGPARSE_IS_ARG (-1)
194 #define ARGPARSE_INVALID_OPTION (-2)
195 #define ARGPARSE_MISSING_ARG (-3)
196 #define ARGPARSE_KEYWORD_TOO_LONG (-4)
197 #define ARGPARSE_READ_ERROR (-5)
198 #define ARGPARSE_UNEXPECTED_ARG (-6)
199 #define ARGPARSE_INVALID_COMMAND (-7)
200 #define ARGPARSE_AMBIGUOUS_OPTION (-8)
201 #define ARGPARSE_AMBIGUOUS_COMMAND (-9)
202 #define ARGPARSE_INVALID_ALIAS (-10)
203 #define ARGPARSE_OUT_OF_CORE (-11)
204 #define ARGPARSE_INVALID_ARG (-12)
207 int arg_parse (ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
208 int optfile_parse (FILE *fp, const char *filename, unsigned *lineno,
209 ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
210 void usage (int level);
211 const char *strusage (int level);
212 void set_strusage (const char *(*f)( int ));
213 void argparse_register_outfnc (int (*fnc)(int, const char *));
215 #endif /*GNUPG_COMMON_ARGPARSE_H*/