popt  1.13
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 #ifdef HAVE_FLOAT_H
14 #include <float.h>
15 #endif
16 #include <math.h>
17 
18 #include "findme.h"
19 #include "poptint.h"
20 
21 #ifdef MYDEBUG
22 /*@unchecked@*/
23 int _popt_debug = 0;
24 #endif
25 
26 #if !defined(HAVE_STRERROR) && !defined(__LCLINT__)
27 static char * strerror(int errno)
28 {
29  extern int sys_nerr;
30  extern char * sys_errlist[];
31 
32  if ((0 <= errno) && (errno < sys_nerr))
33  return sys_errlist[errno];
34  else
35  return POPT_("unknown errno");
36 }
37 #endif
38 
39 #ifdef MYDEBUG
40 /*@unused@*/
41 static void prtcon(const char *msg, poptContext con)
42 {
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] : ""));
51 }
52 #endif
53 
54 void poptSetExecPath(poptContext con, const char * path, int allowAbsolute)
55 {
56  con->execPath = _free(con->execPath);
57  con->execPath = xstrdup(path);
58  con->execAbsolute = allowAbsolute;
59 /*@-nullstate@*/ /* LCL: con->execPath not NULL */
60  return;
61 /*@=nullstate@*/
62 }
63 
64 static void invokeCallbacksPRE(poptContext con, const struct poptOption * opt)
65  /*@globals internalState@*/
66  /*@modifies internalState@*/
67 {
68  if (opt != NULL)
69  for (; opt->longName || opt->shortName || opt->arg; opt++) {
70  if (opt->arg == NULL) continue; /* XXX program error. */
72  void * arg = opt->arg;
73  /* XXX sick hack to preserve pretense of ABI. */
74  if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
75  /* Recurse on included sub-tables. */
76  invokeCallbacksPRE(con, arg);
77  } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
78  (opt->argInfo & POPT_CBFLAG_PRE))
79  { /*@-castfcnptr@*/
81  /*@=castfcnptr@*/
82  /* Perform callback. */
83  /*@-noeffectuncon @*/
84  cb(con, POPT_CALLBACK_REASON_PRE, NULL, NULL, opt->descrip);
85  /*@=noeffectuncon @*/
86  }
87  }
88 }
89 
90 static void invokeCallbacksPOST(poptContext con, const struct poptOption * opt)
91  /*@globals internalState@*/
92  /*@modifies internalState@*/
93 {
94  if (opt != NULL)
95  for (; opt->longName || opt->shortName || opt->arg; opt++) {
96  if (opt->arg == NULL) continue; /* XXX program error. */
98  void * arg = opt->arg;
99  /* XXX sick hack to preserve pretense of ABI. */
100  if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
101  /* Recurse on included sub-tables. */
102  invokeCallbacksPOST(con, arg);
103  } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
104  (opt->argInfo & POPT_CBFLAG_POST))
105  { /*@-castfcnptr@*/
107  /*@=castfcnptr@*/
108  /* Perform callback. */
109  /*@-noeffectuncon @*/
110  cb(con, POPT_CALLBACK_REASON_POST, NULL, NULL, opt->descrip);
111  /*@=noeffectuncon @*/
112  }
113  }
114 }
115 
117  const struct poptOption * opt,
118  const struct poptOption * myOpt,
119  /*@null@*/ const void * myData, int shorty)
120  /*@globals internalState@*/
121  /*@modifies internalState@*/
122 {
123  const struct poptOption * cbopt = NULL;
124 
125  if (opt != NULL)
126  for (; opt->longName || opt->shortName || opt->arg; opt++) {
127  if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
128  void * arg = opt->arg;
129  /* XXX sick hack to preserve pretense of ABI. */
130  if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
131  /* Recurse on included sub-tables. */
132  if (opt->arg != NULL) /* XXX program error */
133  invokeCallbacksOPTION(con, opt->arg, myOpt, myData, shorty);
134  } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK &&
135  !(opt->argInfo & POPT_CBFLAG_SKIPOPTION)) {
136  /* Save callback info. */
137  cbopt = opt;
138  } else if (cbopt != NULL &&
139  ((myOpt->shortName && opt->shortName && shorty &&
140  myOpt->shortName == opt->shortName) ||
141  (myOpt->longName != NULL && opt->longName != NULL &&
142  !strcmp(myOpt->longName, opt->longName)))
143  )
144  { /*@-castfcnptr@*/
146  /*@=castfcnptr@*/
147  const void * cbData = (cbopt->descrip ? cbopt->descrip : myData);
148  /* Perform callback. */
149  if (cb != NULL) { /* XXX program error */
150  /*@-noeffectuncon @*/
151  cb(con, POPT_CALLBACK_REASON_OPTION, myOpt,
152  con->os->nextArg, cbData);
153  /*@=noeffectuncon @*/
154  }
155  /* Terminate (unless explcitly continuing). */
156  if (!(cbopt->argInfo & POPT_CBFLAG_CONTINUE))
157  return;
158  }
159  }
160 }
161 
162 poptContext poptGetContext(const char * name, int argc, const char ** argv,
163  const struct poptOption * options, unsigned int flags)
164 {
165  poptContext con = malloc(sizeof(*con));
166 
167  if (con == NULL) return NULL; /* XXX can't happen */
168  memset(con, 0, sizeof(*con));
169 
170  con->os = con->optionStack;
171  con->os->argc = argc;
172 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
173  con->os->argv = argv;
174 /*@=dependenttrans =assignexpose@*/
175  con->os->argb = NULL;
176 
177  if (!(flags & POPT_CONTEXT_KEEP_FIRST))
178  con->os->next = 1; /* skip argv[0] */
179 
180  con->leftovers = calloc( (argc + 1), sizeof(*con->leftovers) );
181 /*@-dependenttrans -assignexpose@*/ /* FIX: W2DO? */
182  con->options = options;
183 /*@=dependenttrans =assignexpose@*/
184  con->aliases = NULL;
185  con->numAliases = 0;
186  con->flags = flags;
187  con->execs = NULL;
188  con->numExecs = 0;
189  con->execFail = NULL;
190  con->finalArgvAlloced = argc * 2;
191  con->finalArgv = calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );
192  con->execAbsolute = 1;
193  con->arg_strip = NULL;
194 
195  if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
197 
198  if (name) {
199  char * t = malloc(strlen(name) + 1);
200  if (t) con->appName = strcpy(t, name);
201  }
202 
203 /*@-internalglobs@*/
204  invokeCallbacksPRE(con, con->options);
205 /*@=internalglobs@*/
206 
207  return con;
208 }
209 
210 static void cleanOSE(/*@special@*/ struct optionStackEntry *os)
211  /*@uses os @*/
212  /*@releases os->nextArg, os->argv, os->argb @*/
213  /*@modifies os @*/
214 {
215  os->nextArg = _free(os->nextArg);
216  os->argv = _free(os->argv);
217  os->argb = PBM_FREE(os->argb);
218 }
219 
221 {
222  int i;
223 
224  if (con == NULL) return;
225  while (con->os > con->optionStack) {
226  cleanOSE(con->os--);
227  }
228  con->os->argb = PBM_FREE(con->os->argb);
229  con->os->currAlias = NULL;
230  con->os->nextCharArg = NULL;
231  con->os->nextArg = NULL;
232  con->os->next = 1; /* skip argv[0] */
233 
234  con->numLeftovers = 0;
235  con->nextLeftover = 0;
236  con->restLeftover = 0;
237  con->doExec = NULL;
238  con->execFail = _free(con->execFail);
239 
240  if (con->finalArgv != NULL)
241  for (i = 0; i < con->finalArgvCount; i++) {
242 /*@-unqualifiedtrans@*/ /* FIX: typedef double indirection. */
243  con->finalArgv[i] = _free(con->finalArgv[i]);
244 /*@=unqualifiedtrans@*/
245  }
246 
247  con->finalArgvCount = 0;
248  con->arg_strip = PBM_FREE(con->arg_strip);
249 /*@-nullstate@*/ /* FIX: con->finalArgv != NULL */
250  return;
251 /*@=nullstate@*/
252 }
253 
254 /* Only one of longName, shortName should be set, not both. */
255 static int handleExec(/*@special@*/ poptContext con,
256  /*@null@*/ const char * longName, char shortName)
257  /*@uses con->execs, con->numExecs, con->flags, con->doExec,
258  con->finalArgv, con->finalArgvAlloced, con->finalArgvCount @*/
259  /*@modifies con @*/
260 {
261  poptItem item;
262  int i;
263 
264  if (con->execs == NULL || con->numExecs <= 0) /* XXX can't happen */
265  return 0;
266 
267  for (i = con->numExecs - 1; i >= 0; i--) {
268  item = con->execs + i;
269  if (longName && !(item->option.longName &&
270  !strcmp(longName, item->option.longName)))
271  continue;
272  else if (shortName != item->option.shortName)
273  continue;
274  break;
275  }
276  if (i < 0) return 0;
277 
278 
279  if (con->flags & POPT_CONTEXT_NO_EXEC)
280  return 1;
281 
282  if (con->doExec == NULL) {
283  con->doExec = con->execs + i;
284  return 1;
285  }
286 
287  /* We already have an exec to do; remember this option for next
288  time 'round */
289  if ((con->finalArgvCount + 1) >= (con->finalArgvAlloced)) {
290  con->finalArgvAlloced += 10;
291  con->finalArgv = realloc(con->finalArgv,
292  sizeof(*con->finalArgv) * con->finalArgvAlloced);
293  }
294 
295  i = con->finalArgvCount++;
296  if (con->finalArgv != NULL) /* XXX can't happen */
297  { char *s = malloc((longName ? strlen(longName) : 0) + 3);
298  if (s != NULL) { /* XXX can't happen */
299  if (longName)
300  sprintf(s, "--%s", longName);
301  else
302  sprintf(s, "-%c", shortName);
303  con->finalArgv[i] = s;
304  } else
305  con->finalArgv[i] = NULL;
306  }
307 
308  return 1;
309 }
310 
311 /* Only one of longName, shortName may be set at a time */
312 static int handleAlias(/*@special@*/ poptContext con,
313  /*@null@*/ const char * longName, size_t longNameLen,
314  char shortName,
315  /*@exposed@*/ /*@null@*/ const char * nextArg)
316  /*@uses con->aliases, con->numAliases, con->optionStack, con->os,
317  con->os->currAlias, con->os->currAlias->option.longName @*/
318  /*@modifies con @*/
319 {
320  poptItem item = con->os->currAlias;
321  int rc;
322  int i;
323 
324  if (item) {
325  if (longName && item->option.longName
326  && longNameLen == strlen(item->option.longName)
327  && !strncmp(longName, item->option.longName, longNameLen))
328  return 0;
329  else
330  if (shortName && shortName == item->option.shortName)
331  return 0;
332  }
333 
334  if (con->aliases == NULL || con->numAliases <= 0) /* XXX can't happen */
335  return 0;
336 
337  for (i = con->numAliases - 1; i >= 0; i--) {
338  item = con->aliases + i;
339  if (longName) {
340  if (item->option.longName == NULL)
341  continue;
342  if (longNameLen != strlen(item->option.longName))
343  continue;
344  if (strncmp(longName, item->option.longName, longNameLen))
345  continue;
346  } else if (shortName != item->option.shortName)
347  continue;
348  break;
349  }
350  if (i < 0) return 0;
351 
352  if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
353  return POPT_ERROR_OPTSTOODEEP;
354 
355  if (longName == NULL && nextArg && *nextArg)
356  con->os->nextCharArg = nextArg;
357 
358  con->os++;
359  con->os->next = 0;
360  con->os->stuffed = 0;
361  con->os->nextArg = NULL;
362  con->os->nextCharArg = NULL;
363  con->os->currAlias = con->aliases + i;
364  { const char ** av;
365  int ac = con->os->currAlias->argc;
366  /* Append --foo=bar arg to alias argv array (if present). */
367  if (longName && nextArg && *nextArg) {
368  int i;
369  av = alloca((ac + 1 + 1) * sizeof(*av));
370  for (i = 0; i < ac; i++)
371  av[i] = con->os->currAlias->argv[i];
372  av[ac++] = nextArg;
373  av[ac] = NULL;
374  } else
375  av = con->os->currAlias->argv;
376  rc = poptDupArgv(ac, av, &con->os->argc, &con->os->argv);
377  }
378  con->os->argb = NULL;
379 
380  return (rc ? rc : 1);
381 }
382 
383 static int execCommand(poptContext con)
384  /*@globals internalState @*/
385  /*@modifies internalState @*/
386 {
387  poptItem item = con->doExec;
388  const char ** argv = NULL;
389  int argc = 0;
390  int rc;
391  int ec = POPT_ERROR_ERRNO;
392 
393  if (item == NULL) /*XXX can't happen*/
394  return POPT_ERROR_NOARG;
395 
396  if (item->argv == NULL || item->argc < 1 ||
397  (!con->execAbsolute && strchr(item->argv[0], '/')))
398  return POPT_ERROR_NOARG;
399 
400  argv = malloc(sizeof(*argv) *
401  (6 + item->argc + con->numLeftovers + con->finalArgvCount));
402  if (argv == NULL) return POPT_ERROR_MALLOC;
403 
404  if (!strchr(item->argv[0], '/') && con->execPath != NULL) {
405  char *s = malloc(strlen(con->execPath) + strlen(item->argv[0]) + sizeof("/"));
406  if (s)
407  sprintf(s, "%s/%s", con->execPath, item->argv[0]);
408  argv[argc] = s;
409  } else
410  argv[argc] = POPT_findProgramPath(item->argv[0]);
411  if (argv[argc++] == NULL) {
412  ec = POPT_ERROR_NOARG;
413  goto exit;
414  }
415 
416  if (item->argc > 1) {
417  memcpy(argv + argc, item->argv + 1, sizeof(*argv) * (item->argc - 1));
418  argc += (item->argc - 1);
419  }
420 
421  if (con->finalArgv != NULL && con->finalArgvCount > 0) {
422  memcpy(argv + argc, con->finalArgv,
423  sizeof(*argv) * con->finalArgvCount);
424  argc += con->finalArgvCount;
425  }
426 
427  if (con->leftovers != NULL && con->numLeftovers > 0) {
428  memcpy(argv + argc, con->leftovers, sizeof(*argv) * con->numLeftovers);
429  argc += con->numLeftovers;
430  }
431 
432  argv[argc] = NULL;
433 
434 #if defined(hpux) || defined(__hpux)
435  rc = setresgid(getgid(), getgid(),-1);
436  if (rc) goto exit;
437  rc = setresuid(getuid(), getuid(),-1);
438  if (rc) goto exit;
439 #else
440 /*
441  * XXX " ... on BSD systems setuid() should be preferred over setreuid()"
442  * XXX sez' Timur Bakeyev <mc@bat.ru>
443  * XXX from Norbert Warmuth <nwarmuth@privat.circular.de>
444  */
445 #if defined(HAVE_SETUID)
446  rc = setgid(getgid());
447  if (rc) goto exit;
448  rc = setuid(getuid());
449  if (rc) goto exit;
450 #elif defined (HAVE_SETREUID)
451  rc = setregid(getgid(), getgid());
452  if (rc) goto exit;
453  rc = setreuid(getuid(), getuid());
454  if (rc) goto exit;
455 #else
456  ; /* Can't drop privileges */
457 #endif
458 #endif
459 
460 #ifdef MYDEBUG
461 if (_popt_debug)
462  { const char ** avp;
463  fprintf(stderr, "==> execvp(%s) argv[%d]:", argv[0], argc);
464  for (avp = argv; *avp; avp++)
465  fprintf(stderr, " '%s'", *avp);
466  fprintf(stderr, "\n");
467  }
468 #endif
469 
470 /*@-nullstate@*/
471  rc = execvp(argv[0], (char *const *)argv);
472 /*@=nullstate@*/
473  con->execFail = xstrdup(argv[0]);
474 
475 exit:
476  if (argv) {
477  if (argv[0])
478  free((void *)argv[0]);
479  free(argv);
480  }
481  return ec;
482 }
483 
484 /*@observer@*/ /*@null@*/ static const struct poptOption *
485 findOption(const struct poptOption * opt, /*@null@*/ const char * longName, int longNameLen,
486  char shortName,
487  /*@null@*/ /*@out@*/ poptCallbackType * callback,
488  /*@null@*/ /*@out@*/ const void ** callbackData,
489  int singleDash)
490  /*@modifies *callback, *callbackData */
491 {
492  const struct poptOption * cb = NULL;
493 
494  /* This happens when a single - is given */
495  if (singleDash && !shortName && (longName && *longName == '\0'))
496  shortName = '-';
497 
498  for (; opt->longName || opt->shortName || opt->arg; opt++) {
499 
500  if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_INCLUDE_TABLE) {
501  const struct poptOption * opt2;
502  void * arg = opt->arg;
503 
504  /* XXX sick hack to preserve pretense of ABI. */
505  if (arg == poptHelpOptions) arg = poptHelpOptionsI18N;
506  /* Recurse on included sub-tables. */
507  if (arg == NULL) continue; /* XXX program error */
508  opt2 = findOption(arg, longName, longNameLen, shortName, callback,
509  callbackData, singleDash);
510  if (opt2 == NULL) continue;
511  /* Sub-table data will be inheirited if no data yet. */
512  if (!(callback && *callback)) return opt2;
513  if (!(callbackData && *callbackData == NULL)) return opt2;
514 /*@-observertrans -dependenttrans @*/
515  *callbackData = opt->descrip;
516 /*@=observertrans =dependenttrans @*/
517  return opt2;
518  } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_CALLBACK) {
519  cb = opt;
520  } else if (longName != NULL && opt->longName != NULL &&
521  (!singleDash || (opt->argInfo & POPT_ARGFLAG_ONEDASH)) &&
522  (!strncmp(longName, opt->longName, longNameLen) && strlen(opt->longName) == longNameLen))
523  {
524  break;
525  } else if (shortName && shortName == opt->shortName) {
526  break;
527  }
528  }
529 
530  if (opt->longName == NULL && !opt->shortName)
531  return NULL;
532 
533 /*@-modobserver -mods @*/
534  if (callback) *callback = NULL;
535  if (callbackData) *callbackData = NULL;
536  if (cb) {
537  if (callback)
538  /*@-castfcnptr@*/
539  *callback = (poptCallbackType)cb->arg;
540  /*@=castfcnptr@*/
541  if (!(cb->argInfo & POPT_CBFLAG_INC_DATA)) {
542  if (callbackData)
543 /*@-observertrans@*/ /* FIX: typedef double indirection. */
544  *callbackData = cb->descrip;
545 /*@=observertrans@*/
546  }
547  }
548 /*@=modobserver =mods @*/
549 
550  return opt;
551 }
552 
553 static const char * findNextArg(/*@special@*/ poptContext con,
554  unsigned argx, int delete_arg)
555  /*@uses con->optionStack, con->os,
556  con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
557  /*@modifies con @*/
558 {
559  struct optionStackEntry * os = con->os;
560  const char * arg;
561 
562  do {
563  int i;
564  arg = NULL;
565  while (os->next == os->argc && os > con->optionStack) os--;
566  if (os->next == os->argc && os == con->optionStack) break;
567  if (os->argv != NULL)
568  for (i = os->next; i < os->argc; i++) {
569 /*@-sizeoftype@*/
570  if (os->argb && PBM_ISSET(i, os->argb))
571  /*@innercontinue@*/ continue;
572  if (*os->argv[i] == '-')
573  /*@innercontinue@*/ continue;
574  if (--argx > 0)
575  /*@innercontinue@*/ continue;
576  arg = os->argv[i];
577  if (delete_arg) {
578  if (os->argb == NULL) os->argb = PBM_ALLOC(os->argc);
579  if (os->argb != NULL) /* XXX can't happen */
580  PBM_SET(i, os->argb);
581  }
582  /*@innerbreak@*/ break;
583 /*@=sizeoftype@*/
584  }
585  if (os > con->optionStack) os--;
586  } while (arg == NULL);
587  return arg;
588 }
589 
590 static /*@only@*/ /*@null@*/ const char *
591 expandNextArg(/*@special@*/ poptContext con, const char * s)
592  /*@uses con->optionStack, con->os,
593  con->os->next, con->os->argb, con->os->argc, con->os->argv @*/
594  /*@modifies con @*/
595 {
596  const char * a = NULL;
597  size_t alen;
598  char *t, *te;
599  size_t tn = strlen(s) + 1;
600  char c;
601 
602  te = t = malloc(tn);;
603  if (t == NULL) return NULL; /* XXX can't happen */
604  while ((c = *s++) != '\0') {
605  switch (c) {
606 #if 0 /* XXX can't do this */
607  case '\\': /* escape */
608  c = *s++;
609  /*@switchbreak@*/ break;
610 #endif
611  case '!':
612  if (!(s[0] == '#' && s[1] == ':' && s[2] == '+'))
613  /*@switchbreak@*/ break;
614  /* XXX Make sure that findNextArg deletes only next arg. */
615  if (a == NULL) {
616  if ((a = findNextArg(con, 1U, 1)) == NULL)
617  /*@switchbreak@*/ break;
618  }
619  s += 3;
620 
621  alen = strlen(a);
622  tn += alen;
623  *te = '\0';
624  t = realloc(t, tn);
625  te = t + strlen(t);
626  strncpy(te, a, alen); te += alen;
627  continue;
628  /*@notreached@*/ /*@switchbreak@*/ break;
629  default:
630  /*@switchbreak@*/ break;
631  }
632  *te++ = c;
633  }
634  *te = '\0';
635  t = realloc(t, strlen(t) + 1); /* XXX memory leak, hard to plug */
636  return t;
637 }
638 
639 static void poptStripArg(/*@special@*/ poptContext con, int which)
640  /*@uses con->arg_strip, con->optionStack @*/
641  /*@defines con->arg_strip @*/
642  /*@modifies con @*/
643 {
644 /*@-sizeoftype@*/
645  if (con->arg_strip == NULL)
646  con->arg_strip = PBM_ALLOC(con->optionStack[0].argc);
647  if (con->arg_strip != NULL) /* XXX can't happen */
648  PBM_SET(which, con->arg_strip);
649 /*@=sizeoftype@*/
650 /*@-compdef@*/ /* LCL: con->arg_strip undefined? */
651  return;
652 /*@=compdef@*/
653 }
654 
655 /*@unchecked@*/
656 static unsigned int seed = 0;
657 
658 /*@-bitwisesigned@*/ /* LCL: logical ops with unsigned. */
659 int poptSaveLong(long * arg, unsigned int argInfo, long aLong)
660 {
661  /* XXX Check alignment, may fail on funky platforms. */
662  if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
663  return POPT_ERROR_NULLARG;
664 
665  if (aLong != 0 && argInfo & POPT_ARGFLAG_RANDOM) {
666  if (!seed) {
667  srandom((unsigned)getpid());
668  srandom((unsigned)random());
669  }
670  aLong = random() % (aLong > 0 ? aLong : -aLong);
671  aLong++;
672  }
673  if (argInfo & POPT_ARGFLAG_NOT)
674  aLong = ~aLong;
675  switch (argInfo & POPT_ARGFLAG_LOGICALOPS) {
676  case 0:
677  *arg = aLong;
678  break;
679  case POPT_ARGFLAG_OR:
680  *arg |= aLong;
681  break;
682  case POPT_ARGFLAG_AND:
683  *arg &= aLong;
684  break;
685  case POPT_ARGFLAG_XOR:
686  *arg ^= aLong;
687  break;
688  default:
690  /*@notreached@*/ break;
691  }
692  return 0;
693 }
694 /*@=bitwisesigned@*/
695 
696 /*@-bitwisesigned@*/ /* LCL: logical ops with unsigned. */
697 int poptSaveInt(/*@null@*/ int * arg, unsigned int argInfo, long aLong)
698 {
699  /* XXX Check alignment, may fail on funky platforms. */
700  if (arg == NULL || (((unsigned long)arg) & (sizeof(*arg)-1)))
701  return POPT_ERROR_NULLARG;
702 
703  if (aLong != 0 && argInfo & POPT_ARGFLAG_RANDOM) {
704  if (!seed) {
705  srandom((unsigned)getpid());
706  srandom((unsigned)random());
707  }
708  aLong = random() % (aLong > 0 ? aLong : -aLong);
709  aLong++;
710  }
711  if (argInfo & POPT_ARGFLAG_NOT)
712  aLong = ~aLong;
713  switch (argInfo & POPT_ARGFLAG_LOGICALOPS) {
714  case 0:
715  *arg = aLong;
716  break;
717  case POPT_ARGFLAG_OR:
718  *arg |= aLong;
719  break;
720  case POPT_ARGFLAG_AND:
721  *arg &= aLong;
722  break;
723  case POPT_ARGFLAG_XOR:
724  *arg ^= aLong;
725  break;
726  default:
728  /*@notreached@*/ break;
729  }
730  return 0;
731 }
732 /*@=bitwisesigned@*/
733 
734 /* returns 'val' element, -1 on last item, POPT_ERROR_* on error */
736 {
737  const struct poptOption * opt = NULL;
738  int done = 0;
739 
740  if (con == NULL)
741  return -1;
742  while (!done) {
743  const char * origOptString = NULL;
744  poptCallbackType cb = NULL;
745  const void * cbData = NULL;
746  const char * longArg = NULL;
747  int canstrip = 0;
748  int shorty = 0;
749 
750  while (!con->os->nextCharArg && con->os->next == con->os->argc
751  && con->os > con->optionStack) {
752  cleanOSE(con->os--);
753  }
754  if (!con->os->nextCharArg && con->os->next == con->os->argc) {
755  invokeCallbacksPOST(con, con->options);
756 
757  if (con->maincall) {
758  /*@-noeffectuncon @*/
759  (void) (*con->maincall) (con->finalArgvCount, con->finalArgv);
760  /*@=noeffectuncon @*/
761  return -1;
762  }
763 
764  if (con->doExec) return execCommand(con);
765  return -1;
766  }
767 
768  /* Process next long option */
769  if (!con->os->nextCharArg) {
770  const char * optString;
771  int optStringLen;
772  int thisopt;
773 
774 /*@-sizeoftype@*/
775  if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
776  con->os->next++;
777  continue;
778  }
779 /*@=sizeoftype@*/
780  thisopt = con->os->next;
781  if (con->os->argv != NULL) /* XXX can't happen */
782  origOptString = con->os->argv[con->os->next++];
783 
784  if (origOptString == NULL) /* XXX can't happen */
785  return POPT_ERROR_BADOPT;
786 
787  if (con->restLeftover || *origOptString != '-' ||
788  (*origOptString == '-' && origOptString[1] == '\0'))
789  {
791  con->restLeftover = 1;
792  if (con->flags & POPT_CONTEXT_ARG_OPTS) {
793  con->os->nextArg = xstrdup(origOptString);
794  return 0;
795  }
796  if (con->leftovers != NULL) /* XXX can't happen */
797  con->leftovers[con->numLeftovers++] = origOptString;
798  continue;
799  }
800 
801  /* Make a copy we can hack at */
802  optString = origOptString;
803 
804  if (optString[0] == '\0')
805  return POPT_ERROR_BADOPT;
806 
807  if (optString[1] == '-' && !optString[2]) {
808  con->restLeftover = 1;
809  continue;
810  } else {
811  const char *oe;
812  int singleDash;
813 
814  optString++;
815  if (*optString == '-')
816  singleDash = 0, optString++;
817  else
818  singleDash = 1;
819 
820  /* Check for "--long=arg" option. */
821  for (oe = optString; *oe && *oe != '='; oe++)
822  {};
823  optStringLen = oe - optString;
824  if (*oe == '=')
825  longArg = oe + 1;
826 
827  /* XXX aliases with arg substitution need "--alias=arg" */
828  if (handleAlias(con, optString, optStringLen, '\0', longArg)) {
829  longArg = NULL;
830  continue;
831  }
832 
833  if (handleExec(con, optString, '\0'))
834  continue;
835 
836  opt = findOption(con->options, optString, optStringLen, '\0', &cb, &cbData,
837  singleDash);
838  if (!opt && !singleDash)
839  return POPT_ERROR_BADOPT;
840  }
841 
842  if (!opt) {
843  con->os->nextCharArg = origOptString + 1;
844  } else {
845  if (con->os == con->optionStack &&
847  {
848  canstrip = 1;
849  poptStripArg(con, thisopt);
850  }
851  shorty = 0;
852  }
853  }
854 
855  /* Process next short option */
856  if (con->os->nextCharArg) {
857  origOptString = con->os->nextCharArg;
858 
859  con->os->nextCharArg = NULL;
860 
861  if (handleAlias(con, NULL, 0, *origOptString, origOptString + 1))
862  continue;
863 
864  if (handleExec(con, NULL, *origOptString)) {
865  /* Restore rest of short options for further processing */
866  origOptString++;
867  if (*origOptString != '\0')
868  con->os->nextCharArg = origOptString;
869  continue;
870  }
871 
872  opt = findOption(con->options, NULL, 0, *origOptString, &cb,
873  &cbData, 0);
874  if (!opt)
875  return POPT_ERROR_BADOPT;
876  shorty = 1;
877 
878  origOptString++;
879  if (*origOptString != '\0')
880  con->os->nextCharArg = origOptString;
881  }
882 
883  if (opt == NULL) return POPT_ERROR_BADOPT; /* XXX can't happen */
884  if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) {
885  if (poptSaveInt((int *)opt->arg, opt->argInfo, 1L))
887  } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) {
888  if (opt->arg) {
889  if (poptSaveInt((int *)opt->arg, opt->argInfo, (long)opt->val))
891  }
892  } else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
893  con->os->nextArg = _free(con->os->nextArg);
894  if (longArg) {
895  longArg = expandNextArg(con, longArg);
896  con->os->nextArg = (char *) longArg;
897  } else if (con->os->nextCharArg) {
898  longArg = expandNextArg(con, con->os->nextCharArg);
899  con->os->nextArg = (char *) longArg;
900  con->os->nextCharArg = NULL;
901  } else {
902  while (con->os->next == con->os->argc &&
903  con->os > con->optionStack) {
904  cleanOSE(con->os--);
905  }
906  if (con->os->next == con->os->argc) {
907  if (!(opt->argInfo & POPT_ARGFLAG_OPTIONAL))
908  return POPT_ERROR_NOARG;
909  con->os->nextArg = NULL;
910  } else {
911 
912  /*
913  * Make sure this isn't part of a short arg or the
914  * result of an alias expansion.
915  */
916  if (con->os == con->optionStack &&
917  (opt->argInfo & POPT_ARGFLAG_STRIP) &&
918  canstrip) {
919  poptStripArg(con, con->os->next);
920  }
921 
922  if (con->os->argv != NULL) { /* XXX can't happen */
923  if (opt->argInfo & POPT_ARGFLAG_OPTIONAL &&
924  con->os->argv[con->os->next][0] == '-') {
925  con->os->nextArg = NULL;
926  } else {
927  /* XXX watchout: subtle side-effects live here. */
928  longArg = con->os->argv[con->os->next++];
929  longArg = expandNextArg(con, longArg);
930  con->os->nextArg = (char *) longArg;
931  }
932  }
933  }
934  }
935  longArg = NULL;
936 
937  if (opt->arg) {
938  switch (opt->argInfo & POPT_ARG_MASK) {
939  case POPT_ARG_STRING:
940  /* XXX memory leak, hard to plug */
941  *((const char **) opt->arg) = (con->os->nextArg)
942  ? xstrdup(con->os->nextArg) : NULL;
943  /*@switchbreak@*/ break;
944 
945  case POPT_ARG_INT:
946  case POPT_ARG_LONG:
947  { long aLong = 0;
948  char *end;
949 
950  if (con->os->nextArg) {
951  aLong = strtol(con->os->nextArg, &end, 0);
952  if (!(end && *end == '\0'))
953  return POPT_ERROR_BADNUMBER;
954  }
955 
956  if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) {
957  if (aLong == LONG_MIN || aLong == LONG_MAX)
958  return POPT_ERROR_OVERFLOW;
959  if (poptSaveLong((long *)opt->arg, opt->argInfo, aLong))
961  } else {
962  if (aLong > INT_MAX || aLong < INT_MIN)
963  return POPT_ERROR_OVERFLOW;
964  if (poptSaveInt((int *)opt->arg, opt->argInfo, aLong))
966  }
967  } /*@switchbreak@*/ break;
968 
969  case POPT_ARG_FLOAT:
970  case POPT_ARG_DOUBLE:
971  { double aDouble = 0.0;
972  char *end;
973 
974  if (con->os->nextArg) {
975 /*@-mods@*/
976  int saveerrno = errno;
977  errno = 0;
978  aDouble = strtod(con->os->nextArg, &end);
979  if (errno == ERANGE)
980  return POPT_ERROR_OVERFLOW;
981  errno = saveerrno;
982 /*@=mods@*/
983  if (*end != '\0')
984  return POPT_ERROR_BADNUMBER;
985  }
986 
987  if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_DOUBLE) {
988  *((double *) opt->arg) = aDouble;
989  } else {
990 #define _ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a))
991  if ((_ABS(aDouble) - FLT_MAX) > DBL_EPSILON)
992  return POPT_ERROR_OVERFLOW;
993  if ((FLT_MIN - _ABS(aDouble)) > DBL_EPSILON)
994  return POPT_ERROR_OVERFLOW;
995  *((float *) opt->arg) = aDouble;
996  }
997  } /*@switchbreak@*/ break;
998  case POPT_ARG_MAINCALL:
999 /*@-type@*/
1000  con->maincall = opt->arg;
1001 /*@=type@*/
1002  /*@switchbreak@*/ break;
1003  default:
1004  fprintf(stdout,
1005  POPT_("option type (%d) not implemented in popt\n"),
1006  (opt->argInfo & POPT_ARG_MASK));
1007  exit(EXIT_FAILURE);
1008  /*@notreached@*/ /*@switchbreak@*/ break;
1009  }
1010  }
1011  }
1012 
1013  if (cb)
1014  invokeCallbacksOPTION(con, con->options, opt, cbData, shorty);
1015  else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
1016  done = 1;
1017 
1018  if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
1019  con->finalArgvAlloced += 10;
1020  con->finalArgv = realloc(con->finalArgv,
1021  sizeof(*con->finalArgv) * con->finalArgvAlloced);
1022  }
1023 
1024  if (con->finalArgv != NULL)
1025  { char *s = malloc((opt->longName ? strlen(opt->longName) : 0) + 3);
1026  if (s != NULL) { /* XXX can't happen */
1027  if (opt->longName)
1028  sprintf(s, "%s%s",
1029  ((opt->argInfo & POPT_ARGFLAG_ONEDASH) ? "-" : "--"),
1030  opt->longName);
1031  else
1032  sprintf(s, "-%c", opt->shortName);
1033  con->finalArgv[con->finalArgvCount++] = s;
1034  } else
1035  con->finalArgv[con->finalArgvCount++] = NULL;
1036  }
1037 
1038  if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE)
1039  /*@-ifempty@*/ ; /*@=ifempty@*/
1040  else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL)
1041  /*@-ifempty@*/ ; /*@=ifempty@*/
1042  else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
1043  if (con->finalArgv != NULL && con->os->nextArg != NULL)
1044  con->finalArgv[con->finalArgvCount++] =
1045  xstrdup(con->os->nextArg);
1046  }
1047  }
1048 
1049  return (opt ? opt->val : -1); /* XXX can't happen */
1050 }
1051 
1053 {
1054  char * ret = NULL;
1055  if (con) {
1056  ret = con->os->nextArg;
1057  con->os->nextArg = NULL;
1058  }
1059  return ret;
1060 }
1061 
1062 const char * poptGetArg(poptContext con)
1063 {
1064  const char * ret = NULL;
1065  if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1066  ret = con->leftovers[con->nextLeftover++];
1067  return ret;
1068 }
1069 
1070 const char * poptPeekArg(poptContext con)
1071 {
1072  const char * ret = NULL;
1073  if (con && con->leftovers != NULL && con->nextLeftover < con->numLeftovers)
1074  ret = con->leftovers[con->nextLeftover];
1075  return ret;
1076 }
1077 
1078 const char ** poptGetArgs(poptContext con)
1079 {
1080  if (con == NULL ||
1081  con->leftovers == NULL || con->numLeftovers == con->nextLeftover)
1082  return NULL;
1083 
1084  /* some apps like [like RPM ;-) ] need this NULL terminated */
1085  con->leftovers[con->numLeftovers] = NULL;
1086 
1087 /*@-nullret -nullstate @*/ /* FIX: typedef double indirection. */
1088  return (con->leftovers + con->nextLeftover);
1089 /*@=nullret =nullstate @*/
1090 }
1091 
1093 {
1094  poptItem item;
1095  int i;
1096 
1097  if (con == NULL) return con;
1098  poptResetContext(con);
1099  con->os->argb = _free(con->os->argb);
1100 
1101  if (con->aliases != NULL)
1102  for (i = 0; i < con->numAliases; i++) {
1103  item = con->aliases + i;
1104 /*@-modobserver -observertrans -dependenttrans@*/
1105  item->option.longName = _free(item->option.longName);
1106  item->option.descrip = _free(item->option.descrip);
1107  item->option.argDescrip = _free(item->option.argDescrip);
1108 /*@=modobserver =observertrans =dependenttrans@*/
1109  item->argv = _free(item->argv);
1110  }
1111  con->aliases = _free(con->aliases);
1112 
1113  if (con->execs != NULL)
1114  for (i = 0; i < con->numExecs; i++) {
1115  item = con->execs + i;
1116 /*@-modobserver -observertrans -dependenttrans@*/
1117  item->option.longName = _free(item->option.longName);
1118  item->option.descrip = _free(item->option.descrip);
1119  item->option.argDescrip = _free(item->option.argDescrip);
1120 /*@=modobserver =observertrans =dependenttrans@*/
1121  item->argv = _free(item->argv);
1122  }
1123  con->execs = _free(con->execs);
1124 
1125  con->leftovers = _free(con->leftovers);
1126  con->finalArgv = _free(con->finalArgv);
1127  con->appName = _free(con->appName);
1128  con->otherHelp = _free(con->otherHelp);
1129  con->execPath = _free(con->execPath);
1130  con->arg_strip = PBM_FREE(con->arg_strip);
1131 
1132  con = _free(con);
1133  return con;
1134 }
1135 
1136 int poptAddAlias(poptContext con, struct poptAlias alias,
1137  /*@unused@*/ int flags)
1138 {
1139  struct poptItem_s item_buf;
1140  poptItem item = &item_buf;
1141  memset(item, 0, sizeof(*item));
1142  item->option.longName = alias.longName;
1143  item->option.shortName = alias.shortName;
1144  item->option.argInfo = POPT_ARGFLAG_DOC_HIDDEN;
1145  item->option.arg = 0;
1146  item->option.val = 0;
1147  item->option.descrip = NULL;
1148  item->option.argDescrip = NULL;
1149  item->argc = alias.argc;
1150  item->argv = alias.argv;
1151  return poptAddItem(con, item, 0);
1152 }
1153 
1154 int poptAddItem(poptContext con, poptItem newItem, int flags)
1155 {
1156  poptItem * items, item;
1157  int * nitems;
1158 
1159  switch (flags) {
1160  case 1:
1161  items = &con->execs;
1162  nitems = &con->numExecs;
1163  break;
1164  case 0:
1165  items = &con->aliases;
1166  nitems = &con->numAliases;
1167  break;
1168  default:
1169  return 1;
1170  /*@notreached@*/ break;
1171  }
1172 
1173  *items = realloc((*items), ((*nitems) + 1) * sizeof(**items));
1174  if ((*items) == NULL)
1175  return 1;
1176 
1177  item = (*items) + (*nitems);
1178 
1179  item->option.longName =
1180  (newItem->option.longName ? xstrdup(newItem->option.longName) : NULL);
1181  item->option.shortName = newItem->option.shortName;
1182  item->option.argInfo = newItem->option.argInfo;
1183  item->option.arg = newItem->option.arg;
1184  item->option.val = newItem->option.val;
1185  item->option.descrip =
1186  (newItem->option.descrip ? xstrdup(newItem->option.descrip) : NULL);
1187  item->option.argDescrip =
1188  (newItem->option.argDescrip ? xstrdup(newItem->option.argDescrip) : NULL);
1189  item->argc = newItem->argc;
1190  item->argv = newItem->argv;
1191 
1192  (*nitems)++;
1193 
1194  return 0;
1195 }
1196 
1197 const char * poptBadOption(poptContext con, unsigned int flags)
1198 {
1199  struct optionStackEntry * os = NULL;
1200  const char *badOpt = NULL;
1201 
1202  if (con != NULL) {
1203  /* Stupid hack to return something semi-meaningful from exec failure */
1204  if (con->execFail) {
1205  badOpt = con->execFail;
1206  } else {
1207  os = (flags & POPT_BADOPTION_NOALIAS) ? con->optionStack : con->os;
1208  badOpt = os->argv[os->next - 1];
1209  }
1210  }
1211 
1212  return badOpt;
1213 }
1214 
1215 const char * poptStrerror(const int error)
1216 {
1217  switch (error) {
1218  case POPT_ERROR_NOARG:
1219  return POPT_("missing argument");
1220  case POPT_ERROR_BADOPT:
1221  return POPT_("unknown option");
1223  return POPT_("mutually exclusive logical operations requested");
1224  case POPT_ERROR_NULLARG:
1225  return POPT_("opt->arg should not be NULL");
1227  return POPT_("aliases nested too deeply");
1228  case POPT_ERROR_BADQUOTE:
1229  return POPT_("error in parameter quoting");
1230  case POPT_ERROR_BADNUMBER:
1231  return POPT_("invalid numeric value");
1232  case POPT_ERROR_OVERFLOW:
1233  return POPT_("number too large or too small");
1234  case POPT_ERROR_MALLOC:
1235  return POPT_("memory allocation failed");
1236  case POPT_ERROR_ERRNO:
1237  return strerror(errno);
1238  default:
1239  return POPT_("unknown error");
1240  }
1241 }
1242 
1243 int poptStuffArgs(poptContext con, const char ** argv)
1244 {
1245  int argc;
1246  int rc;
1247 
1248  if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
1249  return POPT_ERROR_OPTSTOODEEP;
1250 
1251  for (argc = 0; argv[argc]; argc++)
1252  {};
1253 
1254  con->os++;
1255  con->os->next = 0;
1256  con->os->nextArg = NULL;
1257  con->os->nextCharArg = NULL;
1258  con->os->currAlias = NULL;
1259  rc = poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
1260  con->os->argb = NULL;
1261  con->os->stuffed = 1;
1262 
1263  return rc;
1264 }
1265 
1267 {
1268  return (con->os->argv ? con->os->argv[0] : "");
1269 }
1270 
1271 int poptStrippedArgv(poptContext con, int argc, char ** argv)
1272 {
1273  int numargs = argc;
1274  int j = 1;
1275  int i;
1276 
1277 /*@-sizeoftype@*/
1278  if (con->arg_strip)
1279  for (i = 1; i < argc; i++) {
1280  if (PBM_ISSET(i, con->arg_strip))
1281  numargs--;
1282  }
1283 
1284  for (i = 1; i < argc; i++) {
1285  if (con->arg_strip && PBM_ISSET(i, con->arg_strip))
1286  continue;
1287  argv[j] = (j < numargs) ? argv[i] : NULL;
1288  j++;
1289  }
1290 /*@=sizeoftype@*/
1291 
1292  return numargs;
1293 }

Generated for popt by  doxygen 1.8.1.2