tSCC* pz_enum_err_fmt;
static void
enumError(
tOptions* pOpts,
tOptDesc* pOD,
tCC* const * paz_names,
int name_ct );
static uintptr_t
findName(
tCC* pzName,
tOptions* pOpts,
tOptDesc* pOD,
tCC* const * paz_names,
unsigned int name_ct );
static void
enumError(
tOptions* pOpts,
tOptDesc* pOD,
tCC* const * paz_names,
int name_ct )
{
size_t max_len = 0;
size_t ttl_len = 0;
int ct_down = name_ct;
int hidden = 0;
if (pOpts > OPTPROC_EMIT_LIMIT)
fprintf(option_usage_fp, pz_enum_err_fmt, pOpts->pzProgName,
pOD->optArg.argString, pOD->pz_Name);
fprintf(option_usage_fp, zValidKeys, pOD->pz_Name);
if (**paz_names == 0x7F) {
paz_names++;
hidden = 1;
ct_down = --name_ct;
}
{
tCC * const * paz = paz_names;
do {
size_t len = strlen( *(paz++) ) + 1;
if (len > max_len)
max_len = len;
ttl_len += len;
} while (--ct_down > 0);
ct_down = name_ct;
}
if (max_len > 35) {
do {
fprintf( option_usage_fp, " %s\n", *(paz_names++) );
} while (--ct_down > 0);
}
else if (ttl_len < 76) {
fputc( ' ', option_usage_fp );
do {
fputc( ' ', option_usage_fp );
fputs( *(paz_names++), option_usage_fp );
} while (--ct_down > 0);
fputc( '\n', option_usage_fp );
}
else {
int ent_no = 0;
char zFmt[16];
sprintf( zFmt, "%%-%ds", (int)max_len );
max_len = 78 / max_len;
fputs( " ", option_usage_fp );
ct_down = name_ct;
while (--ct_down > 0) {
if (++ent_no == max_len) {
fprintf( option_usage_fp, "%s\n ", *(paz_names++) );
ent_no = 0;
}
else
fprintf(option_usage_fp, zFmt, *(paz_names++) );
}
fprintf(option_usage_fp, "%s\n", *paz_names);
}
if (pOpts > OPTPROC_EMIT_LIMIT) {
fprintf(option_usage_fp, zIntRange, hidden, name_ct - 1 + hidden);
(*(pOpts->pUsageProc))( pOpts, EXIT_FAILURE );
}
if (OPTST_GET_ARGTYPE(pOD->fOptState) == OPARG_TYPE_MEMBERSHIP) {
fprintf(option_usage_fp, zLowerBits, name_ct);
fputs(zSetMemberSettings, option_usage_fp);
} else {
fprintf(option_usage_fp, zIntRange, hidden, name_ct - 1 + hidden);
}
}
static uintptr_t
findName(
tCC* pzName,
tOptions* pOpts,
tOptDesc* pOD,
tCC* const * paz_names,
unsigned int name_ct )
{
uintptr_t res = name_ct;
size_t len = strlen( (char*)pzName );
uintptr_t idx;
if (IS_DEC_DIGIT_CHAR(*pzName)) {
char * pz = (char *)(void *)pzName;
unsigned long val = strtoul(pz, &pz, 0);
if ((*pz == NUL) && (val < name_ct))
return (uintptr_t)val;
enumError(pOpts, pOD, paz_names, (int)name_ct);
return name_ct;
}
for (idx = 0; idx < name_ct; idx++) {
if (strncmp( (char*)paz_names[idx], (char*)pzName, len) == 0) {
if (paz_names[idx][len] == NUL)
return idx;
res = (res != name_ct) ? ~0 : idx;
}
}
if (res < name_ct)
return res;
pz_enum_err_fmt = (res == name_ct) ? zNoKey : zAmbigKey;
option_usage_fp = stderr;
enumError(pOpts, pOD, paz_names, (int)name_ct);
return name_ct;
}
char const*
optionKeywordName(
tOptDesc* pOD,
unsigned int enum_val )
{
tOptDesc od;
od.optArg.argEnum = enum_val;
(*(pOD->pOptProc))(OPTPROC_RETURN_VALNAME, &od );
return od.optArg.argString;
}
uintptr_t
optionEnumerationVal(
tOptions* pOpts,
tOptDesc* pOD,
tCC * const * paz_names,
unsigned int name_ct )
{
uintptr_t res = 0UL;
switch ((uintptr_t)pOpts) {
case (uintptr_t)OPTPROC_EMIT_USAGE:
enumError(pOpts, pOD, paz_names, (int)name_ct);
break;
case (uintptr_t)OPTPROC_EMIT_SHELL:
{
unsigned int ix = pOD->optArg.argEnum;
if (ix >= name_ct)
printf( "INVALID-%d", ix );
else
fputs( paz_names[ ix ], stdout );
break;
}
case (uintptr_t)OPTPROC_RETURN_VALNAME:
{
tSCC zInval[] = "*INVALID*";
unsigned int ix = pOD->optArg.argEnum;
if (ix >= name_ct)
return (uintptr_t)zInval;
pOD->optArg.argString = paz_names[ix];
break;
}
default:
res = findName(pOD->optArg.argString, pOpts, pOD, paz_names, name_ct);
if (pOD->fOptState & OPTST_ALLOC_ARG) {
AGFREE(pOD->optArg.argString);
pOD->fOptState &= ~OPTST_ALLOC_ARG;
pOD->optArg.argString = NULL;
}
}
return res;
}
void
optionSetMembers(
tOptions* pOpts,
tOptDesc* pOD,
tCC* const * paz_names,
unsigned int name_ct )
{
switch ((uintptr_t)pOpts) {
case (uintptr_t)OPTPROC_EMIT_USAGE:
enumError(OPTPROC_EMIT_USAGE, pOD, paz_names, (int)name_ct );
return;
case (uintptr_t)OPTPROC_EMIT_SHELL:
{
int ix = 0;
uintptr_t bits = (uintptr_t)pOD->optCookie;
size_t len = 0;
bits &= ((uintptr_t)1 << (uintptr_t)name_ct) - (uintptr_t)1;
while (bits != 0) {
if (bits & 1) {
if (len++ > 0) fputs( " | ", stdout );
fputs(paz_names[ix], stdout);
}
if (++ix >= name_ct) break;
bits >>= 1;
}
return;
}
case (uintptr_t)OPTPROC_RETURN_VALNAME:
{
char* pz;
uintptr_t bits = (uintptr_t)pOD->optCookie;
int ix = 0;
size_t len = 5;
bits &= ((uintptr_t)1 << (uintptr_t)name_ct) - (uintptr_t)1;
while (bits != 0) {
if (bits & 1)
len += strlen( paz_names[ix]) + 8;
if (++ix >= name_ct) break;
bits >>= 1;
}
pOD->optArg.argString = pz = AGALOC(len, "enum name");
strcpy( pz, "none" );
pz += 4;
bits = (uintptr_t)pOD->optCookie;
bits &= ((uintptr_t)1 << (uintptr_t)name_ct) - (uintptr_t)1;
ix = 0;
while (bits != 0) {
if (bits & 1) {
strcpy( pz, " + " );
strcpy( pz+3, paz_names[ix]);
pz += strlen( paz_names[ix]) + 3;
}
if (++ix >= name_ct) break;
bits >>= 1;
}
return;
}
default:
break;
}
if ((pOD->fOptState & OPTST_RESET) != 0)
return;
{
tCC* pzArg = pOD->optArg.argString;
uintptr_t res;
if ((pzArg == NULL) || (*pzArg == NUL)) {
pOD->optCookie = (void*)0;
return;
}
res = (uintptr_t)pOD->optCookie;
for (;;) {
tSCC zSpn[] = " ,|+\t\r\f\n";
int iv, len;
pzArg += strspn( pzArg, zSpn );
iv = (*pzArg == '!');
if (iv)
pzArg += strspn( pzArg+1, zSpn ) + 1;
len = strcspn( pzArg, zSpn );
if (len == 0)
break;
if ((len == 3) && (strncmp(pzArg, zAll, (size_t)3) == 0)) {
if (iv)
res = 0;
else res = ~0UL;
}
else if ((len == 4) && (strncmp(pzArg, zNone, (size_t)4) == 0)) {
if (! iv)
res = 0;
}
else do {
char* pz;
uintptr_t bit = strtoul( pzArg, &pz, 0 );
if (pz != pzArg + len) {
char z[ AO_NAME_SIZE ];
tCC* p;
int shift_ct;
if (*pz != NUL) {
if (len >= AO_NAME_LIMIT)
break;
strncpy( z, pzArg, (size_t)len );
z[len] = NUL;
p = z;
} else {
p = pzArg;
}
shift_ct = findName(p, pOpts, pOD, paz_names, name_ct);
if (shift_ct >= name_ct) {
pOD->optCookie = (void*)0;
return;
}
bit = 1UL << shift_ct;
}
if (iv)
res &= ~bit;
else res |= bit;
} while (0);
if (pzArg[len] == NUL)
break;
pzArg += len + 1;
}
if (name_ct < (8 * sizeof( uintptr_t ))) {
res &= (1UL << name_ct) - 1UL;
}
pOD->optCookie = (void*)res;
}
}