
/********************************* executes the command from TCL  **************************/
/*
 * $Id: vertcl_main_user.c,v 1.1.1.1 2003/11/04 23:34:56 mjames Exp $
 *
 * $Log: vertcl_main_user.c,v $
 * Revision 1.1.1.1  2003/11/04 23:34:56  mjames
 * Imported into local repositrory
 *
 * Revision 1.6  2001/10/31 22:20:19  mjames
 * Tidying up problematical comments caused by CVS
 * 'intelligent' comment guessing
 *
 */
/* 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 "vertcl_main.h"

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
/* used for dynamic string allocation/deallocation code
 * used here for command keywords May 2 2000 */
#include "lx_support.h"

#define DEB_MENU

#if defined DEB_MENU
FILE *f;
#endif

/* fills in a single menu item */
void create_tcl_menu (
    ET_TCLARGS,
    char *menuname, /* vertical.m.xxxx */
    char *vertcmd,  /* vertical xxxx */
    struct command *menu)
{
        char *thismenu, *verticalcmd;
        int i;

        /* create a menu */
        Et_EvalF (interp, "menu %q", menuname);
#if defined DEB_MENU
        if (f)
                fprintf (f, "menu %s\n", menuname);
#endif
        for (i = 0; menu[i].name; i++)
        {
                if (menu[i].Menu)
                {
                        thismenu = Tcl_Alloc (strlen (menuname) + strlen (menu[i].name) + 10);
                        verticalcmd =
                            Tcl_Alloc (strlen (vertcmd) + strlen (menu[i].name) + 10);
                        sprintf (thismenu, "%s.%s", menuname, menu[i].name);
                        sprintf (verticalcmd, "%s %s", vertcmd, menu[i].name);

                        Et_EvalF (
                            interp,
                            "%q add cascade -label \"%q\" -menu \"%q\"",
                            menuname,
                            menu[i].name,
                            thismenu);
#if defined DEB_MENU
                        if (f)
                                fprintf (
                                    f,
                                    "%s add cascade -label \"%s\" -menu \"%s\"\n",
                                    menuname,
                                    menu[i].name,
                                    thismenu);
#endif

                        create_tcl_menu (
                            clientData,
                            interp,
                            argc,
                            argv,
                            thismenu,
                            verticalcmd,
                            menu[i].Menu);
                }
                else
                {
                        verticalcmd =
                            Tcl_Alloc (strlen (vertcmd) + strlen (menu[i].name) + 10);
                        sprintf (verticalcmd, "%s %s", vertcmd, menu[i].name);

                        Et_EvalF (
                            interp,
                            "%q add command -label \"%q\" -command \"%q\" ",
                            menuname,
                            menu[i].name,
                            verticalcmd);
#if defined DEB_MENU
                        if (f)
                                fprintf (
                                    f,
                                    "%s add command -label \"%s\" -command \"%s\"\n",
                                    menuname,
                                    menu[i].name,
                                    verticalcmd);
#endif
                }
        }
}
#define MENUNAME ".mb.vertical"
#define MENU_TOP MENUNAME ".m"

/* creates the top level menu */
int ET_COMMAND_wossat (ET_TCLARGS)
{
        Tcl_interp = interp;

        Et_EvalF (interp, "menubutton " MENUNAME " -text Vertical -menu " MENU_TOP);
#if defined DEB_MENU
        f = fopen ("aamenu.txt", "w");
        if (f)
                fprintf (f, "menubutton " MENUNAME " -text Vertical -menu " MENU_TOP "\n");
#endif
        create_tcl_menu (clientData, interp, argc, argv, MENU_TOP, "vertical", TopMenu);
        Et_EvalF (interp, "pack " MENUNAME " -side left -padx 5");
#if defined DEB_MENU
        if (f)
        {
                fprintf (f, "pack " MENUNAME " -side left -padx 5\n");
                fclose (f);
        }
#endif

        return TCL_OK;
}

/********************************* executes the command from TCL  **************************/
/* this takes the arglist passed in by TCL and converts it to a
   VERTICAL command structure */

int ET_COMMAND_vertical (ET_TCLARGS)
{
        FILE *f;
        int i, l, Status;
        char *p;
        char *cmdbuff;

        Tcl_interp = interp;
        if (argc)
        {
                argc--;
                argv++;
        }
        l = 0;
        for (i = 0; i < argc - 1; i++)
        {
                l += strlen (argv[i + 1]) + 1;
        }

        cmdbuff = Tcl_Alloc (l + strlen (argv[0]) + 2);
        cmdbuff[0] = 0;
        for (i = 0; i < argc; i++)
        {
                strcat (cmdbuff, argv[i]);
                strcat (cmdbuff, " ");
        }
        /* whatever command is given, put it in the console scroll
           buffer , but not yet in the history */
        Log_Command ("#    %s\n", cmdbuff);

        Status = Execute (argc, argv, TopMenu);

        switch (Status)
        {
        case TCL_ERROR:
                Log (LOG_ERROR, "# Unknown command:");
                break;
        default:
                Status = TCL_OK;
        };
        if (Status != TCL_OK)
        {
                Et_ResultF (interp, "Problem with 'vertical %s'\n", cmdbuff);
        };

        Tcl_Free (cmdbuff);

        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 */

        /* enter GUI mode for application
         * Later this will be based on a command line switch */
        Tcl_Mode = Tcl_GUI;

        atexit (EndErrorLog);

        InitErrorLog ();
        InitialiseData ();
        /* called inside vertical */
        //  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;
}
