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