Commit 0f710e40 authored by Sam Lantinga's avatar Sam Lantinga

Fixed bug #810

 Lauri Kenttä      2009-09-26 06:42:23 PDT

Support for disabling stdio redirect with environment variables.

--HG--
branch : SDL-1.2
extra : convert_revision : svn%3Ac70aab31-4412-0410-b14c-859654838e24/branches/SDL-1.2%403917
parent 8299975e
...@@ -38,14 +38,15 @@ ...@@ -38,14 +38,15 @@
#define STDOUT_FILE TEXT("stdout.txt") #define STDOUT_FILE TEXT("stdout.txt")
#define STDERR_FILE TEXT("stderr.txt") #define STDERR_FILE TEXT("stderr.txt")
#ifndef NO_STDIO_REDIRECT /* Set a variable to tell if the stdio redirect has been enabled. */
# ifdef _WIN32_WCE static int stdioRedirectEnabled = 0;
#ifdef _WIN32_WCE
static wchar_t stdoutPath[MAX_PATH]; static wchar_t stdoutPath[MAX_PATH];
static wchar_t stderrPath[MAX_PATH]; static wchar_t stderrPath[MAX_PATH];
# else #else
static char stdoutPath[MAX_PATH]; static char stdoutPath[MAX_PATH];
static char stderrPath[MAX_PATH]; static char stderrPath[MAX_PATH];
# endif
#endif #endif
#if defined(_WIN32_WCE) && _WIN32_WCE < 300 #if defined(_WIN32_WCE) && _WIN32_WCE < 300
...@@ -158,18 +159,19 @@ static void cleanup(void) ...@@ -158,18 +159,19 @@ static void cleanup(void)
} }
/* Remove the output files if there was no output written */ /* Remove the output files if there was no output written */
static void cleanup_output(void) static void cleanup_output(void) {
{
#ifndef NO_STDIO_REDIRECT
FILE *file; FILE *file;
int empty; int empty;
#endif
/* Flush the output in case anything is queued */ /* Flush the output in case anything is queued */
fclose(stdout); fclose(stdout);
fclose(stderr); fclose(stderr);
#ifndef NO_STDIO_REDIRECT /* Without redirection we're done */
if (!stdioRedirectEnabled) {
return;
}
/* See if the files have any output in them */ /* See if the files have any output in them */
if ( stdoutPath[0] ) { if ( stdoutPath[0] ) {
file = fopen(stdoutPath, TEXT("rb")); file = fopen(stdoutPath, TEXT("rb"));
...@@ -191,7 +193,74 @@ static void cleanup_output(void) ...@@ -191,7 +193,74 @@ static void cleanup_output(void)
} }
} }
} }
}
/* Redirect the output (stdout and stderr) to a file */
static void redirect_output(void)
{
DWORD pathlen;
#ifdef _WIN32_WCE
wchar_t path[MAX_PATH];
#else
char path[MAX_PATH];
#endif
FILE *newfp;
pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
while ( pathlen > 0 && path[pathlen] != '\\' ) {
--pathlen;
}
path[pathlen] = '\0';
#ifdef _WIN32_WCE
wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
#else
SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
#endif
/* Redirect standard input and standard output */
newfp = freopen(stdoutPath, TEXT("w"), stdout);
#ifndef _WIN32_WCE
if ( newfp == NULL ) { /* This happens on NT */
#if !defined(stdout)
stdout = fopen(stdoutPath, TEXT("w"));
#else
newfp = fopen(stdoutPath, TEXT("w"));
if ( newfp ) {
*stdout = *newfp;
}
#endif
}
#endif /* _WIN32_WCE */
#ifdef _WIN32_WCE
wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) );
wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
#else
SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
#endif
newfp = freopen(stderrPath, TEXT("w"), stderr);
#ifndef _WIN32_WCE
if ( newfp == NULL ) { /* This happens on NT */
#if !defined(stderr)
stderr = fopen(stderrPath, TEXT("w"));
#else
newfp = fopen(stderrPath, TEXT("w"));
if ( newfp ) {
*stderr = *newfp;
}
#endif #endif
}
#endif /* _WIN32_WCE */
setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */
setbuf(stderr, NULL); /* No buffering */
stdioRedirectEnabled = 1;
} }
#if defined(_MSC_VER) && !defined(_WIN32_WCE) #if defined(_MSC_VER) && !defined(_WIN32_WCE)
...@@ -263,6 +332,7 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) ...@@ -263,6 +332,7 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
char **argv; char **argv;
int argc; int argc;
char *cmdline; char *cmdline;
char *env_str;
#ifdef _WIN32_WCE #ifdef _WIN32_WCE
wchar_t *bufp; wchar_t *bufp;
int nLen; int nLen;
...@@ -270,15 +340,6 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) ...@@ -270,15 +340,6 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
char *bufp; char *bufp;
size_t nLen; size_t nLen;
#endif #endif
#ifndef NO_STDIO_REDIRECT
DWORD pathlen;
#ifdef _WIN32_WCE
wchar_t path[MAX_PATH];
#else
char path[MAX_PATH];
#endif
FILE *newfp;
#endif
/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
keep them open. This is a hack.. hopefully it will be fixed keep them open. This is a hack.. hopefully it will be fixed
...@@ -289,62 +350,17 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw) ...@@ -289,62 +350,17 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
FreeLibrary(handle); FreeLibrary(handle);
} }
#ifndef NO_STDIO_REDIRECT /* Check for stdio redirect settings and do the redirection */
pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path)); if ((env_str = SDL_getenv("SDL_STDIO_REDIRECT"))) {
while ( pathlen > 0 && path[pathlen] != '\\' ) { if (SDL_atoi(env_str)) {
--pathlen; redirect_output();
}
path[pathlen] = '\0';
#ifdef _WIN32_WCE
wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
#else
SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
#endif
/* Redirect standard input and standard output */
newfp = freopen(stdoutPath, TEXT("w"), stdout);
#ifndef _WIN32_WCE
if ( newfp == NULL ) { /* This happens on NT */
#if !defined(stdout)
stdout = fopen(stdoutPath, TEXT("w"));
#else
newfp = fopen(stdoutPath, TEXT("w"));
if ( newfp ) {
*stdout = *newfp;
} }
#endif
} }
#endif /* _WIN32_WCE */ #ifndef NO_STDIO_REDIRECT
else {
#ifdef _WIN32_WCE redirect_output();
wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) );
wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
#else
SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
#endif
newfp = freopen(stderrPath, TEXT("w"), stderr);
#ifndef _WIN32_WCE
if ( newfp == NULL ) { /* This happens on NT */
#if !defined(stderr)
stderr = fopen(stderrPath, TEXT("w"));
#else
newfp = fopen(stderrPath, TEXT("w"));
if ( newfp ) {
*stderr = *newfp;
}
#endif
} }
#endif /* _WIN32_WCE */ #endif
setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */
setbuf(stderr, NULL); /* No buffering */
#endif /* !NO_STDIO_REDIRECT */
#ifdef _WIN32_WCE #ifdef _WIN32_WCE
nLen = wcslen(szCmdLine)+128+1; nLen = wcslen(szCmdLine)+128+1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment