
/********************************* executes the command from TCL  **************************/
/* this takes the arglist passed in by TCL and converts it to a
   VERTICAL command structure */
#include "cmdexec.h"
#include "cmdlog.h"
#include "cmdparse.h"
#include "cmdutil.h"
#include "expression.h"
#include "generic.h"

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <tcl.h>
/* used for dynamic string allocation/deallocation code
 * used here for command keywords May 2 2000 */
#include "lx_support.h"
#include "vertcl_main.h"

int ET_COMMAND_vertical (ET_TCLARGS)
{
        FILE *f;
        struct CommandStruct Cmd;
        int i, Status;
        char *p;

        Cmd.Text = NULL; /* place the command in the command structure */
        Cmd.NumberCommands = argc - 1;
        Cmd.CurrentCommand = 0;

        f = fopen ("Varg.txt", "w");

        for (i = 0; i < argc - 1 && i < WORDSINLINE; i++)
        {
                Cmd.Words[i] = strdup (argv[i + 1]);
                fprintf (f, "%02d : %s\n", i, Cmd.Words[i]);
        }
        if (Cmd.NumberCommands == 0 || Cmd.Words[0][0] == '-' || Cmd.Words[0][0] == '#')
                return (TCL_OK); /* comment or a null string */

        Status = TopMenu (&Cmd); /* pass the command through the top-level menu */
        switch (Status)
        {
        case UNKNOWNCMD:
                Status = TCL_ERROR;
                Log (LOG_ERROR, "# Unknown command:");
                break;
        case NARGS:
                Status = TCL_ERROR;
                Log (LOG_ERROR, "# Too few parameters:");
                break;
        case FAILED:
                Status = TCL_ERROR;
                Log (LOG_ERROR, "# Could not execute command:");
                break;
        default:
                Status = TCL_OK;
        };
        if (Status != TCL_OK)
        {
                /*     for (i=0; i < Cmd.NumberCommands; i++)   */ /* display offending command
                                                                    */
                /*       Et_ResultF(interp," %s", Cmd.Words[i]);*/
                Et_ResultF (interp, "vertical command %s\n", Cmd.Words[1]);
        };

        for (i = 0; i < argc - 1; i++)
                free (Cmd.Words[i]);

        return (Status);
}

/*
** Main routine for UNIX programs.  If the user has supplied
** their own main() routine in a C module, then the ET_HAVE_MAIN
** macro will be set to 1 and this code will be skipped.
*/
int main (int argc, char **argv)
{
        /* VERTICAL SPECIFIC INITIALISATION */
        InitErrorLog ();
        InitialiseData ();
        ExecuteString (
            "echo running \\$(VERTICAL_INIT) file name = $(VERTICAL_INIT) ", argc, argv);
        ExecuteString ("do $(VERTICAL_INIT) ", argc, argv);

#if ET_AUTO_FORK
        int rc = fork ();
        if (rc < 0)
        {
                perror ("can't fork");
                exit (1);
        }
        if (rc > 0)
                return 0;
        close (0);
        open ("/dev/null", O_RDONLY);
        close (1);
        open ("/dev/null", O_WRONLY);
#endif
        return Et_Init (argc, argv) != TCL_OK;
}
