/* Created by EasyMotif Revision: 5.28 Release:2_3_7_2  */

/*
 *    Project:    toolchest        Path:    /home/keidel/Brainstorm/toolchest/
 *    Program-name:    toolchest
 *    Designer:    J.Keidel        Version:    $Revision: 2.1 $
 *    Description:    Button box to start programs
*/

/***************************************************************************
 ***************************************************************************
 *
 *  Projectname:    toolchest
 *
 *  Programname:        toolchest
 *  
 *  resourcename:    Toolchest
 *
 *  Designer:        J.Keidel
 *
 *
 *  Documentation:    
 *
 *
 *  History:        Thu Nov 22 15:34:34 2012

 *
 *
 ***************************************************************************
 ***************************************************************************/
#include  <stdio.h>
/* well this is weird, stdio.h is included, which defines the (legacy) function
   cuserid(), but the compiler is saying the function declaration is implicit,
   so declare it here anyway, C. S. 18 October 2021
*/
extern char *cuserid (char *__s);

#include  <stdlib.h>
/*     Includes */

#include <Xm/XmAll.h>
#include "include/XMind.h"
#include <math.h>

/*   PRIVATE INCLUDES   */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
/* 
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public
    License ; if not, write to the Free
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
/*
   Author:   J.Keidel         wizant.keidel@freenet.de  
*/


/*   PRIVATE DEFINES   */
typedef struct d_f {
    char  key[8];
    char  *result;
    int   (*func)(char *argv , struct d_f *def) ;
    } COMMarg;

typedef struct tool {
    struct tool *next;
    struct tool *menu;
    short int   type;
    short int   exec;
    char        label[40];
    char        command[1024];
    char     set[40];        
    char        unset[40]; 
   }Tools;    

enum types{Label=0,Root,Separator,CheckExec,Exec,Menu,Toggle,Button};

#define NCommands  12


/*** PRIVATE HEADER DECLARATIONS ***/ 
int NMenus = 0;
Tools  **menus;
static char commands[NCommands][16] ={"f.label","Menu","no-label","f.menu"
                                     ,"f.separator","f.checkexec","f.exec"
                                     ,"{","}","f.toggle","sinclude",""};
static FILE *aux;
static char item[1024];
static int orient = XmVERTICAL;



/*** PRIVATE PROTOTYPES  ***/ 


/***  END   HEADER   ***/


/*     GLOBAL WIDGETS */

  XtAppContext app_context;

/*     GLOBAL Graphics */


/*    STATIC GLOBAL WIDGETS */

static Widget Toolchest;

/*    STATIC GLOBAL Graphics */


/*    Prototypes */

Widget Toolchest_Create (Widget parent );

/*    FUNCTIONS */

static int checkexec(char *command) ;
static void command_cb(Widget parent,  XtPointer data,  XmPushButtonCallbackStruct *cbs) ;
static void commline_dec(int argc , char *argv[],COMMarg *def , int nentries) ;
static void create_main(Widget parent , Tools *act) ;
static void create_menus(void) ;
static void create_pop(Widget parent , Tools *act) ;
static void create_pop_sub(Widget parent , Tools *act) ;
static void get_all_menus(void) ;
static int get_command(Tools *act) ;
static int get_item(void) ;
static int get_menu(Tools *menu) ;
static void link_menu(Tools *act) ;
static void main_popup( Tools *act) ;
static void sinclude(void) ;
static void tcommand_cb (Widget parent, XtPointer data , XmToggleButtonCallbackStruct * cbs ) ;


/*  FALLBACK RESOURCES */
 static String fallback_resources[] = { 
"*fontList: 7x13",
"*Highlight:  cyan",
"*help.background:  PaleGreen",
/***********************************************************
 *
 * Insert additional fallbacks
 *
 * Syntax:
 * "ResourceName*WidgetName.Resource:   Value",<NEWLINE>
 *
 * Example:
 * "MyProg*myButton.fontList:   6x10",
 *
 ***********************************************************/
"Toolchest*fontList: -*-helvetica-medium-r-*-*-10-*-*-*-*-*-*-*",
"Toolchest*pushButton.fontList:     -*-helvetica-bold-r-*-*-12-*-*-*-*-*-*-*",
"Toolchest*cascadeButtonp.fontList:     -*-helvetica-bold-r-*-*-12-*-*-*-*-*-*-*",
"Toolchest*sub.fontList:     -*-helvetica-bold-r-normal--*-*-*-*-*-*-*-*",
"Toolchest*sub.background: LightCyan",
"Toolchest*cascadeButtonp.background: LightCyan",
"Toolchest*chest.entryBorder: 0",
"Toolchest*chest.borderWidth: 0",
"Toolchest*chest.shadowThickness: 0",
"Toolchest*chest.spacing: 0",
"Toolchest*chest*Label_1.fontList: -*-helvetica-medium-r-*-*-10-*-*-*-*-*-*-*",
"Toolchest*Toggle.indicatorSize: 20",
"Toolchest*Toggle.indicatorType: XmONE_OF_MANY_DIAMOND",

  NULL 
};
/*************************************************************************** 
 ***************************************************************************
 *
 *  Functionname:    main
 *
 *  Input:            int argc; char *argv[]
 *
 *  Return:            int        
 *
 *
 ***************************************************************************
 ***************************************************************************/
int main(int argc,char **argv){
  Widget Toplevel, MainDialog;
  Arg args[40];
  Cardinal n=0;

/* toolchest INIT-Function*/
static char hor[2]="",ver[2]="";
COMMarg  commline [2]={ {"-hor",hor,0},
{"-ver",ver,0}
};

commline_dec(argc, argv, commline, 2);

  if(hor[0])
    orient = XmHORIZONTAL;
  if(ver[0])
    orient = XmVERTICAL;
  XtSetLanguageProc(NULL, NULL, NULL);

  /* INITIALIZE THE TOPLEVEL */

    XtSetArg(args[n], XmNtitle, "Toolchest"); n++;
    XtSetArg(args[n],XmNmwmDecorations ,0); n++;
    XtSetArg(args[n],XmNx , 0); n++;
    XtSetArg(args[n],XmNy , 80); n++;
  /* INITIALIZE THE TOPLEVEL */

    /* Toplevel = XtAppInitialize(&app_context, "Toolchest", NULL, 0, &argc, argv, fallback_resources, args, n); */
    Toplevel = XtOpenApplication(
        &app_context,
        "Toolchest",
        NULL,
        0,
        &argc,
        argv,
        NULL,
        applicationShellWidgetClass,
        args,
        n);

      /* CREATE THE MAINDIALOG */

    MainDialog = Toolchest_Create(Toplevel);
  /* MANAGE AND REALIZE THE SHELL WIDGETS */

    XtManageChild(MainDialog);
    XtRealizeWidget(Toplevel);

  /* START THE MESSAGE-QUEUE */
    XtAppMainLoop(app_context);
     Mterminate_thread( &app_context);
    return 0;
}

/*************************************************************************** 
 * 
 * Funktionsname:    Toolchest_Create
 * 
 * 
 * Input:        Widget parent
 * 
 * Return:        Widget 
 * 
 * Dokumentation:        
 * 
 * 
 ***************************************************************************/

Widget Toolchest_Create (Widget parent )
{
    if(parent == NULL) printf("Null widget! toolchest.c line 251\n");
    Arg args[40]; 
    Cardinal n;
    XmString tmpLabelString = NULL;
    Widget Toolchest_machine;

    /* Toolchest INIT-Function*/
    create_menus();

   /* Main Dialog */

    n = 0;
    XtSetArg(args[n], XmNwidth, 150); n++;
    XtSetArg(args[n], XmNheight, 703); n++;
    XtSetArg(args[n], XmNautoUnmanage, FALSE); n++;
    XtSetArg(args[n], XmNnoResize, FALSE); n++;
    XtSetArg(args[n], XmNnumColumns,1); n++;
    XtSetArg(args[n], XmNpacking, 1); n++;
    XtSetArg(args[n], XmNresizeHeight, FALSE ); n++;
    XtSetArg(args[n], XmNresizeWidth, FALSE); n++;
    XtSetArg(args[n], XmNadjustLast, FALSE); n++;
    XtSetArg(args[n], XmNorientation, XmVERTICAL); n++;
    Toolchest = XmCreateRowColumn(parent, "chest", args, n);
    n = 0;
#ifndef LESSTIFF
    XtSetArg(args[n], XmNleftAttachment, XmATTACH_FORM ); n++;
    XtSetArg(args[n], XmNrightAttachment, XmATTACH_FORM ); n++;
#endif
    XtSetArg(args[n], XmNx, 3); n++;
    XtSetArg(args[n], XmNy, 7); n++;
    XtSetArg(args[n], XmNrecomputeSize, FALSE ); n++;
    XtSetArg(args[n], XmNwidth, 144); n++;
    XtSetArg(args[n], XmNheight, 25); n++;
    tmpLabelString = XmStringCreateLtoR("Machinename", XmFONTLIST_DEFAULT_TAG);
    XtSetArg(args[n], XmNlabelString, tmpLabelString); n++;
    Toolchest_machine = XmCreateLabel(Toolchest, "Label_1", args, n);
    XmStringFree(tmpLabelString);
    XtManageChild(Toolchest_machine);

    /*  create graphics */

    /*  create additional attaches */

    /*  add all callbacks */

/* Toolchest FINIT-function*/

  n=0;
  XtSetArg(args[n], XmNresizeHeight, TRUE ); n++;
  XtSetArg(args[n], XmNresizeWidth, TRUE); n++;
  XtSetArg(args[n], XmNy, 0); n++;
  XtSetArg(args[n], XmNx, 0); n++;
  if(orient == XmHORIZONTAL)
    XtSetArg(args[n], XmNorientation, XmHORIZONTAL); 
  else
     XtSetArg(args[n], XmNorientation, XmVERTICAL); n++; 
 n++; 
  XtSetValues(Toolchest, args, n);
  create_main(Toolchest ,menus[0]);
{
   char machname[40];
   XtWidgetGeometry req,res;
    gethostname (machname, 20);  n=0;
    strcat (machname," : ");
    strcat(machname,(char *)cuserid(NULL));
      tmpLabelString =  XmStringCreateLtoR(machname, XmFONTLIST_DEFAULT_TAG);
      XtSetArg(args[n], XmNlabelString, tmpLabelString); n++;
/*       XtSetArg(args[n], XmNalignment, 1 ); n++; */

    XtSetValues(Toolchest_machine, args, n);
      XmStringFree(tmpLabelString);
      XtManageChild(Toolchest);

    req.request_mode = CWX | CWY;
    req.x = 1;
    req.y = 1;
    XtMakeGeometryRequest(Toolchest,&req,&res);

}
    XtManageChild(Toolchest);
    return Toolchest;
}

/*    FUNCTIONS */
static int checkexec(char *command) {
  char comm[1024] , str[1024];
    sscanf(command,"%s",str);
    strcpy(comm,"test -x ");
    strcat(comm,str);
    
    return(system(comm)) ? 0 : 1;

}
static void command_cb(Widget parent,  XtPointer data,  XmPushButtonCallbackStruct *cbs) {
   char *command = (char *)data;
   int i;
        i=system(command);

}

static void commline_dec(int argc , char *argv[],COMMarg *def , int nentries)
{
    /*
    C    DECODER ROUTINE FOR COMMANDLINE
    C    The given structure array COMMarg
    C    defines the keys and acvtions.
    C    COMMarg.key    character string ([4]) defining the key to react
    C            it should be a standard UNIX definition as -..
    C    COMMarg.result  is a character pointer (where a string may be
    C            stored)
    C    COMMarg.func    is an address of a function to be called
    C            if actual key detected.
    C            its an int function 
    C            arguments are the character string as char []
    C            containing the next item after key
    C            and the COMMarg entry (actual entry)
    C            The next ityem after key is used as function
    C            argument, NOT decoded as key
    C        A NULL pointer inhibits calling
    C
    C        if a function is given, it is called
    C        if no function given, the actual key
    C        is marked in *result
    */

    int i;
    char *arg;
    while(--argc  )  /* loop all items on command line */
    {
        arg = *++argv;
        for (i = 0; i < nentries; i++) /* search through all structure entries */
        {
            if(!strcmp(def[i].key, arg))
            {            /* key found */
                if(def[i].func) 
                {
                    (*def[i].func)(*++argv, &def[i]);
                    --argc;    /* function called, goto next key */
                }
                else *(def[i].result) = i+1 ; /* no function set .result */
                break;
            }
        }
    }
}

static void create_main(Widget parent , Tools *act) {
  Arg args[40];
  Cardinal n;
  XmString tmpLabelString = NULL;
  char lab[40] , res[40];
  Widget separator;
  Widget button;

    act=act->next;     /* skip root menue */

    while(act){

        if(act->type == Separator){
                  n=0;
                    XtSetArg(args[n], XmNhighlightThickness, 0); n++;
                    XtSetArg(args[n], XmNheight, 3); n++;
                    XtSetArg(args[n], XmNmargin, 0); n++;          
                XtSetArg(args[n], XmNseparatorType, XmSHADOW_ETCHED_OUT); n++;
                    separator = XmCreateSeparator(parent, "separator", args, n);
                     XtManageChild(separator);
           }else if(act->type != Menu ){
                  n=0;
                    XtSetArg(args[n], XmNheight, 20); n++;
              tmpLabelString = XmStringCreateLtoR(act->label, XmFONTLIST_DEFAULT_TAG);
                      if(act->type == CheckExec && !act->exec){
                  XtSetArg(args[n], XmNsensitive, FALSE); n++;
                  }else {
                  XtSetArg(args[n], XmNsensitive, TRUE); n++;
                      }
                        
                    XtSetArg(args[n], XmNlabelString, tmpLabelString); n++;
                        if(act->type == Toggle){
                XtSetArg(args[n],XmNshadowThickness ,2);n++;
                XtSetArg(args[n],XmNindicatorSize ,20);n++;
                XtSetArg(args[n],XmNindicatorType ,XmONE_OF_MANY_DIAMOND);n++;
                                
                            button = XmCreateToggleButton(parent, "Toggle", args, n); 
                      XtManageChild(button);
                XtAddCallback(button, XmNvalueChangedCallback, (XtCallbackProc) tcommand_cb, (XtPointer)act);        
                        }else{        
                        button = XmCreatePushButton(parent,"pushButton" , args, n);
                      XtManageChild(button);
                      if((act->type == CheckExec && act->exec) || act->type == Exec)
                    XtAddCallback(button, XmNactivateCallback, (XtCallbackProc) command_cb, (XtPointer)act->command);        
                        }        
                    XmStringFree(tmpLabelString);
                  }else if(act->type == Menu){
            main_popup( act);
           }
        act = act->next;
    }

}
static void create_menus(void) {
  int i=0;
  char *home , newfile[255];
    
    menus = (Tools **)calloc(150,sizeof(Tools *));
    menus[0] = (Tools *)calloc(1,sizeof(Tools));
    /* aux = fopen("/server/softy/Config/system.chestrc","r"); */
     aux = fopen("/usr/local/Config/system.chestrc","r");   
    if(!aux){
    home = getenv("HOME");
    strcpy(newfile,home);
    strcat(newfile,"/.auxchestrc");
    aux = fopen(newfile,"r");
    if(!aux)
        exit (0);
    }

    get_all_menus();
    for (i=NMenus;i>=0;--i)
    link_menu(menus[i]);
      
    fclose(aux);    
}
static void create_pop(Widget parent , Tools *act) {
  Arg args[40];
  Cardinal n;
  XmString tmpLabelString = NULL;
  Widget  field;
  Widget separator;
  Widget button;
       
    act = act->next;     /* root of menu is not interesting */
    while(act){
      
        if(act->type == Separator){
            n = 0;
            XtSetArg(args[n], XmNhighlightThickness, 0); n++;
            XtSetArg(args[n], XmNheight, 4); n++;
            XtSetArg(args[n], XmNmargin, 0); n++;
            
        XtSetArg(args[n], XmNseparatorType, XmSHADOW_ETCHED_OUT); n++;
            field = XmCreateSeparator(parent, "separatorp", args, n);
               XtManageChild(field);
        }else if(act->type != Menu ){
            n=0;
              XtSetArg(args[n], XmNheight, 20); n++;
              tmpLabelString = XmStringCreateLtoR(act->label, XmFONTLIST_DEFAULT_TAG);
            if(act->type == CheckExec && !act->exec){
                XtSetArg(args[n], XmNsensitive, FALSE); n++;
              }else{
                  XtSetArg(args[n], XmNsensitive, TRUE); n++;
            }
            XtSetArg(args[n], XmNlabelString, tmpLabelString); n++;
            if(act->type == Toggle){
        XtSetArg(args[n],XmNshadowThickness ,2);n++;
        XtSetArg(args[n],XmNindicatorSize ,20);n++;
        XtSetArg(args[n],XmNindicatorType ,XmONE_OF_MANY_DIAMOND);n++;
                button = XmCreateToggleButton(parent, "Toggle", args, n); 
            XtManageChild(button);
                XtAddCallback(button, XmNvalueChangedCallback, (XtCallbackProc) tcommand_cb, (XtPointer)act);        
            }else{        
                button = XmCreatePushButton(parent, "pushButton", args, n);
                XtManageChild(button);
                if((act->type == CheckExec && act->exec) || act->type == Exec)
                    XtAddCallback(button, XmNactivateCallback, (XtCallbackProc) command_cb, (XtPointer)act->command);        
                }        
              XmStringFree(tmpLabelString);
                        
        }else if(act->type == Menu){  
              tmpLabelString = XmStringCreateLtoR(act->label, XmFONTLIST_DEFAULT_TAG);
            n=0;
            XtSetArg(args[n], XmNsensitive, TRUE); n++;
            XtSetArg(args[n], XmNlabelString, tmpLabelString); n++;
    /* XtSetArg(args[n],XmNbackground ,MGetPixel("green",parent));n++; */
            button = XmCreateCascadeButton(parent, "cascadeButtonp", args, n);
            XtManageChild(button);
            XmStringFree(tmpLabelString);
            XtManageChild(button);
            create_pop_sub(button , act->menu);
                
    }
        act = act->next;
    }

}
static void create_pop_sub(Widget parent , Tools *act) {
  Arg args[40];
  Cardinal n;
  XmString tmpLabelString = NULL;
  Widget  field;
  Widget  SubId;
  
    act = act->next;     /* root of menu is not interesting */
  
    SubId = XmCreatePulldownMenu(parent, "cascadeButtonp", NULL, 0);
    while(act){
      
        if(act->type == Separator){
            n = 0;
            XtSetArg(args[n], XmNhighlightThickness, 0); n++;
            XtSetArg(args[n], XmNheight, 3); n++;
            XtSetArg(args[n], XmNmargin, 0); n++;
        XtSetArg(args[n], XmNseparatorType, XmSHADOW_ETCHED_OUT); n++;
            field = XmCreateSeparator(SubId, "separatorp", args, n);
        }else if(act->type != Menu ){
            n = 0;
            tmpLabelString =  XmStringCreate(act->label, XmFONTLIST_DEFAULT_TAG);
            if(act->type == CheckExec && !act->exec){
                XtSetArg(args[n], XmNsensitive, FALSE); n++;
            }else{
                XtSetArg(args[n], XmNsensitive, TRUE); n++;
            }    
            XtSetArg(args[n], XmNlabelString, tmpLabelString); n++;
            if(act->type == Toggle){
        XtSetArg(args[n],XmNshadowThickness ,2);n++;
        XtSetArg(args[n],XmNindicatorSize ,20);n++;
        XtSetArg(args[n],XmNindicatorType ,XmONE_OF_MANY_DIAMOND);n++;
                field = XmCreateToggleButton(SubId, "Toggle", args, n); 
              XtManageChild(field);
        XtAddCallback(field, XmNvalueChangedCallback, (XtCallbackProc) tcommand_cb, (XtPointer)act);        
            }else{        
                    field = XmCreatePushButton(SubId,"pushButton" , args, n);
                  XtManageChild(field);
                  if((act->type == CheckExec && act->exec) || act->type == Exec)
                XtAddCallback(field, XmNactivateCallback, (XtCallbackProc) command_cb, (XtPointer)act->command);        
            }        
            
                XmStringFree(tmpLabelString);
  
        }else if(act->type == Menu){
               tmpLabelString = XmStringCreateLtoR(act->label, XmFONTLIST_DEFAULT_TAG);
            n=0;
            XtSetArg(args[n], XmNsensitive, TRUE); n++;
               XtSetArg(args[n], XmNlabelString, tmpLabelString); n++;
    /* XtSetArg(args[n],XmNbackground ,MGetPixel("green",parent));n++; */
            field = XmCreateCascadeButton(SubId, "cascadeButtonp", args, n);
            XmStringFree(tmpLabelString);
            XtManageChild(field);
              create_pop_sub(field , act->menu);
        }    
    act = act->next;
    }
    XtVaSetValues (parent, XmNsubMenuId, SubId, NULL);
}
static void get_all_menus(void) {
  int  mflag = 0;
    while((mflag = get_menu(menus[NMenus])) != -1){
    if(mflag == 1)
        continue;      /* add on menu part */
    if(!menus[NMenus]->next && !menus[NMenus]->command[0])
        break;
         NMenus++;
    
    menus[NMenus] = (Tools *)calloc(1,sizeof(Tools));
    }

}
static int get_command(Tools *act) {
  int i , nextflag =1;
  char *s;
  
    while(!(i=get_item())  && item[0] != '}'){
    if(nextflag){
        act ->next = (Tools *)calloc(1,sizeof(Tools));
        act = act->next;
        }
    if(!strcasecmp(item,commands[2]))
        strcpy(act->label,"    ");
    else
        strcpy(act->label,item);
    get_item();             /* get second part of command */    
    
    for(i=3;i<NCommands-3;i++){
        if(!strcasecmp(commands[i],item))
        break;
    }
    if(i >= NCommands-3){
        nextflag=0;
        s=fgets(item,200,aux);   /* skip remaining line */
        continue;
    }
    switch(i){
        case 3:     /* f.Menu */
        act->type = Menu;
            get_item();
        strcpy(act->command,item);
        break;
        case 4:    /* separator */
        act->type = Separator;
        continue;
        case 5:    /* f.checkexec */
        get_item();
        act->type = CheckExec;
        act->exec = checkexec(item);
        strcpy(act->command,item);
        if(!strchr(act->command,'&'))
            strcat(act->command," &");
        break;
        case 6:  /* f.exec */
        get_item();
        act->type = Exec;
        act->exec = 1;
        strcpy(act->command,item);
        if(!strchr(act->command,'&'))
            strcat(act->command," &");
        break;
            case 9:   /* f.toggle */
                act->type = Toggle;
                get_item();
                strcpy(act->command,item);
                 get_item();
                strncpy (act->set,item,40);
                get_item();
                strncpy (act->unset,item,40);
            break;    
    }
    }
    return (i <0) ? i : 0;    

}
static int get_item(void) {
  int start = 0 , con=0;
  char st[200] ;
  int i;
  char *s;
  
    do{  
    con=0;
         i=fscanf(aux,"%s",st);
    if(feof(aux))
        return -1;
    if(st[0] == '#'){
        s=fgets(st,200,aux);
        con=1;
        continue;
    }
        if(st[0] == '"' && !start){
            start=1;
            if(st[strlen(st)-1] == '"'){
                st[strlen(st)-1] =0;
            start =0;
            }
            strcpy(item,&st[1]);
            if(!start)
            return 0;
            strcat(item," ");
        }
        else if(start){
            if(st[strlen(st)-1] == '"'){
                st[strlen(st)-1] =0;
                start =0;
            }
            strcat(item,st);
        if(start)
              strcat(item," ");
        }
        else
            strcpy(item,st);
    }while(start || con);
    return 0;

}
static int get_menu(Tools *menu) {
 int i , flag = 0;
    while(1) {
    if(get_item() < 0)
        return -1;
    if(!strcasecmp(item,commands[1]))
        break;
    if(!strcasecmp(item,commands[10])){   /* sinclude  */        
        sinclude();
        continue;
    }

    }
    get_item();
    for (i=0 ; i<NMenus ; i++){
        if(!strcasecmp(item,menus[i]->label)){
            menu = menus[i];
            while(menu->next)
                menu=menu->next;
            flag = 1;
            break;
        }
    }
     if(!flag){
        strcpy(menu->label ,item);
        menu->type = Root;
    }

    get_item();             /* this must be open parenth */
    get_command(menu);
    
    return flag;

}
static void link_menu(Tools *act) {
  int i;  
    while(act){
    if(act->type == Menu){
        for (i=1 ; i<NMenus ; i++){
        if(!strcasecmp(act->command,menus[i]->label)){
            act->menu = menus[i];
            break;
        }
        }
    }
    act=act->next;
    }

}
static void main_popup( Tools *act) {
  Arg args[40];
  Cardinal n;
  XmString tmpLabelString = NULL;
  Widget Menubar;
  Widget OptionPulldown;
  Widget button;
  
  
    n=0;
  
    OptionPulldown = XmCreatePulldownMenu(Toolchest, "Menu", NULL, 0);

    n=0;
    XtSetArg(args[n], XmNx, 8); n++;
    XtSetArg(args[n], XmNy, 0); n++;
    Menubar = XmCreateMenuBar(Toolchest, "Menu", args, n);
    XtManageChild(Menubar);

    n=0;
    tmpLabelString = XmStringCreateLtoR(act->label, XmFONTLIST_DEFAULT_TAG);
        
      XtSetArg(args[n], XmNwidth, 109); n++;
      XtSetArg(args[n], XmNheight, 20); n++;
    XtSetArg (args[n], XmNsubMenuId,OptionPulldown); n++;
    tmpLabelString =  XmStringCreate(act->label, XmFONTLIST_DEFAULT_TAG);
    XtSetArg(args[n], XmNlabelString, tmpLabelString); n++;
      button = XmCreateCascadeButton(Menubar, "sub", args, n);
    XtManageChild(button);
    /* CREATE SUBWIDGETS */
         create_pop(OptionPulldown , act->menu);
       
}
static void sinclude(void) {
    FILE *saveit;
  char *home , newfile[120];
    
        saveit = aux;
    get_item();
    if(item[0]=='~'){
        home = getenv("HOME");
        strcpy(newfile,home);
        strcat(newfile,&item[1]);
        aux = fopen(newfile,"r");
    }
    else
        aux = fopen(item,"r");
    if(!aux){
        aux = saveit;
        return;
    }
         NMenus++;
    
    menus[NMenus] = (Tools *)calloc(1,sizeof(Tools));
    get_all_menus();
    fclose(aux);
    aux = saveit;
    return;

}
static void tcommand_cb (Widget parent, XtPointer data , XmToggleButtonCallbackStruct * cbs ) {
  Tools *tdata = (Tools *)data;
  char command[200] ;
  int i;
  
      strcpy(command,tdata->command);
  
      if(cbs->set)
            strcat(command,tdata->set);
        else
            strcat(command,tdata->unset);
        strcat (command," &");
        i=system(command);                
}
