comparison th_args.c @ 0:728243125263

Import.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 20 Mar 2008 00:15:03 +0000
parents
children ecfa4e3597e3
comparison
equal deleted inserted replaced
-1:000000000000 0:728243125263
1 /*
2 * Simple commandline argument processing
3 * Programmed and designed by Matti 'ccr' Hamalainen
4 * (C) Copyright 2002-2008 Tecnic Software productions (TNSP)
5 *
6 * Please read file 'COPYING' for information on license and distribution.
7 */
8 /*
9 Description
10 ===========
11 Structures and functions for simple commandline argument processing,
12 option argument handling (short and long forms supported).
13
14 Output function for printing out short on-line help for said options
15 and program commandline usage.
16
17
18 Format for typical commandline:
19
20 $ program -a -b -c --long-d -e argument-for-e --long-f="argument for f"
21
22 where -a, -b and -c are short options without required arguments;
23 --long-d is a long option without argument; -e is short option with
24 argument and finally --long-f is long option with argument.
25
26
27 Introduction
28 ============
29 Handling of commandline options in th_args_process() has various
30 options, which effect how an option is handled. Also the error
31 situations (unknown option/no required argument) can be handled
32 in different ways.
33
34 Typically th_args_process() is called as follows:
35
36 BOOL optionHandlerCallback(int optionNumber, char *optionArgument, char *optionName)
37 {
38 if (option is OK)
39 return TRUE;
40 else
41 return FALSE;
42 }
43
44 BOOL nonoptionHandlerCallback(char *argumentString)
45 {
46 // return value same as in optionHandlerCallback
47 }
48
49
50 int main(int argc, char *argv[])
51 {
52 if (th_args_process(argc, argv, optList, optListN,
53 optionHandlerCallback, nonoptionHandlerCallback)) {
54 ... arguments OK ...
55 } else {
56 ... arguments invalid or required arguments missing ...
57 }
58 }
59
60
61 NOTICE!
62 -------
63 The return value from handler callbacks affects the return value of
64 th_args_process(). Additionally, a failure in callback (returns FALSE)
65 effects the argument processing if bailOut argument of th_args_process()
66 is TRUE!
67
68 If bailOut is TRUE, any error/failure in argument processing (including
69 callbacks) immediately stops the argument processing and FALSE is
70 returned from th_args_process().
71
72 If bailOut is FALSE, most errors are "ignored", but FALSE is still returned
73 if any errors occured.
74
75
76 NOTICE #2!
77 ----------
78 A small temporary buffer of N*sizeof(BOOL) (where N is number of
79 options in optList[]) is allocated for processing required options.
80 If this allocation fails, the program is immediately exited with
81 code 128.
82
83
84 Examples
85 ========
86 Example of different options, in a fictional optionlist struct:
87
88 optarg_t optList[] = {
89 // Option without arguments
90 { 0, '?', "help", "Show this help", OPT_NONE },
91
92 // Option with a required argument
93 { 1, 'o', "output", "Output file name", OPT_ARGREQ },
94
95 // Option with optional argument
96 { 2, 'f', "foobar", "Use foobar, with optional string", OPT_ARGOPT },
97
98 // This option is required to be given, though without other flags
99 // it may not make much sense.
100 { 4, 'S', "stupid", "You must give this option", OPT_REQUIRED },
101
102 // The flags can be combined with OR operator: this option is both
103 // required to be specified, and also requires argument (the filename)
104 { 5, 'i', "input", "Input file name", OPT_REQUIRED | OPT_ARGREQ },
105
106
107 // Option with only long form
108 { 0, 0, "only-long", "Long option", OPT_NONE },
109
110 // Option with only short form
111 { 0, 's', NULL, "Short option", OPT_NONE },
112
113 };
114
115 const DINT optListN = (sizeof(optList) / sizeof(t_opt));
116
117
118 */
119 #ifdef HAVE_CONFIG_H
120 #include <config.h>
121 #endif
122 #include "th_util.h"
123 #include "th_args.h"
124 #include "th_string.h"
125
126
127 /* Check if option requires an argument
128 */
129 static BOOL th_args_check_arg(optarg_t *o, char *optArg)
130 {
131 if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ && optArg == NULL) {
132 if (o->optShort != 0 && o->optLong != NULL) {
133 THERR("Option '-%c ARG' (--%s=ARG) requires an argument!\n",
134 o->optShort, o->optLong);
135 } else if (o->optShort != 0) {
136 THERR("Option '-%c ARG' requires an argument!\n", o->optShort);
137 } else if (o->optLong != NULL) {
138 THERR("Option --%s=ARG requires an argument!\n", o->optLong);
139 }
140
141 return FALSE;
142 } else
143 return TRUE;
144
145 }
146
147
148 /* Handle short options
149 */
150 static BOOL th_args_process_short(char *currArg, int *newArgIndex,
151 BOOL *wasGiven, int argc, char *argv[],
152 optarg_t optList[], int optListN,
153 BOOL (*handleOpt)(int, char *, char *))
154 {
155 char *tmpArg = currArg, *optArg;
156 int optN;
157 BOOL isFound;
158
159 /* Short options can be combined: -a -b -c == -abc */
160 while (*tmpArg) {
161
162 for (optN = 0, isFound = FALSE; (optN < optListN) && !isFound; optN++)
163 if (*tmpArg == optList[optN].optShort) {
164 /* Get possible option argument, if needed */
165 if ((optList[optN].optFlags & OPT_ARGMASK) != 0 && (++(*newArgIndex) < argc))
166 optArg = argv[*newArgIndex];
167 else
168 optArg = NULL;
169
170 /* Check if option argument is required */
171 if (!th_args_check_arg(&optList[optN], optArg))
172 return FALSE;
173 else {
174 char tmpStr[2] = { 0, 0 };
175
176 /* Option was given succesfully, try to handle it */
177 wasGiven[optN] = TRUE;
178
179 tmpStr[0] = *tmpArg;
180
181 if (!handleOpt(optList[optN].optID, optArg, tmpStr))
182 return FALSE;
183 }
184
185 isFound = TRUE;
186 }
187
188 if (!isFound) {
189 THERR("Unknown short option '%c' in argument '-%s'\n",
190 *tmpArg, currArg);
191 return FALSE;
192 }
193
194 tmpArg++;
195 }
196
197 return TRUE;
198 }
199
200
201 /* Handle long options
202 */
203 static BOOL th_args_process_long(char *currArg, int *newArgIndex,
204 BOOL *wasGiven, int argc, char *argv[],
205 optarg_t optList[], int optListN,
206 BOOL (*handleOpt)(int, char *, char *))
207 {
208 int optN, optLen, i;
209 char *optArg;
210
211 (void) argc; (void) argv; (void) newArgIndex;
212
213 /* Long option */
214 for (optN = -1, optLen = i = 0; (i < optListN) && (optN < 0); i++)
215 if (optList[i].optLong) {
216 optLen = th_strlen(optList[i].optLong);
217 if (th_strncmp(currArg, optList[i].optLong, optLen) == 0)
218 optN = i;
219 }
220
221 /* Get possible option argument, if needed */
222 if (optN >= 0) {
223 if ((optList[optN].optFlags & OPT_ARGMASK) != 0) {
224 if (currArg[optLen] == '=')
225 optArg = &currArg[optLen + 1];
226 else
227 optArg = NULL;
228 } else
229 optArg = NULL;
230
231 /* Check if option argument is required */
232 if (!th_args_check_arg(&optList[optN], optArg))
233 return FALSE;
234 else {
235 /* Option was given succesfully, try to handle it */
236 wasGiven[optN] = TRUE;
237 if (!handleOpt(optList[optN].optID, optArg, currArg))
238 return FALSE;
239 }
240 } else {
241 THERR("Unknown long option '--%s'\n", currArg);
242 return FALSE;
243 }
244
245 return TRUE;
246 }
247
248
249 /* Process arguments, handling short and long options by
250 * calling the given callback functions.
251 */
252 BOOL th_args_process(int argc, char *argv[],
253 optarg_t optList[], int optListN,
254 BOOL (*handleOpt) (int, char *, char *),
255 BOOL (*handleNonOption) (char *), BOOL bailOut)
256 {
257 BOOL endOptions, optionsOK;
258 int argIndex, newArgIndex, i;
259 char *currArg;
260 BOOL *wasGiven;
261
262 /* Allocate wasGiven */
263 wasGiven = (BOOL *) th_calloc(optListN, sizeof(BOOL));
264 if (!wasGiven) {
265 THERR("FATAL ERROR! Could not allocate wasGiven in th_args_process()!\n");
266 exit(128);
267 }
268
269 /* Parse arguments */
270 argIndex = 1;
271 optionsOK = TRUE;
272 endOptions = FALSE;
273 while (argIndex < argc) {
274 currArg = argv[argIndex];
275 if ((currArg[0] == '-') && !endOptions) {
276 newArgIndex = argIndex;
277 currArg++;
278 if (*currArg == '-') {
279 /* Check for "--", which ends the options-list */
280 currArg++;
281 if (*currArg == 0) {
282 endOptions = TRUE;
283 continue;
284 }
285
286 /* Long options */
287 if (!th_args_process_long(currArg, &newArgIndex,
288 wasGiven, argc, argv, optList, optListN,
289 handleOpt))
290 optionsOK = FALSE;
291 } else {
292 /* Short options */
293 if (!th_args_process_short(currArg, &newArgIndex,
294 wasGiven, argc, argv, optList, optListN,
295 handleOpt))
296 optionsOK = FALSE;
297 }
298
299 argIndex = newArgIndex;
300 } else {
301 /* Was not option argument */
302 if (!handleNonOption || (handleNonOption && !handleNonOption(currArg))) {
303 THERR("Invalid argument '%s'\n", currArg);
304 optionsOK = FALSE;
305 }
306 }
307
308 /* Check if we bail out on invalid argument */
309 if (!optionsOK && bailOut) {
310 th_free(wasGiven);
311 return FALSE;
312 }
313
314 argIndex++;
315 }
316
317 /* Check wasGiven by isRequired */
318 for (i = 0; i < optListN; i++)
319 if ((optList[i].optFlags & OPT_REQUIRED) != 0 && !wasGiven[i]) {
320 THERR("Option -%s (--%s) is required.\n",
321 optList[i].optShort, optList[i].optLong);
322
323 optionsOK = FALSE;
324 if (bailOut) break;
325 }
326
327 th_free(wasGiven);
328 return optionsOK;
329 }
330
331
332 /* Print help for commandline arguments/options
333 */
334 void th_args_help(FILE * outFile,
335 optarg_t optList[], int optListN,
336 char * progName, char * progUsage)
337 {
338 int i, nrequired;
339
340 fprintf(outFile,
341 "\n%s v%s (%s)\n"
342 "%s\n"
343 "%s\n"
344 "Usage: %s %s\n",
345 th_prog_name, th_prog_version, th_prog_fullname,
346 th_prog_author, th_prog_license, progName, progUsage);
347
348
349 for (i = nrequired = 0; i < optListN; i++) {
350 optarg_t *o = &optList[i];
351
352 /* Print short option */
353 if (o->optShort != 0)
354 fprintf(outFile, " -%c, ", o->optShort);
355 else
356 fprintf(outFile, " ");
357
358 /* Print long option */
359 if (o->optLong) {
360 char tmpStr[64], *p;
361
362 if ((o->optFlags & OPT_ARGMASK) == OPT_ARGOPT) {
363 snprintf(tmpStr, sizeof(tmpStr), "%s[=ARG]", optList[i].optLong);
364 p = tmpStr;
365 } else if ((o->optFlags & OPT_ARGMASK) == OPT_ARGREQ) {
366 snprintf(tmpStr, sizeof(tmpStr), "%s=ARG", optList[i].optLong);
367 p = tmpStr;
368 } else
369 p = o->optLong;
370
371 fprintf(outFile, "--%-15s", p);
372 } else
373 fprintf(outFile, " ");
374
375 fprintf(outFile, " %s.", optList[i].optDesc);
376
377 if (o->optFlags & OPT_REQUIRED) {
378 fprintf(outFile, " [*]\n");
379 nrequired++;
380 } else
381 fprintf(outFile, "\n");
382 }
383
384 if (nrequired > 0)
385 fprintf(outFile, "(Options marked with [*] are required)\n");
386 }
387