mirror of
https://gitlab.com/openconnect/ocserv.git
synced 2026-10-14 02:22:03 +08:00
updated libopts
This commit is contained in:
+82
-78
@@ -2,13 +2,15 @@
|
||||
/*
|
||||
* \file sort.c
|
||||
*
|
||||
* Time-stamp: "2011-05-24 18:07:14 bkorb"
|
||||
*
|
||||
* This module implements argument sorting.
|
||||
*
|
||||
* @addtogroup autoopts
|
||||
* @{
|
||||
*/
|
||||
/*
|
||||
* This file is part of AutoOpts, a companion to AutoGen.
|
||||
* AutoOpts is free software.
|
||||
* AutoOpts is Copyright (c) 1992-2012 by Bruce Korb - all rights reserved
|
||||
* AutoOpts is Copyright (C) 1992-2013 by Bruce Korb - all rights reserved
|
||||
*
|
||||
* AutoOpts is available under any one of two licenses. The license
|
||||
* in use must be one of these two and the choice is under the control
|
||||
@@ -20,35 +22,35 @@
|
||||
* The Modified Berkeley Software Distribution License
|
||||
* See the file "COPYING.mbsd"
|
||||
*
|
||||
* These files have the following md5sums:
|
||||
* These files have the following sha256 sums:
|
||||
*
|
||||
* 43b91e8ca915626ed3818ffb1b71248b pkg/libopts/COPYING.gplv3
|
||||
* 06a1a2e4760c90ea5e1dad8dfaac4d39 pkg/libopts/COPYING.lgplv3
|
||||
* 66a5cedaf62c4b2637025f049f9b826f pkg/libopts/COPYING.mbsd
|
||||
* 8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95 COPYING.gplv3
|
||||
* 4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b COPYING.lgplv3
|
||||
* 13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239 COPYING.mbsd
|
||||
*/
|
||||
|
||||
/* = = = START-STATIC-FORWARD = = = */
|
||||
static tSuccess
|
||||
mustHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
char** ppzOpts, int* pOptsIdx);
|
||||
must_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
|
||||
char ** opt_txt, uint32_t * opt_idx);
|
||||
|
||||
static tSuccess
|
||||
mayHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
char** ppzOpts, int* pOptsIdx);
|
||||
maybe_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
|
||||
char ** opt_txt, uint32_t * opt_idx);
|
||||
|
||||
static tSuccess
|
||||
checkShortOpts(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
char** ppzOpts, int* pOptsIdx);
|
||||
short_opt_ck(tOptions * opts, char * arg_txt, tOptState * pOS,
|
||||
char ** opt_txt, uint32_t * opt_idx);
|
||||
/* = = = END-STATIC-FORWARD = = = */
|
||||
|
||||
/*
|
||||
* "mustHandleArg" and "mayHandleArg" are really similar. The biggest
|
||||
* "must_arg" and "maybe_arg" are really similar. The biggest
|
||||
* difference is that "may" will consume the next argument only if it
|
||||
* does not start with a hyphen and "must" will consume it, hyphen or not.
|
||||
*/
|
||||
static tSuccess
|
||||
mustHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
char** ppzOpts, int* pOptsIdx)
|
||||
must_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
|
||||
char ** opt_txt, uint32_t * opt_idx)
|
||||
{
|
||||
/*
|
||||
* An option argument is required. Long options can either have
|
||||
@@ -61,7 +63,7 @@ mustHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
* See if an arg string follows the flag character. If not,
|
||||
* the next arg must be the option argument.
|
||||
*/
|
||||
if (*pzArg != NUL)
|
||||
if (*arg_txt != NUL)
|
||||
return SUCCESS;
|
||||
break;
|
||||
|
||||
@@ -77,16 +79,16 @@ mustHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
default:
|
||||
return FAILURE;
|
||||
}
|
||||
if (pOpts->curOptIdx >= pOpts->origArgCt)
|
||||
if (opts->curOptIdx >= opts->origArgCt)
|
||||
return FAILURE;
|
||||
|
||||
ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
|
||||
opt_txt[ (*opt_idx)++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
static tSuccess
|
||||
mayHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
char** ppzOpts, int* pOptsIdx)
|
||||
maybe_arg(tOptions * opts, char * arg_txt, tOptState * pOS,
|
||||
char ** opt_txt, uint32_t * opt_idx)
|
||||
{
|
||||
/*
|
||||
* An option argument is optional.
|
||||
@@ -98,7 +100,7 @@ mayHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
* THEN see if there is another argument. If so and if it
|
||||
* does *NOT* start with a hyphen, then it is the option arg.
|
||||
*/
|
||||
if (*pzArg != NUL)
|
||||
if (*arg_txt != NUL)
|
||||
return SUCCESS;
|
||||
break;
|
||||
|
||||
@@ -114,12 +116,12 @@ mayHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
default:
|
||||
return FAILURE;
|
||||
}
|
||||
if (pOpts->curOptIdx >= pOpts->origArgCt)
|
||||
if (opts->curOptIdx >= opts->origArgCt)
|
||||
return PROBLEM;
|
||||
|
||||
pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
|
||||
if (*pzArg != '-')
|
||||
ppzOpts[ (*pOptsIdx)++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
|
||||
arg_txt = opts->origArgVect[ opts->curOptIdx ];
|
||||
if (*arg_txt != '-')
|
||||
opt_txt[ (*opt_idx)++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
|
||||
return SUCCESS;
|
||||
}
|
||||
|
||||
@@ -128,31 +130,31 @@ mayHandleArg(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
* does or may take an argument, the do the argument processing and leave.
|
||||
*/
|
||||
static tSuccess
|
||||
checkShortOpts(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
char** ppzOpts, int* pOptsIdx)
|
||||
short_opt_ck(tOptions * opts, char * arg_txt, tOptState * pOS,
|
||||
char ** opt_txt, uint32_t * opt_idx)
|
||||
{
|
||||
while (*pzArg != NUL) {
|
||||
if (FAILED(opt_find_short(pOpts, (tAoUC)*pzArg, pOS)))
|
||||
while (*arg_txt != NUL) {
|
||||
if (FAILED(opt_find_short(opts, (uint8_t)*arg_txt, pOS)))
|
||||
return FAILURE;
|
||||
|
||||
/*
|
||||
* See if we can have an arg.
|
||||
*/
|
||||
if (OPTST_GET_ARGTYPE(pOS->pOD->fOptState) == OPARG_TYPE_NONE) {
|
||||
pzArg++;
|
||||
arg_txt++;
|
||||
|
||||
} else if (pOS->pOD->fOptState & OPTST_ARG_OPTIONAL) {
|
||||
/*
|
||||
* Take an argument if it is not attached and it does not
|
||||
* start with a hyphen.
|
||||
*/
|
||||
if (pzArg[1] != NUL)
|
||||
if (arg_txt[1] != NUL)
|
||||
return SUCCESS;
|
||||
|
||||
pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
|
||||
if (*pzArg != '-')
|
||||
ppzOpts[ (*pOptsIdx)++ ] =
|
||||
pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
|
||||
arg_txt = opts->origArgVect[ opts->curOptIdx ];
|
||||
if (*arg_txt != '-')
|
||||
opt_txt[ (*opt_idx)++ ] =
|
||||
opts->origArgVect[ (opts->curOptIdx)++ ];
|
||||
return SUCCESS;
|
||||
|
||||
} else {
|
||||
@@ -160,11 +162,11 @@ checkShortOpts(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
* IF we need another argument, be sure it is there and
|
||||
* take it.
|
||||
*/
|
||||
if (pzArg[1] == NUL) {
|
||||
if (pOpts->curOptIdx >= pOpts->origArgCt)
|
||||
if (arg_txt[1] == NUL) {
|
||||
if (opts->curOptIdx >= opts->origArgCt)
|
||||
return FAILURE;
|
||||
ppzOpts[ (*pOptsIdx)++ ] =
|
||||
pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
|
||||
opt_txt[ (*opt_idx)++ ] =
|
||||
opts->origArgVect[ (opts->curOptIdx)++ ];
|
||||
}
|
||||
return SUCCESS;
|
||||
}
|
||||
@@ -177,12 +179,12 @@ checkShortOpts(tOptions* pOpts, char* pzArg, tOptState* pOS,
|
||||
* then this routine will to the trick.
|
||||
*/
|
||||
LOCAL void
|
||||
optionSort(tOptions* pOpts)
|
||||
optionSort(tOptions * opts)
|
||||
{
|
||||
char** ppzOpts;
|
||||
char** ppzOpds;
|
||||
int optsIdx = 0;
|
||||
int opdsIdx = 0;
|
||||
char ** opt_txt;
|
||||
char ** ppzOpds;
|
||||
uint32_t optsIdx = 0;
|
||||
uint32_t opdsIdx = 0;
|
||||
|
||||
tOptState os = OPTSTATE_INITIALIZER(DEFINED);
|
||||
|
||||
@@ -190,24 +192,24 @@ optionSort(tOptions* pOpts)
|
||||
* Disable for POSIX conformance, or if there are no operands.
|
||||
*/
|
||||
if ( (getenv("POSIXLY_CORRECT") != NULL)
|
||||
|| NAMED_OPTS(pOpts))
|
||||
|| NAMED_OPTS(opts))
|
||||
return;
|
||||
|
||||
/*
|
||||
* Make sure we can allocate two full-sized arg vectors.
|
||||
*/
|
||||
ppzOpts = malloc(pOpts->origArgCt * sizeof(char*));
|
||||
if (ppzOpts == NULL)
|
||||
opt_txt = malloc(opts->origArgCt * sizeof(char*));
|
||||
if (opt_txt == NULL)
|
||||
goto exit_no_mem;
|
||||
|
||||
ppzOpds = malloc(pOpts->origArgCt * sizeof(char*));
|
||||
ppzOpds = malloc(opts->origArgCt * sizeof(char*));
|
||||
if (ppzOpds == NULL) {
|
||||
free(ppzOpts);
|
||||
free(opt_txt);
|
||||
goto exit_no_mem;
|
||||
}
|
||||
|
||||
pOpts->curOptIdx = 1;
|
||||
pOpts->pzCurOpt = NULL;
|
||||
opts->curOptIdx = 1;
|
||||
opts->pzCurOpt = NULL;
|
||||
|
||||
/*
|
||||
* Now, process all the options from our current position onward.
|
||||
@@ -215,30 +217,30 @@ optionSort(tOptions* pOpts)
|
||||
* non-standard programs that require it.)
|
||||
*/
|
||||
for (;;) {
|
||||
char* pzArg;
|
||||
char * arg_txt;
|
||||
tSuccess res;
|
||||
|
||||
/*
|
||||
* If we're out of arguments, we're done. Join the option and
|
||||
* operand lists into the original argument vector.
|
||||
*/
|
||||
if (pOpts->curOptIdx >= pOpts->origArgCt) {
|
||||
if (opts->curOptIdx >= opts->origArgCt) {
|
||||
errno = 0;
|
||||
goto joinLists;
|
||||
}
|
||||
|
||||
pzArg = pOpts->origArgVect[ pOpts->curOptIdx ];
|
||||
if (*pzArg != '-') {
|
||||
ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
|
||||
arg_txt = opts->origArgVect[ opts->curOptIdx ];
|
||||
if (*arg_txt != '-') {
|
||||
ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (pzArg[1]) {
|
||||
switch (arg_txt[1]) {
|
||||
case NUL:
|
||||
/*
|
||||
* A single hyphen is an operand.
|
||||
*/
|
||||
ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
|
||||
ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
|
||||
continue;
|
||||
|
||||
case '-':
|
||||
@@ -246,12 +248,12 @@ optionSort(tOptions* pOpts)
|
||||
* Two consecutive hypens. Put them on the options list and then
|
||||
* _always_ force the remainder of the arguments to be operands.
|
||||
*/
|
||||
if (pzArg[2] == NUL) {
|
||||
ppzOpts[ optsIdx++ ] =
|
||||
pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
|
||||
if (arg_txt[2] == NUL) {
|
||||
opt_txt[ optsIdx++ ] =
|
||||
opts->origArgVect[ (opts->curOptIdx)++ ];
|
||||
goto restOperands;
|
||||
}
|
||||
res = opt_find_long(pOpts, pzArg+2, &os);
|
||||
res = opt_find_long(opts, arg_txt+2, &os);
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -260,10 +262,10 @@ optionSort(tOptions* pOpts)
|
||||
* option processing. Otherwise the character must be a
|
||||
* short (i.e. single character) option.
|
||||
*/
|
||||
if ((pOpts->fOptSet & OPTPROC_SHORTOPT) == 0) {
|
||||
res = opt_find_long(pOpts, pzArg+1, &os);
|
||||
if ((opts->fOptSet & OPTPROC_SHORTOPT) == 0) {
|
||||
res = opt_find_long(opts, arg_txt+1, &os);
|
||||
} else {
|
||||
res = opt_find_short(pOpts, (tAoUC)pzArg[1], &os);
|
||||
res = opt_find_short(opts, (uint8_t)arg_txt[1], &os);
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -277,7 +279,7 @@ optionSort(tOptions* pOpts)
|
||||
* Next, we have to see if we need to pull another argument to be
|
||||
* used as the option argument.
|
||||
*/
|
||||
ppzOpts[ optsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
|
||||
opt_txt[ optsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
|
||||
|
||||
if (OPTST_GET_ARGTYPE(os.pOD->fOptState) == OPARG_TYPE_NONE) {
|
||||
/*
|
||||
@@ -286,20 +288,20 @@ optionSort(tOptions* pOpts)
|
||||
* of the argument string.
|
||||
*/
|
||||
if ( (os.optType == TOPT_SHORT)
|
||||
&& FAILED(checkShortOpts(pOpts, pzArg+2, &os, ppzOpts,
|
||||
&optsIdx)) ) {
|
||||
&& FAILED(short_opt_ck(opts, arg_txt+2, &os, opt_txt,
|
||||
&optsIdx)) ) {
|
||||
errno = EINVAL;
|
||||
goto freeTemps;
|
||||
}
|
||||
|
||||
} else if (os.pOD->fOptState & OPTST_ARG_OPTIONAL) {
|
||||
switch (mayHandleArg(pOpts, pzArg+2, &os, ppzOpts, &optsIdx)) {
|
||||
switch (maybe_arg(opts, arg_txt+2, &os, opt_txt, &optsIdx)) {
|
||||
case FAILURE: errno = EIO; goto freeTemps;
|
||||
case PROBLEM: errno = 0; goto joinLists;
|
||||
}
|
||||
|
||||
} else {
|
||||
switch (mustHandleArg(pOpts, pzArg+2, &os, ppzOpts, &optsIdx)) {
|
||||
switch (must_arg(opts, arg_txt+2, &os, opt_txt, &optsIdx)) {
|
||||
case PROBLEM:
|
||||
case FAILURE: errno = EIO; goto freeTemps;
|
||||
}
|
||||
@@ -307,18 +309,19 @@ optionSort(tOptions* pOpts)
|
||||
} /* for (;;) */
|
||||
|
||||
restOperands:
|
||||
while (pOpts->curOptIdx < pOpts->origArgCt)
|
||||
ppzOpds[ opdsIdx++ ] = pOpts->origArgVect[ (pOpts->curOptIdx)++ ];
|
||||
while (opts->curOptIdx < opts->origArgCt)
|
||||
ppzOpds[ opdsIdx++ ] = opts->origArgVect[ (opts->curOptIdx)++ ];
|
||||
|
||||
joinLists:
|
||||
if (optsIdx > 0)
|
||||
memcpy(pOpts->origArgVect + 1, ppzOpts, optsIdx * sizeof(char*));
|
||||
memcpy(opts->origArgVect + 1, opt_txt,
|
||||
(size_t)optsIdx * sizeof(char*));
|
||||
if (opdsIdx > 0)
|
||||
memcpy(pOpts->origArgVect + 1 + optsIdx, ppzOpds,
|
||||
opdsIdx * sizeof(char*));
|
||||
memcpy(opts->origArgVect + 1 + optsIdx, ppzOpds,
|
||||
(size_t)opdsIdx * sizeof(char*));
|
||||
|
||||
freeTemps:
|
||||
free(ppzOpts);
|
||||
free(opt_txt);
|
||||
free(ppzOpds);
|
||||
return;
|
||||
|
||||
@@ -327,7 +330,8 @@ optionSort(tOptions* pOpts)
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
/** @}
|
||||
*
|
||||
* Local Variables:
|
||||
* mode: C
|
||||
* c-file-style: "stroustrup"
|
||||
|
||||
Reference in New Issue
Block a user