diff --git a/emcc.c b/emcc.c index eedfc35..31aedbd 100644 --- a/emcc.c +++ b/emcc.c @@ -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); diff --git a/fifteen.c b/fifteen.c index 0d19288..11e1a7b 100644 --- a/fifteen.c +++ b/fifteen.c @@ -714,10 +714,8 @@ static char *interpret_move(const game_state *state, game_ui *ui, 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 */ diff --git a/lightup.c b/lightup.c index 181c502..db9fa2d 100644 --- a/lightup.c +++ b/lightup.c @@ -2168,11 +2168,8 @@ static void tile_redraw(drawing *dr, game_drawstate *ds, 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, diff --git a/loopy.c b/loopy.c index 0a22e6a..c627f2b 100644 --- a/loopy.c +++ b/loopy.c @@ -3292,11 +3292,8 @@ static void game_redraw_line(drawing *dr, game_drawstate *ds, 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 { diff --git a/misc.c b/misc.c index c4c4b69..f18f17e 100644 --- a/misc.c +++ b/misc.c @@ -198,6 +198,14 @@ char *fgetline(FILE *fp) 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]) diff --git a/pearl.c b/pearl.c index ac9274a..fe023a4 100644 --- a/pearl.c +++ b/pearl.c @@ -1929,8 +1929,7 @@ static int get_gui_style(void) 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; diff --git a/puzzles.h b/puzzles.h index ae48b2e..36ba2a9 100644 --- a/puzzles.h +++ b/puzzles.h @@ -377,6 +377,8 @@ char *fgetline(FILE *fp); 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]); diff --git a/range.c b/range.c index c360b75..0887f9a 100644 --- a/range.c +++ b/range.c @@ -1324,10 +1324,8 @@ static char *interpret_move(const game_state *state, game_ui *ui, */ { 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; diff --git a/signpost.c b/signpost.c index 97beefd..1fbfbf1 100644 --- a/signpost.c +++ b/signpost.c @@ -2161,10 +2161,8 @@ static void game_redraw(drawing *dr, game_drawstate *ds, * 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 diff --git a/slant.c b/slant.c index 8d0fddc..f607733 100644 --- a/slant.c +++ b/slant.c @@ -1681,10 +1681,8 @@ static char *interpret_move(const game_state *state, game_ui *ui, */ { 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; diff --git a/towers.c b/towers.c index b1f6a16..dd774a1 100644 --- a/towers.c +++ b/towers.c @@ -1636,7 +1636,7 @@ static game_drawstate *game_new_drawstate(drawing *dr, const game_state *state) 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++) diff --git a/windows.c b/windows.c index 7287478..5735850 100644 --- a/windows.c +++ b/windows.c @@ -104,7 +104,7 @@ void debug_printf(const char *fmt, ...) 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);