src/main/win32/SDL_win32_main.c
changeset 1895 c121d94672cb
parent 1769 290b5baf2fca
child 2286 41a6675d8700
     1.1 --- a/src/main/win32/SDL_win32_main.c	Thu Jul 06 18:01:37 2006 +0000
     1.2 +++ b/src/main/win32/SDL_win32_main.c	Mon Jul 10 21:04:37 2006 +0000
     1.3 @@ -10,20 +10,6 @@
     1.4  #define WIN32_LEAN_AND_MEAN
     1.5  #include <windows.h>
     1.6  
     1.7 -#ifdef _WIN32_WCE
     1.8 -# define DIR_SEPERATOR TEXT("\\")
     1.9 -# undef _getcwd
    1.10 -# define _getcwd(str,len)	wcscpy(str,TEXT(""))
    1.11 -# define setbuf(f,b)
    1.12 -# define setvbuf(w,x,y,z)
    1.13 -# define fopen		_wfopen
    1.14 -# define freopen	_wfreopen
    1.15 -# define remove(x)	DeleteFile(x)
    1.16 -#else
    1.17 -# define DIR_SEPERATOR TEXT("/")
    1.18 -# include <direct.h>
    1.19 -#endif
    1.20 -
    1.21  /* Include the SDL main definition header */
    1.22  #include "SDL.h"
    1.23  #include "SDL_main.h"
    1.24 @@ -34,135 +20,80 @@
    1.25  # endif /* _WIN32_WCE_EMULATION */
    1.26  #endif /* main */
    1.27  
    1.28 -/* The standard output files */
    1.29 -#define STDOUT_FILE	TEXT("stdout.txt")
    1.30 -#define STDERR_FILE	TEXT("stderr.txt")
    1.31 -
    1.32 -#ifndef NO_STDIO_REDIRECT
    1.33 -# ifdef _WIN32_WCE
    1.34 -  static wchar_t stdoutPath[MAX_PATH];
    1.35 -  static wchar_t stderrPath[MAX_PATH];
    1.36 -# else
    1.37 -  static char stdoutPath[MAX_PATH];
    1.38 -  static char stderrPath[MAX_PATH];
    1.39 -# endif
    1.40 -#endif
    1.41 -
    1.42  #if defined(_WIN32_WCE) && _WIN32_WCE < 300
    1.43  /* seems to be undefined in Win CE although in online help */
    1.44  #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
    1.45  #endif /* _WIN32_WCE < 300 */
    1.46  
    1.47  /* Parse a command line buffer into arguments */
    1.48 -static int ParseCommandLine(char *cmdline, char **argv)
    1.49 +static int
    1.50 +ParseCommandLine(char *cmdline, char **argv)
    1.51  {
    1.52 -	char *bufp;
    1.53 -	int argc;
    1.54 +    char *bufp;
    1.55 +    int argc;
    1.56  
    1.57 -	argc = 0;
    1.58 -	for ( bufp = cmdline; *bufp; ) {
    1.59 -		/* Skip leading whitespace */
    1.60 -		while ( isspace(*bufp) ) {
    1.61 -			++bufp;
    1.62 -		}
    1.63 -		/* Skip over argument */
    1.64 -		if ( *bufp == '"' ) {
    1.65 -			++bufp;
    1.66 -			if ( *bufp ) {
    1.67 -				if ( argv ) {
    1.68 -					argv[argc] = bufp;
    1.69 -				}
    1.70 -				++argc;
    1.71 -			}
    1.72 -			/* Skip over word */
    1.73 -			while ( *bufp && (*bufp != '"') ) {
    1.74 -				++bufp;
    1.75 -			}
    1.76 -		} else {
    1.77 -			if ( *bufp ) {
    1.78 -				if ( argv ) {
    1.79 -					argv[argc] = bufp;
    1.80 -				}
    1.81 -				++argc;
    1.82 -			}
    1.83 -			/* Skip over word */
    1.84 -			while ( *bufp && ! isspace(*bufp) ) {
    1.85 -				++bufp;
    1.86 -			}
    1.87 -		}
    1.88 -		if ( *bufp ) {
    1.89 -			if ( argv ) {
    1.90 -				*bufp = '\0';
    1.91 -			}
    1.92 -			++bufp;
    1.93 -		}
    1.94 -	}
    1.95 -	if ( argv ) {
    1.96 -		argv[argc] = NULL;
    1.97 -	}
    1.98 -	return(argc);
    1.99 +    argc = 0;
   1.100 +    for (bufp = cmdline; *bufp;) {
   1.101 +        /* Skip leading whitespace */
   1.102 +        while (isspace(*bufp)) {
   1.103 +            ++bufp;
   1.104 +        }
   1.105 +        /* Skip over argument */
   1.106 +        if (*bufp == '"') {
   1.107 +            ++bufp;
   1.108 +            if (*bufp) {
   1.109 +                if (argv) {
   1.110 +                    argv[argc] = bufp;
   1.111 +                }
   1.112 +                ++argc;
   1.113 +            }
   1.114 +            /* Skip over word */
   1.115 +            while (*bufp && (*bufp != '"')) {
   1.116 +                ++bufp;
   1.117 +            }
   1.118 +        } else {
   1.119 +            if (*bufp) {
   1.120 +                if (argv) {
   1.121 +                    argv[argc] = bufp;
   1.122 +                }
   1.123 +                ++argc;
   1.124 +            }
   1.125 +            /* Skip over word */
   1.126 +            while (*bufp && !isspace(*bufp)) {
   1.127 +                ++bufp;
   1.128 +            }
   1.129 +        }
   1.130 +        if (*bufp) {
   1.131 +            if (argv) {
   1.132 +                *bufp = '\0';
   1.133 +            }
   1.134 +            ++bufp;
   1.135 +        }
   1.136 +    }
   1.137 +    if (argv) {
   1.138 +        argv[argc] = NULL;
   1.139 +    }
   1.140 +    return (argc);
   1.141  }
   1.142  
   1.143  /* Show an error message */
   1.144 -static void ShowError(const char *title, const char *message)
   1.145 +static void
   1.146 +ShowError(const char *title, const char *message)
   1.147  {
   1.148  /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
   1.149  #ifdef USE_MESSAGEBOX
   1.150 -	MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
   1.151 +    MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
   1.152  #else
   1.153 -	fprintf(stderr, "%s: %s\n", title, message);
   1.154 +    fprintf(stderr, "%s: %s\n", title, message);
   1.155  #endif
   1.156  }
   1.157  
   1.158  /* Pop up an out of memory message, returns to Windows */
   1.159 -static BOOL OutOfMemory(void)
   1.160 -{
   1.161 -	ShowError("Fatal Error", "Out of memory - aborting");
   1.162 -	return FALSE;
   1.163 -}
   1.164 -
   1.165 -/* SDL_Quit() shouldn't be used with atexit() directly because
   1.166 -   calling conventions may differ... */
   1.167 -static void cleanup(void)
   1.168 -{
   1.169 -	SDL_Quit();
   1.170 -}
   1.171 -
   1.172 -/* Remove the output files if there was no output written */
   1.173 -static void cleanup_output(void)
   1.174 +static BOOL
   1.175 +OutOfMemory(void)
   1.176  {
   1.177 -#ifndef NO_STDIO_REDIRECT
   1.178 -	FILE *file;
   1.179 -	int empty;
   1.180 -#endif
   1.181 -
   1.182 -	/* Flush the output in case anything is queued */
   1.183 -	fclose(stdout);
   1.184 -	fclose(stderr);
   1.185 -
   1.186 -#ifndef NO_STDIO_REDIRECT
   1.187 -	/* See if the files have any output in them */
   1.188 -	if ( stdoutPath[0] ) {
   1.189 -		file = fopen(stdoutPath, TEXT("rb"));
   1.190 -		if ( file ) {
   1.191 -			empty = (fgetc(file) == EOF) ? 1 : 0;
   1.192 -			fclose(file);
   1.193 -			if ( empty ) {
   1.194 -				remove(stdoutPath);
   1.195 -			}
   1.196 -		}
   1.197 -	}
   1.198 -	if ( stderrPath[0] ) {
   1.199 -		file = fopen(stderrPath, TEXT("rb"));
   1.200 -		if ( file ) {
   1.201 -			empty = (fgetc(file) == EOF) ? 1 : 0;
   1.202 -			fclose(file);
   1.203 -			if ( empty ) {
   1.204 -				remove(stderrPath);
   1.205 -			}
   1.206 -		}
   1.207 -	}
   1.208 -#endif
   1.209 +    ShowError("Fatal Error", "Out of memory - aborting");
   1.210 +    return FALSE;
   1.211  }
   1.212  
   1.213  #if defined(_MSC_VER) && !defined(_WIN32_WCE)
   1.214 @@ -171,187 +102,73 @@
   1.215  #endif
   1.216  
   1.217  /* This is where execution begins [console apps] */
   1.218 -int console_main(int argc, char *argv[])
   1.219 +int
   1.220 +console_main(int argc, char *argv[])
   1.221  {
   1.222 -	size_t n;
   1.223 -	char *bufp, *appname;
   1.224 -	int status;
   1.225 -
   1.226 -	/* Get the class name from argv[0] */
   1.227 -	appname = argv[0];
   1.228 -	if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
   1.229 -		appname = bufp+1;
   1.230 -	} else
   1.231 -	if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) {
   1.232 -		appname = bufp+1;
   1.233 -	}
   1.234 -
   1.235 -	if ( (bufp=SDL_strrchr(appname, '.')) == NULL )
   1.236 -		n = SDL_strlen(appname);
   1.237 -	else
   1.238 -		n = (bufp-appname);
   1.239 +    int status;
   1.240  
   1.241 -	bufp = SDL_stack_alloc(char, n+1);
   1.242 -	if ( bufp == NULL ) {
   1.243 -		return OutOfMemory();
   1.244 -	}
   1.245 -	SDL_strlcpy(bufp, appname, n+1);
   1.246 -	appname = bufp;
   1.247 -
   1.248 -	/* Load SDL dynamic link library */
   1.249 -	if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
   1.250 -		ShowError("WinMain() error", SDL_GetError());
   1.251 -		return(FALSE);
   1.252 -	}
   1.253 -	atexit(cleanup_output);
   1.254 -	atexit(cleanup);
   1.255 +    /* Run the application main() code */
   1.256 +    status = SDL_main(argc, argv);
   1.257  
   1.258 -	/* Sam:
   1.259 -	   We still need to pass in the application handle so that
   1.260 -	   DirectInput will initialize properly when SDL_RegisterApp()
   1.261 -	   is called later in the video initialization.
   1.262 -	 */
   1.263 -	SDL_SetModuleHandle(GetModuleHandle(NULL));
   1.264 +    /* Exit cleanly, calling atexit() functions */
   1.265 +    exit(status);
   1.266  
   1.267 -	/* Run the application main() code */
   1.268 -	status = SDL_main(argc, argv);
   1.269 -
   1.270 -	/* Exit cleanly, calling atexit() functions */
   1.271 -	exit(status);
   1.272 -
   1.273 -	/* Hush little compiler, don't you cry... */
   1.274 -	return 0;
   1.275 +    /* Hush little compiler, don't you cry... */
   1.276 +    return 0;
   1.277  }
   1.278  
   1.279  /* This is where execution begins [windowed apps] */
   1.280 -#ifdef _WIN32_WCE
   1.281 -int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
   1.282 -#else
   1.283 -int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
   1.284 -#endif
   1.285 +int WINAPI
   1.286 +WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw)
   1.287  {
   1.288 -	HINSTANCE handle;
   1.289 -	char **argv;
   1.290 -	int argc;
   1.291 -	char *cmdline;
   1.292 -	DWORD pathlen;
   1.293 +    char **argv;
   1.294 +    int argc;
   1.295 +    char *cmdline;
   1.296  #ifdef _WIN32_WCE
   1.297 -	wchar_t path[MAX_PATH];
   1.298 -#else
   1.299 -	char path[MAX_PATH];
   1.300 -#endif
   1.301 -#ifdef _WIN32_WCE
   1.302 -	wchar_t *bufp;
   1.303 -	int nLen;
   1.304 +    wchar_t *bufp;
   1.305 +    int nLen;
   1.306  #else
   1.307 -	char *bufp;
   1.308 -	size_t nLen;
   1.309 -#endif
   1.310 -#ifndef NO_STDIO_REDIRECT
   1.311 -	FILE *newfp;
   1.312 +    char *bufp;
   1.313 +    size_t nLen;
   1.314  #endif
   1.315  
   1.316 -	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   1.317 -	   keep them open.  This is a hack.. hopefully it will be fixed 
   1.318 -	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
   1.319 -	 */
   1.320 -	handle = LoadLibrary(TEXT("DDRAW.DLL"));
   1.321 -	if ( handle != NULL ) {
   1.322 -		FreeLibrary(handle);
   1.323 -	}
   1.324 -
   1.325 -#ifndef NO_STDIO_REDIRECT
   1.326 -	pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
   1.327 -	while ( pathlen > 0 && path[pathlen] != '\\' ) {
   1.328 -		--pathlen;
   1.329 -	}
   1.330 -	path[pathlen] = '\0';
   1.331 -
   1.332  #ifdef _WIN32_WCE
   1.333 -	wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
   1.334 -	wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   1.335 -#else
   1.336 -	SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
   1.337 -	SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   1.338 -#endif
   1.339 -    
   1.340 -	/* Redirect standard input and standard output */
   1.341 -	newfp = freopen(stdoutPath, TEXT("w"), stdout);
   1.342 -
   1.343 -#ifndef _WIN32_WCE
   1.344 -	if ( newfp == NULL ) {	/* This happens on NT */
   1.345 -#if !defined(stdout)
   1.346 -		stdout = fopen(stdoutPath, TEXT("w"));
   1.347 +    nLen = wcslen(szCmdLine) + 128 + 1;
   1.348 +    bufp = SDL_stack_alloc(wchar_t, nLen * 2);
   1.349 +    wcscpy(bufp, TEXT("\""));
   1.350 +    GetModuleFileName(NULL, bufp + 1, 128 - 3);
   1.351 +    wcscpy(bufp + wcslen(bufp), TEXT("\" "));
   1.352 +    wcsncpy(bufp + wcslen(bufp), szCmdLine, nLen - wcslen(bufp));
   1.353 +    nLen = wcslen(bufp) + 1;
   1.354 +    cmdline = SDL_stack_alloc(char, nLen);
   1.355 +    if (cmdline == NULL) {
   1.356 +        return OutOfMemory();
   1.357 +    }
   1.358 +    WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   1.359  #else
   1.360 -		newfp = fopen(stdoutPath, TEXT("w"));
   1.361 -		if ( newfp ) {
   1.362 -			*stdout = *newfp;
   1.363 -		}
   1.364 -#endif
   1.365 -	}
   1.366 -#endif /* _WIN32_WCE */
   1.367 -
   1.368 -#ifdef _WIN32_WCE
   1.369 -	wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) );
   1.370 -	wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   1.371 -#else
   1.372 -	SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
   1.373 -	SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
   1.374 +    /* Grab the command line */
   1.375 +    bufp = GetCommandLine();
   1.376 +    nLen = SDL_strlen(bufp) + 1;
   1.377 +    cmdline = SDL_stack_alloc(char, nLen);
   1.378 +    if (cmdline == NULL) {
   1.379 +        return OutOfMemory();
   1.380 +    }
   1.381 +    SDL_strlcpy(cmdline, bufp, nLen);
   1.382  #endif
   1.383  
   1.384 -	newfp = freopen(stderrPath, TEXT("w"), stderr);
   1.385 -#ifndef _WIN32_WCE
   1.386 -	if ( newfp == NULL ) {	/* This happens on NT */
   1.387 -#if !defined(stderr)
   1.388 -		stderr = fopen(stderrPath, TEXT("w"));
   1.389 -#else
   1.390 -		newfp = fopen(stderrPath, TEXT("w"));
   1.391 -		if ( newfp ) {
   1.392 -			*stderr = *newfp;
   1.393 -		}
   1.394 -#endif
   1.395 -	}
   1.396 -#endif /* _WIN32_WCE */
   1.397 -
   1.398 -	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
   1.399 -	setbuf(stderr, NULL);			/* No buffering */
   1.400 -#endif /* !NO_STDIO_REDIRECT */
   1.401 +    /* Parse it into argv and argc */
   1.402 +    argc = ParseCommandLine(cmdline, NULL);
   1.403 +    argv = SDL_stack_alloc(char *, argc + 1);
   1.404 +    if (argv == NULL) {
   1.405 +        return OutOfMemory();
   1.406 +    }
   1.407 +    ParseCommandLine(cmdline, argv);
   1.408  
   1.409 -#ifdef _WIN32_WCE
   1.410 -	nLen = wcslen(szCmdLine)+128+1;
   1.411 -	bufp = SDL_stack_alloc(wchar_t, nLen*2);
   1.412 -	wcscpy (bufp, TEXT("\""));
   1.413 -	GetModuleFileName(NULL, bufp+1, 128-3);
   1.414 -	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
   1.415 -	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
   1.416 -	nLen = wcslen(bufp)+1;
   1.417 -	cmdline = SDL_stack_alloc(char, nLen);
   1.418 -	if ( cmdline == NULL ) {
   1.419 -		return OutOfMemory();
   1.420 -	}
   1.421 -	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   1.422 -#else
   1.423 -	/* Grab the command line */
   1.424 -	bufp = GetCommandLine();
   1.425 -	nLen = SDL_strlen(bufp)+1;
   1.426 -	cmdline = SDL_stack_alloc(char, nLen);
   1.427 -	if ( cmdline == NULL ) {
   1.428 -		return OutOfMemory();
   1.429 -	}
   1.430 -	SDL_strlcpy(cmdline, bufp, nLen);
   1.431 -#endif
   1.432 +    /* Run the main program */
   1.433 +    console_main(argc, argv);
   1.434  
   1.435 -	/* Parse it into argv and argc */
   1.436 -	argc = ParseCommandLine(cmdline, NULL);
   1.437 -	argv = SDL_stack_alloc(char*, argc+1);
   1.438 -	if ( argv == NULL ) {
   1.439 -		return OutOfMemory();
   1.440 -	}
   1.441 -	ParseCommandLine(cmdline, argv);
   1.442 +    /* Hush little compiler, don't you cry... */
   1.443 +    return 0;
   1.444 +}
   1.445  
   1.446 -	/* Run the main program (after a little SDL initialization) */
   1.447 -	console_main(argc, argv);
   1.448 -
   1.449 -	/* Hush little compiler, don't you cry... */
   1.450 -	return 0;
   1.451 -}
   1.452 +/* vi: set ts=4 sw=4 expandtab: */