From 1bf1219e072f3728cb535c92920d9d45b65f746a Mon Sep 17 00:00:00 2001 From: Rangi42 Date: Tue, 12 Aug 2025 15:53:57 -0400 Subject: [PATCH] Factor out shared `--color`-parsing code --- include/style.hpp | 2 +- src/asm/main.cpp | 6 +----- src/fix/main.cpp | 10 ++-------- src/gfx/main.cpp | 10 ++-------- src/link/main.cpp | 10 ++-------- src/style.cpp | 35 ++++++++++++++++++++++++++++++----- 6 files changed, 38 insertions(+), 35 deletions(-) diff --git a/include/style.hpp b/include/style.hpp index c64205fe..cad69905 100644 --- a/include/style.hpp +++ b/include/style.hpp @@ -35,7 +35,7 @@ enum StyleColor { #endif }; -void style_Enable(bool enable); +bool style_Parse(char const *arg); void style_Set(FILE *file, StyleColor color, bool bold); void style_Reset(FILE *file); diff --git a/src/asm/main.cpp b/src/asm/main.cpp index bdc79022..47112a3d 100644 --- a/src/asm/main.cpp +++ b/src/asm/main.cpp @@ -476,11 +476,7 @@ int main(int argc, char *argv[]) { case 0: switch (longOpt) { case 'c': - if (!strcasecmp(musl_optarg, "always")) { - style_Enable(true); - } else if (!strcasecmp(musl_optarg, "never")) { - style_Enable(false); - } else if (strcasecmp(musl_optarg, "auto")) { + if (!style_Parse(musl_optarg)) { fatal("Invalid argument for option '--color'"); } break; diff --git a/src/fix/main.cpp b/src/fix/main.cpp index 4ce119f4..4d9a9bd6 100644 --- a/src/fix/main.cpp +++ b/src/fix/main.cpp @@ -831,14 +831,8 @@ int main(int argc, char *argv[]) { // Long-only options case 0: - if (longOpt == 'c') { - if (!strcasecmp(musl_optarg, "always")) { - style_Enable(true); - } else if (!strcasecmp(musl_optarg, "never")) { - style_Enable(false); - } else if (strcasecmp(musl_optarg, "auto")) { - fatal("Invalid argument for option '--color'"); - } + if (longOpt == 'c' && !style_Parse(musl_optarg)) { + fatal("Invalid argument for option '--color'"); } break; diff --git a/src/gfx/main.cpp b/src/gfx/main.cpp index 7784a0bd..4795a4b2 100644 --- a/src/gfx/main.cpp +++ b/src/gfx/main.cpp @@ -544,14 +544,8 @@ static char *parseArgv(int argc, char *argv[]) { options.columnMajor = true; break; case 0: // Long-only options - if (longOpt == 'c') { - if (!strcasecmp(musl_optarg, "always")) { - style_Enable(true); - } else if (!strcasecmp(musl_optarg, "never")) { - style_Enable(false); - } else if (strcasecmp(musl_optarg, "auto")) { - fatal("Invalid argument for option '--color'"); - } + if (longOpt == 'c' && !style_Parse(musl_optarg)) { + fatal("Invalid argument for option '--color'"); } break; case 1: // Positional argument, requested by leading `-` in opt string diff --git a/src/link/main.cpp b/src/link/main.cpp index 4380b3e9..5aa509fa 100644 --- a/src/link/main.cpp +++ b/src/link/main.cpp @@ -392,14 +392,8 @@ int main(int argc, char *argv[]) { options.is32kMode = true; break; case 0: // Long-only options - if (longOpt == 'c') { - if (!strcasecmp(musl_optarg, "always")) { - style_Enable(true); - } else if (!strcasecmp(musl_optarg, "never")) { - style_Enable(false); - } else if (strcasecmp(musl_optarg, "auto")) { - fatal("Invalid argument for option '--color'"); - } + if (longOpt == 'c' && !style_Parse(musl_optarg)) { + fatal("Invalid argument for option '--color'"); } break; default: diff --git a/src/style.cpp b/src/style.cpp index 9a088756..7ce30fa0 100644 --- a/src/style.cpp +++ b/src/style.cpp @@ -8,7 +8,7 @@ #include // getenv #include -#include "platform.hpp" // isatty +#include "platform.hpp" // isatty, strcasecmp #if !STYLE_ANSI // clang-format off: maintain `include` order @@ -36,7 +36,7 @@ static HANDLE getHandle(FILE *file) { } #endif // !STYLE_ANSI -static Tribool forceStyle = []() { +static Tribool const envStyle = []() { if (char const *forceColor = getenv("FORCE_COLOR"); forceColor && strcmp(forceColor, "") && strcmp(forceColor, "0")) { return TRI_YES; @@ -48,6 +48,8 @@ static Tribool forceStyle = []() { return TRI_MAYBE; }(); +static Tribool argStyle = TRI_MAYBE; + static bool isTerminal(FILE *file) { static bool isOutTerminal = isatty(STDOUT_FILENO); static bool isErrTerminal = isatty(STDERR_FILENO); @@ -56,11 +58,34 @@ static bool isTerminal(FILE *file) { } static bool allowStyle(FILE *file) { - return forceStyle == TRI_YES || (forceStyle == TRI_MAYBE && isTerminal(file)); + if (argStyle == TRI_YES) { + return true; + } else if (argStyle == TRI_NO) { + return false; + } + + if (envStyle == TRI_YES) { + return true; + } else if (envStyle == TRI_NO) { + return false; + } + + return isTerminal(file); } -void style_Enable(bool enable) { - forceStyle = enable ? TRI_YES : TRI_NO; +bool style_Parse(char const *arg) { + if (!strcasecmp(arg, "always")) { + argStyle = TRI_YES; + return true; + } else if (!strcasecmp(arg, "never")) { + argStyle = TRI_NO; + return true; + } else if (!strcasecmp(arg, "auto")) { + argStyle = TRI_MAYBE; + return true; + } else { + return false; + } } void style_Set(FILE *file, StyleColor color, bool bold) {