1 /* Emacs style mode select -*- C++ -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 * Copyright 2005, 2006 by
8 * Florian Schulze, Colin Phipps, Neil Stevens, Andrey Budko
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 * 02111-1307, USA.
24 *
25 * DESCRIPTION:
26 * SDL_main.c, placed in the public domain by Sam Lantinga 4/13/98
27 * The WinMain function -- calls your program's main() function
28 */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32
33 #define WIN32_LEAN_AND_MEAN
34 #include <windows.h>
35
36 #ifdef _WIN32_WCE
37 # define DIR_SEPERATOR TEXT("\\")
38 # undef _getcwd
39 # define _getcwd(str,len) wcscpy(str,TEXT(""))
40 # define setbuf(f,b)
41 # define setvbuf(w,x,y,z)
42 # define fopen _wfopen
43 # define freopen _wfreopen
44 # define remove(x) DeleteFile(x)
45 #else
46 # define DIR_SEPERATOR TEXT("/")
47 # include <direct.h>
48 #endif
49
50 // proff
51 #define HAVE_MALLOC 1
52 #define HAVE_STRLEN 1
53 #define HAVE_STRRCHR 1
54 #define HAVE_STRLCPY 1
55 #define HAVE_STRLCAT 1
56
strlcpy(char * dst,const char * src,size_t maxlen)57 size_t strlcpy(char *dst, const char *src, size_t maxlen)
58 {
59 size_t srclen = strlen(src);
60 if ( maxlen > 0 ) {
61 size_t len = min(srclen, maxlen-1);
62 memcpy(dst, src, len);
63 dst[len] = '\0';
64 }
65 return srclen;
66 }
67
strlcat(char * dst,const char * src,size_t maxlen)68 size_t strlcat(char *dst, const char *src, size_t maxlen)
69 {
70 size_t dstlen = strlen(dst);
71 size_t srclen = strlen(src);
72 if ( dstlen < maxlen ) {
73 strlcpy(dst+dstlen, src, maxlen-dstlen);
74 }
75 return dstlen+srclen;
76 }
77
78 // proff - end of additions
79 /* Include the SDL main definition header */
80 #include "SDL.h"
81 #include "SDL_main.h"
82
83 #ifdef main
84 # ifndef _WIN32_WCE_EMULATION
85 # undef main
86 # endif /* _WIN32_WCE_EMULATION */
87 #endif /* main */
88
89 /* The standard output files */
90 #define STDOUT_FILE TEXT("stdout.txt")
91 #define STDERR_FILE TEXT("stderr.txt")
92
93 #ifndef NO_STDIO_REDIRECT
94 # ifdef _WIN32_WCE
95 static wchar_t stdoutPath[MAX_PATH];
96 static wchar_t stderrPath[MAX_PATH];
97 # else
98 static char stdoutPath[MAX_PATH];
99 static char stderrPath[MAX_PATH];
100 # endif
101 #endif
102
103 #if defined(_WIN32_WCE) && _WIN32_WCE < 300
104 /* seems to be undefined in Win CE although in online help */
105 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
106 #endif /* _WIN32_WCE < 300 */
107
108 /* Parse a command line buffer into arguments */
ParseCommandLine(char * cmdline,char ** argv)109 static int ParseCommandLine(char *cmdline, char **argv)
110 {
111 char *bufp;
112 int argc;
113
114 argc = 0;
115 for ( bufp = cmdline; *bufp; ) {
116 /* Skip leading whitespace */
117 while ( isspace(*bufp) ) {
118 ++bufp;
119 }
120 /* Skip over argument */
121 if ( *bufp == '"' ) {
122 ++bufp;
123 if ( *bufp ) {
124 if ( argv ) {
125 argv[argc] = bufp;
126 }
127 ++argc;
128 }
129 /* Skip over word */
130 while ( *bufp && (*bufp != '"') ) {
131 ++bufp;
132 }
133 } else {
134 if ( *bufp ) {
135 if ( argv ) {
136 argv[argc] = bufp;
137 }
138 ++argc;
139 }
140 /* Skip over word */
141 while ( *bufp && ! isspace(*bufp) ) {
142 ++bufp;
143 }
144 }
145 if ( *bufp ) {
146 if ( argv ) {
147 *bufp = '\0';
148 }
149 ++bufp;
150 }
151 }
152 if ( argv ) {
153 argv[argc] = NULL;
154 }
155 return(argc);
156 }
157
158 /* Show an error message */
ShowError(const char * title,const char * message)159 static void ShowError(const char *title, const char *message)
160 {
161 /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
162 #ifdef USE_MESSAGEBOX
163 MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
164 #else
165 fprintf(stderr, "%s: %s\n", title, message);
166 #endif
167 }
168
169 /* Pop up an out of memory message, returns to Windows */
OutOfMemory(void)170 static BOOL OutOfMemory(void)
171 {
172 ShowError("Fatal Error", "Out of memory - aborting");
173 return FALSE;
174 }
175
176 /* SDL_Quit() shouldn't be used with atexit() directly because
177 calling conventions may differ... */
cleanup(void)178 static void cleanup(void)
179 {
180 SDL_Quit();
181 }
182
183 /* Remove the output files if there was no output written */
cleanup_output(void)184 static void cleanup_output(void)
185 {
186 #ifndef NO_STDIO_REDIRECT
187 FILE *file;
188 int empty;
189 #endif
190
191 /* Flush the output in case anything is queued */
192 fclose(stdout);
193 fclose(stderr);
194
195 #ifndef NO_STDIO_REDIRECT
196 /* See if the files have any output in them */
197 if ( stdoutPath[0] ) {
198 file = fopen(stdoutPath, TEXT("rb"));
199 if ( file ) {
200 empty = (fgetc(file) == EOF) ? 1 : 0;
201 fclose(file);
202 if ( empty ) {
203 remove(stdoutPath);
204 }
205 }
206 }
207 if ( stderrPath[0] ) {
208 file = fopen(stderrPath, TEXT("rb"));
209 if ( file ) {
210 empty = (fgetc(file) == EOF) ? 1 : 0;
211 fclose(file);
212 if ( empty ) {
213 remove(stderrPath);
214 }
215 }
216 }
217 #endif
218 }
219
220 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
221 /* The VC++ compiler needs main defined */
222 #define console_main main
223 #endif
224
225 /* This is where execution begins [console apps] */
console_main(int argc,char * argv[])226 int console_main(int argc, char *argv[])
227 {
228 size_t n;
229 char *bufp, *appname;
230 int status;
231
232 /* Get the class name from argv[0] */
233 appname = argv[0];
234 if ( (bufp=SDL_strrchr(argv[0], '\\')) != NULL ) {
235 appname = bufp+1;
236 } else
237 if ( (bufp=SDL_strrchr(argv[0], '/')) != NULL ) {
238 appname = bufp+1;
239 }
240
241 if ( (bufp=SDL_strrchr(appname, '.')) == NULL )
242 n = SDL_strlen(appname);
243 else
244 n = (bufp-appname);
245
246 bufp = SDL_stack_alloc(char, n+1);
247 if ( bufp == NULL ) {
248 return OutOfMemory();
249 }
250 SDL_strlcpy(bufp, appname, n+1);
251 appname = bufp;
252
253 /* Load SDL dynamic link library */
254 if ( SDL_Init(SDL_INIT_NOPARACHUTE) < 0 ) {
255 ShowError("WinMain() error", SDL_GetError());
256 return(FALSE);
257 }
258 atexit(cleanup_output);
259 atexit(cleanup);
260
261 /* Sam:
262 We still need to pass in the application handle so that
263 DirectInput will initialize properly when SDL_RegisterApp()
264 is called later in the video initialization.
265 */
266 SDL_SetModuleHandle(GetModuleHandle(NULL));
267
268 /* Run the application main() code */
269 status = SDL_main(argc, argv);
270
271 /* Exit cleanly, calling atexit() functions */
272 exit(status);
273
274 /* Hush little compiler, don't you cry... */
275 return 0;
276 }
277
278 /* This is where execution begins [windowed apps] */
279 #ifdef _WIN32_WCE
WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPWSTR szCmdLine,int sw)280 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
281 #else
282 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
283 #endif
284 {
285 HINSTANCE handle;
286 char **argv;
287 int argc;
288 char *cmdline;
289 DWORD pathlen;
290 #ifdef _WIN32_WCE
291 wchar_t path[MAX_PATH];
292 #else
293 char path[MAX_PATH];
294 #endif
295 #ifdef _WIN32_WCE
296 wchar_t *bufp;
297 int nLen;
298 #else
299 char *bufp;
300 size_t nLen;
301 #endif
302 #ifndef NO_STDIO_REDIRECT
303 FILE *newfp;
304 #endif
305
306 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
307 keep them open. This is a hack.. hopefully it will be fixed
308 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
309 */
310 handle = LoadLibrary(TEXT("DDRAW.DLL"));
311 if ( handle != NULL ) {
312 FreeLibrary(handle);
313 }
314
315 #ifndef NO_STDIO_REDIRECT
316 pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
317 while ( pathlen > 0 && path[pathlen] != '\\' ) {
318 --pathlen;
319 }
320 path[pathlen] = '\0';
321
322 #ifdef _WIN32_WCE
323 wcsncpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
324 wcsncat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
325 #else
326 SDL_strlcpy( stdoutPath, path, SDL_arraysize(stdoutPath) );
327 SDL_strlcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
328 #endif
329
330 /* Redirect standard input and standard output */
331 newfp = freopen(stdoutPath, TEXT("w"), stdout);
332
333 #ifndef _WIN32_WCE
334 if ( newfp == NULL ) { /* This happens on NT */
335 #if !defined(stdout)
336 stdout = fopen(stdoutPath, TEXT("w"));
337 #else
338 newfp = fopen(stdoutPath, TEXT("w"));
339 if ( newfp ) {
340 *stdout = *newfp;
341 }
342 #endif
343 }
344 #endif /* _WIN32_WCE */
345
346 #ifdef _WIN32_WCE
347 wcsncpy( stderrPath, path, SDL_arraysize(stdoutPath) );
348 wcsncat( stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath) );
349 #else
350 SDL_strlcpy( stderrPath, path, SDL_arraysize(stderrPath) );
351 SDL_strlcat( stderrPath, DIR_SEPERATOR STDERR_FILE, SDL_arraysize(stderrPath) );
352 #endif
353
354 newfp = freopen(stderrPath, TEXT("w"), stderr);
355 #ifndef _WIN32_WCE
356 if ( newfp == NULL ) { /* This happens on NT */
357 #if !defined(stderr)
358 stderr = fopen(stderrPath, TEXT("w"));
359 #else
360 newfp = fopen(stderrPath, TEXT("w"));
361 if ( newfp ) {
362 *stderr = *newfp;
363 }
364 #endif
365 }
366 #endif /* _WIN32_WCE */
367
368 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */
369 setbuf(stderr, NULL); /* No buffering */
370 #endif /* !NO_STDIO_REDIRECT */
371
372 #ifdef _WIN32_WCE
373 nLen = wcslen(szCmdLine)+128+1;
374 bufp = SDL_stack_alloc(wchar_t, nLen*2);
375 wcscpy (bufp, TEXT("\""));
376 GetModuleFileName(NULL, bufp+1, 128-3);
377 wcscpy (bufp+wcslen(bufp), TEXT("\" "));
378 wcsncpy(bufp+wcslen(bufp), szCmdLine,nLen-wcslen(bufp));
379 nLen = wcslen(bufp)+1;
380 cmdline = SDL_stack_alloc(char, nLen);
381 if ( cmdline == NULL ) {
382 return OutOfMemory();
383 }
384 WideCharToMultiByte(CP_ACP, 0, bufp, -1, cmdline, nLen, NULL, NULL);
385 #else
386 /* Grab the command line */
387 bufp = GetCommandLine();
388 nLen = SDL_strlen(bufp)+1;
389 cmdline = SDL_stack_alloc(char, nLen);
390 if ( cmdline == NULL ) {
391 return OutOfMemory();
392 }
393 SDL_strlcpy(cmdline, bufp, nLen);
394 #endif
395
396 /* Parse it into argv and argc */
397 argc = ParseCommandLine(cmdline, NULL);
398 argv = SDL_stack_alloc(char*, argc+1);
399 if ( argv == NULL ) {
400 return OutOfMemory();
401 }
402 ParseCommandLine(cmdline, argv);
403
404 /* Run the main program (after a little SDL initialization) */
405 console_main(argc, argv);
406
407 /* Hush little compiler, don't you cry... */
408 return 0;
409 }
410