From 3e7acb02962b05db8cf83e5b6b082ac368c8c88e Mon Sep 17 00:00:00 2001 From: Mel Date: Thu, 12 Jun 2025 18:20:21 +0200 Subject: Fix `failure()` colors + stream flush, define ANSI escape codes Signed-off-by: Mel --- boot/common.c | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) (limited to 'boot/common.c') diff --git a/boot/common.c b/boot/common.c index 6851312..b9da4cf 100644 --- a/boot/common.c +++ b/boot/common.c @@ -39,17 +39,46 @@ #define nil NULL #define unknown void +// ansi escape codes for terminal color and style +#define ANSI(code) "\33[" code "m" + +#define ANSI_RESET ANSI("0") +#define ANSI_DEFAULT ANSI("39") + +#define ANSI_RED ANSI("31") +#define ANSI_RED_BG ANSI("41") +#define ANSI_GREEN ANSI("32") +#define ANSI_GREEN_BG ANSI("42") +#define ANSI_YELLOW ANSI("33") +#define ANSI_YELLOW_BG ANSI("43") +#define ANSI_BLUE ANSI("34") +#define ANSI_BLUE_BG ANSI("44") +#define ANSI_MAGENTA ANSI("35") +#define ANSI_MAGENTA_BG ANSI("45") +#define ANSI_CYAN ANSI("36") +#define ANSI_CYAN_BG ANSI("46") +#define ANSI_WHITE ANSI("37") +#define ANSI_WHITE_BG ANSI("47") +#define ANSI_BLACK ANSI("30") +#define ANSI_BLACK_BG ANSI("40") + +#define ANSI_BOLD ANSI("1") +#define ANSI_NO_BOLD ANSI("22") +#define ANSI_UNDERLINE ANSI("4") +#define ANSI_NO_UNDERLINE ANSI("24") + // call on irrecoverable failure. // prints a very sad, apologetic message for // the user and aborts program with failure status. void failure(const ascii* message) { + fflush(stdout); // flush stdout to ensure any message is printed before the error. + const ascii* format = - "\\e[0;31m" - ";( sorry, a failure has occurred...\n" - "-> %s!\n" - "\\e[0m"; + ANSI_RED ANSI_BOLD ";( sorry, a failure has occurred..." ANSI_NO_BOLD "\n" + "-> %s!" "\n" + ANSI_RESET; fprintf(stderr, format, message); exit(EXIT_FAILURE); -- cgit 1.4.1