13 #if defined(__LCLINT__) 15 extern long long int strtoll(
const char *nptr,
char **endptr,
38 #if !defined(HAVE_STRERROR) && !defined(__LCLINT__) 42 extern char * sys_errlist[];
44 if ((0 <= errno) && (errno < sys_nerr))
45 return sys_errlist[errno];
47 return POPT_(
"unknown errno");
53 static void prtcon(
const char *msg,
poptContext con)
55 if (msg) fprintf(stderr,
"%s", msg);
56 fprintf(stderr,
"\tcon %p os %p nextCharArg \"%s\" nextArg \"%s\" argv[%d] \"%s\"\n",
125 const void * myData,
int shorty)
138 if (opt->
arg != NULL)
148 if (cbopt == NULL || cbarg.
cb == NULL)
154 {
const void *cbData = (cbopt->
descrip ? cbopt->
descrip : myData);
169 const struct poptOption * options,
unsigned int flags)
173 if (con == NULL)
return NULL;
174 memset(con, 0,
sizeof(*con));
200 if (getenv(
"POSIXLY_CORRECT") || getenv(
"POSIX_ME_HARDER"))
225 if (con == NULL)
return;
267 for (i = con->
numExecs - 1; i >= 0; i--) {
268 item = con->
execs + i;
282 if (con->
doExec == NULL) {
297 {
char *s = malloc((longName ? strlen(longName) : 0) +
sizeof(
"--"));
322 const char *
longName,
size_t longNameLen)
325 const char * optLongName = opt->
longName;
328 if (optLongName == NULL || longName == NULL)
332 if (optLongName[0] ==
'n' && optLongName[1] ==
'o') {
333 optLongName +=
sizeof(
"no") - 1;
334 if (optLongName[0] ==
'-')
337 if (longName[0] ==
'n' && longName[1] ==
'o') {
338 longName +=
sizeof(
"no") - 1;
339 longNameLen -=
sizeof(
"no") - 1;
340 if (longName[0] ==
'-') {
346 rc = (int)(strlen(optLongName) == longNameLen);
348 rc = (int)(strncmp(optLongName, longName, longNameLen) == 0);
354 const char *
longName,
size_t longNameLen,
356 const char * nextArg)
393 if (longName == NULL && nextArg != NULL && *nextArg !=
'\0')
405 if (longName && nextArg != NULL && *nextArg !=
'\0') {
406 av = malloc((ac + 1 + 1) *
sizeof(*av));
408 for (i = 0; i < ac; i++) {
423 return (rc ? rc : 1);
435 char *path = NULL, *s = NULL, *se;
438 if (argv0 == NULL)
return NULL;
442 if (strchr(argv0,
'/'))
445 if ((path = getenv(
"PATH")) == NULL || (path =
xstrdup(path)) == NULL)
449 if ((t = malloc(strlen(path) + strlen(argv0) +
sizeof(
"/"))) != NULL)
450 for (s = path; s && *s; s = se) {
453 if ((se = strchr(s,
':')))
460 if (!access(t, X_OK))
466 if (!(s && *s) && t != NULL)
489 if (item->
argv == NULL || item->
argc < 1 ||
493 argv = malloc(
sizeof(*argv) *
497 if (!strchr(item->
argv[0],
'/') && con->
execPath != NULL) {
498 char *s = malloc(strlen(con->
execPath) + strlen(item->
argv[0]) +
sizeof(
"/"));
505 if (argv[argc++] == NULL) {
510 if (item->
argc > 1) {
511 memcpy(argv + argc, item->
argv + 1,
sizeof(*argv) * (item->
argc - 1));
512 argc += (item->
argc - 1);
528 #if defined(hpux) || defined(__hpux) 529 rc = setresgid(getgid(), getgid(),-1);
531 rc = setresuid(getuid(), getuid(),-1);
539 #if defined(HAVE_SETUID) 540 rc = setgid(getgid());
542 rc = setuid(getuid());
544 #elif defined (HAVE_SETREUID) 545 rc = setregid(getgid(), getgid());
547 rc = setreuid(getuid(), getuid());
557 fprintf(stderr,
"==> execvp(%s) argv[%d]:", argv[0], argc);
558 for (avp = argv; *avp; avp++)
559 fprintf(stderr,
" '%s'", *avp);
560 fprintf(stderr,
"\n");
565 rc = execvp(argv[0], (
char *
const *)argv);
571 free((
void *)argv[0]);
580 const char *
longName,
size_t longNameLen,
583 const void ** callbackData,
591 if (
LF_ISSET(ONEDASH) && !shortName && (longName && *longName ==
'\0'))
602 if (arg.
ptr == NULL)
continue;
603 opt2 =
findOption(arg.
opt, longName, longNameLen, shortName, callback,
604 callbackData, argInfo);
605 if (opt2 == NULL)
continue;
608 if (callback && *callback
609 && callbackData && *callbackData == NULL)
623 if (longName != NULL && opt->
longName != NULL &&
628 }
else if (shortName && shortName == opt->
shortName) {
638 *callback = (cb ? cbarg.
cb : NULL);
649 unsigned argx,
int delete_arg)
662 if (os->
argv != NULL)
663 for (i = os->
next; i < os->
argc; i++) {
667 if (*os->
argv[i] ==
'-')
674 if (os->
argb != NULL)
681 }
while (arg == NULL);
691 const char * a = NULL;
693 size_t tn = strlen(s) + 1;
697 if (t == NULL)
return NULL;
699 while ((c = *s++) !=
'\0') {
707 if (!(s[0] ==
'#' && s[1] ==
':' && s[2] ==
'+'))
714 s +=
sizeof(
"#:+") - 1;
717 {
size_t pos = (size_t) (te - t);
718 if ((t = realloc(t, tn)) == NULL)
733 if ((te = realloc(t, (
size_t)(te - t))) == NULL)
771 if (*bitsp == NULL) {
787 size_t ns = (s ? strlen(s) : 0);
791 if (bits == NULL || ns == 0)
797 uint32_t h = h0 + ns * h1;
806 size_t ns = (s ? strlen(s) : 0);
811 if (bits == NULL || ns == 0)
817 uint32_t h = h0 + ns * h1;
834 memset(bits, 0, nw * nbw);
840 size_t ns = (s ? strlen(s) : 0);
844 if (bits == NULL || ns == 0)
850 uint32_t h = h0 + ns * h1;
870 for (i = 0; i < nw; i++) {
871 abits[i] &= bbits[i];
890 for (i = 0; i < nw; i++) {
891 abits[i] |= bbits[i];
919 UNUSED(
unsigned int argInfo),
const char * s)
925 if (bitsp == NULL || s == NULL || *s ==
'\0' ||
_poptBitsNew(bitsp))
930 while ((t = te) != NULL && *t) {
931 while (*te !=
'\0' && *te !=
',')
954 UNUSED(
unsigned int argInfo),
const char * val)
958 if (argvp == NULL || val == NULL)
963 while ((*argvp)[argc] != NULL)
967 if ((*argvp =
xrealloc(*argvp, (argc + 1 + 1) *
sizeof(**argvp))) != NULL) {
968 (*argvp)[argc++] =
xstrdup(val);
969 (*argvp)[
argc ] = NULL;
983 || (((
unsigned long long)arg) & (
sizeof(*arg)-1))
988 if (aLongLong != 0 &&
LF_ISSET(RANDOM)) {
989 #if defined(HAVE_SRANDOM) 991 srandom((
unsigned)getpid());
992 srandom((
unsigned)random());
994 aLongLong = (
long long)(random() % (aLongLong > 0 ? aLongLong : -aLongLong));
1002 aLongLong = ~aLongLong;
1008 *(
unsigned long long *)arg |= (
unsigned long long)aLongLong;
1011 *(
unsigned long long *)arg &= (
unsigned long long)aLongLong;
1014 *(
unsigned long long *)arg ^= (
unsigned long long)aLongLong;
1026 if (arg == NULL || (((
unsigned long)arg) & (
sizeof(*arg)-1)))
1029 if (aLong != 0 &&
LF_ISSET(RANDOM)) {
1030 #if defined(HAVE_SRANDOM) 1032 srandom((
unsigned)getpid());
1033 srandom((
unsigned)random());
1035 aLong = random() % (aLong > 0 ? aLong : -aLong);
1045 case 0: *arg = aLong;
break;
1046 case POPT_ARGFLAG_OR: *(
unsigned long *)arg |= (
unsigned long)aLong;
break;
1047 case POPT_ARGFLAG_AND: *(
unsigned long *)arg &= (
unsigned long)aLong;
break;
1048 case POPT_ARGFLAG_XOR: *(
unsigned long *)arg ^= (
unsigned long)aLong;
break;
1059 if (arg == NULL || (((
unsigned long)arg) & (
sizeof(*arg)-1)))
1062 if (aLong != 0 &&
LF_ISSET(RANDOM)) {
1063 #if defined(HAVE_SRANDOM) 1065 srandom((
unsigned)getpid());
1066 srandom((
unsigned)random());
1068 aLong = random() % (aLong > 0 ? aLong : -aLong);
1078 case 0: *arg = (int) aLong;
break;
1079 case POPT_ARGFLAG_OR: *(
unsigned int *)arg |= (
unsigned int) aLong;
break;
1080 case POPT_ARGFLAG_AND: *(
unsigned int *)arg &= (
unsigned int) aLong;
break;
1081 case POPT_ARGFLAG_XOR: *(
unsigned int *)arg ^= (
unsigned int) aLong;
break;
1092 if (arg == NULL || (((
unsigned long)arg) & (
sizeof(*arg)-1)))
1095 if (aLong != 0 &&
LF_ISSET(RANDOM)) {
1096 #if defined(HAVE_SRANDOM) 1098 srandom((
unsigned)getpid());
1099 srandom((
unsigned)random());
1101 aLong = random() % (aLong > 0 ? aLong : -aLong);
1111 case 0: *arg = (short) aLong;
1113 case POPT_ARGFLAG_OR: *(
unsigned short *)arg |= (
unsigned short) aLong;
1134 unsigned int argInfo = opt->
argInfo;
1138 const char * longName = con->
os->
argv[con->
os->
next-1];
1139 while (*longName ==
'-') longName++;
1162 UNUSED(
unsigned int argInfo),
1168 *llp = strtoll(val, &end, 0);
1172 if (!(end && *end ==
'\0'))
1219 #if !defined(LLONG_MAX) 1220 # define LLONG_MAX 9223372036854775807LL 1221 # define LLONG_MIN (-LLONG_MAX - 1LL) 1228 rc = !(aNUM < (long long)LONG_MIN || aNUM > (
long long)LONG_MAX)
1233 rc = !(aNUM < (long long)INT_MIN || aNUM > (
long long)INT_MAX)
1238 rc = !(aNUM < (long long)SHRT_MIN || aNUM > (
long long)SHRT_MAX)
1248 double aDouble = 0.0;
1252 int saveerrno = errno;
1254 aDouble = strtod(con->
os->
nextArg, &end);
1255 if (errno == ERANGE) {
1272 #if !defined(DBL_EPSILON) && !defined(__LCLINT__) 1273 #define DBL_EPSILON 2.2204460492503131e-16 1275 #define POPT_ABS(a) ((((a) - 0.0) < DBL_EPSILON) ? -(a) : (a)) 1280 arg.
floatp[0] = (float) aDouble;
1290 fprintf(stdout,
POPT_(
"option type (%u) not implemented in popt\n"),
1307 const char * origOptString = NULL;
1309 const void * cbData = NULL;
1310 const char * longArg = NULL;
1334 const char * optString;
1335 size_t optStringLen;
1345 if (con->
os->
argv != NULL)
1348 if (origOptString == NULL)
1352 (*origOptString ==
'-' && origOptString[1] ==
'\0'))
1366 optString = origOptString;
1368 if (optString[0] ==
'\0')
1371 if (optString[1] ==
'-' && !optString[2]) {
1379 if (*optString ==
'-')
1385 for (oe = optString; *oe && *oe !=
'='; oe++)
1387 optStringLen = (size_t)(oe - optString);
1392 if (
handleAlias(con, optString, optStringLen,
'\0', longArg)) {
1425 if (
handleAlias(con, NULL, 0, *nextCharArg, nextCharArg + 1))
1431 if (*nextCharArg !=
'\0')
1443 if (*nextCharArg !=
'\0')
1444 con->
os->
nextCharArg = nextCharArg + (int)(*nextCharArg ==
'=');
1486 &&
F_ISSET(opt, STRIP) && canstrip)
1491 if (con->
os->
argv != NULL) {
1523 {
char *s = malloc((opt->
longName ? strlen(opt->
longName) : 0) +
sizeof(
"--"));
1550 return (opt ? opt->
val : -1);
1565 const char * ret = NULL;
1573 const char * ret = NULL;
1597 if (items != NULL) {
1599 while (--nitems >= 0) {
1608 items =
_free(items);
1615 if (con == NULL)
return con;
1641 memset(item, 0,
sizeof(*item));
1642 item->option.longName = alias.
longName;
1643 item->option.shortName = alias.
shortName;
1645 item->option.arg = 0;
1646 item->option.val = 0;
1647 item->option.descrip = NULL;
1648 item->option.argDescrip = NULL;
1649 item->argc = alias.
argc;
1650 item->argv = alias.
argv;
1661 items = &con->
execs;
1673 *items = realloc((*items), ((*nitems) + 1) *
sizeof(**items));
1674 if ((*items) == NULL)
1677 item = (*items) + (*nitems);
1704 return (os != NULL && os->
argv != NULL ? os->
argv[os->
next - 1] : NULL);
1711 return POPT_(
"missing argument");
1713 return POPT_(
"unknown option");
1715 return POPT_(
"mutually exclusive logical operations requested");
1717 return POPT_(
"opt->arg should not be NULL");
1719 return POPT_(
"aliases nested too deeply");
1721 return POPT_(
"error in parameter quoting");
1723 return POPT_(
"invalid numeric value");
1725 return POPT_(
"number too large or too small");
1727 return POPT_(
"memory allocation failed");
1729 return POPT_(
"config file failed sanity test");
1733 return POPT_(
"unknown error");
1745 for (argc = 0; argv[
argc]; argc++)
1773 for (i = 1; i <
argc; i++) {
1778 for (i = 1; i <
argc; i++) {
1781 argv[j] = (j < numargs) ? argv[i] : NULL;
static const char * expandNextArg(poptContext con, const char *s)
static void invokeCallbacksOPTION(poptContext con, const struct poptOption *opt, const struct poptOption *myOpt, const void *myData, int shorty)
#define POPT_CONTEXT_ARG_OPTS
const char ** poptGetArgs(poptContext con)
Return remaining arguments.
A popt alias argument for poptAddAlias().
#define POPT_ERROR_BADQUOTE
#define poptArgType(_opt)
int poptSaveBits(poptBits *bitsp, unsigned int argInfo, const char *s)
Save a string into a bit set (experimental).
const char * poptGetInvocationName(poptContext con)
Return argv[0] from context.
#define POPT_ARG_LONGLONG
char * poptGetOptArg(poptContext con)
Return next option argument (if any).
#define poptSubstituteHelpI18N(opt)
#define POPT_BADOPTION_NOALIAS
static poptItem poptFreeItems(poptItem items, int nitems)
int poptBitsClr(poptBits bits)
static char * stpcpy(char *dest, const char *src)
A popt alias or exec argument for poptAddItem().
int poptBitsArgs(poptContext con, poptBits *ap)
const char * poptBadOption(poptContext con, unsigned int flags)
Return the option which caused the most recent error.
#define POPT_ERROR_NULLARG
#define POPT_ERROR_BADCONFIG
static int _poptBitsNew(poptBits *bitsp)
void poptJlu32lpair(const void *key, size_t size, uint32_t *pc, uint32_t *pb)
#define POPT_ERROR_BADOPERATION
#define POPT_CONTEXT_NO_EXEC
int(* maincall)(int argc, const char **argv)
poptContext poptGetContext(const char *name, int argc, const char **argv, const struct poptOption *options, unsigned int flags)
Initialize popt context.
void poptResetContext(poptContext con)
Reinitialize popt context.
#define POPT_ERROR_BADOPT
const char * poptGetArg(poptContext con)
Return next argument.
#define POPT_CONTEXT_POSIXMEHARDER
int poptSaveShort(short *arg, unsigned int argInfo, long aLong)
Save a short integer, performing logical operation with value.
unsigned int _poptGroupMask
int poptGetNextOpt(poptContext con)
Return value of next option found.
#define POPT_ERROR_BADNUMBER
#define POPT_ARG_INCLUDE_TABLE
int poptDupArgv(int argc, const char **argv, int *argcPtr, const char ***argvPtr)
Duplicate an argument array.
#define POPT_ERROR_OPTSTOODEEP
static int poptParseInteger(long long *llp, unsigned int argInfo, const char *val)
Parse an integer expression.
struct optionStackEntry optionStack[POPT_OPTION_DEPTH]
static void invokeCallbacksPOST(poptContext con, const struct poptOption *opt)
int poptBitsChk(poptBits bits, const char *s)
int poptAddAlias(poptContext con, struct poptAlias alias, int flags)
Add alias to context.
#define POPT_ARG_MAINCALL
struct optionStackEntry * os
static int handleAlias(poptContext con, const char *longName, size_t longNameLen, char shortName, const char *nextArg)
static int handleExec(poptContext con, const char *longName, char shortName)
void(* poptCallbackType)(poptContext con, enum poptCallbackReason reason, const struct poptOption *opt, const char *arg, const void *data)
Table callback prototype.
const struct poptOption * options
static unsigned int poptArgInfo(poptContext con, const struct poptOption *opt)
Return argInfo field, handling POPT_ARGFLAG_TOGGLE overrides.
void * xrealloc(void *ptr, size_t size)
A union to simplify opt->arg access without casting.
#define CBF_ISSET(_opt, _FLAG)
int poptStuffArgs(poptContext con, const char **argv)
Add arguments to context.
const char * poptStrerror(const int error)
Return formatted error string for popt failure.
#define POPT_ARGFLAG_DOC_HIDDEN
int poptBitsIntersect(poptBits *ap, const poptBits b)
static const char * findNextArg(poptContext con, unsigned argx, int delete_arg)
int poptSaveInt(int *arg, unsigned int argInfo, long aLong)
Save an integer, performing logical operation with value.
unsigned int _poptArgMask
int poptStrippedArgv(poptContext con, int argc, char **argv)
Shuffle argv pointers to remove stripped args, returns new argc.
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
static const char * findProgramPath(const char *argv0)
Return absolute path to executable by searching PATH.
#define F_ISSET(_opt, _FLAG)
int poptSaveString(const char ***argvp, unsigned int argInfo, const char *val)
Add a string to an argv array.
static void invokeCallbacksPRE(poptContext con, const struct poptOption *opt)
int poptBitsDel(poptBits bits, const char *s)
#define POPT_ARG_CALLBACK
#define POPT_ERROR_OVERFLOW
poptContext poptFreeContext(poptContext con)
Destroy context.
static int longOptionStrcmp(const struct poptOption *opt, const char *longName, size_t longNameLen)
Compare long option for equality, adjusting for POPT_ARGFLAG_TOGGLE.
#define POPT_OPTION_DEPTH
char * xstrdup(const char *str)
static int poptSaveArg(poptContext con, const struct poptOption *opt)
Save the option argument through the (*opt->arg) pointer.
void poptSetExecPath(poptContext con, const char *path, int allowAbsolute)
Limit search for executables.
static int execCommand(poptContext con)
#define POPT_CONTEXT_KEEP_FIRST
const char * poptPeekArg(poptContext con)
Peek at current argument.
int poptAddItem(poptContext con, poptItem newItem, int flags)
Add alias/exec item to context.
int poptBitsUnion(poptBits *ap, const poptBits b)
static const struct poptOption * findOption(const struct poptOption *opt, const char *longName, size_t longNameLen, char shortName, poptCallbackType *callback, const void **callbackData, unsigned int argInfo)
int poptSaveLong(long *arg, unsigned int argInfo, long aLong)
Save a long, performing logical operation with value.
#define POPT_ERROR_MALLOC
int poptSaveLongLong(long long *arg, unsigned int argInfo, long long aLongLong)
Save a long long, performing logical operation with value.
#define POPT_ARGFLAG_ONEDASH
static void poptStripArg(poptContext con, int which)
int poptBitsAdd(poptBits bits, const char *s)
static char * strerror(int errno)
static void cleanOSE(struct optionStackEntry *os)