From da0e3907ec5f02dc9bc575a8dcd84cae0f79f8fa Mon Sep 17 00:00:00 2001 From: Kugel Fuhr <98353208+kugelfuhr@users.noreply.github.com> Date: Tue, 8 Jul 2025 20:45:23 +0200 Subject: [PATCH] Move the code to parse color strings into the CP_Parse() function and use it. --- src/ca65/main.c | 11 ++++------- src/common/consprop.c | 18 ++++++++++++++++++ src/common/consprop.h | 7 ++++++- 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/ca65/main.c b/src/ca65/main.c index 66a0f38af..0e5195757 100644 --- a/src/ca65/main.c +++ b/src/ca65/main.c @@ -504,14 +504,11 @@ static void OptBinIncludeDir (const char* Opt attribute ((unused)), const char* static void OptColor(const char* Opt, const char* Arg) /* Handle the --color option */ { - if (strcmp (Arg, "off") == 0 || strcmp (Arg, "false") == 0) { - CP_SetColorMode (CM_OFF); - } else if (strcmp (Arg, "auto") == 0) { - CP_SetColorMode (CM_AUTO); - } else if (strcmp (Arg, "on") == 0 || strcmp (Arg, "true") == 0) { - CP_SetColorMode (CM_ON); - } else { + ColorMode Mode = CP_Parse (Arg); + if (Mode == CM_INVALID) { AbEnd ("Invalid argument to %s: %s", Opt, Arg); + } else { + CP_SetColorMode (Mode); } } diff --git a/src/common/consprop.c b/src/common/consprop.c index cb1983953..61ee8102a 100644 --- a/src/common/consprop.c +++ b/src/common/consprop.c @@ -31,6 +31,7 @@ #include +#include #if !defined(_WIN32) # include #else @@ -153,6 +154,23 @@ void CP_Init (void) +ColorMode CP_Parse (const char* Mode) +/* Parse the given string which is assumed to be one of the color modes. + * Return the matching enum or CM_INVALID if there was no match. + */ +{ + if (strcmp (Mode, "off") == 0 || strcmp (Mode, "false") == 0) { + return CM_OFF; + } else if (strcmp (Mode, "auto") == 0) { + return CM_AUTO; + } else if (strcmp (Mode, "on") == 0 || strcmp (Mode, "true") == 0) { + return CM_ON; + } else { + return CM_INVALID; + } +} + + int CP_IsTTY (void) /* Return true if console output goes to a tty */ { diff --git a/src/common/consprop.h b/src/common/consprop.h index d27dda7cf..70a8160f4 100644 --- a/src/common/consprop.h +++ b/src/common/consprop.h @@ -48,7 +48,7 @@ /* Color mode for the program */ -enum ColorMode { CM_OFF, CM_AUTO, CM_ON }; +enum ColorMode { CM_INVALID = -1, CM_OFF, CM_AUTO, CM_ON }; typedef enum ColorMode ColorMode; /* Colors */ @@ -96,6 +96,11 @@ void CP_Init (void); ** data from this module. **/ +ColorMode CP_Parse (const char* Mode); +/* Parse the given string which is assumed to be one of the color modes. +** Return the matching enum or CM_INVALID if there was no match. +*/ + int CP_IsTTY (void); /* Return true if console output goes to a tty */