Annotation of sbbs/src/conio/sdl_win32_main.c, revision 1.1

1.1     ! root        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 <string.h>
        !             9: #include <ctype.h>
        !            10: #include <stdlib.h>
        !            11: 
        !            12: #include <windows.h>
        !            13: #include <malloc.h>                    /* For _alloca() */
        !            14: 
        !            15: #ifdef _WIN32_WCE
        !            16: # define DIR_SEPERATOR TEXT("\\")
        !            17: # undef _getcwd
        !            18: # define _getcwd(str,len)      wcscpy(str,TEXT(""))
        !            19: # define setbuf(f,b)
        !            20: # define setvbuf(w,x,y,z)
        !            21: # define fopen         _wfopen
        !            22: # define freopen       _wfreopen
        !            23: # define remove(x)     DeleteFile(x)
        !            24: # define strcat                wcscat
        !            25: #else
        !            26: # define DIR_SEPERATOR TEXT("/")
        !            27: # include <direct.h>
        !            28: #endif
        !            29: 
        !            30: /* Include the SDL main definition header */
        !            31: #include "SDL.h"
        !            32: #include "SDL_main.h"
        !            33: extern C_LINKAGE int SDL_main_env(int argc, char *argv[], char **env);
        !            34: 
        !            35: #ifdef main
        !            36: # ifndef _WIN32_WCE_EMULATION
        !            37: #  undef main
        !            38: # endif /* _WIN32_WCE_EMULATION */
        !            39: #endif /* main */
        !            40: 
        !            41: #define NO_STDIO_REDIRECT
        !            42: 
        !            43: /* The standard output files */
        !            44: #define STDOUT_FILE    TEXT("stdout.txt")
        !            45: #define STDERR_FILE    TEXT("stderr.txt")
        !            46: 
        !            47: #ifndef NO_STDIO_REDIRECT
        !            48: # ifdef _WIN32_WCE
        !            49:   static wchar_t stdoutPath[MAX_PATH];
        !            50:   static wchar_t stderrPath[MAX_PATH];
        !            51: # else
        !            52:   static char stdoutPath[MAX_PATH];
        !            53:   static char stderrPath[MAX_PATH];
        !            54: # endif
        !            55: #endif
        !            56: 
        !            57: /* Special Dynamic/Static hackery */
        !            58: #include "sdlfuncs.h"
        !            59: struct sdlfuncs sdl;
        !            60: 
        !            61: #if defined(_WIN32_WCE) && _WIN32_WCE < 300
        !            62: /* seems to be undefined in Win CE although in online help */
        !            63: #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
        !            64: 
        !            65: /* seems to be undefined in Win CE although in online help */
        !            66: char *strrchr(char *str, int c)
        !            67: {
        !            68:        char *p;
        !            69: 
        !            70:        /* Skip to the end of the string */
        !            71:        p=str;
        !            72:        while (*p)
        !            73:                p++;
        !            74: 
        !            75:        /* Look for the given character */
        !            76:        while ( (p >= str) && (*p != (CHAR)c) )
        !            77:                p--;
        !            78: 
        !            79:        /* Return NULL if character not found */
        !            80:        if ( p < str ) {
        !            81:                p = NULL;
        !            82:        }
        !            83:        return p;
        !            84: }
        !            85: #endif /* _WIN32_WCE < 300 */
        !            86: 
        !            87: /* Parse a command line buffer into arguments */
        !            88: static int ParseCommandLine(char *cmdline, char **argv)
        !            89: {
        !            90:        char *bufp;
        !            91:        int argc;
        !            92: 
        !            93:        argc = 0;
        !            94:        for ( bufp = cmdline; *bufp; ) {
        !            95:                /* Skip leading whitespace */
        !            96:                while ( isspace(*bufp) ) {
        !            97:                        ++bufp;
        !            98:                }
        !            99:                /* Skip over argument */
        !           100:                if ( *bufp == '"' ) {
        !           101:                        ++bufp;
        !           102:                        if ( *bufp ) {
        !           103:                                if ( argv ) {
        !           104:                                        argv[argc] = bufp;
        !           105:                                }
        !           106:                                ++argc;
        !           107:                        }
        !           108:                        /* Skip over word */
        !           109:                        while ( *bufp && (*bufp != '"') ) {
        !           110:                                ++bufp;
        !           111:                        }
        !           112:                } else {
        !           113:                        if ( *bufp ) {
        !           114:                                if ( argv ) {
        !           115:                                        argv[argc] = bufp;
        !           116:                                }
        !           117:                                ++argc;
        !           118:                        }
        !           119:                        /* Skip over word */
        !           120:                        while ( *bufp && ! isspace(*bufp) ) {
        !           121:                                ++bufp;
        !           122:                        }
        !           123:                }
        !           124:                if ( *bufp ) {
        !           125:                        if ( argv ) {
        !           126:                                *bufp = '\0';
        !           127:                        }
        !           128:                        ++bufp;
        !           129:                }
        !           130:        }
        !           131:        if ( argv ) {
        !           132:                argv[argc] = NULL;
        !           133:        }
        !           134:        return(argc);
        !           135: }
        !           136: 
        !           137: /* Show an error message */
        !           138: static void ShowError(const char *title, const char *message)
        !           139: {
        !           140: /* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
        !           141: #ifdef USE_MESSAGEBOX
        !           142:        MessageBox(NULL, message, title, MB_ICONEXCLAMATION|MB_OK);
        !           143: #else
        !           144:        fprintf(stderr, "%s: %s\n", title, message);
        !           145: #endif
        !           146: }
        !           147: 
        !           148: /* Pop up an out of memory message, returns to Windows */
        !           149: static BOOL OutOfMemory(void)
        !           150: {
        !           151:        ShowError("Fatal Error", "Out of memory - aborting");
        !           152:        return FALSE;
        !           153: }
        !           154: 
        !           155: /* Remove the output files if there was no output written */
        !           156: static void __cdecl cleanup_output(void)
        !           157: {
        !           158: #ifndef NO_STDIO_REDIRECT
        !           159:        FILE *file;
        !           160:        int empty;
        !           161: #endif
        !           162: 
        !           163:        /* Flush the output in case anything is queued */
        !           164:        fclose(stdout);
        !           165:        fclose(stderr);
        !           166: 
        !           167: #ifndef NO_STDIO_REDIRECT
        !           168:        /* See if the files have any output in them */
        !           169:        if ( stdoutPath[0] ) {
        !           170:                file = fopen(stdoutPath, TEXT("rb"));
        !           171:                if ( file ) {
        !           172:                        empty = (fgetc(file) == EOF) ? 1 : 0;
        !           173:                        fclose(file);
        !           174:                        if ( empty ) {
        !           175:                                remove(stdoutPath);
        !           176:                        }
        !           177:                }
        !           178:        }
        !           179:        if ( stderrPath[0] ) {
        !           180:                file = fopen(stderrPath, TEXT("rb"));
        !           181:                if ( file ) {
        !           182:                        empty = (fgetc(file) == EOF) ? 1 : 0;
        !           183:                        fclose(file);
        !           184:                        if ( empty ) {
        !           185:                                remove(stderrPath);
        !           186:                        }
        !           187:                }
        !           188:        }
        !           189: #endif
        !           190: }
        !           191: 
        !           192: #if (defined(__BORLANDC__) || defined(_MSC_VER)) && !defined(_WIN32_WCE)
        !           193: /* The VC++ compiler needs main defined */
        !           194: #define console_main main
        !           195: #endif
        !           196: 
        !           197: /* This is where execution begins [console apps] */
        !           198: int console_main(int argc, char *argv[], char **env)
        !           199: {
        !           200:        int n;
        !           201:        char *bufp, *appname;
        !           202: 
        !           203:        /* Get the class name from argv[0] */
        !           204:        appname = argv[0];
        !           205:        if ( (bufp=strrchr(argv[0], '\\')) != NULL ) {
        !           206:                appname = bufp+1;
        !           207:        } else
        !           208:        if ( (bufp=strrchr(argv[0], '/')) != NULL ) {
        !           209:                appname = bufp+1;
        !           210:        }
        !           211: 
        !           212:        if ( (bufp=strrchr(appname, '.')) == NULL )
        !           213:                n = strlen(appname);
        !           214:        else
        !           215:                n = (bufp-appname);
        !           216: 
        !           217:        bufp = (char *)alloca(n+1);
        !           218:        if ( bufp == NULL ) {
        !           219:                return OutOfMemory();
        !           220:        }
        !           221:        strncpy(bufp, appname, n);
        !           222:        bufp[n] = '\0';
        !           223:        appname = bufp;
        !           224: 
        !           225:        /* Load SDL dynamic link library */
        !           226:        if(!load_sdl_funcs(&sdl)) {
        !           227:                if ( sdl.Init(SDL_INIT_NOPARACHUTE) < 0 ) {
        !           228:                        return(FALSE);
        !           229:                }
        !           230:                atexit(cleanup_output);
        !           231: 
        !           232: #ifndef DISABLE_VIDEO
        !           233:                /* Sam:
        !           234:                   We still need to pass in the application handle so that
        !           235:                   DirectInput will initialize properly when SDL_RegisterApp()
        !           236:                   is called later in the video initialization.
        !           237:                 */
        !           238:                sdl.SetModuleHandle(GetModuleHandle(NULL));
        !           239: #endif /* !DISABLE_VIDEO */
        !           240:        }
        !           241: 
        !           242:        /* Run the application main() code */
        !           243:        n=SDL_main_env(argc, argv, env);
        !           244: 
        !           245:        /* Exit cleanly, calling atexit() functions */
        !           246:        exit(n);
        !           247: 
        !           248:        /* Hush little compiler, don't you cry... */
        !           249:        return(n);
        !           250: }
        !           251: 
        !           252: /* This is where execution begins [windowed apps] */
        !           253: #ifdef _WIN32_WCE
        !           254: int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw)
        !           255: #else
        !           256: int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
        !           257: #endif
        !           258: {
        !           259:        HINSTANCE handle;
        !           260:        char **argv;
        !           261:        int argc;
        !           262:        char *cmdline;
        !           263: #ifdef _WIN32_WCE
        !           264:        wchar_t *bufp;
        !           265:        int nLen;
        !           266: #else
        !           267:        char *bufp;
        !           268: #endif
        !           269: #ifndef NO_STDIO_REDIRECT
        !           270:        FILE *newfp;
        !           271: #endif
        !           272: 
        !           273:        /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
        !           274:           keep them open.  This is a hack.. hopefully it will be fixed 
        !           275:           someday.  DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
        !           276:         */
        !           277:        handle = LoadLibrary(TEXT("DDRAW.DLL"));
        !           278:        if ( handle != NULL ) {
        !           279:                FreeLibrary(handle);
        !           280:        }
        !           281: 
        !           282: #ifndef NO_STDIO_REDIRECT
        !           283:        _getcwd( stdoutPath, sizeof( stdoutPath ) );
        !           284:        strcat( stdoutPath, DIR_SEPERATOR STDOUT_FILE );
        !           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:        _getcwd( stderrPath, sizeof( stderrPath ) );
        !           303:        strcat( stderrPath, DIR_SEPERATOR STDERR_FILE );
        !           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 = (char *)alloca(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:        cmdline = (char *)alloca(strlen(bufp)+1);
        !           340:        if ( cmdline == NULL ) {
        !           341:                return OutOfMemory();
        !           342:        }
        !           343:        strcpy(cmdline, bufp);
        !           344: #endif
        !           345: 
        !           346:        /* Parse it into argv and argc */
        !           347:        argc = ParseCommandLine(cmdline, NULL);
        !           348:        argv = (char **)alloca((argc+1)*(sizeof *argv));
        !           349:        if ( argv == NULL ) {
        !           350:                return OutOfMemory();
        !           351:        }
        !           352:        ParseCommandLine(cmdline, argv);
        !           353: 
        !           354:        /* Run the main program (after a little SDL initialization) */
        !           355:        return(console_main(argc, argv, _environ));
        !           356: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.