b91e2391 |
1 | /* -*-c-*- |
2 | * |
a1af6b50 |
3 | * $Id: mdwopt.h,v 1.10 2004/04/08 01:36:24 mdw Exp $ |
b91e2391 |
4 | * |
5 | * Options parsing, similar to GNU @getopt_long@ |
6 | * |
9cecacb1 |
7 | * (c) 1996 Straylight/Edgeware |
b91e2391 |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of many programs. |
13 | * |
14 | * `mdwopt' is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * `mdwopt' is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
4033d96b |
25 | * License along with `mdwopt'; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
b91e2391 |
28 | */ |
29 | |
b91e2391 |
30 | #ifndef MDWOPT_H |
31 | #define MDWOPT_H |
32 | |
33 | /*----- Options handling structures ---------------------------------------*/ |
34 | |
35 | #ifdef __cplusplus |
36 | extern "C" { |
37 | #endif |
38 | |
39 | /* --- @mdwopt_data@ --- * |
40 | * |
41 | * Contains all the information needed by the @mdwopt@ routine to do its |
379c3233 |
42 | * work. Try not to use @prog@ any more. If you're using mLib, the @quis@/ |
43 | * @ego@ interface works better. |
b91e2391 |
44 | */ |
45 | |
46 | typedef struct { |
379c3233 |
47 | |
b91e2391 |
48 | /* --- Public variables --- */ |
49 | |
50 | char *arg; /* Arg of current option, or 0 */ |
51 | int opt; /* Value of current option */ |
52 | int ind; /* 0 for init, index when done */ |
53 | int err; /* Set nonzero for error messages */ |
54 | char *prog; /* Program name (from @argv[0]@) */ |
55 | |
56 | /* --- Private variables --- * |
57 | * |
58 | * Don't play with these, please. |
59 | */ |
60 | |
61 | char *list; /* Current short options pointer */ |
62 | int next; /* Next argument, unpermuted */ |
63 | int order; /* Ordering of options, flags */ |
64 | char *env; /* Where we are in the env var */ |
65 | char *estart; /* Pointer to env var buffer */ |
66 | } |
67 | mdwopt_data; |
68 | |
69 | /*----- Global variables --------------------------------------------------*/ |
70 | |
71 | extern mdwopt_data mdwopt_global; /* The default global data */ |
72 | |
73 | /* --- For compatibility with older programs (and prettiness) --- * |
74 | * |
75 | * The macros here access the global structure defined above. I consider it |
76 | * to be perfectly acceptable to use these macros in new code, because it |
77 | * looks nicer than playing with @mdwopt_global@. |
78 | */ |
79 | |
80 | #define optarg (mdwopt_global.arg) /* Argument of current option */ |
81 | #define optopt (mdwopt_global.opt) /* Code of current option */ |
82 | #define opterr (mdwopt_global.err) /* Zero to report error messages */ |
83 | #define optind (mdwopt_global.ind) /* Index of first non-option */ |
84 | #define optprog (mdwopt_global.prog) /* Pointer to program name */ |
85 | |
86 | /*----- Type definitions --------------------------------------------------*/ |
87 | |
88 | /* --- Long options definition table --- */ |
89 | |
90 | struct option { |
91 | const char *name; /* Name of the long option */ |
92 | int has_arg; /* Does it have an argument? */ |
93 | int *flag; /* Address of flag variable */ |
94 | int val; /* Value to store/return */ |
95 | }; |
96 | |
97 | /* --- Old-style names for argument flags in long options table --- */ |
98 | |
99 | enum { |
100 | no_argument, /* No argument required */ |
101 | required_argument, /* User must specify argument */ |
102 | optional_argument /* Argument is optional */ |
103 | }; |
104 | |
105 | /* --- New style flag names --- */ |
106 | |
204877d5 |
107 | #define OPTF_NOARG 0u /* No argument */ |
108 | #define OPTF_ARGREQ 1u /* Required argument */ |
109 | #define OPTF_ARGOPT 2u /* Optional argument */ |
110 | #define OPTF_ARG 3u /* Argument type bitmask */ |
111 | #define OPTF_SWITCH 4u /* OR val into flag, don't store */ |
112 | #define OPTF_NEGATE 8u /* Allow long option to be negated */ |
113 | |
114 | #define OPTF_NOLONGS 1u /* Don't read long options */ |
115 | #define OPTF_NOSHORTS 2u /* Don't read short options */ |
116 | #define OPTF_NUMBERS 4u /* Read numeric options */ |
117 | #define OPTF_NEGATION 8u /* Allow `%|+|%' for negations */ |
118 | #define OPTF_ENVVAR 16u /* Parse options from env var */ |
119 | #define OPTF_NOPROGNAME 32u /* Don't set @optprog@ */ |
120 | #define OPTF_NEGNUMBER 64u /* Allow negated number options */ |
121 | |
122 | #define OPTF_NEGATED 256u /* Option flag was negated by user */ |
379c3233 |
123 | |
124 | /* --- Older new-style names --- */ |
125 | |
126 | enum { |
127 | gFlag_argReq = 1, gFlag_argOpt = 2, gFlag_switch = 4, gFlag_negate = 8 |
b91e2391 |
128 | }; |
129 | |
130 | enum { |
379c3233 |
131 | gFlag_noLongs = 1, gFlag_noShorts = 2, gFlag_numbers = 4, |
132 | gFlag_negation = 8, gFlag_envVar = 16, gFlag_noProgName = 32, |
133 | gFlag_negNumber = 64 |
b91e2391 |
134 | }; |
135 | |
136 | enum { |
379c3233 |
137 | gFlag_negated = 256 |
b91e2391 |
138 | }; |
139 | |
140 | /*----- Main code ---------------------------------------------------------*/ |
141 | |
142 | /* --- @mdwopt@ --- * |
143 | * |
144 | * Arguments: @int argc@ = number of command line arguments |
145 | * @char * const *argv@ = pointer to command line arguments |
146 | * @const char *shortopt@ = pointer to short options information |
147 | * @const struct option *longopts@ = pointer to long opts info |
148 | * @int *longind@ = where to store matched longopt |
149 | * @mdwopt_data *data@ = persistent state for the parser |
150 | * @int flags@ = various useful flags |
151 | * |
152 | * Returns: Value of option found next, or an error character, or |
153 | * @EOF@ for the last thing. |
154 | * |
155 | * Use: Reads options. The routine should be more-or-less compatible |
156 | * with standard getopts, although it provides many more |
157 | * features even than the standard GNU implementation. |
158 | * |
159 | * The precise manner of options parsing is determined by |
160 | * various flag settings, which are described below. By setting |
161 | * flag values appropriately, you can achieve behaviour very |
162 | * similar to most other getopt routines. |
163 | * |
164 | * |
165 | * How options parsing appears to users |
166 | * |
167 | * A command line consists of a number of `words' (which may |
168 | * contain spaces, according to various shell quoting |
169 | * conventions). A word may be an option, an argument to an |
170 | * option, or a non-option. An option begins with a special |
171 | * character, usually `%|-|%', although `%|+|%' is also used |
172 | * sometimes. As special exceptions, the word containing only a |
173 | * `%|-|%' is considered to be a non-option, since it usually |
174 | * represents standard input or output as a filename, and the |
175 | * word containing a double-dash `%|--|%' is used to mark all |
176 | * following words as being non-options regardless of their |
177 | * initial character. |
178 | * |
179 | * Traditionally, all words after the first non-option have been |
180 | * considered to be non-options automatically, so that options |
181 | * must be specified before filenames. However, this |
182 | * implementation can extract all the options from the command |
183 | * line regardless of their position. This can usually be |
184 | * disabled by setting one of the environment variables |
185 | * `%|POSIXLY_CORRECT|%' or `%|_POSIX_OPTION_ORDER|%'. |
186 | * |
187 | * There are two different styles of options: `short' and |
188 | * `long'. |
189 | * |
190 | * Short options are the sort which Unix has known for ages: an |
191 | * option is a single letter, preceded by a `%|-|%'. Short |
192 | * options can be joined together to save space (and possibly to |
193 | * make silly words): e.g., instead of giving options |
194 | * `%|-x.-y|%', a user could write `%|-xy|%'. Some short |
195 | * options can have arguments, which appear after the option |
196 | * letter, either immediately following, or in the next `word' |
197 | * (so an option with an argument could be written as |
198 | * `%|-o foo|%' or as `%|-ofoo|%'). Note that options with |
199 | * optional arguments must be written in the second style. |
200 | * |
201 | * When a short option controls a flag setting, it is sometimes |
202 | * possible to explicitly turn the flag off, as well as turning |
203 | * it on, (usually to override default options). This is |
204 | * usually done by using a `%|+|%' instead of a `%|-|%' to |
205 | * introduce the option. |
206 | * |
5b59e227 |
207 | * Long options, as popularized by the GNU utilities, are given |
b91e2391 |
208 | * long-ish memorable names, preceded by a double-dash `%|--|%'. |
209 | * Since their names are more than a single character, long |
210 | * options can't be combined in the same way as short options. |
211 | * Arguments to long options may be given either in the same |
212 | * `word', separated from the option name by an equals sign, or |
213 | * in the following `word'. |
214 | * |
215 | * Long option names can be abbreviated if necessary, as long |
216 | * as the abbreviation is unique. This means that options can |
217 | * have sensible and memorable names but still not require much |
218 | * typing from an experienced user. |
219 | * |
220 | * Like short options, long options can control flag settings. |
221 | * The options to manipulate these settings come in pairs: an |
222 | * option of the form `%|--set-flag|%' might set the flag, while |
223 | * an option of the form `%|--no-set-flag|%' might clear it. |
224 | * |
225 | * It is usual for applications to provide both short and long |
226 | * options with identical behaviour. Some applications with |
227 | * lots of options may only provide long options (although they |
228 | * will often be only two or three characters long). In this |
229 | * case, long options can be preceded with a single `%|-|%' |
230 | * character, and negated by a `%|+|%' character. |
231 | * |
232 | * Finally, some (older) programs accept arguments of the form |
233 | * `%%@.{"-"<number>}%%', to set some numerical parameter, |
234 | * typically a line count of some kind. |
235 | * |
236 | * |
237 | * How programs parse options |
238 | * |
239 | * An application parses its options by calling mdwopt |
240 | * repeatedly. Each time it is called, mdwopt returns a value |
241 | * describing the option just read, and stores information about |
242 | * the option in a data block. The value %$-1$% is returned |
243 | * when there are no more options to be read. The `%|?|%' |
244 | * character is returned when an error is encountered. |
245 | * |
246 | * Before starting to parse options, the value @data->ind@ must |
247 | * be set to 0 or 1. The value of @data->err@ can also be set, |
248 | * to choose whether errors are reported by mdwopt. |
249 | * |
250 | * The program's `@argc@' and `@argv@' arguments are passed to |
251 | * the options parser, so that it can read the command line. A |
252 | * flags word is also passed, allowing the program fine control |
253 | * over parsing. The flags are described above. |
254 | * |
255 | * Short options are described by a string, which once upon a |
256 | * time just contained the permitted option characters. Now the |
257 | * options string begins with a collection of flag characters, |
258 | * and various flag characters can be put after options |
259 | * characters to change their properties. |
260 | * |
261 | * If the first character of the short options string is |
262 | * `%|+|%', `%|-|%' or `%|!|%', the order in which options are |
263 | * read is modified, as follows: |
264 | * |
265 | * `%|+|%' forces the POSIX order to be used. As soon as a non- |
266 | * option is found, mdwopt returns %$-1$%. |
267 | * |
268 | * `%|-|%' makes mdwopt treat non-options as being `special' |
269 | * sorts of option. When a non-option word is found, the |
270 | * value 0 is returned, and the actual text of the word |
271 | * is stored as being the option's argument. |
272 | * |
273 | * `%|!|%' forces the default order to be used. The entire |
274 | * command line is scanned for options, which are |
275 | * returned in order. However, during this process, |
276 | * the options are moved in the @argv@ array, so that |
277 | * they appear before the non- options. |
278 | * |
279 | * A `%|:|%' character may be placed after the ordering flag (or |
280 | * at the very beginning if no ordering flag is given) which |
281 | * indicates that the character `%|:|%', rather than `%|?|%', |
282 | * should be returned if a missing argument error is detected. |
283 | * |
284 | * Each option in the string can be followed by a `%|+|%' sign, |
285 | * indicating that it can be negated, a `%|:|%' sign indicating |
286 | * that it requires an argument, or a `%|::|%' string, |
287 | * indicating an optional argument. Both `%|+|%' and `%|:|%' or |
288 | * `%|::|%' may be given, although the `%|+|%' must come first. |
289 | * |
290 | * If an option is found, the option character is returned to |
291 | * the caller. A pointer to an argument is stored in |
292 | * @data->arg@, or @NULL@ is stored if there was no argument. |
293 | * If a negated option was found, the option character is |
79eae586 |
294 | * returned ORred with @OPTF_NEGATED@ (bit 8 set). |
b91e2391 |
295 | * |
296 | * Long options are described in a table. Each entry in the |
297 | * table is of type @struct option@, and the table is terminated |
298 | * by an entry whose @name@ field is null. Each option has |
299 | * a flags word which, due to historical reasons, is called |
300 | * @has_arg@. This describes various properties of the option, |
301 | * such as what sort of argument it takes, and whether it can |
302 | * be negated. |
303 | * |
304 | * When mdwopt finds a long option, it looks the name up in the |
305 | * table. The index of the matching entry is stored in the |
306 | * @longind@ variable, passed to mdwopt (unless @longind@ is 0): |
307 | * a value of %$-1$% indicates that no long option was |
308 | * found. The behaviour is then dependent on the values in the |
309 | * table entry. If @flag@ is nonzero, it points to an integer |
310 | * to be modified by mdwopt. Usually the value in the @val@ |
311 | * field is simply stored in the @flag@ variable. If the flag |
79eae586 |
312 | * @OPTF_SWITCH@ is set, however, the value is combined with |
b91e2391 |
313 | * the existing value of the flags using a bitwise OR. If |
79eae586 |
314 | * @OPTF_NEGATE@ is set, then the flag bit will be cleared if a |
b91e2391 |
315 | * matching negated long option is found. The value 0 is |
316 | * returned. |
317 | * |
318 | * If @flag@ is zero, the value in @val@ is returned by mdwopt, |
319 | * possibly with bit 8 set if the option was negated. |
320 | * |
321 | * Arguments for long options are stored in @data->arg@, as |
322 | * before. |
323 | * |
324 | * Numeric options, if enabled, cause the value `%|#|%' to be |
325 | * returned, and the numeric value to be stored in @data->opt@. |
326 | * |
79eae586 |
327 | * If the flag @OPTF_ENVVAR@ is set on entry, options will be |
b91e2391 |
328 | * extracted from an environment variable whose name is built by |
79eae586 |
329 | * capitalizing all the letters of the program's name. (This |
b91e2391 |
330 | * allows a user to have different default settings for a |
79eae586 |
331 | * program, by calling it through different symbolic links.) |
332 | */ |
b91e2391 |
333 | |
334 | extern int mdwopt(int /*argc*/, char *const */*argv*/, |
335 | const char */*shortopt*/, |
336 | const struct option */*longopts*/, int */*longind*/, |
337 | mdwopt_data */*data*/, int /*flags*/); |
338 | |
339 | /* --- Macros for more commonly used routines --- */ |
340 | |
79eae586 |
341 | #define getopt(c, v, o) mdwopt(c, v, o, 0, 0, 0, OPTF_NOLONGS) |
b91e2391 |
342 | #define getopt_long(c, v, o, l, li) mdwopt(c, v, o, l, li, 0, 0) |
343 | #define getopt_long_only(c, v, o, l, li) \ |
79eae586 |
344 | mdwopt(c, v, o, l, li, 0, OPTF_NOSHORTS) |
b91e2391 |
345 | |
346 | #ifdef __cplusplus |
347 | } |
348 | #endif |
349 | |
350 | /*----- C++ wrapper class -------------------------------------------------*/ |
351 | |
352 | #ifdef __cplusplus |
353 | |
354 | /* --- Class: @MdwOpt@ --- * |
355 | * |
356 | * Parent: --- |
357 | * |
358 | * Methods: @MdwOpt@ -- construct a new mdwopt object with the given |
359 | * arguments. These are remembered for later use. |
360 | * @arg@ -- return the argument of the current option |
361 | * arguments. These are remembered for later use. |
362 | * @arg@ -- return the argument of the current option |
363 | * @opt@ -- return the value of the current option |
364 | * @ind@ -- return the index of the next unread argument |
365 | * @longind@ -- return index of current long option in table |
366 | * @errors@ -- return or set whether we report errors to the |
367 | * user |
368 | * @prog@ -- return program name from @argv[0]@ |
369 | * @next@ -- return next option read from the table |
370 | * |
371 | * Use: A simple C++ class for encapsulating the options parser. |
372 | * The methods are all nice and simple, and extremely similar |
373 | * to the normal C interface described above. |
374 | */ |
375 | |
376 | class MdwOpt { |
377 | protected: |
378 | int argc; |
379 | char * const *argv; |
380 | const char *shortopts; |
381 | const struct option *longopts; |
382 | int long_ind; |
383 | int flags; |
384 | |
385 | mdwopt_data data; |
386 | |
387 | public: |
388 | MdwOpt(int c, char * const *v, const char *so, |
389 | const struct option *lo, int f=0) : |
390 | argc(c), argv(v), shortopts(so), longopts(lo), flags(f) { |
391 | data.ind = 0; |
392 | data.err = 1; |
393 | } |
394 | |
395 | const char *arg(void) const { return (data.arg); } |
396 | int opt(void) const { return (data.opt); } |
397 | int errors(void) const { return (data.err); } |
398 | int errors(int e) { int oe = data.err; data.err = e; return (oe); } |
399 | int ind(void) const { return (data.ind); } |
400 | int longind(void) const { return (long_ind); } |
401 | const char *prog(void) const { return (data.prog); } |
402 | |
403 | int next(void) { |
404 | return (mdwopt(argc, argv, shortopts, |
405 | longopts, &long_ind, &data, flags)); |
406 | } |
407 | }; |
408 | |
409 | #endif |
410 | |
411 | /*----- That's all, folks -------------------------------------------------*/ |
412 | |
413 | #endif |