New shared function, getenv_bool()

This provides a standard way to get a boolean from an environment
variable.  It treats the variable as true iff its value begins with 'y'
or 'Y', like most of the current implementations.  The function takes a
default value which it returns if the environment variable is undefined.

This replaces the various ad-hoc tests of environment variable scattered
around and mostly doesn't change their behaviour.  The exceptions are
TOWERS_2D in Towers and DEBUG_PUZZLES in the Windows front end.  Both of
those were treated as true if they were defined at all, but now follow
the same rules as other boolean environment variables.
This commit is contained in:
Ben Harris
2023-03-22 16:06:18 +00:00
parent adf2a09829
commit 09c15f206e
12 changed files with 26 additions and 35 deletions

6
emcc.c
View File

@ -987,7 +987,6 @@ int main(int argc, char **argv)
*/
{
struct preset_menu *menu = midend_get_presets(me, &npresets);
char *env;
bool may_configure = false;
presets = snewn(npresets, game_params *);
for (i = 0; i < npresets; i++)
@ -999,10 +998,7 @@ int main(int argc, char **argv)
* Crude hack to allow the "Custom..." item to be hidden on
* KaiOS, where dialogs don't yet work.
*/
env = getenv("PUZZLES_ALLOW_CUSTOM");
if (thegame.can_configure &&
(!env || env[0] == 'y' || env[0] == 'Y'))
if (thegame.can_configure && getenv_bool("PUZZLES_ALLOW_CUSTOM", true))
may_configure = true;
if (may_configure)
js_add_preset(0, "Custom...", -1);