SDL_windows_main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. SDL_windows_main.c, placed in the public domain by Sam Lantinga 4/13/98
  3. The WinMain function -- calls your program's main() function
  4. */
  5. #include "SDL_config.h"
  6. #ifdef __WIN32__
  7. /* Include this so we define UNICODE properly */
  8. #include "../../core/windows/SDL_windows.h"
  9. /* Include the SDL main definition header */
  10. #include "SDL.h"
  11. #include "SDL_main.h"
  12. #ifdef main
  13. # undef main
  14. #endif /* main */
  15. static void
  16. UnEscapeQuotes(char *arg)
  17. {
  18. char *last = NULL;
  19. while (*arg) {
  20. if (*arg == '"' && (last != NULL && *last == '\\')) {
  21. char *c_curr = arg;
  22. char *c_last = last;
  23. while (*c_curr) {
  24. *c_last = *c_curr;
  25. c_last = c_curr;
  26. c_curr++;
  27. }
  28. *c_last = '\0';
  29. }
  30. last = arg;
  31. arg++;
  32. }
  33. }
  34. /* Parse a command line buffer into arguments */
  35. static int
  36. ParseCommandLine(char *cmdline, char **argv)
  37. {
  38. char *bufp;
  39. char *lastp = NULL;
  40. int argc, last_argc;
  41. argc = last_argc = 0;
  42. for (bufp = cmdline; *bufp;) {
  43. /* Skip leading whitespace */
  44. while (SDL_isspace(*bufp)) {
  45. ++bufp;
  46. }
  47. /* Skip over argument */
  48. if (*bufp == '"') {
  49. ++bufp;
  50. if (*bufp) {
  51. if (argv) {
  52. argv[argc] = bufp;
  53. }
  54. ++argc;
  55. }
  56. /* Skip over word */
  57. lastp = bufp;
  58. while (*bufp && (*bufp != '"' || *lastp == '\\')) {
  59. lastp = bufp;
  60. ++bufp;
  61. }
  62. } else {
  63. if (*bufp) {
  64. if (argv) {
  65. argv[argc] = bufp;
  66. }
  67. ++argc;
  68. }
  69. /* Skip over word */
  70. while (*bufp && !SDL_isspace(*bufp)) {
  71. ++bufp;
  72. }
  73. }
  74. if (*bufp) {
  75. if (argv) {
  76. *bufp = '\0';
  77. }
  78. ++bufp;
  79. }
  80. /* Strip out \ from \" sequences */
  81. if (argv && last_argc != argc) {
  82. UnEscapeQuotes(argv[last_argc]);
  83. }
  84. last_argc = argc;
  85. }
  86. if (argv) {
  87. argv[argc] = NULL;
  88. }
  89. return (argc);
  90. }
  91. /* Pop up an out of memory message, returns to Windows */
  92. static BOOL
  93. OutOfMemory(void)
  94. {
  95. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal Error", "Out of memory - aborting", NULL);
  96. return FALSE;
  97. }
  98. #if defined(_MSC_VER)
  99. /* The VC++ compiler needs main/wmain defined */
  100. # define console_ansi_main main
  101. # if UNICODE
  102. # define console_wmain wmain
  103. # endif
  104. #endif
  105. /* WinMain, main, and wmain eventually call into here. */
  106. static int
  107. main_utf8(int argc, char *argv[])
  108. {
  109. SDL_SetMainReady();
  110. /* Run the application main() code */
  111. return SDL_main(argc, argv);
  112. }
  113. /* This is where execution begins [console apps, ansi] */
  114. int
  115. console_ansi_main(int argc, char *argv[])
  116. {
  117. /* !!! FIXME: are these in the system codepage? We need to convert to UTF-8. */
  118. return main_utf8(argc, argv);
  119. }
  120. #if UNICODE
  121. /* This is where execution begins [console apps, unicode] */
  122. int
  123. console_wmain(int argc, wchar_t *wargv[], wchar_t *wenvp)
  124. {
  125. char **argv = SDL_stack_alloc(char*, argc);
  126. int i;
  127. for (i = 0; i < argc; ++i) {
  128. argv[i] = WIN_StringToUTF8(wargv[i]);
  129. }
  130. return main_utf8(argc, argv);
  131. }
  132. #endif
  133. /* This is where execution begins [windowed apps] */
  134. int WINAPI
  135. WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
  136. {
  137. char **argv;
  138. int argc;
  139. char *cmdline;
  140. /* Grab the command line */
  141. TCHAR *text = GetCommandLine();
  142. #if UNICODE
  143. cmdline = WIN_StringToUTF8(text);
  144. #else
  145. cmdline = SDL_strdup(text);
  146. #endif
  147. if (cmdline == NULL) {
  148. return OutOfMemory();
  149. }
  150. /* Parse it into argv and argc */
  151. argc = ParseCommandLine(cmdline, NULL);
  152. argv = SDL_stack_alloc(char *, argc + 1);
  153. if (argv == NULL) {
  154. return OutOfMemory();
  155. }
  156. ParseCommandLine(cmdline, argv);
  157. /* Run the main program */
  158. main_utf8(argc, argv);
  159. SDL_stack_free(argv);
  160. SDL_free(cmdline);
  161. /* Hush little compiler, don't you cry... */
  162. return 0;
  163. }
  164. #endif /* __WIN32__ */
  165. /* vi: set ts=4 sw=4 expandtab: */