Mixe for Privacy and Anonymity in the Internet
Defines | Functions
popt.cpp File Reference
#include "../StdAfx.h"
#include "poptint.h"
Include dependency graph for popt.cpp:

Go to the source code of this file.

Defines

#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() */

Functions

void poptSetExecPath (poptContext con, const char *path, int allowAbsolute)
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)
void poptFreeContext (poptContext con)
int poptAddAlias (poptContext con, struct poptAlias newAlias, int flags)
const char * poptBadOption (poptContext con, int flags)
const char * poptStrerror (const int error)
int poptStuffArgs (poptContext con, const char **argv)
const char * poptGetInvocationName (poptContext con)
int poptStrippedArgv (poptContext con, int argc, char **argv)

Define Documentation

#define POPT_ERROR_BADOPT   -11

Definition at line 705 of file popt.cpp.

Referenced by CACmdLnOptions::parse(), poptGetNextOpt(), and poptStrerror().

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

Definition at line 707 of file popt.cpp.

Referenced by poptParseArgvString(), and poptStrerror().

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

Definition at line 708 of file popt.cpp.

Referenced by poptStrerror().

#define POPT_ERROR_NOARG   -10

Definition at line 704 of file popt.cpp.

Referenced by poptDupArgv(), poptGetNextOpt(), and poptStrerror().

#define POPT_ERROR_OPTSTOODEEP   -13

Definition at line 706 of file popt.cpp.

Referenced by poptStrerror(), and poptStuffArgs().


Function Documentation

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

Definition at line 669 of file popt.cpp.

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

{
    int aliasNum = con->numAliases++;
    struct poptAlias * alias;

    /* SunOS won't realloc(NULL, ...) */
    if (!con->aliases)
  con->aliases = (struct poptAlias*)malloc(sizeof(newAlias) * con->numAliases);
    else
  con->aliases = (struct poptAlias*)realloc(con->aliases,
             sizeof(newAlias) * con->numAliases);
    alias = con->aliases + aliasNum;

    alias->longName = (newAlias.longName)
  ? strcpy((char*)malloc(strlen(newAlias.longName) + 1), newAlias.longName)
  : NULL;
    alias->shortName = newAlias.shortName;
    alias->argc = newAlias.argc;
    alias->argv = newAlias.argv;

    return 0;
}
const char* poptBadOption ( poptContext  con,
int  flags 
)

Definition at line 693 of file popt.cpp.

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

                                                       {
    struct optionStackEntry * os;

    if (flags & POPT_BADOPTION_NOALIAS)
  os = con->optionStack;
    else
  os = con->os;

    return os->argv[os->next - 1];
}

Definition at line 641 of file popt.cpp.

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, execEntry::longName, poptAlias::longName, poptContext_s::numAliases, poptContext_s::numExecs, poptContext_s::os, poptContext_s::otherHelp, PBM_FREE, poptResetContext(), execEntry::script, and xfree.

Referenced by CACmdLnOptions::parse().

                                      {
    int i;

    poptResetContext(con);
    if (con->os->argb) free(con->os->argb);

    for (i = 0; i < con->numAliases; i++) {
  if (con->aliases[i].longName) xfree(con->aliases[i].longName);
  free((void*)con->aliases[i].argv);
    }

    for (i = 0; i < con->numExecs; i++) {
  if (con->execs[i].longName) xfree(con->execs[i].longName);
  xfree(con->execs[i].script);
    }
    if (con->execs) xfree(con->execs);

    free((void*)con->leftovers);
    free((void*)con->finalArgv);
    if (con->appName) xfree(con->appName);
    if (con->aliases) free(con->aliases);
    if (con->otherHelp) xfree(con->otherHelp);
    if (con->execPath) xfree(con->execPath);
    if (con->arg_strip) PBM_FREE(con->arg_strip);
    
    free(con);
}

Here is the call graph for this function:

const char* poptGetArg ( poptContext  con)

Definition at line 622 of file popt.cpp.

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

                                         {
    if (con->numLeftovers == con->nextLeftover) return NULL;
    return con->leftovers[con->nextLeftover++];
}
const char** poptGetArgs ( poptContext  con)

Definition at line 632 of file popt.cpp.

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

                                           {
    if (con->numLeftovers == con->nextLeftover) return NULL;

    /* some apps like [like RPM ;-) ] need this NULL terminated */
    con->leftovers[con->numLeftovers] = NULL;

    return (con->leftovers + con->nextLeftover);
}
poptContext poptGetContext ( const char *  name,
int  argc,
const char **  argv,
const struct poptOption options,
int  flags 
)

Definition at line 50 of file popt.cpp.

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().

                                                       {
    poptContext con = (poptContext)malloc(sizeof(*con));

    memset(con, 0, sizeof(*con));

    con->os = con->optionStack;
    con->os->argc = argc;
    con->os->argv = argv;
    con->os->argb = NULL;

    if (!(flags & POPT_CONTEXT_KEEP_FIRST))
  con->os->next = 1;      /* skip argv[0] */

    con->leftovers = (const char**)calloc( (argc + 1), sizeof(char *) );
    con->options = options;
    con->aliases = NULL;
    con->numAliases = 0;
    con->flags = flags;
    con->execs = NULL;
    con->numExecs = 0;
    con->finalArgvAlloced = argc * 2;
    con->finalArgv = (const char**)calloc( con->finalArgvAlloced, sizeof(*con->finalArgv) );
    con->execAbsolute = 1;
    con->arg_strip = NULL;

    if (getenv("POSIXLY_CORRECT") || getenv("POSIX_ME_HARDER"))
  con->flags |= POPT_CONTEXT_POSIXMEHARDER;

    if (name)
  con->appName = strcpy((char*)malloc(strlen(name) + 1), name);

    invokeCallbacks(con, con->options, 0);

    return con;
}
const char* poptGetInvocationName ( poptContext  con)

Definition at line 752 of file popt.cpp.

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

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

Definition at line 401 of file popt.cpp.

References alloca(), poptOption::arg, optionStackEntry::argb, optionStackEntry::argc, poptOption::argInfo, optionStackEntry::argv, poptContext_s::finalArgv, poptContext_s::finalArgvAlloced, poptContext_s::finalArgvCount, poptContext_s::flags, poptContext_s::leftovers, poptOption::longName, optionStackEntry::next, optionStackEntry::nextArg, optionStackEntry::nextCharArg, poptContext_s::numLeftovers, poptContext_s::options, poptContext_s::optionStack, poptContext_s::os, PBM_ISSET, POPT_, POPT_ARG_INT, POPT_ARG_LONG, POPT_ARG_MASK, POPT_ARG_NONE, POPT_ARG_STRING, POPT_ARG_VAL, POPT_ARGFLAG_STRIP, POPT_CALLBACK_REASON_OPTION, POPT_CONTEXT_POSIXMEHARDER, POPT_ERROR_BADNUMBER, POPT_ERROR_BADOPT, POPT_ERROR_NOARG, POPT_ERROR_OVERFLOW, poptContext_s::restLeftover, poptOption::shortName, poptOption::val, xfree, and xstrdup().

Referenced by CACmdLnOptions::parse().

{
    const struct poptOption * opt = NULL;
    int done = 0;

    while (!done) {
  const char * origOptString = NULL;
  poptCallbackType cb = NULL;
  const void * cbData = NULL;
  const char * longArg = NULL;
  int canstrip = 0;

  while (!con->os->nextCharArg && con->os->next == con->os->argc
    && con->os > con->optionStack) {
      cleanOSE(con->os--);
  }
  if (!con->os->nextCharArg && con->os->next == con->os->argc) {
      invokeCallbacks(con, con->options, 1);
      //if (con->doExec) execCommand(con);
      return -1;
  }

  /* Process next long option */
  if (!con->os->nextCharArg) {
      char * localOptString, * optString;
      int thisopt;

      if (con->os->argb && PBM_ISSET(con->os->next, con->os->argb)) {
    con->os->next++;
    continue;
      }
      thisopt=con->os->next;
      origOptString = con->os->argv[con->os->next++];

      if (con->restLeftover || *origOptString != '-') {
    con->leftovers[con->numLeftovers++] = origOptString;
    if (con->flags & POPT_CONTEXT_POSIXMEHARDER)
        con->restLeftover = 1;
    continue;
      }

      /* Make a copy we can hack at */
      localOptString = optString =
      strcpy((char*)alloca(strlen(origOptString) + 1),
      origOptString);

      if (!optString[0])
    return POPT_ERROR_BADOPT;

      if (optString[1] == '-' && !optString[2]) {
    con->restLeftover = 1;
    continue;
      } else {
    char *oe;
    int singleDash;

    optString++;
    if (*optString == '-')
        singleDash = 0, optString++;
    else
        singleDash = 1;

    /* XXX aliases with arg substitution need "--alias=arg" */
    if (handleAlias(con, optString, '\0', NULL))
        continue;
    if (handleExec(con, optString, '\0'))
        continue;

    /* Check for "--long=arg" option. */
    for (oe = optString; *oe && *oe != '='; oe++)
        ;
    if (*oe == '=') {
        *oe++ = '\0';
        /* XXX longArg is mapped back to persistent storage. */
        longArg = origOptString + (oe - localOptString);
    }

    opt = findOption(con->options, optString, '\0', &cb, &cbData,
         singleDash);
    if (!opt && !singleDash)
        return POPT_ERROR_BADOPT;
      }

      if (!opt) {
    con->os->nextCharArg = origOptString + 1;
      } else {
    if(con->os == con->optionStack &&
       opt->argInfo & POPT_ARGFLAG_STRIP) {
        canstrip = 1;
        poptStripArg(con, thisopt);
    }
      }
  }

  /* Process next short option */
  if (con->os->nextCharArg) {
      origOptString = con->os->nextCharArg;

      con->os->nextCharArg = NULL;

      if (handleAlias(con, NULL, *origOptString,
          origOptString + 1)) {
    origOptString++;
    continue;
      }
      if (handleExec(con, NULL, *origOptString))
    continue;

      opt = findOption(con->options, NULL, *origOptString, &cb,
           &cbData, 0);
      if (!opt)
    return POPT_ERROR_BADOPT;

      origOptString++;
      if (*origOptString)
    con->os->nextCharArg = origOptString;
  }

  if (opt->arg && (opt->argInfo & POPT_ARG_MASK) == POPT_ARG_NONE) {
      *((int *)opt->arg) = 1;
  } else if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_VAL) {
      if (opt->arg)
    *((int *) opt->arg) = opt->val;
  } else if ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE) {
      if (con->os->nextArg) {
    xfree(con->os->nextArg);
    con->os->nextArg = NULL;
      }
      if (longArg) {
    con->os->nextArg = expandNextArg(con, longArg);
      } else if (con->os->nextCharArg) {
    con->os->nextArg = expandNextArg(con, con->os->nextCharArg);
    con->os->nextCharArg = NULL;
      } else {
    while (con->os->next == con->os->argc &&
           con->os > con->optionStack) {
        cleanOSE(con->os--);
    }
    if (con->os->next == con->os->argc)
        return POPT_ERROR_NOARG;

    /* make sure this isn't part of a short arg or the
                   result of an alias expansion */
    if(con->os == con->optionStack &&
       opt->argInfo & POPT_ARGFLAG_STRIP &&
       canstrip) {
        poptStripArg(con, con->os->next);
    }
    
    con->os->nextArg = expandNextArg(con, con->os->argv[con->os->next++]);
      }

      if (opt->arg) {
    long aLong;
    char *end;

    switch (opt->argInfo & POPT_ARG_MASK) {
      case POPT_ARG_STRING:
        /* XXX memory leak, hard to plug */
        *((const char **) opt->arg) = xstrdup(con->os->nextArg);
        break;

      case POPT_ARG_INT:
      case POPT_ARG_LONG:
        aLong = strtol(con->os->nextArg, &end, 0);
        if (!(end && *end == '\0'))
      return POPT_ERROR_BADNUMBER;

        if (aLong == LONG_MIN || aLong == LONG_MAX)
      return POPT_ERROR_OVERFLOW;
        if ((opt->argInfo & POPT_ARG_MASK) == POPT_ARG_LONG) {
      *((long *) opt->arg) = aLong;
        } else {
      if (aLong > INT_MAX || aLong < INT_MIN)
          return POPT_ERROR_OVERFLOW;
      *((int *) opt->arg) = aLong;
        }
        break;

      default:
        fprintf(stdout, POPT_("option type (%d) not implemented in popt\n"),
          opt->argInfo & POPT_ARG_MASK);
        exit(EXIT_FAILURE);
    }
      }
  }

  if (cb)
      cb(con, POPT_CALLBACK_REASON_OPTION, opt, con->os->nextArg, cbData);
  else if (opt->val && ((opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL))
      done = 1;

  if ((con->finalArgvCount + 2) >= (con->finalArgvAlloced)) {
      con->finalArgvAlloced += 10;
      con->finalArgv = (const char**)realloc((void*)con->finalArgv,
          sizeof(*con->finalArgv) * con->finalArgvAlloced);
  }

  {    char *s = (char*)malloc((opt->longName ? strlen(opt->longName) : 0) + 3);
      if (opt->longName)
    sprintf(s, "--%s", opt->longName);
      else
    sprintf(s, "-%c", opt->shortName);
      con->finalArgv[con->finalArgvCount++] = s;
  }

  if (opt->arg && (opt->argInfo & POPT_ARG_MASK) != POPT_ARG_NONE
         && (opt->argInfo & POPT_ARG_MASK) != POPT_ARG_VAL) {
      con->finalArgv[con->finalArgvCount++] = xstrdup(con->os->nextArg);
  }
    }

    return opt->val;
}

Here is the call graph for this function:

const char* poptGetOptArg ( poptContext  con)

Definition at line 616 of file popt.cpp.

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

                                            {
    const char * ret = con->os->nextArg;
    con->os->nextArg = NULL;
    return ret;
}
const char* poptPeekArg ( poptContext  con)

Definition at line 627 of file popt.cpp.

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

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

Definition at line 103 of file popt.cpp.

References poptContext_s::arg_strip, optionStackEntry::argb, optionStackEntry::currAlias, poptContext_s::doExec, poptContext_s::finalArgv, poptContext_s::finalArgvCount, optionStackEntry::next, optionStackEntry::nextArg, optionStackEntry::nextCharArg, poptContext_s::nextLeftover, poptContext_s::numLeftovers, poptContext_s::optionStack, poptContext_s::os, PBM_FREE, poptContext_s::restLeftover, and xfree.

Referenced by poptFreeContext().

                                       {
    int i;

    while (con->os > con->optionStack) {
  cleanOSE(con->os--);
    }
    if (con->os->argb) {
  PBM_FREE(con->os->argb);
  con->os->argb = NULL;
    }
    con->os->currAlias = NULL;
    con->os->nextCharArg = NULL;
    con->os->nextArg = NULL;
    con->os->next = 1;      /* skip argv[0] */

    con->numLeftovers = 0;
    con->nextLeftover = 0;
    con->restLeftover = 0;
    con->doExec = NULL;

    for (i = 0; i < con->finalArgvCount; i++) {
  if (con->finalArgv[i]) {
      xfree(con->finalArgv[i]);
      con->finalArgv[i] = NULL;
  }
    }

    con->finalArgvCount = 0;

    if (con->arg_strip) {
  PBM_FREE(con->arg_strip);
  con->arg_strip = NULL;
    }
}
void poptSetExecPath ( poptContext  con,
const char *  path,
int  allowAbsolute 
)

Definition at line 25 of file popt.cpp.

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

                                                                            {
    if (con->execPath) xfree(con->execPath);
    con->execPath = xstrdup(path);
    con->execAbsolute = allowAbsolute;
}

Here is the call graph for this function:

const char* poptStrerror ( const int  error)

Definition at line 710 of file popt.cpp.

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

                                             {
    switch (error) {
      case POPT_ERROR_NOARG:
  return POPT_("missing argument");
      case POPT_ERROR_BADOPT:
  return POPT_("unknown option");
      case POPT_ERROR_OPTSTOODEEP:
  return POPT_("aliases nested too deeply");
      case POPT_ERROR_BADQUOTE:
  return POPT_("error in paramter quoting");
      case POPT_ERROR_BADNUMBER:
  return POPT_("invalid numeric value");
      case POPT_ERROR_OVERFLOW:
  return POPT_("number too large or too small");
      case POPT_ERROR_ERRNO:
  return strerror(errno);
      default:
  return POPT_("unknown error");
    }
}
int poptStrippedArgv ( poptContext  con,
int  argc,
char **  argv 
)

Definition at line 756 of file popt.cpp.

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

{
    int i,j=1, numargs=argc;
    
    for(i=1; i<argc; i++) {
  if(PBM_ISSET(i, con->arg_strip)) {
      numargs--;
  }
    }
    
    for(i=1; i<argc; i++) {
  if(PBM_ISSET(i, con->arg_strip)) {
      continue;
  } else {
      if(j<numargs) {
    argv[j++]=argv[i];
      } else {
    argv[j++]='\0';
      }
  }
    }
    
    return(numargs);
}
int poptStuffArgs ( poptContext  con,
const char **  argv 
)

Definition at line 731 of file popt.cpp.

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.

                                                       {
    int argc;

    if ((con->os - con->optionStack) == POPT_OPTION_DEPTH)
  return POPT_ERROR_OPTSTOODEEP;

    for (argc = 0; argv[argc]; argc++)
  ;

    con->os++;
    con->os->next = 0;
    con->os->nextArg = NULL;
    con->os->nextCharArg = NULL;
    con->os->currAlias = NULL;
    poptDupArgv(argc, argv, &con->os->argc, &con->os->argv);
    con->os->argb = NULL;
    con->os->stuffed = 1;

    return 0;
}

Here is the call graph for this function: