Mercurial > sdl-ios-xcode
comparison src/main/win32/SDL_win32_main.c @ 4246:8b8314cc34a6 SDL-1.2
Fixed bug #810
Lauri Kenttä 2009-09-26 06:42:23 PDT
Support for disabling stdio redirect with environment variables.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 26 Sep 2009 23:17:08 +0000 |
parents | 3c068a8c016f |
children |
comparison
equal
deleted
inserted
replaced
4245:0319c84d274d | 4246:8b8314cc34a6 |
---|---|
36 | 36 |
37 /* The standard output files */ | 37 /* The standard output files */ |
38 #define STDOUT_FILE TEXT("stdout.txt") | 38 #define STDOUT_FILE TEXT("stdout.txt") |
39 #define STDERR_FILE TEXT("stderr.txt") | 39 #define STDERR_FILE TEXT("stderr.txt") |
40 | 40 |
41 #ifndef NO_STDIO_REDIRECT | 41 /* Set a variable to tell if the stdio redirect has been enabled. */ |
42 # ifdef _WIN32_WCE | 42 static int stdioRedirectEnabled = 0; |
43 | |
44 #ifdef _WIN32_WCE | |
43 static wchar_t stdoutPath[MAX_PATH]; | 45 static wchar_t stdoutPath[MAX_PATH]; |
44 static wchar_t stderrPath[MAX_PATH]; | 46 static wchar_t stderrPath[MAX_PATH]; |
45 # else | 47 #else |
46 static char stdoutPath[MAX_PATH]; | 48 static char stdoutPath[MAX_PATH]; |
47 static char stderrPath[MAX_PATH]; | 49 static char stderrPath[MAX_PATH]; |
48 # endif | |
49 #endif | 50 #endif |
50 | 51 |
51 #if defined(_WIN32_WCE) && _WIN32_WCE < 300 | 52 #if defined(_WIN32_WCE) && _WIN32_WCE < 300 |
52 /* seems to be undefined in Win CE although in online help */ | 53 /* seems to be undefined in Win CE although in online help */ |
53 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t')) | 54 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t')) |
156 { | 157 { |
157 SDL_Quit(); | 158 SDL_Quit(); |
158 } | 159 } |
159 | 160 |
160 /* Remove the output files if there was no output written */ | 161 /* Remove the output files if there was no output written */ |
161 static void cleanup_output(void) | 162 static void cleanup_output(void) { |
162 { | |
163 #ifndef NO_STDIO_REDIRECT | |
164 FILE *file; | 163 FILE *file; |
165 int empty; | 164 int empty; |
166 #endif | |
167 | 165 |
168 /* Flush the output in case anything is queued */ | 166 /* Flush the output in case anything is queued */ |
169 fclose(stdout); | 167 fclose(stdout); |
170 fclose(stderr); | 168 fclose(stderr); |
171 | 169 |
172 #ifndef NO_STDIO_REDIRECT | 170 /* Without redirection we're done */ |
171 if (!stdioRedirectEnabled) { | |
172 return; | |
173 } | |
174 | |
173 /* See if the files have any output in them */ | 175 /* See if the files have any output in them */ |
174 if ( stdoutPath[0] ) { | 176 if ( stdoutPath[0] ) { |
175 file = fopen(stdoutPath, TEXT("rb")); | 177 file = fopen(stdoutPath, TEXT("rb")); |
176 if ( file ) { | 178 if ( file ) { |
177 empty = (fgetc(file) == EOF) ? 1 : 0; | 179 empty = (fgetc(file) == EOF) ? 1 : 0; |
189 if ( empty ) { | 191 if ( empty ) { |
190 remove(stderrPath); | 192 remove(stderrPath); |
191 } | 193 } |
192 } | 194 } |
193 } | 195 } |
194 #endif | 196 } |
197 | |
198 /* Redirect the output (stdout and stderr) to a file */ | |
199 static void redirect_output(void) | |
200 { | |
201 DWORD pathlen; | |
202 #ifdef _WIN32_WCE | |
203 wchar_t path[MAX_PATH]; | |
204 #else | |
205 char path[MAX_PATH]; | |
206 #endif | |
207 FILE *newfp; | |
208 | |
209 pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path)); | |
210 while ( pathlen > 0 && path[pathlen] != '\\' ) { | |
211 --pathlen; | |
212 } | |
213 path[pathlen] = '\0'; | |
214 | |
215 #ifdef _WIN32_WCE | |
216 wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); | |
217 wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); | |
218 #else | |
219 SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); | |
220 SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); | |
221 #endif | |
222 | |
223 /* Redirect standard input and standard output */ | |
224 newfp = freopen(stdoutPath, TEXT("w"), stdout); | |
225 | |
226 #ifndef _WIN32_WCE | |
227 if ( newfp == NULL ) { /* This happens on NT */ | |
228 #if !defined(stdout) | |
229 stdout = fopen(stdoutPath, TEXT("w")); | |
230 #else | |
231 newfp = fopen(stdoutPath, TEXT("w")); | |
232 if ( newfp ) { | |
233 *stdout = *newfp; | |
234 } | |
235 #endif | |
236 } | |
237 #endif /* _WIN32_WCE */ | |
238 | |
239 #ifdef _WIN32_WCE | |
240 wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) ); | |
241 wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); | |
242 #else | |
243 SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) ); | |
244 SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) ); | |
245 #endif | |
246 | |
247 newfp = freopen(stderrPath, TEXT("w"), stderr); | |
248 #ifndef _WIN32_WCE | |
249 if ( newfp == NULL ) { /* This happens on NT */ | |
250 #if !defined(stderr) | |
251 stderr = fopen(stderrPath, TEXT("w")); | |
252 #else | |
253 newfp = fopen(stderrPath, TEXT("w")); | |
254 if ( newfp ) { | |
255 *stderr = *newfp; | |
256 } | |
257 #endif | |
258 } | |
259 #endif /* _WIN32_WCE */ | |
260 | |
261 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */ | |
262 setbuf(stderr, NULL); /* No buffering */ | |
263 stdioRedirectEnabled = 1; | |
195 } | 264 } |
196 | 265 |
197 #if defined(_MSC_VER) && !defined(_WIN32_WCE) | 266 #if defined(_MSC_VER) && !defined(_WIN32_WCE) |
198 /* The VC++ compiler needs main defined */ | 267 /* The VC++ compiler needs main defined */ |
199 #define console_main main | 268 #define console_main main |
261 { | 330 { |
262 HINSTANCE handle; | 331 HINSTANCE handle; |
263 char **argv; | 332 char **argv; |
264 int argc; | 333 int argc; |
265 char *cmdline; | 334 char *cmdline; |
335 char *env_str; | |
266 #ifdef _WIN32_WCE | 336 #ifdef _WIN32_WCE |
267 wchar_t *bufp; | 337 wchar_t *bufp; |
268 int nLen; | 338 int nLen; |
269 #else | 339 #else |
270 char *bufp; | 340 char *bufp; |
271 size_t nLen; | 341 size_t nLen; |
272 #endif | |
273 #ifndef NO_STDIO_REDIRECT | |
274 DWORD pathlen; | |
275 #ifdef _WIN32_WCE | |
276 wchar_t path[MAX_PATH]; | |
277 #else | |
278 char path[MAX_PATH]; | |
279 #endif | |
280 FILE *newfp; | |
281 #endif | 342 #endif |
282 | 343 |
283 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't | 344 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't |
284 keep them open. This is a hack.. hopefully it will be fixed | 345 keep them open. This is a hack.. hopefully it will be fixed |
285 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded. | 346 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded. |
287 handle = LoadLibrary(TEXT("DDRAW.DLL")); | 348 handle = LoadLibrary(TEXT("DDRAW.DLL")); |
288 if ( handle != NULL ) { | 349 if ( handle != NULL ) { |
289 FreeLibrary(handle); | 350 FreeLibrary(handle); |
290 } | 351 } |
291 | 352 |
353 /* Check for stdio redirect settings and do the redirection */ | |
354 if ((env_str = SDL_getenv("SDL_STDIO_REDIRECT"))) { | |
355 if (SDL_atoi(env_str)) { | |
356 redirect_output(); | |
357 } | |
358 } | |
292 #ifndef NO_STDIO_REDIRECT | 359 #ifndef NO_STDIO_REDIRECT |
293 pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path)); | 360 else { |
294 while ( pathlen > 0 && path[pathlen] != '\\' ) { | 361 redirect_output(); |
295 --pathlen; | 362 } |
296 } | 363 #endif |
297 path[pathlen] = '\0'; | |
298 | |
299 #ifdef _WIN32_WCE | |
300 wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); | |
301 wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); | |
302 #else | |
303 SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) ); | |
304 SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); | |
305 #endif | |
306 | |
307 /* Redirect standard input and standard output */ | |
308 newfp = freopen(stdoutPath, TEXT("w"), stdout); | |
309 | |
310 #ifndef _WIN32_WCE | |
311 if ( newfp == NULL ) { /* This happens on NT */ | |
312 #if !defined(stdout) | |
313 stdout = fopen(stdoutPath, TEXT("w")); | |
314 #else | |
315 newfp = fopen(stdoutPath, TEXT("w")); | |
316 if ( newfp ) { | |
317 *stdout = *newfp; | |
318 } | |
319 #endif | |
320 } | |
321 #endif /* _WIN32_WCE */ | |
322 | |
323 #ifdef _WIN32_WCE | |
324 wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) ); | |
325 wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) ); | |
326 #else | |
327 SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) ); | |
328 SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) ); | |
329 #endif | |
330 | |
331 newfp = freopen(stderrPath, TEXT("w"), stderr); | |
332 #ifndef _WIN32_WCE | |
333 if ( newfp == NULL ) { /* This happens on NT */ | |
334 #if !defined(stderr) | |
335 stderr = fopen(stderrPath, TEXT("w")); | |
336 #else | |
337 newfp = fopen(stderrPath, TEXT("w")); | |
338 if ( newfp ) { | |
339 *stderr = *newfp; | |
340 } | |
341 #endif | |
342 } | |
343 #endif /* _WIN32_WCE */ | |
344 | |
345 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */ | |
346 setbuf(stderr, NULL); /* No buffering */ | |
347 #endif /* !NO_STDIO_REDIRECT */ | |
348 | 364 |
349 #ifdef _WIN32_WCE | 365 #ifdef _WIN32_WCE |
350 nLen = wcslen(szCmdLine)+128+1; | 366 nLen = wcslen(szCmdLine)+128+1; |
351 bufp = SDL_stack_alloc(wchar_t, nLen*2); | 367 bufp = SDL_stack_alloc(wchar_t, nLen*2); |
352 wcscpy (bufp, TEXT("\"")); | 368 wcscpy (bufp, TEXT("\"")); |