docs: update winbind refresh tickets example to be consistent with default
[Samba.git] / ctdb / lib / popt / popt.c
blob33a5ec7d4f2f71605ed53399115f740242d99391
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 HAVE_MATH_H
14 #include <math.h>
15 #endif
16 #if HAVE_FLOAT_H
17 #include <float.h>
18 #endif
20 #include "findme.h"
21 #include "poptint.h"
23 #ifdef MYDEBUG
24 /*@unchecked@*/
25 int _popt_debug = 0;
26 #endif
28 #ifndef HAVE_STRERROR
29 static char * strerror(int errno) {
30 extern int sys_nerr;
31 extern char * sys_errlist[];
33 if ((0 <= errno) && (errno < sys_nerr))
34 return sys_errlist[errno];
35 else
36 return POPT_("unknown errno");
38 #endif
40 #ifdef MYDEBUG
41 /*@unused@*/ static void prtcon(const char *msg, poptContext con)
43 if (msg) fprintf(stderr, "%s", msg);
44 fprintf(stderr, "\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
45 con, con->os,
46 (con->os->nextCharArg ? con->os->nextCharArg : ""),
47 (con->os->nextArg ? con->os->nextArg : ""),
48 con->os->next,
49 (con->os->argv && con->os->argv[con->os->next]
50 ? con->os->argv[con->os->next] : ""));
52 #endif
54 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
56 con->execPath = _free(con->execPath);
57 con->execPath = xstrdup(path);
58 con->execAbsolute = allowAbsolute;
59 /*@-nullstate@*/ /* LCL: con->execPath can be NULL? */
60 return;
61 /*@=nullstate@*/
64 static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
65 /*@globals internalState@*/
66 /*@modifies internalState@*/
68 if (opt != NULL)
69 for (; opt->longName || opt->shortName || opt->arg; opt++) {
70 if (opt->arg == NULL) continue; /* XXX program error. */
71 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
72 /* Recurse on included sub-tables. */
73 invokeCallbacksPRE(con, opt->arg);
74 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
75 (opt->argInfo & POPT_CBFLAG_PRE))
76 { /*@-castfcnptr@*/
77 poptCallbackType cb = (poptCallbackType)opt->arg;
78 /*@=castfcnptr@*/
79 /* Perform callback. */
80 /*@-moduncon -noeffectuncon @*/
81 cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
82 /*@=moduncon =noeffectuncon @*/
87 static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
88 /*@globals internalState@*/
89 /*@modifies internalState@*/
91 if (opt != NULL)
92 for (; opt->longName || opt->shortName || opt->arg; opt++) {
93 if (opt->arg == NULL) continue; /* XXX program error. */
94 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
95 /* Recurse on included sub-tables. */
96 invokeCallbacksPOST(con, opt->arg);
97 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
98 (opt->argInfo & POPT_CBFLAG_POST))
99 { /*@-castfcnptr@*/
100 poptCallbackType cb = (poptCallbackType)opt->arg;
101 /*@=castfcnptr@*/
102 /* Perform callback. */
103 /*@-moduncon -noeffectuncon @*/
104 cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
105 /*@=moduncon =noeffectuncon @*/
110 static void invokeCallbacksOPTION(poptContext con,
111 const struct poptOption * opt,
112 const struct poptOption * myOpt,
113 /*@null@*/ const void * myData, int shorty)
114 /*@globals internalState@*/
115 /*@modifies internalState@*/
117 const struct poptOption * cbopt = NULL;
119 if (opt != NULL)
120 for (; opt->longName || opt->shortName || opt->arg; opt++) {
121 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
122 /* Recurse on included sub-tables. */
123 if (opt->arg != NULL) /* XXX program error */
124 invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
125 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
126 !(opt->argInfo & POPT_CBFLAG_SKIPOPTION)) {
127 /* Save callback info. */
128 cbopt = opt;
129 } else if (cbopt != NULL &&
130 ((myOpt->shortName && opt->shortName && shorty &&
131 myOpt->shortName == opt->shortName) ||
132 (myOpt->longName && opt->longName &&
133 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
134 !strcmp(myOpt->longName, opt->longName)))
135 /*@=nullpass@*/
137 { /*@-castfcnptr@*/
138 poptCallbackType cb = (poptCallbackType)cbopt->arg;
139 /*@=castfcnptr@*/
140 const void * cbData = (cbopt->descrip ? cbopt->descrip : myData);
141 /* Perform callback. */
142 if (cb != NULL) { /* XXX program error */
143 /*@-moduncon -noeffectuncon @*/
144 cb(con, POPT_CALLBACK_REASON_OPTION, myOpt,
145 con->os->nextArg, cbData);
146 /*@=moduncon =noeffectuncon @*/
148 /* Terminate (unless explcitly continuing). */
149 if (!(cbopt->argInfo & POPT_CBFLAG_CONTINUE))
150 return;
155 poptContext poptGetContext(const char * name, int argc, const char ** argv,
156 const struct poptOption * options, int flags)
158 poptContext con = malloc(sizeof(*con));
160 if (con == NULL) return NULL; /* XXX can't happen */
161 memset(con, 0, sizeof(*con));
163 con->os = con->optionStack;
164 con->os->argc = argc;
165 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
166 con->os->argv = argv;
167 /*@=dependenttrans =assignexpose@*/
168 con->os->argb = NULL;
170 if (!(flags & POPT_CONTEXT_KEEP_FIRST))
171 con->os->next = 1; /* skip argv[0] */
173 con->leftovers = calloc( (argc + 1), sizeof(*con->leftovers) );
174 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
175 con->options = options;
176 /*@=dependenttrans =assignexpose@*/
177 con->aliases = NULL;
178 con->numAliases = 0;
179 con->flags = flags;
180 con->execs = NULL;
181 con->numExecs = 0;
182 con->finalArgvAlloced = argc * 2;
183 con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );
184 con->execAbsolute = 1;
185 con->arg_strip = NULL;
187 if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
188 con->flags |= POPT_CONTEXT_POSIXMEHARDER;
190 if (name) {
191 char * t = malloc(strlen(name) + 1);
192 if (t) con->appName = strcpy(t, name);
195 /*@-internalglobs@*/
196 invokeCallbacksPRE(con, con->options);
197 /*@=internalglobs@*/
199 return con;
202 static void cleanOSE(/*@special@*/ struct optionStackEntry *os)
203 /*@uses os @*/
204 /*@releases os->nextArg, os->argv, os->argb @*/
205 /*@modifies os @*/
207 os->nextArg = _free(os->nextArg);
208 os->argv = _free(os->argv);
209 os->argb = PBM_FREE(os->argb);
212 /*@-boundswrite@*/
213 void poptResetContext(poptContext con)
215 int i;
217 if (con == NULL) return;
218 while (con->os > con->optionStack) {
219 cleanOSE(con->os--);
221 con->os->argb = PBM_FREE(con->os->argb);
222 con->os->currAlias = NULL;
223 con->os->nextCharArg = NULL;
224 con->os->nextArg = NULL;
225 con->os->next = 1; /* skip argv[0] */
227 con->numLeftovers = 0;
228 con->nextLeftover = 0;
229 con->restLeftover = 0;
230 con->doExec = NULL;
232 if (con->finalArgv != NULL)
233 for (i = 0; i < con->finalArgvCount; i++) {
234 /*@-unqualifiedtrans@*/ /* FIX: typedef double indirection. */
235 con->finalArgv[i] = _free(con->finalArgv[i]);
236 /*@=unqualifiedtrans@*/
239 con->finalArgvCount = 0;
240 con->arg_strip = PBM_FREE(con->arg_strip);
241 /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
242 return;
243 /*@=nullstate@*/
245 /*@=boundswrite@*/
247 /* Only one of longName, shortName should be set, not both. */
248 /*@-boundswrite@*/
249 static int handleExec(/*@special@*/ poptContext con,
250 /*@null@*/ const char * longName, char shortName)
251 /*@uses con->execs, con->numExecs, con->flags, con->doExec,
252 con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
253 /*@modifies con @*/
255 poptItem item;
256 int i;
258 if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
259 return 0;
261 for (i = con->numExecs - 1; i >= 0; i--) {
262 item = con->execs + i;
263 if (longName && !(item->option.longName &&
264 !strcmp(longName, item->option.longName)))
265 continue;
266 else if (shortName != item->option.shortName)
267 continue;
268 break;
270 if (i < 0) return 0;
273 if (con->flags & POPT_CONTEXT_NO_EXEC)
274 return 1;
276 if (con->doExec == NULL) {
277 con->doExec = con->execs + i;
278 return 1;
281 /* We already have an exec to do; remember this option for next
282 time 'round */
283 if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
284 con->finalArgvAlloced += 10;
285 con->finalArgv = realloc(con->finalArgv,
286 sizeof(*con->finalArgv) * con->finalArgvAlloced);
289 i = con->finalArgvCount++;
290 if (con->finalArgv != NULL) /* XXX can't happen */
291 { char *s = malloc((longName ? strlen(longName) : 0) + 3);
292 if (s != NULL) { /* XXX can't happen */
293 if (longName)
294 sprintf(s, "--%s", longName);
295 else
296 sprintf(s, "-%c", shortName);
297 con->finalArgv[i] = s;
298 } else
299 con->finalArgv[i] = NULL;
302 /*@-nullstate@*/ /* FIX: con->finalArgv[] == NULL */
303 return 1;
304 /*@=nullstate@*/
306 /*@=boundswrite@*/
308 /* Only one of longName, shortName may be set at a time */
309 static int handleAlias(/*@special@*/ poptContext con,
310 /*@null@*/ const char * longName, char shortName,
311 /*@exposed@*/ /*@null@*/ const char * nextCharArg)
312 /*@uses con->aliases, con->numAliases, con->optionStack, con->os,
313 con->os->currAlias, con->os->currAlias->option.longName @*/
314 /*@modifies con @*/
316 poptItem item = con->os->currAlias;
317 int rc;
318 int i;
320 if (item) {
321 if (longName && (item->option.longName &&
322 !strcmp(longName, item->option.longName)))
323 return 0;
324 if (shortName && shortName == item->option.shortName)
325 return 0;
328 if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
329 return 0;
331 for (i = con->numAliases - 1; i >= 0; i--) {
332 item = con->aliases + i;
333 if (longName && !(item->option.longName &&
334 !strcmp(longName, item->option.longName)))
335 continue;
336 else if (shortName != item->option.shortName)
337 continue;
338 break;
340 if (i < 0) return 0;
342 if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
343 return POPT_ERROR_OPTSTOODEEP;
345 /*@-boundsread@*/
346 if (nextCharArg && *nextCharArg)
347 con->os->nextCharArg = nextCharArg;
348 /*@=boundsread@*/
350 con->os++;
351 con->os->next = 0;
352 con->os->stuffed = 0;
353 con->os->nextArg = NULL;
354 con->os->nextCharArg = NULL;
355 con->os->currAlias = con->aliases + i;
356 rc = poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv,
357 &con->os->argc, &con->os->argv);
358 con->os->argb = NULL;
360 return (rc ? rc : 1);
363 /*@-bounds -boundswrite @*/
364 static int execCommand(poptContext con)
365 /*@globals internalState @*/
366 /*@modifies internalState @*/
368 poptItem item = con->doExec;
369 const char ** argv;
370 int argc = 0;
371 #if defined(__hpux) || defined(HAVE_SETUID) || defined(HAVE_SETREUID)
372 int rc;
373 #endif
375 if (item == NULL) /*XXX can't happen*/
376 return POPT_ERROR_NOARG;
378 if (item->argv == NULL || item->argc < 1 ||
379 (!con->execAbsolute && strchr(item->argv[0], '/')))
380 return POPT_ERROR_NOARG;
382 argv = malloc(sizeof(*argv) *
383 (6 + item->argc + con->numLeftovers + con->finalArgvCount));
384 if (argv == NULL) return POPT_ERROR_MALLOC; /* XXX can't happen */
386 if (!strchr(item->argv[0], '/') && con->execPath) {
387 char *s = alloca(strlen(con->execPath) + strlen(item->argv[0]) + sizeof("/"));
388 sprintf(s, "%s/%s", con->execPath, item->argv[0]);
389 argv[argc] = s;
390 } else {
391 argv[argc] = findProgramPath(item->argv[0]);
393 if (argv[argc++] == NULL) return POPT_ERROR_NOARG;
395 if (item->argc > 1) {
396 memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
397 argc += (item->argc - 1);
400 if (con->finalArgv != NULL && con->finalArgvCount > 0) {
401 memcpy(argv + argc, con->finalArgv,
402 sizeof(*argv) * con->finalArgvCount);
403 argc += con->finalArgvCount;
406 if (con->leftovers != NULL && con->numLeftovers > 0) {
407 #if 0
408 argv[argc++] = "--";
409 #endif
410 memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
411 argc += con->numLeftovers;
414 argv[argc] = NULL;
416 #ifdef __hpux
417 rc = setresuid(getuid(), getuid(),-1);
418 if (rc) return POPT_ERROR_ERRNO;
419 #else
421 * XXX " ... on BSD systems setuid() should be preferred over setreuid()"
422 * XXX sez' Timur Bakeyev <mc@bat.ru>
423 * XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
425 #if defined(HAVE_SETUID)
426 rc = setuid(getuid());
427 if (rc) return POPT_ERROR_ERRNO;
428 #elif defined (HAVE_SETREUID)
429 rc = setreuid(getuid(), getuid()); /*hlauer: not portable to hpux9.01 */
430 if (rc) return POPT_ERROR_ERRNO;
431 #else
432 ; /* Can't drop privileges */
433 #endif
434 #endif
436 if (argv[0] == NULL)
437 return POPT_ERROR_NOARG;
439 #ifdef MYDEBUG
440 if (_popt_debug)
441 { const char ** avp;
442 fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
443 for (avp = argv; *avp; avp++)
444 fprintf(stderr, " '%s'", *avp);
445 fprintf(stderr, "\n");
447 #endif
449 (void) execvp(argv[0], (char *const *)(intptr_t)argv);
451 return POPT_ERROR_ERRNO;
453 /*@=bounds =boundswrite @*/
455 /*@-boundswrite@*/
456 /*@observer@*/ /*@null@*/ static const struct poptOption *
457 findOption(const struct poptOption * opt, /*@null@*/ const char * longName,
458 char shortName,
459 /*@null@*/ /*@out@*/ poptCallbackType * callback,
460 /*@null@*/ /*@out@*/ const void ** callbackData,
461 int singleDash)
462 /*@modifies *callback, *callbackData */
464 const struct poptOption * cb = NULL;
466 /* This happens when a single - is given */
467 if (singleDash && !shortName && (longName && *longName == '\0'))
468 shortName = '-';
470 for (; opt->longName || opt->shortName || opt->arg; opt++) {
472 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
473 const struct poptOption * opt2;
475 /* Recurse on included sub-tables. */
476 if (opt->arg == NULL) continue; /* XXX program error */
477 opt2 = findOption(opt->arg, longName, shortName, callback,
478 callbackData, singleDash);
479 if (opt2 == NULL) continue;
480 /* Sub-table data will be inheirited if no data yet. */
481 if (!(callback && *callback)) return opt2;
482 if (!(callbackData && *callbackData == NULL)) return opt2;
483 /*@-observertrans -dependenttrans @*/
484 *callbackData = opt->descrip;
485 /*@=observertrans =dependenttrans @*/
486 return opt2;
487 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) {
488 cb = opt;
489 } else if (longName && opt->longName &&
490 (!singleDash || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) &&
491 /*@-nullpass@*/ /* LCL: opt->longName != NULL */
492 !strcmp(longName, opt->longName))
493 /*@=nullpass@*/
495 break;
496 } else if (shortName && shortName == opt->shortName) {
497 break;
501 if (!opt->longName && !opt->shortName)
502 return NULL;
503 /*@-modobserver -mods @*/
504 if (callback) *callback = NULL;
505 if (callbackData) *callbackData = NULL;
506 if (cb) {
507 if (callback)
508 /*@-castfcnptr@*/
509 *callback = (poptCallbackType)cb->arg;
510 /*@=castfcnptr@*/
511 if (!(cb->argInfo & POPT_CBFLAG_INC_DATA)) {
512 if (callbackData)
513 /*@-observertrans@*/ /* FIX: typedef double indirection. */
514 *callbackData = cb->descrip;
515 /*@=observertrans@*/
518 /*@=modobserver =mods @*/
520 return opt;
522 /*@=boundswrite@*/
524 static const char * findNextArg(/*@special@*/ poptContext con,
525 unsigned argx, int delete_arg)
526 /*@uses con->optionStack, con->os,
527 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
528 /*@modifies con @*/
530 struct optionStackEntry * os = con->os;
531 const char * arg;
533 do {
534 int i;
535 arg = NULL;
536 while (os->next == os->argc && os > con->optionStack) os--;
537 if (os->next == os->argc && os == con->optionStack) break;
538 if (os->argv != NULL)
539 for (i = os->next; i < os->argc; i++) {
540 /*@-sizeoftype@*/
541 if (os->argb && PBM_ISSET(i, os->argb))
542 /*@innercontinue@*/ continue;
543 if (*os->argv[i] == '-')
544 /*@innercontinue@*/ continue;
545 if (--argx > 0)
546 /*@innercontinue@*/ continue;
547 arg = os->argv[i];
548 if (delete_arg) {
549 if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
550 if (os->argb != NULL) /* XXX can't happen */
551 PBM_SET(i, os->argb);
553 /*@innerbreak@*/ break;
554 /*@=sizeoftype@*/
556 if (os > con->optionStack) os--;
557 } while (arg == NULL);
558 return arg;
561 /*@-boundswrite@*/
562 static /*@only@*/ /*@null@*/ const char *
563 expandNextArg(/*@special@*/ poptContext con, const char * s)
564 /*@uses con->optionStack, con->os,
565 con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
566 /*@modifies con @*/
568 const char * a = NULL;
569 size_t alen;
570 char *t, *te;
571 size_t tn = strlen(s) + 1;
572 char c;
574 te = t = malloc(tn);;
575 if (t == NULL) return NULL; /* XXX can't happen */
576 while ((c = *s++) != '\0') {
577 switch (c) {
578 #if 0 /* XXX can't do this */
579 case '\\': /* escape */
580 c = *s++;
581 /*@switchbreak@*/ break;
582 #endif
583 case '!':
584 if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
585 /*@switchbreak@*/ break;
586 /* XXX Make sure that findNextArg deletes only next arg. */
587 if (a == NULL) {
588 if ((a = findNextArg(con, 1, 1)) == NULL)
589 /*@switchbreak@*/ break;
591 s += 3;
593 alen = strlen(a);
594 tn += alen;
595 *te = '\0';
596 t = realloc(t, tn);
597 te = t + strlen(t);
598 strncpy(te, a, alen); te += alen;
599 continue;
600 /*@notreached@*/ /*@switchbreak@*/ break;
601 default:
602 /*@switchbreak@*/ break;
604 *te++ = c;
606 *te = '\0';
607 t = realloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */
608 return t;
610 /*@=boundswrite@*/
612 static void poptStripArg(/*@special@*/ poptContext con, int which)
613 /*@uses con->arg_strip, con->optionStack @*/
614 /*@defines con->arg_strip @*/
615 /*@modifies con @*/
617 /*@-sizeoftype@*/
618 if (con->arg_strip == NULL)
619 con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
620 if (con->arg_strip != NULL) /* XXX can't happen */
621 PBM_SET(which, con->arg_strip);
622 /*@=sizeoftype@*/
623 /*@-compdef@*/ /* LCL: con->arg_strip udefined? */
624 return;
625 /*@=compdef@*/
628 int poptSaveLong(long * arg, int argInfo, long aLong)
630 /* XXX Check alignment, may fail on funky platforms. */
631 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
632 return POPT_ERROR_NULLARG;
634 if (argInfo & POPT_ARGFLAG_NOT)
635 aLong = ~aLong;
636 switch (argInfo & POPT_ARGFLAG_LOGICALOPS) {
637 case 0:
638 *arg = aLong;
639 break;
640 case POPT_ARGFLAG_OR:
641 *arg |= aLong;
642 break;
643 case POPT_ARGFLAG_AND:
644 *arg &= aLong;
645 break;
646 case POPT_ARGFLAG_XOR:
647 *arg ^= aLong;
648 break;
649 default:
650 return POPT_ERROR_BADOPERATION;
651 /*@notreached@*/ break;
653 return 0;
656 int poptSaveInt(/*@null@*/ int * arg, int argInfo, long aLong)
658 /* XXX Check alignment, may fail on funky platforms. */
659 if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
660 return POPT_ERROR_NULLARG;
662 if (argInfo & POPT_ARGFLAG_NOT)
663 aLong = ~aLong;
664 switch (argInfo & POPT_ARGFLAG_LOGICALOPS) {
665 case 0:
666 *arg = aLong;
667 break;
668 case POPT_ARGFLAG_OR:
669 *arg |= aLong;
670 break;
671 case POPT_ARGFLAG_AND:
672 *arg &= aLong;
673 break;
674 case POPT_ARGFLAG_XOR:
675 *arg ^= aLong;
676 break;
677 default:
678 return POPT_ERROR_BADOPERATION;
679 /*@notreached@*/ break;
681 return 0;
684 /*@-boundswrite@*/
685 /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
686 int poptGetNextOpt(poptContext con)
688 const struct poptOption * opt = NULL;
689 int done = 0;
691 if (con == NULL)
692 return -1;
693 while (!done) {
694 const char * origOptString = NULL;
695 poptCallbackType cb = NULL;
696 const void * cbData = NULL;
697 const char * longArg = NULL;
698 int canstrip = 0;
699 int shorty = 0;
701 while (!con->os->nextCharArg && con->os->next == con->os->argc
702 && con->os > con->optionStack) {
703 cleanOSE(con->os--);
705 if (!con->os->nextCharArg && con->os->next == con->os->argc) {
706 /*@-internalglobs@*/
707 invokeCallbacksPOST(con, con->options);
708 /*@=internalglobs@*/
709 if (con->doExec) return execCommand(con);
710 return -1;
713 /* Process next long option */
714 if (!con->os->nextCharArg) {
715 char * localOptString, * optString;
716 int thisopt;
718 /*@-sizeoftype@*/
719 if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
720 con->os->next++;
721 continue;
723 /*@=sizeoftype@*/
724 thisopt = con->os->next;
725 if (con->os->argv != NULL) /* XXX can't happen */
726 origOptString = con->os->argv[con->os->next++];
728 if (origOptString == NULL) /* XXX can't happen */
729 return POPT_ERROR_BADOPT;
731 if (con->restLeftover || *origOptString != '-') {
732 if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
733 con->restLeftover = 1;
734 if (con->flags & POPT_CONTEXT_ARG_OPTS) {
735 con->os->nextArg = xstrdup(origOptString);
736 return 0;
738 if (con->leftovers != NULL) /* XXX can't happen */
739 con->leftovers[con->numLeftovers++] = origOptString;
740 continue;
743 /* Make a copy we can hack at */
744 localOptString = optString =
745 strcpy(alloca(strlen(origOptString) + 1), origOptString);
747 if (optString[0] == '\0')
748 return POPT_ERROR_BADOPT;
750 if (optString[1] == '-' && !optString[2]) {
751 con->restLeftover = 1;
752 continue;
753 } else {
754 char *oe;
755 int singleDash;
757 optString++;
758 if (*optString == '-')
759 singleDash = 0, optString++;
760 else
761 singleDash = 1;
763 /* XXX aliases with arg substitution need "--alias=arg" */
764 if (handleAlias(con, optString, '\0', NULL))
765 continue;
767 if (handleExec(con, optString, '\0'))
768 continue;
770 /* Check for "--long=arg" option. */
771 for (oe = optString; *oe && *oe != '='; oe++)
773 if (*oe == '=') {
774 *oe++ = '\0';
775 /* XXX longArg is mapped back to persistent storage. */
776 longArg = origOptString + (oe - localOptString);
779 opt = findOption(con->options, optString, '\0', &cb, &cbData,
780 singleDash);
781 if (!opt && !singleDash)
782 return POPT_ERROR_BADOPT;
785 if (!opt) {
786 con->os->nextCharArg = origOptString + 1;
787 } else {
788 if (con->os == con->optionStack &&
789 opt->argInfo & POPT_ARGFLAG_STRIP)
791 canstrip = 1;
792 poptStripArg(con, thisopt);
794 shorty = 0;
798 /* Process next short option */
799 /*@-branchstate@*/ /* FIX: W2DO? */
800 if (con->os->nextCharArg) {
801 origOptString = con->os->nextCharArg;
803 con->os->nextCharArg = NULL;
805 if (handleAlias(con, NULL, *origOptString, origOptString + 1))
806 continue;
808 if (handleExec(con, NULL, *origOptString)) {
809 /* Restore rest of short options for further processing */
810 origOptString++;
811 if (*origOptString != '\0')
812 con->os->nextCharArg = origOptString;
813 continue;
816 opt = findOption(con->options, NULL, *origOptString, &cb,
817 &cbData, 0);
818 if (!opt)
819 return POPT_ERROR_BADOPT;
820 shorty = 1;
822 origOptString++;
823 if (*origOptString != '\0')
824 con->os->nextCharArg = origOptString;
826 /*@=branchstate@*/
828 if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */
829 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) {
830 if (poptSaveInt((int *)opt->arg, opt->argInfo, 1L))
831 return POPT_ERROR_BADOPERATION;
832 } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) {
833 if (opt->arg) {
834 if (poptSaveInt((int *)opt->arg, opt->argInfo, (long)opt->val))
835 return POPT_ERROR_BADOPERATION;
837 } else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
838 con->os->nextArg = _free(con->os->nextArg);
839 /*@-usedef@*/ /* FIX: W2DO? */
840 if (longArg) {
841 /*@=usedef@*/
842 longArg = expandNextArg(con, longArg);
843 con->os->nextArg = longArg;
844 } else if (con->os->nextCharArg) {
845 longArg = expandNextArg(con, con->os->nextCharArg);
846 con->os->nextArg = longArg;
847 con->os->nextCharArg = NULL;
848 } else {
849 while (con->os->next == con->os->argc &&
850 con->os > con->optionStack) {
851 cleanOSE(con->os--);
853 if (con->os->next == con->os->argc) {
854 if (!(opt->argInfo & POPT_ARGFLAG_OPTIONAL))
855 /*@-compdef@*/ /* FIX: con->os->argv not defined */
856 return POPT_ERROR_NOARG;
857 /*@=compdef@*/
858 con->os->nextArg = NULL;
859 } else {
862 * Make sure this isn't part of a short arg or the
863 * result of an alias expansion.
865 if (con->os == con->optionStack &&
866 (opt->argInfo & POPT_ARGFLAG_STRIP) &&
867 canstrip) {
868 poptStripArg(con, con->os->next);
871 if (con->os->argv != NULL) { /* XXX can't happen */
872 /* XXX watchout: subtle side-effects live here. */
873 longArg = con->os->argv[con->os->next++];
874 longArg = expandNextArg(con, longArg);
875 con->os->nextArg = longArg;
879 longArg = NULL;
881 if (opt->arg) {
882 switch (opt->argInfo & POPT_ARG_MASK) {
883 case POPT_ARG_STRING:
884 /* XXX memory leak, hard to plug */
885 *((const char **) opt->arg) = (con->os->nextArg)
886 ? xstrdup(con->os->nextArg) : NULL;
887 /*@switchbreak@*/ break;
889 case POPT_ARG_INT:
890 case POPT_ARG_LONG:
891 { long aLong = 0;
892 char *end;
894 if (con->os->nextArg) {
895 aLong = strtol(con->os->nextArg, &end, 0);
896 if (!(end && *end == '\0'))
897 return POPT_ERROR_BADNUMBER;
900 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) {
901 if (aLong == LONG_MIN || aLong == LONG_MAX)
902 return POPT_ERROR_OVERFLOW;
903 if (poptSaveLong((long *)opt->arg, opt->argInfo, aLong))
904 return POPT_ERROR_BADOPERATION;
905 } else {
906 if (aLong > INT_MAX || aLong < INT_MIN)
907 return POPT_ERROR_OVERFLOW;
908 if (poptSaveInt((int *)opt->arg, opt->argInfo, aLong))
909 return POPT_ERROR_BADOPERATION;
911 } /*@switchbreak@*/ break;
913 case POPT_ARG_FLOAT:
914 case POPT_ARG_DOUBLE:
915 { double aDouble = 0.0;
916 char *end;
918 if (con->os->nextArg) {
919 /*@-mods@*/
920 int saveerrno = errno;
921 errno = 0;
922 aDouble = strtod(con->os->nextArg, &end);
923 if (errno == ERANGE)
924 return POPT_ERROR_OVERFLOW;
925 errno = saveerrno;
926 /*@=mods@*/
927 if (*end != '\0')
928 return POPT_ERROR_BADNUMBER;
931 if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_DOUBLE) {
932 *((double *) opt->arg) = aDouble;
933 } else {
934 #define _ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
935 if ((_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
936 return POPT_ERROR_OVERFLOW;
937 if ((FLT_MIN - _ABS(aDouble)) > DBL_EPSILON)
938 return POPT_ERROR_OVERFLOW;
939 *((float *) opt->arg) = aDouble;
941 } /*@switchbreak@*/ break;
942 default:
943 fprintf(stdout,
944 POPT_("option type (%d) not implemented in popt\n"),
945 (opt->argInfo & POPT_ARG_MASK));
946 exit(EXIT_FAILURE);
947 /*@notreached@*/ /*@switchbreak@*/ break;
952 if (cb) {
953 /*@-internalglobs@*/
954 invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
955 /*@=internalglobs@*/
956 } else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
957 done = 1;
959 if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
960 con->finalArgvAlloced += 10;
961 con->finalArgv = realloc(con->finalArgv,
962 sizeof(*con->finalArgv) * con->finalArgvAlloced);
965 if (con->finalArgv != NULL)
966 { char *s = malloc((opt->longName ? strlen(opt->longName) : 0) + 3);
967 if (s != NULL) { /* XXX can't happen */
968 if (opt->longName)
969 sprintf(s, "%s%s",
970 ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
971 opt->longName);
972 else
973 sprintf(s, "-%c", opt->shortName);
974 con->finalArgv[con->finalArgvCount++] = s;
975 } else
976 con->finalArgv[con->finalArgvCount++] = NULL;
979 if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE)
980 /*@-ifempty@*/ ; /*@=ifempty@*/
981 else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL)
982 /*@-ifempty@*/ ; /*@=ifempty@*/
983 else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
984 if (con->finalArgv != NULL && con->os->nextArg)
985 con->finalArgv[con->finalArgvCount++] =
986 /*@-nullpass@*/ /* LCL: con->os->nextArg != NULL */
987 xstrdup(con->os->nextArg);
988 /*@=nullpass@*/
992 return (opt ? opt->val : -1); /* XXX can't happen */
994 /*@=boundswrite@*/
996 const char * poptGetOptArg(poptContext con)
998 const char * ret = NULL;
999 /*@-branchstate@*/
1000 if (con) {
1001 ret = con->os->nextArg;
1002 con->os->nextArg = NULL;
1004 /*@=branchstate@*/
1005 return ret;
1008 const char * poptGetArg(poptContext con)
1010 const char * ret = NULL;
1011 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1012 ret = con->leftovers[con->nextLeftover++];
1013 return ret;
1016 const char * poptPeekArg(poptContext con)
1018 const char * ret = NULL;
1019 if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1020 ret = con->leftovers[con->nextLeftover];
1021 return ret;
1024 /*@-boundswrite@*/
1025 const char ** poptGetArgs(poptContext con)
1027 if (con == NULL ||
1028 con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1029 return NULL;
1031 /* some apps like [like RPM ;-) ] need this NULL terminated */
1032 con->leftovers[con->numLeftovers] = NULL;
1034 /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */
1035 return (con->leftovers + con->nextLeftover);
1036 /*@=nullret =nullstate @*/
1038 /*@=boundswrite@*/
1040 poptContext poptFreeContext(poptContext con)
1042 poptItem item;
1043 int i;
1045 if (con == NULL) return con;
1046 poptResetContext(con);
1047 con->os->argb = _free(con->os->argb);
1049 if (con->aliases != NULL)
1050 for (i = 0; i < con->numAliases; i++) {
1051 item = con->aliases + i;
1052 /*@-modobserver -observertrans -dependenttrans@*/
1053 item->option.longName = _free(item->option.longName);
1054 item->option.descrip = _free(item->option.descrip);
1055 item->option.argDescrip = _free(item->option.argDescrip);
1056 /*@=modobserver =observertrans =dependenttrans@*/
1057 item->argv = _free(item->argv);
1059 con->aliases = _free(con->aliases);
1061 if (con->execs != NULL)
1062 for (i = 0; i < con->numExecs; i++) {
1063 item = con->execs + i;
1064 /*@-modobserver -observertrans -dependenttrans@*/
1065 item->option.longName = _free(item->option.longName);
1066 item->option.descrip = _free(item->option.descrip);
1067 item->option.argDescrip = _free(item->option.argDescrip);
1068 /*@=modobserver =observertrans =dependenttrans@*/
1069 item->argv = _free(item->argv);
1071 con->execs = _free(con->execs);
1073 con->leftovers = _free(con->leftovers);
1074 con->finalArgv = _free(con->finalArgv);
1075 con->appName = _free(con->appName);
1076 con->otherHelp = _free(con->otherHelp);
1077 con->execPath = _free(con->execPath);
1078 con->arg_strip = PBM_FREE(con->arg_strip);
1080 con = _free(con);
1081 return con;
1084 int poptAddAlias(poptContext con, struct poptAlias alias,
1085 /*@unused@*/ int flags)
1087 poptItem item = alloca(sizeof(*item));
1088 memset(item, 0, sizeof(*item));
1089 item->option.longName = alias.longName;
1090 item->option.shortName = alias.shortName;
1091 item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
1092 item->option.arg = 0;
1093 item->option.val = 0;
1094 item->option.descrip = NULL;
1095 item->option.argDescrip = NULL;
1096 item->argc = alias.argc;
1097 item->argv = alias.argv;
1098 return poptAddItem(con, item, 0);
1101 /*@-boundswrite@*/
1102 /*@-mustmod@*/ /* LCL: con not modified? */
1103 int poptAddItem(poptContext con, poptItem newItem, int flags)
1105 poptItem * items, item;
1106 int * nitems;
1108 switch (flags) {
1109 case 1:
1110 items = &con->execs;
1111 nitems = &con->numExecs;
1112 break;
1113 case 0:
1114 items = &con->aliases;
1115 nitems = &con->numAliases;
1116 break;
1117 default:
1118 return 1;
1119 /*@notreached@*/ break;
1122 *items = realloc((*items), ((*nitems) + 1) * sizeof(**items));
1123 if ((*items) == NULL)
1124 return 1;
1126 item = (*items) + (*nitems);
1128 item->option.longName =
1129 (newItem->option.longName ? xstrdup(newItem->option.longName) : NULL);
1130 item->option.shortName = newItem->option.shortName;
1131 item->option.argInfo = newItem->option.argInfo;
1132 item->option.arg = newItem->option.arg;
1133 item->option.val = newItem->option.val;
1134 item->option.descrip =
1135 (newItem->option.descrip ? xstrdup(newItem->option.descrip) : NULL);
1136 item->option.argDescrip =
1137 (newItem->option.argDescrip ? xstrdup(newItem->option.argDescrip) : NULL);
1138 item->argc = newItem->argc;
1139 item->argv = newItem->argv;
1141 (*nitems)++;
1143 return 0;
1145 /*@=mustmod@*/
1146 /*@=boundswrite@*/
1148 const char * poptBadOption(poptContext con, int flags)
1150 struct optionStackEntry * os = NULL;
1152 if (con != NULL)
1153 os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
1155 /*@-nullderef@*/ /* LCL: os->argv != NULL */
1156 return (os && os->argv ? os->argv[os->next - 1] : NULL);
1157 /*@=nullderef@*/
1160 const char *poptStrerror(const int error)
1162 switch (error) {
1163 case POPT_ERROR_NOARG:
1164 return POPT_("missing argument");
1165 case POPT_ERROR_BADOPT:
1166 return POPT_("unknown option");
1167 case POPT_ERROR_BADOPERATION:
1168 return POPT_("mutually exclusive logical operations requested");
1169 case POPT_ERROR_NULLARG:
1170 return POPT_("opt->arg should not be NULL");
1171 case POPT_ERROR_OPTSTOODEEP:
1172 return POPT_("aliases nested too deeply");
1173 case POPT_ERROR_BADQUOTE:
1174 return POPT_("error in parameter quoting");
1175 case POPT_ERROR_BADNUMBER:
1176 return POPT_("invalid numeric value");
1177 case POPT_ERROR_OVERFLOW:
1178 return POPT_("number too large or too small");
1179 case POPT_ERROR_MALLOC:
1180 return POPT_("memory allocation failed");
1181 case POPT_ERROR_ERRNO:
1182 return strerror(errno);
1183 default:
1184 return POPT_("unknown error");
1188 int poptStuffArgs(poptContext con, const char ** argv)
1190 int argc;
1191 int rc;
1193 if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1194 return POPT_ERROR_OPTSTOODEEP;
1196 for (argc = 0; argv[argc]; argc++)
1199 con->os++;
1200 con->os->next = 0;
1201 con->os->nextArg = NULL;
1202 con->os->nextCharArg = NULL;
1203 con->os->currAlias = NULL;
1204 rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
1205 con->os->argb = NULL;
1206 con->os->stuffed = 1;
1208 return rc;
1211 const char * poptGetInvocationName(poptContext con)
1213 return (con->os->argv ? con->os->argv[0] : "");
1216 /*@-boundswrite@*/
1217 int poptStrippedArgv(poptContext con, int argc, char ** argv)
1219 int numargs = argc;
1220 int j = 1;
1221 int i;
1223 /*@-sizeoftype@*/
1224 if (con->arg_strip)
1225 for (i = 1; i < argc; i++) {
1226 if (PBM_ISSET(i, con->arg_strip))
1227 numargs--;
1230 for (i = 1; i < argc; i++) {
1231 if (con->arg_strip && PBM_ISSET(i, con->arg_strip))
1232 continue;
1233 argv[j] = (j < numargs) ? argv[i] : NULL;
1234 j++;
1236 /*@=sizeoftype@*/
1238 return numargs;
1240 /*@=boundswrite@*/