popt  1.16
popt.c
Go to the documentation of this file.
1 
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 */
8 
9 #undef MYDEBUG
10 
11 #include "system.h"
12 
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
20 
21 #ifdef HAVE_FLOAT_H
22 #include <float.h>
23 #endif
24 #include <math.h>
25 
26 #include "poptint.h"
27 
28 #ifdef MYDEBUG
29 /*@unchecked@*/
30 int _popt_debug = 0;
31 #endif
32 
33 /*@unchecked@*/
34 unsigned int _poptArgMask = POPT_ARG_MASK;
35 /*@unchecked@*/
37 
38 #if !defined(HAVE_STRERROR) && !defined(__LCLINT__)
39 static char * strerror(int errno)
40 {
41  extern int sys_nerr;
42  extern char * sys_errlist[];
43 
44  if ((0 <= errno) && (errno < sys_nerr))
45  return sys_errlist[errno];
46  else
47  return POPT_("unknown errno");
48 }
49 #endif
50 
51 #ifdef MYDEBUG
52 /*@unused@*/
53 static void prtcon(const char *msg, poptContext con)
54 {
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] : ""));
63 }
64 #endif
65 
66 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
67 {
68  if (con == NULL) return;
69  con->execPath = _free(con->execPath);
70  con->execPath = xstrdup(path);
71  con->execAbsolute = allowAbsolute;
72  return;
73 }
74 
75 static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
76  /*@globals internalState@*/
77  /*@modifies internalState@*/
78 {
79  if (con == NULL || opt == NULL) return;
80  for (; opt->longName || opt->shortName || opt->arg; opt++) {
81  poptArg arg = { .ptr = opt->arg };
82  if (arg.ptr)
83  switch (poptArgType(opt)) {
84  case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
85  poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
86  invokeCallbacksPRE(con, arg.opt);
87  /*@switchbreak@*/ break;
88  case POPT_ARG_CALLBACK: /* Perform callback. */
89  if (!CBF_ISSET(opt, PRE))
90  /*@switchbreak@*/ break;
91 /*@-noeffectuncon @*/ /* XXX no known way to annotate (*vector) calls. */
92  arg.cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
93 /*@=noeffectuncon @*/
94  /*@switchbreak@*/ break;
95  }
96  }
97 }
98 
99 static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
100  /*@globals internalState@*/
101  /*@modifies internalState@*/
102 {
103  if (con == NULL || opt == NULL) return;
104  for (; opt->longName || opt->shortName || opt->arg; opt++) {
105  poptArg arg = { .ptr = opt->arg };
106  if (arg.ptr)
107  switch (poptArgType(opt)) {
108  case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
109  poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
110  invokeCallbacksPOST(con, arg.opt);
111  /*@switchbreak@*/ break;
112  case POPT_ARG_CALLBACK: /* Perform callback. */
113  if (!CBF_ISSET(opt, POST))
114  /*@switchbreak@*/ break;
115 /*@-noeffectuncon @*/ /* XXX no known way to annotate (*vector) calls. */
116  arg.cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
117 /*@=noeffectuncon @*/
118  /*@switchbreak@*/ break;
119  }
120  }
121 }
122 
124  const struct poptOption * opt,
125  const struct poptOption * myOpt,
126  /*@null@*/ const void * myData, int shorty)
127  /*@globals internalState@*/
128  /*@modifies internalState@*/
129 {
130  const struct poptOption * cbopt = NULL;
131  poptArg cbarg = { .ptr = NULL };
132 
133  if (con == NULL || opt == NULL) return;
134  for (; opt->longName || opt->shortName || opt->arg; opt++) {
135  poptArg arg = { .ptr = opt->arg };
136  switch (poptArgType(opt)) {
137  case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
138  poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
139  if (opt->arg != NULL)
140  invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
141  /*@switchbreak@*/ break;
142  case POPT_ARG_CALLBACK: /* Save callback info. */
143  if (CBF_ISSET(opt, SKIPOPTION))
144  /*@switchbreak@*/ break;
145  cbopt = opt;
146  cbarg.ptr = opt->arg;
147  /*@switchbreak@*/ break;
148  default: /* Perform callback on matching option. */
149  if (cbopt == NULL || cbarg.cb == NULL)
150  /*@switchbreak@*/ break;
151  if ((myOpt->shortName && opt->shortName && shorty &&
152  myOpt->shortName == opt->shortName)
153  || (myOpt->longName != NULL && opt->longName != NULL &&
154  !strcmp(myOpt->longName, opt->longName)))
155  { const void *cbData = (cbopt->descrip ? cbopt->descrip : myData);
156 /*@-noeffectuncon @*/ /* XXX no known way to annotate (*vector) calls. */
157  cbarg.cb(con, POPT_CALLBACK_REASON_OPTION,
158  myOpt, con->os->nextArg, cbData);
159 /*@=noeffectuncon @*/
160  /* Terminate (unless explcitly continuing). */
161  if (!CBF_ISSET(cbopt, CONTINUE))
162  return;
163  }
164  /*@switchbreak@*/ break;
165  }
166  }
167 }
168 
169 poptContext poptGetContext(const char * name, int argc, const char ** argv,
170  const struct poptOption * options, unsigned int flags)
171 {
172  poptContext con;
173 
174  if (argc < 1) return NULL;
175 
176  con = malloc(sizeof(*con));
177  if (con == NULL) return NULL; /* XXX can't happen */
178  memset(con, 0, sizeof(*con));
179 
180  con->os = con->optionStack;
181  con->os->argc = argc;
182 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
183  con->os->argv = argv;
184 /*@=dependenttrans =assignexpose@*/
185  con->os->argb = NULL;
186 
187  if (!(flags & POPT_CONTEXT_KEEP_FIRST))
188  con->os->next = 1; /* skip argv[0] */
189 
190  con->leftovers = calloc( (size_t)(argc + 1), sizeof(*con->leftovers) );
191 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
192  con->options = options;
193 /*@=dependenttrans =assignexpose@*/
194  con->aliases = NULL;
195  con->numAliases = 0;
196  con->flags = flags;
197  con->execs = NULL;
198  con->numExecs = 0;
199  con->finalArgvAlloced = argc * 2;
200  con->finalArgv = calloc( (size_t)con->finalArgvAlloced, sizeof(*con->finalArgv) );
201  if (con->finalArgv == NULL) con->finalArgvAlloced = 0;
202  con->execAbsolute = 1;
203  con->arg_strip = NULL;
204 
205  if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
207 
208  if (name)
209  con->appName = xstrdup(name);
210 
211  invokeCallbacksPRE(con, con->options);
212 
213  return con;
214 }
215 
216 static void cleanOSE(/*@special@*/ struct optionStackEntry *os)
217  /*@uses os @*/
218  /*@releases os->nextArg, os->argv, os->argb @*/
219  /*@modifies os @*/
220 {
221  os->nextArg = _free(os->nextArg);
222  os->argv = _free(os->argv);
223  os->argb = PBM_FREE(os->argb);
224 }
225 
227 {
228  int i;
229 
230  if (con == NULL) return;
231  while (con->os > con->optionStack) {
232  cleanOSE(con->os--);
233  }
234  con->os->argb = PBM_FREE(con->os->argb);
235  con->os->currAlias = NULL;
236  con->os->nextCharArg = NULL;
237  con->os->nextArg = NULL;
238  con->os->next = 1; /* skip argv[0] */
239 
240  con->numLeftovers = 0;
241  con->nextLeftover = 0;
242  con->restLeftover = 0;
243  con->doExec = NULL;
244 
245  if (con->finalArgv != NULL)
246  for (i = 0; i < con->finalArgvCount; i++) {
247 /*@-unqualifiedtrans@*/ /* FIX: typedef double indirection. */
248  con->finalArgv[i] = _free(con->finalArgv[i]);
249 /*@=unqualifiedtrans@*/
250  }
251 
252  con->finalArgvCount = 0;
253  con->arg_strip = PBM_FREE(con->arg_strip);
254 /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
255  return;
256 /*@=nullstate@*/
257 }
258 
259 /* Only one of longName, shortName should be set, not both. */
260 static int handleExec(/*@special@*/ poptContext con,
261  /*@null@*/ const char * longName, char shortName)
262  /*@uses con->execs, con->numExecs, con->flags, con->doExec,
263  con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
264  /*@modifies con @*/
265 {
266  poptItem item;
267  int i;
268 
269  if (con == NULL || con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
270  return 0;
271 
272  for (i = con->numExecs - 1; i >= 0; i--) {
273  item = con->execs + i;
274  if (longName && !(item->option.longName &&
275  !strcmp(longName, item->option.longName)))
276  continue;
277  else if (shortName != item->option.shortName)
278  continue;
279  break;
280  }
281  if (i < 0) return 0;
282 
283 
284  if (con->flags & POPT_CONTEXT_NO_EXEC)
285  return 1;
286 
287  if (con->doExec == NULL) {
288  con->doExec = con->execs + i;
289  return 1;
290  }
291 
292  /* We already have an exec to do; remember this option for next
293  time 'round */
294  if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
295  con->finalArgvAlloced += 10;
296  con->finalArgv = realloc(con->finalArgv,
297  sizeof(*con->finalArgv) * con->finalArgvAlloced);
298  if (con->finalArgv == NULL) con->finalArgvCount = con->finalArgvAlloced = 0;
299  }
300 
301  i = con->finalArgvCount++;
302  if (con->finalArgv != NULL) /* XXX can't happen */
303  { char *s = malloc((longName ? strlen(longName) : 0) + sizeof("--"));
304  if (s != NULL) { /* XXX can't happen */
305  con->finalArgv[i] = s;
306  *s++ = '-';
307  if (longName)
308  s = stpcpy( stpcpy(s, "-"), longName);
309  else
310  *s++ = shortName;
311  *s = '\0';
312  } else
313  con->finalArgv[i] = NULL;
314  }
315 
316  return 1;
317 }
318 
326 static int
327 longOptionStrcmp(const struct poptOption * opt,
328  /*@null@*/ const char * longName, size_t longNameLen)
329  /*@*/
330 {
331  const char * optLongName = opt->longName;
332  int rc;
333 
334  if (optLongName == NULL || longName == NULL) /* XXX can't heppen */
335  return 0;
336 
337  if (F_ISSET(opt, TOGGLE)) {
338  if (optLongName[0] == 'n' && optLongName[1] == 'o') {
339  optLongName += sizeof("no") - 1;
340  if (optLongName[0] == '-')
341  optLongName++;
342  }
343  if (longName[0] == 'n' && longName[1] == 'o') {
344  longName += sizeof("no") - 1;
345  longNameLen -= sizeof("no") - 1;
346  if (longName[0] == '-') {
347  longName++;
348  longNameLen--;
349  }
350  }
351  }
352  rc = (int)(strlen(optLongName) == longNameLen);
353  if (rc)
354  rc = (int)(strncmp(optLongName, longName, longNameLen) == 0);
355  return rc;
356 }
357 
358 /* Only one of longName, shortName may be set at a time */
359 static int handleAlias(/*@special@*/ poptContext con,
360  /*@null@*/ const char * longName, size_t longNameLen,
361  char shortName,
362  /*@exposed@*/ /*@null@*/ const char * nextArg)
363  /*@uses con->aliases, con->numAliases, con->optionStack, con->os,
364  con->os->currAlias, con->os->currAlias->option.longName @*/
365  /*@modifies con @*/
366 {
367  poptItem item = (con && con->os) ? con->os->currAlias : NULL;
368  int rc;
369  int i;
370 
371  if (item) {
372  if (longName && item->option.longName != NULL
373  && longOptionStrcmp(&item->option, longName, longNameLen))
374  return 0;
375  else
376  if (shortName && shortName == item->option.shortName)
377  return 0;
378  }
379 
380  if (con == NULL || con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
381  return 0;
382 
383  for (i = con->numAliases - 1; i >= 0; i--) {
384  item = con->aliases + i;
385  if (longName) {
386  if (item->option.longName == NULL)
387  continue;
388  if (!longOptionStrcmp(&item->option, longName, longNameLen))
389  continue;
390  } else if (shortName != item->option.shortName)
391  continue;
392  break;
393  }
394  if (i < 0) return 0;
395 
396  if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
397  return POPT_ERROR_OPTSTOODEEP;
398 
399  if (longName == NULL && nextArg != NULL && *nextArg != '\0')
400  con->os->nextCharArg = nextArg;
401 
402  con->os++;
403  con->os->next = 0;
404  con->os->stuffed = 0;
405  con->os->nextArg = NULL;
406  con->os->nextCharArg = NULL;
407  con->os->currAlias = con->aliases + i;
408  { const char ** av;
409  int ac = con->os->currAlias->argc;
410  /* Append --foo=bar arg to alias argv array (if present). */
411  if (longName && nextArg != NULL && *nextArg != '\0') {
412  av = malloc((ac + 1 + 1) * sizeof(*av));
413  if (av != NULL) { /* XXX won't happen. */
414  for (i = 0; i < ac; i++) {
415  av[i] = con->os->currAlias->argv[i];
416  }
417  av[ac++] = nextArg;
418  av[ac] = NULL;
419  } else /* XXX revert to old popt behavior if malloc fails. */
420  av = con->os->currAlias->argv;
421  } else
422  av = con->os->currAlias->argv;
423  rc = poptDupArgv(ac, av, &con->os->argc, &con->os->argv);
424  if (av != NULL && av != con->os->currAlias->argv)
425  free(av);
426  }
427  con->os->argb = NULL;
428 
429  return (rc ? rc : 1);
430 }
431 
437 static /*@null@*/
438 const char * findProgramPath(/*@null@*/ const char * argv0)
439  /*@*/
440 {
441  char *path = NULL, *s = NULL, *se;
442  char *t = NULL;
443 
444  if (argv0 == NULL) return NULL; /* XXX can't happen */
445 
446  /* If there is a / in argv[0], it has to be an absolute path. */
447  /* XXX Hmmm, why not if (argv0[0] == '/') ... instead? */
448  if (strchr(argv0, '/'))
449  return xstrdup(argv0);
450 
451  if ((path = getenv("PATH")) == NULL || (path = xstrdup(path)) == NULL)
452  return NULL;
453 
454  /* The return buffer in t is big enough for any path. */
455  if ((t = malloc(strlen(path) + strlen(argv0) + sizeof("/"))) != NULL)
456  for (s = path; s && *s; s = se) {
457 
458  /* Snip PATH element into [s,se). */
459  if ((se = strchr(s, ':')))
460  *se++ = '\0';
461 
462  /* Append argv0 to PATH element. */
463  (void) stpcpy(stpcpy(stpcpy(t, s), "/"), argv0);
464 
465  /* If file is executable, bingo! */
466  if (!access(t, X_OK))
467  break;
468  }
469 
470  /* If no executable was found in PATH, return NULL. */
471 /*@-compdef@*/
472  if (!(s && *s) && t != NULL)
473  t = _free(t);
474 /*@=compdef@*/
475 /*@-modobserver -observertrans -usedef @*/
476  path = _free(path);
477 /*@=modobserver =observertrans =usedef @*/
478 
479  return t;
480 }
481 
482 static int execCommand(poptContext con)
483  /*@globals internalState @*/
484  /*@modifies internalState @*/
485 {
486  poptItem item = con ? con->doExec : NULL;
487  poptArgv argv = NULL;
488  int argc = 0;
489  int rc;
490  int ec = POPT_ERROR_ERRNO;
491 
492  if (item == NULL) /*XXX can't happen*/
493  return POPT_ERROR_NOARG;
494 
495  if (item->argv == NULL || item->argc < 1 ||
496  (!con->execAbsolute && strchr(item->argv[0], '/')))
497  return POPT_ERROR_NOARG;
498 
499  argv = malloc(sizeof(*argv) *
500  (6 + item->argc + con->numLeftovers + con->finalArgvCount));
501  if (argv == NULL) return POPT_ERROR_MALLOC;
502 
503  if (!strchr(item->argv[0], '/') && con->execPath != NULL) {
504  char *s = malloc(strlen(con->execPath) + strlen(item->argv[0]) + sizeof("/"));
505  if (s)
506  (void)stpcpy(stpcpy(stpcpy(s, con->execPath), "/"), item->argv[0]);
507 
508  argv[argc] = s;
509  } else
510  argv[argc] = findProgramPath(item->argv[0]);
511  if (argv[argc++] == NULL) {
512  ec = POPT_ERROR_NOARG;
513  goto exit;
514  }
515 
516  if (item->argc > 1) {
517  memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
518  argc += (item->argc - 1);
519  }
520 
521  if (con->finalArgv != NULL && con->finalArgvCount > 0) {
522  memcpy(argv + argc, con->finalArgv,
523  sizeof(*argv) * con->finalArgvCount);
524  argc += con->finalArgvCount;
525  }
526 
527  if (con->leftovers != NULL && con->numLeftovers > 0) {
528  memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
529  argc += con->numLeftovers;
530  }
531 
532  argv[argc] = NULL;
533 
534 #if defined(hpux) || defined(__hpux)
535  rc = setresgid(getgid(), getgid(),-1);
536  if (rc) goto exit;
537  rc = setresuid(getuid(), getuid(),-1);
538  if (rc) goto exit;
539 #else
540 /*
541  * XXX " ... on BSD systems setuid() should be preferred over setreuid()"
542  * XXX sez' Timur Bakeyev <mc@bat.ru>
543  * XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
544  */
545 #if defined(HAVE_SETUID)
546  rc = setgid(getgid());
547  if (rc) goto exit;
548  rc = setuid(getuid());
549  if (rc) goto exit;
550 #elif defined (HAVE_SETREUID)
551  rc = setregid(getgid(), getgid());
552  if (rc) goto exit;
553  rc = setreuid(getuid(), getuid());
554  if (rc) goto exit;
555 #else
556  ; /* Can't drop privileges */
557 #endif
558 #endif
559 
560 #ifdef MYDEBUG
561 if (_popt_debug)
562  { poptArgv avp;
563  fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
564  for (avp = argv; *avp; avp++)
565  fprintf(stderr, " '%s'", *avp);
566  fprintf(stderr, "\n");
567  }
568 #endif
569 
570 /*@-nullstate@*/
571  rc = execvp(argv[0], (char *const *)argv);
572 /*@=nullstate@*/
573 
574 exit:
575  if (argv) {
576  if (argv[0])
577  free((void *)argv[0]);
578  free(argv);
579  }
580  return ec;
581 }
582 
583 /*@observer@*/ /*@null@*/
584 static const struct poptOption *
585 findOption(const struct poptOption * opt,
586  /*@null@*/ const char * longName, size_t longNameLen,
587  char shortName,
588  /*@null@*/ /*@out@*/ poptCallbackType * callback,
589  /*@null@*/ /*@out@*/ const void ** callbackData,
590  unsigned int argInfo)
591  /*@modifies *callback, *callbackData */
592 {
593  const struct poptOption * cb = NULL;
594  poptArg cbarg = { .ptr = NULL };
595 
596  /* This happens when a single - is given */
597  if (LF_ISSET(ONEDASH) && !shortName && (longName && *longName == '\0'))
598  shortName = '-';
599 
600  for (; opt->longName || opt->shortName || opt->arg; opt++) {
601  poptArg arg = { .ptr = opt->arg };
602 
603  switch (poptArgType(opt)) {
604  case POPT_ARG_INCLUDE_TABLE: /* Recurse on included sub-tables. */
605  { const struct poptOption * opt2;
606 
607  poptSubstituteHelpI18N(arg.opt); /* XXX side effects */
608  if (arg.ptr == NULL) continue; /* XXX program error */
609  opt2 = findOption(arg.opt, longName, longNameLen, shortName, callback,
610  callbackData, argInfo);
611  if (opt2 == NULL) continue;
612  /* Sub-table data will be inheirited if no data yet. */
613 /*@-observertrans -dependenttrans @*/
614  if (callback && *callback
615  && callbackData && *callbackData == NULL)
616  *callbackData = opt->descrip;
617 /*@=observertrans =dependenttrans @*/
618  return opt2;
619  } /*@notreached@*/ /*@switchbreak@*/ break;
620  case POPT_ARG_CALLBACK:
621  cb = opt;
622  cbarg.ptr = opt->arg;
623  continue;
624  /*@notreached@*/ /*@switchbreak@*/ break;
625  default:
626  /*@switchbreak@*/ break;
627  }
628 
629  if (longName != NULL && opt->longName != NULL &&
630  (!LF_ISSET(ONEDASH) || F_ISSET(opt, ONEDASH)) &&
631  longOptionStrcmp(opt, longName, longNameLen))
632  {
633  break;
634  } else if (shortName && shortName == opt->shortName) {
635  break;
636  }
637  }
638 
639  if (opt->longName == NULL && !opt->shortName)
640  return NULL;
641 
642 /*@-modobserver -mods @*/
643  if (callback)
644  *callback = (cb ? cbarg.cb : NULL);
645  if (callbackData)
646 /*@-observertrans -dependenttrans @*/
647  *callbackData = (cb && !CBF_ISSET(cb, INC_DATA) ? cb->descrip : NULL);
648 /*@=observertrans =dependenttrans @*/
649 /*@=modobserver =mods @*/
650 
651  return opt;
652 }
653 
654 static const char * findNextArg(/*@special@*/ poptContext con,
655  unsigned argx, int delete_arg)
656  /*@uses con->optionStack, con->os,
657  con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
658  /*@modifies con @*/
659 {
660  struct optionStackEntry * os = con ? con->os : NULL;
661  const char * arg;
662 
663  if (os == NULL || con->optionStack == NULL) return NULL;
664 
665  do {
666  int i;
667  arg = NULL;
668  while (os->next == os->argc && os > con->optionStack) os--;
669  if (os->next == os->argc && os == con->optionStack) break;
670  if (os->argv != NULL)
671  for (i = os->next; i < os->argc; i++) {
672 /*@-sizeoftype@*/
673  if (os->argb && PBM_ISSET(i, os->argb))
674  /*@innercontinue@*/ continue;
675  if (*os->argv[i] == '-')
676  /*@innercontinue@*/ continue;
677  if (--argx > 0)
678  /*@innercontinue@*/ continue;
679  arg = os->argv[i];
680  if (delete_arg) {
681  if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
682  if (os->argb != NULL) /* XXX can't happen */
683  PBM_SET(i, os->argb);
684  }
685  /*@innerbreak@*/ break;
686 /*@=sizeoftype@*/
687  }
688  if (os > con->optionStack) os--;
689  } while (arg == NULL);
690  return arg;
691 }
692 
693 static /*@only@*/ /*@null@*/ const char *
694 expandNextArg(/*@special@*/ poptContext con, const char * s)
695  /*@uses con->optionStack, con->os,
696  con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
697  /*@modifies con @*/
698 {
699  const char * a = NULL;
700  char *t, *te;
701  size_t tn = strlen(s) + 1;
702  char c;
703 
704  if (con == NULL) return NULL;
705 
706  te = t = malloc(tn);
707  if (t == NULL) return NULL; /* XXX can't happen */
708  *t = '\0';
709  while ((c = *s++) != '\0') {
710  switch (c) {
711 #if 0 /* XXX can't do this */
712  case '\\': /* escape */
713  c = *s++;
714  /*@switchbreak@*/ break;
715 #endif
716  case '!':
717  if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
718  /*@switchbreak@*/ break;
719  /* XXX Make sure that findNextArg deletes only next arg. */
720  if (a == NULL) {
721  if ((a = findNextArg(con, 1U, 1)) == NULL)
722  /*@switchbreak@*/ break;
723  }
724  s += sizeof("#:+") - 1;
725 
726  tn += strlen(a);
727  { size_t pos = (size_t) (te - t);
728  if ((t = realloc(t, tn)) == NULL) /* XXX can't happen */
729  return NULL;
730  te = stpcpy(t + pos, a);
731  }
732  continue;
733  /*@notreached@*/ /*@switchbreak@*/ break;
734  default:
735  /*@switchbreak@*/ break;
736  }
737  *te++ = c;
738  }
739  *te++ = '\0';
740  /* If the new string is longer than needed, shorten. */
741  if ((t + tn) > te) {
742 /*@-usereleased@*/ /* XXX splint can't follow the pointers. */
743  if ((te = realloc(t, (size_t)(te - t))) == NULL)
744  free(t);
745  t = te;
746 /*@=usereleased@*/
747  }
748  return t;
749 }
750 
751 static void poptStripArg(/*@special@*/ poptContext con, int which)
752  /*@uses con->optionStack @*/
753  /*@defines con->arg_strip @*/
754  /*@modifies con @*/
755 {
756 /*@-compdef -sizeoftype -usedef @*/
757  if (con->arg_strip == NULL)
758  con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
759  if (con->arg_strip != NULL) /* XXX can't happen */
760  PBM_SET(which, con->arg_strip);
761  return;
762 /*@=compdef =sizeoftype =usedef @*/
763 }
764 
765 /*@unchecked@*/
766 unsigned int _poptBitsN = _POPT_BITS_N;
767 /*@unchecked@*/
768 unsigned int _poptBitsM = _POPT_BITS_M;
769 /*@unchecked@*/
770 unsigned int _poptBitsK = _POPT_BITS_K;
771 
772 /*@-sizeoftype@*/
773 static int _poptBitsNew(/*@null@*/ poptBits *bitsp)
774  /*@globals _poptBitsN, _poptBitsM, _poptBitsK @*/
775  /*@modifies *bitsp, _poptBitsN, _poptBitsM, _poptBitsK @*/
776 {
777  if (bitsp == NULL)
778  return POPT_ERROR_NULLARG;
779 
780  /* XXX handle negated initialization. */
781  if (*bitsp == NULL) {
782  if (_poptBitsN == 0) {
785  }
786  if (_poptBitsM == 0U) _poptBitsM = (3 * _poptBitsN) / 2;
787  if (_poptBitsK == 0U || _poptBitsK > 32U) _poptBitsK = _POPT_BITS_K;
788  *bitsp = PBM_ALLOC(_poptBitsM-1);
789  }
790 /*@-nullstate@*/
791  return 0;
792 /*@=nullstate@*/
793 }
794 
795 int poptBitsAdd(poptBits bits, const char * s)
796 {
797  size_t ns = (s ? strlen(s) : 0);
798  uint32_t h0 = 0;
799  uint32_t h1 = 0;
800 
801  if (bits == NULL || ns == 0)
802  return POPT_ERROR_NULLARG;
803 
804  poptJlu32lpair(s, ns, &h0, &h1);
805 
806  for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
807  uint32_t h = h0 + ns * h1;
808  uint32_t ix = (h % _poptBitsM);
809  PBM_SET(ix, bits);
810  }
811  return 0;
812 }
813 
814 int poptBitsChk(poptBits bits, const char * s)
815 {
816  size_t ns = (s ? strlen(s) : 0);
817  uint32_t h0 = 0;
818  uint32_t h1 = 0;
819  int rc = 1;
820 
821  if (bits == NULL || ns == 0)
822  return POPT_ERROR_NULLARG;
823 
824  poptJlu32lpair(s, ns, &h0, &h1);
825 
826  for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
827  uint32_t h = h0 + ns * h1;
828  uint32_t ix = (h % _poptBitsM);
829  if (PBM_ISSET(ix, bits))
830  continue;
831  rc = 0;
832  break;
833  }
834  return rc;
835 }
836 
838 {
839  static size_t nbw = (__PBM_NBITS/8);
840  size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
841 
842  if (bits == NULL)
843  return POPT_ERROR_NULLARG;
844  memset(bits, 0, nw * nbw);
845  return 0;
846 }
847 
848 int poptBitsDel(poptBits bits, const char * s)
849 {
850  size_t ns = (s ? strlen(s) : 0);
851  uint32_t h0 = 0;
852  uint32_t h1 = 0;
853 
854  if (bits == NULL || ns == 0)
855  return POPT_ERROR_NULLARG;
856 
857  poptJlu32lpair(s, ns, &h0, &h1);
858 
859  for (ns = 0; ns < (size_t)_poptBitsK; ns++) {
860  uint32_t h = h0 + ns * h1;
861  uint32_t ix = (h % _poptBitsM);
862  PBM_CLR(ix, bits);
863  }
864  return 0;
865 }
866 
868 {
869  __pbm_bits *abits;
870  __pbm_bits *bbits;
871  __pbm_bits rc = 0;
872  size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
873  size_t i;
874 
875  if (ap == NULL || b == NULL || _poptBitsNew(ap))
876  return POPT_ERROR_NULLARG;
877  abits = __PBM_BITS(*ap);
878  bbits = __PBM_BITS(b);
879 
880  for (i = 0; i < nw; i++) {
881  abits[i] &= bbits[i];
882  rc |= abits[i];
883  }
884  return (rc ? 1 : 0);
885 }
886 
887 int poptBitsUnion(poptBits *ap, const poptBits b)
888 {
889  __pbm_bits *abits;
890  __pbm_bits *bbits;
891  __pbm_bits rc = 0;
892  size_t nw = (__PBM_IX(_poptBitsM-1) + 1);
893  size_t i;
894 
895  if (ap == NULL || b == NULL || _poptBitsNew(ap))
896  return POPT_ERROR_NULLARG;
897  abits = __PBM_BITS(*ap);
898  bbits = __PBM_BITS(b);
899 
900  for (i = 0; i < nw; i++) {
901  abits[i] |= bbits[i];
902  rc |= abits[i];
903  }
904  return (rc ? 1 : 0);
905 }
906 
908 {
909  const char ** av;
910  int rc = 0;
911 
912  if (con == NULL || ap == NULL || _poptBitsNew(ap) ||
913  con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
914  return POPT_ERROR_NULLARG;
915 
916  /* some apps like [like RPM ;-) ] need this NULL terminated */
917  con->leftovers[con->numLeftovers] = NULL;
918 
919  for (av = con->leftovers + con->nextLeftover; *av != NULL; av++) {
920  if ((rc = poptBitsAdd(*ap, *av)) != 0)
921  break;
922  }
923 /*@-nullstate@*/
924  return rc;
925 /*@=nullstate@*/
926 }
927 
928 int poptSaveBits(poptBits * bitsp,
929  /*@unused@*/ UNUSED(unsigned int argInfo), const char * s)
930 {
931  char *tbuf = NULL;
932  char *t, *te;
933  int rc = 0;
934 
935  if (bitsp == NULL || s == NULL || *s == '\0' || _poptBitsNew(bitsp))
936  return POPT_ERROR_NULLARG;
937 
938  /* Parse comma separated attributes. */
939  te = tbuf = xstrdup(s);
940  while ((t = te) != NULL && *t) {
941  while (*te != '\0' && *te != ',')
942  te++;
943  if (*te != '\0')
944  *te++ = '\0';
945  /* XXX Ignore empty strings. */
946  if (*t == '\0')
947  continue;
948  /* XXX Permit negated attributes. caveat emptor: false negatives. */
949  if (*t == '!') {
950  t++;
951  if ((rc = poptBitsChk(*bitsp, t)) > 0)
952  rc = poptBitsDel(*bitsp, t);
953  } else
954  rc = poptBitsAdd(*bitsp, t);
955  if (rc)
956  break;
957  }
958  tbuf = _free(tbuf);
959  return rc;
960 }
961 /*@=sizeoftype@*/
962 
963 int poptSaveString(const char *** argvp,
964  /*@unused@*/ UNUSED(unsigned int argInfo), const char * val)
965 {
966  int argc = 0;
967 
968  if (argvp == NULL || val == NULL)
969  return POPT_ERROR_NULLARG;
970 
971  /* XXX likely needs an upper bound on argc. */
972  if (*argvp != NULL)
973  while ((*argvp)[argc] != NULL)
974  argc++;
975 
976 /*@-unqualifiedtrans -nullstate@*/ /* XXX no annotation for (*argvp) */
977  if ((*argvp = xrealloc(*argvp, (argc + 1 + 1) * sizeof(**argvp))) != NULL) {
978  (*argvp)[argc++] = xstrdup(val);
979  (*argvp)[argc ] = NULL;
980  }
981  return 0;
982 /*@=unqualifiedtrans =nullstate@*/
983 }
984 
985 /*@unchecked@*/
986 static unsigned int seed = 0;
987 
988 int poptSaveLongLong(long long * arg, unsigned int argInfo, long long aLongLong)
989 {
990  if (arg == NULL
991 #ifdef NOTYET
992  /* XXX Check alignment, may fail on funky platforms. */
993  || (((unsigned long long)arg) & (sizeof(*arg)-1))
994 #endif
995  )
996  return POPT_ERROR_NULLARG;
997 
998  if (aLongLong != 0 && LF_ISSET(RANDOM)) {
999 #if defined(HAVE_SRANDOM)
1000  if (!seed) {
1001  srandom((unsigned)getpid());
1002  srandom((unsigned)random());
1003  }
1004  aLongLong = (long long)(random() % (aLongLong > 0 ? aLongLong : -aLongLong));
1005  aLongLong++;
1006 #else
1007  /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
1008  return POPT_ERROR_BADOPERATION;
1009 #endif
1010  }
1011  if (LF_ISSET(NOT))
1012  aLongLong = ~aLongLong;
1013  switch (LF_ISSET(LOGICALOPS)) {
1014  case 0:
1015  *arg = aLongLong;
1016  break;
1017  case POPT_ARGFLAG_OR:
1018  *(unsigned long long *)arg |= (unsigned long long)aLongLong;
1019  break;
1020  case POPT_ARGFLAG_AND:
1021  *(unsigned long long *)arg &= (unsigned long long)aLongLong;
1022  break;
1023  case POPT_ARGFLAG_XOR:
1024  *(unsigned long long *)arg ^= (unsigned long long)aLongLong;
1025  break;
1026  default:
1027  return POPT_ERROR_BADOPERATION;
1028  /*@notreached@*/ break;
1029  }
1030  return 0;
1031 }
1032 
1033 int poptSaveLong(long * arg, unsigned int argInfo, long aLong)
1034 {
1035  /* XXX Check alignment, may fail on funky platforms. */
1036  if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
1037  return POPT_ERROR_NULLARG;
1038 
1039  if (aLong != 0 && LF_ISSET(RANDOM)) {
1040 #if defined(HAVE_SRANDOM)
1041  if (!seed) {
1042  srandom((unsigned)getpid());
1043  srandom((unsigned)random());
1044  }
1045  aLong = random() % (aLong > 0 ? aLong : -aLong);
1046  aLong++;
1047 #else
1048  /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
1049  return POPT_ERROR_BADOPERATION;
1050 #endif
1051  }
1052  if (LF_ISSET(NOT))
1053  aLong = ~aLong;
1054  switch (LF_ISSET(LOGICALOPS)) {
1055  case 0: *arg = aLong; break;
1056  case POPT_ARGFLAG_OR: *(unsigned long *)arg |= (unsigned long)aLong; break;
1057  case POPT_ARGFLAG_AND: *(unsigned long *)arg &= (unsigned long)aLong; break;
1058  case POPT_ARGFLAG_XOR: *(unsigned long *)arg ^= (unsigned long)aLong; break;
1059  default:
1060  return POPT_ERROR_BADOPERATION;
1061  /*@notreached@*/ break;
1062  }
1063  return 0;
1064 }
1065 
1066 int poptSaveInt(/*@null@*/ int * arg, unsigned int argInfo, long aLong)
1067 {
1068  /* XXX Check alignment, may fail on funky platforms. */
1069  if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
1070  return POPT_ERROR_NULLARG;
1071 
1072  if (aLong != 0 && LF_ISSET(RANDOM)) {
1073 #if defined(HAVE_SRANDOM)
1074  if (!seed) {
1075  srandom((unsigned)getpid());
1076  srandom((unsigned)random());
1077  }
1078  aLong = random() % (aLong > 0 ? aLong : -aLong);
1079  aLong++;
1080 #else
1081  /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
1082  return POPT_ERROR_BADOPERATION;
1083 #endif
1084  }
1085  if (LF_ISSET(NOT))
1086  aLong = ~aLong;
1087  switch (LF_ISSET(LOGICALOPS)) {
1088  case 0: *arg = (int) aLong; break;
1089  case POPT_ARGFLAG_OR: *(unsigned int *)arg |= (unsigned int) aLong; break;
1090  case POPT_ARGFLAG_AND: *(unsigned int *)arg &= (unsigned int) aLong; break;
1091  case POPT_ARGFLAG_XOR: *(unsigned int *)arg ^= (unsigned int) aLong; break;
1092  default:
1093  return POPT_ERROR_BADOPERATION;
1094  /*@notreached@*/ break;
1095  }
1096  return 0;
1097 }
1098 
1099 int poptSaveShort(/*@null@*/ short * arg, unsigned int argInfo, long aLong)
1100 {
1101  /* XXX Check alignment, may fail on funky platforms. */
1102  if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
1103  return POPT_ERROR_NULLARG;
1104 
1105  if (aLong != 0 && LF_ISSET(RANDOM)) {
1106 #if defined(HAVE_SRANDOM)
1107  if (!seed) {
1108  srandom((unsigned)getpid());
1109  srandom((unsigned)random());
1110  }
1111  aLong = random() % (aLong > 0 ? aLong : -aLong);
1112  aLong++;
1113 #else
1114  /* XXX avoid adding POPT_ERROR_UNIMPLEMENTED to minimize i18n churn. */
1115  return POPT_ERROR_BADOPERATION;
1116 #endif
1117  }
1118  if (LF_ISSET(NOT))
1119  aLong = ~aLong;
1120  switch (LF_ISSET(LOGICALOPS)) {
1121  case 0: *arg = (short) aLong;
1122  break;
1123  case POPT_ARGFLAG_OR: *(unsigned short *)arg |= (unsigned short) aLong;
1124  break;
1125  case POPT_ARGFLAG_AND: *(unsigned short *)arg &= (unsigned short) aLong;
1126  break;
1127  case POPT_ARGFLAG_XOR: *(unsigned short *)arg ^= (unsigned short) aLong;
1128  break;
1129  default: return POPT_ERROR_BADOPERATION;
1130  /*@notreached@*/ break;
1131  }
1132  return 0;
1133 }
1134 
1141 static unsigned int poptArgInfo(poptContext con, const struct poptOption * opt)
1142  /*@*/
1143 {
1144  unsigned int argInfo = opt->argInfo;
1145 
1146  if (con->os->argv != NULL && con->os->next > 0 && opt->longName != NULL)
1147  if (LF_ISSET(TOGGLE)) {
1148  const char * longName = con->os->argv[con->os->next-1];
1149  while (*longName == '-') longName++;
1150  /* XXX almost good enough but consider --[no]nofoo corner cases. */
1151  if (longName[0] != opt->longName[0] || longName[1] != opt->longName[1])
1152  {
1153  if (!LF_ISSET(XOR)) { /* XXX dont toggle with XOR */
1154  /* Toggle POPT_BIT_SET <=> POPT_BIT_CLR. */
1155  if (LF_ISSET(LOGICALOPS))
1156  argInfo ^= (POPT_ARGFLAG_OR|POPT_ARGFLAG_AND);
1157  argInfo ^= POPT_ARGFLAG_NOT;
1158  }
1159  }
1160  }
1161  return argInfo;
1162 }
1163 
1171 static int poptParseInteger(long long * llp,
1172  /*@unused@*/ UNUSED(unsigned int argInfo),
1173  /*@null@*/ const char * val)
1174  /*@modifies *llp @*/
1175 {
1176  if (val) {
1177  char *end = NULL;
1178  *llp = strtoll(val, &end, 0);
1179 
1180  /* XXX parse scaling suffixes here. */
1181 
1182  if (!(end && *end == '\0'))
1183  return POPT_ERROR_BADNUMBER;
1184  } else
1185  *llp = 0;
1186  return 0;
1187 }
1188 
1195 static int poptSaveArg(poptContext con, const struct poptOption * opt)
1196  /*@globals fileSystem, internalState @*/
1197  /*@modifies con, fileSystem, internalState @*/
1198 {
1199  poptArg arg = { .ptr = opt->arg };
1200  int rc = 0; /* assume success */
1201 
1202  switch (poptArgType(opt)) {
1203  case POPT_ARG_BITSET:
1204  /* XXX memory leak, application is responsible for free. */
1205  rc = poptSaveBits(arg.ptr, opt->argInfo, con->os->nextArg);
1206  /*@switchbreak@*/ break;
1207  case POPT_ARG_ARGV:
1208  /* XXX memory leak, application is responsible for free. */
1209  rc = poptSaveString(arg.ptr, opt->argInfo, con->os->nextArg);
1210  /*@switchbreak@*/ break;
1211  case POPT_ARG_STRING:
1212  /* XXX memory leak, application is responsible for free. */
1213  arg.argv[0] = (con->os->nextArg) ? xstrdup(con->os->nextArg) : NULL;
1214  /*@switchbreak@*/ break;
1215 
1216  case POPT_ARG_INT:
1217  case POPT_ARG_SHORT:
1218  case POPT_ARG_LONG:
1219  case POPT_ARG_LONGLONG:
1220  { unsigned int argInfo = poptArgInfo(con, opt);
1221  long long aNUM = 0;
1222 
1223  if ((rc = poptParseInteger(&aNUM, argInfo, con->os->nextArg)) != 0)
1224  break;
1225 
1226  switch (poptArgType(opt)) {
1227  case POPT_ARG_LONGLONG:
1228 /* XXX let's not demand C99 compiler flags for <limits.h> quite yet. */
1229 #if !defined(LLONG_MAX)
1230 # define LLONG_MAX 9223372036854775807LL
1231 # define LLONG_MIN (-LLONG_MAX - 1LL)
1232 #endif
1233  rc = !(aNUM == LLONG_MIN || aNUM == LLONG_MAX)
1234  ? poptSaveLongLong(arg.longlongp, argInfo, aNUM)
1236  /*@innerbreak@*/ break;
1237  case POPT_ARG_LONG:
1238  rc = !(aNUM < (long long)LONG_MIN || aNUM > (long long)LONG_MAX)
1239  ? poptSaveLong(arg.longp, argInfo, (long)aNUM)
1241  /*@innerbreak@*/ break;
1242  case POPT_ARG_INT:
1243  rc = !(aNUM < (long long)INT_MIN || aNUM > (long long)INT_MAX)
1244  ? poptSaveInt(arg.intp, argInfo, (long)aNUM)
1246  /*@innerbreak@*/ break;
1247  case POPT_ARG_SHORT:
1248  rc = !(aNUM < (long long)SHRT_MIN || aNUM > (long long)SHRT_MAX)
1249  ? poptSaveShort(arg.shortp, argInfo, (long)aNUM)
1251  /*@innerbreak@*/ break;
1252  }
1253  } /*@switchbreak@*/ break;
1254 
1255  case POPT_ARG_FLOAT:
1256  case POPT_ARG_DOUBLE:
1257  { char *end = NULL;
1258  double aDouble = 0.0;
1259 
1260  if (con->os->nextArg) {
1261 /*@-mods@*/
1262  int saveerrno = errno;
1263  errno = 0;
1264  aDouble = strtod(con->os->nextArg, &end);
1265  if (errno == ERANGE) {
1266  rc = POPT_ERROR_OVERFLOW;
1267  break;
1268  }
1269  errno = saveerrno;
1270 /*@=mods@*/
1271  if (*end != '\0') {
1272  rc = POPT_ERROR_BADNUMBER;
1273  break;
1274  }
1275  }
1276 
1277  switch (poptArgType(opt)) {
1278  case POPT_ARG_DOUBLE:
1279  arg.doublep[0] = aDouble;
1280  /*@innerbreak@*/ break;
1281  case POPT_ARG_FLOAT:
1282 #if !defined(DBL_EPSILON) && !defined(__LCLINT__)
1283 #define DBL_EPSILON 2.2204460492503131e-16
1284 #endif
1285 #define POPT_ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
1286  if ((FLT_MIN - POPT_ABS(aDouble)) > DBL_EPSILON
1287  || (POPT_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
1288  rc = POPT_ERROR_OVERFLOW;
1289  else
1290  arg.floatp[0] = (float) aDouble;
1291  /*@innerbreak@*/ break;
1292  }
1293  } /*@switchbreak@*/ break;
1294  case POPT_ARG_MAINCALL:
1295 /*@-assignexpose -type@*/
1296  con->maincall = opt->arg;
1297 /*@=assignexpose =type@*/
1298  /*@switchbreak@*/ break;
1299  default:
1300  fprintf(stdout, POPT_("option type (%u) not implemented in popt\n"),
1301  poptArgType(opt));
1302  exit(EXIT_FAILURE);
1303  /*@notreached@*/ /*@switchbreak@*/ break;
1304  }
1305  return rc;
1306 }
1307 
1308 /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
1310 {
1311  const struct poptOption * opt = NULL;
1312  int done = 0;
1313 
1314  if (con == NULL)
1315  return -1;
1316  while (!done) {
1317  const char * origOptString = NULL;
1318  poptCallbackType cb = NULL;
1319  const void * cbData = NULL;
1320  const char * longArg = NULL;
1321  int canstrip = 0;
1322  int shorty = 0;
1323 
1324  while (!con->os->nextCharArg && con->os->next == con->os->argc
1325  && con->os > con->optionStack) {
1326  cleanOSE(con->os--);
1327  }
1328  if (!con->os->nextCharArg && con->os->next == con->os->argc) {
1329  invokeCallbacksPOST(con, con->options);
1330 
1331  if (con->maincall) {
1332  /*@-noeffectuncon @*/
1333  (void) (*con->maincall) (con->finalArgvCount, con->finalArgv);
1334  /*@=noeffectuncon @*/
1335  return -1;
1336  }
1337 
1338  if (con->doExec) return execCommand(con);
1339  return -1;
1340  }
1341 
1342  /* Process next long option */
1343  if (!con->os->nextCharArg) {
1344  const char * optString;
1345  size_t optStringLen;
1346  int thisopt;
1347 
1348 /*@-sizeoftype@*/
1349  if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
1350  con->os->next++;
1351  continue;
1352  }
1353 /*@=sizeoftype@*/
1354  thisopt = con->os->next;
1355  if (con->os->argv != NULL) /* XXX can't happen */
1356  origOptString = con->os->argv[con->os->next++];
1357 
1358  if (origOptString == NULL) /* XXX can't happen */
1359  return POPT_ERROR_BADOPT;
1360 
1361  if (con->restLeftover || *origOptString != '-' ||
1362  (*origOptString == '-' && origOptString[1] == '\0'))
1363  {
1364  if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
1365  con->restLeftover = 1;
1366  if (con->flags & POPT_CONTEXT_ARG_OPTS) {
1367  con->os->nextArg = xstrdup(origOptString);
1368  return 0;
1369  }
1370  if (con->leftovers != NULL) /* XXX can't happen */
1371  con->leftovers[con->numLeftovers++] = origOptString;
1372  continue;
1373  }
1374 
1375  /* Make a copy we can hack at */
1376  optString = origOptString;
1377 
1378  if (optString[0] == '\0')
1379  return POPT_ERROR_BADOPT;
1380 
1381  if (optString[1] == '-' && !optString[2]) {
1382  con->restLeftover = 1;
1383  continue;
1384  } else {
1385  const char *oe;
1386  unsigned int argInfo = 0;
1387 
1388  optString++;
1389  if (*optString == '-')
1390  optString++;
1391  else
1393 
1394  /* Check for "--long=arg" option. */
1395  for (oe = optString; *oe && *oe != '='; oe++)
1396  {};
1397  optStringLen = (size_t)(oe - optString);
1398  if (*oe == '=')
1399  longArg = oe + 1;
1400 
1401  /* XXX aliases with arg substitution need "--alias=arg" */
1402  if (handleAlias(con, optString, optStringLen, '\0', longArg)) {
1403  longArg = NULL;
1404  continue;
1405  }
1406 
1407  if (handleExec(con, optString, '\0'))
1408  continue;
1409 
1410  opt = findOption(con->options, optString, optStringLen, '\0', &cb, &cbData,
1411  argInfo);
1412  if (!opt && !LF_ISSET(ONEDASH))
1413  return POPT_ERROR_BADOPT;
1414  }
1415 
1416  if (!opt) {
1417  con->os->nextCharArg = origOptString + 1;
1418  longArg = NULL;
1419  } else {
1420  if (con->os == con->optionStack && F_ISSET(opt, STRIP))
1421  {
1422  canstrip = 1;
1423  poptStripArg(con, thisopt);
1424  }
1425  shorty = 0;
1426  }
1427  }
1428 
1429  /* Process next short option */
1430  if (con->os->nextCharArg) {
1431  const char * nextCharArg = con->os->nextCharArg;
1432 
1433  con->os->nextCharArg = NULL;
1434 
1435  if (handleAlias(con, NULL, 0, *nextCharArg, nextCharArg + 1))
1436  continue;
1437 
1438  if (handleExec(con, NULL, *nextCharArg)) {
1439  /* Restore rest of short options for further processing */
1440  nextCharArg++;
1441  if (*nextCharArg != '\0')
1442  con->os->nextCharArg = nextCharArg;
1443  continue;
1444  }
1445 
1446  opt = findOption(con->options, NULL, 0, *nextCharArg, &cb,
1447  &cbData, 0);
1448  if (!opt)
1449  return POPT_ERROR_BADOPT;
1450  shorty = 1;
1451 
1452  nextCharArg++;
1453  if (*nextCharArg != '\0')
1454  con->os->nextCharArg = nextCharArg + (int)(*nextCharArg == '=');
1455  }
1456 
1457  if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */
1458  if (opt->arg && poptArgType(opt) == POPT_ARG_NONE) {
1459  unsigned int argInfo = poptArgInfo(con, opt);
1460  if (poptSaveInt((int *)opt->arg, argInfo, 1L))
1461  return POPT_ERROR_BADOPERATION;
1462  } else if (poptArgType(opt) == POPT_ARG_VAL) {
1463  if (opt->arg) {
1464  unsigned int argInfo = poptArgInfo(con, opt);
1465  if (poptSaveInt((int *)opt->arg, argInfo, (long)opt->val))
1466  return POPT_ERROR_BADOPERATION;
1467  }
1468  } else if (poptArgType(opt) != POPT_ARG_NONE) {
1469  int rc;
1470 
1471  con->os->nextArg = _free(con->os->nextArg);
1472  if (longArg) {
1473  longArg = expandNextArg(con, longArg);
1474  con->os->nextArg = (char *) longArg;
1475  } else if (con->os->nextCharArg) {
1476  longArg = expandNextArg(con, con->os->nextCharArg);
1477  con->os->nextArg = (char *) longArg;
1478  con->os->nextCharArg = NULL;
1479  } else {
1480  while (con->os->next == con->os->argc &&
1481  con->os > con->optionStack)
1482  {
1483  cleanOSE(con->os--);
1484  }
1485  if (con->os->next == con->os->argc) {
1486  if (!F_ISSET(opt, OPTIONAL))
1487  return POPT_ERROR_NOARG;
1488  con->os->nextArg = NULL;
1489  } else {
1490 
1491  /*
1492  * Make sure this isn't part of a short arg or the
1493  * result of an alias expansion.
1494  */
1495  if (con->os == con->optionStack
1496  && F_ISSET(opt, STRIP) && canstrip)
1497  {
1498  poptStripArg(con, con->os->next);
1499  }
1500 
1501  if (con->os->argv != NULL) { /* XXX can't happen */
1502  if (F_ISSET(opt, OPTIONAL) &&
1503  con->os->argv[con->os->next][0] == '-') {
1504  con->os->nextArg = NULL;
1505  } else {
1506  /* XXX watchout: subtle side-effects live here. */
1507  longArg = con->os->argv[con->os->next++];
1508  longArg = expandNextArg(con, longArg);
1509  con->os->nextArg = (char *) longArg;
1510  }
1511  }
1512  }
1513  }
1514  longArg = NULL;
1515 
1516  /* Save the option argument through a (*opt->arg) pointer. */
1517  if (opt->arg != NULL && (rc = poptSaveArg(con, opt)) != 0)
1518  return rc;
1519  }
1520 
1521  if (cb)
1522  invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
1523  else if (opt->val && (poptArgType(opt) != POPT_ARG_VAL))
1524  done = 1;
1525 
1526  if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
1527  con->finalArgvAlloced += 10;
1528  con->finalArgv = realloc(con->finalArgv,
1529  sizeof(*con->finalArgv) * con->finalArgvAlloced);
1530  if (con->finalArgv == NULL) con->finalArgvCount = con->finalArgvAlloced = 0;
1531  }
1532 
1533  if (con->finalArgv != NULL)
1534  { char *s = malloc((opt->longName ? strlen(opt->longName) : 0) + sizeof("--"));
1535  if (s != NULL) { /* XXX can't happen */
1536  con->finalArgv[con->finalArgvCount++] = s;
1537  *s++ = '-';
1538  if (opt->longName) {
1539  if (!F_ISSET(opt, ONEDASH))
1540  *s++ = '-';
1541  s = stpcpy(s, opt->longName);
1542  } else {
1543  *s++ = opt->shortName;
1544  *s = '\0';
1545  }
1546  } else
1547  con->finalArgv[con->finalArgvCount++] = NULL;
1548  }
1549 
1550  if (opt->arg && poptArgType(opt) == POPT_ARG_NONE)
1551  /*@-ifempty@*/ ; /*@=ifempty@*/
1552  else if (poptArgType(opt) == POPT_ARG_VAL)
1553  /*@-ifempty@*/ ; /*@=ifempty@*/
1554  else if (poptArgType(opt) != POPT_ARG_NONE) {
1555  if (con->finalArgv != NULL && con->os->nextArg != NULL)
1556  con->finalArgv[con->finalArgvCount++] =
1557  xstrdup(con->os->nextArg);
1558  }
1559  }
1560 
1561  return (opt ? opt->val : -1); /* XXX can't happen */
1562 }
1563 
1565 {
1566  char * ret = NULL;
1567  if (con) {
1568  ret = con->os->nextArg;
1569  con->os->nextArg = NULL;
1570  }
1571  return ret;
1572 }
1573 
1574 const char * poptGetArg(poptContext con)
1575 {
1576  const char * ret = NULL;
1577  if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1578  ret = con->leftovers[con->nextLeftover++];
1579  return ret;
1580 }
1581 
1582 const char * poptPeekArg(poptContext con)
1583 {
1584  const char * ret = NULL;
1585  if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1586  ret = con->leftovers[con->nextLeftover];
1587  return ret;
1588 }
1589 
1590 const char ** poptGetArgs(poptContext con)
1591 {
1592  if (con == NULL ||
1593  con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1594  return NULL;
1595 
1596  /* some apps like [like RPM ;-) ] need this NULL terminated */
1597  con->leftovers[con->numLeftovers] = NULL;
1598 
1599 /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */
1600  return (con->leftovers + con->nextLeftover);
1601 /*@=nullret =nullstate @*/
1602 }
1603 
1604 static /*@null@*/
1605 poptItem poptFreeItems(/*@only@*/ /*@null@*/ poptItem items, int nitems)
1606  /*@modifies items @*/
1607 {
1608  if (items != NULL) {
1609  poptItem item = items;
1610  while (--nitems >= 0) {
1611 /*@-modobserver -observertrans -dependenttrans@*/
1612  item->option.longName = _free(item->option.longName);
1613  item->option.descrip = _free(item->option.descrip);
1614  item->option.argDescrip = _free(item->option.argDescrip);
1615 /*@=modobserver =observertrans =dependenttrans@*/
1616  item->argv = _free(item->argv);
1617  item++;
1618  }
1619  items = _free(items);
1620  }
1621  return NULL;
1622 }
1623 
1625 {
1626  if (con == NULL) return con;
1627  poptResetContext(con);
1628  con->os->argb = _free(con->os->argb);
1629 
1630  con->aliases = poptFreeItems(con->aliases, con->numAliases);
1631  con->numAliases = 0;
1632 
1633  con->execs = poptFreeItems(con->execs, con->numExecs);
1634  con->numExecs = 0;
1635 
1636  con->leftovers = _free(con->leftovers);
1637  con->finalArgv = _free(con->finalArgv);
1638  con->appName = _free(con->appName);
1639  con->otherHelp = _free(con->otherHelp);
1640  con->execPath = _free(con->execPath);
1641  con->arg_strip = PBM_FREE(con->arg_strip);
1642 
1643  con = _free(con);
1644  return con;
1645 }
1646 
1647 int poptAddAlias(poptContext con, struct poptAlias alias,
1648  /*@unused@*/ UNUSED(int flags))
1649 {
1650  struct poptItem_s item_buf;
1651  poptItem item = &item_buf;
1652 
1653  if (con == NULL) return 1;
1654 
1655  memset(item, 0, sizeof(*item));
1656  item->option.longName = alias.longName;
1657  item->option.shortName = alias.shortName;
1658  item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
1659  item->option.arg = 0;
1660  item->option.val = 0;
1661  item->option.descrip = NULL;
1662  item->option.argDescrip = NULL;
1663  item->argc = alias.argc;
1664  item->argv = alias.argv;
1665  return poptAddItem(con, item, 0);
1666 }
1667 
1668 int poptAddItem(poptContext con, poptItem newItem, int flags)
1669 {
1670  poptItem * items, item;
1671  int * nitems;
1672 
1673  if (con == NULL) return 1;
1674 
1675  switch (flags) {
1676  case 1:
1677  items = &con->execs;
1678  nitems = &con->numExecs;
1679  break;
1680  case 0:
1681  items = &con->aliases;
1682  nitems = &con->numAliases;
1683  break;
1684  default:
1685  return 1;
1686  /*@notreached@*/ break;
1687  }
1688 
1689  *items = realloc((*items), ((*nitems) + 1) * sizeof(**items));
1690  if ((*items) == NULL)
1691  return 1;
1692 
1693  item = (*items) + (*nitems);
1694 
1695  item->option.longName =
1696  (newItem->option.longName ? xstrdup(newItem->option.longName) : NULL);
1697  item->option.shortName = newItem->option.shortName;
1698  item->option.argInfo = newItem->option.argInfo;
1699  item->option.arg = newItem->option.arg;
1700  item->option.val = newItem->option.val;
1701  item->option.descrip =
1702  (newItem->option.descrip ? xstrdup(newItem->option.descrip) : NULL);
1703  item->option.argDescrip =
1704  (newItem->option.argDescrip ? xstrdup(newItem->option.argDescrip) : NULL);
1705  item->argc = newItem->argc;
1706  item->argv = newItem->argv;
1707 
1708  (*nitems)++;
1709 
1710  return 0;
1711 }
1712 
1713 const char * poptBadOption(poptContext con, unsigned int flags)
1714 {
1715  struct optionStackEntry * os;
1716 
1717  if (!con)
1718  return NULL;
1719 
1720  os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
1721  if (!os)
1722  return NULL;
1723 
1724  if (con->doExec && con->doExec->argv && !os->nextCharArg && os->next == os->argc)
1725  return con->doExec->argv[0];
1726 
1727  return ((os->argv && (os->next > 0)) ? os->argv[os->next - 1] : NULL);
1728 }
1729 
1730 const char * poptStrerror(const int error)
1731 {
1732  switch (error) {
1733  case POPT_ERROR_NOARG:
1734  return POPT_("missing argument");
1735  case POPT_ERROR_BADOPT:
1736  return POPT_("unknown option");
1738  return POPT_("mutually exclusive logical operations requested");
1739  case POPT_ERROR_NULLARG:
1740  return POPT_("opt->arg should not be NULL");
1742  return POPT_("aliases nested too deeply");
1743  case POPT_ERROR_BADQUOTE:
1744  return POPT_("error in parameter quoting");
1745  case POPT_ERROR_BADNUMBER:
1746  return POPT_("invalid numeric value");
1747  case POPT_ERROR_OVERFLOW:
1748  return POPT_("number too large or too small");
1749  case POPT_ERROR_MALLOC:
1750  return POPT_("memory allocation failed");
1751  case POPT_ERROR_BADCONFIG:
1752  return POPT_("config file failed sanity test");
1753  case POPT_ERROR_ERRNO:
1754  return strerror(errno);
1755  case POPT_ERROR_NOCONTEXT:
1756  return POPT_("no context");
1757  default:
1758  return POPT_("unknown error");
1759  }
1760 }
1761 
1762 int poptStuffArgs(poptContext con, const char ** argv)
1763 {
1764  int argc;
1765  int rc;
1766 
1767  if (con == NULL)
1768  return POPT_ERROR_NOCONTEXT;
1769 
1770  if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1771  return POPT_ERROR_OPTSTOODEEP;
1772 
1773  for (argc = 0; argv[argc]; argc++)
1774  {};
1775 
1776  con->os++;
1777  con->os->next = 0;
1778  con->os->nextArg = NULL;
1779  con->os->nextCharArg = NULL;
1780  con->os->currAlias = NULL;
1781  rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
1782  con->os->argb = NULL;
1783  con->os->stuffed = 1;
1784 
1785  return rc;
1786 }
1787 
1789 {
1790  return ((con && con->os->argv) ? con->os->argv[0] : "");
1791 }
1792 
1793 int poptStrippedArgv(poptContext con, int argc, char ** argv)
1794 {
1795  int numargs = argc;
1796  int j = 1;
1797  int i;
1798 
1799 /*@-sizeoftype@*/
1800  if (con && con->arg_strip)
1801  for (i = 1; i < argc; i++) {
1802  if (PBM_ISSET(i, con->arg_strip))
1803  numargs--;
1804  }
1805 
1806  for (i = 1; i < argc; i++) {
1807  if (con && con->arg_strip && PBM_ISSET(i, con->arg_strip))
1808  continue;
1809  argv[j] = (j < numargs) ? argv[i] : NULL;
1810  j++;
1811  }
1812 /*@=sizeoftype@*/
1813 
1814  return numargs;
1815 }
poptArg_u::intp
int * intp
Definition: poptint.h:66
POPT_ARG_LONG
#define POPT_ARG_LONG
Definition: popt.h:23
poptArg_u::shortp
short * shortp
Definition: poptint.h:67
poptBitsArgs
int poptBitsArgs(poptContext con, poptBits *ap)
Definition: popt.c:907
POPT_ARGFLAG_NOT
#define POPT_ARGFLAG_NOT
Definition: popt.h:62
poptArg_u::doublep
double * doublep
Definition: poptint.h:71
poptBits_s
Definition: popt.h:686
poptGetOptArg
char * poptGetOptArg(poptContext con)
Definition: popt.c:1564
POPT_OPTION_DEPTH
#define POPT_OPTION_DEPTH
Definition: popt.h:14
seed
static unsigned int seed
Definition: popt.c:986
poptSubstituteHelpI18N
#define poptSubstituteHelpI18N(opt)
Definition: poptint.h:94
poptAlias
Definition: popt.h:144
POPT_ARG_INCLUDE_TABLE
#define POPT_ARG_INCLUDE_TABLE
Definition: popt.h:24
stpcpy
static char * stpcpy(char *dest, const char *src)
Definition: system.h:69
PBM_ISSET
#define PBM_ISSET(d, s)
Definition: poptint.h:45
__pbm_bits
unsigned int __pbm_bits
Definition: poptint.h:29
POPT_ARG_FLOAT
#define POPT_ARG_FLOAT
Definition: popt.h:34
POPT_ERROR_OPTSTOODEEP
#define POPT_ERROR_OPTSTOODEEP
Definition: popt.h:95
POPT_ARGFLAG_XOR
#define POPT_ARGFLAG_XOR
Definition: popt.h:61
POPT_CALLBACK_REASON_PRE
@ POPT_CALLBACK_REASON_PRE
Definition: popt.h:217
poptAlias::longName
const char * longName
Definition: popt.h:146
poptOption::argDescrip
const char * argDescrip
Definition: popt.h:138
optionStackEntry::nextCharArg
const char * nextCharArg
Definition: poptint.h:109
POPT_ARG_MASK
#define POPT_ARG_MASK
Definition: popt.h:43
__PBM_BITS
#define __PBM_BITS(set)
Definition: poptint.h:39
poptBitsUnion
int poptBitsUnion(poptBits *ap, const poptBits b)
Definition: popt.c:887
findProgramPath
static const char * findProgramPath(const char *argv0)
Return absolute path to executable by searching PATH.
Definition: popt.c:438
_free
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: poptint.h:20
findNextArg
static const char * findNextArg(poptContext con, unsigned argx, int delete_arg)
Definition: popt.c:654
_POPT_BITS_N
#define _POPT_BITS_N
Definition: popt.h:691
PBM_ALLOC
#define PBM_ALLOC(d)
Definition: poptint.h:41
poptSaveInt
int poptSaveInt(int *arg, unsigned int argInfo, long aLong)
Save an integer, performing logical operation with value.
Definition: popt.c:1066
POPT_CONTEXT_POSIXMEHARDER
#define POPT_CONTEXT_POSIXMEHARDER
Definition: popt.h:121
_poptBitsNew
static int _poptBitsNew(poptBits *bitsp)
Definition: popt.c:773
POPT_ARG_MAINCALL
#define POPT_ARG_MAINCALL
Definition: popt.h:38
__PBM_IX
#define __PBM_IX(d)
Definition: poptint.h:32
poptFreeContext
poptContext poptFreeContext(poptContext con)
Definition: popt.c:1624
POPT_ARG_VAL
#define POPT_ARG_VAL
Definition: popt.h:33
poptParseInteger
static int poptParseInteger(long long *llp, unsigned int argInfo, const char *val)
Parse an integer expression.
Definition: popt.c:1171
poptContext_s
Definition: poptint.h:115
POPT_ERROR_NULLARG
#define POPT_ERROR_NULLARG
Definition: popt.h:101
poptArg_u::ptr
void * ptr
Definition: poptint.h:65
poptResetContext
void poptResetContext(poptContext con)
Definition: popt.c:226
poptArgType
#define poptArgType(_opt)
Definition: poptint.h:86
LLONG_MAX
#define LLONG_MAX
poptContext_s::nextLeftover
int nextLeftover
Definition: poptint.h:122
optionStackEntry::nextArg
char * nextArg
Definition: poptint.h:107
optionStackEntry::stuffed
int stuffed
Definition: poptint.h:112
POPT_ARG_BITSET
#define POPT_ARG_BITSET
Definition: popt.h:41
poptFreeItems
static poptItem poptFreeItems(poptItem items, int nitems)
Definition: popt.c:1605
LLONG_MIN
#define LLONG_MIN
POPT_ERROR_BADOPT
#define POPT_ERROR_BADOPT
Definition: popt.h:94
poptBitsClr
int poptBitsClr(poptBits bits)
Definition: popt.c:837
poptCallbackType
void(* poptCallbackType)(poptContext con, enum poptCallbackReason reason, const struct poptOption *opt, const char *arg, const void *data)
Definition: popt.h:236
poptAddAlias
int poptAddAlias(poptContext con, struct poptAlias alias, int flags)
Definition: popt.c:1647
invokeCallbacksOPTION
static void invokeCallbacksOPTION(poptContext con, const struct poptOption *opt, const struct poptOption *myOpt, const void *myData, int shorty)
Definition: popt.c:123
expandNextArg
static const char * expandNextArg(poptContext con, const char *s)
Definition: popt.c:694
poptContext_s::flags
unsigned int flags
Definition: poptint.h:131
poptStrippedArgv
int poptStrippedArgv(poptContext con, int argc, char **argv)
Definition: popt.c:1793
optionStackEntry::argb
pbm_set * argb
Definition: poptint.h:104
POPT_CONTEXT_ARG_OPTS
#define POPT_CONTEXT_ARG_OPTS
Definition: popt.h:122
poptBitsIntersect
int poptBitsIntersect(poptBits *ap, const poptBits b)
Definition: popt.c:867
poptArg_u::cb
poptCallbackType cb
Definition: poptint.h:73
POPT_ERROR_ERRNO
#define POPT_ERROR_ERRNO
Definition: popt.h:97
optionStackEntry::next
int next
Definition: poptint.h:105
findOption
static const struct poptOption * findOption(const struct poptOption *opt, const char *longName, size_t longNameLen, char shortName, poptCallbackType *callback, const void **callbackData, unsigned int argInfo)
Definition: popt.c:585
poptDupArgv
int poptDupArgv(int argc, const char **argv, int *argcPtr, const char ***argvPtr)
Definition: poptparse.c:13
POPT_ARGFLAG_ONEDASH
#define POPT_ARGFLAG_ONEDASH
Definition: popt.h:52
POPT_CALLBACK_REASON_OPTION
@ POPT_CALLBACK_REASON_OPTION
Definition: popt.h:219
poptArgInfo
static unsigned int poptArgInfo(poptContext con, const struct poptOption *opt)
Return argInfo field, handling POPT_ARGFLAG_TOGGLE overrides.
Definition: popt.c:1141
POPT_ERROR_NOARG
#define POPT_ERROR_NOARG
Definition: popt.h:93
poptOption::argInfo
unsigned int argInfo
Definition: popt.h:131
xrealloc
void * xrealloc(void *ptr, size_t size)
POPT_ARGFLAG_DOC_HIDDEN
#define POPT_ARGFLAG_DOC_HIDDEN
Definition: popt.h:53
poptStuffArgs
int poptStuffArgs(poptContext con, const char **argv)
Definition: popt.c:1762
_poptBitsM
unsigned int _poptBitsM
Definition: popt.c:768
PBM_FREE
#define PBM_FREE(s)
Definition: poptint.h:42
poptAlias::argv
const char ** argv
Definition: popt.h:150
poptContext_s::aliases
poptItem aliases
Definition: poptint.h:129
POPT_ERROR_OVERFLOW
#define POPT_ERROR_OVERFLOW
Definition: popt.h:99
poptArg_u::floatp
float * floatp
Definition: poptint.h:70
poptArg_u::argv
const char ** argv
Definition: poptint.h:72
CBF_ISSET
#define CBF_ISSET(_opt, _FLAG)
Definition: poptint.h:91
poptOption::longName
const char * longName
Definition: popt.h:129
POPT_CALLBACK_REASON_POST
@ POPT_CALLBACK_REASON_POST
Definition: popt.h:218
poptint.h
poptContext_s::numExecs
int numExecs
Definition: poptint.h:134
poptContext_s::appName
const char * appName
Definition: poptint.h:127
poptContext_s::arg_strip
pbm_set * arg_strip
Definition: poptint.h:149
_poptBitsN
unsigned int _poptBitsN
Definition: popt.c:766
poptArg_u::opt
poptOption opt
Definition: poptint.h:75
POPT_ARG_CALLBACK
#define POPT_ARG_CALLBACK
Definition: popt.h:25
poptSaveLongLong
int poptSaveLongLong(long long *arg, unsigned int argInfo, long long aLongLong)
Save a long long, performing logical operation with value.
Definition: popt.c:988
optionStackEntry::argc
int argc
Definition: poptint.h:100
poptArg_u::longlongp
long long * longlongp
Definition: poptint.h:69
poptOption::arg
void * arg
Definition: popt.h:133
F_ISSET
#define F_ISSET(_opt, _FLAG)
Definition: poptint.h:89
POPT_CONTEXT_NO_EXEC
#define POPT_CONTEXT_NO_EXEC
Definition: popt.h:119
POPT_ARG_LONGLONG
#define POPT_ARG_LONGLONG
Definition: popt.h:36
POPT_ARGFLAG_OR
#define POPT_ARGFLAG_OR
Definition: popt.h:57
strerror
static char * strerror(int errno)
Definition: popt.c:39
_poptBitsK
unsigned int _poptBitsK
Definition: popt.c:770
POPT_ERROR_NOCONTEXT
#define POPT_ERROR_NOCONTEXT
Definition: popt.h:103
poptItem_s::argc
int argc
Definition: popt.h:159
POPT_BADOPTION_NOALIAS
#define POPT_BADOPTION_NOALIAS
Definition: popt.h:112
poptBitsDel
int poptBitsDel(poptBits bits, const char *s)
Definition: popt.c:848
poptContext_s::maincall
int(* maincall)(int argc, const char **argv)
Definition: poptint.h:140
poptContext_s::finalArgvAlloced
int finalArgvAlloced
Definition: poptint.h:138
poptBitsAdd
int poptBitsAdd(poptBits bits, const char *s)
Definition: popt.c:795
poptContext_s::os
struct optionStackEntry * os
Definition: poptint.h:118
invokeCallbacksPRE
static void invokeCallbacksPRE(poptContext con, const struct poptOption *opt)
Definition: popt.c:75
poptAlias::argc
int argc
Definition: popt.h:148
poptSaveString
int poptSaveString(const char ***argvp, unsigned int argInfo, const char *val)
Add a string to an argv array.
Definition: popt.c:963
optionStackEntry::currAlias
poptItem currAlias
Definition: poptint.h:111
handleAlias
static int handleAlias(poptContext con, const char *longName, size_t longNameLen, char shortName, const char *nextArg)
Definition: popt.c:359
UNUSED
#define UNUSED(x)
Definition: system.h:105
_poptArgMask
unsigned int _poptArgMask
Definition: popt.c:34
poptGetArgs
const char ** poptGetArgs(poptContext con)
Definition: popt.c:1590
POPT_ARG_DOUBLE
#define POPT_ARG_DOUBLE
Definition: popt.h:35
POPT_GROUP_MASK
#define POPT_GROUP_MASK
Definition: popt.h:44
poptContext_s::restLeftover
int restLeftover
Definition: poptint.h:125
poptContext_s::numAliases
int numAliases
Definition: poptint.h:130
POPT_ARG_STRING
#define POPT_ARG_STRING
Definition: popt.h:21
poptGetArg
const char * poptGetArg(poptContext con)
Definition: popt.c:1574
poptOption::shortName
char shortName
Definition: popt.h:130
execCommand
static int execCommand(poptContext con)
Definition: popt.c:482
poptArg_u
Definition: poptint.h:63
POPT_ERROR_BADCONFIG
#define POPT_ERROR_BADCONFIG
Definition: popt.h:104
__PBM_NBITS
#define __PBM_NBITS
Definition: poptint.h:31
_POPT_BITS_K
#define _POPT_BITS_K
Definition: popt.h:693
system.h
POPT_ERROR_BADOPERATION
#define POPT_ERROR_BADOPERATION
Definition: popt.h:100
poptAddItem
int poptAddItem(poptContext con, poptItem newItem, int flags)
Definition: popt.c:1668
optionStackEntry::argv
poptArgv argv
Definition: poptint.h:102
poptSaveBits
int poptSaveBits(poptBits *bitsp, unsigned int argInfo, const char *s)
Save a string into a bit set (experimental).
Definition: popt.c:928
poptSaveLong
int poptSaveLong(long *arg, unsigned int argInfo, long aLong)
Save a long, performing logical operation with value.
Definition: popt.c:1033
cleanOSE
static void cleanOSE(struct optionStackEntry *os)
Definition: popt.c:216
POPT_ARG_SHORT
#define POPT_ARG_SHORT
Definition: popt.h:40
POPT_CONTEXT_KEEP_FIRST
#define POPT_CONTEXT_KEEP_FIRST
Definition: popt.h:120
poptContext_s::execPath
const char * execPath
Definition: poptint.h:144
poptSaveArg
static int poptSaveArg(poptContext con, const struct poptOption *opt)
Save the option argument through the (*opt->arg) pointer.
Definition: popt.c:1195
poptOption::val
int val
Definition: popt.h:134
poptArgv
poptString * poptArgv
Definition: poptint.h:56
poptSetExecPath
void poptSetExecPath(poptContext con, const char *path, int allowAbsolute)
Definition: popt.c:66
poptJlu32lpair
void poptJlu32lpair(const void *key, size_t size, uint32_t *pc, uint32_t *pb)
POPT_ERROR_BADNUMBER
#define POPT_ERROR_BADNUMBER
Definition: popt.h:98
poptContext_s::optionStack
struct optionStackEntry optionStack[POPT_OPTION_DEPTH]
Definition: poptint.h:116
invokeCallbacksPOST
static void invokeCallbacksPOST(poptContext con, const struct poptOption *opt)
Definition: popt.c:99
poptSaveShort
int poptSaveShort(short *arg, unsigned int argInfo, long aLong)
Save a short integer, performing logical operation with value.
Definition: popt.c:1099
POPT_ERROR_MALLOC
#define POPT_ERROR_MALLOC
Definition: popt.h:102
POPT_ARGFLAG_AND
#define POPT_ARGFLAG_AND
Definition: popt.h:59
poptContext_s::finalArgvCount
int finalArgvCount
Definition: poptint.h:137
POPT_ABS
#define POPT_ABS(a)
poptItem_s
Definition: popt.h:157
POPT_ARG_NONE
#define POPT_ARG_NONE
Definition: popt.h:20
poptContext_s::leftovers
poptArgv leftovers
Definition: poptint.h:120
poptOption::descrip
const char * descrip
Definition: popt.h:136
poptContext_s::finalArgv
poptArgv finalArgv
Definition: poptint.h:136
LF_ISSET
#define LF_ISSET(_FLAG)
Definition: poptint.h:90
poptItem_s::argv
const char ** argv
Definition: popt.h:161
poptContext_s::execs
poptItem execs
Definition: poptint.h:133
poptGetContext
poptContext poptGetContext(const char *name, int argc, const char **argv, const struct poptOption *options, unsigned int flags)
Definition: popt.c:169
poptGetInvocationName
const char * poptGetInvocationName(poptContext con)
Definition: popt.c:1788
poptContext_s::options
const struct poptOption * options
Definition: poptint.h:124
DBL_EPSILON
#define DBL_EPSILON
poptContext_s::doExec
poptItem doExec
Definition: poptint.h:142
POPT_ARG_INT
#define POPT_ARG_INT
Definition: popt.h:22
poptContext_s::otherHelp
const char * otherHelp
Definition: poptint.h:147
poptStrerror
const char * poptStrerror(const int error)
Definition: popt.c:1730
handleExec
static int handleExec(poptContext con, const char *longName, char shortName)
Definition: popt.c:260
_POPT_BITS_M
#define _POPT_BITS_M
Definition: popt.h:692
poptPeekArg
const char * poptPeekArg(poptContext con)
Definition: popt.c:1582
poptArg_u::longp
long * longp
Definition: poptint.h:68
poptGetNextOpt
int poptGetNextOpt(poptContext con)
Definition: popt.c:1309
poptBadOption
const char * poptBadOption(poptContext con, unsigned int flags)
Definition: popt.c:1713
poptItem_s::option
struct poptOption option
Definition: popt.h:158
poptContext_s::execAbsolute
int execAbsolute
Definition: poptint.h:145
xstrdup
char * xstrdup(const char *str)
poptBitsChk
int poptBitsChk(poptBits bits, const char *s)
Definition: popt.c:814
POPT_ARG_ARGV
#define POPT_ARG_ARGV
Definition: popt.h:39
poptOption
Definition: popt.h:127
poptStripArg
static void poptStripArg(poptContext con, int which)
Definition: popt.c:751
POPT_ERROR_BADQUOTE
#define POPT_ERROR_BADQUOTE
Definition: popt.h:96
poptAlias::shortName
char shortName
Definition: popt.h:147
poptContext_s::numLeftovers
int numLeftovers
Definition: poptint.h:121
POPT_
#define POPT_(foo)
Definition: poptint.h:218
PBM_CLR
#define PBM_CLR(d, s)
Definition: poptint.h:44
optionStackEntry
Definition: poptint.h:99
longOptionStrcmp
static int longOptionStrcmp(const struct poptOption *opt, const char *longName, size_t longNameLen)
Compare long option for equality, adjusting for POPT_ARGFLAG_TOGGLE.
Definition: popt.c:327
_poptGroupMask
unsigned int _poptGroupMask
Definition: popt.c:36
PBM_SET
#define PBM_SET(d, s)
Definition: poptint.h:43

Generated for popt by  doxygen 1.8.17