Files
sics/SCinter.c
cvs bc02cb79e7 - made fixes to hkl
- Introduced a help system
- introduced a module for handling automatic updates of files during
  long measurements
- Added a circular buffer and handling facilities to varlog
- Upgraded documentation


SKIPPED:
	psi/faverage.h
	psi/nxamor.tex
	psi/pimotor.h
	psi/pimotor.tex
2003-12-10 13:50:44 +00:00

794 lines
18 KiB
C

/*---------------------------------------------------------------------------
Implementation file for the SICS-interpreter.
Mark Koennecke, November 1996
Made ListObjects moe intelligent: list objects according to interface etc.
Mark Koennecke, December 2003
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.
M. Zolliker, Sept 2000, introduced formal aliases, modifications marked M.Z
Mark Koennecke, August 2001, modified SicsWriteStatus to write motor
positions on demand.
---------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <assert.h>
#include <string.h>
#include <tcl.h>
#include "fortify.h"
#include "sics.h"
#include "splitter.h"
#include "servlog.h"
#include "macro.h"
#include "interface.h"
#include "motor.h"
#include "obdes.h"
/* M.Z. */
#include "definealias.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->AList.pFirst = NULL; /* M.Z. */
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);
RemoveAlias(&pInterp->AList,pBueffel); /* M.Z. */
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
{
printf("Executing -> %s <- from dummy socket\n", 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], *pCmd;
assert(self);
if(self->iDeleting)
{
return NULL;
}
strcpy(pBueffel,pName);
strtolower(pBueffel);
pCmd=TranslateAlias(&self->AList, pBueffel); /* M.Z. */
pCurrent = self->pCList;
while(pCurrent)
{
if(pCurrent->pName != NULL)
{
if(strcmp(pCurrent->pName, pCmd) == 0 ) /* M.Z. */
{
return pCurrent;
}
}
pCurrent = pCurrent->pNext;
}
return NULL;
}
/*------------------------------------------------------------------------*/
int WriteSicsStatus(SicsInterp *self, char *file, int iMot)
{
CommandList *pCurrent = NULL;
FILE *fd = NULL;
Dummy *pDum = NULL;
float fVal;
pIDrivable pDriv = NULL;
void *pTest = 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);
if(iMot)
{
/*
save values of motors but not of environment devices as they
may not be present the next time round
*/
pDriv = pDum->pDescriptor->GetInterface(pDum,DRIVEID);
pTest = pDum->pDescriptor->GetInterface(pDum,ENVIRINTERFACE);
if(pDriv && !pTest)
{
if(strcmp(pDum->pDescriptor->name,"Motor") == 0)
{
MotorGetSoftPosition((pMotor)pDum,pServ->dummyCon,&fVal);
}
else
{
fVal = pDriv->GetValue(pDum,pServ->dummyCon);
}
if(fVal > -990.)
{
fprintf(fd,"run %s %f\n",pCurrent->pName, fVal);
}
}
}
}
pCurrent = pCurrent->pNext;
}
if(iMot)
{
fprintf(fd,"Success \n");
}
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)
{
/* printf("Deleting %s\n",pCurrent->pName); */
free(pCurrent->pName);
}
pTemp = pCurrent->pNext;
free(pCurrent);
pCurrent = pTemp;
}
FreeAliasList(&self->AList); /* M.Z. */
/* 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);
}
/*------------------------------------------------------------------------*/
static void printAll(SicsInterp *pSics, SConnection *pCon)
{
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);
}
}
/*-----------------------------------------------------------------------
printInterface prints only those objects which implement an interface
as specified bi the id given
-------------------------------------------------------------------------*/
static void printInterface(SicsInterp *pSics, SConnection *pCon, int id)
{
CommandList *pCurrent;
char pBueffel[256];
int iNum = 0;
pObjectDescriptor pObj = NULL;
assert(pSics);
assert(pCon);
pBueffel[0] = '\0';
pCurrent = pSics->pCList;
while(pCurrent)
{
pObj = FindDescriptor(pCurrent->pData);
if(pObj != NULL)
{
if(pObj->GetInterface(pObj,id) != NULL)
{
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 */
strcat(pBueffel,"\r\n");
SCWrite(pCon,pBueffel,eStatus);
}
/*-----------------------------------------------------------------------
printMatch prints only those objects which match the wildcard string given
-------------------------------------------------------------------------*/
extern int match(const char *mask, const char *name); /* from wwildcard.c */
static void printMatch(SicsInterp *pSics, SConnection *pCon, char *mask)
{
CommandList *pCurrent;
char pBueffel[256];
int iNum = 0;
pObjectDescriptor pObj = NULL;
assert(pSics);
assert(pCon);
pBueffel[0] = '\0';
pCurrent = pSics->pCList;
while(pCurrent)
{
pObj = FindDescriptor(pCurrent->pData);
if(pObj != NULL)
{
if(!match(mask,pObj->name))
{
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 */
strcat(pBueffel,"\r\n");
SCWrite(pCon,pBueffel,eStatus);
}
/*-----------------------------------------------------------------------
printType prints only those objects which match the type given
-------------------------------------------------------------------------*/
static void printType(SicsInterp *pSics, SConnection *pCon, char *type)
{
CommandList *pCurrent;
char pBueffel[256];
int iNum = 0;
assert(pSics);
assert(pCon);
pBueffel[0] = '\0';
pCurrent = pSics->pCList;
while(pCurrent)
{
if(pCurrent->pData != NULL)
{
if(iHasType(pCurrent->pData,type))
{
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 */
strcat(pBueffel,"\r\n");
SCWrite(pCon,pBueffel,eStatus);
}
/*--------------------------------------------------------------------------*/
int ListObjects(SConnection *pCon, SicsInterp *pSics, void *pData,
int argc, char *argv[])
{
if(argc < 2)
{
printAll(pSics,pCon);
return 1;
}
strtolower(argv[1]);
/*
stand alone subcommands
*/
if(strstr(argv[1],"var") != NULL)
{
printType(pSics,pCon,"SicsVariable");
return 1;
}
if(strstr(argv[1],"mot") != NULL)
{
printType(pSics,pCon,"Motor");
return 1;
}
/*
subcommand with three args
*/
if(argc < 3)
{
SCWrite(pCon,"ERROR: missing parameter to command or bad subcommand",
eError);
return 0;
}
/*
interface
*/
if(strcmp(argv[1],"inter") == 0)
{
strtolower(argv[2]);
if(strstr(argv[2],"driv") != NULL)
{
printInterface(pSics,pCon,DRIVEID);
return 1;
}
if(strstr(argv[2],"coun") != NULL)
{
printInterface(pSics,pCon,COUNTID);
return 1;
}
if(strstr(argv[2],"env") != NULL)
{
printInterface(pSics,pCon,ENVIRINTERFACE);
return 1;
}
SCWrite(pCon,"ERROR: interface description nor recognized",eError);
return 0;
}
/*
match
*/
if(strcmp(argv[1],"match") == 0)
{
printMatch(pSics,pCon,argv[2]);
return 1;
}
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;
}
/*------------------------------------------------------------------------*/
void *FindDrivable(SicsInterp *pSics, char *name){
pIDrivable pDriv;
pDummy pDum = NULL;
CommandList *pCom = NULL;
pCom = FindCommand(pSics,name);
if(pCom != NULL){
pDum = (pDummy)pCom->pData;
if(pDum != NULL){
return pDum->pDescriptor->GetInterface(pDum,DRIVEID);
}
}
return NULL;
}