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