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