src/main/win32/SDL_win32_main.c
author Sam Lantinga
Sun, 19 Feb 2006 23:46:34 +0000
changeset 1379 c0a74f199ecf
parent 1361 19418e4422cb
child 1423 4ed717f9e509
permissions -rw-r--r--
Use only safe string functions
     1 /*
     2     SDL_main.c, placed in the public domain by Sam Lantinga  4/13/98
     3 
     4     The WinMain function -- calls your program's main() function
     5 */
     6 
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 
    10 #include "SDL_windows.h"
    11 
    12 #ifdef _WIN32_WCE
    13 # define DIR_SEPERATOR TEXT("\\")
    14 # undef _getcwd
    15 # define _getcwd(str,len)	wcscpy(str,TEXT(""))
    16 # define setbuf(f,b)
    17 # define setvbuf(w,x,y,z)
    18 # define fopen		_wfopen
    19 # define freopen	_wfreopen
    20 # define remove(x)	DeleteFile(x)
    21 #else
    22 # define DIR_SEPERATOR TEXT("/")
    23 # include <direct.h>
    24 #endif
    25 
    26 /* Include the SDL main definition header */
    27 #include "SDL.h"
    28 #include "SDL_main.h"
    29 
    30 #ifdef main
    31 # ifndef _WIN32_WCE_EMULATION
    32 #  undef main
    33 # endif /* _WIN32_WCE_EMULATION */
    34 #endif /* main */
    35 
    36 /* The standard output files */
    37 #define STDOUT_FILE	TEXT("stdout.txt")
    38 #define STDERR_FILE	TEXT("stderr.txt")
    39 
    40 #ifndef NO_STDIO_REDIRECT
    41 # ifdef _WIN32_WCE
    42   static wchar_t stdoutPath[MAX_PATH];
    43   static wchar_t stderrPath[MAX_PATH];
    44 # else
    45   static char stdoutPath[MAX_PATH];
    46   static char stderrPath[MAX_PATH];
    47 # endif
    48 #endif
    49 
    50 #if defined(_WIN32_WCE) && _WIN32_WCE < 300
    51 /* seems to be undefined in Win CE although in online help */
    52 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
    53 
    54 /* seems to be undefined in Win CE although in online help */
    55 char *SDL_strrchr(char *str, int c)
    56 {
    57 	char *p;
    58 
    59 	/* Skip to the end of the string */
    60 	p=str;
    61 	while (*p)
    62 		p++;
    63 
    64 	/* Look for the given character */
    65 	while ( (p >= str) && (*p != (CHAR)c) )
    66 		p--;
    67 
    68 	/* Return NULL if character not found */
    69 	if ( p < str ) {
    70 		p = NULL;
    71 	}
    72 	return p;
    73 }
    74 #endif /* _WIN32_WCE < 300 */
    75 
    76 /* Parse a command line buffer into arguments */
    77 static int ParseCommandLine(char *cmdline, char **argv)
    78 {
    79 	char *bufp;
    80 	int argc;
    81 
    82 	argc = 0;
    83 	for ( bufp = cmdline; *bufp; ) {
    84 		/* Skip leading whitespace */
    85 		while ( isspace(*bufp) ) {
    86 			++bufp;
    87 		}
    88 		/* Skip over argument */
    89 		if ( *bufp == '"' ) {
    90 			++bufp;
    91 			if ( *bufp ) {
    92 				if ( argv ) {
    93 					argv[argc] = bufp;
    94 				}
    95 				++argc;
    96 			}
    97 			/* Skip over word */
    98 			while ( *bufp && (*bufp != '"') ) {
    99 				++bufp;
   100 			}
   101 		} else {
   102 			if ( *bufp ) {
   103 				if ( argv ) {
   104 					argv[argc] = bufp;
   105 				}
   106 				++argc;
   107 			}
   108 			/* Skip over word */
   109 			while ( *bufp && ! isspace(*bufp) ) {
   110 				++bufp;
   111 			}
   112 		}
   113 		if ( *bufp ) {
   114 			if ( argv ) {
   115 				*bufp = '\0';
   116 			}
   117 			++bufp;
   118 		}
   119 	}
   120 	if ( argv ) {
   121 		argv[argc] = NULL;
   122 	}
   123 	return(argc);
   124 }
   125 
   126 /* Show an error message */
   127 static void ShowError(const char *title, const char *message)
   128 {
   129 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
   130 #ifdef USE_MESSAGEBOX
   131 	MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
   132 #else
   133 	fprintf(stderr, "%s: %s\n", title, message);
   134 #endif
   135 }
   136 
   137 /* Pop up an out of memory message, returns to Windows */
   138 static BOOL OutOfMemory(void)
   139 {
   140 	ShowError("Fatal Error", "Out of memory - aborting");
   141 	return FALSE;
   142 }
   143 
   144 /* Remove the output files if there was no output written */
   145 static void __cdecl cleanup_output(void)
   146 {
   147 #ifndef NO_STDIO_REDIRECT
   148 	FILE *file;
   149 	int empty;
   150 #endif
   151 
   152 	/* Flush the output in case anything is queued */
   153 	fclose(stdout);
   154 	fclose(stderr);
   155 
   156 #ifndef NO_STDIO_REDIRECT
   157 	/* See if the files have any output in them */
   158 	if ( stdoutPath[0] ) {
   159 		file = fopen(stdoutPath, TEXT("rb"));
   160 		if ( file ) {
   161 			empty = (fgetc(file) == EOF) ? 1 : 0;
   162 			fclose(file);
   163 			if ( empty ) {
   164 				remove(stdoutPath);
   165 			}
   166 		}
   167 	}
   168 	if ( stderrPath[0] ) {
   169 		file = fopen(stderrPath, TEXT("rb"));
   170 		if ( file ) {
   171 			empty = (fgetc(file) == EOF) ? 1 : 0;
   172 			fclose(file);
   173 			if ( empty ) {
   174 				remove(stderrPath);
   175 			}
   176 		}
   177 	}
   178 #endif
   179 }
   180 
   181 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
   182 /* The VC++ compiler needs main defined */
   183 #define console_main main
   184 #endif
   185 
   186 /* This is where execution begins [console apps] */
   187 int console_main(int argc, char *argv[])
   188 {
   189 	int n;
   190 	char *bufp, *appname;
   191 
   192 	/* Get the class name from argv[0] */
   193 	appname = argv[0];
   194 	if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
   195 		appname = bufp+1;
   196 	} else
   197 	if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) {
   198 		appname = bufp+1;
   199 	}
   200 
   201 	if ( (bufp=SDL_strrchr(appname, '.')) == NULL )
   202 		n = SDL_strlen(appname);
   203 	else
   204 		n = (bufp-appname);
   205 
   206 	bufp = (char *)alloca(n+1);
   207 	if ( bufp == NULL ) {
   208 		return OutOfMemory();
   209 	}
   210 	SDL_strlcpy(bufp, appname, n);
   211 	appname = bufp;
   212 
   213 	/* Load SDL dynamic link library */
   214 	if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
   215 		ShowError("WinMain() error", SDL_GetError());
   216 		return(FALSE);
   217 	}
   218 	atexit(cleanup_output);
   219 	atexit(SDL_Quit);
   220 
   221 	/* Sam:
   222 	   We still need to pass in the application handle so that
   223 	   DirectInput will initialize properly when SDL_RegisterApp()
   224 	   is called later in the video initialization.
   225 	 */
   226 	SDL_SetModuleHandle(GetModuleHandle(NULL));
   227 
   228 	/* Run the application main() code */
   229 	SDL_main(argc, argv);
   230 
   231 	/* Exit cleanly, calling atexit() functions */
   232 	exit(0);
   233 
   234 	/* Hush little compiler, don't you cry... */
   235 	return(0);
   236 }
   237 
   238 /* This is where execution begins [windowed apps] */
   239 #ifdef _WIN32_WCE
   240 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
   241 #else
   242 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
   243 #endif
   244 {
   245 	HINSTANCE handle;
   246 	char **argv;
   247 	int argc;
   248 	char *cmdline;
   249 	DWORD pathlen;
   250 #ifdef _WIN32_WCE
   251 	wchar_t path[MAX_PATH];
   252 #else
   253 	char path[MAX_PATH];
   254 #endif
   255 #ifdef _WIN32_WCE
   256 	wchar_t *bufp;
   257 	int nLen;
   258 #else
   259 	char *bufp;
   260 	size_t nLen;
   261 #endif
   262 #ifndef NO_STDIO_REDIRECT
   263 	FILE *newfp;
   264 #endif
   265 	int retval;
   266 
   267 	/* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
   268 	   keep them open.  This is a hack.. hopefully it will be fixed 
   269 	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
   270 	 */
   271 	handle = LoadLibrary(TEXT("DDRAW.DLL"));
   272 	if ( handle != NULL ) {
   273 		FreeLibrary(handle);
   274 	}
   275 
   276 #ifndef NO_STDIO_REDIRECT
   277 	pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
   278 	while ( pathlen > 0 && path[pathlen] != '\\' ) {
   279 		--pathlen;
   280 	}
   281 	path[pathlen] = '\0';
   282 
   283 	SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
   284 	SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
   285     
   286 	/* Redirect standard input and standard output */
   287 	newfp = freopen(stdoutPath, TEXT("w"), stdout);
   288 
   289 #ifndef _WIN32_WCE
   290 	if ( newfp == NULL ) {	/* This happens on NT */
   291 #if !defined(stdout)
   292 		stdout = fopen(stdoutPath, TEXT("w"));
   293 #else
   294 		newfp = fopen(stdoutPath, TEXT("w"));
   295 		if ( newfp ) {
   296 			*stdout = *newfp;
   297 		}
   298 #endif
   299 	}
   300 #endif /* _WIN32_WCE */
   301 
   302 	SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
   303 	SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
   304 
   305 	newfp = freopen(stderrPath, TEXT("w"), stderr);
   306 #ifndef _WIN32_WCE
   307 	if ( newfp == NULL ) {	/* This happens on NT */
   308 #if !defined(stderr)
   309 		stderr = fopen(stderrPath, TEXT("w"));
   310 #else
   311 		newfp = fopen(stderrPath, TEXT("w"));
   312 		if ( newfp ) {
   313 			*stderr = *newfp;
   314 		}
   315 #endif
   316 	}
   317 #endif /* _WIN32_WCE */
   318 
   319 	setvbuf(stdout, NULL, _IOLBF, BUFSIZ);	/* Line buffered */
   320 	setbuf(stderr, NULL);			/* No buffering */
   321 #endif /* !NO_STDIO_REDIRECT */
   322 
   323 #ifdef _WIN32_WCE
   324 	nLen = wcslen(szCmdLine)+128+1;
   325 	bufp = (wchar_t *)alloca(nLen*2);
   326 	wcscpy (bufp, TEXT("\""));
   327 	GetModuleFileName(NULL, bufp+1, 128-3);
   328 	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
   329 	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
   330 	nLen = wcslen(bufp)+1;
   331 	cmdline = SDL_stack_alloc(wchar_t, nLen);
   332 	if ( cmdline == NULL ) {
   333 		return OutOfMemory();
   334 	}
   335 	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   336 #else
   337 	/* Grab the command line (use alloca() on Windows) */
   338 	bufp = GetCommandLine();
   339 	nLen = SDL_strlen(bufp)+1;
   340 	cmdline = SDL_stack_alloc(char, nLen);
   341 	if ( cmdline == NULL ) {
   342 		return OutOfMemory();
   343 	}
   344 	SDL_strlcpy(cmdline, bufp, nLen);
   345 #endif
   346 
   347 	/* Parse it into argv and argc */
   348 	argc = ParseCommandLine(cmdline, NULL);
   349 	argv = SDL_stack_alloc(char*, argc+1);
   350 	if ( argv == NULL ) {
   351 		return OutOfMemory();
   352 	}
   353 	ParseCommandLine(cmdline, argv);
   354 
   355 	/* Run the main program (after a little SDL initialization) */
   356 	retval = console_main(argc, argv);
   357 
   358 	SDL_stack_free(cmdline);
   359 	SDL_stack_free(argv);
   360 	return retval;
   361 }