/*-------------------------------------------------------------------------
				S C R I P T

       A few utility commands which permit more control about Sics from
       within the macro language.
       
       Mark Koennecke, February 1997
       
       added sicsprompt, Mark Koennecke, November 1999

       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 <string.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <tcl.h>
#include "fortify.h"
#include "sics.h"
#include "fupa.h"
#include "motor.h"
#include "countdriv.h"
#include "counter.h"
#include "interrupt.h"
#include "splitter.h"
#include "status.h"
#include "script.h"
#include "devexec.h"
		
 extern Tcl_Interp *InterpGetTcl(SicsInterp *pSics);
 extern void SNXFormatTime(char *pBueffel, int iLen); /* in nxdata.c */
 
/* -------------------------- Interrupts -----------------------------------*/
  int GetSICSInterrupt(SConnection *pCon, SicsInterp *pSics, void *pData,
                   int argc, char *argv[])
  {
     char pBueffel[132];
     SConnection *pOwner = NULL;
     pExeList pDev = NULL;
     
     assert(pCon);
     assert(pSics);

     /* we are interested in the interrupt pending in the executor. If
        not specified, use our very own
     */            
     pDev = GetExecutor();
     assert(pDev);
     pOwner = GetExeOwner(pDev);
     if(pOwner)
     {     
       Interrupt2Text(SCGetInterrupt(pOwner),pBueffel,131);
       SCWrite(pCon,pBueffel,eStatus);
     }
     else
     {
       Interrupt2Text(SCGetInterrupt(pCon),pBueffel,131);
       SCWrite(pCon,pBueffel,eStatus);
     }    
     return 1;
  }                 
/*-------------------------------------------------------------------------*/                   
  int SetSICSInterrupt(SConnection *pCon, SicsInterp *pSics, void *pData,
                   int argc, char *argv[])
  {
     int iInt;
     char pBueffel[132];
     SConnection *pOwner = NULL;
     pExeList pDev = NULL;
     
     assert(pCon);
     assert(pSics);
     
     /* minimum  user privelege */
     if(!SCMatchRights(pCon,usUser))
     {
       SCWrite(pCon,"ERROR: you are not priviledged to set interrupts",eError);
       return 0; 
     }
     
     /* only in script processing */
     if(!SCinMacro(pCon))
     {
       SCWrite(pCon,"ERROR: Interrupt manipulation only permitted in Macros",
               eError);
       return 0;        
     }
     
     /* is there a value ? */
     if(argc < 2)
     {
       SCWrite(pCon,"ERROR: missing parameter for SetInterrupt",eError);
       return 0;
     }
     
     /* actually do a job */
     strtolower(argv[1]);
     iInt = Text2Interrupt(argv[1]);
     if(iInt < 0)
     {
       sprintf(pBueffel,"ERROR: %s not recognized as Interrupt code",
               argv[1]);
       SCWrite(pCon,pBueffel,eError);
       return 0;
     }

     /* we want to change the interrupt pending in the executor. If none
        there, charnge our own.
     */   
     pDev = GetExecutor();
     assert(pDev);
     pOwner = GetExeOwner(pDev);
     if(pOwner)
     {
         SCSetInterrupt(pOwner,iInt);
     }
     else
     {
         SCSetInterrupt(pCon,iInt);
     }
     SCSendOK(pCon);
     return 1;
  }             
/*-------------------------------------------------------------------------*/                   
  int SetSICSStatus(SConnection *pCon, SicsInterp *pSics, void *pData,
                   int argc, char *argv[])
  {
     int iInt;
     char pBueffel[132];
     
     assert(pCon);
     assert(pSics);
     
     /* minimum  user privelege */
     if(!SCMatchRights(pCon,usUser))
     {
       SCWrite(pCon,"ERROR: you are not priviledged to set status",eError);
       return 0; 
     }
     
     /* only in script processing */
     if(!SCinMacro(pCon))
     {
       SCWrite(pCon,"ERROR: status manipulation only permitted in Macros",
               eError);
       return 0;        
     }
     
     /* is there a value ? */
     if(argc < 2)
     {
       SCWrite(pCon,"ERROR: missing parameter for SetStatus",eError);
       return 0;
     }
     
     /* actually do a job */
     strtolower(argv[1]);
     iInt = SetStatusFromText(argv[1]);
     if(iInt)
     {
       SCSendOK(pCon);
       return 1;
     }
     else
     {
       sprintf(pBueffel,"ERROR: %s not recognized as status code",argv[1]);
       SCWrite(pCon,pBueffel,eError);
       return 0;
     }
  }             
/*-----------------------------------------------------------------------*/
  static int isNum(char *pText)
  {
    int i;
    int iRet = 1;
    
      for(i = 0; i < strlen(pText); i++)
      {
        if(!isdigit(pText[i]))
        {
          if(!((pText[i] == '+') || (pText[i] == '-') || (pText[i] == '.')))
          {
            iRet = 0;
            break;
          }
        }
      }
      return iRet;
  }      
                   
/*--------------------------- Type Checking -------------------------------
  classifies a given string: numeric, countable, drivable, sicsobject, text
*/
  
  int SICSType(SConnection *pCon, SicsInterp *pSics, void *pData,
               int argc, char *argv[])
  {
     CommandList *pCom = NULL;
     Dummy *pDum = NULL;
     char pBueffel[132];
     
     
     assert(pCon);
     assert(pSics);
     
     /* is there a parameter anyway */
     if(argc < 2)
     {
       SCWrite(pCon,"ERROR: no object to test specified!",eError);
       return 0;
     }
     
     /* test, one by one */
     strtolower(argv[1]);
     if(isNum(argv[1]))
     {
        SCWrite(pCon,"NUM",eValue);
        return 1;
     }
     
     pCom = FindCommand(pSics,argv[1]);
     if(pCom)
     {
       pDum = (Dummy *)pCom->pData;
       if(pDum)
       {
         if(pDum->pDescriptor->GetInterface(pDum,DRIVEID))
         {
           SCWrite(pCon,"DRIV",eValue);
           return 1;
         }
         if(pDum->pDescriptor->GetInterface(pDum,COUNTID))
         {
            SCWrite(pCon,"COUNT",eValue);
            return 1;
         }
       }
       SCWrite(pCon,"COM",eValue);
       return 1;
     }
      
     SCWrite(pCon,"TEXT",eValue);
     return 1; 
  }             
               
/*----------------- Bounds checking for all driveables ---------------------*/
   int SICSBounds(SConnection *pCon, SicsInterp *pSics, void *pData,
                  int argc, char *argv[])
   {
     CommandList *pCom = NULL;
     pIDrivable pInt = NULL;
     Dummy *pDum = NULL;
     char pBueffel[132];
     float fVal;
     int iRet;
     
     assert(pCon);
     assert(pSics);
     
     if(argc < 3)
     {
        SCWrite(pCon,"ERROR: not enough parameters specified",eError);
        return 0;
     }
     
     /* argv[1] should be drivable */
     pCom = FindCommand(pSics,argv[1]);
     if(!pCom)
     {
        sprintf(pBueffel,"ERROR: %s is no Sics Object",argv[1]);
        SCWrite(pCon,pBueffel,eError);
        return 0;
     }
     
     pDum = (Dummy *)pCom->pData;
     if(!pDum)
     {
        sprintf(pBueffel,"ERROR: %s is no valid Sics Object",argv[1]);
        SCWrite(pCon,pBueffel,eError);
        return 0;
     }
     
     pInt = pDum->pDescriptor->GetInterface(pDum,DRIVEID);
     if(!pInt)
     {
        sprintf(pBueffel,"ERROR: %s is not drivable!",argv[1]);
        SCWrite(pCon,pBueffel,eError);
        return 0;
     }
     
     /* argv[2] should be a numeric */
     if(!isNum(argv[2]))
     {
        sprintf(pBueffel,"ERROR: %s is not a number!",argv[2]);
        SCWrite(pCon,pBueffel,eError);
        return 0;
     }
     
     /* do a job */
     fVal = atof(argv[2]);
     iRet = pInt->CheckLimits(pCom->pData,fVal,pBueffel,131);
     if(iRet)
     {
       SCWrite(pCon,"OK",eValue);
       return 1;
     }
     else
     {
       SCWrite(pCon,pBueffel,eWarning);
       return 0;
     }
     return 0; /* not reached */
   }                                   
/*----------------- Status checking for Sics Objects   ---------------------*/
   int SICSStatus(SConnection *pCon, SicsInterp *pSics, void *pData,
                  int argc, char *argv[])
   {
     CommandList *pCom = NULL;
     Dummy *pDum = NULL;
     pIDrivable pDInt = NULL;
     pICountable pCInt = NULL;
     char pBueffel[132];
     float fVal;
     int iRet;
     
     assert(pCon);
     assert(pSics);
     
     if(argc < 2)
     {
        SCWrite(pCon,"ERROR: not enough parameters specified",eError);
        return 0;
     }
     
     /* argv[1] should be drivable */
     pCom = FindCommand(pSics,argv[1]);
     if(!pCom)
     {
        sprintf(pBueffel,"ERROR: %s is no Sics Object",argv[1]);
        SCWrite(pCon,pBueffel,eError);
        return 0;
     }
     
     pDum = (Dummy *)pCom->pData;
     if(!pDum)
     {
        sprintf(pBueffel,"ERROR: %s is no valid Sics Object",argv[1]);
        SCWrite(pCon,pBueffel,eError);
        return 0;
     }
     
     pDInt = pDum->pDescriptor->GetInterface(pDum,DRIVEID);
     pCInt = pDum->pDescriptor->GetInterface(pDum,COUNTID);
     if(pDInt)
     {
        iRet = pDInt->CheckStatus(pDum,pCon);
        sprintf(pBueffel,"%d",iRet);
        SCWrite(pCon,pBueffel,eValue);
        return 1;
     }
     else if(pCInt)
     {
        iRet = pCInt->CheckCountStatus(pDum,pCon);
        sprintf(pBueffel,"%d",iRet);
        SCWrite(pCon,pBueffel,eValue);
        return 1;
     }
     else
     {
        sprintf(pBueffel,"ERROR: %s is neither drivable nor countable",argv[1]);
        SCWrite(pCon,pBueffel,eError);
        return 0;
     }
     /* not reached */
     return 0;

   }                                   
/*--------------------------------------------------------------------------*/
   int SICSDebug(SConnection *pCon, SicsInterp *pSics, void *pData,
                  int argc, char *argv[])
   {
      char pBueffel[256];
      Tcl_Interp *pTcl = NULL;
      int iRet;
      
      assert(pCon);
      assert(pSics);
      
      if(!SCMatchRights(pCon,usInternal))
      {
        SCWrite(pCon,"ERROR: no privilege to interact with Tcl for you",
                eError);
        return 0;
      }
      
      pTcl = InterpGetTcl(pSics);
      assert(pTcl);
      
      Arg2Text(argc-1,&argv[1],pBueffel,255);
      iRet = Tcl_Eval(pTcl,pBueffel);
      if(strlen(pTcl->result) > 1)
      {
         SCWrite(pCon,pTcl->result,eValue);
      } 
      return iRet;
   }
/*--------------------------------------------------------------------------*/
   int SICSTime(SConnection *pCon, SicsInterp *pSics, void *pData,
                  int argc, char *argv[])
   {
      char pBueffel[256];

      SNXFormatTime(pBueffel,255);
      SCWrite(pCon,pBueffel,eValue);
      return 1;
   }
/*--------------------------------------------------------------------------
  Kill a command from SICS
*/  
   int SICSKill(SConnection *pCon, SicsInterp *pSics, void *pData,
                  int argc, char *argv[])
   {
       CommandList *pCom = NULL, *pCurrent = NULL;
       void *pDat = NULL;
       char *pPtr = NULL;
       
       if(!SCMatchRights(pCon,usMugger))
       {
          SCWrite(pCon,"ERROR: you may not kill commands here, no privilege",
                  eError);
          return 0;        
       } 
       
       if(argc < 2)
       {
         SCWrite(pCon,"ERROR: need name of command to kill",eError);
         return 0;
       }

       /* memorise data for alias search */
       pCom = FindCommand(pSics,argv[1]);
       if(pCom)
       {
         pDat = pCom->pData;
       }       
       else
       {
         pDat = NULL;
       }
       RemoveCommand(pSics,argv[1]);
       if(!pDat) /* no data, no alias */
       {
         SCSendOK(pCon);
         return 0;
       }
       
       /* kill aliases */
       pPtr = FindAlias(pSics,pDat);
       while(pPtr)
       {
         RemoveCommand(pSics,pPtr);
         pPtr = FindAlias(pSics,pDat);
       }
       SCSendOK(pCon);
       return 1;  
   }
/*------------------------------------------------------------------------*/
   int SicsPrompt(SConnection *pCon, SicsInterp *pSics, void *pData,
                  int argc, char *argv[])
   {
      char pBueffel[512];
      int iRet;  

      /* all arguments are optional */    
      if(argc < 1)
      {
          iRet = SCPrompt(pCon, "SICS> ", 
                          pBueffel, 511);
      }
      else
      {
          iRet = SCPrompt(pCon, argv[1], 
                          pBueffel, 511);
      }
      if(iRet == 1)
      {
        SCWrite(pCon,pBueffel,eValue);
        return 1;
      }
      else
      {
        SCWrite(pCon,"ERROR: Interrupted while waiting for data",eError);
        return 0;
      }
   }   
