about summary refs log tree commit diff
path: root/boot
diff options
context:
space:
mode:
authorMel <mel@rnrd.eu>2025-08-24 03:11:20 +0200
committerMel <mel@rnrd.eu>2025-08-24 03:11:20 +0200
commit0e08695d031ebb2e00a13582a75e1f27c2d6c73a (patch)
tree8d0700a7f1b87508e09feb2125abb0b2dfaacb04 /boot
parentcbda0181b9f5480cfce2c08e17fd998babb77d4d (diff)
downloadcatskill-0e08695d031ebb2e00a13582a75e1f27c2d6c73a.tar.zst
catskill-0e08695d031ebb2e00a13582a75e1f27c2d6c73a.zip
Parameterize/improve failure and check functions
Signed-off-by: Mel <mel@rnrd.eu>
Diffstat (limited to 'boot')
-rw-r--r--boot/common.c39
1 files changed, 29 insertions, 10 deletions
diff --git a/boot/common.c b/boot/common.c
index 4f9c2ad..b8587bc 100644
--- a/boot/common.c
+++ b/boot/common.c
@@ -79,31 +79,50 @@
 #define ANSI_UNDERLINE ANSI("4")
 #define ANSI_NO_UNDERLINE ANSI("24")
 
+#define FAILURE_MESSAGE ANSI_BOLD ";( sorry, a failure has occurred..." ANSI_NO_BOLD
+
 // call on irrecoverable failure.
 // prints a very sad, apologetic message for
 // the user and aborts program with failure status.
 NORETURN
 void
-failure(const ascii* message)
+failure(const ascii* message, ...)
 {
     fflush(stdout); // flush stdout to ensure any message is printed before the error.
 
-    const ascii* format = ANSI_RED ANSI_BOLD
-        ";( sorry, a failure has occurred..." ANSI_NO_BOLD "\n"
-        "-> %s!"
-        "\n" ANSI_RESET;
-    fprintf(stderr, format, message);
+    fprintf(stderr, ANSI_RED FAILURE_MESSAGE "\n-> ");
 
+    va_list args;
+    va_start(args, message);
+    vfprintf(stderr, message, args);
+    va_end(args);
+
+    fprintf(stderr, "!\n" ANSI_RESET);
     exit(EXIT_FAILURE);
 }
 
-// check a condition, triggering a failure if it's false.
+// internal check function, use the check() macro instead.
 void
-check(bool condition, const ascii* message)
-{
-    if (!condition) failure(message);
+_check(bool condition, const ascii* condition_str, const ascii* message, ...)
+{
+    if (!condition) {
+        va_list args;
+        va_start(args, message);
+        fflush(stdout);
+        fprintf(stderr, ANSI_RED FAILURE_MESSAGE "\n");
+        fprintf(stderr, "-> check failed: %s\n", condition_str);
+        fprintf(stderr, "-> ");
+        vfprintf(stderr, message, args);
+        fprintf(stderr, "!\n" ANSI_RESET);
+        va_end(args);
+        exit(EXIT_FAILURE);
+    }
 }
 
+// check a condition, triggering a failure if it's false.
+// prints both the condition and the formatted message.
+#define check(condition, message, ...) _check((condition), #condition, (message), ##__VA_ARGS__)
+
 NORETURN
 void
 unreachable()