comparison src/dmargs.c @ 2586:9807ae37ad69

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