5 /* (C) 1998-2002 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 #define DBL_EPSILON 2.2204460492503131e-16
30 #if !defined(HAVE_STRERROR) && !defined(__LCLINT__)
31 static char * strerror(int errno)
34 extern char * sys_errlist[];
36 if ((0 <= errno) && (errno < sys_nerr))
37 return sys_errlist[errno];
39 return POPT_("unknown errno");
45 static void prtcon(const char *msg, poptContext con)
47 if (msg) fprintf(stderr, "%s", msg);
48 fprintf(stderr, "\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
50 (con->os->nextCharArg ? con->os->nextCharArg : ""),
51 (con->os->nextArg ? con->os->nextArg : ""),
53 (con->os->argv && con->os->argv[con->os->next]
54 ? con->os->argv[con->os->next] : ""));
58 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
60 con->execPath = _free(con->execPath);
61 con->execPath = xstrdup(path);
62 con->execAbsolute = allowAbsolute;
63 /*@-nullstate@*/ /* LCL: con->execPath not NULL */
68 static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
69 /*@globals internalState@*/
70 /*@modifies internalState@*/
73 for (; opt->longName || opt->shortName || opt->arg; opt++) {
74 if (opt->arg == NULL) continue; /* XXX program error. */
75 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
76 void * arg = opt->arg;
78 /* XXX sick hack to preserve pretense of ABI. */
79 if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
81 /* Recurse on included sub-tables. */
82 invokeCallbacksPRE(con, arg);
83 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
84 (opt->argInfo & POPT_CBFLAG_PRE))
86 poptCallbackType cb = (poptCallbackType)opt->arg;
88 /* Perform callback. */
90 cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
96 static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
97 /*@globals internalState@*/
98 /*@modifies internalState@*/
101 for (; opt->longName || opt->shortName || opt->arg; opt++) {
102 if (opt->arg == NULL) continue; /* XXX program error. */
103 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
104 void * arg = opt->arg;
106 /* XXX sick hack to preserve pretense of ABI. */
107 if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
109 /* Recurse on included sub-tables. */
110 invokeCallbacksPOST(con, arg);
111 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
112 (opt->argInfo & POPT_CBFLAG_POST))
114 poptCallbackType cb = (poptCallbackType)opt->arg;
116 /* Perform callback. */
117 /*@-noeffectuncon @*/
118 cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
119 /*@=noeffectuncon @*/
124 static void invokeCallbacksOPTION(poptContext con,
125 const struct poptOption * opt,
126 const struct poptOption * myOpt,
127 /*@null@*/ const void * myData, int shorty)
128 /*@globals internalState@*/
129 /*@modifies internalState@*/
131 const struct poptOption * cbopt = NULL;
134 for (; opt->longName || opt->shortName || opt->arg; opt++) {
135 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
136 void * arg = opt->arg;
138 /* XXX sick hack to preserve pretense of ABI. */
139 if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
141 /* Recurse on included sub-tables. */
142 if (opt->arg != NULL) /* XXX program error */
143 invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
144 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
145 !(opt->argInfo & POPT_CBFLAG_SKIPOPTION)) {
146 /* Save callback info. */
148 } else if (cbopt != NULL &&
149 ((myOpt->shortName && opt->shortName && shorty &&
150 myOpt->shortName == opt->shortName) ||
151 (myOpt->longName && opt->longName &&
152 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
153 !strcmp(myOpt->longName, opt->longName)))
157 poptCallbackType cb = (poptCallbackType)cbopt->arg;
159 const void * cbData = (cbopt->descrip ? cbopt->descrip : myData);
160 /* Perform callback. */
161 if (cb != NULL) { /* XXX program error */
162 /*@-noeffectuncon @*/
163 cb(con, POPT_CALLBACK_REASON_OPTION, myOpt,
164 con->os->nextArg, cbData);
165 /*@=noeffectuncon @*/
167 /* Terminate (unless explcitly continuing). */
168 if (!(cbopt->argInfo & POPT_CBFLAG_CONTINUE))
174 poptContext poptGetContext(const char * name, int argc, const char ** argv,
175 const struct poptOption * options, int flags)
177 poptContext con = malloc(sizeof(*con));
179 if (con == NULL) return NULL; /* XXX can't happen */
180 memset(con, 0, sizeof(*con));
182 con->os = con->optionStack;
183 con->os->argc = argc;
184 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
185 con->os->argv = argv;
186 /*@=dependenttrans =assignexpose@*/
187 con->os->argb = NULL;
189 if (!(flags & POPT_CONTEXT_KEEP_FIRST))
190 con->os->next = 1; /* skip argv[0] */
192 con->leftovers = calloc( (argc + 1), sizeof(*con->leftovers) );
193 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
194 con->options = options;
195 /*@=dependenttrans =assignexpose@*/
201 con->finalArgvAlloced = argc * 2;
202 con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );
203 con->execAbsolute = 1;
204 con->arg_strip = NULL;
206 if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
207 con->flags |= POPT_CONTEXT_POSIXMEHARDER;
210 size_t bufsize = strlen(name) + 1;
211 char * t = malloc(bufsize);
213 strlcpy(t, name, bufsize);
219 invokeCallbacksPRE(con, con->options);
225 static void cleanOSE(/*@special@*/ struct optionStackEntry *os)
227 /*@releases os->nextArg, os->argv, os->argb @*/
230 os->nextArg = _free(os->nextArg);
231 os->argv = _free(os->argv);
232 os->argb = PBM_FREE(os->argb);
236 void poptResetContext(poptContext con)
240 if (con == NULL) return;
241 while (con->os > con->optionStack) {
244 con->os->argb = PBM_FREE(con->os->argb);
245 con->os->currAlias = NULL;
246 con->os->nextCharArg = NULL;
247 con->os->nextArg = NULL;
248 con->os->next = 1; /* skip argv[0] */
250 con->numLeftovers = 0;
251 con->nextLeftover = 0;
252 con->restLeftover = 0;
255 if (con->finalArgv != NULL)
256 for (i = 0; i < con->finalArgvCount; i++) {
257 /*@-unqualifiedtrans@*/ /* FIX: typedef double indirection. */
258 con->finalArgv[i] = _free(con->finalArgv[i]);
259 /*@=unqualifiedtrans@*/
262 con->finalArgvCount = 0;
263 con->arg_strip = PBM_FREE(con->arg_strip);
264 /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
270 /* Only one of longName, shortName should be set, not both. */
272 static int handleExec(/*@special@*/ poptContext con,
273 /*@null@*/ const char * longName, char shortName)
274 /*@uses con->execs, con->numExecs, con->flags, con->doExec,
275 con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
281 if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
284 for (i = con->numExecs - 1; i >= 0; i--) {
285 item = con->execs + i;
286 if (longName && !(item->option.longName &&
287 !strcmp(longName, item->option.longName)))
289 else if (shortName != item->option.shortName)
296 if (con->flags & POPT_CONTEXT_NO_EXEC)
299 if (con->doExec == NULL) {
300 con->doExec = con->execs + i;
304 /* We already have an exec to do; remember this option for next
306 if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
307 con->finalArgvAlloced += 10;
308 con->finalArgv = realloc(con->finalArgv,
309 sizeof(*con->finalArgv) * con->finalArgvAlloced);
312 i = con->finalArgvCount++;
313 if (con->finalArgv != NULL) /* XXX can't happen */
314 { size_t bufsize = (longName ? strlen(longName) : 0) + 3;
315 char *s = malloc(bufsize);
316 if (s != NULL) { /* XXX can't happen */
318 snprintf(s, bufsize, "--%s", longName);
320 snprintf(s, bufsize, "-%c", shortName);
321 con->finalArgv[i] = s;
323 con->finalArgv[i] = NULL;
326 /*@-nullstate@*/ /* FIX: con->finalArgv[] == NULL */
332 /* Only one of longName, shortName may be set at a time */
333 static int handleAlias(/*@special@*/ poptContext con,
334 /*@null@*/ const char * longName, char shortName,
335 /*@exposed@*/ /*@null@*/ const char * nextCharArg)
336 /*@uses con->aliases, con->numAliases, con->optionStack, con->os,
337 con->os->currAlias, con->os->currAlias->option.longName @*/
340 poptItem item = con->os->currAlias;
345 if (longName && (item->option.longName &&
346 !strcmp(longName, item->option.longName)))
348 if (shortName && shortName == item->option.shortName)
352 if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
355 for (i = con->numAliases - 1; i >= 0; i--) {
356 item = con->aliases + i;
357 if (longName && !(item->option.longName &&
358 !strcmp(longName, item->option.longName)))
360 else if (shortName != item->option.shortName)
366 if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
367 return POPT_ERROR_OPTSTOODEEP;
370 if (nextCharArg && *nextCharArg)
371 con->os->nextCharArg = nextCharArg;
376 con->os->stuffed = 0;
377 con->os->nextArg = NULL;
378 con->os->nextCharArg = NULL;
379 con->os->currAlias = con->aliases + i;
380 rc = poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv,
381 &con->os->argc, &con->os->argv);
382 con->os->argb = NULL;
384 return (rc ? rc : 1);
387 /*@-bounds -boundswrite @*/
388 static int execCommand(poptContext con)
389 /*@globals internalState @*/
390 /*@modifies internalState @*/
392 poptItem item = con->doExec;
397 if (item == NULL) /*XXX can't happen*/
398 return POPT_ERROR_NOARG;
400 if (item->argv == NULL || item->argc < 1 ||
401 (!con->execAbsolute && strchr(item->argv[0], '/')))
402 return POPT_ERROR_NOARG;
404 argv = malloc(sizeof(*argv) *
405 (6 + item->argc + con->numLeftovers + con->finalArgvCount));
406 if (argv == NULL) return POPT_ERROR_MALLOC;
408 if (!strchr(item->argv[0], '/') && con->execPath != NULL) {
409 size_t bufsize = strlen(con->execPath) + strlen(item->argv[0]) + sizeof "/";
410 char *s = alloca(bufsize);
411 snprintf(s, bufsize, "%s/%s", con->execPath, item->argv[0]);
414 argv[argc] = findProgramPath(item->argv[0]);
415 if (argv[argc++] == NULL) return POPT_ERROR_NOARG;
417 if (item->argc > 1) {
418 memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
419 argc += (item->argc - 1);
422 if (con->finalArgv != NULL && con->finalArgvCount > 0) {
423 memcpy(argv + argc, con->finalArgv,
424 sizeof(*argv) * con->finalArgvCount);
425 argc += con->finalArgvCount;
428 if (con->leftovers != NULL && con->numLeftovers > 0) {
429 memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
430 argc += con->numLeftovers;
436 rc = setresgid(getgid(), getgid(),-1);
437 if (rc) return POPT_ERROR_ERRNO;
438 rc = setresuid(getuid(), getuid(),-1);
439 if (rc) return POPT_ERROR_ERRNO;
442 * XXX " ... on BSD systems setuid() should be preferred over setreuid()"
443 * XXX sez' Timur Bakeyev <mc@bat.ru>
444 * XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
446 #if defined(HAVE_SETUID)
447 rc = setgid(getgid());
448 if (rc) return POPT_ERROR_ERRNO;
449 rc = setuid(getuid());
450 if (rc) return POPT_ERROR_ERRNO;
451 #elif defined (HAVE_SETREUID)
452 rc = setregid(getgid(), getgid());
453 if (rc) return POPT_ERROR_ERRNO;
454 rc = setreuid(getuid(), getuid());
455 if (rc) return POPT_ERROR_ERRNO;
457 ; /* Can't drop privileges */
462 return POPT_ERROR_NOARG;
467 fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
468 for (avp = argv; *avp; avp++)
469 fprintf(stderr, " '%s'", *avp);
470 fprintf(stderr, "\n");
474 rc = execvp(argv[0], (char *const *)argv);
476 return POPT_ERROR_ERRNO;
478 /*@=bounds =boundswrite @*/
481 /*@observer@*/ /*@null@*/ static const struct poptOption *
482 findOption(const struct poptOption * opt, /*@null@*/ const char * longName,
484 /*@null@*/ /*@out@*/ poptCallbackType * callback,
485 /*@null@*/ /*@out@*/ const void ** callbackData,
487 /*@modifies *callback, *callbackData */
489 const struct poptOption * cb = NULL;
491 /* This happens when a single - is given */
492 if (singleDash && !shortName && (longName && *longName == '\0'))
495 for (; opt->longName || opt->shortName || opt->arg; opt++) {
497 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
498 const struct poptOption * opt2;
499 void * arg = opt->arg;
502 /* XXX sick hack to preserve pretense of ABI. */
503 if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
505 /* Recurse on included sub-tables. */
506 if (arg == NULL) continue; /* XXX program error */
507 opt2 = findOption(arg, longName, shortName, callback,
508 callbackData, singleDash);
509 if (opt2 == NULL) continue;
510 /* Sub-table data will be inheirited if no data yet. */
511 if (!(callback && *callback)) return opt2;
512 if (!(callbackData && *callbackData == NULL)) return opt2;
513 /*@-observertrans -dependenttrans @*/
514 *callbackData = opt->descrip;
515 /*@=observertrans =dependenttrans @*/
517 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) {
519 } else if (longName && opt->longName &&
520 (!singleDash || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) &&
521 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
522 !strcmp(longName, opt->longName))
526 } else if (shortName && shortName == opt->shortName) {
531 if (!opt->longName && !opt->shortName)
533 /*@-modobserver -mods @*/
534 if (callback) *callback = NULL;
535 if (callbackData) *callbackData = NULL;
539 *callback = (poptCallbackType)cb->arg;
541 if (!(cb->argInfo & POPT_CBFLAG_INC_DATA)) {
543 /*@-observertrans@*/ /* FIX: typedef double indirection. */
544 *callbackData = cb->descrip;
548 /*@=modobserver =mods @*/
554 static const char * findNextArg(/*@special@*/ poptContext con,
555 unsigned argx, int delete_arg)
556 /*@uses con->optionStack, con->os,
557 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
560 struct optionStackEntry * os = con->os;
566 while (os->next == os->argc && os > con->optionStack) os--;
567 if (os->next == os->argc && os == con->optionStack) break;
568 if (os->argv != NULL)
569 for (i = os->next; i < os->argc; i++) {
571 if (os->argb && PBM_ISSET(i, os->argb))
572 /*@innercontinue@*/ continue;
573 if (*os->argv[i] == '-')
574 /*@innercontinue@*/ continue;
576 /*@innercontinue@*/ continue;
579 if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
580 if (os->argb != NULL) /* XXX can't happen */
581 PBM_SET(i, os->argb);
583 /*@innerbreak@*/ break;
586 if (os > con->optionStack) os--;
587 } while (arg == NULL);
592 static /*@only@*/ /*@null@*/ const char *
593 expandNextArg(/*@special@*/ poptContext con, const char * s)
594 /*@uses con->optionStack, con->os,
595 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
598 const char * a = NULL;
601 size_t tn = strlen(s) + 1;
604 te = t = malloc(tn);;
605 if (t == NULL) return NULL; /* XXX can't happen */
606 while ((c = *s++) != '\0') {
608 #if 0 /* XXX can't do this */
609 case '\\': /* escape */
611 /*@switchbreak@*/ break;
614 if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
615 /*@switchbreak@*/ break;
616 /* XXX Make sure that findNextArg deletes only next arg. */
618 if ((a = findNextArg(con, 1, 1)) == NULL)
619 /*@switchbreak@*/ break;
628 strncpy(te, a, alen); te += alen;
630 /*@notreached@*/ /*@switchbreak@*/ break;
632 /*@switchbreak@*/ break;
637 t = realloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */
642 static void poptStripArg(/*@special@*/ poptContext con, int which)
643 /*@uses con->arg_strip, con->optionStack @*/
644 /*@defines con->arg_strip @*/
648 if (con->arg_strip == NULL)
649 con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
650 if (con->arg_strip != NULL) /* XXX can't happen */
651 PBM_SET(which, con->arg_strip);
653 /*@-compdef@*/ /* LCL: con->arg_strip udefined? */
658 int poptSaveLong(long * arg, int argInfo, long aLong)
660 /* XXX Check alignment, may fail on funky platforms. */
661 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
662 return POPT_ERROR_NULLARG;
664 if (argInfo & POPT_ARGFLAG_NOT)
666 switch (argInfo & POPT_ARGFLAG_LOGICALOPS) {
670 case POPT_ARGFLAG_OR:
673 case POPT_ARGFLAG_AND:
676 case POPT_ARGFLAG_XOR:
680 return POPT_ERROR_BADOPERATION;
681 /*@notreached@*/ break;
686 int poptSaveInt(/*@null@*/ int * arg, int argInfo, long aLong)
688 /* XXX Check alignment, may fail on funky platforms. */
689 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
690 return POPT_ERROR_NULLARG;
692 if (argInfo & POPT_ARGFLAG_NOT)
694 switch (argInfo & POPT_ARGFLAG_LOGICALOPS) {
698 case POPT_ARGFLAG_OR:
701 case POPT_ARGFLAG_AND:
704 case POPT_ARGFLAG_XOR:
708 return POPT_ERROR_BADOPERATION;
709 /*@notreached@*/ break;
715 /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
716 int poptGetNextOpt(poptContext con)
718 const struct poptOption * opt = NULL;
724 const char * origOptString = NULL;
725 poptCallbackType cb = NULL;
726 const void * cbData = NULL;
727 const char * longArg = NULL;
731 while (!con->os->nextCharArg && con->os->next == con->os->argc
732 && con->os > con->optionStack) {
735 if (!con->os->nextCharArg && con->os->next == con->os->argc) {
737 invokeCallbacksPOST(con, con->options);
739 if (con->doExec) return execCommand(con);
743 /* Process next long option */
744 if (!con->os->nextCharArg) {
745 char * localOptString, * optString;
749 if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
754 thisopt = con->os->next;
755 if (con->os->argv != NULL) /* XXX can't happen */
756 origOptString = con->os->argv[con->os->next++];
758 if (origOptString == NULL) /* XXX can't happen */
759 return POPT_ERROR_BADOPT;
761 if (con->restLeftover || *origOptString != '-') {
762 if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
763 con->restLeftover = 1;
764 if (con->flags & POPT_CONTEXT_ARG_OPTS) {
765 con->os->nextArg = xstrdup(origOptString);
768 if (con->leftovers != NULL) /* XXX can't happen */
769 con->leftovers[con->numLeftovers++] = origOptString;
773 /* Make a copy we can hack at */
774 { size_t bufsize = strlen(origOptString) + 1;
775 localOptString = optString = alloca(bufsize);
776 if (optString == NULL) /* XXX can't happen */
777 return POPT_ERROR_BADOPT;
778 strlcpy(optString, origOptString, bufsize);
781 if (optString[0] == '\0')
782 return POPT_ERROR_BADOPT;
784 if (optString[1] == '-' && !optString[2]) {
785 con->restLeftover = 1;
792 if (*optString == '-')
793 singleDash = 0, optString++;
797 /* XXX aliases with arg substitution need "--alias=arg" */
798 if (handleAlias(con, optString, '\0', NULL))
801 if (handleExec(con, optString, '\0'))
804 /* Check for "--long=arg" option. */
805 for (oe = optString; *oe && *oe != '='; oe++)
809 /* XXX longArg is mapped back to persistent storage. */
810 longArg = origOptString + (oe - localOptString);
813 opt = findOption(con->options, optString, '\0', &cb, &cbData,
815 if (!opt && !singleDash)
816 return POPT_ERROR_BADOPT;
820 con->os->nextCharArg = origOptString + 1;
822 if (con->os == con->optionStack &&
823 opt->argInfo & POPT_ARGFLAG_STRIP)
826 poptStripArg(con, thisopt);
832 /* Process next short option */
833 /*@-branchstate@*/ /* FIX: W2DO? */
834 if (con->os->nextCharArg) {
835 origOptString = con->os->nextCharArg;
837 con->os->nextCharArg = NULL;
839 if (handleAlias(con, NULL, *origOptString, origOptString + 1))
842 if (handleExec(con, NULL, *origOptString)) {
843 /* Restore rest of short options for further processing */
845 if (*origOptString != '\0')
846 con->os->nextCharArg = origOptString;
850 opt = findOption(con->options, NULL, *origOptString, &cb,
853 return POPT_ERROR_BADOPT;
857 if (*origOptString != '\0')
858 con->os->nextCharArg = origOptString;
862 if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */
863 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) {
864 if (poptSaveInt((int *)opt->arg, opt->argInfo, 1L))
865 return POPT_ERROR_BADOPERATION;
866 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) {
868 if (poptSaveInt((int *)opt->arg, opt->argInfo, (long)opt->val))
869 return POPT_ERROR_BADOPERATION;
871 } else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
872 con->os->nextArg = _free(con->os->nextArg);
873 /*@-usedef@*/ /* FIX: W2DO? */
876 longArg = expandNextArg(con, longArg);
877 con->os->nextArg = longArg;
878 } else if (con->os->nextCharArg) {
879 longArg = expandNextArg(con, con->os->nextCharArg);
880 con->os->nextArg = longArg;
881 con->os->nextCharArg = NULL;
883 while (con->os->next == con->os->argc &&
884 con->os > con->optionStack) {
887 if (con->os->next == con->os->argc) {
888 if (!(opt->argInfo & POPT_ARGFLAG_OPTIONAL))
889 /*@-compdef@*/ /* FIX: con->os->argv not defined */
890 return POPT_ERROR_NOARG;
892 con->os->nextArg = NULL;
896 * Make sure this isn't part of a short arg or the
897 * result of an alias expansion.
899 if (con->os == con->optionStack &&
900 (opt->argInfo & POPT_ARGFLAG_STRIP) &&
902 poptStripArg(con, con->os->next);
905 if (con->os->argv != NULL) { /* XXX can't happen */
906 /* XXX watchout: subtle side-effects live here. */
907 longArg = con->os->argv[con->os->next++];
908 longArg = expandNextArg(con, longArg);
909 con->os->nextArg = longArg;
916 switch (opt->argInfo & POPT_ARG_MASK) {
917 case POPT_ARG_STRING:
918 /* XXX memory leak, hard to plug */
919 *((const char **) opt->arg) = (con->os->nextArg)
920 ? xstrdup(con->os->nextArg) : NULL;
921 /*@switchbreak@*/ break;
928 if (con->os->nextArg) {
929 aLong = strtol(con->os->nextArg, &end, 0);
930 if (!(end && *end == '\0'))
931 return POPT_ERROR_BADNUMBER;
934 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) {
935 if (aLong == LONG_MIN || aLong == LONG_MAX)
936 return POPT_ERROR_OVERFLOW;
937 if (poptSaveLong((long *)opt->arg, opt->argInfo, aLong))
938 return POPT_ERROR_BADOPERATION;
940 if (aLong > INT_MAX || aLong < INT_MIN)
941 return POPT_ERROR_OVERFLOW;
942 if (poptSaveInt((int *)opt->arg, opt->argInfo, aLong))
943 return POPT_ERROR_BADOPERATION;
945 } /*@switchbreak@*/ break;
948 case POPT_ARG_DOUBLE:
949 { double aDouble = 0.0;
952 if (con->os->nextArg) {
954 int saveerrno = errno;
956 aDouble = strtod(con->os->nextArg, &end);
958 return POPT_ERROR_OVERFLOW;
962 return POPT_ERROR_BADNUMBER;
965 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_DOUBLE) {
966 *((double *) opt->arg) = aDouble;
968 #define MY_ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
969 if ((MY_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
970 return POPT_ERROR_OVERFLOW;
971 if ((FLT_MIN - MY_ABS(aDouble)) > DBL_EPSILON)
972 return POPT_ERROR_OVERFLOW;
973 *((float *) opt->arg) = aDouble;
975 } /*@switchbreak@*/ break;
978 POPT_("option type (%d) not implemented in popt\n"),
979 (opt->argInfo & POPT_ARG_MASK));
981 /*@notreached@*/ /*@switchbreak@*/ break;
988 invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
990 } else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
993 if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
994 con->finalArgvAlloced += 10;
995 con->finalArgv = realloc(con->finalArgv,
996 sizeof(*con->finalArgv) * con->finalArgvAlloced);
999 if (con->finalArgv != NULL)
1000 { ssize_t bufsize = (opt->longName ? strlen(opt->longName) : 0) + 3;
1001 char *s = malloc(bufsize);
1002 if (s != NULL) { /* XXX can't happen */
1004 snprintf(s, bufsize, "%s%s",
1005 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
1008 snprintf(s, bufsize, "-%c", opt->shortName);
1009 con->finalArgv[con->finalArgvCount++] = s;
1011 con->finalArgv[con->finalArgvCount++] = NULL;
1014 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE)
1015 /*@-ifempty@*/ ; /*@=ifempty@*/
1016 else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL)
1017 /*@-ifempty@*/ ; /*@=ifempty@*/
1018 else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
1019 if (con->finalArgv != NULL && con->os->nextArg)
1020 con->finalArgv[con->finalArgvCount++] =
1021 /*@-nullpass@*/ /* LCL: con->os->nextArg != NULL */
1022 xstrdup(con->os->nextArg);
1027 return (opt ? opt->val : -1); /* XXX can't happen */
1031 const char * poptGetOptArg(poptContext con)
1033 const char * ret = NULL;
1036 ret = con->os->nextArg;
1037 con->os->nextArg = NULL;
1043 const char * poptGetArg(poptContext con)
1045 const char * ret = NULL;
1046 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1047 ret = con->leftovers[con->nextLeftover++];
1051 const char * poptPeekArg(poptContext con)
1053 const char * ret = NULL;
1054 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1055 ret = con->leftovers[con->nextLeftover];
1060 const char ** poptGetArgs(poptContext con)
1063 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1066 /* some apps like [like RPM ;-) ] need this NULL terminated */
1067 con->leftovers[con->numLeftovers] = NULL;
1069 /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */
1070 return (con->leftovers + con->nextLeftover);
1071 /*@=nullret =nullstate @*/
1075 poptContext poptFreeContext(poptContext con)
1080 if (con == NULL) return con;
1081 poptResetContext(con);
1082 con->os->argb = _free(con->os->argb);
1084 if (con->aliases != NULL)
1085 for (i = 0; i < con->numAliases; i++) {
1086 item = con->aliases + i;
1087 /*@-modobserver -observertrans -dependenttrans@*/
1088 item->option.longName = _free(item->option.longName);
1089 item->option.descrip = _free(item->option.descrip);
1090 item->option.argDescrip = _free(item->option.argDescrip);
1091 /*@=modobserver =observertrans =dependenttrans@*/
1092 item->argv = _free(item->argv);
1094 con->aliases = _free(con->aliases);
1096 if (con->execs != NULL)
1097 for (i = 0; i < con->numExecs; i++) {
1098 item = con->execs + i;
1099 /*@-modobserver -observertrans -dependenttrans@*/
1100 item->option.longName = _free(item->option.longName);
1101 item->option.descrip = _free(item->option.descrip);
1102 item->option.argDescrip = _free(item->option.argDescrip);
1103 /*@=modobserver =observertrans =dependenttrans@*/
1104 item->argv = _free(item->argv);
1106 con->execs = _free(con->execs);
1108 con->leftovers = _free(con->leftovers);
1109 con->finalArgv = _free(con->finalArgv);
1110 con->appName = _free(con->appName);
1111 con->otherHelp = _free(con->otherHelp);
1112 con->execPath = _free(con->execPath);
1113 con->arg_strip = PBM_FREE(con->arg_strip);
1119 int poptAddAlias(poptContext con, struct poptAlias alias,
1120 /*@unused@*/ UNUSED(int flags))
1122 poptItem item = (poptItem) alloca(sizeof(*item));
1123 memset(item, 0, sizeof(*item));
1124 item->option.longName = alias.longName;
1125 item->option.shortName = alias.shortName;
1126 item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
1127 item->option.arg = 0;
1128 item->option.val = 0;
1129 item->option.descrip = NULL;
1130 item->option.argDescrip = NULL;
1131 item->argc = alias.argc;
1132 item->argv = alias.argv;
1133 return poptAddItem(con, item, 0);
1137 /*@-mustmod@*/ /* LCL: con not modified? */
1138 int poptAddItem(poptContext con, poptItem newItem, int flags)
1140 poptItem * items, item;
1145 items = &con->execs;
1146 nitems = &con->numExecs;
1149 items = &con->aliases;
1150 nitems = &con->numAliases;
1154 /*@notreached@*/ break;
1157 *items = realloc((*items), ((*nitems) + 1) * sizeof(**items));
1158 if ((*items) == NULL)
1161 item = (*items) + (*nitems);
1163 item->option.longName =
1164 (newItem->option.longName ? xstrdup(newItem->option.longName) : NULL);
1165 item->option.shortName = newItem->option.shortName;
1166 item->option.argInfo = newItem->option.argInfo;
1167 item->option.arg = newItem->option.arg;
1168 item->option.val = newItem->option.val;
1169 item->option.descrip =
1170 (newItem->option.descrip ? xstrdup(newItem->option.descrip) : NULL);
1171 item->option.argDescrip =
1172 (newItem->option.argDescrip ? xstrdup(newItem->option.argDescrip) : NULL);
1173 item->argc = newItem->argc;
1174 item->argv = newItem->argv;
1183 const char * poptBadOption(poptContext con, int flags)
1185 struct optionStackEntry * os = NULL;
1188 os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
1190 /*@-nullderef@*/ /* LCL: os->argv != NULL */
1191 return (os && os->argv ? os->argv[os->next - 1] : NULL);
1195 const char * poptStrerror(const int error)
1198 case POPT_ERROR_NOARG:
1199 return POPT_("missing argument");
1200 case POPT_ERROR_BADOPT:
1201 return POPT_("unknown option");
1202 case POPT_ERROR_BADOPERATION:
1203 return POPT_("mutually exclusive logical operations requested");
1204 case POPT_ERROR_NULLARG:
1205 return POPT_("opt->arg should not be NULL");
1206 case POPT_ERROR_OPTSTOODEEP:
1207 return POPT_("aliases nested too deeply");
1208 case POPT_ERROR_BADQUOTE:
1209 return POPT_("error in parameter quoting");
1210 case POPT_ERROR_BADNUMBER:
1211 return POPT_("invalid numeric value");
1212 case POPT_ERROR_OVERFLOW:
1213 return POPT_("number too large or too small");
1214 case POPT_ERROR_MALLOC:
1215 return POPT_("memory allocation failed");
1216 case POPT_ERROR_ERRNO:
1217 return strerror(errno);
1219 return POPT_("unknown error");
1223 int poptStuffArgs(poptContext con, const char ** argv)
1228 if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1229 return POPT_ERROR_OPTSTOODEEP;
1231 for (argc = 0; argv[argc]; argc++)
1236 con->os->nextArg = NULL;
1237 con->os->nextCharArg = NULL;
1238 con->os->currAlias = NULL;
1239 rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
1240 con->os->argb = NULL;
1241 con->os->stuffed = 1;
1246 const char * poptGetInvocationName(poptContext con)
1248 return (con->os->argv ? con->os->argv[0] : "");
1252 int poptStrippedArgv(poptContext con, int argc, char ** argv)
1260 for (i = 1; i < argc; i++) {
1261 if (PBM_ISSET(i, con->arg_strip))
1265 for (i = 1; i < argc; i++) {
1266 if (con->arg_strip && PBM_ISSET(i, con->arg_strip))
1268 argv[j] = (j < numargs) ? argv[i] : NULL;