src/main/windows/SDL_windows_main.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 06 Jul 2013 15:56:06 +0200
changeset 7356 d1a0bbe64f15
parent 7281 cd5516b9263d
child 7450 29654d59c439
permissions -rw-r--r--
Added missing call to SDL_stack_free().
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@6044
     6
#include "SDL_config.h"
slouken@6044
     7
slouken@6044
     8
#ifdef __WIN32__
slouken@754
     9
slouken@754
    10
#include <stdio.h>
slouken@754
    11
#include <stdlib.h>
slouken@754
    12
slouken@1433
    13
#define WIN32_LEAN_AND_MEAN
slouken@1433
    14
#include <windows.h>
slouken@1336
    15
slouken@754
    16
/* Include the SDL main definition header */
slouken@754
    17
#include "SDL.h"
slouken@754
    18
#include "SDL_main.h"
slouken@754
    19
slouken@754
    20
#ifdef main
slouken@754
    21
#  undef main
slouken@754
    22
#endif /* main */
slouken@754
    23
slouken@2286
    24
static void
slouken@2286
    25
UnEscapeQuotes(char *arg)
slouken@2286
    26
{
slouken@2286
    27
    char *last = NULL;
slouken@2286
    28
slouken@2286
    29
    while (*arg) {
aschiffler@6719
    30
        if (*arg == '"' && (last != NULL && *last == '\\')) {
slouken@2286
    31
            char *c_curr = arg;
slouken@2286
    32
            char *c_last = last;
slouken@2286
    33
slouken@2286
    34
            while (*c_curr) {
slouken@2286
    35
                *c_last = *c_curr;
slouken@2286
    36
                c_last = c_curr;
slouken@2286
    37
                c_curr++;
slouken@2286
    38
            }
slouken@2286
    39
            *c_last = '\0';
slouken@2286
    40
        }
slouken@2286
    41
        last = arg;
slouken@2286
    42
        arg++;
slouken@2286
    43
    }
slouken@2286
    44
}
slouken@2286
    45
slouken@754
    46
/* Parse a command line buffer into arguments */
slouken@1895
    47
static int
slouken@1895
    48
ParseCommandLine(char *cmdline, char **argv)
slouken@754
    49
{
slouken@1895
    50
    char *bufp;
slouken@2286
    51
    char *lastp = NULL;
slouken@2286
    52
    int argc, last_argc;
slouken@754
    53
slouken@2286
    54
    argc = last_argc = 0;
slouken@1895
    55
    for (bufp = cmdline; *bufp;) {
slouken@1895
    56
        /* Skip leading whitespace */
slouken@1895
    57
        while (isspace(*bufp)) {
slouken@1895
    58
            ++bufp;
slouken@1895
    59
        }
slouken@1895
    60
        /* Skip over argument */
slouken@1895
    61
        if (*bufp == '"') {
slouken@1895
    62
            ++bufp;
slouken@1895
    63
            if (*bufp) {
slouken@1895
    64
                if (argv) {
slouken@1895
    65
                    argv[argc] = bufp;
slouken@1895
    66
                }
slouken@1895
    67
                ++argc;
slouken@1895
    68
            }
slouken@1895
    69
            /* Skip over word */
slouken@4511
    70
            lastp = bufp;
slouken@2286
    71
            while (*bufp && (*bufp != '"' || *lastp == '\\')) {
slouken@2286
    72
                lastp = bufp;
slouken@1895
    73
                ++bufp;
slouken@1895
    74
            }
slouken@1895
    75
        } else {
slouken@1895
    76
            if (*bufp) {
slouken@1895
    77
                if (argv) {
slouken@1895
    78
                    argv[argc] = bufp;
slouken@1895
    79
                }
slouken@1895
    80
                ++argc;
slouken@1895
    81
            }
slouken@1895
    82
            /* Skip over word */
slouken@1895
    83
            while (*bufp && !isspace(*bufp)) {
slouken@1895
    84
                ++bufp;
slouken@1895
    85
            }
slouken@1895
    86
        }
slouken@1895
    87
        if (*bufp) {
slouken@1895
    88
            if (argv) {
slouken@1895
    89
                *bufp = '\0';
slouken@1895
    90
            }
slouken@1895
    91
            ++bufp;
slouken@1895
    92
        }
slouken@2286
    93
slouken@2286
    94
        /* Strip out \ from \" sequences */
slouken@2286
    95
        if (argv && last_argc != argc) {
slouken@2286
    96
            UnEscapeQuotes(argv[last_argc]);
slouken@2286
    97
        }
slouken@2286
    98
        last_argc = argc;
slouken@1895
    99
    }
slouken@1895
   100
    if (argv) {
slouken@1895
   101
        argv[argc] = NULL;
slouken@1895
   102
    }
slouken@1895
   103
    return (argc);
slouken@754
   104
}
slouken@754
   105
slouken@754
   106
/* Show an error message */
slouken@1895
   107
static void
slouken@1895
   108
ShowError(const char *title, const char *message)
slouken@754
   109
{
slouken@754
   110
/* If USE_MESSAGEBOX is defined, you need to link with user32.lib */
slouken@754
   111
#ifdef USE_MESSAGEBOX
slouken@1895
   112
    MessageBox(NULL, message, title, MB_ICONEXCLAMATION | MB_OK);
slouken@754
   113
#else
slouken@1895
   114
    fprintf(stderr, "%s: %s\n", title, message);
slouken@754
   115
#endif
slouken@754
   116
}
slouken@754
   117
slouken@754
   118
/* Pop up an out of memory message, returns to Windows */
slouken@1895
   119
static BOOL
slouken@1895
   120
OutOfMemory(void)
slouken@754
   121
{
slouken@1895
   122
    ShowError("Fatal Error", "Out of memory - aborting");
slouken@1895
   123
    return FALSE;
slouken@754
   124
}
slouken@754
   125
icculus@6430
   126
#if defined(_MSC_VER)
slouken@754
   127
/* The VC++ compiler needs main defined */
slouken@754
   128
#define console_main main
slouken@754
   129
#endif
slouken@754
   130
slouken@754
   131
/* This is where execution begins [console apps] */
slouken@1895
   132
int
slouken@1895
   133
console_main(int argc, char *argv[])
slouken@754
   134
{
slouken@1895
   135
    int status;
slouken@754
   136
slouken@7281
   137
    SDL_SetMainReady();
slouken@7281
   138
slouken@1895
   139
    /* Run the application main() code */
slouken@1895
   140
    status = SDL_main(argc, argv);
slouken@754
   141
slouken@1895
   142
    /* Exit cleanly, calling atexit() functions */
slouken@1895
   143
    exit(status);
slouken@754
   144
slouken@1895
   145
    /* Hush little compiler, don't you cry... */
slouken@1895
   146
    return 0;
slouken@754
   147
}
slouken@754
   148
slouken@754
   149
/* This is where execution begins [windowed apps] */
slouken@1895
   150
int WINAPI
slouken@6672
   151
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
slouken@754
   152
{
slouken@1895
   153
    char **argv;
slouken@1895
   154
    int argc;
slouken@1895
   155
    char *cmdline;
slouken@754
   156
slouken@1895
   157
    /* Grab the command line */
slouken@6672
   158
    TCHAR *text = GetCommandLine();
slouken@6672
   159
#if UNICODE
slouken@6672
   160
    cmdline = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(text), (SDL_wcslen(text)+1)*sizeof(WCHAR));
slouken@6672
   161
#else
slouken@6672
   162
    cmdline = SDL_strdup(text);
slouken@6672
   163
#endif
slouken@1895
   164
    if (cmdline == NULL) {
slouken@1895
   165
        return OutOfMemory();
slouken@1895
   166
    }
slouken@754
   167
slouken@1895
   168
    /* Parse it into argv and argc */
slouken@1895
   169
    argc = ParseCommandLine(cmdline, NULL);
slouken@1895
   170
    argv = SDL_stack_alloc(char *, argc + 1);
slouken@1895
   171
    if (argv == NULL) {
slouken@1895
   172
        return OutOfMemory();
slouken@1895
   173
    }
slouken@1895
   174
    ParseCommandLine(cmdline, argv);
slouken@754
   175
slouken@1895
   176
    /* Run the main program */
slouken@1895
   177
    console_main(argc, argv);
slouken@754
   178
philipp@7356
   179
    SDL_stack_free(argv);
philipp@7356
   180
slouken@6672
   181
    SDL_free(cmdline);
slouken@6672
   182
slouken@1895
   183
    /* Hush little compiler, don't you cry... */
slouken@1895
   184
    return 0;
slouken@1895
   185
}
slouken@754
   186
slouken@6044
   187
#endif /* __WIN32__ */
slouken@6044
   188
slouken@1895
   189
/* vi: set ts=4 sw=4 expandtab: */