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