From 80ff833dd8ad011b579bff26ac654819e6735bce Mon Sep 17 00:00:00 2001 From: Michael Pavone Date: Sat, 25 Jul 2015 18:22:07 -0700 Subject: Use a new fatal_error function instead of calling fprintf and exit for fatal errors. This new function more gracefully handles the case in which BlastEm was not started from a terminal or disconnected from ther terminal (Windows). --- util.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'util.c') diff --git a/util.c b/util.c index 2a59b08..3ad290d 100644 --- a/util.c +++ b/util.c @@ -8,6 +8,9 @@ #include #include +#include "blastem.h" //for headless global +#include "render.h" //for render_errorbox + char * alloc_concat(char * first, char * second) { int flen = strlen(first); @@ -86,6 +89,62 @@ void set_exe_str(char * str) exe_str = str; } +void fatal_error(char *format, ...) +{ + va_list args; + va_start(args, format); + if (!headless) { + //take a guess at the final size + size_t size = strlen(format) * 2; + char *buf = malloc(size); + size_t actual = vsnprintf(buf, size, format, args); + if (actual >= size) { + actual++; + free(buf); + buf = malloc(actual); + va_end(args); + va_start(args, format); + vsnprintf(buf, actual, format, args); + } + fputs(buf, stderr); + render_errorbox("Fatal Error", buf); + free(buf); + } else { + vfprintf(stderr, format, args); + } + va_end(args); + exit(1); +} + +void info_message(char *format, ...) +{ + va_list args; + va_start(args, format); +#ifndef _WIN32 + if (headless || (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO))) { + vprintf(format, args); + } else { +#endif + size_t size = strlen(format) * 2; + char *buf = malloc(size); + size_t actual = vsnprintf(buf, size, format, args); + if (actual >= size) { + actual++; + free(buf); + buf = malloc(actual); + va_end(args); + va_start(args, format); + vsnprintf(buf, actual, format, args); + } + fputs(buf, stdout); + render_infobox("BlastEm Info", buf); + free(buf); +#ifndef _WIN32 + } +#endif + va_end(args); +} + #ifdef _WIN32 #include "Shlobj.h" #include "Windows.h" -- cgit v1.2.3