/* vi:set sts=4 sw=4:
 *
 * VIM - Vi IMproved		by Bram Moolenaar
 *				GUI support by Robert Webb
 *
 * Do ":help uganda"  in Vim to read copying and usage conditions.
 * Do ":help credits" in Vim to see a list of people who contributed.
 *
 * Windows GUI: main program (EXE) entry point:
 *
 * Ron Aaron <ron@mossbayeng.com> wrote this and  the DLL support code.
 */
#include "vim.h"
#include "globals.h"
#include "proto.h"
#include <windows.h>

/* cproto doesn't create a prototype for main() */
int _cdecl main __PARMS((int argc, char **argv));

#ifndef PROTO
#ifdef __BORLANDC__
void SaveInst(HINSTANCE hInst);
#endif

int WINAPI
WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInst,
    LPSTR lpszCmdLine,
    int nCmdShow)
{
    int argc;
    char **argv;

    int i;
    char* pch;
    char* pszNewCmdLine;
    char prog[256];
    char *p;
    int fIsQuote;

    /*
     * Ron: added full path name so that the $VIM variable will get set to our
     * startup path (so the .vimrc file can be found w/o a VIM env. var.)
     */
    GetModuleFileName(NULL, prog, 255);
    p = strrchr(prog, '.');
    if (p != NULL)
	*p = '\0';

    /*
     * Add the size of the string, the separating space, and a terminating '\0'
     */   
    pszNewCmdLine = (char *) malloc(STRLEN(lpszCmdLine) + STRLEN(prog) + 2);
    if (pszNewCmdLine == NULL)
	return 0;
    
    STRCPY(pszNewCmdLine, prog);
    STRCAT(pszNewCmdLine, " ");
    STRCAT(pszNewCmdLine, lpszCmdLine);
    
    while ((*pszNewCmdLine != '\0') && (*pszNewCmdLine == ' '))
	pszNewCmdLine++;	   /* start on 1st non-space */
    
    pch = pszNewCmdLine;
    argc = 0;

    while ( *pch != '\0' )
    {
	/* Ron: Handle quoted strings in args list */
	fIsQuote = (*pch == '\"');
	if (fIsQuote)
	    ++pch;

	argc++;			    /* this is an argument */
	if (fIsQuote) 
	{
	    while ((*pch != '\0') && (*pch != '\"'))
		pch++;		    /* advance until a closing quote */
	    if (*pch)
		pch++;
	}
	else
	{
	    while ((*pch != '\0') && (*pch != ' '))
		pch++;		    /* advance until a space */
	}
	while (*pch && *pch == ' ' )
	    pch++;		    /* advance until a non-space */
    }
    
    argv = (char**) malloc((argc+1) * sizeof(char*));
    if (argv == NULL )
	return 0;		   /* malloc error */
    
    i = 0;
    pch = pszNewCmdLine;

    while ((i < argc) && (*pch != '\0'))
    {
	fIsQuote = (*pch == '\"');
	if (fIsQuote)
	    ++pch;
	
	argv[i++] = pch;
	if (fIsQuote)
	{
	    while (*pch != '\0' && *pch != '\"')
		pch++;		    /* advance until the closing quote */
	}
	else
	{
	    while (*pch != '\0' && *pch != ' ')
		pch++;		    /* advance until a space */
	}
	if (*pch != '\0')
	    *(pch++) = '\0';	    /* parse argument here */
	while (*pch && *pch == ' ')
	    pch++;		    /* advance until a non-space */
    }

    ASSERT(i == argc);
    
    argv[argc] = (char *) NULL;    /* NULL-terminated list */
    

// ron: This is either local, or in the DLL depending on what we've done:
#ifdef VIMDLL
//  put here LoadLibrary if we decide to do that...
#endif
#ifdef __BORLANDC__
    SaveInst(hInstance);
#endif
    main (argc, argv);
    
    free(argv);
    free(pszNewCmdLine);

    return 0;
}
#endif
