Debian Codegraph @

src/petopt.cpp

raw symbols SCIP
1 /*
2 ** petopt.c - Command line argument parser
3 **
4 ** Copyright (c) 1999 Peter Eriksson <[email protected]>
5 **
6 ** This program is free software; you can redistribute it and/or
7 ** modify it as you wish - as long as you don't claim that you wrote
8 ** it.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 */
14
15 // #include "config.h" removed because OES does not use this file
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <syslog.h>
23
24 #include "petopt.h"
25
26
27
28 /*
29 ** Compare the user supplied long style command line option against
30 ** the program defined. Handle abbreviations, truncation and ambiguous
31 ** abbreviations.
32 */
33 static int
34 compare_long(const char *user_option,
35 const char *aov_long,
36 int len)
37 {
38 unsigned char *uc, *ac;
39 int diff = 0;
40 int saved_len = len;
41
42
43 uc = (unsigned char *) user_option;
44 ac = (unsigned char *) aov_long;
45
46 while (len > 0 && diff == 0)
47 {
48 /* If we reach a word delimiter, skip to next word */
49 if (*uc == '-')
50 while (*ac && *ac != '-')
51 ++ac;
52
53 diff = (int) tolower(*uc) - (int) tolower(*ac);
54
55 ++uc;
56 ++ac;
57 --len;
58 }
59
60 if (diff)
61 {
62 /* Check abbrev */
63 int adiff = 0;
64
65
66 uc = (unsigned char *) user_option;
67 ac = (unsigned char *) aov_long;
68 len = saved_len;
69
70 while (len > 0 && adiff == 0)
71 {
72 /* Locate abbrev characters (uppercase) */
73 while (*ac && (islower(*ac) || *ac == '-'))
74 ++ac;
75
76 adiff = (int) tolower(*uc) - (int) tolower(*ac);
77
78 ++uc;
79 ++ac;
80 --len;
81 }
82
83 if (adiff == 0)
84 diff = 0;
85 }
86 return diff;
87 }
88
89
90
91 static int
92 petopt_parse_option(PETOPT *pop,
93 PETOPTS **pov,
94 const char **optarg)
95 {
96 int i;
97
98
99 *pov = NULL;
100 *optarg = NULL;
101
102
103 Again:
104 /* Index out of bounds? */
105 if (pop->ai >= pop->argc)
106 {
107 return POE_EOF;
108 }
109
110 if (pop->ci == 0)
111 {
112 /* Non option or "-" */
113 if (pop->argv[pop->ai][0] != '-' ||
114 (pop->argv[pop->ai][0] == '-' &&
115 pop->argv[pop->ai][1] == '\0'))
116 {
117 pop->oav[pop->oac++] = strdup(pop->argv[pop->ai++]);
118 goto Again;
119 }
120
121 /* "--", stop parsing and skip */
122 if (strcmp(pop->argv[pop->ai], "--") == 0)
123 {
124 pop->ai++;
125
126 while (pop->ai < pop->argc)
127 {
128 pop->oav[pop->oac++] = strdup(pop->argv[pop->ai++]);
129 }
130
131 return POE_EOF;
132 }
133
134 if (pop->argv[pop->ai][1] == '-')
135 {
136 /* Long option */
137 const char *opt, *arg;
138 int len, ix;
139 char *boolval = NULL;
140 int withval = 0;
141
142
143 pop->saved_ai = pop->ai;
144 pop->saved_ci = pop->ci;
145
146 opt = pop->argv[pop->ai]+2;
147 if (strncasecmp(opt, "enable-", 7) == 0)
148 {
149 boolval = "true";
150 opt += 7;
151 }
152 else if (strncasecmp(opt, "disable-", 8) == 0)
153 {
154 boolval = "false";
155 opt += 8;
156 }
157 else if (strncasecmp(opt, "with-", 5) == 0)
158 {
159 withval = 1;
160 opt += 5;
161 }
162 else if (strncasecmp(opt, "without-", 8) == 0)
163 {
164 withval = -1;
165 opt += 8;
166 }
167
168
169 arg = strchr(opt, '=');
170 if (arg)
171 len = arg - opt;
172 else
173 len = strlen(opt);
174
175
176 ix = -1;
177 for (i = 0; pop->pov[i].s != -1; i++)
178 {
179 if (pop->pov[i].l != NULL &&
180 compare_long(opt, pop->pov[i].l, len) == 0)
181 {
182 /* Match found */
183 if (ix < 0)
184 ix = i;
185 else
186 return POE_MULTI;
187 }
188 }
189
190
191 if (ix >= 0)
192 {
193 /* Found a unique match */
194
195 pop->ai++;
196
197 *pov = &pop->pov[ix];
198
199 if (pop->pov[ix].f)
200 {
201 /* Check for (optional) argument */
202
203 if (boolval)
204 {
205 /* --enable-XXX or --disable-XXX */
206
207 if (arg ||
208 (pop->pov[ix].f & POF_TYPEMASK) != POF_BOOL)
209 {
210 return POE_INVALID;
211 }
212
213 *optarg = boolval;
214 return 0;
215 }
216
217 if (withval)
218 {
219 if (withval == 1)
220 {
221 if (arg)
222 *optarg = arg+1;
223 else
224 *optarg = "yes";
225 }
226 else
227 {
228 if (arg)
229 return POE_INVALID;
230 else
231 *optarg = NULL;
232 }
233 return 0;
234 }
235
236 if (arg)
237 {
238 /* Argument after "=" */
239 *optarg = arg+1;
240 return 0;
241 }
242
243 if (pop->ai < pop->argc &&
244 (pop->argv[pop->ai][0] != '-' ||
245 pop->argv[pop->ai][0] == '\0'))
246 {
247 /* Have an argument, empty string or "-" */
248 if (((pop->pov[ix].f & POF_TYPEMASK) == POF_STR) ||
249 isdigit((int) pop->argv[pop->ai][0]))
250 {
251 *optarg = pop->argv[pop->ai];
252 pop->ai++;
253 }
254 }
255
256 /* Argument missing, and not optional? */
257 if (*optarg == NULL &&
258 !((pop->pov[ix].f & POF_OPT) ||
259 ((pop->pov[ix].f & POF_TYPEMASK) == POF_BOOL)))
260 {
261 pop->ai--;
262 return POE_MISSING;
263 }
264 }
265
266 return 0;
267 }
268
269 /* Unknown long command line switch */
270 return POE_OPTION;
271 }
272 else
273 {
274 /* Short option */
275
276 pop->ci = 1;
277 }
278 }
279
280 /* Short option */
281
282 pop->saved_ai = pop->ai;
283 pop->saved_ci = pop->ci;
284
285 i = 0;
286 while (pop->pov[i].s != -1 && pop->pov[i].s != pop->argv[pop->ai][pop->ci])
287 i++;
288
289 if (pop->pov[i].s == -1)
290 {
291 /* Unknown short command line switch */
292 return POE_OPTION;
293 }
294
295 /* Found a matching short option */
296
297 *pov = &pop->pov[i];
298 pop->ci++;
299
300 if (pop->argv[pop->ai][pop->ci] == '\0')
301 {
302 pop->ai++;
303 pop->ci = 0;
304 }
305
306 /* Have an (optional) argument? */
307 if (pop->pov[i].f)
308 {
309 if (pop->ai < pop->argc &&
310 (pop->ci != 0 ||
311 ((pop->argv[pop->ai][0] != '-') ||
312 pop->argv[pop->ai][1] == '\0')))
313 {
314 if (((pop->pov[i].f & POF_TYPEMASK) == POF_STR) ||
315 (isdigit((int) pop->argv[pop->ai][pop->ci]) ||
316 pop->argv[pop->ai][pop->ci] == '-'))
317 {
318 *optarg = pop->argv[pop->ai]+pop->ci;
319
320 pop->ai++;
321 pop->ci = 0;
322 }
323 }
324 }
325
326 /* Argument missing, and not optional? */
327 if (pop->pov[i].f &&
328 !((pop->pov[i].f & POF_OPT) ||
329 (pop->pov[i].f & POF_TYPEMASK) == POF_BOOL) &&
330 *optarg == NULL)
331 {
332 if (pop->ci == 0)
333 {
334 pop->ai--;
335 pop->ci = strlen(pop->argv[pop->ai])-1;
336 }
337
338 return POE_MISSING;
339 }
340
341 return 0;
342 }
343
344
345 int
346 petopt_print_error(PETOPT *pop,
347 PETOPTS *pov,
348 int err,
349 FILE *fp)
350 {
351 char buf[3];
352 const char *arg;
353
354
355 if (pop->saved_ci > 0)
356 {
357 buf[0] = '-';
358 buf[1] = pop->argv[pop->saved_ai][pop->saved_ci];
359 buf[2] = '\0';
360 arg = buf;
361 }
362 else
363 {
364 arg = pop->argv[pop->saved_ai];
365 if (arg == NULL)
366 arg = "";
367 }
368
369 switch (err)
370 {
371 case POE_EOF:
372 return 0;
373
374 case POE_OPTION:
375 if (pop->f & POF_SYSLOG)
376 syslog(LOG_ERR, "Unrecognized option: %s", arg);
377
378 fprintf(fp, "%s: Unrecognized option: %s\n",
379 pop->argv[0], arg);
380 break;
381
382 case POE_MULTI:
383 if (pop->f & POF_SYSLOG)
384 syslog(LOG_ERR, "Ambiguous option: %s", arg);
385
386 fprintf(fp, "%s: Ambiguous option: %s\n",
387 pop->argv[0], arg);
388 break;
389
390 case POE_MISSING:
391 if (pop->f & POF_SYSLOG)
392 syslog(LOG_ERR, "Missing argument for option: %s", arg);
393
394 fprintf(fp, "%s: Missing argument for option: %s\n",
395 pop->argv[0], arg);
396 break;
397
398 case POE_INVALID:
399 if (pop->f & POF_SYSLOG)
400 syslog(LOG_ERR, "Invalid argument for option: %s", arg);
401
402 fprintf(fp, "%s: Invalid argument for option: %s\n",
403 pop->argv[0], arg);
404 break;
405
406 case POE_INTERNAL:
407 if (pop->f & POF_SYSLOG)
408 syslog(LOG_ERR, "Internal error parsing option: %s", arg);
409
410 fprintf(fp, "%s: Internal error parsing option: %s\n",
411 pop->argv[0], arg);
412 break;
413
414 default:
415 if (pop->f & POF_SYSLOG)
416 syslog(LOG_ERR, "Internal options parsing error: #%d", err);
417
418 fprintf(fp, "%s: Internal options parsing error: #%d\n",
419 pop->argv[0], err);
420 }
421
422 return -1;
423 }
424
425
426 int
427 petopt_parse(PETOPT *pop,
428 int *o_argc,
429 char ***o_argv)
430 {
431 int err;
432 const char *arg;
433 PETOPTS *pov;
434
435
436 while ((err = petopt_parse_option(pop, &pov, &arg)) == 0)
437 {
438 if (pop->parse &&
439 (err = pop->parse(pop, pov, arg)) < 0)
440 {
441 goto Fail;
442 }
443
444 /* Parser assigned the values */
445 if (err == 0)
446 continue;
447
448 /* Option not handled, and no storage assigned? */
449 if (pov->v == NULL)
450 {
451 err = POE_INTERNAL;
452 goto Fail;
453 }
454
455 switch (pov->f & POF_TYPEMASK)
456 {
457 case POF_NONE:
458 break;
459
460 case POF_BOOL:
461 if (arg && *arg != '\0')
462 {
463 if (strcasecmp(arg, "true") == 0 ||
464 strcasecmp(arg, "yes") == 0 ||
465 strcasecmp(arg, "on") == 0)
466 {
467 *(int *)(pov->v) = 1;
468 }
469 else if (strcasecmp(arg, "false") == 0 ||
470 strcasecmp(arg, "no") == 0 ||
471 strcasecmp(arg, "off") == 0)
472 {
473 *(int *)(pov->v) = 0;
474 }
475 else
476 {
477 err = POE_INVALID;
478 goto Fail;
479 }
480 }
481 else
482 {
483 *(int *)(pov->v) = 1;
484 }
485 break;
486
487 case POF_INT:
488 if (arg && *arg != '\0')
489 {
490 /* XXX: Check for integer number */
491 *(int *)(pov->v) = atoi(arg);
492 }
493 else
494 {
495 /* XXX: Check for POF_INC flag?? */
496 ++*(int *)(pov->v);
497 }
498 break;
499
500 case POF_STR:
501 if (pov->f & POF_DUP)
502 *(char **)(pov->v) = strdup(arg);
503 else
504 *(char **)(pov->v) = (char *) arg;
505 break;
506
507 default:
508 err = POE_OPTION;
509 goto Fail;
510 }
511 }
512
513 if (err == POE_EOF)
514 err = 0;
515
516 Fail:
517 if (err)
518 err = pop->errpr(pop, pov, err,
519 (pop->f & POF_NOERRPRINT) ? NULL : stderr);
520
521 *o_argc = pop->oac;
522 *o_argv = pop->oav;
523
524 return err;
525 }
526
527
528
529 int
530 petopt_print_usage(PETOPT *pop,
531 FILE *fp)
532 {
533 int i, len;
534 struct petopt_option *pov;
535
536
537 pov = pop->pov;
538 for (i = 0; pov[i].s != -1; i++)
539 {
540 if (pov[i].s > ' ' && pov[i].s <='~')
541 fprintf(fp, " -%c, ", pov[i].s);
542 else
543 fprintf(fp, " ");
544
545 len = 0;
546 if (pov[i].l)
547 {
548 len = fprintf(fp, "--%s", pov[i].l);
549 if (pov[i].f)
550 {
551 if (pov[i].f & POF_OPT)
552 len += fprintf(fp, " [ARG]");
553 else
554 len += fprintf(fp, " ARG");
555 }
556 }
557
558 while (len++ < 30)
559 putchar(' ');
560
561 if (pov[i].h)
562 fputs(pov[i].h, fp);
563 if (pop->f & POF_PRDEFAULT && pov[i].v != NULL)
564 switch (pov[i].f & POF_TYPEMASK)
565 {
566 case POF_INT:
567 {
568 int *iv = (int*)pov[i].v;
569 if (*iv != 0)
570 fprintf(fp, " (Default: %d)", *iv);
571 }
572 break;
573
574 case POF_STR:
575 {
576 char *cv = (char*)pov[i].v;
577 if (cv)
578 fprintf(fp, " (Default: \"%s\")", cv);
579 }
580 break;
581 }
582 putc('\n', fp);
583 }
584
585 return 0;
586 }
587
588
589 int
590 petopt_setup(PETOPT **popp,
591 int f,
592 int argc,
593 char **argv,
594 PETOPTS *pov,
595 int (*parse)(PETOPT *pop, PETOPTS *pov, const char *arg),
596 int (*errpr)(PETOPT *pop, PETOPTS *pov, int err, FILE *fp))
597 {
598 PETOPT *pop = (PETOPT*)malloc(sizeof(PETOPT));
599 if (pop == NULL)
600 return errno;
601
602 memset(pop, 0, sizeof(*pop));
603
604 pop->f = f;
605
606 pop->argc = argc;
607 pop->argv = (const char **) argv;
608 pop->pov = pov;
609 pop->parse = parse;
610
611 if (errpr)
612 pop->errpr = errpr;
613 else
614 pop->errpr = petopt_print_error;
615
616 pop->ai = 1;
617 pop->ci = 0;
618
619 pop->saved_ai = 0;
620 pop->saved_ci = 0;
621
622 pop->oac = 1;
623
624 pop->oav = (char**)calloc(argc+1, sizeof(char *));
625 pop->oav[0] = strdup(pop->argv[0]);
626 pop->oav[1] = NULL;
627
628 *popp = pop;
629 return 0;
630 }
631
632
633 int
634 petopt_rewind(PETOPT *pop)
635 {
636 pop->ai = 1;
637 pop->ci = 0;
638
639 pop->oac = 1;
640 pop->oav[1] = NULL;
641
642 return 0;
643 }
644
645
646 int
647 petopt_cleanup(PETOPT *pop)
648 {
649 int i;
650
651 if (pop->oav)
652 {
653 for (i = 0; i < pop->oac; i++)
654 if (pop->oav[i])
655 free(pop->oav[i]);
656 free(pop->oav);
657 }
658
659 free(pop);
660 return 0;
661 }