/*---------------------------------------------------------------------------

  Implementation file for the SICS-interpreter. 
	


  Mark Koennecke,    November 1996

  Copyright:

  Labor fuer Neutronenstreuung
  Paul Scherrer Institut
  CH-5423 Villigen-PSI


  The authors hereby grant permission to use, copy, modify, distribute,
  and license this software and its documentation for any purpose, provided
  that existing copyright notices are retained in all copies and that this
  notice is included verbatim in any distributions. No written agreement,
  license, or royalty fee is required for any of the authorized uses.
  Modifications to this software may be copyrighted by their authors
  and need not follow the licensing terms described here, provided that
  the new terms are clearly indicated on the first page of each file where
  they apply.

  IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
  DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
  POSSIBILITY OF SUCH DAMAGE.

  THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.  THIS SOFTWARE
  IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
  NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
  MODIFICATIONS.
---------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include <tcl.h>
#include "fortify.h"
#include "conman.h"
#include "splitter.h"
#include "Scommon.h"
#include "SCinter.h"
#include "obdes.h"
#include "devexec.h"
#include "servlog.h"
#include "macro.h"

#define MAXLEN 256
#define MAXPAR 100

/*--------------------------------------------------------------------------*/
  SicsInterp *InitInterp(void)
  {
     SicsInterp *pInter = NULL;
     int i;
     
     pInter = (SicsInterp *)malloc(sizeof(SicsInterp));
     if(!pInter)
     {
     	SICSLogWrite("Error allocating memory for Interpreter",eInternal);
     	return NULL;
     }
     pInter->pCList = NULL;
     pInter->pTcl = (void *)MacroInit(pInter);
     if(!pInter->pTcl)
     {
       free(pInter);
       return NULL;
     }

     pInter->iDeleting = 0;
     return pInter;
  }
/*------------------------------------------------------------------------*/
  int AddCommand(SicsInterp *pInterp, char *pName, ObjectFunc pFunc,
                KillFunc pKFunc, void *pData)
  {
    CommandList *pNew = NULL;
    char pBueffel[512];

    assert(pName);
    assert(pFunc);
    assert(pInterp);
    
    strcpy(pBueffel,pName);
    strtolower(pBueffel);
    if(FindCommand(pInterp,pBueffel) != NULL)
    {
      return 0;
    }
    
    /* new memory */
    pNew = (CommandList *)malloc(sizeof(CommandList));
    if(!pNew)
    {
        sprintf(pBueffel,
               "Out of memory creating command - %s -", pName);
    	SICSLogWrite(pBueffel,eInternal);
    	return 0;
    }

   /* if no data given, initialise with Dummy struct */
   if(!pData)
   {
     pData = (void *)CreateDummy(pBueffel);
     if(!pKFunc)
     {
        pKFunc = KillDummy;
     }
   }
    
    /* initialise datastructures */
    pNew->pName = strdup(pBueffel);
    pNew->OFunc = pFunc;
    pNew->KFunc = pKFunc;
    pNew->pData = pData;
    pNew->pNext = pInterp->pCList;


    if(pInterp->pCList)
    {
      pInterp->pCList->pPrevious = pNew;
    }
    pNew->pPrevious = NULL;
    
    /* update headpointer */
    pInterp->pCList = pNew;
    return 1;
  }
/*------------------------------------------------------------------------*/
 int RemoveCommand(SicsInterp *pInterp, char *pName)
 {
     CommandList *pVictim = NULL;
     char pBueffel[256];     

     assert(pInterp);
     assert(pName);
     
     strcpy(pBueffel,pName);
     strtolower(pBueffel);
     
     if(pInterp->iDeleting)
     {
       return 0;
     }
     
    /* find our victim */
     pVictim = FindCommand(pInterp, pBueffel);
     if(!pVictim)
        return 0;
     
     /* found, remove it */
     /* kall KillFunction first */
     if(pVictim->KFunc)
     {
       pVictim->KFunc(pVictim->pData);
     }
     
     /* delete and unlink data */
     if(pVictim->pName)
     {
        free(pVictim->pName);
     }
     if(pVictim->pPrevious)
     {
       pVictim->pPrevious->pNext = pVictim->pNext;
     }   
     if(pVictim->pNext)
     {
      pVictim->pNext->pPrevious = pVictim->pPrevious;
     }
     /* adjust headpointer if necessary */
     if(pVictim == pInterp->pCList)
     {
       pInterp->pCList = pVictim->pNext;
     }
     free(pVictim);
     return 1;
 }

#define MAXLEN 256
#define MAXCOM 50
extern char *stptok(char  *s, char *tok, unsigned int toklen, char *brk);
extern char *SkipSpace(char *pPtr);
/*------------------------------------------------------------------------*/
  int InterpExecute(SicsInterp *self,SConnection *pCon, char *pText)
  {
    int iCount = 0;
    int iRet;
    int i, argc;
    char pBueffel[1024]; 
    CommandList *pCommand = NULL;
    char pBrk[] = {" \r\n\0"};
    char *pPtr;
    char **argv = NULL;
   
    
    assert(self);
    assert(pCon);

   /* write info to Log */
   if(pCon->pSock)
   {
      sprintf(pBueffel,"Executing -> %s <- from socket %d",pText, 
              pCon->pSock->sockid);
      SICSLogWrite(pBueffel,eCommand);
   }
   else
   {
      if(!pCon->iDummy)
      {
        printf("Executing -> %s <- from dummy socket\n", pText);
      }
      else
      {
         sprintf(pBueffel,"Executing -> %s <- from sicscron",pText);
         SICSLogWrite(pBueffel,eCommand);
      }
   }

   /* convert to argc, argv */
   argc = 0;
   Text2Arg(pText,&argc,&argv);   
 
    /* the first one must be the target object. If not given an empty
    command string was given which will be silently ignored  */
    if(argc < 1)
    {
      return 1;
    }
    if(argv[0] == NULL)
    {
       SCWrite(pCon,"ERROR: failed to parse command",eError);
       return -1;
    }
    
    /* find it */
    pCommand = FindCommand(self,argv[0]);
    if(!pCommand)
    {
      sprintf(pBueffel,"ERROR: Object  -> %s <- NOT found",
             argv[0]);
      SCWrite(pCon,pBueffel,eError);
      return -1;

    }

    
    /* invoke the command */
    self->eOut = eStatus;
    Tcl_ResetResult((Tcl_Interp *)self->pTcl);
    MacroPush(pCon);
    iRet = pCommand->OFunc(pCon, self, pCommand->pData, argc, argv);
    MacroPop();

    /* delete argv */
    for(i = 0; i < argc; i++)
    {
       if(argv[i] != NULL)
       {
         free(argv[i]);
       }
    }
    free(argv);

    return iRet;
  }
/*------------------------------------------------------------------------*/
  CommandList *FindCommand(SicsInterp *self, char *pName)
  {
    CommandList *pCurrent = NULL;
    char pBueffel[256];    

    assert(self);
    
    if(self->iDeleting)
    {
      return NULL;
    }
    
    strcpy(pBueffel,pName);
    strtolower(pBueffel);
    pCurrent = self->pCList;
    while(pCurrent)
    {
      if(pCurrent->pName != NULL)
      {
        if(strcmp(pCurrent->pName, pBueffel) == 0 )
        {
          return pCurrent;
        }
      }  
      pCurrent = pCurrent->pNext;
    }
    return NULL;
  }  
/*------------------------------------------------------------------------*/
  int WriteSicsStatus(SicsInterp *self, char *file)
  {
    CommandList *pCurrent = NULL;
    FILE *fd = NULL;
    Dummy *pDum = NULL;
    
    assert(self);
    assert(file);
    
    /* open file */
    fd = fopen(file,"w");
    if(!fd)
    {
      return 0;
    }
    /* remove it, as I found garbage from previous runs in the
       status file 
    */   
    fclose(fd);
    remove(file);
    
    fd = fopen(file,"w");
    if(!fd)
    {
      return 0;
    }
    
    /* cycle through the list */
    pCurrent = self->pCList;
    while(pCurrent)
    {
      pDum = (Dummy *)pCurrent->pData;
      if(pDum)
      {
        pDum->pDescriptor->SaveStatus(pCurrent->pData,pCurrent->pName,fd);
      }
      pCurrent = pCurrent->pNext;
    }
    fclose(fd);
    return 1;
  }  
/*------------------------------------------------------------------------*/
  void DeleteInterp(SicsInterp *self)
  {
     CommandList *pCurrent = NULL;
     CommandList *pTemp;
     Tcl_Interp *pTcl = NULL;
     int i;
     
     assert(self);
     self->iDeleting = 1;
     
     /* delete Commandlist */
     pCurrent = self->pCList;
     while(pCurrent)
     {
       if(pCurrent->KFunc)
       {
         pCurrent->KFunc(pCurrent->pData);
       }
       if(pCurrent->pName)
       {
        free(pCurrent->pName);
       }
       pTemp = pCurrent->pNext;
       free(pCurrent);
       pCurrent = pTemp;
     }

     /* clear Tcl_Interpreter. Must be AFTER deleting command list because
        some devices may have Tcl drivers which need to be accessed for
        proper closing of devices. 
     */
     pTcl = (Tcl_Interp *)self->pTcl;
     if(pTcl)
     {
       Tcl_DeleteInterp(pTcl);
     }
     
     free(self);
  }	

/*--------------------------------------------------------------------------*/
  int ListObjects(SConnection *pCon, SicsInterp *pSics, void *pData,
                  int argc, char *argv[])
  {
    CommandList *pCurrent;
    char pBueffel[256];
    int iNum = 0;
    
    assert(pSics);
    assert(pCon);
    
    pCurrent = pSics->pCList;
    while(pCurrent)
    {
      if(iNum == 0)
      {
        strcpy(pBueffel,pCurrent->pName);
        iNum++;
      }
      else if(iNum < 4)
      {
          strcat(pBueffel,"   ");
          strcat(pBueffel,pCurrent->pName);
          iNum++;
      }
      else
      {
          strcat(pBueffel,"   ");
          strcat(pBueffel,pCurrent->pName);
          strcat(pBueffel,"\r\n");
          SCWrite(pCon,pBueffel,eStatus);                  
          iNum = 0;
      }
      pCurrent = pCurrent->pNext;
    }
    
    /* write final entries */
    if(strlen(pBueffel) > 2)
    {
        strcat(pBueffel,"\r\n");
        SCWrite(pCon,pBueffel,eStatus);                  
    }
    return 1;
  }
/*---------------------------------------------------------------------------*/
  int InterpWrite(SicsInterp *pSics, char *buffer)
  {
    Tcl_Interp *pTcl = NULL;
    
    assert(pSics);
    pTcl = (Tcl_Interp *)pSics->pTcl;
    Tcl_SetResult(pTcl,buffer,TCL_VOLATILE);
    return 1;
  }
/*---------------------------------------------------------------------------*/
  Tcl_Interp *InterpGetTcl(SicsInterp *pSics)
  {
     Tcl_Interp *pTcl = NULL;
     
     pTcl = (Tcl_Interp *)pSics->pTcl;
     return pTcl;
  }
/*---------------------------------------------------------------------------*/
  void strtolower(char *pText)
  {
    assert(pText);
    
    while(*pText != '\0')
    {
      *pText = tolower(*pText);
      pText++;
    }
  }
/*---------------------------------------------------------------------------*/
  void argtolower(int argc, char *argv[])
  {
    int i;
    
    for(i = 0; i < argc; i++)
    {
      strtolower(argv[i]);
    }
  }
  
/*------------------------------------------------------------------------*/
  char  *FindAlias(SicsInterp *self, void *pData)
  {
    CommandList *pCurrent = NULL;

    assert(self);
    
    if(self->iDeleting)
    {
      return NULL;
    }
    
    pCurrent = self->pCList;
    while(pCurrent)
    {
      if(pCurrent->pData == pData)
      {
        return pCurrent->pName;
      }
      pCurrent = pCurrent->pNext;
    }
    return NULL;
  }  
/*---------------------------------------------------------------------------*/
  void *FindCommandData(SicsInterp *pSics, char *name, char *cclass)
  {
    CommandList *pCom;
    pDummy pDum = NULL;
    
    pCom = FindCommand(pSics,name);
    if(!pCom)
    {
       return NULL;
    }
    if(!pCom->pData)
       return NULL;
       
    pDum = (pDummy)pCom->pData;
    if(strcmp(pDum->pDescriptor->name,cclass) == 0)
    {
      return pCom->pData;
    }   
    return NULL;
  }
