ESH

Executive Shell
git clone http://git.omkov.net/ESH
Log | Tree | Refs | README | Download

AuthorJamozed <[email protected]>
Date2021-11-26 13:00:41
Commit557f683de8b02a5c484e4c981c10efb377a6a546
Parent19538ba5d979a5bd3fb6c78d7312225bf3ed287d

Add shorthand macros to util/error

Diffstat

M src/util/error.c | 8 ++++----
M src/util/error.h | 35 +++++++++++++++++++++++++++++++++++

2 files changed, 39 insertions, 4 deletions

diff --git a/src/util/error.c b/src/util/error.c
index e51a00d..d6fdfdd 100644
--- a/src/util/error.c
+++ b/src/util/error.c
@@ -43,26 +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;
 }
 
-/* Print a warning message but do not set the warned flag */
+/* 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) */
+/* Shorthand for strerror(errno). DEPRECIATED, use the SERR macro. */
 char *serr(void) { return strerror(errno); }
diff --git a/src/util/error.h b/src/util/error.h
index 2a242ec..44461a3 100644
--- a/src/util/error.h
+++ b/src/util/error.h
@@ -37,15 +37,50 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH THE SOFTWARE.
 #include <stdbool.h>
 #include <stdnoreturn.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 // OMKOV_LIB_ERROR_H_38W06M3W