shithub: puzzles

Download patch

ref: 09c15f206edac18bd2158c189c821b9ba85d3939
parent: adf2a098298f1aa73aca2c816174d5e63ff45a32
author: Ben Harris <[email protected]>
date: Wed Mar 22 12:06:18 EDT 2023

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.

--- a/emcc.c
+++ b/emcc.c
@@ -987,7 +987,6 @@
      */
     {
         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 @@
          * 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);
--- a/fifteen.c
+++ b/fifteen.c
@@ -714,10 +714,8 @@
             return NULL;               /* out of bounds */
     } else if (IS_CURSOR_MOVE(button)) {
         static int invert_cursor = -1;
-        if (invert_cursor == -1) {
-            char *env = getenv("FIFTEEN_INVERT_CURSOR");
-            invert_cursor = (env && (env[0] == 'y' || env[0] == 'Y'));
-        }
+        if (invert_cursor == -1)
+            invert_cursor = getenv_bool("FIFTEEN_INVERT_CURSOR", false);
         button = flip_cursor(button); /* the default */
         if (invert_cursor)
             button = flip_cursor(button); /* undoes the first flip */
--- a/lightup.c
+++ b/lightup.c
@@ -2168,11 +2168,8 @@
                         lcol, COL_BLACK);
         } else if ((ds_flags & DF_IMPOSSIBLE)) {
             static int draw_blobs_when_lit = -1;
-            if (draw_blobs_when_lit < 0) {
-		char *env = getenv("LIGHTUP_LIT_BLOBS");
-		draw_blobs_when_lit = (!env || (env[0] == 'y' ||
-                                                env[0] == 'Y'));
-            }
+            if (draw_blobs_when_lit < 0)
+		draw_blobs_when_lit = getenv_bool("LIGHTUP_LIT_BLOBS", true);
             if (!(ds_flags & DF_LIT) || draw_blobs_when_lit) {
                 int rlen = TILE_SIZE / 4;
                 draw_rect(dr, dx + TILE_SIZE/2 - rlen/2,
--- a/loopy.c
+++ b/loopy.c
@@ -3292,11 +3292,8 @@
 
     if (line_colour == COL_FAINT) {
 	static int draw_faint_lines = -1;
-	if (draw_faint_lines < 0) {
-	    char *env = getenv("LOOPY_FAINT_LINES");
-	    draw_faint_lines = (!env || (env[0] == 'y' ||
-					 env[0] == 'Y'));
-	}
+	if (draw_faint_lines < 0)
+	    draw_faint_lines = getenv_bool("LOOPY_FAINT_LINES", true);
 	if (draw_faint_lines)
 	    draw_line(dr, x1, y1, x2, y2, line_colour);
     } else {
--- a/misc.c
+++ b/misc.c
@@ -198,6 +198,14 @@
     return ret;
 }
 
+bool getenv_bool(const char *name, bool dflt)
+{
+    char *env = getenv(name);
+    if (env == NULL) return dflt;
+    if (env[0] == 'y' || env[0] == 'Y') return true;
+    return false;
+}
+
 /* Utility functions for colour manipulation. */
 
 static float colour_distance(const float a[3], const float b[3])
--- a/pearl.c
+++ b/pearl.c
@@ -1929,8 +1929,7 @@
     static int gui_style = -1;
 
     if (gui_style == -1) {
-        char *env = getenv("PEARL_GUI_LOOPY");
-        if (env && (env[0] == 'y' || env[0] == 'Y'))
+        if (getenv_bool("PEARL_GUI_LOOPY", false))
             gui_style = GUI_LOOPY;
         else
             gui_style = GUI_MASYU;
--- a/puzzles.h
+++ b/puzzles.h
@@ -377,6 +377,8 @@
 char *bin2hex(const unsigned char *in, int inlen);
 unsigned char *hex2bin(const char *in, int outlen);
 
+bool getenv_bool(const char *name, bool dflt);
+
 /* Mixes two colours in specified proportions. */
 void colour_mix(const float src1[3], const float src2[3], float p,
                 float dst[3]);
--- a/range.c
+++ b/range.c
@@ -1324,10 +1324,8 @@
 	 */
 	{
 	    static int swap_buttons = -1;
-	    if (swap_buttons < 0) {
-		char *env = getenv("RANGE_SWAP_BUTTONS");
-		swap_buttons = (env && (env[0] == 'y' || env[0] == 'Y'));
-	    }
+	    if (swap_buttons < 0)
+                swap_buttons = getenv_bool("RANGE_SWAP_BUTTONS", false);
 	    if (swap_buttons) {
 		if (button == LEFT_BUTTON)
 		    button = RIGHT_BUTTON;
--- a/signpost.c
+++ b/signpost.c
@@ -2161,10 +2161,8 @@
                      * yourself which is more brain-twisting :-)
                      */
                     static int gear_mode = -1;
-                    if (gear_mode < 0) {
-                        char *env = getenv("SIGNPOST_GEARS");
-                        gear_mode = (env && (env[0] == 'y' || env[0] == 'Y'));
-                    }
+                    if (gear_mode < 0)
+                        gear_mode = getenv_bool("SIGNPOST_GEARS", false);
                     if (gear_mode)
                         sign = 1 - 2 * ((x ^ y) & 1);
                     else
--- a/slant.c
+++ b/slant.c
@@ -1681,10 +1681,8 @@
 	 */
 	{
 	    static int swap_buttons = -1;
-	    if (swap_buttons < 0) {
-		char *env = getenv("SLANT_SWAP_BUTTONS");
-		swap_buttons = (env && (env[0] == 'y' || env[0] == 'Y'));
-	    }
+	    if (swap_buttons < 0)
+                swap_buttons = getenv_bool("SLANT_SWAP_BUTTONS", false);
 	    if (swap_buttons) {
 		if (button == LEFT_BUTTON)
 		    button = RIGHT_BUTTON;
--- a/towers.c
+++ b/towers.c
@@ -1636,7 +1636,7 @@
     int i;
 
     ds->tilesize = 0;
-    ds->three_d = !getenv("TOWERS_2D");
+    ds->three_d = !getenv_bool("TOWERS_2D", false);
     ds->tiles = snewn((w+2)*(w+2), long);
     ds->drawn = snewn((w+2)*(w+2)*4, long);
     for (i = 0; i < (w+2)*(w+2)*4; i++)
--- a/windows.c
+++ b/windows.c
@@ -104,7 +104,7 @@
     static int debugging = -1;
 
     if (debugging == -1)
-        debugging = getenv("DEBUG_PUZZLES") ? 1 : 0;
+        debugging = getenv_bool("DEBUG_PUZZLES", false);
 
     if (debugging) {
         va_start(ap, fmt);