comparison glib/options.h @ 0:5242703e91d3 tip

Initial checkin for AIM92 aimR8.2 (last updated May 1997).
author tomwalters
date Fri, 20 May 2011 15:19:45 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:5242703e91d3
1 /*
2 Copyright (c) Applied Psychology Unit, Medical Research Council. 1988, 1989
3 ===========================================================================
4
5 Permission to use, copy, modify, and distribute this software without fee
6 is hereby granted for research purposes, provided that this copyright
7 notice appears in all copies and in all supporting documentation, and that
8 the software is not redistributed for any fee (except for a nominal shipping
9 charge). Anyone wanting to incorporate all or part of this software in a
10 commercial product must obtain a license from the Medical Research Council.
11
12 The MRC makes no representations about the suitability of this
13 software for any purpose. It is provided "as is" without express or implied
14 warranty.
15
16 THE MRC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
17 ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL THE
18 A.P.U. BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
19 DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
20 AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 */
23
24 /* ---------------------------------------------------------------------------------
25
26 *
27 The Leanest Resource Manager in the West
28 -----------------------------------------
29
30
31 (*) is no more! The gallant <getopts.[ch]> has
32 given way to a new daring duo! Henceforth,
33 read "options" for "getopts". The new features
34 include classified options and user-supplied
35 "Help" information. See <options.c> for edit
36 history.
37
38 Copyright (c), 1989 The Medical Research Council of the United Kingdom.
39
40 Author : Paul Manson
41 Written : March 1st 1989 [See options.c for modifications history]
42
43 Options may be supplied as defaults (actually in the code), as command line
44 arguments, and in an option file. The default arguments may be overridden by
45 the option-file contents, which may be overridden in turn by command line
46 arguments. The syntax of a option file is as follows:
47
48 <option file> ::= { <option line> <linefeed> }
49
50 <option line> ::= <option specification> | <comment line> | <blank line>
51
52 <option specification> ::= <name> "=" <option value> [ <comment line> ]
53
54 <option value> ::= <contiguous character string> |
55 """ <any printable chars except """> """
56
57 <comment line> ::= "#" <absolutely anything>
58
59 where spaces are not considered significant except that the <value> ought to
60 be a quoted string if it contains spaces. On Unix, the OPTIONSPATH path in the
61 users' environment is used to search for a option file, but if this path is
62 absent, the current working directory will still be searched. On an IBM PC, the
63 the directory in which the application program was found is searched for the
64 option file. [In the future, this will be modified so that the current directory
65 is searched first].
66
67 Command line arguments may take any of the following forms;
68
69 1. -name
70
71 2. -name=value
72
73 3. name=value
74
75 where it should be noted that spaces ARE significant, and that the first of these
76 formats effects a set/toggle operation on the named option, by setting its
77 value to some system-defined default value (at present, this is "1").
78
79 If a user specifies -help on the command line then getopts uses the "help" option
80 (if there is one) to print a message detailing the options available to the
81 user. Specifying -update on the command line causes a new version of the currently
82 read option file to be written, incorporating any alterations which have been
83 effected by previous command line options.
84
85 Abbreviations are permitted for options, but it must be noted that only
86 unambiguous option specifications will have any effect (ambiguous specifications
87 are rejected with a warning message). If a option specification uses a name which
88 is an abbreviation for two options, but matches one of these options EXACTLY,
89 this is not considered to be ambiguous (the exact match being taken as the intended
90 one). Abbreviations are NOT permitted in option headers (see readopts and writeopts
91 below).
92
93 --------------------------------------------------------------------------------- */
94
95 #include <stdio.h>
96
97 #define InputOption (1)
98 #define OutputOption (2)
99 #define InOutOption (0)
100 #define SilentOption (3)
101
102 typedef struct {
103 char *name ; /* The name of the option, */
104 char *defaultValue ; /* its default value, */
105 char **value ; /* The place where its current value is stored */
106 char *comment ; /* Something to print when the user types "-help" */
107 short classification; /* What form of option it is. */
108 } Option, *OptionPtr;
109
110 /* ---------------------------------------------------------------------------------
111
112 This structure may be used to build a database of option handles, as indicated
113 in the following sample program;
114
115 static char *samplerate, *inputfile;
116
117 static Option res[] = {
118 { "Sample Rate", "10000.0", &samplerate, "The Rate of Sampling", InOutOption },
119 { "Input File ", "input.data", &inputfile , "A File to Read From ", InOutOption }};
120
121 which declares two options, <samplerate> and <inputfile>, and gives them
122 default values. These defaults may later be overwritten by the function
123 getopts(), either from the option file or from the command line.
124
125 --------------------------------------------------------------------------------- */
126
127
128 #define NumOptions(res) (sizeof(res) / ((sizeof(char *) + (sizeof(char **)))))
129
130 /* ---------------------------------------------------------------------------------
131
132 Provides a macro for determining how many options are present in a res[]
133 declaration such as that shown above. eg. NumOptions(res) == 2. This macro
134 should be used on calls to getopts, etc.
135
136 --------------------------------------------------------------------------------- */
137
138
139 extern void getnopts(); /* (options, numOptions, &argc, &argv) */
140
141 /* ---------------------------------------------------------------------------------
142
143 This function should be called before an application program examines its arguments
144 (this is because it butchers them!). First of all, it reads the appropriate option
145 file and interprets the options specified therein (the name of the option file
146 is given by the name of the application, ie. argv[0]). After redefining the options
147 specified by the option file, it examines the arguments (given by argc and argv)
148 and scans them for additional options. Finally, it resets argc and argv for the
149 remainder of the program (if any) to deal with. NOTE THAT argc AND argv _MUST_ BE
150 PASSED AS POINTERS TO THIS ROUTINE!.
151
152 --------------------------------------------------------------------------------- */
153
154
155 extern void getopts(); /* (options, &argc, &argv) */
156 extern void cmd_line_opts() ; /* (options, &argc, &argv) */
157
158 /* ---------------------------------------------------------------------------------
159
160 Some fussy people don't like to count their own option lists, so this routine
161 does the necessary dirty-work before passing the result(s) to getnopts(). You MUST
162 remember to null-terminate your options array; ie.
163
164 static char *samplerate, *inputfile;
165
166 static Option res[] = {
167 { "Sample Rate", "10000.0", &samplerate, "The Rate of Sampling", InOutOption },
168 { "Input File ", "input.data", &inputfile , "A File to Read From ", InOutOption },
169 NULL};
170
171 and upon your own head (or lesser, more sensitive, appendages) be it if you forget
172 to do this!
173
174 --------------------------------------------------------------------------------- */
175
176
177 extern void readnopts(); /* (options, numOptions, filePointer) */
178 extern void readopts() ; /* (options, filePointer) */
179
180 /* ---------------------------------------------------------------------------------
181
182 Try to read a options header from the specified <file>, with reference to the
183 array of <options>. The header layout is specified below (see writenopts), and
184 the operation, if successful, will leave the filePointer set to the first byte
185 after the (padded) option header. Unsuccessful readnopts will cause en error
186 message and will terminate the program. If there does not appear to be a header
187 on the file at all, readnopts does nothing. The routine readopts counts the
188 options in <options> for you, and you MUST null-terminate your options
189 array if you use this routine.
190
191 WARNING: readopts and readnopts do NOT require that a option name found in a
192 header be an actual option of the calling program, and will IGNORE any such
193 option assignments.
194
195 --------------------------------------------------------------------------------- */
196
197
198 extern void writenopts(); /* (options, numOptions, filePointer) */
199 extern void writeopts() ; /* (options, filePointer) */
200
201 /* ---------------------------------------------------------------------------------
202
203 Write the current option settings as specified by <options> into the
204 specified file. The options are written as an ASCII header, in the same form
205 as is required by getopts (and consequently, readopts). This header will begin
206 with some suitable comment (line beginning with a # character) to indicate that
207 it is in fact a option header. Following this comment will be information
208 concerning the size of the option file header. This information is used to tell
209 subsequent calls to readnopts the length of the option header; this header will
210 actually be padded to a block boundary to avoid disturbing the following data
211 unnecessarily. As might be expected, writeopts is a counting version of writenopts;
212 remember to NULL-terminate your option array if you use this routine.
213
214 --------------------------------------------------------------------------------- */
215
216
217 extern FILE *fileopts(); /* (options, &argc, &argv ) */
218
219 /* ---------------------------------------------------------------------------------
220
221 A packaging function which performs getopts(options, &argc, &argv), then fopens
222 the next argument and does readopts on it. It returns a file handle to this file
223 with the header removed (ie. it points at the first byte of data).
224
225 --------------------------------------------------------------------------------- */
226
227 extern FILE *optoutput(); /* (String) */
228
229 /* ---------------------------------------------------------------------------------
230
231 Simply open a file for output (depending on the value of String)... a NULL string
232 returns NULL, the default String (at present this is "1") sends to stdout, and
233 anything else is taken as being a path name for a file to create for writing.
234
235 --------------------------------------------------------------------------------- */
236
237 extern void helpnopts(); /* (options, numOptions, program_name) */
238 extern void helpopts(); /* (options, program_name) */
239
240 /* ---------------------------------------------------------------------------------
241
242 Allows application programs to invoke the online help (ie. usage) information at
243 any point. The helpopts call counts the number of options for you, BUT ONLY IF
244 you remember to NULL-TERMINATE IT!
245
246 --------------------------------------------------------------------------------- */
247
248 typedef void helpRoutine( /* (options, numOptions, program_name) */ );
249
250 extern helpRoutine *helpOptsHandler(); /* (helpRoutine) */
251
252 /* ---------------------------------------------------------------------------------
253
254 Allows an application program to install its very own helpHandler routine to
255 override the default help-message handler. The behaviour of such a routine is
256 implicitly trusted by <options>, so you should only install your own handler
257 when you are quite familiar with the Options structure format. This routine
258 returns the address of the previously installed handler (which is initially the
259 default handler) so that this may be reinstated at a later point.
260
261 ---------------------------------------------------------------------------------- */
262
263
264 #define isON(_STR) (!isOFF(_STR))
265
266 extern int isOFF();
267 /* char *optionStr; */
268
269 /* ---------------------------------------------------------------------------------
270
271 A convenience call which returns TRUE (1) if the optionStr is what options thinks
272 of as "ON", "YES", "TRUE", or "1".... This merely allows us to hide the actual
273 implementation of "ON" from the application programs.
274
275 --------------------------------------------------------------------------------- */
276
277 extern int isNULL();
278 /* char *optionStr; */
279
280 /* ---------------------------------------------------------------------------------
281
282 A convenience call which returns TRUE (1) if the optionStr is what options thinks
283 of as "NULL", "NONE", nothing, zip, etc... This merely allows us to hide the actual
284 implementation of "NULL" from the application programs.
285
286 --------------------------------------------------------------------------------- */
287
288 #define ON_OPTION "on"
289 #define OFF_OPTION "off"
290 #define NULL_OPTION "none"
291
292 /* ---------------------------------------------------------------------------------
293
294 NOTE: The above definitions allow the expression of constant option settings
295 via static option[] arrays... they do so at the risk of allowing some klutz
296 to alter them... the only solution to this problem (other than forcing
297 application programmers to read .h files) is to return copies of these
298 strings via procedure calls, and this precludes static initialisation.
299
300 --------------------------------------------------------------------------------- */
301
302 extern int OptionStringsEqual();
303 /* char *str1, *str2; */
304
305 /* ---------------------------------------------------------------------------------
306
307 A routine to reduce the load on the application programmer by providing a simple
308 boolean check for string equality WITHOUT CASE SIGNIFICANCE. This allows applications
309 to leave case and typing problems within "options" reliably.
310
311 --------------------------------------------------------------------------------- */
312
313 extern char *versionstr;