// (c) 1998-2007 Electronic Arts Inc.

const char *SPIT_history[] =
{
"------------------------------------------------------------------",
"                                                                  ",
"                     Editor Frontend v1.15                        ",
"                                                                  ",
"                     by Frank Barchard, EAC                       ",
"                                                                  ",
"                   Main program - Feb 02, 2004                    ",
"                                                                  ",
"------------------------------------------------------------------",
"                                                                  ",
" Version Date   SE History:                                       ",
" ------- ----   -- --------                                       ",
"    1.00 981029 FB initial version                                ",
"    1.01 981105 FB 'name' for spaces                              ",
"    1.02 990129 FB WinMain, verbose 2 print, msdev/notepad        ",
"    1.03 990308 FB eg.c suffix                                    ",
"    1.04 990526 FB prepend drive/path, use spawn for win2000      ",
"    1.05 990628 FB .vsm,.cc,v.dsm for sony                        ",
"    1.06 990827 FB _test and _time suffixes                       ",
"    1.07 991130 FB .java                                          ",
"    1.08 000531 FB .xml                                           ",
"    1.09 001129 FB _bug extension                                 ",
"    1.10 010123 FB suffixes for batch files too                   ",
"    1.11 010531 FB .asp,.ref                                      ",
"    1.12 021017 FB .cs                                            ",
"    1.13 021031 FB devenv,editplus,notepad                        ",
"    1.14 021031 FB inl                                            ",
"    1.15 050302 FB removed extensions, added quoted names/spaces  ",
"                                                                  ",
"------------------------------------------------------------------",
"\0"
};

#define CONSOLE 1

/****************************************************************/
/*  Spit Setup                                                  */
/****************************************************************/

#include <stdio.h>
#include <limits.h>

#if defined(_MSC_VER)
#pragma warning(push,1)
#include <windows.h>
#include <process.h>
#pragma warning(pop)
#endif

#include "spit.h"

const char TextVersion[]="1.15 (Samba) 2007.12.4";
#define TOOL_VERSION 115
#define TOOL_NAME "e"

char OPTeditor[SPITMAXLINE];
char OPTeditor2[SPITMAXLINE];
char OPTeditor3[SPITMAXLINE];
int OPTuseeditor;

int SPIT_fileexist(char *nextname)
{
    int ok=0;
    GSTREAM *g;
    g = gopen(nextname);
    if (g)
    {
        ok = 1;
        gclose(g);
    }
    return(ok);
}

#if 0
/*
;
; ABSTRACT
;
; e - editor tool to spit front end
;
; SUMMARY
;
; e -options [files]
;     @file     response file, containing a list of options and files
;     =file     specify a output file ie e source=dest
;     -1        use 1st editor (devenv)
;     -2        use 2nd editor (editplus)
;     -3        use 3rd editor (notepad)
;     -e<path>  path to editor 1. default devenv
;     -e2<path> path to editor 2. default editplus
;     -e3<path> path to editor 3. default notepad
;     -nologo   disable banner
;     -r        recurse subdirectories
;     -v0       no console output
;     -v1       prints source file name and errors
;     -v2       status line for each file (default)
;     -v3       additional info
;
; DESCRIPTION
;
; Calls editor with arguments.  Editor by default is devenv, but
; you can set the E_OPTS environment variable to -e<path> to configure
; whatever editor you want. This spit based front end lets you use spit
; response files and wildcards with any editor.
; Tolerates grep -l output (File name:)
;
; RETURNS
;
; 0 if no errors else number of errors
;
; SEE ALSO
;
; e32, codewrite, visual c
;
; SAMPLE OUTPUT
;
; e bob.c
; e @rsp
;
; END ABSTRACT
;
*/
#endif

/****************************************************************/
/*  SPIT Routines                                               */
/****************************************************************/

void SPIT_reset(void)
{
    OPTbanner = SPITNO;
    OPTdestdir = 0; /* disable SPIT from checking for destination directories */
    OPTminargs = 0;
    strcpy(OPTeditor, "devenv.exe");
    strcpy(OPTeditor2,"editplus.exe");
    strcpy(OPTeditor3,"notepad.exe");
    OPTuseeditor = 0;
    return;
}

void SPIT_usage(const char *prgname, const char *envname, const char *urlname, int utype)
{
#if CONSOLE
    printf("E v%s by FrANK Barchard, EAC\n",TextVersion);
    if (utype)
        printf("USAGE: %s -options [files]\n", prgname);

    switch (utype)
    {
        case 2:
            puts("    -e path of editor            -2 use 2nd editor (editplus)");
            puts("    -3 use 3rd editor (notepad)  -r recurse subdirectories");
            puts("    -w write file                -v verbose");
            puts("     @ response file              = output file");
          printf("type '%s -h' for more help\n", prgname);
          printf("type '%s -help' for web page (%s)\n", prgname, urlname);
            break;

        case 3:
            puts("");
          printf("VERSION: %s v%s, Spit v%.2f\n",SPIT_MACHINE,TextVersion,(float)SPIT_VERSION/100.0f);
            puts("SUMMARY: Text editor spit frontend");
          printf("         SPIT environment variable is %s\n",envname);
            puts("");
            puts("    @file     response file, containing a list of options and files");
          printf("    =file     specify a output file ie %s source=dest\n", prgname);
            puts("    -1        use 1st editor (devenv)");
            puts("    -2        use 2nd editor (editplus)");
            puts("    -3        use 3rd editor (notepad)");
            puts("    -e<path>  path to editor 1. default devenv");
            puts("    -e2<path> path to editor 2. default editplus");
            puts("    -e3<path> path to editor 3. default notepad");
            puts("    -nologo   disable banner");
            puts("    -r        recurse subdirectories");
            puts("    -v0       no console output");
            puts("    -v1       prints source file name and errors");
            puts("    -v2       status line for each file (default)");
            puts("    -v3       additional info");
            puts("");

        default:
            break;
    }
#endif
}

char *SPIT_opt(const char* optptr)
{
    char *d=(char *)0;

    switch (*optptr|(char)' ')
    {
        case '1':
        case '2':
        case '3':
            OPTuseeditor=*optptr-'1'+1;
            d = (char *)optptr+1;
            break;

        case 'e':
            if (optptr[1]=='2')
                d = SPIT_getfield(OPTeditor2,optptr+2,1,0);
            else if (optptr[1]=='3')
                d = SPIT_getfield(OPTeditor3,optptr+2,1,0);
            else
                d = SPIT_getfield(OPTeditor,optptr+1,1,0);
            break;

        default:;
    }
    return(d);
}

int main(int argc, char* argv[])
{
    int more;
    int i;
    int hintuse2=0;
    static char toolline[1024];

    if (argc>=2)
        if (strstr(argv[1],"  "))
            hintuse2 = 1;

    /* Command line options. */
    SPIT_init(SPIT_reset, SPIT_usage, SPIT_opt, SPIT_history);
    do
    {
        more = SPIT_parse(argc, argv, TOOL_NAME, TOOL_VERSION);

        if (!SPIThelpgiven)
        {
            if (OPTuseeditor==2 || (!OPTuseeditor && hintuse2))
            {
                if (OPTverbose>=5)
                    puts("editor2");
                if (OPTeditor2[0])
                    strcpy(toolline,OPTeditor2);
            }
            else if (OPTuseeditor==3)
            {
                if (OPTverbose>=5)
                    puts("editor3");
                if (OPTeditor2[0])
                    strcpy(toolline,OPTeditor2);
            }
            else
            {
                if (OPTeditor[0])
                    strcpy(toolline,OPTeditor);
            }

            if (!toolline[0])
            {
                if (getenv("MSDEVDIR"))
                    strcpy(toolline,"%MSDEVDIR%\\ide\\devenv.exe");
                else
                    strcpy(toolline,"notepad.exe");
            }

            // fix up for grep output
            for (i=0; i<SPITargnum-1; i+=2)
            {
                if (!stricmp(SPITsargv[i],"file"))
                {
                    int l=strlen(SPITsargv[i+1]);
                    if (l>1 && SPITsargv[i+1][l-1]==':')
                    {
                        SPITsargv[i][0] = 0;        /* remove 'file' */
                        SPITsargv[i+1][l-1] = 0;    /* remove ':' */
                    }
                }
            }

            {
                char **spawnptrs;
                char *spawnbuf;
                int numargs=(SPITargnum+2); // 1 extra for tool and 1 extra for NULL
                spawnptrs = (char **) galloc(4*numargs);
                memset(spawnptrs,0,4*numargs);
                spawnbuf = (char *) galloc(4*numargs*SPITMAXLINE);
                memset(spawnbuf,0,4*numargs*SPITMAXLINE);

                int args=0;
                spawnptrs[args++] = "e";   // argv[0]

                for (i=0; i<SPITargnum; ++i)
                {
                    int isopt = 0;
                    if (SPITsargv[i][0]=='-' || SPITsargv[i][0]=='+' || (SPITsargv[i][0]==OPTdot && SPITsargv[i][1]!='.' && SPITsargv[i][1]!='/' && SPITsargv[i][1]!='\\'))
                    {
                        isopt = Zop(SPITsargv[i]+1)?1:0;
                    }
                    else if (SPITsargv[i][0]=='=')
                    {
                        isopt = Zop(SPITsargv[i])?1:0;
                    }

                    if (!isopt)
                    {
                        sprintf(spawnbuf+args*SPITMAXLINE,"\"%s\"",SPITsargv[i]);   // quote name for any spaces
                        spawnptrs[args] = spawnbuf+args*SPITMAXLINE;
                        ++args;
                    }
                }

                spawnptrs[args++] = NULL;

#if CONSOLE
                if (OPTverbose>=6)
                {
                    printf("args %d\n",args);
                    printf("toolline (%p) %s\n",toolline,toolline);
                    for (int j=0; j<numargs; ++j)
                    {
                        printf("spawnptrs[%d] %s\n",j,spawnptrs[j]);
                    }
                }
#endif
                if (OPTwrite)  /* -w0 to disable */
                    spawnvp(_P_NOWAIT, toolline /*spawnptrs[0]*/ ,spawnptrs);
                gfree(spawnptrs);
            }
        }
    } while (more>0);

    return(0);
}

#if defined(_MSC_VER) && !defined(DEBUG)
int WINAPI WinMain (HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, PSTR lpszCmdLine, int /*cmdShow*/)
{
    char *argv[2];
    argv[0] = "e";
    argv[1] = lpszCmdLine;
    return main(2,argv);
}
#endif

