14 static char * strerror(
int errno) {
16 extern char * sys_errlist[];
18 if ((0 <= errno) && (errno < sys_nerr))
19 return sys_errlist[errno];
21 return POPT_(
"unknown errno");
55 memset(con, 0,
sizeof(*con));
65 con->
leftovers = (
const char**)calloc((argc + 1),
sizeof(
char *));
77 if (getenv(
"POSIXLY_CORRECT") || getenv(
"POSIX_ME_HARDER"))
81 con->
appName = strcpy((
char*)malloc(strlen(name) + 1), name);
83 invokeCallbacks(con, con->
options, 0);
158 if (con->
doExec == NULL) {
185 const char * nextCharArg) {
210 if (nextCharArg && *nextCharArg)
298 callbackData, singleDash);
300 if (*callback && !*callbackData)
320 *callbackData = NULL;
331 static const char *findNextArg(
poptContext con,
unsigned argx,
int deletE)
341 for (i = os->
next; i < os->
argc; i++) {
343 if (*os->
argv[i] ==
'-')
continue;
344 if (--argx > 0)
continue;
353 }
while (arg == NULL);
357 static const char * expandNextArg(
poptContext con,
const char * s)
362 size_t tn = strlen(s) + 1;
365 te = t = (
char*)malloc(tn);;
366 while ((c = *s++) !=
'\0') {
374 if (!(s[0] ==
'#' && s[1] ==
':' && s[2] ==
'+'))
376 if ((a = findNextArg(con, 1, 1)) == NULL)
383 t = (
char*)realloc(t, tn);
385 strncpy(te, a, alen); te += alen;
394 t = (
char*)realloc(t, strlen(t) + 1);
398 static void poptStripArg(
poptContext con,
int which)
413 const char * origOptString = NULL;
415 const void * cbData = NULL;
416 const char * longArg = NULL;
424 invokeCallbacks(con, con->
options, 1);
431 char * localOptString, *optString;
449 localOptString = optString =
450 strcpy((
char*)
alloca(strlen(origOptString) + 1),
456 if (optString[1] ==
'-' && !optString[2]) {
465 if (*optString ==
'-')
466 singleDash = 0, optString++;
471 if (handleAlias(con, optString,
'\0', NULL))
473 if (handleExec(con, optString,
'\0'))
477 for (oe = optString; *oe && *oe !=
'='; oe++)
482 longArg = origOptString + (oe - localOptString);
485 opt = findOption(con->
options, optString,
'\0', &cb, &cbData,
487 if (!opt && !singleDash)
498 poptStripArg(con, thisopt);
509 if (handleAlias(con, NULL, *origOptString,
510 origOptString + 1)) {
514 if (handleExec(con, NULL, *origOptString))
517 opt = findOption(con->
options, NULL, *origOptString, &cb,
528 *((
int *)opt->
arg) = 1;
532 *((
int *)opt->
arg) = opt->
val;
540 con->
os->
nextArg = expandNextArg(con, longArg);
559 poptStripArg(con, con->
os->
next);
577 aLong = strtol(con->
os->
nextArg, &end, 0);
578 if (!(end && *end ==
'\0'))
581 if (aLong == LONG_MIN || aLong == LONG_MAX)
584 *((
long *)opt->
arg) = aLong;
587 if (aLong > INT_MAX || aLong < INT_MIN)
589 *((
int *)opt->
arg) = aLong;
594 fprintf(stdout,
POPT_(
"option type (%d) not implemented in popt\n"),
665 for (i = 0; i < con->
numExecs; i++) {
694 alias = con->
aliases + aliasNum;
717 #define POPT_ERROR_NOARG -10
718 #define POPT_ERROR_BADOPT -11
719 #define POPT_ERROR_OPTSTOODEEP -13
720 #define POPT_ERROR_BADQUOTE -15
721 #define POPT_ERROR_ERRNO -16
726 return POPT_(
"missing argument");
728 return POPT_(
"unknown option");
730 return POPT_(
"aliases nested too deeply");
732 return POPT_(
"error in paramter quoting");
734 return POPT_(
"invalid numeric value");
736 return POPT_(
"number too large or too small");
738 return strerror(errno);
740 return POPT_(
"unknown error");
771 int i, j = 1, numargs =
argc;
773 for (i = 1; i <
argc; i++) {
779 for (i = 1; i <
argc; i++) {
int poptAddAlias(poptContext con, struct poptAlias newAlias, int flags)
void poptSetExecPath(poptContext con, const char *path, int allowAbsolute)
const char * poptPeekArg(poptContext con)
#define POPT_ERROR_OPTSTOODEEP
#define POPT_ERROR_BADOPT
int poptStuffArgs(poptContext con, const char **argv)
const char * poptGetInvocationName(poptContext con)
const char ** poptGetArgs(poptContext con)
const char * poptGetOptArg(poptContext con)
#define POPT_ERROR_BADQUOTE
int poptStrippedArgv(poptContext con, int argc, char **argv)
void poptResetContext(poptContext con)
const char * poptStrerror(const int error)
int poptGetNextOpt(poptContext con)
void poptFreeContext(poptContext con)
const char * poptGetArg(poptContext con)
poptContext poptGetContext(const char *name, int argc, const char **argv, const struct poptOption *options, int flags)
const char * poptBadOption(poptContext con, int flags)
#define POPT_ERROR_OVERFLOW
#define POPT_CONTEXT_KEEP_FIRST
void(* poptCallbackType)(poptContext con, enum poptCallbackReason reason, const struct poptOption *opt, const char *arg, const void *data)
#define POPT_ARGFLAG_STRIP
#define POPT_ARGFLAG_ONEDASH
int poptDupArgv(int argc, const char **argv, int *argcPtr, const char ***argvPtr)
#define POPT_CONTEXT_NO_EXEC
#define POPT_BADOPTION_NOALIAS
#define POPT_CBFLAG_INC_DATA
#define POPT_ARG_INCLUDE_TABLE
#define POPT_OPTION_DEPTH
#define POPT_ARG_CALLBACK
struct poptContext_s * poptContext
#define POPT_CONTEXT_POSIXMEHARDER
#define POPT_ERROR_BADNUMBER
@ POPT_CALLBACK_REASON_PRE
@ POPT_CALLBACK_REASON_OPTION
@ POPT_CALLBACK_REASON_POST
struct poptAlias * currAlias
const struct poptOption * options
struct optionStackEntry * os
struct poptAlias * aliases
struct optionStackEntry optionStack[POPT_OPTION_DEPTH]
struct execEntry * doExec
char * xstrdup(const char *str)