static char const zNil[] = "";
static tSuccess
findOptDesc( tOptions* pOpts, tOptState* pOptState );
static tSuccess
next_opt_arg_must(tOptions* pOpts, tOptState* pOptState);
static tSuccess
next_opt_arg_may(tOptions* pOpts, tOptState* pOptState);
static tSuccess
next_opt_arg_none(tOptions* pOpts, tOptState* pOptState);
static tSuccess
nextOption(tOptions* pOpts, tOptState* pOptState);
static tSuccess
doPresets( tOptions* pOpts );
static int
checkConsistency( tOptions* pOpts );
LOCAL void *
ao_malloc( size_t sz )
{
void * res = malloc(sz);
if (res == NULL) {
fprintf( stderr, "malloc of %d bytes failed\n", (int)sz );
exit( EXIT_FAILURE );
}
return res;
}
#undef malloc
#define malloc(_s) ao_malloc(_s)
LOCAL void *
ao_realloc( void *p, size_t sz )
{
void * res = realloc(p, sz);
if (res == NULL) {
fprintf( stderr, "realloc of %d bytes at 0x%p failed\n", (int)sz, p );
exit( EXIT_FAILURE );
}
return res;
}
#undef realloc
#define realloc(_p,_s) ao_realloc(_p,_s)
LOCAL void
ao_free( void *p )
{
if (p != NULL)
free(p);
}
#undef free
#define free(_p) ao_free(_p)
LOCAL char *
ao_strdup( char const *str )
{
char * res = strdup(str);
if (res == NULL) {
fprintf(stderr, "strdup of %d byte string failed\n", (int)strlen(str));
exit( EXIT_FAILURE );
}
return res;
}
#undef strdup
#define strdup(_p) ao_strdup(_p)
#ifndef HAVE_PATHFIND
# include "compat/pathfind.c"
#endif
#ifndef HAVE_SNPRINTF
# include "compat/snprintf.c"
#endif
#ifndef HAVE_STRDUP
# include "compat/strdup.c"
#endif
#ifndef HAVE_STRCHR
# include "compat/strchr.c"
#endif
LOCAL tSuccess
handleOption( tOptions* pOpts, tOptState* pOptState )
{
tOptDesc* pOD = pOptState->pOD;
tOptProc* pOP = pOD->pOptProc;
if (pOD->fOptState & OPTST_ALLOC_ARG)
AGFREE(pOD->optArg.argString);
pOD->optArg.argString = pOptState->pzOptArg;
if ( ((pOpts->fOptSet & OPTPROC_PRESETTING) != 0)
&& ((pOD->fOptState & OPTST_NO_INIT) != 0)
)
return PROBLEM;
if (pOD->optEquivIndex != NO_EQUIVALENT) {
tOptDesc* p = pOpts->pOptDesc + pOD->optEquivIndex;
if ((pOD->fOptState & OPTST_DEFINED) != 0) {
if (p->optActualIndex != pOD->optIndex) {
fprintf( stderr, (char*)zMultiEquiv, p->pz_Name, pOD->pz_Name,
(pOpts->pOptDesc + p->optActualIndex)->pz_Name);
return FAILURE;
}
} else {
p->optActualIndex = NO_EQUIVALENT;
}
if (p->optActualIndex != pOD->optIndex) {
p->optActualValue = pOD->optValue;
p->optActualIndex = pOD->optIndex;
pOptState->flags |= OPTST_EQUIVALENCE;
}
p->optArg.argString = pOD->optArg.argString;
pOD = p;
} else {
pOD->optActualValue = pOD->optValue;
pOD->optActualIndex = pOD->optIndex;
}
pOD->fOptState &= OPTST_PERSISTENT_MASK;
pOD->fOptState |= (pOptState->flags & ~OPTST_PERSISTENT_MASK);
if ( (pOD->fOptState & OPTST_DEFINED)
&& (++pOD->optOccCt > pOD->optMaxCt) ) {
if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
char const * pzEqv =
(pOD->optEquivIndex != NO_EQUIVALENT) ? zEquiv : zNil;
fputs( zErrOnly, stderr );
if (pOD->optMaxCt > 1)
fprintf(stderr, zAtMost, pOD->optMaxCt, pOD->pz_Name, pzEqv);
else
fprintf(stderr, zOnlyOne, pOD->pz_Name, pzEqv);
}
return FAILURE;
}
if (pOP != NULL)
(*pOP)( pOpts, pOD );
return SUCCESS;
}
LOCAL tSuccess
longOptionFind( tOptions* pOpts, char* pzOptName, tOptState* pOptState )
{
ag_bool disable = AG_FALSE;
char* pzEq = strchr( pzOptName, '=' );
tOptDesc* pOD = pOpts->pOptDesc;
int idx = 0;
int idxLim = pOpts->optCt;
int matchCt = 0;
int matchIdx = 0;
int nameLen;
char opt_name_buf[128];
if (pzEq != NULL) {
nameLen = (int)(pzEq - pzOptName);
if (nameLen >= sizeof(opt_name_buf))
return FAILURE;
memcpy(opt_name_buf, pzOptName, nameLen);
opt_name_buf[nameLen] = NUL;
pzOptName = opt_name_buf;
pzEq++;
} else nameLen = strlen( pzOptName );
do {
if (SKIP_OPT(pOD)) {
if ( (pOD->fOptState != (OPTST_OMITTED | OPTST_NO_INIT))
|| (pOD->pz_Name == NULL))
continue;
}
else assert(pOD->pz_Name != NULL);
if (strneqvcmp( pzOptName, pOD->pz_Name, nameLen ) == 0) {
if (pOD->pz_Name[ nameLen ] == NUL) {
matchCt = 1;
matchIdx = idx;
break;
}
}
else if ( (pOD->pz_DisableName != NULL)
&& (strneqvcmp(pzOptName, pOD->pz_DisableName, nameLen) == 0)
) {
disable = AG_TRUE;
if (pOD->pz_DisableName[ nameLen ] == NUL) {
matchCt = 1;
matchIdx = idx;
break;
}
}
else
continue;
matchIdx = idx;
if (++matchCt > 1)
break;
} while (pOD++, (++idx < idxLim));
if (matchCt == 1) {
pOD = pOpts->pOptDesc + matchIdx;
if (SKIP_OPT(pOD)) {
fprintf(stderr, zDisabledErr, pOpts->pzProgName, pOD->pz_Name);
if (pOD->pzText != NULL)
fprintf(stderr, " -- %s", pOD->pzText);
fputc('\n', stderr);
(*pOpts->pUsageProc)(pOpts, EXIT_FAILURE);
}
if (disable)
pOptState->flags |= OPTST_DISABLED;
pOptState->pOD = pOD;
pOptState->pzOptArg = pzEq;
pOptState->optType = TOPT_LONG;
return SUCCESS;
}
if ( (pzEq == NULL)
&& NAMED_OPTS(pOpts)
&& (pOpts->specOptIdx.default_opt != NO_EQUIVALENT)) {
pOptState->pOD = pOpts->pOptDesc + pOpts->specOptIdx.default_opt;
pOptState->pzOptArg = pzOptName;
pOptState->optType = TOPT_DEFAULT;
return SUCCESS;
}
if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
fprintf(stderr, (matchCt == 0) ? zIllOptStr : zAmbigOptStr,
pOpts->pzProgPath, pzOptName);
(*pOpts->pUsageProc)(pOpts, EXIT_FAILURE);
}
return FAILURE;
}
LOCAL tSuccess
shortOptionFind( tOptions* pOpts, uint_t optValue, tOptState* pOptState )
{
tOptDesc* pRes = pOpts->pOptDesc;
int ct = pOpts->optCt;
do {
if (optValue != pRes->optValue)
continue;
if (SKIP_OPT(pRes)) {
if ( (pRes->fOptState == (OPTST_OMITTED | OPTST_NO_INIT))
&& (pRes->pz_Name != NULL)) {
fprintf(stderr, zDisabledErr, pOpts->pzProgPath, pRes->pz_Name);
if (pRes->pzText != NULL)
fprintf(stderr, " -- %s", pRes->pzText);
fputc('\n', stderr);
(*pOpts->pUsageProc)(pOpts, EXIT_FAILURE);
}
goto short_opt_error;
}
pOptState->pOD = pRes;
pOptState->optType = TOPT_SHORT;
return SUCCESS;
} while (pRes++, --ct > 0);
if ( IS_DEC_DIGIT_CHAR(optValue)
&& (pOpts->specOptIdx.number_option != NO_EQUIVALENT) ) {
pOptState->pOD = \
pRes = pOpts->pOptDesc + pOpts->specOptIdx.number_option;
(pOpts->pzCurOpt)--;
pOptState->optType = TOPT_SHORT;
return SUCCESS;
}
short_opt_error:
if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
fprintf( stderr, zIllOptChr, pOpts->pzProgPath, optValue );
(*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
}
return FAILURE;
}
static tSuccess
findOptDesc( tOptions* pOpts, tOptState* pOptState )
{
if ((pOpts->pzCurOpt != NULL) && (*pOpts->pzCurOpt != NUL))
return shortOptionFind( pOpts, (tAoUC)*(pOpts->pzCurOpt), pOptState );
if (pOpts->curOptIdx >= pOpts->origArgCt)
return PROBLEM;
pOpts->pzCurOpt = pOpts->origArgVect[ pOpts->curOptIdx ];
if (NAMED_OPTS(pOpts)) {
char * pz = pOpts->pzCurOpt;
int def;
tSuccess res;
tAoUS * def_opt;
pOpts->curOptIdx++;
if (*pz != '-')
return longOptionFind(pOpts, pz, pOptState);
while (*(++pz) == '-') ;
def_opt = (void *)&(pOpts->specOptIdx.default_opt);
def = *def_opt;
*def_opt = NO_EQUIVALENT;
res = longOptionFind(pOpts, pz, pOptState);
*def_opt = def;
return res;
}
if (*((pOpts->pzCurOpt)++) != '-')
return PROBLEM;
if (*(pOpts->pzCurOpt) == NUL)
return PROBLEM;
pOpts->curOptIdx++;
if (pOpts->pzCurOpt[0] == '-') {
if (*++(pOpts->pzCurOpt) == NUL)
return PROBLEM;
if ((pOpts->fOptSet & OPTPROC_LONGOPT) == 0) {
fprintf( stderr, zIllOptStr, pOpts->pzProgPath,
zIllegal, pOpts->pzCurOpt-2 );
return FAILURE;
}
return longOptionFind( pOpts, pOpts->pzCurOpt, pOptState );
}
if ((pOpts->fOptSet & OPTPROC_SHORTOPT) != 0)
return shortOptionFind( pOpts, (tAoUC)*(pOpts->pzCurOpt), pOptState );
return longOptionFind( pOpts, pOpts->pzCurOpt, pOptState );
}
static tSuccess
next_opt_arg_must(tOptions* pOpts, tOptState* pOptState)
{
switch (pOptState->optType) {
case TOPT_SHORT:
if (*++(pOpts->pzCurOpt) == NUL)
pOpts->pzCurOpt = pOpts->origArgVect[ pOpts->curOptIdx++ ];
pOptState->pzOptArg = pOpts->pzCurOpt;
break;
case TOPT_LONG:
if (pOptState->pzOptArg == NULL)
pOptState->pzOptArg = pOpts->origArgVect[ pOpts->curOptIdx++ ];
break;
default:
#ifdef DEBUG
fputs( "AutoOpts lib error: option type not selected\n",
stderr );
exit( EXIT_FAILURE );
#endif
case TOPT_DEFAULT:
break;
}
if (pOpts->curOptIdx > pOpts->origArgCt) {
fprintf( stderr, zMisArg, pOpts->pzProgPath,
pOptState->pOD->pz_Name );
return FAILURE;
}
pOpts->pzCurOpt = NULL;
return SUCCESS;
}
static tSuccess
next_opt_arg_may(tOptions* pOpts, tOptState* pOptState)
{
switch (pOptState->optType) {
case TOPT_SHORT:
if (*++pOpts->pzCurOpt != NUL)
pOptState->pzOptArg = pOpts->pzCurOpt;
else {
char* pzLA = pOpts->origArgVect[ pOpts->curOptIdx ];
if ((pzLA == NULL) || (*pzLA == '-'))
pOptState->pzOptArg = NULL;
else {
pOpts->curOptIdx++;
pOptState->pzOptArg = pzLA;
}
}
break;
case TOPT_LONG:
if ( (pOptState->pzOptArg == NULL)
&& (! NAMED_OPTS(pOpts))) {
char* pzLA = pOpts->origArgVect[ pOpts->curOptIdx ];
if ((pzLA == NULL) || (*pzLA == '-'))
pOptState->pzOptArg = NULL;
else {
pOpts->curOptIdx++;
pOptState->pzOptArg = pzLA;
}
}
break;
default:
case TOPT_DEFAULT:
fputs(zAO_Woops, stderr );
exit( EX_SOFTWARE );
}
pOpts->pzCurOpt = NULL;
return SUCCESS;
}
static tSuccess
next_opt_arg_none(tOptions* pOpts, tOptState* pOptState)
{
if (pOptState->optType == TOPT_SHORT)
(pOpts->pzCurOpt)++;
else if (pOptState->pzOptArg != NULL) {
fprintf(stderr, zNoArg, pOpts->pzProgPath, pOptState->pOD->pz_Name);
return FAILURE;
}
else
pOpts->pzCurOpt = NULL;
return SUCCESS;
}
static tSuccess
nextOption(tOptions* pOpts, tOptState* pOptState)
{
{
tSuccess res;
res = findOptDesc( pOpts, pOptState );
if (! SUCCESSFUL( res ))
return res;
}
if ( ((pOptState->flags & OPTST_DEFINED) != 0)
&& ((pOptState->pOD->fOptState & OPTST_NO_COMMAND) != 0)) {
fprintf(stderr, zNotCmdOpt, pOptState->pOD->pz_Name);
return FAILURE;
}
pOptState->flags |= (pOptState->pOD->fOptState & OPTST_PERSISTENT_MASK);
{
enum { ARG_NONE, ARG_MAY, ARG_MUST } arg_type = ARG_NONE;
tSuccess res;
if ((pOptState->flags & OPTST_DISABLED) != 0)
arg_type = ARG_NONE;
else if (OPTST_GET_ARGTYPE(pOptState->flags) == OPARG_TYPE_NONE)
arg_type = ARG_NONE;
else if (pOptState->flags & OPTST_ARG_OPTIONAL)
arg_type = ARG_MAY;
else
arg_type = ARG_MUST;
switch (arg_type) {
case ARG_MUST: res = next_opt_arg_must(pOpts, pOptState); break;
case ARG_MAY: res = next_opt_arg_may( pOpts, pOptState); break;
case ARG_NONE: res = next_opt_arg_none(pOpts, pOptState); break;
}
return res;
}
}
LOCAL tSuccess
doImmediateOpts( tOptions* pOpts )
{
pOpts->curOptIdx = 1;
pOpts->pzCurOpt = NULL;
for (;;) {
tOptState optState = OPTSTATE_INITIALIZER(PRESET);
switch (nextOption( pOpts, &optState )) {
case FAILURE: goto optionsDone;
case PROBLEM: return SUCCESS;
case SUCCESS: break;
}
if (! DO_IMMEDIATELY(optState.flags))
continue;
if (! SUCCESSFUL( handleOption( pOpts, &optState )))
break;
} optionsDone:;
if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0)
(*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
return FAILURE;
}
LOCAL tSuccess
doRegularOpts( tOptions* pOpts )
{
for (;;) {
tOptState optState = OPTSTATE_INITIALIZER(DEFINED);
switch (nextOption( pOpts, &optState )) {
case FAILURE: goto optionsDone;
case PROBLEM: return SUCCESS;
case SUCCESS: break;
}
if (! DO_NORMALLY(optState.flags)) {
if (! DO_SECOND_TIME(optState.flags))
continue;
optState.pOD->optOccCt--;
}
if (! SUCCESSFUL( handleOption( pOpts, &optState )))
break;
} optionsDone:;
if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0)
(*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
return FAILURE;
}
static tSuccess
doPresets( tOptions* pOpts )
{
tOptDesc * pOD = NULL;
if (! SUCCESSFUL( doImmediateOpts( pOpts )))
return FAILURE;
if ( (pOpts->specOptIdx.save_opts != NO_EQUIVALENT)
&& (pOpts->specOptIdx.save_opts != 0)) {
pOD = pOpts->pOptDesc + pOpts->specOptIdx.save_opts + 1;
if (DISABLED_OPT(pOD))
return SUCCESS;
}
pOpts->fOptSet |= OPTPROC_PRESETTING;
if (pOpts->papzHomeList == NULL) {
doEnvPresets( pOpts, ENV_ALL );
}
else {
doEnvPresets( pOpts, ENV_IMM );
if ((pOD != NULL) && ! DISABLED_OPT(pOD))
internalFileLoad( pOpts );
doEnvPresets( pOpts, ENV_NON_IMM );
}
pOpts->fOptSet &= ~OPTPROC_PRESETTING;
return SUCCESS;
}
static int
checkConsistency( tOptions* pOpts )
{
int errCt = 0;
tOptDesc* pOD = pOpts->pOptDesc;
int oCt = pOpts->presetOptCt;
for (;;) {
const int* pMust = pOD->pOptMust;
const int* pCant = pOD->pOptCant;
if (SELECTED_OPT(pOD)) {
if (pMust != NULL) for (;;) {
tOptDesc* p = pOpts->pOptDesc + *(pMust++);
if (UNUSED_OPT(p)) {
const tOptDesc* pN = pOpts->pOptDesc + pMust[-1];
errCt++;
fprintf( stderr, zReqFmt, pOD->pz_Name, pN->pz_Name );
}
if (*pMust == NO_EQUIVALENT)
break;
}
if (pCant != NULL) for (;;) {
tOptDesc* p = pOpts->pOptDesc + *(pCant++);
if (SELECTED_OPT(p)) {
const tOptDesc* pN = pOpts->pOptDesc + pCant[-1];
errCt++;
fprintf( stderr, zCantFmt, pOD->pz_Name, pN->pz_Name );
}
if (*pCant == NO_EQUIVALENT)
break;
}
}
if ( (pOD->optEquivIndex == NO_EQUIVALENT)
|| (pOD->optEquivIndex == pOD->optIndex) ) do {
if (pOD->optOccCt >= pOD->optMinCt)
break;
if ( (pOD->fOptState & OPTST_MUST_SET)
&& (pOD->fOptState & (OPTST_PRESET | OPTST_SET)) )
break;
errCt++;
if (pOD->optMinCt > 1)
fprintf( stderr, zNotEnough, pOD->pz_Name, pOD->optMinCt );
else fprintf( stderr, zNeedOne, pOD->pz_Name );
} while (0);
if (--oCt <= 0)
break;
pOD++;
}
if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
if ((pOpts->fOptSet & OPTPROC_NO_ARGS) != 0) {
if (pOpts->origArgCt > pOpts->curOptIdx) {
fprintf( stderr, zNoArgs, pOpts->pzProgName );
++errCt;
}
}
else if ((pOpts->fOptSet & OPTPROC_ARGS_REQ) != 0) {
if (pOpts->origArgCt <= pOpts->curOptIdx) {
fprintf( stderr, zArgsMust, pOpts->pzProgName );
++errCt;
}
}
}
return errCt;
}
int
optionProcess(
tOptions* pOpts,
int argCt,
char** argVect )
{
if (! SUCCESSFUL( validateOptionsStruct( pOpts, argVect[0] )))
exit( EX_SOFTWARE );
if ((pOpts->fOptSet & OPTPROC_INITDONE) == 0) {
pOpts->origArgCt = argCt;
pOpts->origArgVect = argVect;
pOpts->fOptSet |= OPTPROC_INITDONE;
if (! SUCCESSFUL( doPresets( pOpts )))
return 0;
if ( ((pOpts->fOptSet & OPTPROC_TRANSLATE) != 0)
&& (pOpts->pTransProc != NULL)
&& ((pOpts->fOptSet & OPTPROC_NO_XLAT_MASK)
== OPTPROC_NXLAT_OPT_CFG) ) {
pOpts->fOptSet &= ~OPTPROC_NXLAT_OPT_CFG;
(*pOpts->pTransProc)();
}
if ((pOpts->fOptSet & OPTPROC_REORDER) != 0)
optionSort( pOpts );
pOpts->curOptIdx = 1;
pOpts->pzCurOpt = NULL;
}
else if (pOpts->curOptIdx <= 0) {
pOpts->curOptIdx = 1;
pOpts->pzCurOpt = NULL;
}
if (! SUCCESSFUL( doRegularOpts( pOpts )))
return pOpts->origArgCt;
if ( (pOpts->specOptIdx.save_opts != NO_EQUIVALENT)
&& (pOpts->specOptIdx.save_opts != 0)) {
tOptDesc* pOD = pOpts->pOptDesc + pOpts->specOptIdx.save_opts;
if (SELECTED_OPT( pOD )) {
optionSaveFile( pOpts );
exit( EXIT_SUCCESS );
}
}
if ((pOpts->fOptSet & OPTPROC_ERRSTOP) != 0) {
if (checkConsistency( pOpts ) != 0)
(*pOpts->pUsageProc)( pOpts, EXIT_FAILURE );
}
return pOpts->curOptIdx;
}