common: New function parse_compatibility_flags.

* common/miscellaneous.c (parse_compatibility_flags): New.
* common/util.h (struct compatibility_flags_s): New.
--

This is similar to parse_debug_flags but does not support specifying a
value.  This way we can more easily change the internal values or
re-use them for other purposes.
This commit is contained in:
Werner Koch 2022-06-13 17:35:05 +02:00
parent dd600bbc84
commit ce63eaa4f8
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
2 changed files with 89 additions and 0 deletions

View File

@ -712,3 +712,83 @@ parse_debug_flag (const char *string, unsigned int *debugvar,
*debugvar |= result;
return 0;
}
/* Parse an --comaptibility_flags style argument consisting of comma
* separated strings.
*
* Returns: 0 on success or -1 and ERRNO set on error. On success the
* supplied variable is updated by the parsed flags.
*
* If STRING is NULL the enabled flags are printed.
*/
int
parse_compatibility_flags (const char *string, unsigned int *flagvar,
const struct compatibility_flags_s *flags)
{
unsigned long result = 0;
int i, j;
if (!string)
{
if (flagvar)
{
log_info ("enabled compatibility flags:");
for (i=0; flags[i].name; i++)
if ((*flagvar & flags[i].flag))
log_printf (" %s", flags[i].name);
log_printf ("\n");
}
return 0;
}
while (spacep (string))
string++;
if (!strcmp (string, "?") || !strcmp (string, "help"))
{
log_info ("available compatibility flags:\n");
for (i=0; flags[i].name; i++)
log_info (" %s\n", flags[i].name);
if (flags[i].flag != 77)
exit (0);
}
else
{
char **words;
words = strtokenize (string, ",");
if (!words)
return -1;
for (i=0; words[i]; i++)
{
if (*words[i])
{
for (j=0; flags[j].name; j++)
if (!strcmp (words[i], flags[j].name))
{
result |= flags[j].flag;
break;
}
if (!flags[j].name)
{
if (!strcmp (words[i], "none"))
{
*flagvar = 0;
result = 0;
}
else if (!strcmp (words[i], "all"))
result = ~0;
else
log_info ("unknown compatibility flag '%s' ignored\n",
words[i]);
}
}
}
xfree (words);
}
*flagvar |= result;
return 0;
}

View File

@ -372,6 +372,15 @@ struct debug_flags_s
int parse_debug_flag (const char *string, unsigned int *debugvar,
const struct debug_flags_s *flags);
struct compatibility_flags_s
{
unsigned int flag;
const char *name;
const char *desc;
};
int parse_compatibility_flags (const char *string, unsigned int *flagvar,
const struct compatibility_flags_s *flags);
/*-- Simple replacement functions. */