pytest:samba-tool: add a flag to print more in runcmd
[samba.git] / third_party / popt / popt.c
blobc6bae95aecab540bdef74869906c6523c8faf7b8
1 /** \ingroup popt
2 * \file popt/popt.c
3 */
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 */
9 #undef MYDEBUG
11 #include "system.h"
13 #if defined(__LCLINT__)
14 /*@-declundef -exportheader @*/
15 extern long long int strtoll(const char *nptr, /*@null@*/ char **endptr,
16 int base)
17 /*@modifies *endptr@*/;
18 /*@=declundef =exportheader @*/
19 #endif
21 #ifdef HAVE_FLOAT_H
22 #include <float.h>
23 #endif
24 #include <math.h>
26 #include "poptint.h"
28 #ifdef MYDEBUG
29 /*@unchecked@*/
30 int _popt_debug = 0;
31 #endif
33 /*@unchecked@*/
34 unsigned int _poptArgMask = POPT_ARG_MASK;
35 /*@unchecked@*/
36 unsigned int _poptGroupMask = POPT_GROUP_MASK;
38 #if !defined(HAVE_STRERROR) && !defined(__LCLINT__)
39 static char * strerror(int errno)
41 extern int sys_nerr;
42 extern char * sys_errlist[];
44 if ((0 <= errno) && (errno < sys_nerr))
45 return sys_errlist[errno];
46 else
47 return POPT_("unknown errno");
49 #endif
51 #ifdef MYDEBUG
52 /*@unused@*/
53 static void prtcon(const char *msg, poptContext con)
55 if (msg) fprintf(stderr, "%s", msg);
56 fprintf(stderr, "\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
57 con, con->os,
58 (con->os->nextCharArg ? con->os->nextCharArg : ""),
59 (con->os->nextArg ? con->os->nextArg : ""),
60 con->os->next,
61 (con->os->argv && con->os->argv[con->os->next]
62 ? con->os->argv[con->os->next] : ""));
64 #endif
66 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
68 con->execPath = _free(con->execPath);
69 con->execPath = xstrdup(path);
70 con->execAbsolute = allowAbsolute;
71 return;
74 static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
75 /*@globals internalState@*/
76 /*@modifies internalState@*/
78 if (opt != NULL)
79 for (; opt->longName || opt->shortName || opt->arg; opt++) {
80 poptArg arg = { .ptr = opt->arg };
81 if (arg.ptr)
82 switch (poptArgType(opt)) {
83 case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
84 poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
85 invokeCallbacksPRE(con, arg.opt);
86 /*@switchbreak@*/ break;
87 case POPT_ARG_CALLBACK: /* Perform callback. */
88 if (!CBF_ISSET(opt, PRE))
89 /*@switchbreak@*/ break;
90 /*@-noeffectuncon @*/ /* XXX no known way to annotate (*vector) calls. */
91 arg.cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
92 /*@=noeffectuncon @*/
93 /*@switchbreak@*/ break;
98 static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
99 /*@globals internalState@*/
100 /*@modifies internalState@*/
102 if (opt != NULL)
103 for (; opt->longName || opt->shortName || opt->arg; opt++) {
104 poptArg arg = { .ptr = opt->arg };
105 if (arg.ptr)
106 switch (poptArgType(opt)) {
107 case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
108 poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
109 invokeCallbacksPOST(con, arg.opt);
110 /*@switchbreak@*/ break;
111 case POPT_ARG_CALLBACK: /* Perform callback. */
112 if (!CBF_ISSET(opt, POST))
113 /*@switchbreak@*/ break;
114 /*@-noeffectuncon @*/ /* XXX no known way to annotate (*vector) calls. */
115 arg.cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
116 /*@=noeffectuncon @*/
117 /*@switchbreak@*/ break;
122 static void invokeCallbacksOPTION(poptContext con,
123 const struct poptOption * opt,
124 const struct poptOption * myOpt,
125 /*@null@*/ const void * myData, int shorty)
126 /*@globals internalState@*/
127 /*@modifies internalState@*/
129 const struct poptOption * cbopt = NULL;
130 poptArg cbarg = { .ptr = NULL };
132 if (opt != NULL)
133 for (; opt->longName || opt->shortName || opt->arg; opt++) {
134 poptArg arg = { .ptr = opt->arg };
135 switch (poptArgType(opt)) {
136 case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
137 poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
138 if (opt->arg != NULL)
139 invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
140 /*@switchbreak@*/ break;
141 case POPT_ARG_CALLBACK: /* Save callback info. */
142 if (CBF_ISSET(opt, SKIPOPTION))
143 /*@switchbreak@*/ break;
144 cbopt = opt;
145 cbarg.ptr = opt->arg;
146 /*@switchbreak@*/ break;
147 default: /* Perform callback on matching option. */
148 if (cbopt == NULL || cbarg.cb == NULL)
149 /*@switchbreak@*/ break;
150 if ((myOpt->shortName && opt->shortName && shorty &&
151 myOpt->shortName == opt->shortName)
152 || (myOpt->longName != NULL && opt->longName != NULL &&
153 !strcmp(myOpt->longName, opt->longName)))
154 { const void *cbData = (cbopt->descrip ? cbopt->descrip : myData);
155 /*@-noeffectuncon @*/ /* XXX no known way to annotate (*vector) calls. */
156 cbarg.cb(con, POPT_CALLBACK_REASON_OPTION,
157 myOpt, con->os->nextArg, cbData);
158 /*@=noeffectuncon @*/
159 /* Terminate (unless explcitly continuing). */
160 if (!CBF_ISSET(cbopt, CONTINUE))
161 return;
163 /*@switchbreak@*/ break;
168 poptContext poptGetContext(const char * name, int argc, const char ** argv,
169 const struct poptOption * options, unsigned int flags)
171 poptContext con = malloc(sizeof(*con));
173 if (con == NULL) return NULL; /* XXX can't happen */
174 memset(con, 0, sizeof(*con));
176 con->os = con->optionStack;
177 con->os->argc = argc;
178 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
179 con->os->argv = argv;
180 /*@=dependenttrans =assignexpose@*/
181 con->os->argb = NULL;
183 if (!(flags & POPT_CONTEXT_KEEP_FIRST))
184 con->os->next = 1; /* skip argv[0] */
186 con->leftovers = calloc( (size_t)(argc + 1), sizeof(*con->leftovers) );
187 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
188 con->options = options;
189 /*@=dependenttrans =assignexpose@*/
190 con->aliases = NULL;
191 con->numAliases = 0;
192 con->flags = flags;
193 con->execs = NULL;
194 con->numExecs = 0;
195 con->finalArgvAlloced = argc * 2;
196 con->finalArgv = calloc( (size_t)con->finalArgvAlloced, sizeof(*con->finalArgv) );
197 con->execAbsolute = 1;
198 con->arg_strip = NULL;
200 if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
201 con->flags |= POPT_CONTEXT_POSIXMEHARDER;
203 if (name)
204 con->appName = xstrdup(name);
206 invokeCallbacksPRE(con, con->options);
208 return con;
211 static void cleanOSE(/*@special@*/ struct optionStackEntry *os)
212 /*@uses os @*/
213 /*@releases os->nextArg, os->argv, os->argb @*/
214 /*@modifies os @*/
216 os->nextArg = _free(os->nextArg);
217 os->argv = _free(os->argv);
218 os->argb = PBM_FREE(os->argb);
221 void poptResetContext(poptContext con)
223 int i;
225 if (con == NULL) return;
226 while (con->os > con->optionStack) {
227 cleanOSE(con->os--);
229 con->os->argb = PBM_FREE(con->os->argb);
230 con->os->currAlias = NULL;
231 con->os->nextCharArg = NULL;
232 con->os->nextArg = NULL;
233 con->os->next = 1; /* skip argv[0] */
235 con->numLeftovers = 0;
236 con->nextLeftover = 0;
237 con->restLeftover = 0;
238 con->doExec = NULL;
240 if (con->finalArgv != NULL)
241 for (i = 0; i < con->finalArgvCount; i++) {
242 /*@-unqualifiedtrans@*/ /* FIX: typedef double indirection. */
243 con->finalArgv[i] = _free(con->finalArgv[i]);
244 /*@=unqualifiedtrans@*/
247 con->finalArgvCount = 0;
248 con->arg_strip = PBM_FREE(con->arg_strip);
249 /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
250 return;
251 /*@=nullstate@*/
254 /* Only one of longName, shortName should be set, not both. */
255 static int handleExec(/*@special@*/ poptContext con,
256 /*@null@*/ const char * longName, char shortName)
257 /*@uses con->execs, con->numExecs, con->flags, con->doExec,
258 con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
259 /*@modifies con @*/
261 poptItem item;
262 int i;
264 if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
265 return 0;
267 for (i = con->numExecs - 1; i >= 0; i--) {
268 item = con->execs + i;
269 if (longName && !(item->option.longName &&
270 !strcmp(longName, item->option.longName)))
271 continue;
272 else if (shortName != item->option.shortName)
273 continue;
274 break;
276 if (i < 0) return 0;
279 if (con->flags & POPT_CONTEXT_NO_EXEC)
280 return 1;
282 if (con->doExec == NULL) {
283 con->doExec = con->execs + i;
284 return 1;
287 /* We already have an exec to do; remember this option for next
288 time 'round */
289 if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
290 con->finalArgvAlloced += 10;
291 con->finalArgv = realloc(con->finalArgv,
292 sizeof(*con->finalArgv) * con->finalArgvAlloced);
295 i = con->finalArgvCount++;
296 if (con->finalArgv != NULL) /* XXX can't happen */
297 { char *s = malloc((longName ? strlen(longName) : 0) + sizeof("--"));
298 if (s != NULL) { /* XXX can't happen */
299 con->finalArgv[i] = s;
300 *s++ = '-';
301 if (longName)
302 s = stpcpy( stpcpy(s, "-"), longName);
303 else
304 *s++ = shortName;
305 *s = '\0';
306 } else
307 con->finalArgv[i] = NULL;
310 return 1;
314 * Compare long option for equality, adjusting for POPT_ARGFLAG_TOGGLE.
315 * @param opt option
316 * @param longName arg option
317 * @param longNameLen arg option length
318 * @return does long option match?
320 static int
321 longOptionStrcmp(const struct poptOption * opt,
322 /*@null@*/ const char * longName, size_t longNameLen)
323 /*@*/
325 const char * optLongName = opt->longName;
326 int rc;
328 if (optLongName == NULL || longName == NULL) /* XXX can't heppen */
329 return 0;
331 if (F_ISSET(opt, TOGGLE)) {
332 if (optLongName[0] == 'n' && optLongName[1] == 'o') {
333 optLongName += sizeof("no") - 1;
334 if (optLongName[0] == '-')
335 optLongName++;
337 if (longName[0] == 'n' && longName[1] == 'o') {
338 longName += sizeof("no") - 1;
339 longNameLen -= sizeof("no") - 1;
340 if (longName[0] == '-') {
341 longName++;
342 longNameLen--;
346 rc = (int)(strlen(optLongName) == longNameLen);
347 if (rc)
348 rc = (int)(strncmp(optLongName, longName, longNameLen) == 0);
349 return rc;
352 /* Only one of longName, shortName may be set at a time */
353 static int handleAlias(/*@special@*/ poptContext con,
354 /*@null@*/ const char * longName, size_t longNameLen,
355 char shortName,
356 /*@exposed@*/ /*@null@*/ const char * nextArg)
357 /*@uses con->aliases, con->numAliases, con->optionStack, con->os,
358 con->os->currAlias, con->os->currAlias->option.longName @*/
359 /*@modifies con @*/
361 poptItem item = con->os->currAlias;
362 int rc;
363 int i;
365 if (item) {
366 if (longName && item->option.longName != NULL
367 && longOptionStrcmp(&item->option, longName, longNameLen))
368 return 0;
369 else
370 if (shortName && shortName == item->option.shortName)
371 return 0;
374 if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
375 return 0;
377 for (i = con->numAliases - 1; i >= 0; i--) {
378 item = con->aliases + i;
379 if (longName) {
380 if (item->option.longName == NULL)
381 continue;
382 if (!longOptionStrcmp(&item->option, longName, longNameLen))
383 continue;
384 } else if (shortName != item->option.shortName)
385 continue;
386 break;
388 if (i < 0) return 0;
390 if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
391 return POPT_ERROR_OPTSTOODEEP;
393 if (longName == NULL && nextArg != NULL && *nextArg != '\0')
394 con->os->nextCharArg = nextArg;
396 con->os++;
397 con->os->next = 0;
398 con->os->stuffed = 0;
399 con->os->nextArg = NULL;
400 con->os->nextCharArg = NULL;
401 con->os->currAlias = con->aliases + i;
402 { const char ** av;
403 int ac = con->os->currAlias->argc;
404 /* Append --foo=bar arg to alias argv array (if present). */
405 if (longName && nextArg != NULL && *nextArg != '\0') {
406 av = malloc((ac + 1 + 1) * sizeof(*av));
407 if (av != NULL) { /* XXX won't happen. */
408 for (i = 0; i < ac; i++) {
409 av[i] = con->os->currAlias->argv[i];
411 av[ac++] = nextArg;
412 av[ac] = NULL;
413 } else /* XXX revert to old popt behavior if malloc fails. */
414 av = con->os->currAlias->argv;
415 } else
416 av = con->os->currAlias->argv;
417 rc = poptDupArgv(ac, av, &con->os->argc, &con->os->argv);
418 if (av != NULL && av != con->os->currAlias->argv)
419 free(av);
421 con->os->argb = NULL;
423 return (rc ? rc : 1);
427 * Return absolute path to executable by searching PATH.
428 * @param argv0 name of executable
429 * @return (malloc'd) absolute path to executable (or NULL)
431 static /*@null@*/
432 const char * findProgramPath(/*@null@*/ const char * argv0)
433 /*@*/
435 char *path = NULL, *s = NULL, *se;
436 char *t = NULL;
438 if (argv0 == NULL) return NULL; /* XXX can't happen */
440 /* If there is a / in argv[0], it has to be an absolute path. */
441 /* XXX Hmmm, why not if (argv0[0] == '/') ... instead? */
442 if (strchr(argv0, '/'))
443 return xstrdup(argv0);
445 if ((path = getenv("PATH")) == NULL || (path = xstrdup(path)) == NULL)
446 return NULL;
448 /* The return buffer in t is big enough for any path. */
449 if ((t = malloc(strlen(path) + strlen(argv0) + sizeof("/"))) != NULL)
450 for (s = path; s && *s; s = se) {
452 /* Snip PATH element into [s,se). */
453 if ((se = strchr(s, ':')))
454 *se++ = '\0';
456 /* Append argv0 to PATH element. */
457 (void) stpcpy(stpcpy(stpcpy(t, s), "/"), argv0);
459 /* If file is executable, bingo! */
460 if (!access(t, X_OK))
461 break;
464 /* If no executable was found in PATH, return NULL. */
465 /*@-compdef@*/
466 if (!(s && *s) && t != NULL)
467 t = _free(t);
468 /*@=compdef@*/
469 /*@-modobserver -observertrans -usedef @*/
470 path = _free(path);
471 /*@=modobserver =observertrans =usedef @*/
473 return t;
476 static int execCommand(poptContext con)
477 /*@globals internalState @*/
478 /*@modifies internalState @*/
480 poptItem item = con->doExec;
481 poptArgv argv = NULL;
482 int argc = 0;
483 int rc;
484 int ec = POPT_ERROR_ERRNO;
486 if (item == NULL) /*XXX can't happen*/
487 return POPT_ERROR_NOARG;
489 if (item->argv == NULL || item->argc < 1 ||
490 (!con->execAbsolute && strchr(item->argv[0], '/')))
491 return POPT_ERROR_NOARG;
493 argv = malloc(sizeof(*argv) *
494 (6 + item->argc + con->numLeftovers + con->finalArgvCount));
495 if (argv == NULL) return POPT_ERROR_MALLOC;
497 if (!strchr(item->argv[0], '/') && con->execPath != NULL) {
498 char *s = malloc(strlen(con->execPath) + strlen(item->argv[0]) + sizeof("/"));
499 if (s)
500 (void)stpcpy(stpcpy(stpcpy(s, con->execPath), "/"), item->argv[0]);
502 argv[argc] = s;
503 } else
504 argv[argc] = findProgramPath(item->argv[0]);
505 if (argv[argc++] == NULL) {
506 ec = POPT_ERROR_NOARG;
507 goto exit;
510 if (item->argc > 1) {
511 memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
512 argc += (item->argc - 1);
515 if (con->finalArgv != NULL && con->finalArgvCount > 0) {
516 memcpy(argv + argc, con->finalArgv,
517 sizeof(*argv) * con->finalArgvCount);
518 argc += con->finalArgvCount;
521 if (con->leftovers != NULL && con->numLeftovers > 0) {
522 memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
523 argc += con->numLeftovers;
526 argv[argc] = NULL;
528 #if defined(hpux) || defined(__hpux)
529 rc = setresgid(getgid(), getgid(),-1);
530 if (rc) goto exit;
531 rc = setresuid(getuid(), getuid(),-1);
532 if (rc) goto exit;
533 #else
535 * XXX " ... on BSD systems setuid() should be preferred over setreuid()"
536 * XXX sez' Timur Bakeyev <mc@bat.ru>
537 * XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
539 #if defined(HAVE_SETUID)
540 rc = setgid(getgid());
541 if (rc) goto exit;
542 rc = setuid(getuid());
543 if (rc) goto exit;
544 #elif defined (HAVE_SETREUID)
545 rc = setregid(getgid(), getgid());
546 if (rc) goto exit;
547 rc = setreuid(getuid(), getuid());
548 if (rc) goto exit;
549 #else
550 ; /* Can't drop privileges */
551 #endif
552 #endif
554 #ifdef MYDEBUG
555 if (_popt_debug)
556 { poptArgv avp;
557 fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
558 for (avp = argv; *avp; avp++)
559 fprintf(stderr, " '%s'", *avp);
560 fprintf(stderr, "\n");
562 #endif
564 /*@-nullstate@*/
565 rc = execvp(argv[0], (char *const *)argv);
566 /*@=nullstate@*/
568 exit:
569 if (argv) {
570 if (argv[0])
571 free((void *)argv[0]);
572 free(argv);
574 return ec;
577 /*@observer@*/ /*@null@*/
578 static const struct poptOption *
579 findOption(const struct poptOption * opt,
580 /*@null@*/ const char * longName, size_t longNameLen,
581 char shortName,
582 /*@null@*/ /*@out@*/ poptCallbackType * callback,
583 /*@null@*/ /*@out@*/ const void ** callbackData,
584 unsigned int argInfo)
585 /*@modifies *callback, *callbackData */
587 const struct poptOption * cb = NULL;
588 poptArg cbarg = { .ptr = NULL };
590 /* This happens when a single - is given */
591 if (LF_ISSET(ONEDASH) && !shortName && (longName && *longName == '\0'))
592 shortName = '-';
594 for (; opt->longName || opt->shortName || opt->arg; opt++) {
595 poptArg arg = { .ptr = opt->arg };
597 switch (poptArgType(opt)) {
598 case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
599 { const struct poptOption * opt2;
601 poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
602 if (arg.ptr == NULL) continue; /* XXX program error */
603 opt2 = findOption(arg.opt, longName, longNameLen, shortName, callback,
604 callbackData, argInfo);
605 if (opt2 == NULL) continue;
606 /* Sub-table data will be inheirited if no data yet. */
607 /*@-observertrans -dependenttrans @*/
608 if (callback && *callback
609 && callbackData && *callbackData == NULL)
610 *callbackData = opt->descrip;
611 /*@=observertrans =dependenttrans @*/
612 return opt2;
613 } /*@notreached@*/ /*@switchbreak@*/ break;
614 case POPT_ARG_CALLBACK:
615 cb = opt;
616 cbarg.ptr = opt->arg;
617 continue;
618 /*@notreached@*/ /*@switchbreak@*/ break;
619 default:
620 /*@switchbreak@*/ break;
623 if (longName != NULL && opt->longName != NULL &&
624 (!LF_ISSET(ONEDASH) || F_ISSET(opt, ONEDASH)) &&
625 longOptionStrcmp(opt, longName, longNameLen))
627 break;
628 } else if (shortName && shortName == opt->shortName) {
629 break;
633 if (opt->longName == NULL && !opt->shortName)
634 return NULL;
636 /*@-modobserver -mods @*/
637 if (callback)
638 *callback = (cb ? cbarg.cb : NULL);
639 if (callbackData)
640 /*@-observertrans -dependenttrans @*/
641 *callbackData = (cb && !CBF_ISSET(cb, INC_DATA) ? cb->descrip : NULL);
642 /*@=observertrans =dependenttrans @*/
643 /*@=modobserver =mods @*/
645 return opt;
648 static const char * findNextArg(/*@special@*/ poptContext con,
649 unsigned argx, int delete_arg)
650 /*@uses con->optionStack, con->os,
651 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
652 /*@modifies con @*/
654 struct optionStackEntry * os = con->os;
655 const char * arg;
657 do {
658 int i;
659 arg = NULL;
660 while (os->next == os->argc && os > con->optionStack) os--;
661 if (os->next == os->argc && os == con->optionStack) break;
662 if (os->argv != NULL)
663 for (i = os->next; i < os->argc; i++) {
664 /*@-sizeoftype@*/
665 if (os->argb && PBM_ISSET(i, os->argb))
666 /*@innercontinue@*/ continue;
667 if (*os->argv[i] == '-')
668 /*@innercontinue@*/ continue;
669 if (--argx > 0)
670 /*@innercontinue@*/ continue;
671 arg = os->argv[i];
672 if (delete_arg) {
673 if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
674 if (os->argb != NULL) /* XXX can't happen */
675 PBM_SET(i, os->argb);
677 /*@innerbreak@*/ break;
678 /*@=sizeoftype@*/
680 if (os > con->optionStack) os--;
681 } while (arg == NULL);
682 return arg;
685 static /*@only@*/ /*@null@*/ const char *
686 expandNextArg(/*@special@*/ poptContext con, const char * s)
687 /*@uses con->optionStack, con->os,
688 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
689 /*@modifies con @*/
691 const char * a = NULL;
692 char *t, *te;
693 size_t tn = strlen(s) + 1;
694 char c;
696 te = t = malloc(tn);
697 if (t == NULL) return NULL; /* XXX can't happen */
698 *t = '\0';
699 while ((c = *s++) != '\0') {
700 switch (c) {
701 #if 0 /* XXX can't do this */
702 case '\\': /* escape */
703 c = *s++;
704 /*@switchbreak@*/ break;
705 #endif
706 case '!':
707 if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
708 /*@switchbreak@*/ break;
709 /* XXX Make sure that findNextArg deletes only next arg. */
710 if (a == NULL) {
711 if ((a = findNextArg(con, 1U, 1)) == NULL)
712 /*@switchbreak@*/ break;
714 s += sizeof("#:+") - 1;
716 tn += strlen(a);
717 { size_t pos = (size_t) (te - t);
718 if ((t = realloc(t, tn)) == NULL) /* XXX can't happen */
719 return NULL;
720 te = stpcpy(t + pos, a);
722 continue;
723 /*@notreached@*/ /*@switchbreak@*/ break;
724 default:
725 /*@switchbreak@*/ break;
727 *te++ = c;
729 *te++ = '\0';
730 /* If the new string is longer than needed, shorten. */
731 if ((t + tn) > te) {
732 /*@-usereleased@*/ /* XXX splint can't follow the pointers. */
733 if ((te = realloc(t, (size_t)(te - t))) == NULL)
734 free(t);
735 t = te;
736 /*@=usereleased@*/
738 return t;
741 static void poptStripArg(/*@special@*/ poptContext con, int which)
742 /*@uses con->optionStack @*/
743 /*@defines con->arg_strip @*/
744 /*@modifies con @*/
746 /*@-compdef -sizeoftype -usedef @*/
747 if (con->arg_strip == NULL)
748 con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
749 if (con->arg_strip != NULL) /* XXX can't happen */
750 PBM_SET(which, con->arg_strip);
751 return;
752 /*@=compdef =sizeoftype =usedef @*/
755 /*@unchecked@*/
756 unsigned int _poptBitsN = _POPT_BITS_N;
757 /*@unchecked@*/
758 unsigned int _poptBitsM = _POPT_BITS_M;
759 /*@unchecked@*/
760 unsigned int _poptBitsK = _POPT_BITS_K;
762 /*@-sizeoftype@*/
763 static int _poptBitsNew(/*@null@*/ poptBits *bitsp)
764 /*@globals _poptBitsN, _poptBitsM, _poptBitsK @*/
765 /*@modifies *bitsp, _poptBitsN, _poptBitsM, _poptBitsK @*/
767 if (bitsp == NULL)
768 return POPT_ERROR_NULLARG;
770 /* XXX handle negated initialization. */
771 if (*bitsp == NULL) {
772 if (_poptBitsN == 0) {
773 _poptBitsN = _POPT_BITS_N;
774 _poptBitsM = _POPT_BITS_M;
776 if (_poptBitsM == 0U) _poptBitsM = (3 * _poptBitsN) / 2;
777 if (_poptBitsK == 0U || _poptBitsK > 32U) _poptBitsK = _POPT_BITS_K;
778 *bitsp = PBM_ALLOC(_poptBitsM-1);
780 /*@-nullstate@*/
781 return 0;
782 /*@=nullstate@*/
785 int poptBitsAdd(poptBits bits, const char * s)
787 size_t ns = (s ? strlen(s) : 0);
788 uint32_t h0 = 0;
789 uint32_t h1 = 0;
791 if (bits == NULL || ns == 0)
792 return POPT_ERROR_NULLARG;
794 poptJlu32lpair(s, ns, &h0, &h1);
796 for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
797 uint32_t h = h0 + ns * h1;
798 uint32_t ix = (h % _poptBitsM);
799 PBM_SET(ix, bits);
801 return 0;
804 int poptBitsChk(poptBits bits, const char * s)
806 size_t ns = (s ? strlen(s) : 0);
807 uint32_t h0 = 0;
808 uint32_t h1 = 0;
809 int rc = 1;
811 if (bits == NULL || ns == 0)
812 return POPT_ERROR_NULLARG;
814 poptJlu32lpair(s, ns, &h0, &h1);
816 for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
817 uint32_t h = h0 + ns * h1;
818 uint32_t ix = (h % _poptBitsM);
819 if (PBM_ISSET(ix, bits))
820 continue;
821 rc = 0;
822 break;
824 return rc;
827 int poptBitsClr(poptBits bits)
829 static size_t nbw = (__PBM_NBITS/8);
830 size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
832 if (bits == NULL)
833 return POPT_ERROR_NULLARG;
834 memset(bits, 0, nw * nbw);
835 return 0;
838 int poptBitsDel(poptBits bits, const char * s)
840 size_t ns = (s ? strlen(s) : 0);
841 uint32_t h0 = 0;
842 uint32_t h1 = 0;
844 if (bits == NULL || ns == 0)
845 return POPT_ERROR_NULLARG;
847 poptJlu32lpair(s, ns, &h0, &h1);
849 for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
850 uint32_t h = h0 + ns * h1;
851 uint32_t ix = (h % _poptBitsM);
852 PBM_CLR(ix, bits);
854 return 0;
857 int poptBitsIntersect(poptBits *ap, const poptBits b)
859 __pbm_bits *abits;
860 __pbm_bits *bbits;
861 __pbm_bits rc = 0;
862 size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
863 size_t i;
865 if (ap == NULL || b == NULL || _poptBitsNew(ap))
866 return POPT_ERROR_NULLARG;
867 abits = __PBM_BITS(*ap);
868 bbits = __PBM_BITS(b);
870 for (i = 0; i < nw; i++) {
871 abits[i] &= bbits[i];
872 rc |= abits[i];
874 return (rc ? 1 : 0);
877 int poptBitsUnion(poptBits *ap, const poptBits b)
879 __pbm_bits *abits;
880 __pbm_bits *bbits;
881 __pbm_bits rc = 0;
882 size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
883 size_t i;
885 if (ap == NULL || b == NULL || _poptBitsNew(ap))
886 return POPT_ERROR_NULLARG;
887 abits = __PBM_BITS(*ap);
888 bbits = __PBM_BITS(b);
890 for (i = 0; i < nw; i++) {
891 abits[i] |= bbits[i];
892 rc |= abits[i];
894 return (rc ? 1 : 0);
897 int poptBitsArgs(poptContext con, poptBits *ap)
899 const char ** av;
900 int rc = 0;
902 if (con == NULL || ap == NULL || _poptBitsNew(ap) ||
903 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
904 return POPT_ERROR_NULLARG;
906 /* some apps like [like RPM ;-) ] need this NULL terminated */
907 con->leftovers[con->numLeftovers] = NULL;
909 for (av = con->leftovers + con->nextLeftover; *av != NULL; av++) {
910 if ((rc = poptBitsAdd(*ap, *av)) != 0)
911 break;
913 /*@-nullstate@*/
914 return rc;
915 /*@=nullstate@*/
918 int poptSaveBits(poptBits * bitsp,
919 /*@unused@*/ UNUSED(unsigned int argInfo), const char * s)
921 char *tbuf = NULL;
922 char *t, *te;
923 int rc = 0;
925 if (bitsp == NULL || s == NULL || *s == '\0' || _poptBitsNew(bitsp))
926 return POPT_ERROR_NULLARG;
928 /* Parse comma separated attributes. */
929 te = tbuf = xstrdup(s);
930 while ((t = te) != NULL && *t) {
931 while (*te != '\0' && *te != ',')
932 te++;
933 if (*te != '\0')
934 *te++ = '\0';
935 /* XXX Ignore empty strings. */
936 if (*t == '\0')
937 continue;
938 /* XXX Permit negated attributes. caveat emptor: false negatives. */
939 if (*t == '!') {
940 t++;
941 if ((rc = poptBitsChk(*bitsp, t)) > 0)
942 rc = poptBitsDel(*bitsp, t);
943 } else
944 rc = poptBitsAdd(*bitsp, t);
945 if (rc)
946 break;
948 tbuf = _free(tbuf);
949 return rc;
951 /*@=sizeoftype@*/
953 int poptSaveString(const char *** argvp,
954 /*@unused@*/ UNUSED(unsigned int argInfo), const char * val)
956 int argc = 0;
958 if (argvp == NULL || val == NULL)
959 return POPT_ERROR_NULLARG;
961 /* XXX likely needs an upper bound on argc. */
962 if (*argvp != NULL)
963 while ((*argvp)[argc] != NULL)
964 argc++;
966 /*@-unqualifiedtrans -nullstate@*/ /* XXX no annotation for (*argvp) */
967 if ((*argvp = xrealloc(*argvp, (argc + 1 + 1) * sizeof(**argvp))) != NULL) {
968 (*argvp)[argc++] = xstrdup(val);
969 (*argvp)[argc ] = NULL;
971 return 0;
972 /*@=unqualifiedtrans =nullstate@*/
975 /*@unchecked@*/
976 static unsigned int seed = 0;
978 int poptSaveLongLong(long long * arg, unsigned int argInfo, long long aLongLong)
980 if (arg == NULL
981 #ifdef NOTYET
982 /* XXX Check alignment, may fail on funky platforms. */
983 || (((unsigned long long)arg) & (sizeof(*arg)-1))
984 #endif
986 return POPT_ERROR_NULLARG;
988 if (aLongLong != 0 && LF_ISSET(RANDOM)) {
989 #if defined(HAVE_SRANDOM)
990 if (!seed) {
991 srandom((unsigned)getpid());
992 srandom((unsigned)random());
994 aLongLong = (long long)(random() % (aLongLong > 0 ? aLongLong : -aLongLong));
995 aLongLong++;
996 #else
997 /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
998 return POPT_ERROR_BADOPERATION;
999 #endif
1001 if (LF_ISSET(NOT))
1002 aLongLong = ~aLongLong;
1003 switch (LF_ISSET(LOGICALOPS)) {
1004 case 0:
1005 *arg = aLongLong;
1006 break;
1007 case POPT_ARGFLAG_OR:
1008 *(unsigned long long *)arg |= (unsigned long long)aLongLong;
1009 break;
1010 case POPT_ARGFLAG_AND:
1011 *(unsigned long long *)arg &= (unsigned long long)aLongLong;
1012 break;
1013 case POPT_ARGFLAG_XOR:
1014 *(unsigned long long *)arg ^= (unsigned long long)aLongLong;
1015 break;
1016 default:
1017 return POPT_ERROR_BADOPERATION;
1018 /*@notreached@*/ break;
1020 return 0;
1023 int poptSaveLong(long * arg, unsigned int argInfo, long aLong)
1025 /* XXX Check alignment, may fail on funky platforms. */
1026 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
1027 return POPT_ERROR_NULLARG;
1029 if (aLong != 0 && LF_ISSET(RANDOM)) {
1030 #if defined(HAVE_SRANDOM)
1031 if (!seed) {
1032 srandom((unsigned)getpid());
1033 srandom((unsigned)random());
1035 aLong = random() % (aLong > 0 ? aLong : -aLong);
1036 aLong++;
1037 #else
1038 /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
1039 return POPT_ERROR_BADOPERATION;
1040 #endif
1042 if (LF_ISSET(NOT))
1043 aLong = ~aLong;
1044 switch (LF_ISSET(LOGICALOPS)) {
1045 case 0: *arg = aLong; break;
1046 case POPT_ARGFLAG_OR: *(unsigned long *)arg |= (unsigned long)aLong; break;
1047 case POPT_ARGFLAG_AND: *(unsigned long *)arg &= (unsigned long)aLong; break;
1048 case POPT_ARGFLAG_XOR: *(unsigned long *)arg ^= (unsigned long)aLong; break;
1049 default:
1050 return POPT_ERROR_BADOPERATION;
1051 /*@notreached@*/ break;
1053 return 0;
1056 int poptSaveInt(/*@null@*/ int * arg, unsigned int argInfo, long aLong)
1058 /* XXX Check alignment, may fail on funky platforms. */
1059 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
1060 return POPT_ERROR_NULLARG;
1062 if (aLong != 0 && LF_ISSET(RANDOM)) {
1063 #if defined(HAVE_SRANDOM)
1064 if (!seed) {
1065 srandom((unsigned)getpid());
1066 srandom((unsigned)random());
1068 aLong = random() % (aLong > 0 ? aLong : -aLong);
1069 aLong++;
1070 #else
1071 /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
1072 return POPT_ERROR_BADOPERATION;
1073 #endif
1075 if (LF_ISSET(NOT))
1076 aLong = ~aLong;
1077 switch (LF_ISSET(LOGICALOPS)) {
1078 case 0: *arg = (int) aLong; break;
1079 case POPT_ARGFLAG_OR: *(unsigned int *)arg |= (unsigned int) aLong; break;
1080 case POPT_ARGFLAG_AND: *(unsigned int *)arg &= (unsigned int) aLong; break;
1081 case POPT_ARGFLAG_XOR: *(unsigned int *)arg ^= (unsigned int) aLong; break;
1082 default:
1083 return POPT_ERROR_BADOPERATION;
1084 /*@notreached@*/ break;
1086 return 0;
1089 int poptSaveShort(/*@null@*/ short * arg, unsigned int argInfo, long aLong)
1091 /* XXX Check alignment, may fail on funky platforms. */
1092 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
1093 return POPT_ERROR_NULLARG;
1095 if (aLong != 0 && LF_ISSET(RANDOM)) {
1096 #if defined(HAVE_SRANDOM)
1097 if (!seed) {
1098 srandom((unsigned)getpid());
1099 srandom((unsigned)random());
1101 aLong = random() % (aLong > 0 ? aLong : -aLong);
1102 aLong++;
1103 #else
1104 /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
1105 return POPT_ERROR_BADOPERATION;
1106 #endif
1108 if (LF_ISSET(NOT))
1109 aLong = ~aLong;
1110 switch (LF_ISSET(LOGICALOPS)) {
1111 case 0: *arg = (short) aLong;
1112 break;
1113 case POPT_ARGFLAG_OR: *(unsigned short *)arg |= (unsigned short) aLong;
1114 break;
1115 case POPT_ARGFLAG_AND: *(unsigned short *)arg &= (unsigned short) aLong;
1116 break;
1117 case POPT_ARGFLAG_XOR: *(unsigned short *)arg ^= (unsigned short) aLong;
1118 break;
1119 default: return POPT_ERROR_BADOPERATION;
1120 /*@notreached@*/ break;
1122 return 0;
1126 * Return argInfo field, handling POPT_ARGFLAG_TOGGLE overrides.
1127 * @param con context
1128 * @param opt option
1129 * @return argInfo
1131 static unsigned int poptArgInfo(poptContext con, const struct poptOption * opt)
1132 /*@*/
1134 unsigned int argInfo = opt->argInfo;
1136 if (con->os->argv != NULL && con->os->next > 0 && opt->longName != NULL)
1137 if (LF_ISSET(TOGGLE)) {
1138 const char * longName = con->os->argv[con->os->next-1];
1139 while (*longName == '-') longName++;
1140 /* XXX almost good enough but consider --[no]nofoo corner cases. */
1141 if (longName[0] != opt->longName[0] || longName[1] != opt->longName[1])
1143 if (!LF_ISSET(XOR)) { /* XXX dont toggle with XOR */
1144 /* Toggle POPT_BIT_SET <=> POPT_BIT_CLR. */
1145 if (LF_ISSET(LOGICALOPS))
1146 argInfo ^= (POPT_ARGFLAG_OR|POPT_ARGFLAG_AND);
1147 argInfo ^= POPT_ARGFLAG_NOT;
1151 return argInfo;
1155 * Parse an integer expression.
1156 * @retval *llp integer expression value
1157 * @param argInfo integer expression type
1158 * @param val integer expression string
1159 * @return 0 on success, otherwise POPT_* error.
1161 static int poptParseInteger(long long * llp,
1162 /*@unused@*/ UNUSED(unsigned int argInfo),
1163 /*@null@*/ const char * val)
1164 /*@modifies *llp @*/
1166 if (val) {
1167 char *end = NULL;
1168 *llp = strtoll(val, &end, 0);
1170 /* XXX parse scaling suffixes here. */
1172 if (!(end && *end == '\0'))
1173 return POPT_ERROR_BADNUMBER;
1174 } else
1175 *llp = 0;
1176 return 0;
1180 * Save the option argument through the (*opt->arg) pointer.
1181 * @param con context
1182 * @param opt option
1183 * @return 0 on success, otherwise POPT_* error.
1185 static int poptSaveArg(poptContext con, const struct poptOption * opt)
1186 /*@globals fileSystem, internalState @*/
1187 /*@modifies con, fileSystem, internalState @*/
1189 poptArg arg = { .ptr = opt->arg };
1190 int rc = 0; /* assume success */
1192 switch (poptArgType(opt)) {
1193 case POPT_ARG_BITSET:
1194 /* XXX memory leak, application is responsible for free. */
1195 rc = poptSaveBits(arg.ptr, opt->argInfo, con->os->nextArg);
1196 /*@switchbreak@*/ break;
1197 case POPT_ARG_ARGV:
1198 /* XXX memory leak, application is responsible for free. */
1199 rc = poptSaveString(arg.ptr, opt->argInfo, con->os->nextArg);
1200 /*@switchbreak@*/ break;
1201 case POPT_ARG_STRING:
1202 /* XXX memory leak, application is responsible for free. */
1203 arg.argv[0] = (con->os->nextArg) ? xstrdup(con->os->nextArg) : NULL;
1204 /*@switchbreak@*/ break;
1206 case POPT_ARG_INT:
1207 case POPT_ARG_SHORT:
1208 case POPT_ARG_LONG:
1209 case POPT_ARG_LONGLONG:
1210 { unsigned int argInfo = poptArgInfo(con, opt);
1211 long long aNUM = 0;
1213 if ((rc = poptParseInteger(&aNUM, argInfo, con->os->nextArg)) != 0)
1214 break;
1216 switch (poptArgType(opt)) {
1217 case POPT_ARG_LONGLONG:
1218 /* XXX let's not demand C99 compiler flags for <limits.h> quite yet. */
1219 #if !defined(LLONG_MAX)
1220 # define LLONG_MAX 9223372036854775807LL
1221 # define LLONG_MIN (-LLONG_MAX - 1LL)
1222 #endif
1223 rc = !(aNUM == LLONG_MIN || aNUM == LLONG_MAX)
1224 ? poptSaveLongLong(arg.longlongp, argInfo, aNUM)
1225 : POPT_ERROR_OVERFLOW;
1226 /*@innerbreak@*/ break;
1227 case POPT_ARG_LONG:
1228 rc = !(aNUM < (long long)LONG_MIN || aNUM > (long long)LONG_MAX)
1229 ? poptSaveLong(arg.longp, argInfo, (long)aNUM)
1230 : POPT_ERROR_OVERFLOW;
1231 /*@innerbreak@*/ break;
1232 case POPT_ARG_INT:
1233 rc = !(aNUM < (long long)INT_MIN || aNUM > (long long)INT_MAX)
1234 ? poptSaveInt(arg.intp, argInfo, (long)aNUM)
1235 : POPT_ERROR_OVERFLOW;
1236 /*@innerbreak@*/ break;
1237 case POPT_ARG_SHORT:
1238 rc = !(aNUM < (long long)SHRT_MIN || aNUM > (long long)SHRT_MAX)
1239 ? poptSaveShort(arg.shortp, argInfo, (long)aNUM)
1240 : POPT_ERROR_OVERFLOW;
1241 /*@innerbreak@*/ break;
1243 } /*@switchbreak@*/ break;
1245 case POPT_ARG_FLOAT:
1246 case POPT_ARG_DOUBLE:
1247 { char *end = NULL;
1248 double aDouble = 0.0;
1250 if (con->os->nextArg) {
1251 /*@-mods@*/
1252 int saveerrno = errno;
1253 errno = 0;
1254 aDouble = strtod(con->os->nextArg, &end);
1255 if (errno == ERANGE) {
1256 rc = POPT_ERROR_OVERFLOW;
1257 break;
1259 errno = saveerrno;
1260 /*@=mods@*/
1261 if (*end != '\0') {
1262 rc = POPT_ERROR_BADNUMBER;
1263 break;
1267 switch (poptArgType(opt)) {
1268 case POPT_ARG_DOUBLE:
1269 arg.doublep[0] = aDouble;
1270 /*@innerbreak@*/ break;
1271 case POPT_ARG_FLOAT:
1272 #if !defined(DBL_EPSILON) && !defined(__LCLINT__)
1273 #define DBL_EPSILON 2.2204460492503131e-16
1274 #endif
1275 #define POPT_ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
1276 if ((FLT_MIN - POPT_ABS(aDouble)) > DBL_EPSILON
1277 || (POPT_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
1278 rc = POPT_ERROR_OVERFLOW;
1279 else
1280 arg.floatp[0] = (float) aDouble;
1281 /*@innerbreak@*/ break;
1283 } /*@switchbreak@*/ break;
1284 case POPT_ARG_MAINCALL:
1285 /*@-assignexpose -type@*/
1286 con->maincall = opt->arg;
1287 /*@=assignexpose =type@*/
1288 /*@switchbreak@*/ break;
1289 default:
1290 fprintf(stdout, POPT_("option type (%u) not implemented in popt\n"),
1291 poptArgType(opt));
1292 exit(EXIT_FAILURE);
1293 /*@notreached@*/ /*@switchbreak@*/ break;
1295 return rc;
1298 /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
1299 int poptGetNextOpt(poptContext con)
1301 const struct poptOption * opt = NULL;
1302 int done = 0;
1304 if (con == NULL)
1305 return -1;
1306 while (!done) {
1307 const char * origOptString = NULL;
1308 poptCallbackType cb = NULL;
1309 const void * cbData = NULL;
1310 const char * longArg = NULL;
1311 int canstrip = 0;
1312 int shorty = 0;
1314 while (!con->os->nextCharArg && con->os->next == con->os->argc
1315 && con->os > con->optionStack) {
1316 cleanOSE(con->os--);
1318 if (!con->os->nextCharArg && con->os->next == con->os->argc) {
1319 invokeCallbacksPOST(con, con->options);
1321 if (con->maincall) {
1322 /*@-noeffectuncon @*/
1323 (void) (*con->maincall) (con->finalArgvCount, con->finalArgv);
1324 /*@=noeffectuncon @*/
1325 return -1;
1328 if (con->doExec) return execCommand(con);
1329 return -1;
1332 /* Process next long option */
1333 if (!con->os->nextCharArg) {
1334 const char * optString;
1335 size_t optStringLen;
1336 int thisopt;
1338 /*@-sizeoftype@*/
1339 if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
1340 con->os->next++;
1341 continue;
1343 /*@=sizeoftype@*/
1344 thisopt = con->os->next;
1345 if (con->os->argv != NULL) /* XXX can't happen */
1346 origOptString = con->os->argv[con->os->next++];
1348 if (origOptString == NULL) /* XXX can't happen */
1349 return POPT_ERROR_BADOPT;
1351 if (con->restLeftover || *origOptString != '-' ||
1352 (*origOptString == '-' && origOptString[1] == '\0'))
1354 if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
1355 con->restLeftover = 1;
1356 if (con->flags & POPT_CONTEXT_ARG_OPTS) {
1357 con->os->nextArg = xstrdup(origOptString);
1358 return 0;
1360 if (con->leftovers != NULL) /* XXX can't happen */
1361 con->leftovers[con->numLeftovers++] = origOptString;
1362 continue;
1365 /* Make a copy we can hack at */
1366 optString = origOptString;
1368 if (optString[0] == '\0')
1369 return POPT_ERROR_BADOPT;
1371 if (optString[1] == '-' && !optString[2]) {
1372 con->restLeftover = 1;
1373 continue;
1374 } else {
1375 const char *oe;
1376 unsigned int argInfo = 0;
1378 optString++;
1379 if (*optString == '-')
1380 optString++;
1381 else
1382 argInfo |= POPT_ARGFLAG_ONEDASH;
1384 /* Check for "--long=arg" option. */
1385 for (oe = optString; *oe && *oe != '='; oe++)
1387 optStringLen = (size_t)(oe - optString);
1388 if (*oe == '=')
1389 longArg = oe + 1;
1391 /* XXX aliases with arg substitution need "--alias=arg" */
1392 if (handleAlias(con, optString, optStringLen, '\0', longArg)) {
1393 longArg = NULL;
1394 continue;
1397 if (handleExec(con, optString, '\0'))
1398 continue;
1400 opt = findOption(con->options, optString, optStringLen, '\0', &cb, &cbData,
1401 argInfo);
1402 if (!opt && !LF_ISSET(ONEDASH))
1403 return POPT_ERROR_BADOPT;
1406 if (!opt) {
1407 con->os->nextCharArg = origOptString + 1;
1408 longArg = NULL;
1409 } else {
1410 if (con->os == con->optionStack && F_ISSET(opt, STRIP))
1412 canstrip = 1;
1413 poptStripArg(con, thisopt);
1415 shorty = 0;
1419 /* Process next short option */
1420 if (con->os->nextCharArg) {
1421 const char * nextCharArg = con->os->nextCharArg;
1423 con->os->nextCharArg = NULL;
1425 if (handleAlias(con, NULL, 0, *nextCharArg, nextCharArg + 1))
1426 continue;
1428 if (handleExec(con, NULL, *nextCharArg)) {
1429 /* Restore rest of short options for further processing */
1430 nextCharArg++;
1431 if (*nextCharArg != '\0')
1432 con->os->nextCharArg = nextCharArg;
1433 continue;
1436 opt = findOption(con->options, NULL, 0, *nextCharArg, &cb,
1437 &cbData, 0);
1438 if (!opt)
1439 return POPT_ERROR_BADOPT;
1440 shorty = 1;
1442 nextCharArg++;
1443 if (*nextCharArg != '\0')
1444 con->os->nextCharArg = nextCharArg + (int)(*nextCharArg == '=');
1447 if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */
1448 if (opt->arg && poptArgType(opt) == POPT_ARG_NONE) {
1449 unsigned int argInfo = poptArgInfo(con, opt);
1450 if (poptSaveInt((int *)opt->arg, argInfo, 1L))
1451 return POPT_ERROR_BADOPERATION;
1452 } else if (poptArgType(opt) == POPT_ARG_VAL) {
1453 if (opt->arg) {
1454 unsigned int argInfo = poptArgInfo(con, opt);
1455 if (poptSaveInt((int *)opt->arg, argInfo, (long)opt->val))
1456 return POPT_ERROR_BADOPERATION;
1458 } else if (poptArgType(opt) != POPT_ARG_NONE) {
1459 int rc;
1461 con->os->nextArg = _free(con->os->nextArg);
1462 if (longArg) {
1463 longArg = expandNextArg(con, longArg);
1464 con->os->nextArg = (char *) longArg;
1465 } else if (con->os->nextCharArg) {
1466 longArg = expandNextArg(con, con->os->nextCharArg);
1467 con->os->nextArg = (char *) longArg;
1468 con->os->nextCharArg = NULL;
1469 } else {
1470 while (con->os->next == con->os->argc &&
1471 con->os > con->optionStack)
1473 cleanOSE(con->os--);
1475 if (con->os->next == con->os->argc) {
1476 if (!F_ISSET(opt, OPTIONAL))
1477 return POPT_ERROR_NOARG;
1478 con->os->nextArg = NULL;
1479 } else {
1482 * Make sure this isn't part of a short arg or the
1483 * result of an alias expansion.
1485 if (con->os == con->optionStack
1486 && F_ISSET(opt, STRIP) && canstrip)
1488 poptStripArg(con, con->os->next);
1491 if (con->os->argv != NULL) { /* XXX can't happen */
1492 if (F_ISSET(opt, OPTIONAL) &&
1493 con->os->argv[con->os->next][0] == '-') {
1494 con->os->nextArg = NULL;
1495 } else {
1496 /* XXX watchout: subtle side-effects live here. */
1497 longArg = con->os->argv[con->os->next++];
1498 longArg = expandNextArg(con, longArg);
1499 con->os->nextArg = (char *) longArg;
1504 longArg = NULL;
1506 /* Save the option argument through a (*opt->arg) pointer. */
1507 if (opt->arg != NULL && (rc = poptSaveArg(con, opt)) != 0)
1508 return rc;
1511 if (cb)
1512 invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
1513 else if (opt->val && (poptArgType(opt) != POPT_ARG_VAL))
1514 done = 1;
1516 if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
1517 con->finalArgvAlloced += 10;
1518 con->finalArgv = realloc(con->finalArgv,
1519 sizeof(*con->finalArgv) * con->finalArgvAlloced);
1522 if (con->finalArgv != NULL)
1523 { char *s = malloc((opt->longName ? strlen(opt->longName) : 0) + sizeof("--"));
1524 if (s != NULL) { /* XXX can't happen */
1525 con->finalArgv[con->finalArgvCount++] = s;
1526 *s++ = '-';
1527 if (opt->longName) {
1528 if (!F_ISSET(opt, ONEDASH))
1529 *s++ = '-';
1530 s = stpcpy(s, opt->longName);
1531 } else {
1532 *s++ = opt->shortName;
1533 *s = '\0';
1535 } else
1536 con->finalArgv[con->finalArgvCount++] = NULL;
1539 if (opt->arg && poptArgType(opt) == POPT_ARG_NONE)
1540 /*@-ifempty@*/ ; /*@=ifempty@*/
1541 else if (poptArgType(opt) == POPT_ARG_VAL)
1542 /*@-ifempty@*/ ; /*@=ifempty@*/
1543 else if (poptArgType(opt) != POPT_ARG_NONE) {
1544 if (con->finalArgv != NULL && con->os->nextArg != NULL)
1545 con->finalArgv[con->finalArgvCount++] =
1546 xstrdup(con->os->nextArg);
1550 return (opt ? opt->val : -1); /* XXX can't happen */
1553 char * poptGetOptArg(poptContext con)
1555 char * ret = NULL;
1556 if (con) {
1557 ret = con->os->nextArg;
1558 con->os->nextArg = NULL;
1560 return ret;
1563 const char * poptGetArg(poptContext con)
1565 const char * ret = NULL;
1566 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1567 ret = con->leftovers[con->nextLeftover++];
1568 return ret;
1571 const char * poptPeekArg(poptContext con)
1573 const char * ret = NULL;
1574 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1575 ret = con->leftovers[con->nextLeftover];
1576 return ret;
1579 const char ** poptGetArgs(poptContext con)
1581 if (con == NULL ||
1582 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1583 return NULL;
1585 /* some apps like [like RPM ;-) ] need this NULL terminated */
1586 con->leftovers[con->numLeftovers] = NULL;
1588 /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */
1589 return (con->leftovers + con->nextLeftover);
1590 /*@=nullret =nullstate @*/
1593 static /*@null@*/
1594 poptItem poptFreeItems(/*@only@*/ /*@null@*/ poptItem items, int nitems)
1595 /*@modifies items @*/
1597 if (items != NULL) {
1598 poptItem item = items;
1599 while (--nitems >= 0) {
1600 /*@-modobserver -observertrans -dependenttrans@*/
1601 item->option.longName = _free(item->option.longName);
1602 item->option.descrip = _free(item->option.descrip);
1603 item->option.argDescrip = _free(item->option.argDescrip);
1604 /*@=modobserver =observertrans =dependenttrans@*/
1605 item->argv = _free(item->argv);
1606 item++;
1608 items = _free(items);
1610 return NULL;
1613 poptContext poptFreeContext(poptContext con)
1615 if (con == NULL) return con;
1616 poptResetContext(con);
1617 con->os->argb = _free(con->os->argb);
1619 con->aliases = poptFreeItems(con->aliases, con->numAliases);
1620 con->numAliases = 0;
1622 con->execs = poptFreeItems(con->execs, con->numExecs);
1623 con->numExecs = 0;
1625 con->leftovers = _free(con->leftovers);
1626 con->finalArgv = _free(con->finalArgv);
1627 con->appName = _free(con->appName);
1628 con->otherHelp = _free(con->otherHelp);
1629 con->execPath = _free(con->execPath);
1630 con->arg_strip = PBM_FREE(con->arg_strip);
1632 con = _free(con);
1633 return con;
1636 int poptAddAlias(poptContext con, struct poptAlias alias,
1637 /*@unused@*/ UNUSED(int flags))
1639 struct poptItem_s item_buf;
1640 poptItem item = &item_buf;
1641 memset(item, 0, sizeof(*item));
1642 item->option.longName = alias.longName;
1643 item->option.shortName = alias.shortName;
1644 item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
1645 item->option.arg = 0;
1646 item->option.val = 0;
1647 item->option.descrip = NULL;
1648 item->option.argDescrip = NULL;
1649 item->argc = alias.argc;
1650 item->argv = alias.argv;
1651 return poptAddItem(con, item, 0);
1654 int poptAddItem(poptContext con, poptItem newItem, int flags)
1656 poptItem * items, item;
1657 int * nitems;
1659 switch (flags) {
1660 case 1:
1661 items = &con->execs;
1662 nitems = &con->numExecs;
1663 break;
1664 case 0:
1665 items = &con->aliases;
1666 nitems = &con->numAliases;
1667 break;
1668 default:
1669 return 1;
1670 /*@notreached@*/ break;
1673 *items = realloc((*items), ((*nitems) + 1) * sizeof(**items));
1674 if ((*items) == NULL)
1675 return 1;
1677 item = (*items) + (*nitems);
1679 item->option.longName =
1680 (newItem->option.longName ? xstrdup(newItem->option.longName) : NULL);
1681 item->option.shortName = newItem->option.shortName;
1682 item->option.argInfo = newItem->option.argInfo;
1683 item->option.arg = newItem->option.arg;
1684 item->option.val = newItem->option.val;
1685 item->option.descrip =
1686 (newItem->option.descrip ? xstrdup(newItem->option.descrip) : NULL);
1687 item->option.argDescrip =
1688 (newItem->option.argDescrip ? xstrdup(newItem->option.argDescrip) : NULL);
1689 item->argc = newItem->argc;
1690 item->argv = newItem->argv;
1692 (*nitems)++;
1694 return 0;
1697 const char * poptBadOption(poptContext con, unsigned int flags)
1699 struct optionStackEntry * os = NULL;
1701 if (con != NULL)
1702 os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
1704 return (os != NULL && os->argv != NULL ? os->argv[os->next - 1] : NULL);
1707 const char * poptStrerror(const int error)
1709 switch (error) {
1710 case POPT_ERROR_NOARG:
1711 return POPT_("missing argument");
1712 case POPT_ERROR_BADOPT:
1713 return POPT_("unknown option");
1714 case POPT_ERROR_BADOPERATION:
1715 return POPT_("mutually exclusive logical operations requested");
1716 case POPT_ERROR_NULLARG:
1717 return POPT_("opt->arg should not be NULL");
1718 case POPT_ERROR_OPTSTOODEEP:
1719 return POPT_("aliases nested too deeply");
1720 case POPT_ERROR_BADQUOTE:
1721 return POPT_("error in parameter quoting");
1722 case POPT_ERROR_BADNUMBER:
1723 return POPT_("invalid numeric value");
1724 case POPT_ERROR_OVERFLOW:
1725 return POPT_("number too large or too small");
1726 case POPT_ERROR_MALLOC:
1727 return POPT_("memory allocation failed");
1728 case POPT_ERROR_BADCONFIG:
1729 return POPT_("config file failed sanity test");
1730 case POPT_ERROR_ERRNO:
1731 return strerror(errno);
1732 default:
1733 return POPT_("unknown error");
1737 int poptStuffArgs(poptContext con, const char ** argv)
1739 int argc;
1740 int rc;
1742 if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1743 return POPT_ERROR_OPTSTOODEEP;
1745 for (argc = 0; argv[argc]; argc++)
1748 con->os++;
1749 con->os->next = 0;
1750 con->os->nextArg = NULL;
1751 con->os->nextCharArg = NULL;
1752 con->os->currAlias = NULL;
1753 rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
1754 con->os->argb = NULL;
1755 con->os->stuffed = 1;
1757 return rc;
1760 const char * poptGetInvocationName(poptContext con)
1762 return (con->os->argv ? con->os->argv[0] : "");
1765 int poptStrippedArgv(poptContext con, int argc, char ** argv)
1767 int numargs = argc;
1768 int j = 1;
1769 int i;
1771 /*@-sizeoftype@*/
1772 if (con->arg_strip)
1773 for (i = 1; i < argc; i++) {
1774 if (PBM_ISSET(i, con->arg_strip))
1775 numargs--;
1778 for (i = 1; i < argc; i++) {
1779 if (con->arg_strip && PBM_ISSET(i, con->arg_strip))
1780 continue;
1781 argv[j] = (j < numargs) ? argv[i] : NULL;
1782 j++;
1784 /*@=sizeoftype@*/
1786 return numargs;