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
This commit is contained in:
parent
8299975e5f
commit
0f710e40fa
1 changed files with 87 additions and 71 deletions
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 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
|
#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 /* _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)
|
||||||
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)
|
||||||
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,63 +350,18 @@ int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
|
||||||
FreeLibrary(handle);
|
FreeLibrary(handle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Check for stdio redirect settings and do the redirection */
|
||||||
|
if ((env_str = SDL_getenv("SDL_STDIO_REDIRECT"))) {
|
||||||
|
if (SDL_atoi(env_str)) {
|
||||||
|
redirect_output();
|
||||||
|
}
|
||||||
|
}
|
||||||
#ifndef NO_STDIO_REDIRECT
|
#ifndef NO_STDIO_REDIRECT
|
||||||
pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
|
else {
|
||||||
while ( pathlen > 0 && path[pathlen] != '\\' ) {
|
redirect_output();
|
||||||
--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
|
#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 /* _WIN32_WCE */
|
|
||||||
|
|
||||||
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;
|
||||||
bufp = SDL_stack_alloc(wchar_t, nLen*2);
|
bufp = SDL_stack_alloc(wchar_t, nLen*2);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue