diff options
author | Andrey Nazarov <skuller@skuller.net> | 2007-11-28 19:55:33 +0000 |
---|---|---|
committer | Andrey Nazarov <skuller@skuller.net> | 2007-11-28 19:55:33 +0000 |
commit | 686b16c6dbcfd80814595dc06b175c11e9ed6e03 (patch) | |
tree | 696a0bf539fd467317160216362b3dc1e8362832 /source | |
parent | be2af57710a86eb812ec36286541046f1a7c8586 (diff) |
Cleaned up Windows command line handling.
Diffstat (limited to 'source')
-rw-r--r-- | source/sys_win.c | 137 |
1 files changed, 73 insertions, 64 deletions
diff --git a/source/sys_win.c b/source/sys_win.c index d321a5d..2a807c7 100644 --- a/source/sys_win.c +++ b/source/sys_win.c @@ -1508,55 +1508,7 @@ static void msvcrt_sucks( const wchar_t *expr, const wchar_t *func, const wchar_ } #endif - -#define MAX_LINE_TOKENS 128 - -static char *sys_argv[MAX_LINE_TOKENS]; -static int sys_argc; - -/* -=============== -Sys_ParseCommandLine - -=============== -*/ -static void Sys_ParseCommandLine( char *line ) { - sys_argc = 1; - sys_argv[0] = APPLICATION; - while( *line ) { - while( *line && *line <= 32 ) { - line++; - } - if( *line == 0 ) { - break; - } - sys_argv[sys_argc++] = line; - while( *line > 32 ) { - line++; - } - if( *line == 0 ) { - break; - } - *line = 0; - if( sys_argc == MAX_LINE_TOKENS ) { - break; - } - line++; - } -} - -/* -================== -WinMain - -================== -*/ -int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { - /* previous instances do not exist in Win32 */ - if( hPrevInstance ) { - return 1; - } - +static int Sys_Main( int argc, char **argv ) { #ifdef DEDICATED_ONLY if( !GetModuleFileName( NULL, currentDirectory, sizeof( currentDirectory ) - 1 ) ) { return 1; @@ -1578,8 +1530,6 @@ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin currentDirectory[sizeof( currentDirectory ) - 1] = 0; #endif - hGlobalInstance = hInstance; - #ifdef USE_DBGHELP #ifdef _MSC_VER __try { @@ -1594,11 +1544,9 @@ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin _set_invalid_parameter_handler( msvcrt_sucks ); #endif - Sys_ParseCommandLine( lpCmdLine ); + Qcommon_Init( argc, argv ); - Qcommon_Init( sys_argc, sys_argv ); - - /* main program loop */ + // main program loop while( !shouldExit ) { Qcommon_Frame(); } @@ -1619,8 +1567,13 @@ int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLin return 0; } + + #ifdef DEDICATED_ONLY +static char **sys_argv; +static int sys_argc; + static VOID WINAPI ServiceHandler( DWORD fdwControl ) { if( fdwControl == SERVICE_CONTROL_STOP ) { shouldExit = qtrue; @@ -1641,7 +1594,7 @@ static VOID WINAPI ServiceMain( DWORD argc, LPTSTR *argv ) { status.dwControlsAccepted = SERVICE_ACCEPT_STOP; SetServiceStatus( statusHandle, &status ); - WinMain( GetModuleHandle( NULL ), NULL, GetCommandLineA(), 0 ); + Sys_Main( sys_argc, sys_argv ); status.dwCurrentState = SERVICE_STOPPED; status.dwControlsAccepted = 0; @@ -1662,24 +1615,80 @@ main int QDECL main( int argc, char **argv ) { int i; + hGlobalInstance = GetModuleHandle( NULL ); + for( i = 1; i < argc; i++ ) { if( !strcmp( argv[i], "-service" ) ) { - goto service; + argv[i] = NULL; + sys_argc = argc; + sys_argv = argv; + if( StartServiceCtrlDispatcher( serviceTable ) ) { + return 0; + } + if( GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT ) { + break; // fall back to normal server startup + } + return 1; } } + + return Sys_Main( argc, argv ); +} + +#else // DEDICATED_ONLY - return WinMain( GetModuleHandle( NULL ), NULL, GetCommandLineA(), 0 ); +#define MAX_LINE_TOKENS 128 -service: - if( !StartServiceCtrlDispatcher( serviceTable ) ) { - if( GetLastError() == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT ) { - return WinMain( GetModuleHandle( NULL ), NULL, GetCommandLineA(), 0 ); +static char *sys_argv[MAX_LINE_TOKENS]; +static int sys_argc; + +/* +=============== +Sys_ParseCommandLine + +=============== +*/ +static void Sys_ParseCommandLine( char *line ) { + sys_argc = 1; + sys_argv[0] = APPLICATION; + while( *line ) { + while( *line && *line <= 32 ) { + line++; + } + if( *line == 0 ) { + break; + } + sys_argv[sys_argc++] = line; + while( *line > 32 ) { + line++; + } + if( *line == 0 ) { + break; + } + *line = 0; + if( sys_argc == MAX_LINE_TOKENS ) { + break; } + line++; + } +} + +/* +================== +WinMain + +================== +*/ +int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ) { + // previous instances do not exist in Win32 + if( hPrevInstance ) { return 1; } - return 0; + hGlobalInstance = hInstance; + Sys_ParseCommandLine( lpCmdLine ); + return Sys_Main( sys_argc, sys_argv ); } -#endif +#endif // !DEDICATED_ONLY |