annotate dmargs_int.c @ 0:32250b436bca

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