static void
filePreset(
tOptions* pOpts,
char const* pzFileName,
int direction );
static char*
handleComment( char* pzText );
static char*
handleConfig(
tOptions* pOpts,
tOptState* pOS,
char* pzText,
int direction );
static char*
handleDirective(
tOptions* pOpts,
char* pzText );
static char*
handleProgramSection(
tOptions* pOpts,
char* pzText );
static char*
handleStructure(
tOptions* pOpts,
tOptState* pOS,
char* pzText,
int direction );
static char*
parseKeyWordType(
tOptions* pOpts,
char* pzText,
tOptionValue* pType );
static char*
parseSetMemType(
tOptions* pOpts,
char* pzText,
tOptionValue* pType );
static char*
parseValueType(
char* pzText,
tOptionValue* pType );
static char*
skipUnknown( char* pzText );
const tOptionValue*
configFileLoad( char const* pzFile )
{
tmap_info_t cfgfile;
tOptionValue* pRes = NULL;
tOptionLoadMode save_mode = option_load_mode;
char* pzText =
text_mmap( pzFile, PROT_READ, MAP_PRIVATE, &cfgfile );
if (TEXT_MMAP_FAILED_ADDR(pzText))
return NULL;
option_load_mode = OPTION_LOAD_COOKED;
pRes = optionLoadNested(pzText, pzFile, strlen(pzFile));
if (pRes == NULL) {
int err = errno;
text_munmap( &cfgfile );
errno = err;
} else
text_munmap( &cfgfile );
option_load_mode = save_mode;
return pRes;
}
const tOptionValue*
optionFindValue( const tOptDesc* pOptDesc,
char const* pzName, char const* pzVal )
{
const tOptionValue* pRes = NULL;
if ( (pOptDesc == NULL)
|| (OPTST_GET_ARGTYPE(pOptDesc->fOptState) != OPARG_TYPE_HIERARCHY)) {
errno = EINVAL;
}
else if (pOptDesc->optCookie == NULL) {
errno = ENOENT;
}
else do {
tArgList* pAL = pOptDesc->optCookie;
int ct = pAL->useCt;
void** ppOV = (void**)(pAL->apzArgs);
if (ct == 0) {
errno = ENOENT;
break;
}
if (pzName == NULL) {
pRes = (tOptionValue*)*ppOV;
break;
}
while (--ct >= 0) {
const tOptionValue* pOV = *(ppOV++);
const tOptionValue* pRV = optionGetValue( pOV, pzName );
if (pRV == NULL)
continue;
if (pzVal == NULL) {
pRes = pOV;
break;
}
}
if (pRes == NULL)
errno = ENOENT;
} while (0);
return pRes;
}
const tOptionValue*
optionFindNextValue( const tOptDesc* pOptDesc, const tOptionValue* pPrevVal,
char const* pzName, char const* pzVal )
{
int foundOldVal = 0;
tOptionValue* pRes = NULL;
if ( (pOptDesc == NULL)
|| (OPTST_GET_ARGTYPE(pOptDesc->fOptState) != OPARG_TYPE_HIERARCHY)) {
errno = EINVAL;
}
else if (pOptDesc->optCookie == NULL) {
errno = ENOENT;
}
else do {
tArgList* pAL = pOptDesc->optCookie;
int ct = pAL->useCt;
void** ppOV = (void**)pAL->apzArgs;
if (ct == 0) {
errno = ENOENT;
break;
}
while (--ct >= 0) {
tOptionValue* pOV = *(ppOV++);
if (foundOldVal) {
pRes = pOV;
break;
}
if (pOV == pPrevVal)
foundOldVal = 1;
}
if (pRes == NULL)
errno = ENOENT;
} while (0);
return pRes;
}
const tOptionValue*
optionGetValue( const tOptionValue* pOld, char const* pzValName )
{
tArgList* pAL;
tOptionValue* pRes = NULL;
if ((pOld == NULL) || (pOld->valType != OPARG_TYPE_HIERARCHY)) {
errno = EINVAL;
return NULL;
}
pAL = pOld->v.nestVal;
if (pAL->useCt > 0) {
int ct = pAL->useCt;
void** papOV = (void**)(pAL->apzArgs);
if (pzValName == NULL) {
pRes = (tOptionValue*)*papOV;
}
else do {
tOptionValue* pOV = *(papOV++);
if (strcmp( pOV->pzName, pzValName ) == 0) {
pRes = pOV;
break;
}
} while (--ct > 0);
}
if (pRes == NULL)
errno = ENOENT;
return pRes;
}
tOptionValue const *
optionNextValue(tOptionValue const * pOVList,tOptionValue const * pOldOV )
{
tArgList* pAL;
tOptionValue* pRes = NULL;
int err = EINVAL;
if ((pOVList == NULL) || (pOVList->valType != OPARG_TYPE_HIERARCHY)) {
errno = EINVAL;
return NULL;
}
pAL = pOVList->v.nestVal;
{
int ct = pAL->useCt;
void** papNV = (void**)(pAL->apzArgs);
while (ct-- > 0) {
tOptionValue* pNV = *(papNV++);
if (pNV == pOldOV) {
if (ct == 0) {
err = ENOENT;
} else {
err = 0;
pRes = (tOptionValue*)*papNV;
}
break;
}
}
}
if (err != 0)
errno = err;
return pRes;
}
static void
filePreset(
tOptions* pOpts,
char const* pzFileName,
int direction )
{
tmap_info_t cfgfile;
tOptState optst = OPTSTATE_INITIALIZER(PRESET);
char* pzFileText =
text_mmap( pzFileName, PROT_READ|PROT_WRITE, MAP_PRIVATE, &cfgfile );
if (TEXT_MMAP_FAILED_ADDR(pzFileText))
return;
if (direction == DIRECTION_CALLED) {
optst.flags = OPTST_DEFINED;
direction = DIRECTION_PROCESS;
}
if ((pOpts->fOptSet & OPTPROC_PRESETTING) == 0)
optst.flags = OPTST_SET;
do {
while (IS_WHITESPACE_CHAR(*pzFileText)) pzFileText++;
if (IS_VAR_FIRST_CHAR(*pzFileText)) {
pzFileText = handleConfig(pOpts, &optst, pzFileText, direction);
} else switch (*pzFileText) {
case '<':
if (IS_VAR_FIRST_CHAR(pzFileText[1]))
pzFileText =
handleStructure(pOpts, &optst, pzFileText, direction);
else switch (pzFileText[1]) {
case '?':
pzFileText = handleDirective( pOpts, pzFileText );
break;
case '!':
pzFileText = handleComment( pzFileText );
break;
case '/':
pzFileText = strchr( pzFileText+2, '>' );
if (pzFileText++ != NULL)
break;
default:
goto all_done;
}
break;
case '[':
pzFileText = handleProgramSection( pOpts, pzFileText );
break;
case '#':
pzFileText = strchr( pzFileText+1, '\n' );
break;
default:
goto all_done;
}
} while (pzFileText != NULL);
all_done:
text_munmap( &cfgfile );
}
static char*
handleComment( char* pzText )
{
char* pz = strstr( pzText, "-->" );
if (pz != NULL)
pz += 3;
return pz;
}
static char*
handleConfig(
tOptions* pOpts,
tOptState* pOS,
char* pzText,
int direction )
{
char* pzName = pzText++;
char* pzEnd = strchr( pzText, '\n' );
if (pzEnd == NULL)
return pzText + strlen(pzText);
while (IS_VALUE_NAME_CHAR(*pzText)) pzText++;
while (IS_WHITESPACE_CHAR(*pzText)) pzText++;
if (pzText > pzEnd) {
name_only:
*pzEnd++ = NUL;
loadOptionLine( pOpts, pOS, pzName, direction, OPTION_LOAD_UNCOOKED );
return pzEnd;
}
if ((*pzText == '=') || (*pzText == ':')) {
while (IS_WHITESPACE_CHAR(*++pzText)) ;
if (pzText > pzEnd)
goto name_only;
} else if (! IS_WHITESPACE_CHAR(pzText[-1]))
return NULL;
if (pzEnd[-1] == '\\') {
char* pcD = pzEnd-1;
char* pcS = pzEnd;
for (;;) {
char ch = *(pcS++);
switch (ch) {
case NUL:
pcS = NULL;
case '\n':
*pcD = NUL;
pzEnd = pcS;
goto copy_done;
case '\\':
if (*pcS == '\n') {
ch = *(pcS++);
}
default:
*(pcD++) = ch;
}
} copy_done:;
} else {
*(pzEnd++) = NUL;
}
loadOptionLine( pOpts, pOS, pzName, direction, OPTION_LOAD_UNCOOKED );
return pzEnd;
}
static char*
handleDirective(
tOptions* pOpts,
char* pzText )
{
char ztitle[32] = "<?";
size_t title_len = strlen( zProg );
size_t name_len;
if ( (strncmp( pzText+2, zProg, title_len ) != 0)
|| (! IS_WHITESPACE_CHAR(pzText[title_len+2])) ) {
pzText = strchr( pzText+2, '>' );
if (pzText != NULL)
pzText++;
return pzText;
}
name_len = strlen( pOpts->pzProgName );
strcpy( ztitle+2, zProg );
title_len += 2;
do {
pzText += title_len;
if (IS_WHITESPACE_CHAR(*pzText)) {
while (IS_WHITESPACE_CHAR(*++pzText)) ;
if ( (strneqvcmp( pzText, pOpts->pzProgName, (int)name_len) == 0)
&& (pzText[name_len] == '>')) {
pzText += name_len + 1;
break;
}
}
pzText = strstr( pzText, ztitle );
} while (pzText != NULL);
return pzText;
}
static char*
handleProgramSection(
tOptions* pOpts,
char* pzText )
{
size_t len = strlen( pOpts->pzPROGNAME );
if ( (strncmp( pzText+1, pOpts->pzPROGNAME, len ) == 0)
&& (pzText[len+1] == ']'))
return strchr( pzText + len + 2, '\n' );
if (len > 16)
return NULL;
{
char z[24];
sprintf( z, "[%s]", pOpts->pzPROGNAME );
pzText = strstr( pzText, z );
}
if (pzText != NULL)
pzText = strchr( pzText, '\n' );
return pzText;
}
static char*
handleStructure(
tOptions* pOpts,
tOptState* pOS,
char* pzText,
int direction )
{
tOptionLoadMode mode = option_load_mode;
tOptionValue valu;
char* pzName = ++pzText;
char* pzData;
char* pcNulPoint;
while (IS_VALUE_NAME_CHAR(*pzText)) pzText++;
pcNulPoint = pzText;
valu.valType = OPARG_TYPE_STRING;
switch (*pzText) {
case ' ':
case '\t':
pzText = parseAttributes( pOpts, pzText, &mode, &valu );
if (*pzText == '>')
break;
if (*pzText != '/')
return NULL;
case '/':
if (pzText[1] != '>')
return NULL;
*pzText = NUL;
pzText += 2;
loadOptionLine( pOpts, pOS, pzName, direction, mode );
return pzText;
case '>':
break;
default:
pzText = strchr( pzText, '>');
if (pzText != NULL)
pzText++;
return pzText;
}
*pcNulPoint = NUL;
pzData = ++pzText;
{
char z[64], *pz = z;
size_t len = strlen(pzName) + 4;
if (len > sizeof(z))
pz = AGALOC(len, "scan name");
sprintf( pz, "</%s>", pzName );
*pzText = ' ';
pzText = strstr( pzText, pz );
if (pz != z) AGFREE(pz);
if (pzText == NULL)
return pzText;
*pzText = NUL;
pzText += len-1;
}
memset(pcNulPoint, ' ', pzData - pcNulPoint);
if (valu.valType == OPARG_TYPE_STRING) {
char * pzSrc = pzData;
char * pzDst = pzData;
char bf[4];
bf[2] = NUL;
for (;;) {
int ch = ((int)*(pzSrc++)) & 0xFF;
switch (ch) {
case NUL: goto string_fixup_done;
case '%':
bf[0] = *(pzSrc++);
bf[1] = *(pzSrc++);
if ((bf[0] == NUL) || (bf[1] == NUL))
goto string_fixup_done;
ch = strtoul(bf, NULL, 16);
default:
*(pzDst++) = ch;
}
} string_fixup_done:;
*pzDst = NUL;
}
loadOptionLine( pOpts, pOS, pzName, direction, mode );
return pzText;
}
LOCAL void
internalFileLoad( tOptions* pOpts )
{
uint32_t svfl;
int idx;
int inc;
char zFileName[ AG_PATH_MAX+1 ];
if (pOpts->papzHomeList == NULL)
return;
svfl = pOpts->fOptSet;
inc = DIRECTION_PRESET;
pOpts->fOptSet &= ~OPTPROC_ERRSTOP;
for (idx = 0; pOpts->papzHomeList[ idx+1 ] != NULL; ++idx) ;
for (;;) {
struct stat StatBuf;
cch_t* pzPath;
if (idx < 0) {
inc = DIRECTION_PROCESS;
idx = 0;
}
pzPath = pOpts->papzHomeList[ idx ];
if (pzPath == NULL)
break;
idx += inc;
if (! optionMakePath( zFileName, (int)sizeof(zFileName),
pzPath, pOpts->pzProgPath ))
continue;
if (stat( zFileName, &StatBuf ) != 0)
continue;
if (S_ISDIR( StatBuf.st_mode )) {
size_t len = strlen( zFileName );
char* pz;
if (len + 1 + strlen( pOpts->pzRcName ) >= sizeof( zFileName ))
continue;
pz = zFileName + len;
if (pz[-1] != DIRCH)
*(pz++) = DIRCH;
strcpy( pz, pOpts->pzRcName );
}
filePreset( pOpts, zFileName, inc );
{
tOptDesc * pOD = pOpts->pOptDesc + pOpts->specOptIdx.save_opts+1;
if (DISABLED_OPT(pOD) && PRESETTING(inc)) {
idx -= inc;
inc = DIRECTION_PROCESS;
}
}
}
pOpts->fOptSet = svfl;
}
int
optionFileLoad( tOptions* pOpts, char const* pzProgram )
{
if (! SUCCESSFUL( validateOptionsStruct( pOpts, pzProgram )))
return -1;
pOpts->pzProgName = pzProgram;
internalFileLoad( pOpts );
return 0;
}
void
optionLoadOpt( tOptions* pOpts, tOptDesc* pOptDesc )
{
struct stat sb;
if ( DISABLED_OPT(pOptDesc)
|| ((pOptDesc->fOptState & OPTST_RESET) != 0))
return;
if (stat( pOptDesc->optArg.argString, &sb ) != 0) {
if ((pOpts->fOptSet & OPTPROC_ERRSTOP) == 0)
return;
fprintf( stderr, zFSErrOptLoad, errno, strerror( errno ),
pOptDesc->optArg.argString );
exit(EX_NOINPUT);
}
if (! S_ISREG( sb.st_mode )) {
if ((pOpts->fOptSet & OPTPROC_ERRSTOP) == 0)
return;
fprintf( stderr, zNotFile, pOptDesc->optArg.argString );
exit(EX_NOINPUT);
}
filePreset(pOpts, pOptDesc->optArg.argString, DIRECTION_CALLED);
}
LOCAL char*
parseAttributes(
tOptions* pOpts,
char* pzText,
tOptionLoadMode* pMode,
tOptionValue* pType )
{
size_t len;
do {
if (! IS_WHITESPACE_CHAR(*pzText))
switch (*pzText) {
case '/': pType->valType = OPARG_TYPE_NONE;
case '>': return pzText;
default:
case NUL: return NULL;
}
while (IS_WHITESPACE_CHAR(*++pzText)) ;
len = 0;
while (IS_LOWER_CASE_CHAR(pzText[len])) len++;
switch (find_xat_attribute_id(pzText, len)) {
case XAT_KWD_TYPE:
pzText = parseValueType( pzText+len, pType );
break;
case XAT_KWD_WORDS:
pzText = parseKeyWordType( pOpts, pzText+len, pType );
break;
case XAT_KWD_MEMBERS:
pzText = parseSetMemType( pOpts, pzText+len, pType );
break;
case XAT_KWD_COOKED:
pzText += len;
if (! IS_END_XML_TOKEN_CHAR(*pzText))
goto invalid_kwd;
*pMode = OPTION_LOAD_COOKED;
break;
case XAT_KWD_UNCOOKED:
pzText += len;
if (! IS_END_XML_TOKEN_CHAR(*pzText))
goto invalid_kwd;
*pMode = OPTION_LOAD_UNCOOKED;
break;
case XAT_KWD_KEEP:
pzText += len;
if (! IS_END_XML_TOKEN_CHAR(*pzText))
goto invalid_kwd;
*pMode = OPTION_LOAD_KEEP;
break;
default:
case XAT_KWD_INVALID:
invalid_kwd:
pType->valType = OPARG_TYPE_NONE;
return skipUnknown( pzText );
}
} while (pzText != NULL);
return pzText;
}
static char*
parseKeyWordType(
tOptions* pOpts,
char* pzText,
tOptionValue* pType )
{
return skipUnknown( pzText );
}
static char*
parseSetMemType(
tOptions* pOpts,
char* pzText,
tOptionValue* pType )
{
return skipUnknown( pzText );
}
static char*
parseValueType(
char* pzText,
tOptionValue* pType )
{
size_t len = 0;
if (*(pzText++) != '=')
goto woops;
while (IS_OPTION_NAME_CHAR(pzText[len])) len++;
pzText += len;
if ((len == 0) || (! IS_END_XML_TOKEN_CHAR(*pzText))) {
woops:
pType->valType = OPARG_TYPE_NONE;
return skipUnknown( pzText );
}
switch (find_value_type_id(pzText - len, len)) {
default:
case VTP_KWD_INVALID: goto woops;
case VTP_KWD_STRING:
pType->valType = OPARG_TYPE_STRING;
break;
case VTP_KWD_INTEGER:
pType->valType = OPARG_TYPE_NUMERIC;
break;
case VTP_KWD_BOOL:
case VTP_KWD_BOOLEAN:
pType->valType = OPARG_TYPE_BOOLEAN;
break;
case VTP_KWD_KEYWORD:
pType->valType = OPARG_TYPE_ENUMERATION;
break;
case VTP_KWD_SET:
case VTP_KWD_SET_MEMBERSHIP:
pType->valType = OPARG_TYPE_MEMBERSHIP;
break;
case VTP_KWD_NESTED:
case VTP_KWD_HIERARCHY:
pType->valType = OPARG_TYPE_HIERARCHY;
}
return pzText;
}
static char*
skipUnknown( char* pzText )
{
for (;; pzText++) {
if (IS_END_XML_TOKEN_CHAR(*pzText)) return pzText;
if (*pzText == NUL) return NULL;
}
}
LOCAL tSuccess
validateOptionsStruct( tOptions* pOpts, char const* pzProgram )
{
if (pOpts == NULL) {
fputs( zAO_Bad, stderr );
exit( EX_CONFIG );
}
if ( ((pOpts->fOptSet & OPTPROC_TRANSLATE) != 0)
&& (pOpts->pTransProc != NULL) ) {
if ((pOpts->fOptSet & OPTPROC_NO_XLAT_MASK) == OPTPROC_NXLAT_OPT)
pOpts->fOptSet |= OPTPROC_NXLAT_OPT_CFG;
(*pOpts->pTransProc)();
pOpts->fOptSet &= ~OPTPROC_TRANSLATE;
}
if ( ( pOpts->structVersion != OPTIONS_STRUCT_VERSION )
&& ( (pOpts->structVersion > OPTIONS_STRUCT_VERSION )
|| (pOpts->structVersion < OPTIONS_MINIMUM_VERSION )
) ) {
fprintf(stderr, zAO_Err, pzProgram, NUM_TO_VER(pOpts->structVersion));
if (pOpts->structVersion > OPTIONS_STRUCT_VERSION )
fputs( zAO_Big, stderr );
else
fputs( zAO_Sml, stderr );
return FAILURE;
}
if (pOpts->pzProgName == NULL) {
char const* pz = strrchr( pzProgram, DIRCH );
if (pz == NULL)
pOpts->pzProgName = pzProgram;
else pOpts->pzProgName = pz+1;
pOpts->pzProgPath = pzProgram;
strequate( zSepChars );
}
return SUCCESS;
}