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