5 /* (C) 19982000 Red Hat, Inc. -- Licensing details are in the COPYING
6 file accompanying popt source distributions, available from
7 ftp://ftp.rpm.org/pub/rpm/dist */
22 static char * strerror(int errno) {
24 extern char * sys_errlist[];
26 if ((0 <= errno) && (errno < sys_nerr))
27 return sys_errlist[errno];
29 return POPT_("unknown errno");
34 /*@unused@*/ static void prtcon(const char *msg, poptContext con)
36 if (msg) fprintf(stderr, "%s", msg);
37 fprintf(stderr, "\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
39 (con->os->nextCharArg ? con->os->nextCharArg : ""),
40 (con->os->nextArg ? con->os->nextArg : ""),
42 (con->os->argv && con->os->argv[con->os->next]
43 ? con->os->argv[con->os->next] : ""));
47 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
49 con->execPath = _free(con->execPath);
50 con->execPath = xstrdup(path);
51 con->execAbsolute = allowAbsolute;
54 static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
55 /*@modifies internalState@*/
58 for (; opt->longName || opt->shortName || opt->arg; opt++) {
59 if (opt->arg == NULL) continue; /* XXX program error. */
60 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
61 /* Recurse on included sub-tables. */
62 invokeCallbacksPRE(con, opt->arg);
63 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
64 (opt->argInfo & POPT_CBFLAG_PRE))
66 poptCallbackType cb = (poptCallbackType)opt->arg;
68 /* Perform callback. */
69 cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
74 static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
75 /*@modifies internalState@*/
78 for (; opt->longName || opt->shortName || opt->arg; opt++) {
79 if (opt->arg == NULL) continue; /* XXX program error. */
80 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
81 /* Recurse on included sub-tables. */
82 invokeCallbacksPOST(con, opt->arg);
83 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
84 (opt->argInfo & POPT_CBFLAG_POST))
86 poptCallbackType cb = (poptCallbackType)opt->arg;
88 /* Perform callback. */
89 cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
94 static void invokeCallbacksOPTION(poptContext con,
95 const struct poptOption * opt,
96 const struct poptOption * myOpt,
97 /*@null@*/ const void * myData, int shorty)
98 /*@modifies internalState@*/
100 const struct poptOption * cbopt = NULL;
103 for (; opt->longName || opt->shortName || opt->arg; opt++) {
104 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
105 /* Recurse on included sub-tables. */
106 if (opt->arg != NULL) /* XXX program error */
107 invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
108 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
109 !(opt->argInfo & POPT_CBFLAG_SKIPOPTION)) {
110 /* Save callback info. */
112 } else if (cbopt != NULL &&
113 ((myOpt->shortName && opt->shortName && shorty &&
114 myOpt->shortName == opt->shortName) ||
115 (myOpt->longName && opt->longName &&
116 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
117 !strcmp(myOpt->longName, opt->longName)))
121 poptCallbackType cb = (poptCallbackType)cbopt->arg;
123 const void * cbData = (cbopt->descrip ? cbopt->descrip : myData);
124 /* Perform callback. */
125 if (cb != NULL) { /* XXX program error */
126 cb(con, POPT_CALLBACK_REASON_OPTION, myOpt,
127 con->os->nextArg, cbData);
129 /* Terminate (unless explcitly continuing). */
130 if (!(cbopt->argInfo & POPT_CBFLAG_CONTINUE))
136 poptContext poptGetContext(const char * name, int argc, const char ** argv,
137 const struct poptOption * options, int flags)
139 poptContext con = malloc(sizeof(*con));
141 if (con == NULL) return NULL; /* XXX can't happen */
142 memset(con, 0, sizeof(*con));
144 con->os = con->optionStack;
145 con->os->argc = argc;
146 /*@-dependenttrans@*/ /* FIX: W2DO? */
147 con->os->argv = argv;
148 /*@=dependenttrans@*/
149 con->os->argb = NULL;
151 if (!(flags & POPT_CONTEXT_KEEP_FIRST))
152 con->os->next = 1; /* skip argv[0] */
154 con->leftovers = calloc( (argc + 1), sizeof(char *) );
155 /*@-dependenttrans@*/ /* FIX: W2DO? */
156 con->options = options;
157 /*@=dependenttrans@*/
163 con->finalArgvAlloced = argc * 2;
164 con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );
165 con->execAbsolute = 1;
166 con->arg_strip = NULL;
168 if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
169 con->flags |= POPT_CONTEXT_POSIXMEHARDER;
172 char * t = malloc(strlen(name) + 1);
173 if (t) con->appName = strcpy(t, name);
176 invokeCallbacksPRE(con, con->options);
181 static void cleanOSE(/*@special@*/ struct optionStackEntry *os)
183 /*@releases os->nextArg, os->argv, os->argb @*/
185 os->nextArg = _free(os->nextArg);
186 os->argv = _free(os->argv);
187 os->argb = PBM_FREE(os->argb);
190 void poptResetContext(poptContext con)
194 if (con == NULL) return;
195 while (con->os > con->optionStack) {
198 con->os->argb = PBM_FREE(con->os->argb);
199 con->os->currAlias = NULL;
200 con->os->nextCharArg = NULL;
201 con->os->nextArg = NULL;
202 con->os->next = 1; /* skip argv[0] */
204 con->numLeftovers = 0;
205 con->nextLeftover = 0;
206 con->restLeftover = 0;
209 if (con->finalArgv != NULL)
210 for (i = 0; i < con->finalArgvCount; i++)
211 /*@-unqualifiedtrans@*/ /* FIX: typedef double indirection. */
212 con->finalArgv[i] = _free(con->finalArgv[i]);
213 /*@=unqualifiedtrans@*/
215 con->finalArgvCount = 0;
216 con->arg_strip = PBM_FREE(con->arg_strip);
217 /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
222 /* Only one of longName, shortName should be set, not both. */
223 static int handleExec(/*@special@*/ poptContext con,
224 /*@null@*/ const char * longName, char shortName)
225 /*@uses con->execs, con->numExecs, con->flags, con->doExec,
226 con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
230 if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
232 i = con->numExecs - 1;
234 while (i >= 0 && (!con->execs[i].longName ||
235 strcmp(con->execs[i].longName, longName))) i--;
238 con->execs[i].shortName != shortName) i--;
243 if (con->flags & POPT_CONTEXT_NO_EXEC)
246 if (con->doExec == NULL) {
247 con->doExec = con->execs + i;
251 /* We already have an exec to do; remember this option for next
253 if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
254 con->finalArgvAlloced += 10;
255 con->finalArgv = realloc(con->finalArgv,
256 sizeof(*con->finalArgv) * con->finalArgvAlloced);
259 i = con->finalArgvCount++;
260 if (con->finalArgv != NULL) /* XXX can't happen */
261 { char *s = malloc((longName ? strlen(longName) : 0) + 3);
262 if (s != NULL) { /* XXX can't happen */
264 sprintf(s, "--%s", longName);
266 sprintf(s, "-%c", shortName);
267 con->finalArgv[i] = s;
269 con->finalArgv[i] = NULL;
272 /*@-nullstate@*/ /* FIX: con->finalArgv[] == NULL */
277 /* Only one of longName, shortName may be set at a time */
278 static int handleAlias(/*@special@*/ poptContext con,
279 /*@null@*/ const char * longName, char shortName,
280 /*@keep@*/ /*@null@*/ const char * nextCharArg)
281 /*@uses con->aliases, con->numAliases, con->optionStack,
282 con->os, con->os->currAlias, con->os->currAlias->longName @*/
287 if (con->os->currAlias && con->os->currAlias->longName && longName &&
288 /*@-nullpass@*/ /* LCL: con->os->currAlias->longName != NULL */
289 !strcmp(con->os->currAlias->longName, longName))
292 if (con->os->currAlias && shortName &&
293 shortName == con->os->currAlias->shortName)
296 if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
298 i = con->numAliases - 1;
300 while (i >= 0 && (!con->aliases[i].longName ||
301 strcmp(con->aliases[i].longName, longName))) i--;
304 con->aliases[i].shortName != shortName) i--;
309 if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
310 return POPT_ERROR_OPTSTOODEEP;
312 if (nextCharArg && *nextCharArg)
313 con->os->nextCharArg = nextCharArg;
317 con->os->stuffed = 0;
318 con->os->nextArg = NULL;
319 con->os->nextCharArg = NULL;
320 con->os->currAlias = con->aliases + i;
321 rc = poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv,
322 &con->os->argc, &con->os->argv);
323 con->os->argb = NULL;
325 return (rc ? rc : 1);
328 static int execCommand(poptContext con)
329 /*@modifies fileSystem @*/
337 if (con->doExec == NULL || con->doExec->script == NULL) /*XXX can't happen*/
338 return POPT_ERROR_NOARG;
339 rc = poptParseArgvString(con->doExec->script, &sargc, &sargv);
342 if (sargv == NULL || sargc < 1 ||
343 (!con->execAbsolute && strchr(sargv[0], '/')))
344 return POPT_ERROR_NOARG;
346 argv = malloc(sizeof(*argv) *
347 (6 + sargc + con->numLeftovers + con->finalArgvCount));
348 if (argv == NULL) return POPT_ERROR_MALLOC; /* XXX can't happen */
350 if (!strchr(sargv[0], '/') && con->execPath) {
351 char *s = alloca(strlen(con->execPath) + strlen(sargv[0]) + sizeof("/"));
352 sprintf(s, "%s/%s", con->execPath, sargv[0]);
355 argv[argc] = findProgramPath(sargv[0]);
357 if (argv[argc++] == NULL) return POPT_ERROR_NOARG;
360 memcpy(argv + argc, sargv + 1, sizeof(*argv) * (sargc - 1));
364 if (con->finalArgv != NULL && con->finalArgvCount > 0) {
365 memcpy(argv + argc, con->finalArgv,
366 sizeof(*argv) * con->finalArgvCount);
367 argc += con->finalArgvCount;
370 if (con->leftovers != NULL && con->numLeftovers > 0) {
374 memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
375 argc += con->numLeftovers;
381 (void) setresuid(getuid(), getuid(),-1);
384 * XXX " ... on BSD systems setuid() should be preferred over setreuid()"
385 * XXX sez' Timur Bakeyev <mc@bat.ru>
386 * XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
388 #if defined(HAVE_SETUID)
389 (void) setuid(getuid());
390 #elif defined (HAVE_SETREUID)
391 (void) setreuid(getuid(), getuid()); /*hlauer: not portable to hpux9.01 */
393 ; /* Can't drop privileges */
398 return POPT_ERROR_NOARG;
401 fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
402 for (avp = argv; *avp; avp++)
403 fprintf(stderr, " '%s'", *avp);
404 fprintf(stderr, "\n");
408 (void) execvp(argv[0], (char *const *)argv);
409 return POPT_ERROR_ERRNO;
412 /*@observer@*/ /*@null@*/ static const struct poptOption *
413 findOption(const struct poptOption * opt, /*@null@*/ const char * longName,
415 /*@null@*/ /*@out@*/ poptCallbackType * callback,
416 /*@null@*/ /*@out@*/ const void ** callbackData,
418 /*@modifies *callback, *callbackData */
420 const struct poptOption * cb = NULL;
422 /* This happens when a single - is given */
423 if (singleDash && !shortName && (longName && *longName == '\0'))
426 for (; opt->longName || opt->shortName || opt->arg; opt++) {
428 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
429 const struct poptOption * opt2;
431 /* Recurse on included sub-tables. */
432 if (opt->arg == NULL) continue; /* XXX program error */
433 opt2 = findOption(opt->arg, longName, shortName, callback,
434 callbackData, singleDash);
435 if (opt2 == NULL) continue;
436 /* Sub-table data will be inheirited if no data yet. */
437 if (!(callback && *callback)) return opt2;
438 if (!(callbackData && *callbackData == NULL)) return opt2;
439 /*@-observertrans -dependenttrans @*/
440 *callbackData = opt->descrip;
441 /*@=observertrans =dependenttrans @*/
443 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) {
445 } else if (longName && opt->longName &&
446 (!singleDash || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) &&
447 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
448 !strcmp(longName, opt->longName))
452 } else if (shortName && shortName == opt->shortName) {
457 if (!opt->longName && !opt->shortName)
459 /*@-modobserver -mods @*/
460 if (callback) *callback = NULL;
461 if (callbackData) *callbackData = NULL;
465 *callback = (poptCallbackType)cb->arg;
467 if (!(cb->argInfo & POPT_CBFLAG_INC_DATA)) {
469 /*@-observertrans@*/ /* FIX: typedef double indirection. */
470 *callbackData = cb->descrip;
474 /*@=modobserver =mods @*/
479 static const char * findNextArg(/*@special@*/ poptContext con,
480 unsigned argx, int delete)
481 /*@uses con->optionStack, con->os,
482 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
484 struct optionStackEntry * os = con->os;
490 while (os->next == os->argc && os > con->optionStack) os--;
491 if (os->next == os->argc && os == con->optionStack) break;
492 if (os->argv != NULL)
493 for (i = os->next; i < os->argc; i++) {
494 if (os->argb && PBM_ISSET(i, os->argb)) continue;
495 if (*os->argv[i] == '-') continue;
496 if (--argx > 0) continue;
499 if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
500 if (os->argb != NULL) /* XXX can't happen */
501 PBM_SET(i, os->argb);
505 if (os > con->optionStack) os--;
506 } while (arg == NULL);
510 static /*@only@*/ /*@null@*/ const char *
511 expandNextArg(/*@special@*/ poptContext con, const char * s)
512 /*@uses con->optionStack, con->os,
513 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
516 const char * a = NULL;
519 size_t tn = strlen(s) + 1;
522 te = t = malloc(tn);;
523 if (t == NULL) return NULL; /* XXX can't happen */
524 while ((c = *s++) != '\0') {
526 #if 0 /* XXX can't do this */
527 case '\\': /* escape */
532 if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
534 /* XXX Make sure that findNextArg deletes only next arg. */
536 if ((a = findNextArg(con, 1, 1)) == NULL) break;
545 strncpy(te, a, alen); te += alen;
547 /*@notreached@*/ break;
554 t = realloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */
558 static void poptStripArg(poptContext con, int which)
561 if (con->arg_strip == NULL)
562 con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
563 if (con->arg_strip != NULL) /* XXX can't happen */
564 PBM_SET(which, con->arg_strip);
567 static int poptSaveLong(const struct poptOption * opt, long aLong)
568 /*@modifies opt->arg @*/
570 if (opt->arg == NULL)
571 return POPT_ERROR_NULLARG;
573 if (opt->argInfo & POPT_ARGFLAG_NOT)
575 switch (opt->argInfo & POPT_ARGFLAG_LOGICALOPS) {
577 *((long *) opt->arg) = aLong;
579 case POPT_ARGFLAG_OR:
580 *((long *) opt->arg) |= aLong;
582 case POPT_ARGFLAG_AND:
583 *((long *) opt->arg) &= aLong;
585 case POPT_ARGFLAG_XOR:
586 *((long *) opt->arg) ^= aLong;
589 return POPT_ERROR_BADOPERATION;
590 /*@notreached@*/ break;
595 static int poptSaveInt(const struct poptOption * opt, long aLong)
596 /*@modifies opt->arg @*/
598 if (opt->arg == NULL)
599 return POPT_ERROR_NULLARG;
601 if (opt->argInfo & POPT_ARGFLAG_NOT)
603 switch (opt->argInfo & POPT_ARGFLAG_LOGICALOPS) {
605 *((int *) opt->arg) = aLong;
607 case POPT_ARGFLAG_OR:
608 *((int *) opt->arg) |= aLong;
610 case POPT_ARGFLAG_AND:
611 *((int *) opt->arg) &= aLong;
613 case POPT_ARGFLAG_XOR:
614 *((int *) opt->arg) ^= aLong;
617 return POPT_ERROR_BADOPERATION;
618 /*@notreached@*/ break;
623 /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
624 int poptGetNextOpt(poptContext con)
626 const struct poptOption * opt = NULL;
632 const char * origOptString = NULL;
633 poptCallbackType cb = NULL;
634 const void * cbData = NULL;
635 const char * longArg = NULL;
639 while (!con->os->nextCharArg && con->os->next == con->os->argc
640 && con->os > con->optionStack) {
643 if (!con->os->nextCharArg && con->os->next == con->os->argc) {
644 invokeCallbacksPOST(con, con->options);
645 if (con->doExec) return execCommand(con);
649 /* Process next long option */
650 if (!con->os->nextCharArg) {
651 char * localOptString, * optString;
654 if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
658 thisopt = con->os->next;
659 if (con->os->argv != NULL) /* XXX can't happen */
660 origOptString = con->os->argv[con->os->next++];
662 if (origOptString == NULL) /* XXX can't happen */
663 return POPT_ERROR_BADOPT;
665 if (con->restLeftover || *origOptString != '-') {
666 if (con->leftovers != NULL) /* XXX can't happen */
667 con->leftovers[con->numLeftovers++] = origOptString;
668 if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
669 con->restLeftover = 1;
673 /* Make a copy we can hack at */
674 localOptString = optString =
675 strcpy(alloca(strlen(origOptString) + 1), origOptString);
677 if (optString[0] == '\0')
678 return POPT_ERROR_BADOPT;
680 if (optString[1] == '-' && !optString[2]) {
681 con->restLeftover = 1;
688 if (*optString == '-')
689 singleDash = 0, optString++;
693 /* XXX aliases with arg substitution need "--alias=arg" */
694 if (handleAlias(con, optString, '\0', NULL))
697 if (handleExec(con, optString, '\0'))
700 /* Check for "--long=arg" option. */
701 for (oe = optString; *oe && *oe != '='; oe++)
705 /* XXX longArg is mapped back to persistent storage. */
706 longArg = origOptString + (oe - localOptString);
709 opt = findOption(con->options, optString, '\0', &cb, &cbData,
711 if (!opt && !singleDash)
712 return POPT_ERROR_BADOPT;
716 con->os->nextCharArg = origOptString + 1;
718 if (con->os == con->optionStack &&
719 opt->argInfo & POPT_ARGFLAG_STRIP)
722 poptStripArg(con, thisopt);
728 /* Process next short option */
729 if (con->os->nextCharArg) {
730 origOptString = con->os->nextCharArg;
732 con->os->nextCharArg = NULL;
734 if (handleAlias(con, NULL, *origOptString, origOptString + 1))
737 if (handleExec(con, NULL, *origOptString)) {
738 /* Restore rest of short options for further processing */
740 if (*origOptString != '\0')
741 con->os->nextCharArg = origOptString;
745 opt = findOption(con->options, NULL, *origOptString, &cb,
748 return POPT_ERROR_BADOPT;
752 /*@-branchstate@*/ /* FIX: W2DO? */
753 if (*origOptString != '\0')
754 con->os->nextCharArg = origOptString;
758 if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */
759 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) {
760 if (poptSaveInt(opt, 1L))
761 return POPT_ERROR_BADOPERATION;
762 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) {
764 if (poptSaveInt(opt, (long)opt->val))
765 return POPT_ERROR_BADOPERATION;
767 } else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
768 con->os->nextArg = _free(con->os->nextArg);
769 /*@-usedef@*/ /* FIX: W2DO? */
772 longArg = expandNextArg(con, longArg);
773 con->os->nextArg = longArg;
774 } else if (con->os->nextCharArg) {
775 longArg = expandNextArg(con, con->os->nextCharArg);
776 con->os->nextArg = longArg;
777 con->os->nextCharArg = NULL;
779 while (con->os->next == con->os->argc &&
780 con->os > con->optionStack) {
783 if (con->os->next == con->os->argc) {
784 if (opt->argInfo & POPT_ARGFLAG_OPTIONAL)
785 con->os->nextArg = NULL;
787 return POPT_ERROR_NOARG;
791 * Make sure this isn't part of a short arg or the
792 * result of an alias expansion.
794 if (con->os == con->optionStack &&
795 (opt->argInfo & POPT_ARGFLAG_STRIP) &&
797 poptStripArg(con, con->os->next);
800 if (con->os->argv != NULL) { /* XXX can't happen */
801 /* XXX watchout: subtle side-effects live here. */
802 longArg = con->os->argv[con->os->next++];
803 longArg = expandNextArg(con, longArg);
804 con->os->nextArg = longArg;
811 switch (opt->argInfo & POPT_ARG_MASK) {
812 case POPT_ARG_STRING:
813 /* XXX memory leak, hard to plug */
814 *((const char **) opt->arg) = (con->os->nextArg)
815 ? xstrdup(con->os->nextArg) : NULL;
823 if (con->os->nextArg) {
824 aLong = strtol(con->os->nextArg, &end, 0);
825 if (!(end && *end == '\0'))
826 return POPT_ERROR_BADNUMBER;
829 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) {
830 if (aLong == LONG_MIN || aLong == LONG_MAX)
831 return POPT_ERROR_OVERFLOW;
832 if (poptSaveLong(opt, aLong))
833 return POPT_ERROR_BADOPERATION;
835 if (aLong > INT_MAX || aLong < INT_MIN)
836 return POPT_ERROR_OVERFLOW;
837 if (poptSaveInt(opt, aLong))
838 return POPT_ERROR_BADOPERATION;
843 case POPT_ARG_DOUBLE:
844 { double aDouble = 0.0;
847 if (con->os->nextArg) {
848 int saveerrno = errno;
850 aDouble = strtod(con->os->nextArg, &end);
852 return POPT_ERROR_OVERFLOW;
855 return POPT_ERROR_BADNUMBER;
858 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_DOUBLE) {
859 *((double *) opt->arg) = aDouble;
861 #define _ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
862 if ((_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
863 return POPT_ERROR_OVERFLOW;
864 if ((FLT_MIN - _ABS(aDouble)) > DBL_EPSILON)
865 return POPT_ERROR_OVERFLOW;
866 *((float *) opt->arg) = aDouble;
871 POPT_("option type (%d) not implemented in popt\n"),
872 (opt->argInfo & POPT_ARG_MASK));
879 invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
880 else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
883 if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
884 con->finalArgvAlloced += 10;
885 con->finalArgv = realloc(con->finalArgv,
886 sizeof(*con->finalArgv) * con->finalArgvAlloced);
889 if (con->finalArgv != NULL)
890 { char *s = malloc((opt->longName ? strlen(opt->longName) : 0) + 3);
891 if (s != NULL) { /* XXX can't happen */
894 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
897 sprintf(s, "-%c", opt->shortName);
898 con->finalArgv[con->finalArgvCount++] = s;
900 con->finalArgv[con->finalArgvCount++] = NULL;
903 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE)
904 /*@-ifempty@*/ ; /*@=ifempty@*/
905 else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL)
906 /*@-ifempty@*/ ; /*@=ifempty@*/
907 else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
908 if (con->finalArgv != NULL && con->os->nextArg)
909 con->finalArgv[con->finalArgvCount++] =
910 /*@-nullpass@*/ /* LCL: con->os->nextArg != NULL */
911 xstrdup(con->os->nextArg);
916 return (opt ? opt->val : -1); /* XXX can't happen */
919 const char * poptGetOptArg(poptContext con)
921 const char * ret = NULL;
923 ret = con->os->nextArg;
924 con->os->nextArg = NULL;
929 const char * poptGetArg(poptContext con)
931 const char * ret = NULL;
932 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
933 ret = con->leftovers[con->nextLeftover++];
937 const char * poptPeekArg(poptContext con)
939 const char * ret = NULL;
940 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
941 ret = con->leftovers[con->nextLeftover];
945 const char ** poptGetArgs(poptContext con)
948 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
951 /* some apps like [like RPM ;-) ] need this NULL terminated */
952 con->leftovers[con->numLeftovers] = NULL;
954 /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */
955 return (con->leftovers + con->nextLeftover);
956 /*@=nullret =nullstate @*/
959 poptContext poptFreeContext(poptContext con)
963 if (con == NULL) return con;
964 poptResetContext(con);
965 con->os->argb = _free(con->os->argb);
967 if (con->aliases != NULL)
968 for (i = 0; i < con->numAliases; i++) {
969 con->aliases[i].longName = _free(con->aliases[i].longName);
970 con->aliases[i].argv = _free(con->aliases[i].argv);
973 if (con->execs != NULL)
974 for (i = 0; i < con->numExecs; i++) {
975 con->execs[i].longName = _free(con->execs[i].longName);
976 con->execs[i].script = _free(con->execs[i].script);
978 con->execs = _free(con->execs);
980 con->leftovers = _free(con->leftovers);
981 con->finalArgv = _free(con->finalArgv);
982 con->appName = _free(con->appName);
983 con->aliases = _free(con->aliases);
984 con->otherHelp = _free(con->otherHelp);
985 con->execPath = _free(con->execPath);
986 con->arg_strip = PBM_FREE(con->arg_strip);
992 int poptAddAlias(poptContext con, struct poptAlias newAlias,
993 /*@unused@*/ int flags)
995 int aliasNum = con->numAliases++;
996 struct poptAlias * alias;
998 /* SunOS won't realloc(NULL, ...) */
1000 con->aliases = malloc(sizeof(newAlias) * con->numAliases);
1002 con->aliases = realloc(con->aliases,
1003 sizeof(newAlias) * con->numAliases);
1004 alias = con->aliases + aliasNum;
1006 alias->longName = (newAlias.longName)
1007 /*@-nullpass@*/ /* FIX: malloc can return NULL. */
1008 ? strcpy(malloc(strlen(newAlias.longName) + 1), newAlias.longName)
1011 alias->shortName = newAlias.shortName;
1012 alias->argc = newAlias.argc;
1013 alias->argv = newAlias.argv;
1018 const char * poptBadOption(poptContext con, int flags)
1020 struct optionStackEntry * os = NULL;
1023 os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
1025 /*@-nullderef@*/ /* LCL: os->argv != NULL */
1026 return (os && os->argv ? os->argv[os->next - 1] : NULL);
1030 const char *const poptStrerror(const int error)
1033 case POPT_ERROR_NOARG:
1034 return POPT_("missing argument");
1035 case POPT_ERROR_BADOPT:
1036 return POPT_("unknown option");
1037 case POPT_ERROR_BADOPERATION:
1038 return POPT_("mutually exclusive logical operations requested");
1039 case POPT_ERROR_NULLARG:
1040 return POPT_("opt->arg should not be NULL");
1041 case POPT_ERROR_OPTSTOODEEP:
1042 return POPT_("aliases nested too deeply");
1043 case POPT_ERROR_BADQUOTE:
1044 return POPT_("error in parameter quoting");
1045 case POPT_ERROR_BADNUMBER:
1046 return POPT_("invalid numeric value");
1047 case POPT_ERROR_OVERFLOW:
1048 return POPT_("number too large or too small");
1049 case POPT_ERROR_MALLOC:
1050 return POPT_("memory allocation failed");
1051 case POPT_ERROR_ERRNO:
1052 return strerror(errno);
1054 return POPT_("unknown error");
1058 int poptStuffArgs(poptContext con, const char ** argv)
1063 if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1064 return POPT_ERROR_OPTSTOODEEP;
1066 for (argc = 0; argv[argc]; argc++)
1071 con->os->nextArg = NULL;
1072 con->os->nextCharArg = NULL;
1073 con->os->currAlias = NULL;
1074 rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
1075 con->os->argb = NULL;
1076 con->os->stuffed = 1;
1081 const char * poptGetInvocationName(poptContext con)
1083 return (con->os->argv ? con->os->argv[0] : "");
1086 int poptStrippedArgv(poptContext con, int argc, char ** argv)
1093 for (i = 1; i < argc; i++) {
1094 if (PBM_ISSET(i, con->arg_strip))
1098 for (i = 1; i < argc; i++) {
1099 if (con->arg_strip && PBM_ISSET(i, con->arg_strip))
1101 argv[j] = (j < numargs) ? argv[i] : NULL;