comparison util.c @ 792:724bbec47f86

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).
author Michael Pavone <pavone@retrodev.com>
date Sat, 25 Jul 2015 18:22:07 -0700
parents 13d3744b170e
children 792be135d3af
comparison
equal deleted inserted replaced
791:60686f8d5e48 792:724bbec47f86
6 6
7 #include <sys/types.h> 7 #include <sys/types.h>
8 #include <sys/stat.h> 8 #include <sys/stat.h>
9 #include <unistd.h> 9 #include <unistd.h>
10 10
11 #include "blastem.h" //for headless global
12 #include "render.h" //for render_errorbox
13
11 char * alloc_concat(char * first, char * second) 14 char * alloc_concat(char * first, char * second)
12 { 15 {
13 int flen = strlen(first); 16 int flen = strlen(first);
14 int slen = strlen(second); 17 int slen = strlen(second);
15 char * ret = malloc(flen + slen + 1); 18 char * ret = malloc(flen + slen + 1);
82 static char * exe_str; 85 static char * exe_str;
83 86
84 void set_exe_str(char * str) 87 void set_exe_str(char * str)
85 { 88 {
86 exe_str = str; 89 exe_str = str;
90 }
91
92 void fatal_error(char *format, ...)
93 {
94 va_list args;
95 va_start(args, format);
96 if (!headless) {
97 //take a guess at the final size
98 size_t size = strlen(format) * 2;
99 char *buf = malloc(size);
100 size_t actual = vsnprintf(buf, size, format, args);
101 if (actual >= size) {
102 actual++;
103 free(buf);
104 buf = malloc(actual);
105 va_end(args);
106 va_start(args, format);
107 vsnprintf(buf, actual, format, args);
108 }
109 fputs(buf, stderr);
110 render_errorbox("Fatal Error", buf);
111 free(buf);
112 } else {
113 vfprintf(stderr, format, args);
114 }
115 va_end(args);
116 exit(1);
117 }
118
119 void info_message(char *format, ...)
120 {
121 va_list args;
122 va_start(args, format);
123 #ifndef _WIN32
124 if (headless || (isatty(STDOUT_FILENO) && isatty(STDIN_FILENO))) {
125 vprintf(format, args);
126 } else {
127 #endif
128 size_t size = strlen(format) * 2;
129 char *buf = malloc(size);
130 size_t actual = vsnprintf(buf, size, format, args);
131 if (actual >= size) {
132 actual++;
133 free(buf);
134 buf = malloc(actual);
135 va_end(args);
136 va_start(args, format);
137 vsnprintf(buf, actual, format, args);
138 }
139 fputs(buf, stdout);
140 render_infobox("BlastEm Info", buf);
141 free(buf);
142 #ifndef _WIN32
143 }
144 #endif
145 va_end(args);
87 } 146 }
88 147
89 #ifdef _WIN32 148 #ifdef _WIN32
90 #include "Shlobj.h" 149 #include "Shlobj.h"
91 #include "Windows.h" 150 #include "Windows.h"