src/main/win32/SDL_win32_main.c
author Sam Lantinga
Sat, 29 Dec 2007 18:58:09 +0000
branchSDL-1.2
changeset 4118 3c068a8c016f
parent 4084 70e77cbba87c
child 4246 8b8314cc34a6
permissions -rw-r--r--
Tony White fixed bug #503

Support escaped quotes on Win32 command lines.
     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 #define WIN32_LEAN_AND_MEAN
    11 #include <windows.h>
    12 
    13 #ifdef _WIN32_WCE
    14 # define DIR_SEPERATOR TEXT("\\")
    15 # undef _getcwd
    16 # define _getcwd(str,len)	wcscpy(str,TEXT(""))
    17 # define setbuf(f,b)
    18 # define setvbuf(w,x,y,z)
    19 # define fopen		_wfopen
    20 # define freopen	_wfreopen
    21 # define remove(x)	DeleteFile(x)
    22 #else
    23 # define DIR_SEPERATOR TEXT("/")
    24 # include <direct.h>
    25 #endif
    26 
    27 /* Include the SDL main definition header */
    28 #include "SDL.h"
    29 #include "SDL_main.h"
    30 
    31 #ifdef main
    32 # ifndef _WIN32_WCE_EMULATION
    33 #  undef main
    34 # endif /* _WIN32_WCE_EMULATION */
    35 #endif /* main */
    36 
    37 /* The standard output files */
    38 #define STDOUT_FILE	TEXT("stdout.txt")
    39 #define STDERR_FILE	TEXT("stderr.txt")
    40 
    41 #ifndef NO_STDIO_REDIRECT
    42 # ifdef _WIN32_WCE
    43   static wchar_t stdoutPath[MAX_PATH];
    44   static wchar_t stderrPath[MAX_PATH];
    45 # else
    46   static char stdoutPath[MAX_PATH];
    47   static char stderrPath[MAX_PATH];
    48 # endif
    49 #endif
    50 
    51 #if defined(_WIN32_WCE) && _WIN32_WCE < 300
    52 /* seems to be undefined in Win CE although in online help */
    53 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
    54 #endif /* _WIN32_WCE < 300 */
    55 
    56 static void UnEscapeQuotes( char *arg )
    57 {
    58 	char *last = NULL;
    59 
    60 	while( *arg ) {
    61 		if( *arg == '"' && *last == '\\' ) {
    62 			char *c_curr = arg;
    63 			char *c_last = last;
    64 
    65 			while( *c_curr ) {
    66 				*c_last = *c_curr;
    67 				c_last = c_curr;
    68 				c_curr++;
    69 			}
    70 			*c_last = '\0';
    71 		}
    72 		last = arg;
    73 		arg++;
    74 	}
    75 }
    76 
    77 /* Parse a command line buffer into arguments */
    78 static int ParseCommandLine(char *cmdline, char **argv)
    79 {
    80 	char *bufp;
    81 	char *lastp = NULL;
    82 	int argc, last_argc;
    83 
    84 	argc = last_argc = 0;
    85 	for ( bufp = cmdline; *bufp; ) {
    86 		/* Skip leading whitespace */
    87 		while ( isspace(*bufp) ) {
    88 			++bufp;
    89 		}
    90 		/* Skip over argument */
    91 		if ( *bufp == '"' ) {
    92 			++bufp;
    93 			if ( *bufp ) {
    94 				if ( argv ) {
    95 					argv[argc] = bufp;
    96 				}
    97 				++argc;
    98 			}
    99 			/* Skip over word */
   100 			while ( *bufp && ( *bufp != '"' || *lastp == '\\' ) ) {
   101 				lastp = bufp;
   102 				++bufp;
   103 			}
   104 		} else {
   105 			if ( *bufp ) {
   106 				if ( argv ) {
   107 					argv[argc] = bufp;
   108 				}
   109 				++argc;
   110 			}
   111 			/* Skip over word */
   112 			while ( *bufp && ! isspace(*bufp) ) {
   113 				++bufp;
   114 			}
   115 		}
   116 		if ( *bufp ) {
   117 			if ( argv ) {
   118 				*bufp = '\0';
   119 			}
   120 			++bufp;
   121 		}
   122 
   123 		/* Strip out \ from \" sequences */
   124 		if( argv && last_argc != argc ) {
   125 			UnEscapeQuotes( argv[last_argc] );	
   126 		}
   127 		last_argc = argc;	
   128 	}
   129 	if ( argv ) {
   130 		argv[argc] = NULL;
   131 	}
   132 	return(argc);
   133 }
   134 
   135 /* Show an error message */
   136 static void ShowError(const char *title, const char *message)
   137 {
   138 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
   139 #ifdef USE_MESSAGEBOX
   140 	MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
   141 #else
   142 	fprintf(stderr, "%s: %s\n", title, message);
   143 #endif
   144 }
   145 
   146 /* Pop up an out of memory message, returns to Windows */
   147 static BOOL OutOfMemory(void)
   148 {
   149 	ShowError("Fatal Error", "Out of memory - aborting");
   150 	return FALSE;
   151 }
   152 
   153 /* SDL_Quit() shouldn't be used with atexit() directly because
   154    calling conventions may differ... */
   155 static void cleanup(void)
   156 {
   157 	SDL_Quit();
   158 }
   159 
   160 /* Remove the output files if there was no output written */
   161 static void cleanup_output(void)
   162 {
   163 #ifndef NO_STDIO_REDIRECT
   164 	FILE *file;
   165 	int empty;
   166 #endif
   167 
   168 	/* Flush the output in case anything is queued */
   169 	fclose(stdout);
   170 	fclose(stderr);
   171 
   172 #ifndef NO_STDIO_REDIRECT
   173 	/* See if the files have any output in them */
   174 	if ( stdoutPath[0] ) {
   175 		file = fopen(stdoutPath, TEXT("rb"));
   176 		if ( file ) {
   177 			empty = (fgetc(file) == EOF) ? 1 : 0;
   178 			fclose(file);
   179 			if ( empty ) {
   180 				remove(stdoutPath);
   181 			}
   182 		}
   183 	}
   184 	if ( stderrPath[0] ) {
   185 		file = fopen(stderrPath, TEXT("rb"));
   186 		if ( file ) {
   187 			empty = (fgetc(file) == EOF) ? 1 : 0;
   188 			fclose(file);
   189 			if ( empty ) {
   190 				remove(stderrPath);
   191 			}
   192 		}
   193 	}
   194 #endif
   195 }
   196 
   197 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
   198 /* The VC++ compiler needs main defined */
   199 #define console_main main
   200 #endif
   201 
   202 /* This is where execution begins [console apps] */
   203 int console_main(int argc, char *argv[])
   204 {
   205 	size_t n;
   206 	char *bufp, *appname;
   207 	int status;
   208 
   209 	/* Get the class name from argv[0] */
   210 	appname = argv[0];
   211 	if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
   212 		appname = bufp+1;
   213 	} else
   214 	if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) {
   215 		appname = bufp+1;
   216 	}
   217 
   218 	if ( (bufp=SDL_strrchr(appname, '.')) == NULL )
   219 		n = SDL_strlen(appname);
   220 	else
   221 		n = (bufp-appname);
   222 
   223 	bufp = SDL_stack_alloc(char, n+1);
   224 	if ( bufp == NULL ) {
   225 		return OutOfMemory();
   226 	}
   227 	SDL_strlcpy(bufp, appname, n+1);
   228 	appname = bufp;
   229 
   230 	/* Load SDL dynamic link library */
   231 	if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
   232 		ShowError("WinMain() error", SDL_GetError());
   233 		return(FALSE);
   234 	}
   235 	atexit(cleanup_output);
   236 	atexit(cleanup);
   237 
   238 	/* Sam:
   239 	   We still need to pass in the application handle so that
   240 	   DirectInput will initialize properly when SDL_RegisterApp()
   241 	   is called later in the video initialization.
   242 	 */
   243 	SDL_SetModuleHandle(GetModuleHandle(NULL));
   244 
   245 	/* Run the application main() code */
   246 	status = SDL_main(argc, argv);
   247 
   248 	/* Exit cleanly, calling atexit() functions */
   249 	exit(status);
   250 
   251 	/* Hush little compiler, don't you cry... */
   252 	return 0;
   253 }
   254 
   255 /* This is where execution begins [windowed apps] */
   256 #ifdef _WIN32_WCE
   257 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
   258 #else
   259 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
   260 #endif
   261 {
   262 	HINSTANCE handle;
   263 	char **argv;
   264 	int argc;
   265 	char *cmdline;
   266 #ifdef _WIN32_WCE
   267 	wchar_t *bufp;
   268 	int nLen;
   269 #else
   270 	char *bufp;
   271 	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
   282 
   283 	/* 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 
   285 	   someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
   286 	 */
   287 	handle = LoadLibrary(TEXT("DDRAW.DLL"));
   288 	if ( handle != NULL ) {
   289 		FreeLibrary(handle);
   290 	}
   291 
   292 #ifndef NO_STDIO_REDIRECT
   293 	pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
   294 	while ( pathlen > 0 && path[pathlen] != '\\' ) {
   295 		--pathlen;
   296 	}
   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 
   349 #ifdef _WIN32_WCE
   350 	nLen = wcslen(szCmdLine)+128+1;
   351 	bufp = SDL_stack_alloc(wchar_t, nLen*2);
   352 	wcscpy (bufp, TEXT("\""));
   353 	GetModuleFileName(NULL, bufp+1, 128-3);
   354 	wcscpy (bufp+wcslen(bufp), TEXT("\" "));
   355 	wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
   356 	nLen = wcslen(bufp)+1;
   357 	cmdline = SDL_stack_alloc(char, nLen);
   358 	if ( cmdline == NULL ) {
   359 		return OutOfMemory();
   360 	}
   361 	WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
   362 #else
   363 	/* Grab the command line */
   364 	bufp = GetCommandLine();
   365 	nLen = SDL_strlen(bufp)+1;
   366 	cmdline = SDL_stack_alloc(char, nLen);
   367 	if ( cmdline == NULL ) {
   368 		return OutOfMemory();
   369 	}
   370 	SDL_strlcpy(cmdline, bufp, nLen);
   371 #endif
   372 
   373 	/* Parse it into argv and argc */
   374 	argc = ParseCommandLine(cmdline, NULL);
   375 	argv = SDL_stack_alloc(char*, argc+1);
   376 	if ( argv == NULL ) {
   377 		return OutOfMemory();
   378 	}
   379 	ParseCommandLine(cmdline, argv);
   380 
   381 	/* Run the main program (after a little SDL initialization) */
   382 	console_main(argc, argv);
   383 
   384 	/* Hush little compiler, don't you cry... */
   385 	return 0;
   386 }