aboutsummaryrefslogtreecommitdiff
path: root/src/error_exit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/error_exit.c')
-rw-r--r--src/error_exit.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/error_exit.c b/src/error_exit.c
new file mode 100644
index 0000000..748de99
--- /dev/null
+++ b/src/error_exit.c
@@ -0,0 +1,63 @@
+/* error_exit.c -- print an error message and cleanly shutdown cactus */
+/* $Id$ */
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+
+#include "cactus.h"
+#include "thorn_cartoon_2d/src/error_exit.h"
+
+/*
+ * Jonathan Thornburg's favorite syntactic sugar to make the two branches
+ * of an if-else statement symmetrical:
+ *
+ * Also note (in)famous joke saying
+ * "syntactic sugar causes cancer of the semicolon"
+ * :) :) :)
+ */
+#define then /* empty */
+
+/*****************************************************************************/
+
+/*
+ * This function prints an error message (formatted via vfprintf(3S)),
+ * then cleanly shuts down cactus. It does *not* return to its caller.
+ *
+ * Despite not actually returning, this function is declared as returning
+ * an int , so it may be easily used in conditional expressions like
+ * foo = bar_ok ? baz(bar) : error_exit(...);
+ *
+ * Usage:
+ * error_exit(exit_code, message, args...)
+ * where exit_code is one of the codes defined in "error_exit.h"
+ *
+ * ***** THIS VERSION IS FOR ANSI/ISO C *****
+ *
+ * Arguments:
+ * exit_code = (in) One of the codes defined in "error_exit.h", or any
+ * other integer to pass back to the shell as our exit
+ * status.
+ * format = (in) vprintf(3S) format string for error message to be printed.
+ * args... = (in) Any additional arguments are (presumably) used in formatting
+ * the error message string.
+ */
+#ifdef __cplusplus
+extern "C"
+#endif
+/*VARARGS*/
+/* PUBLIC */
+int error_exit(const int exit_code, const char *const format, ...)
+{
+va_list ap;
+
+va_start(ap, format);
+vfprintf(stderr, format, ap);
+va_end(ap);
+
+ if (exit_code == 0) {
+ then STOP; /*NOTREACHED*/
+ } else if (exit_code > 0) {
+ then ERROR_STOP(exit_code); /*NOTREACHED*/
+ } else ERROR_ABORT(exit_code); /*NOTREACHED*/
+}