Mixe for Privacy and Anonymity in the Internet
Classes | Macros | Typedefs | Enumerations | Functions | Variables
popt.h File Reference
#include <stdio.h>
Include dependency graph for popt.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  poptOption
 
struct  poptAlias
 

Macros

#define POPT_OPTION_DEPTH   10
 
#define POPT_ARG_NONE   0
 
#define POPT_ARG_STRING   1
 
#define POPT_ARG_INT   2
 
#define POPT_ARG_LONG   3
 
#define POPT_ARG_INCLUDE_TABLE   4 /* arg points to table */
 
#define POPT_ARG_CALLBACK
 
#define POPT_ARG_INTL_DOMAIN
 
#define POPT_ARG_VAL   7 /* arg should take value val */
 
#define POPT_ARG_MASK   0x0000FFFF
 
#define POPT_ARGFLAG_ONEDASH   0x80000000 /* allow -longoption */
 
#define POPT_ARGFLAG_DOC_HIDDEN   0x40000000 /* don't show in help/usage */
 
#define POPT_ARGFLAG_STRIP   0x20000000 /* strip this arg from argv (only applies to long args) */
 
#define POPT_CBFLAG_PRE   0x80000000 /* call the callback before parse */
 
#define POPT_CBFLAG_POST   0x40000000 /* call the callback after parse */
 
#define POPT_CBFLAG_INC_DATA
 
#define POPT_ERROR_NOARG   -10
 
#define POPT_ERROR_BADOPT   -11
 
#define POPT_ERROR_OPTSTOODEEP   -13
 
#define POPT_ERROR_BADQUOTE   -15 /* only from poptParseArgString() */
 
#define POPT_ERROR_ERRNO   -16 /* only from poptParseArgString() */
 
#define POPT_ERROR_BADNUMBER   -17
 
#define POPT_ERROR_OVERFLOW   -18
 
#define POPT_BADOPTION_NOALIAS   (1 << 0) /* don't go into an alias */
 
#define POPT_CONTEXT_NO_EXEC   (1 << 0) /* ignore exec expansions */
 
#define POPT_CONTEXT_KEEP_FIRST   (1 << 1) /* pay attention to argv[0] */
 
#define POPT_CONTEXT_POSIXMEHARDER   (1 << 2) /* options can't follow args */
 
#define POPT_AUTOHELP
 

Typedefs

typedef struct poptContext_spoptContext
 
typedef struct poptOptionpoptOption
 
typedef void(* poptCallbackType) (poptContext con, enum poptCallbackReason reason, const struct poptOption *opt, const char *arg, const void *data)
 

Enumerations

enum  poptCallbackReason { POPT_CALLBACK_REASON_PRE , POPT_CALLBACK_REASON_POST , POPT_CALLBACK_REASON_OPTION }
 

Functions

poptContext poptGetContext (const char *name, int argc, const char **argv, const struct poptOption *options, int flags)
 
void poptResetContext (poptContext con)
 
int poptGetNextOpt (poptContext con)
 
const char * poptGetOptArg (poptContext con)
 
const char * poptGetArg (poptContext con)
 
const char * poptPeekArg (poptContext con)
 
const char ** poptGetArgs (poptContext con)
 
const char * poptBadOption (poptContext con, int flags)
 
void poptFreeContext (poptContext con)
 
int poptStuffArgs (poptContext con, const char **argv)
 
int poptAddAlias (poptContext con, struct poptAlias alias, int flags)
 
int poptReadConfigFile (poptContext con, const char *fn)
 
int poptReadDefaultConfig (poptContext con, int useEnv)
 
int poptDupArgv (int argc, const char **argv, int *argcPtr, const char ***argvPtr)
 
int poptParseArgvString (const char *s, int *argcPtr, const char ***argvPtr)
 
const char * poptStrerror (const int error)
 
void poptSetExecPath (poptContext con, const char *path, int allowAbsolute)
 
void poptPrintHelp (poptContext con, FILE *f, int flags)
 
void poptPrintUsage (poptContext con, FILE *f, int flags)
 
void poptSetOtherOptionHelp (poptContext con, const char *text)
 
const char * poptGetInvocationName (poptContext con)
 
int poptStrippedArgv (poptContext con, int argc, char **argv)
 

Variables

struct poptOption poptHelpOptions []
 

Macro Definition Documentation

◆ POPT_ARG_CALLBACK

#define POPT_ARG_CALLBACK
Value:
5 /* table-wide callback... must be
set first in table; arg points
to callback, descrip points to
callback data to pass */

Definition at line 21 of file popt.h.

◆ POPT_ARG_INCLUDE_TABLE

#define POPT_ARG_INCLUDE_TABLE   4 /* arg points to table */

Definition at line 20 of file popt.h.

◆ POPT_ARG_INT

#define POPT_ARG_INT   2

Definition at line 18 of file popt.h.

◆ POPT_ARG_INTL_DOMAIN

#define POPT_ARG_INTL_DOMAIN
Value:
6 /* set the translation domain
for this table and any
included tables; arg points
to the domain string */

Definition at line 22 of file popt.h.

◆ POPT_ARG_LONG

#define POPT_ARG_LONG   3

Definition at line 19 of file popt.h.

◆ POPT_ARG_MASK

#define POPT_ARG_MASK   0x0000FFFF

Definition at line 24 of file popt.h.

◆ POPT_ARG_NONE

#define POPT_ARG_NONE   0

Definition at line 16 of file popt.h.

◆ POPT_ARG_STRING

#define POPT_ARG_STRING   1

Definition at line 17 of file popt.h.

◆ POPT_ARG_VAL

#define POPT_ARG_VAL   7 /* arg should take value val */

Definition at line 23 of file popt.h.

◆ POPT_ARGFLAG_DOC_HIDDEN

#define POPT_ARGFLAG_DOC_HIDDEN   0x40000000 /* don't show in help/usage */

Definition at line 26 of file popt.h.

◆ POPT_ARGFLAG_ONEDASH

#define POPT_ARGFLAG_ONEDASH   0x80000000 /* allow -longoption */

Definition at line 25 of file popt.h.

◆ POPT_ARGFLAG_STRIP

#define POPT_ARGFLAG_STRIP   0x20000000 /* strip this arg from argv (only applies to long args) */

Definition at line 27 of file popt.h.

◆ POPT_AUTOHELP

#define POPT_AUTOHELP
Value:
0, "Help options", NULL },
struct poptOption poptHelpOptions[]
Definition: popthelp.cpp:21
#define POPT_ARG_INCLUDE_TABLE
Definition: popt.h:20

Definition at line 66 of file popt.h.

◆ POPT_BADOPTION_NOALIAS

#define POPT_BADOPTION_NOALIAS   (1 << 0) /* don't go into an alias */

Definition at line 41 of file popt.h.

◆ POPT_CBFLAG_INC_DATA

#define POPT_CBFLAG_INC_DATA
Value:
0x20000000 /* use data from the include line,
not the subtable */

Definition at line 30 of file popt.h.

◆ POPT_CBFLAG_POST

#define POPT_CBFLAG_POST   0x40000000 /* call the callback after parse */

Definition at line 29 of file popt.h.

◆ POPT_CBFLAG_PRE

#define POPT_CBFLAG_PRE   0x80000000 /* call the callback before parse */

Definition at line 28 of file popt.h.

◆ POPT_CONTEXT_KEEP_FIRST

#define POPT_CONTEXT_KEEP_FIRST   (1 << 1) /* pay attention to argv[0] */

Definition at line 45 of file popt.h.

◆ POPT_CONTEXT_NO_EXEC

#define POPT_CONTEXT_NO_EXEC   (1 << 0) /* ignore exec expansions */

Definition at line 44 of file popt.h.

◆ POPT_CONTEXT_POSIXMEHARDER

#define POPT_CONTEXT_POSIXMEHARDER   (1 << 2) /* options can't follow args */

Definition at line 46 of file popt.h.

◆ POPT_ERROR_BADNUMBER

#define POPT_ERROR_BADNUMBER   -17

Definition at line 37 of file popt.h.

◆ POPT_ERROR_BADOPT

#define POPT_ERROR_BADOPT   -11

Definition at line 33 of file popt.h.

◆ POPT_ERROR_BADQUOTE

#define POPT_ERROR_BADQUOTE   -15 /* only from poptParseArgString() */

Definition at line 35 of file popt.h.

◆ POPT_ERROR_ERRNO

#define POPT_ERROR_ERRNO   -16 /* only from poptParseArgString() */

Definition at line 36 of file popt.h.

◆ POPT_ERROR_NOARG

#define POPT_ERROR_NOARG   -10

Definition at line 32 of file popt.h.

◆ POPT_ERROR_OPTSTOODEEP

#define POPT_ERROR_OPTSTOODEEP   -13

Definition at line 34 of file popt.h.

◆ POPT_ERROR_OVERFLOW

#define POPT_ERROR_OVERFLOW   -18

Definition at line 38 of file popt.h.

◆ POPT_OPTION_DEPTH

#define POPT_OPTION_DEPTH   10

Definition at line 14 of file popt.h.

Typedef Documentation

◆ poptCallbackType

typedef void(* poptCallbackType) (poptContext con, enum poptCallbackReason reason, const struct poptOption *opt, const char *arg, const void *data)

Definition at line 77 of file popt.h.

◆ poptContext

typedef struct poptContext_s* poptContext

Definition at line 69 of file popt.h.

◆ poptOption

typedef struct poptOption* poptOption

Definition at line 71 of file popt.h.

Enumeration Type Documentation

◆ poptCallbackReason

Enumerator
POPT_CALLBACK_REASON_PRE 
POPT_CALLBACK_REASON_POST 
POPT_CALLBACK_REASON_OPTION 

Definition at line 74 of file popt.h.

@ POPT_CALLBACK_REASON_PRE
Definition: popt.h:74

Function Documentation

◆ poptAddAlias()

int poptAddAlias ( poptContext  con,
struct poptAlias  alias,
int  flags 
)

Definition at line 682 of file popt.cpp.

684 {
685  int aliasNum = con->numAliases++;
686  struct poptAlias * alias;
687 
688  /* SunOS won't realloc(NULL, ...) */
689  if (!con->aliases)
690  con->aliases = (struct poptAlias*)malloc(sizeof(newAlias)* con->numAliases);
691  else
692  con->aliases = (struct poptAlias*)realloc(con->aliases,
693  sizeof(newAlias)* con->numAliases);
694  alias = con->aliases + aliasNum;
695 
696  alias->longName = (newAlias.longName)
697  ? strcpy((char*)malloc(strlen(newAlias.longName) + 1), newAlias.longName)
698  : NULL;
699  alias->shortName = newAlias.shortName;
700  alias->argc = newAlias.argc;
701  alias->argv = newAlias.argv;
702 
703  return 0;
704 }
Definition: popt.h:58
int argc
Definition: popt.h:61
const char ** argv
Definition: popt.h:62
const char * longName
Definition: popt.h:59
char shortName
Definition: popt.h:60
struct poptAlias * aliases
Definition: poptint.h:50
int numAliases
Definition: poptint.h:51

References poptContext_s::aliases, poptAlias::argc, poptAlias::argv, poptAlias::longName, poptContext_s::numAliases, and poptAlias::shortName.

◆ poptBadOption()

const char* poptBadOption ( poptContext  con,
int  flags 
)

Definition at line 706 of file popt.cpp.

706  {
707  struct optionStackEntry * os;
708 
710  os = con->optionStack;
711  else
712  os = con->os;
713 
714  return os->argv[os->next - 1];
715 }
#define POPT_BADOPTION_NOALIAS
Definition: popt.h:41
Definition: poptint.h:24
int next
Definition: poptint.h:28
const char ** argv
Definition: poptint.h:26
struct optionStackEntry * os
Definition: poptint.h:43
struct optionStackEntry optionStack[POPT_OPTION_DEPTH]
Definition: poptint.h:42
UINT16 flags
Definition: typedefs.hpp:1

References optionStackEntry::argv, flags, optionStackEntry::next, poptContext_s::optionStack, poptContext_s::os, and POPT_BADOPTION_NOALIAS.

◆ poptDupArgv()

int poptDupArgv ( int  argc,
const char **  argv,
int *  argcPtr,
const char ***  argvPtr 
)

Definition at line 10 of file poptparse.cpp.

12 {
13  size_t nb = (argc + 1) * sizeof(*argv);
14  const char ** argv2;
15  char * dst;
16  int i;
17 
18  for (i = 0; i < argc; i++) {
19  if (argv[i] == NULL)
20  return POPT_ERROR_NOARG;
21  nb += strlen(argv[i]) + 1;
22  }
23 
24  dst = (char *)malloc(nb);
25  argv2 = (const char**) dst;
26  dst += (argc + 1) * sizeof(*argv);
27 
28  for (i = 0; i < argc; i++) {
29  argv2[i] = dst;
30  dst += strlen(strcpy(dst, argv[i])) + 1;
31  }
32  argv2[argc] = NULL;
33 
34  *argvPtr = argv2;
35  *argcPtr = argc;
36  return 0;
37 }
#define POPT_ERROR_NOARG
Definition: popt.cpp:717

References POPT_ERROR_NOARG.

Referenced by poptParseArgvString(), and poptStuffArgs().

Here is the caller graph for this function:

◆ poptFreeContext()

void poptFreeContext ( poptContext  con)

Definition at line 654 of file popt.cpp.

654  {
655  int i;
656 
657  poptResetContext(con);
658  if (con->os->argb) free(con->os->argb);
659 
660  for (i = 0; i < con->numAliases; i++) {
661  if (con->aliases[i].longName) xfree(con->aliases[i].longName);
662  free((void*)con->aliases[i].argv);
663  }
664 
665  for (i = 0; i < con->numExecs; i++) {
666  if (con->execs[i].longName) xfree(con->execs[i].longName);
667  xfree(con->execs[i].script);
668  }
669  if (con->execs) xfree(con->execs);
670 
671  free((void*)con->leftovers);
672  free((void*)con->finalArgv);
673  if (con->appName) xfree(con->appName);
674  if (con->aliases) free(con->aliases);
675  if (con->otherHelp) xfree(con->otherHelp);
676  if (con->execPath) xfree(con->execPath);
677  if (con->arg_strip) PBM_FREE(con->arg_strip);
678 
679  free(con);
680 }
void poptResetContext(poptContext con)
Definition: popt.cpp:104
#define xfree(_a)
Definition: poptint.h:65
#define PBM_FREE(s)
Definition: poptint.h:19
const char * script
Definition: poptint.h:38
const char * longName
Definition: poptint.h:36
pbm_set * argb
Definition: poptint.h:27
const char ** leftovers
Definition: poptint.h:44
const char * execPath
Definition: poptint.h:59
int numExecs
Definition: poptint.h:54
pbm_set * arg_strip
Definition: poptint.h:62
const char * otherHelp
Definition: poptint.h:61
const char * appName
Definition: poptint.h:49
struct execEntry * execs
Definition: poptint.h:53
const char ** finalArgv
Definition: poptint.h:55

References poptContext_s::aliases, poptContext_s::appName, poptContext_s::arg_strip, optionStackEntry::argb, poptAlias::argv, poptContext_s::execPath, poptContext_s::execs, poptContext_s::finalArgv, poptContext_s::leftovers, poptAlias::longName, execEntry::longName, poptContext_s::numAliases, poptContext_s::numExecs, poptContext_s::os, poptContext_s::otherHelp, PBM_FREE, poptResetContext(), execEntry::script, and xfree.

Referenced by CACmdLnOptions::parse().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ poptGetArg()

const char* poptGetArg ( poptContext  con)

Definition at line 635 of file popt.cpp.

635  {
636  if (con->numLeftovers == con->nextLeftover) return NULL;
637  return con->leftovers[con->nextLeftover++];
638 }
int nextLeftover
Definition: poptint.h:46
int numLeftovers
Definition: poptint.h:45

References poptContext_s::leftovers, poptContext_s::nextLeftover, and poptContext_s::numLeftovers.

◆ poptGetArgs()

const char** poptGetArgs ( poptContext  con)

Definition at line 645 of file popt.cpp.

645  {
646  if (con->numLeftovers == con->nextLeftover) return NULL;
647 
648  /* some apps like [like RPM ;-) ] need this NULL terminated */
649  con->leftovers[con->numLeftovers] = NULL;
650 
651  return (con->leftovers + con->nextLeftover);
652 }

References poptContext_s::leftovers, poptContext_s::nextLeftover, and poptContext_s::numLeftovers.

◆ poptGetContext()

poptContext poptGetContext ( const char *  name,
int  argc,
const char **  argv,
const struct poptOption options,
int  flags 
)

Definition at line 51 of file popt.cpp.

52  {
53  poptContext con = (poptContext)malloc(sizeof(*con));
54 
55  memset(con, 0, sizeof(*con));
56 
57  con->os = con->optionStack;
58  con->os->argc = argc;
59  con->os->argv = argv;
60  con->os->argb = NULL;
61 
63  con->os->next = 1; /* skip argv[0] */
64 
65  con->leftovers = (const char**)calloc((argc + 1), sizeof(char *));
66  con->options = options;
67  con->aliases = NULL;
68  con->numAliases = 0;
69  con->flags = flags;
70  con->execs = NULL;
71  con->numExecs = 0;
72  con->finalArgvAlloced = argc * 2;
73  con->finalArgv = (const char**)calloc(con->finalArgvAlloced, sizeof(*con->finalArgv));
74  con->execAbsolute = 1;
75  con->arg_strip = NULL;
76 
77  if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
79 
80  if (name)
81  con->appName = strcpy((char*)malloc(strlen(name) + 1), name);
82 
83  invokeCallbacks(con, con->options, 0);
84 
85  return con;
86 }
#define POPT_CONTEXT_KEEP_FIRST
Definition: popt.h:45
struct poptContext_s * poptContext
Definition: popt.h:69
#define POPT_CONTEXT_POSIXMEHARDER
Definition: popt.h:46
int argc
Definition: poptint.h:25
int execAbsolute
Definition: poptint.h:60
int finalArgvAlloced
Definition: poptint.h:57
const struct poptOption * options
Definition: poptint.h:47

References poptContext_s::aliases, poptContext_s::appName, poptContext_s::arg_strip, optionStackEntry::argb, optionStackEntry::argc, optionStackEntry::argv, poptContext_s::execAbsolute, poptContext_s::execs, poptContext_s::finalArgv, poptContext_s::finalArgvAlloced, poptContext_s::flags, flags, poptContext_s::leftovers, optionStackEntry::next, poptContext_s::numAliases, poptContext_s::numExecs, poptContext_s::options, poptContext_s::optionStack, poptContext_s::os, POPT_CONTEXT_KEEP_FIRST, and POPT_CONTEXT_POSIXMEHARDER.

Referenced by CACmdLnOptions::parse().

Here is the caller graph for this function:

◆ poptGetInvocationName()

const char* poptGetInvocationName ( poptContext  con)

Definition at line 765 of file popt.cpp.

765  {
766  return con->os->argv[0];
767 }

References optionStackEntry::argv, and poptContext_s::os.

◆ poptGetNextOpt()

int poptGetNextOpt ( poptContext  con)

Definition at line 407 of file popt.cpp.

408 {
409  const struct poptOption * opt = NULL;
410  int done = 0;
411 
412  while (!done) {
413  const char * origOptString = NULL;
414  poptCallbackType cb = NULL;
415  const void * cbData = NULL;
416  const char * longArg = NULL;
417  int canstrip = 0;
418 
419  while (!con->os->nextCharArg && con->os->next == con->os->argc
420  && con->os > con->optionStack) {
421  cleanOSE(con->os--);
422  }
423  if (!con->os->nextCharArg && con->os->next == con->os->argc) {
424  invokeCallbacks(con, con->options, 1);
425  //if (con->doExec) execCommand(con);
426  return -1;
427  }
428 
429  /* Process next long option */
430  if (!con->os->nextCharArg) {
431  char * localOptString, *optString;
432  int thisopt;
433 
434  if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
435  con->os->next++;
436  continue;
437  }
438  thisopt = con->os->next;
439  origOptString = con->os->argv[con->os->next++];
440 
441  if (con->restLeftover || *origOptString != '-') {
442  con->leftovers[con->numLeftovers++] = origOptString;
444  con->restLeftover = 1;
445  continue;
446  }
447 
448  /* Make a copy we can hack at */
449  localOptString = optString =
450  strcpy((char*)alloca(strlen(origOptString) + 1),
451  origOptString);
452 
453  if (!optString[0])
454  return POPT_ERROR_BADOPT;
455 
456  if (optString[1] == '-' && !optString[2]) {
457  con->restLeftover = 1;
458  continue;
459  }
460  else {
461  char *oe;
462  int singleDash;
463 
464  optString++;
465  if (*optString == '-')
466  singleDash = 0, optString++;
467  else
468  singleDash = 1;
469 
470  /* XXX aliases with arg substitution need "--alias=arg" */
471  if (handleAlias(con, optString, '\0', NULL))
472  continue;
473  if (handleExec(con, optString, '\0'))
474  continue;
475 
476  /* Check for "--long=arg" option. */
477  for (oe = optString; *oe && *oe != '='; oe++)
478  ;
479  if (*oe == '=') {
480  *oe++ = '\0';
481  /* XXX longArg is mapped back to persistent storage. */
482  longArg = origOptString + (oe - localOptString);
483  }
484 
485  opt = findOption(con->options, optString, '\0', &cb, &cbData,
486  singleDash);
487  if (!opt && !singleDash)
488  return POPT_ERROR_BADOPT;
489  }
490 
491  if (!opt) {
492  con->os->nextCharArg = origOptString + 1;
493  }
494  else {
495  if (con->os == con->optionStack &&
496  opt->argInfo & POPT_ARGFLAG_STRIP) {
497  canstrip = 1;
498  poptStripArg(con, thisopt);
499  }
500  }
501  }
502 
503  /* Process next short option */
504  if (con->os->nextCharArg) {
505  origOptString = con->os->nextCharArg;
506 
507  con->os->nextCharArg = NULL;
508 
509  if (handleAlias(con, NULL, *origOptString,
510  origOptString + 1)) {
511  origOptString++;
512  continue;
513  }
514  if (handleExec(con, NULL, *origOptString))
515  continue;
516 
517  opt = findOption(con->options, NULL, *origOptString, &cb,
518  &cbData, 0);
519  if (!opt)
520  return POPT_ERROR_BADOPT;
521 
522  origOptString++;
523  if (*origOptString)
524  con->os->nextCharArg = origOptString;
525  }
526 
527  if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) {
528  *((int *)opt->arg) = 1;
529  }
530  else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) {
531  if (opt->arg)
532  *((int *)opt->arg) = opt->val;
533  }
534  else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
535  if (con->os->nextArg) {
536  xfree(con->os->nextArg);
537  con->os->nextArg = NULL;
538  }
539  if (longArg) {
540  con->os->nextArg = expandNextArg(con, longArg);
541  }
542  else if (con->os->nextCharArg) {
543  con->os->nextArg = expandNextArg(con, con->os->nextCharArg);
544  con->os->nextCharArg = NULL;
545  }
546  else {
547  while (con->os->next == con->os->argc &&
548  con->os > con->optionStack) {
549  cleanOSE(con->os--);
550  }
551  if (con->os->next == con->os->argc)
552  return POPT_ERROR_NOARG;
553 
554  /* make sure this isn't part of a short arg or the
555  result of an alias expansion */
556  if (con->os == con->optionStack &&
557  opt->argInfo & POPT_ARGFLAG_STRIP &&
558  canstrip) {
559  poptStripArg(con, con->os->next);
560  }
561 
562  con->os->nextArg = expandNextArg(con, con->os->argv[con->os->next++]);
563  }
564 
565  if (opt->arg) {
566  long aLong;
567  char *end;
568 
569  switch (opt->argInfo & POPT_ARG_MASK) {
570  case POPT_ARG_STRING:
571  /* XXX memory leak, hard to plug */
572  *((const char **)opt->arg) = xstrdup(con->os->nextArg);
573  break;
574 
575  case POPT_ARG_INT:
576  case POPT_ARG_LONG:
577  aLong = strtol(con->os->nextArg, &end, 0);
578  if (!(end && *end == '\0'))
579  return POPT_ERROR_BADNUMBER;
580 
581  if (aLong == LONG_MIN || aLong == LONG_MAX)
582  return POPT_ERROR_OVERFLOW;
583  if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) {
584  *((long *)opt->arg) = aLong;
585  }
586  else {
587  if (aLong > INT_MAX || aLong < INT_MIN)
588  return POPT_ERROR_OVERFLOW;
589  *((int *)opt->arg) = aLong;
590  }
591  break;
592 
593  default:
594  fprintf(stdout, POPT_("option type (%d) not implemented in popt\n"),
595  opt->argInfo & POPT_ARG_MASK);
596  exit(EXIT_FAILURE);
597  }
598  }
599  }
600 
601  if (cb)
602  cb(con, POPT_CALLBACK_REASON_OPTION, opt, con->os->nextArg, cbData);
603  else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
604  done = 1;
605 
606  if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
607  con->finalArgvAlloced += 10;
608  con->finalArgv = (const char**)realloc((void*)con->finalArgv,
609  sizeof(*con->finalArgv) * con->finalArgvAlloced);
610  }
611 
612  { char *s = (char*)malloc((opt->longName ? strlen(opt->longName) : 0) + 3);
613  if (opt->longName)
614  sprintf(s, "--%s", opt->longName);
615  else
616  sprintf(s, "-%c", opt->shortName);
617  con->finalArgv[con->finalArgvCount++] = s;
618  }
619 
620  if (opt->arg && (opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE
621  && (opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL) {
622  con->finalArgv[con->finalArgvCount++] = xstrdup(con->os->nextArg);
623  }
624  }
625 
626  return opt->val;
627 }
#define POPT_ERROR_BADOPT
Definition: popt.cpp:718
#define POPT_ERROR_OVERFLOW
Definition: popt.h:38
#define POPT_ARG_MASK
Definition: popt.h:24
void(* poptCallbackType)(poptContext con, enum poptCallbackReason reason, const struct poptOption *opt, const char *arg, const void *data)
Definition: popt.h:77
#define POPT_ARG_VAL
Definition: popt.h:23
#define POPT_ARGFLAG_STRIP
Definition: popt.h:27
#define POPT_ARG_INT
Definition: popt.h:18
#define POPT_ARG_LONG
Definition: popt.h:19
#define POPT_ARG_NONE
Definition: popt.h:16
#define POPT_ARG_STRING
Definition: popt.h:17
#define POPT_ERROR_BADNUMBER
Definition: popt.h:37
@ POPT_CALLBACK_REASON_OPTION
Definition: popt.h:76
#define POPT_(foo)
Definition: poptint.h:81
#define PBM_ISSET(d, s)
Definition: poptint.h:22
const char * nextArg
Definition: poptint.h:29
const char * nextCharArg
Definition: poptint.h:30
int finalArgvCount
Definition: poptint.h:56
int restLeftover
Definition: poptint.h:48
int argInfo
Definition: popt.h:51
void * arg
Definition: popt.h:52
char shortName
Definition: popt.h:50
int val
Definition: popt.h:53
const char * longName
Definition: popt.h:49
char * xstrdup(const char *str)
char * alloca()

References optionStackEntry::argc, optionStackEntry::next, optionStackEntry::nextCharArg, poptContext_s::optionStack, and poptContext_s::os.

Referenced by CACmdLnOptions::parse().

Here is the caller graph for this function:

◆ poptGetOptArg()

const char* poptGetOptArg ( poptContext  con)

Definition at line 629 of file popt.cpp.

629  {
630  const char * ret = con->os->nextArg;
631  con->os->nextArg = NULL;
632  return ret;
633 }

References optionStackEntry::nextArg, and poptContext_s::os.

◆ poptParseArgvString()

int poptParseArgvString ( const char *  s,
int *  argcPtr,
const char ***  argvPtr 
)

Definition at line 39 of file poptparse.cpp.

40 {
41  const char * src;
42  char quote = '\0';
43  int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA;
44  const char ** argv = (const char **)malloc(sizeof(*argv) * argvAlloced);
45  int argc = 0;
46  int buflen = strlen(s) + 1;
47  char * buf = (char*)memset(alloca(buflen), 0, buflen);
48 
49  argv[argc] = buf;
50 
51  for (src = s; *src; src++) {
52  if (quote == *src) {
53  quote = '\0';
54  } else if (quote) {
55  if (*src == '\\') {
56  src++;
57  if (!*src) {
58  free((void*)argv);
59  return POPT_ERROR_BADQUOTE;
60  }
61  if (*src != quote) *buf++ = '\\';
62  }
63  *buf++ = *src;
64  } else if (isspace(*src)) {
65  if (*argv[argc]) {
66  buf++, argc++;
67  if (argc == argvAlloced) {
68  argvAlloced += POPT_ARGV_ARRAY_GROW_DELTA;
69  argv = (const char**)realloc((void*)argv, sizeof(*argv) * argvAlloced);
70  }
71  argv[argc] = buf;
72  }
73  } else switch (*src) {
74  case '"':
75  case '\'':
76  quote = *src;
77  break;
78  case '\\':
79  src++;
80  if (!*src) {
81  free((void*)argv);
82  return POPT_ERROR_BADQUOTE;
83  }
84  /*@fallthrough@*/
85  default:
86  *buf++ = *src;
87  break;
88  }
89  }
90 
91  if (strlen(argv[argc])) {
92  argc++, buf++;
93  }
94 
95  (void) poptDupArgv(argc, argv, argcPtr, argvPtr);
96 
97  free((void*)argv);
98 
99  return 0;
100 }
#define POPT_ERROR_BADQUOTE
Definition: popt.cpp:720
int poptDupArgv(int argc, const char **argv, int *argcPtr, const char ***argvPtr)
Definition: poptparse.cpp:10
#define POPT_ARGV_ARRAY_GROW_DELTA
Definition: poptparse.cpp:8

References alloca(), POPT_ARGV_ARRAY_GROW_DELTA, POPT_ERROR_BADQUOTE, and poptDupArgv().

Here is the call graph for this function:

◆ poptPeekArg()

const char* poptPeekArg ( poptContext  con)

Definition at line 640 of file popt.cpp.

640  {
641  if (con->numLeftovers == con->nextLeftover) return NULL;
642  return con->leftovers[con->nextLeftover];
643 }

References poptContext_s::leftovers, poptContext_s::nextLeftover, and poptContext_s::numLeftovers.

◆ poptPrintHelp()

void poptPrintHelp ( poptContext  con,
FILE *  f,
int  flags 
)

Definition at line 186 of file popthelp.cpp.

186  {
187  int leftColWidth;
188 
189  showHelpIntro(con, f);
190  if (con->otherHelp)
191  fprintf(f, " %s\n", con->otherHelp);
192  else
193  fprintf(f, " %s\n", POPT_("[OPTION...]"));
194 
195  leftColWidth = maxArgWidth(con->options, NULL);
196  singleTableHelp(f, con->options, leftColWidth, NULL);
197 }

◆ poptPrintUsage()

void poptPrintUsage ( poptContext  con,
FILE *  f,
int  flags 
)

Definition at line 283 of file popthelp.cpp.

283  {
284  int cursor;
285 
286  cursor = showHelpIntro(con, f);
287  cursor += showShortOptions(con->options, f, NULL);
288  singleTableUsage(f, cursor, con->options, NULL);
289 
290  if (con->otherHelp) {
291  cursor += strlen(con->otherHelp) + 1;
292  if (cursor > 79) fprintf(f, "\n ");
293  fprintf(f, " %s", con->otherHelp);
294  }
295 
296  fprintf(f, "\n");
297 }

◆ poptReadConfigFile()

int poptReadConfigFile ( poptContext  con,
const char *  fn 
)

◆ poptReadDefaultConfig()

int poptReadDefaultConfig ( poptContext  con,
int  useEnv 
)

◆ poptResetContext()

void poptResetContext ( poptContext  con)

Definition at line 104 of file popt.cpp.

104  {
105  int i;
106 
107  while (con->os > con->optionStack) {
108  cleanOSE(con->os--);
109  }
110  if (con->os->argb) {
111  PBM_FREE(con->os->argb);
112  con->os->argb = NULL;
113  }
114  con->os->currAlias = NULL;
115  con->os->nextCharArg = NULL;
116  con->os->nextArg = NULL;
117  con->os->next = 1; /* skip argv[0] */
118 
119  con->numLeftovers = 0;
120  con->nextLeftover = 0;
121  con->restLeftover = 0;
122  con->doExec = NULL;
123 
124  for (i = 0; i < con->finalArgvCount; i++) {
125  if (con->finalArgv[i]) {
126  xfree(con->finalArgv[i]);
127  con->finalArgv[i] = NULL;
128  }
129  }
130 
131  con->finalArgvCount = 0;
132 
133  if (con->arg_strip) {
134  PBM_FREE(con->arg_strip);
135  con->arg_strip = NULL;
136  }
137 }
struct poptAlias * currAlias
Definition: poptint.h:31
struct execEntry * doExec
Definition: poptint.h:58

References poptContext_s::optionStack, and poptContext_s::os.

Referenced by poptFreeContext().

Here is the caller graph for this function:

◆ poptSetExecPath()

void poptSetExecPath ( poptContext  con,
const char *  path,
int  allowAbsolute 
)

Definition at line 25 of file popt.cpp.

25  {
26  if (con->execPath) xfree(con->execPath);
27  con->execPath = xstrdup(path);
28  con->execAbsolute = allowAbsolute;
29 }

References poptContext_s::execAbsolute, poptContext_s::execPath, xfree, and xstrdup().

Here is the call graph for this function:

◆ poptSetOtherOptionHelp()

void poptSetOtherOptionHelp ( poptContext  con,
const char *  text 
)

Definition at line 299 of file popthelp.cpp.

299  {
300  if (con->otherHelp) xfree(con->otherHelp);
301  con->otherHelp = xstrdup(text);
302 }

References poptContext_s::otherHelp, xfree, and xstrdup().

Here is the call graph for this function:

◆ poptStrerror()

const char* poptStrerror ( const int  error)

Definition at line 723 of file popt.cpp.

723  {
724  switch (error) {
725  case POPT_ERROR_NOARG:
726  return POPT_("missing argument");
727  case POPT_ERROR_BADOPT:
728  return POPT_("unknown option");
730  return POPT_("aliases nested too deeply");
731  case POPT_ERROR_BADQUOTE:
732  return POPT_("error in paramter quoting");
734  return POPT_("invalid numeric value");
735  case POPT_ERROR_OVERFLOW:
736  return POPT_("number too large or too small");
737  case POPT_ERROR_ERRNO:
738  return strerror(errno);
739  default:
740  return POPT_("unknown error");
741  }
742 }
#define POPT_ERROR_OPTSTOODEEP
Definition: popt.cpp:719
#define POPT_ERROR_ERRNO
Definition: popt.cpp:721

References POPT_, POPT_ERROR_BADNUMBER, POPT_ERROR_BADOPT, POPT_ERROR_BADQUOTE, POPT_ERROR_ERRNO, POPT_ERROR_NOARG, POPT_ERROR_OPTSTOODEEP, and POPT_ERROR_OVERFLOW.

◆ poptStrippedArgv()

int poptStrippedArgv ( poptContext  con,
int  argc,
char **  argv 
)

Definition at line 769 of file popt.cpp.

770 {
771  int i, j = 1, numargs = argc;
772 
773  for (i = 1; i < argc; i++) {
774  if (PBM_ISSET(i, con->arg_strip)) {
775  numargs--;
776  }
777  }
778 
779  for (i = 1; i < argc; i++) {
780  if (PBM_ISSET(i, con->arg_strip)) {
781  continue;
782  }
783  else {
784  if (j < numargs) {
785  argv[j++] = argv[i];
786  }
787  else {
788  argv[j++] = NULL;
789  }
790  }
791  }
792 
793  return(numargs);
794 }

References poptContext_s::arg_strip, optionStackEntry::argc, optionStackEntry::argv, and PBM_ISSET.

◆ poptStuffArgs()

int poptStuffArgs ( poptContext  con,
const char **  argv 
)

Definition at line 744 of file popt.cpp.

744  {
745  int argc;
746 
747  if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
748  return POPT_ERROR_OPTSTOODEEP;
749 
750  for (argc = 0; argv[argc]; argc++)
751  ;
752 
753  con->os++;
754  con->os->next = 0;
755  con->os->nextArg = NULL;
756  con->os->nextCharArg = NULL;
757  con->os->currAlias = NULL;
758  poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
759  con->os->argb = NULL;
760  con->os->stuffed = 1;
761 
762  return 0;
763 }
int poptDupArgv(int argc, const char **argv, int *argcPtr, const char ***argvPtr)
Definition: poptparse.cpp:10
#define POPT_OPTION_DEPTH
Definition: popt.h:14
int stuffed
Definition: poptint.h:32

References optionStackEntry::argb, optionStackEntry::argc, optionStackEntry::argv, optionStackEntry::currAlias, optionStackEntry::next, optionStackEntry::nextArg, optionStackEntry::nextCharArg, poptContext_s::optionStack, poptContext_s::os, POPT_ERROR_OPTSTOODEEP, POPT_OPTION_DEPTH, poptDupArgv(), and optionStackEntry::stuffed.

Here is the call graph for this function:

Variable Documentation

◆ poptHelpOptions

struct poptOption poptHelpOptions[]
extern

Definition at line 10 of file popthelp.cpp.