comparison th_args.c @ 735:31bc1ed07cf5

Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 07 Dec 2022 12:14:39 +0200
parents 29e44a58bc73
children 56594b498180
comparison
equal deleted inserted replaced
734:2ae1045f6c18 735:31bc1ed07cf5
17 * @param argc number of arguments 17 * @param argc number of arguments
18 * @param argv argument string array 18 * @param argv argument string array
19 * @param[in] opts options list array 19 * @param[in] opts options list array
20 * @param[in] nopts number of elements in options list array 20 * @param[in] nopts number of elements in options list array
21 * @param[in] handle_option function pointer to callback that handles option arguments 21 * @param[in] handle_option function pointer to callback that handles option arguments
22 * @param[in] process if TRUE, actually handle the argument, aka call the handle_option() function. if FALSE, only validity of options are checked. 22 * @param[in] process if true, actually handle the argument, aka call the handle_option() function. if false, only validity of options are checked.
23 * @param[in] isLong TRUE if the option is a --long-format one 23 * @param[in] isLong true if the option is a --long-format one
24 * @returns returns @c TRUE if option processing was successful 24 * @returns returns @c true if option processing was successful
25 */ 25 */
26 static BOOL th_args_process_opt( 26 static bool th_args_process_opt(
27 char *currArg, int *argIndex, 27 char *currArg, int *argIndex,
28 int argc, char *argv[], 28 int argc, char *argv[],
29 const th_optarg opts[], int nopts, 29 const th_optarg opts[], int nopts,
30 BOOL (*handle_option)(int id, char *, char *), 30 bool (*handle_option)(int id, char *, char *),
31 BOOL process, BOOL isLong) 31 bool process, bool isLong)
32 { 32 {
33 const th_optarg *opt = NULL; 33 const th_optarg *opt = NULL;
34 char *optArg = NULL; 34 char *optArg = NULL;
35 35
36 for (int optIndex = 0; optIndex < nopts; optIndex++) 36 for (int optIndex = 0; optIndex < nopts; optIndex++)
79 if (optArg == NULL) 79 if (optArg == NULL)
80 { 80 {
81 THERR("Option '%s%s' requires an argument.\n", 81 THERR("Option '%s%s' requires an argument.\n",
82 isLong ? "--" : "-", 82 isLong ? "--" : "-",
83 currArg); 83 currArg);
84 return FALSE; 84 return false;
85 } 85 }
86 } 86 }
87 87
88 // Option was given succesfully, try to process it 88 // Option was given succesfully, try to process it
89 if (process && !handle_option(opt->id, optArg, currArg)) 89 if (process && !handle_option(opt->id, optArg, currArg))
90 return FALSE; 90 return false;
91 } 91 }
92 else 92 else
93 { 93 {
94 THERR("Unknown %s option '%s%s'\n", 94 THERR("Unknown %s option '%s%s'\n",
95 isLong ? "long" : "short", 95 isLong ? "long" : "short",
96 isLong ? "--" : "-", 96 isLong ? "--" : "-",
97 currArg); 97 currArg);
98 98
99 return FALSE; 99 return false;
100 } 100 }
101 101
102 return TRUE; 102 return true;
103 } 103 }
104 104
105 105
106 /** 106 /**
107 * Process given array of commandline arguments, handling short 107 * Process given array of commandline arguments, handling short
112 * @param[in] opts supported option list array 112 * @param[in] opts supported option list array
113 * @param[in] nopts number of elements in the option list array 113 * @param[in] nopts number of elements in the option list array
114 * @param[in] handle_option callback function 114 * @param[in] handle_option callback function
115 * @param[in] handle_other callback function 115 * @param[in] handle_other callback function
116 * @param[in] flags processing flags 116 * @param[in] flags processing flags
117 * @return returns @c TRUE if arguments were processed successfully 117 * @return returns @c true if arguments were processed successfully
118 */ 118 */
119 BOOL th_args_process(int argc, char *argv[], 119 bool th_args_process(int argc, char *argv[],
120 const th_optarg *opts, const int nopts, 120 const th_optarg *opts, const int nopts,
121 BOOL(*handle_option)(int id, char *, char *), 121 bool(*handle_option)(int id, char *, char *),
122 BOOL(*handle_other)(char *), const int flags) 122 bool(*handle_other)(char *), const int flags)
123 { 123 {
124 int handleFlags = flags & OPTH_ONLY_MASK; 124 int handleFlags = flags & OPTH_ONLY_MASK;
125 BOOL optionsOK = TRUE, endOfOptions = FALSE; 125 bool optionsOK = true, endOfOptions = false;
126 126
127 for (int argIndex = 1; argIndex < argc; argIndex++) 127 for (int argIndex = 1; argIndex < argc; argIndex++)
128 { 128 {
129 char *str = argv[argIndex]; 129 char *str = argv[argIndex];
130 if (*str == '-' && !endOfOptions) 130 if (*str == '-' && !endOfOptions)
131 { 131 {
132 // Should we process options? 132 // Should we process options?
133 BOOL process = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0; 133 bool process = (handleFlags & OPTH_ONLY_OPTS) || handleFlags == 0;
134 BOOL isLong; 134 bool isLong;
135 135
136 str++; 136 str++;
137 if (*str == '-') 137 if (*str == '-')
138 { 138 {
139 // Check for "--", which ends the options-list 139 // Check for "--", which ends the options-list
140 str++; 140 str++;
141 if (*str == 0) 141 if (*str == 0)
142 { 142 {
143 endOfOptions = TRUE; 143 endOfOptions = true;
144 continue; 144 continue;
145 } 145 }
146 146
147 // We have a long option 147 // We have a long option
148 isLong = TRUE; 148 isLong = true;
149 } 149 }
150 else 150 else
151 isLong = FALSE; 151 isLong = false;
152 152
153 if (!th_args_process_opt(str, &argIndex, argc, argv, 153 if (!th_args_process_opt(str, &argIndex, argc, argv,
154 opts, nopts, handle_option, process, isLong)) 154 opts, nopts, handle_option, process, isLong))
155 optionsOK = FALSE; 155 optionsOK = false;
156 } 156 }
157 else 157 else
158 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0) 158 if (handleFlags == OPTH_ONLY_OTHER || handleFlags == 0)
159 { 159 {
160 // Was not option argument 160 // Was not option argument
161 if (handle_other == NULL || 161 if (handle_other == NULL ||
162 (handle_other != NULL && !handle_other(str))) 162 (handle_other != NULL && !handle_other(str)))
163 { 163 {
164 optionsOK = FALSE; 164 optionsOK = false;
165 } 165 }
166 } 166 }
167 167
168 // Check if we bail out on invalid argument 168 // Check if we bail out on invalid argument
169 if (!optionsOK && (flags & OPTH_BAILOUT)) 169 if (!optionsOK && (flags & OPTH_BAILOUT))
170 return FALSE; 170 return false;
171 } 171 }
172 172
173 return optionsOK; 173 return optionsOK;
174 } 174 }
175 175
192 */ 192 */
193 void th_print_wrap(FILE *fh, const int spad, int const rpad, 193 void th_print_wrap(FILE *fh, const int spad, int const rpad,
194 const int width, const char *str) 194 const int width, const char *str)
195 { 195 {
196 size_t pos = 0; 196 size_t pos = 0;
197 BOOL first = TRUE; 197 bool first = true;
198 198
199 while (str[pos]) 199 while (str[pos])
200 { 200 {
201 // Pre-pad line 201 // Pre-pad line
202 int linelen = first ? spad : rpad; 202 int linelen = first ? spad : rpad;
203 th_print_pad(fh, first ? 0 : rpad, ' '); 203 th_print_pad(fh, first ? 0 : rpad, ' ');
204 first = FALSE; 204 first = false;
205 205
206 // Skip whitespace at line start 206 // Skip whitespace at line start
207 while (th_isspace(str[pos]) || str[pos] == '\n') 207 while (th_isspace(str[pos]) || str[pos] == '\n')
208 pos++; 208 pos++;
209 209
253 } 253 }
254 254
255 255
256 static void th_args_help_print_item(FILE *fh, const th_optarg *opt, 256 static void th_args_help_print_item(FILE *fh, const th_optarg *opt,
257 int *optWidth, const int maxOptWidth, const int termWidth, 257 int *optWidth, const int maxOptWidth, const int termWidth,
258 const BOOL doPrint) 258 const bool doPrint)
259 { 259 {
260 const char *arg = th_args_get_optarg(opt); 260 const char *arg = th_args_get_optarg(opt);
261 char fmtBuf[32]; 261 char fmtBuf[32];
262 int padWidth; 262 int padWidth;
263 BOOL hasLongOpt = opt->o_long != NULL; 263 bool hasLongOpt = opt->o_long != NULL;
264 264
265 if (opt->o_short != 0) 265 if (opt->o_short != 0)
266 { 266 {
267 if (!hasLongOpt && (opt->flags & OPT_ARGREQ)) 267 if (!hasLongOpt && (opt->flags & OPT_ARGREQ))
268 { 268 {
345 // Determine width of the options and arguments 345 // Determine width of the options and arguments
346 maxOptWidth = 0; 346 maxOptWidth = 0;
347 for (index = 0; index < nopts; index++) 347 for (index = 0; index < nopts; index++)
348 { 348 {
349 int optWidth = 0; 349 int optWidth = 0;
350 th_args_help_print_item(NULL, &opts[index], &optWidth, 0, width, FALSE); 350 th_args_help_print_item(NULL, &opts[index], &optWidth, 0, width, false);
351 if (optWidth > maxOptWidth) 351 if (optWidth > maxOptWidth)
352 maxOptWidth = optWidth; 352 maxOptWidth = optWidth;
353 } 353 }
354 354
355 maxOptWidth += 2; 355 maxOptWidth += 2;
356 356
357 // Print out the formatted option list 357 // Print out the formatted option list
358 for (index = 0; index < nopts; index++) 358 for (index = 0; index < nopts; index++)
359 { 359 {
360 int optWidth; 360 int optWidth;
361 th_args_help_print_item(fh, &opts[index], &optWidth, maxOptWidth, width, TRUE); 361 th_args_help_print_item(fh, &opts[index], &optWidth, maxOptWidth, width, true);
362 } 362 }
363 } 363 }