SDL  2.0
SDL_windows_main.c
Go to the documentation of this file.
1 /*
2  SDL_windows_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 this so we define UNICODE properly */
11 #include "../../core/windows/SDL_windows.h"
12 
13 /* Include the SDL main definition header */
14 #include "SDL.h"
15 #include "SDL_main.h"
16 
17 #ifdef main
18 # undef main
19 #endif /* main */
20 
21 static void
22 UnEscapeQuotes(char *arg)
23 {
24  char *last = NULL;
25 
26  while (*arg) {
27  if (*arg == '"' && (last != NULL && *last == '\\')) {
28  char *c_curr = arg;
29  char *c_last = last;
30 
31  while (*c_curr) {
32  *c_last = *c_curr;
33  c_last = c_curr;
34  c_curr++;
35  }
36  *c_last = '\0';
37  }
38  last = arg;
39  arg++;
40  }
41 }
42 
43 /* Parse a command line buffer into arguments */
44 static int
45 ParseCommandLine(char *cmdline, char **argv)
46 {
47  char *bufp;
48  char *lastp = NULL;
49  int argc, last_argc;
50 
51  argc = last_argc = 0;
52  for (bufp = cmdline; *bufp;) {
53  /* Skip leading whitespace */
54  while (*bufp == ' ' || *bufp == '\t') {
55  ++bufp;
56  }
57  /* Skip over argument */
58  if (*bufp == '"') {
59  ++bufp;
60  if (*bufp) {
61  if (argv) {
62  argv[argc] = bufp;
63  }
64  ++argc;
65  }
66  /* Skip over word */
67  lastp = bufp;
68  while (*bufp && (*bufp != '"' || *lastp == '\\')) {
69  lastp = bufp;
70  ++bufp;
71  }
72  } else {
73  if (*bufp) {
74  if (argv) {
75  argv[argc] = bufp;
76  }
77  ++argc;
78  }
79  /* Skip over word */
80  while (*bufp && (*bufp != ' ' && *bufp != '\t')) {
81  ++bufp;
82  }
83  }
84  if (*bufp) {
85  if (argv) {
86  *bufp = '\0';
87  }
88  ++bufp;
89  }
90 
91  /* Strip out \ from \" sequences */
92  if (argv && last_argc != argc) {
93  UnEscapeQuotes(argv[last_argc]);
94  }
95  last_argc = argc;
96  }
97  if (argv) {
98  argv[argc] = NULL;
99  }
100  return (argc);
101 }
102 
103 /* Pop up an out of memory message, returns to Windows */
104 static BOOL
105 OutOfMemory(void)
106 {
107  SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
108  return FALSE;
109 }
110 
111 #if defined(_MSC_VER)
112 /* The VC++ compiler needs main/wmain defined */
113 # define console_ansi_main main
114 # if UNICODE
115 # define console_wmain wmain
116 # endif
117 #endif
118 
119 /* Gets the arguments with GetCommandLine, converts them to argc and argv
120  and calls SDL_main */
121 static int
122 main_getcmdline()
123 {
124  char **argv;
125  int argc;
126  char *cmdline = NULL;
127  int retval = 0;
128  int cmdalloc = 0;
129  const TCHAR *text = GetCommandLine();
130  const TCHAR *ptr;
131  int argc_guess = 2; /* space for NULL and initial argument. */
132  int rc;
133 
134  /* make a rough guess of command line arguments. Overestimates if there
135  are quoted things. */
136  for (ptr = text; *ptr; ptr++) {
137  if ((*ptr == ' ') || (*ptr == '\t')) {
138  argc_guess++;
139  }
140  }
141 
142 #if UNICODE
143  rc = WideCharToMultiByte(CP_UTF8, 0, text, -1, NULL, 0, NULL, NULL);
144  if (rc > 0) {
145  cmdalloc = rc + (sizeof (char *) * argc_guess);
146  argv = (char **) VirtualAlloc(NULL, cmdalloc, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
147  if (argv) {
148  int rc2;
149  cmdline = (char *) (argv + argc_guess);
150  rc2 = WideCharToMultiByte(CP_UTF8, 0, text, -1, cmdline, rc, NULL, NULL);
151  SDL_assert(rc2 == rc);
152  }
153  }
154 #else
155  /* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */
156  rc = ((int) SDL_strlen(text)) + 1;
157  cmdalloc = rc + (sizeof (char *) * argc_guess);
158  argv = (char **) VirtualAlloc(NULL, cmdalloc, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
159  if (argv) {
160  cmdline = (char *) (argv + argc_guess);
161  SDL_strcpy(cmdline, text);
162  }
163 #endif
164  if (cmdline == NULL) {
165  return OutOfMemory();
166  }
167 
168  /* Parse it into argv and argc */
169  SDL_assert(ParseCommandLine(cmdline, NULL) <= argc_guess);
170  argc = ParseCommandLine(cmdline, argv);
171 
173 
174  /* Run the application main() code */
175  retval = SDL_main(argc, argv);
176 
177  VirtualFree(argv, cmdalloc, MEM_DECOMMIT);
178  VirtualFree(argv, 0, MEM_RELEASE);
179 
180  return retval;
181 }
182 
183 /* This is where execution begins [console apps, ansi] */
184 int
185 console_ansi_main(int argc, char *argv[])
186 {
187  return main_getcmdline();
188 }
189 
190 
191 #if UNICODE
192 /* This is where execution begins [console apps, unicode] */
193 int
194 console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
195 {
196  return main_getcmdline();
197 }
198 #endif
199 
200 /* This is where execution begins [windowed apps] */
201 int WINAPI
202 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
203 {
204  return main_getcmdline();
205 }
206 
207 #endif /* __WIN32__ */
208 
209 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_SetMainReady
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst op &r &cond WK op &r &cond WK op &r &cond WK else op &m &cond &ia op &r &cond WK else op &m &cond &ia elseif elseif else error unsupported base if elseif elseif else error unsupported unaligned pixldst unaligned endm macro pixst base base else pixldst base endif endm macro PF ptr
int CALLBACK WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
SDL_bool retval
#define SDL_ShowSimpleMessageBox
#define SDL_assert(condition)
Definition: SDL_assert.h:169
#define NULL
Definition: begin_code.h:164
static char text[MAX_TEXT_LENGTH]
Definition: testime.c:47
C_LINKAGE SDLMAIN_DECLSPEC int SDL_main(int argc, char *argv[])
#define SDL_strlen
#define FALSE
Definition: edid-parse.c:34