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