aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorDavid M. Syzdek <david@syzdek.net>2022-03-28 12:51:49 -0800
committerDavid M. Syzdek <david@syzdek.net>2022-03-28 12:51:49 -0800
commit7178491e0be9aef9f1862f1453d74472118ad297 (patch)
tree3c4ef973389d5f45c1f9e4d939efdc363667db57
parent13df55820068f83fdbfc172c26a8e4b5b0c349fc (diff)
downloadtinyrad-7178491e0be9aef9f1862f1453d74472118ad297.tar.gz
tinyrad-7178491e0be9aef9f1862f1453d74472118ad297.tar.bz2
tinyrad-7178491e0be9aef9f1862f1453d74472118ad297.tar.xz
tinyrad-7178491e0be9aef9f1862f1453d74472118ad297.zip
tinyrad: refactoring CLI parser
-rw-r--r--src/tru/tinyradutil.c113
-rw-r--r--src/tru/tinyradutil.h9
2 files changed, 64 insertions, 58 deletions
diff --git a/src/tru/tinyradutil.c b/src/tru/tinyradutil.c
index e4ecbf8..2b5956d 100644
--- a/src/tru/tinyradutil.c
+++ b/src/tru/tinyradutil.c
@@ -164,8 +164,6 @@ const TinyRadUtilWidget tru_widget_map[] =
164 164
165int main(int argc, char * argv[]) 165int main(int argc, char * argv[])
166{ 166{
167 int c;
168 int opt_index;
169 int rc; 167 int rc;
170 TinyRadDict * dict; 168 TinyRadDict * dict;
171 TinyRadUtilConf cnfdata; 169 TinyRadUtilConf cnfdata;
@@ -181,32 +179,11 @@ int main(int argc, char * argv[])
181 cnf = &cnfdata; 179 cnf = &cnfdata;
182 dict = NULL; 180 dict = NULL;
183 181
184 while((c = tru_getopt(cnf, argc, argv, short_opt, long_opt, &opt_index)) != -1) 182 // process common cli options
183 if ((rc = tru_cli_parse(cnf, argc, argv, short_opt, long_opt)) != 0)
185 { 184 {
186 switch(c) 185 tru_cleanup(cnf);
187 { 186 return((rc == 0) ? 0 : 1);
188 case TRU_GETOPT_MATCHED: /* captured by common options */
189 case -1: /* no more arguments */
190 case 0: /* long options toggles */
191 break;
192
193 case TRU_GETOPT_EXIT:
194 tru_cleanup(cnf);
195 return(0);
196
197 case TRU_GETOPT_ERROR:
198 tru_cleanup(cnf);
199 return(1);
200
201 case '?':
202 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
203 return(1);
204
205 default:
206 fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c);
207 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
208 return(1);
209 };
210 }; 187 };
211 if ((argc - optind) < 1) 188 if ((argc - optind) < 1)
212 { 189 {
@@ -227,34 +204,10 @@ int main(int argc, char * argv[])
227 }; 204 };
228 205
229 // process widget cli options 206 // process widget cli options
230 optind = 1; 207 if ((rc = tru_cli_parse(cnf, cnf->argc, cnf->argv, cnf->widget->shortopts, cnf->widget->longopts)) != 0)
231 opt_index = 0;
232 while((c = tru_getopt(cnf, cnf->argc, cnf->argv, cnf->widget->shortopts, cnf->widget->longopts, &opt_index)) != -1)
233 { 208 {
234 switch(c) 209 tru_cleanup(cnf);
235 { 210 return((rc == 0) ? 0 : 1);
236 case -2: /* captured by common options */
237 case -1: /* no more arguments */
238 case 0: /* long options toggles */
239 break;
240
241 case TRU_GETOPT_EXIT:
242 tru_cleanup(cnf);
243 return(0);
244
245 case TRU_GETOPT_ERROR:
246 tru_cleanup(cnf);
247 return(1);
248
249 case '?':
250 fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name);
251 return(1);
252
253 default:
254 fprintf(stderr, "%s: %s: unrecognized option `--%c'\n", PROGRAM_NAME, cnf->widget_name, c);
255 fprintf(stderr, "Try `%s %s --help' for more information.\n", PROGRAM_NAME, cnf->widget_name);
256 return(1);
257 };
258 }; 211 };
259 if ((cnf->argc - optind) < cnf->widget->min_arg) 212 if ((cnf->argc - optind) < cnf->widget->min_arg)
260 { 213 {
@@ -350,6 +303,54 @@ tru_load_tinyrad(
350#pragma mark usage functions 303#pragma mark usage functions
351 304
352int 305int
306tru_cli_parse(
307 TinyRadUtilConf * cnf,
308 int argc,
309 char * const * argv,
310 const char * short_opt,
311 const struct option * long_opt )
312{
313 int c;
314 int opt_index;
315
316 optind = 1;
317 opt_index = 0;
318
319 while((c = tru_getopt(cnf, argc, argv, short_opt, long_opt, &opt_index)) != -1)
320 {
321 switch(c)
322 {
323 case TRU_GETOPT_MATCHED: /* captured by common options */
324 case -1: /* no more arguments */
325 case 0: /* long options toggles */
326 break;
327
328 case TRU_GETOPT_EXIT:
329 return(TRU_GETOPT_EXIT);
330
331 case TRU_GETOPT_ERROR:
332 return(TRU_GETOPT_ERROR);
333
334 case 'h':
335 tru_usage(cnf);
336 return(TRU_GETOPT_EXIT);
337
338 case '?':
339 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
340 return(TRU_GETOPT_ERROR);
341
342 default:
343 fprintf(stderr, "%s: unrecognized option `--%c'\n", PROGRAM_NAME, c);
344 fprintf(stderr, "Try `%s --help' for more information.\n", PROGRAM_NAME);
345 return(TRU_GETOPT_ERROR);
346 };
347 };
348
349 return(0);
350}
351
352
353int
353tru_getopt( 354tru_getopt(
354 TinyRadUtilConf * cnf, 355 TinyRadUtilConf * cnf,
355 int argc, 356 int argc,
@@ -380,10 +381,6 @@ tru_getopt(
380 tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt); 381 tinyrad_set_option(NULL, TRAD_OPT_DEBUG_LEVEL, &opt);
381 return(TRU_GETOPT_MATCHED); 382 return(TRU_GETOPT_MATCHED);
382 383
383 case 'h':
384 tru_usage(cnf);
385 return(TRU_GETOPT_EXIT);
386
387 case 'I': 384 case 'I':
388 if (tinyrad_strsadd(&cnf->dict_paths, optarg) != TRAD_SUCCESS) 385 if (tinyrad_strsadd(&cnf->dict_paths, optarg) != TRAD_SUCCESS)
389 { 386 {
diff --git a/src/tru/tinyradutil.h b/src/tru/tinyradutil.h
index b2e4f92..5ebd650 100644
--- a/src/tru/tinyradutil.h
+++ b/src/tru/tinyradutil.h
@@ -176,6 +176,15 @@ tru_load_tinyrad(
176#pragma mark usage prototypes 176#pragma mark usage prototypes
177 177
178int 178int
179tru_cli_parse(
180 TinyRadUtilConf * cnf,
181 int argc,
182 char * const * argv,
183 const char * short_opt,
184 const struct option * long_opt );
185
186
187int
179tru_getopt( 188tru_getopt(
180 TinyRadUtilConf * cnf, 189 TinyRadUtilConf * cnf,
181 int argc, 190 int argc,