/* @(#) 1.16 nt/src/docshell/WinMain.cpp, odshell, od96nt, odnt9712d 11/15/96 10:11:20 [3/21/97 17:16:33] */
//====START_GENERATED_PROLOG======================================
//
//
//   COMPONENT_NAME: odshell
//
//   CLASSES: none
//
//   ORIGINS: 27
//
//
//   (C) COPYRIGHT International Business Machines Corp. 1995,1996
//   All Rights Reserved
//   Licensed Materials - Property of IBM
//   US Government Users Restricted Rights - Use, duplication or
//   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//   	
//   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
//   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
//   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
//   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
//   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
//   OR PERFORMANCE OF THIS SOFTWARE.
//
//====END_GENERATED_PROLOG========================================
//


//===========================================================================
// WinMain.cpp
//
//	This file contains the main entry point for the Win32 version of
//	the OpenDoc document shell.
//
//===========================================================================

#include <windows.h>

#include <ODDebug.h>
#include <ODTypes.h>
#include <ODUtils.h>
#include <WinShell.xh>

#define MAXARGS 8	// Max number of command line args

void GetArgs(int *argc, char **argv);
void GetToken(char *s, int *start, int *length);


//===========================================================================
// WinMain
//
//	The required Win32 entry point.  It instantiates a WinShell
//	and starts it.
//===========================================================================
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                         LPSTR CmdLine, int CmdShow)
{
    long		returnValue = FALSE;
    int			argc = 0;
    char		*argv[MAXARGS];
    WindowSystemData	windowSystemData;
    Environment 	*ev = somGetGlobalEnvironment();

    // _argc and _argv are unavailable in VAC++ so we'll manufacture them
    GetArgs(&argc, argv);

    try
    {

        // Required initialization prior to any SOM calls ...
        ODInitExceptions();

        // Create a WinShell and start it
        WinShell *shell = new WinShell;
        if (shell)
        {
	    // Give the shell the hInstance
	    windowSystemData.instance = hInstance;
	    shell->SetWindowSystemData(ev, &windowSystemData);

	    // Ready, Set, ...
            returnValue = shell->Go(ev, argc, argv);

            // When shell->Go returns we're through
	    ODDeleteObject(shell);
        }

        ODTermExceptions();

    }
    catch (ODException _exception)
    {

        WARNMSG(WARN_INDEX(AMSG_510), "Exception %d returned to main.", 
                ErrorCode());
    } 

    return returnValue;
}

//===========================================================================
// GetArgs
//
//	Get the command line from the system and tokenize it into 
//	argc and argv.
//===========================================================================
void GetArgs(int *argc, char **argv)
{
    int  i = 0;
    int  length;
    int  limit;
    char *cmdline;

    // Get the command line
    cmdline = GetCommandLine();
    limit = strlen(cmdline);

    // Parse the command line
    while (i < limit)
    {
        GetToken(cmdline, &i, &length);
        if (length > 0)
        {
            argv[*argc] = (char *)malloc(length + 1);
            strncpy(argv[*argc], &cmdline[i], length);
            argv[*argc][length] = NULL;
            *argc += 1;
            i += length + 1;   // + 1 to skip the delimiter after this token
        }
    }
}

//===========================================================================
// GetToken
//
//	Scan a string starting at a given index and identify the next token.
//	Return the starting index and the length of the token.
//===========================================================================
void GetToken(char *s, int *start, int *length)
{
    int i;

    // Initialize to zero length (meaning no token)
    *length = 0;

    // Check for existence of string
    if (s)
    {
        // Skip any leading white space
        while (isspace(s[*start]))
            *start += 1;

        // Check for quoted token
        if (s[*start] == '\"')
        {
            // Look past the quote character
            *start += 1;

            // Skip any leading white space
            while (isspace(s[*start]))
                *start += 1;

            // Scan to ending quote or end of string
            i = *start;
            while (s[i] && s[i] != '\"')
                i++;
            *length = i - *start;
        }

        // Not a quoted token
        else
        {
            // Scan to next white space or end of string
            i = *start;
            while (s[i] && !isspace(s[i]))
                i++;
            *length = i - *start;
        }
    }
}
