libutil

C Utility Library
git clone http://git.omkov.net/libutil
Log | Tree | Refs | README | LICENCE | Download

AuthorJamozed <[email protected]>
Date2021-11-27 04:58:37
Commit1e6cded1c9d82af2053cdcb7c34aa3d92065392f
Parent98e59396782378640b62f7f3575a2b26a934ca9e

error: Add alert function

Diffstat

M src/error.c | 15 +++++++++++----
M src/error.h | 43 ++++++++++++++++++++++++++++++++++++++++++-
M src/test/test_error.c | 4 +++-

3 files changed, 56 insertions, 6 deletions

diff --git a/src/error.c b/src/error.c
index 698253d..0e306e9 100644
--- a/src/error.c
+++ b/src/error.c
@@ -1,4 +1,4 @@
-// util/error.c, version 1.0.3
+// util/error.h, version 1.1.1
 // Error source file from libutil
 // Copyright (C) 2020, Jakob Wakeling
 // All rights reserved.
@@ -43,19 +43,26 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 char *A0 = NULL;
 bool warned = false;
 
-/* Print an error message and exit */
+/* Print an error message and exit. */
 noreturn void error(int status, const char *format, ...) {
 	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
 	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
 	fputc('\n', stderr); exit(status);
 }
 
-/* Print a warning message and set the warned flag */
+/* Print a warning message and set the warned flag. */
 void warn(const char *format, ...) {
 	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
 	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
 	fputc('\n', stderr); warned = true; return;
 }
 
-/* Shorthand for strerror(errno) */
+/* Print a warning message but do not set the warned flag. */
+void alert(const char *format, ...) {
+	fflush(stdout); if (A0) { fputs(A0, stderr); fputs(": ", stderr); }
+	va_list ap; va_start(ap, format); vfprintf(stderr, format, ap); va_end(ap);
+	fputc('\n', stderr); return;
+}
+
+/* Shorthand for strerror(errno). DEPRECIATED, use the SERR macro. */
 char *serr(void) { return strerror(errno); }
diff --git a/src/error.h b/src/error.h
index 73648ac..794e5c3 100644
--- a/src/error.h
+++ b/src/error.h
@@ -1,4 +1,4 @@
-// util/error.h, version 1.0.3
+// util/error.h, version 1.1.1
 // Error header file from libutil
 // Copyright (C) 2020, Jakob Wakeling
 // All rights reserved.
@@ -33,15 +33,55 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 #ifndef UTIL_ERROR_H_38W06M3W
 #define UTIL_ERROR_H_38W06M3W
 
+#include <errno.h>
 #include <stdbool.h>
 #include <stdnoreturn.h>
+#include <string.h>
+
+/* Warn and then return status */
+#define WARN_R(status, format, ...) do { \
+	warn(format, __VA_ARGS__); return status; \
+} while (0)
+
+/* Warn and then reset errno */
+#define WARN_E(format, ...) do { \
+	warn(format, __VA_ARGS__); errno = 0; \
+} while (0)
+
+/* Warn, reset errno, and then return status */
+#define WARN_RE(status, format, ...) do { \
+	warn(format, __VA_ARGS__); errno = 0; return status; \
+} while (0)
+
+/* Alert and then return status */
+#define ALERT_R(status, format, ...) do { \
+	alert(format, __VA_ARGS__); return status; \
+} while (0)
+
+/* Alert and then reset errno */
+#define ALERT_E(format, ...) do { \
+	alert(format, __VA_ARGS__); errno = 0; \
+} while (0)
+
+/* Alert, reset errno, and then return status */
+#define ALERT_RE(status, format, ...) do { \
+	alert(format, __VA_ARGS__); errno = 0; return status; \
+} while (0)
+
+/* Shorthand for strerror(serrno). */
+#define SERR (strerror(errno))
 
 extern char *A0;
 extern bool warned;
 
+/* Print an error message and exit. */
 extern noreturn void error(int status, const char *format, ...);
+/* Print a warning message and set the warned flag. */
 extern void warn(const char *format, ...);
+/* Print a warning message but do not set the warned flag. */
+extern void alert(const char *format, ...);
 
+/* Shorthand for strerror(errno). DEPRECIATED, use the SERR macro. */
 extern char *serr(void);
 
 #endif // UTIL_ERROR_H_38W06M3W
diff --git a/src/test/test_error.c b/src/test/test_error.c
index 0ceccec..4e6d8a7 100644
--- a/src/test/test_error.c
+++ b/src/test/test_error.c
@@ -20,7 +20,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 #include "../error.h"
 
 int main(int ac, char *av[]) { A0 = av[0];
-	warn("%s: %s", "warn", serr());
-	error(0, "%s: %s", "error", serr());
+	warn("%s: %s", "warn", SERR);
+	alert("%s: %s", "alert", SERR);
+	error(0, "%s: %s", "error", SERR);
 	return 1; // Fail
 }