1044 lines
26 KiB
C
1044 lines
26 KiB
C
/*---------------------------------------------------------------------------
|
|
|
|
Implementation file for the SICS-interpreter.
|
|
|
|
Mark Koennecke, November 1996
|
|
|
|
Made ListObjects more 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.
|
|
|
|
Made ListObjects moe intelligent: list objects according to interface etc.
|
|
Mark Koennecke, December 2003
|
|
|
|
Extended 'dir' command (function ListObjects) to list via typename from
|
|
object descriptor. For completeness, added function PrintAllTypes.
|
|
Paul Hathaway, May 2004
|
|
|
|
Modified printXXX functions to fix duplicate write of last buffer line.
|
|
Paul Hathaway, May 2004
|
|
---------------------------------------------------------------------------*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <ctype.h>
|
|
#include <assert.h>
|
|
#include <string.h>
|
|
#include <tcl.h>
|
|
#include <time.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"
|
|
#include "lld.h"
|
|
|
|
/* M.Z. */
|
|
#include "definealias.h"
|
|
|
|
/* pvh */
|
|
#include "lld_str.h"
|
|
static void printList(SConnection *pCon, int listID);
|
|
static void freeList(int listID);
|
|
|
|
#define MAXLEN 256
|
|
#define MAXPAR 100
|
|
#define MAXBUF 128
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
SicsInterp *InitInterp(void)
|
|
{
|
|
SicsInterp *pInter = NULL;
|
|
int i;
|
|
|
|
pInter = (SicsInterp *)malloc(sizeof(SicsInterp));
|
|
if(!pInter)
|
|
{
|
|
SICSLogWrite("Error allocating memory for Interpreter",eInternal);
|
|
return NULL;
|
|
}
|
|
memset(pInter,0,sizeof(SicsInterp));
|
|
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 AddCommandWithFlag(SicsInterp *pInterp, char *pName, ObjectFunc pFunc,
|
|
KillFunc pKFunc, void *pData, int startupOnly)
|
|
{
|
|
CommandList *pNew = NULL;
|
|
CommandList *p, *tail;
|
|
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 = NULL;
|
|
pNew->startupOnly = startupOnly;
|
|
pNew->stat = StatisticsNew(pBueffel);
|
|
|
|
/* find end of list */
|
|
tail = NULL;
|
|
p = pInterp->pCList;
|
|
while(p != NULL)
|
|
{
|
|
tail = p;
|
|
p = p->pNext;
|
|
}
|
|
if (tail==NULL) { /* first entry */
|
|
pInterp->pCList = pNew;
|
|
} else { /* insert at tail */
|
|
tail->pNext = pNew;
|
|
}
|
|
pNew->pPrevious = tail;
|
|
|
|
return 1;
|
|
}
|
|
/*------------------------------------------------------------------------*/
|
|
int AddCommand(SicsInterp *pInterp, char *pName, ObjectFunc pFunc,
|
|
KillFunc pKFunc, void *pData)
|
|
{
|
|
return AddCommandWithFlag(pInterp, pName, pFunc, pKFunc, pData, 0);
|
|
}
|
|
/*------------------------------------------------------------------------*/
|
|
int AddIniCmd(char *pName, ObjectFunc pFunc)
|
|
{
|
|
return AddCommandWithFlag(pServ->pSics, pName, pFunc, NULL, NULL, 1);
|
|
}
|
|
/*------------------------------------------------------------------------*/
|
|
int AddCmd(char *pName, ObjectFunc pFunc)
|
|
{
|
|
return AddCommandWithFlag(pServ->pSics, pName, pFunc, NULL, NULL, 0);
|
|
}
|
|
/*------------------------------------------------------------------------*/
|
|
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;
|
|
}
|
|
if (pVictim->stat) {
|
|
StatisticsKill(pVictim->stat);
|
|
}
|
|
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;
|
|
commandContext comCon;
|
|
Statistics *old;
|
|
|
|
|
|
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
|
|
{
|
|
sprintf(pBueffel, "Executing -> %s <- from dummy socket\n", pText);
|
|
SICSLogWrite(pBueffel,eCommand);
|
|
}
|
|
|
|
/* convert to argc, argv */
|
|
argc = 0;
|
|
argv = NULL;
|
|
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)
|
|
{
|
|
iRet = 1;
|
|
goto deleteArgv;
|
|
}
|
|
|
|
if(argv[0] == NULL)
|
|
{
|
|
SCWrite(pCon,"ERROR: failed to parse command",eError);
|
|
iRet = -1;
|
|
goto deleteArgv;
|
|
}
|
|
|
|
/* find it */
|
|
pCommand = FindCommand(self,argv[0]);
|
|
if(!pCommand)
|
|
{
|
|
sprintf(pBueffel,"ERROR: Object -> %s <- NOT found",
|
|
argv[0]);
|
|
SCWrite(pCon,pBueffel,eError);
|
|
iRet = -1;
|
|
goto deleteArgv;
|
|
}
|
|
|
|
|
|
/* invoke the command */
|
|
self->eOut = eStatus;
|
|
Tcl_ResetResult((Tcl_Interp *)self->pTcl);
|
|
MacroPush(pCon);
|
|
pCon->conStatus = 0;
|
|
old = StatisticsBegin(pCommand->stat);
|
|
iRet = pCommand->OFunc(pCon, self, pCommand->pData, argc, argv);
|
|
StatisticsEnd(old);
|
|
/* If a task is registered with the dev exec then conStatus is HWBusy*/
|
|
if (pCon->conStatus != HWBusy) {
|
|
comCon = SCGetContext(pCon);
|
|
if (0 != strcmp("contextdo",comCon.deviceID))
|
|
SCWrite(pCon,"",eFinish);
|
|
}
|
|
MacroPop();
|
|
|
|
deleteArgv:
|
|
if (argv) {
|
|
/* 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;
|
|
char tmpfile[PATH_MAX];
|
|
int l;
|
|
|
|
assert(self);
|
|
assert(file);
|
|
|
|
/* make sure that status file is always valid M.Z. Apr 2005 */
|
|
/* create a temporary file first */
|
|
l=strlen(file);
|
|
if (l >= sizeof tmpfile - 2) {
|
|
return 0;
|
|
}
|
|
strcpy(tmpfile, file);
|
|
tmpfile[l]='.';
|
|
tmpfile[l+1]='\0';
|
|
remove(tmpfile); /* remove already existing file */
|
|
|
|
fd = fopen(tmpfile,"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);
|
|
/* rename temporary to final file (this is an atomic action) */
|
|
if (0 > rename(tmpfile, file)) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
/*------------------------------------------------------------------------*/
|
|
void DeleteInterp(SicsInterp *self)
|
|
{
|
|
CommandList *pCurrent = NULL;
|
|
CommandList *pTemp, *tail;
|
|
Tcl_Interp *pTcl = NULL;
|
|
int i;
|
|
|
|
assert(self);
|
|
self->iDeleting = 1;
|
|
|
|
/* find end of list */
|
|
tail = NULL;
|
|
pCurrent = self->pCList;
|
|
while(pCurrent != NULL)
|
|
{
|
|
tail = pCurrent;
|
|
pCurrent = pCurrent->pNext;
|
|
}
|
|
|
|
/* delete Commandlist (reversed order) */
|
|
if (tail) {
|
|
pCurrent = tail;
|
|
while(pCurrent)
|
|
{
|
|
if(pCurrent->KFunc)
|
|
{
|
|
pCurrent->KFunc(pCurrent->pData);
|
|
}
|
|
if(pCurrent->pName)
|
|
{
|
|
/* printf("Deleting %s\n",pCurrent->pName); */
|
|
free(pCurrent->pName);
|
|
}
|
|
pTemp = pCurrent->pPrevious;
|
|
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)
|
|
{
|
|
/*
|
|
uncommented: the current versions of Tcl (8.3,4) dump core with a
|
|
memory problem deep in the Tcl library. This causes a core dump on
|
|
each SICS restart and breaks the use of external tools.
|
|
Tcl_DeleteInterp(pTcl);
|
|
|
|
call KillSicsUnknown instead to clean up all memory properly. M.Z., Apr 05
|
|
*/
|
|
KillSicsUnknown();
|
|
}
|
|
|
|
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;
|
|
pBueffel[0]='\0';
|
|
}
|
|
pCurrent = pCurrent->pNext;
|
|
}
|
|
|
|
/* write final entries */
|
|
if(strlen(pBueffel) > 2)
|
|
{
|
|
strcat(pBueffel,"\r\n");
|
|
SCWrite(pCon,pBueffel,eStatus);
|
|
}
|
|
}
|
|
/*------------------------------------------------------------------------*/
|
|
/* compareStringNode wraps strcmp for use in findNode(LLD module) calls */
|
|
int compareStringNode(const void *pStr1, const void *ppStr2)
|
|
{
|
|
return strcmp((char *)pStr1,*(char **)ppStr2);
|
|
}
|
|
/*------------------------------------------------------------------------
|
|
printAllTypes prints the list of types of objects instantiated on the
|
|
CommandList.
|
|
iFiltered=0 gives all objects including interpreter command objects
|
|
iFiltered=1 gives types where object name is not the same as its type
|
|
-------------------------------------------------------------------------*/
|
|
static void printAllTypes(SicsInterp *pSics, SConnection *pCon, int iFiltered)
|
|
{
|
|
CommandList *pCurrent = NULL;
|
|
char pBueffel[256];
|
|
char pName_lc[256];
|
|
char pType_lc[256];
|
|
char *pType;
|
|
Dummy *pTest;
|
|
int typeListID;
|
|
|
|
assert(pSics);
|
|
assert(pCon);
|
|
|
|
pBueffel[0] = '\0';
|
|
|
|
typeListID = LLDstringCreate();
|
|
if(-1==typeListID)
|
|
{
|
|
strcpy(pBueffel,"ERROR: Cannot generate list of object types\r\n");
|
|
SCWrite(pCon,pBueffel,eStatus);
|
|
return;
|
|
}
|
|
|
|
pCurrent = pSics->pCList;
|
|
while(pCurrent)
|
|
{
|
|
if(NULL != pCurrent->pData)
|
|
{
|
|
pTest = (pDummy)pCurrent->pData;
|
|
if(NULL != pTest->pDescriptor)
|
|
{
|
|
pType = pTest->pDescriptor->name;
|
|
strcpy(pType_lc,pType);
|
|
strtolower(pType_lc);
|
|
|
|
LLDnodePtr2First(typeListID);
|
|
|
|
/* int LLDnodeFind( int List, CompFunPtr Compare, void * DataPtr ); */
|
|
/* */
|
|
/* Find *DataPtr in the List using the *Compare function. */
|
|
/* Returns the return value of *Compare. */
|
|
/* 0 == equal == found. */
|
|
/* non-zero == not found. Current node is set to found node. */
|
|
/* Returns 2 for an empty list. */
|
|
/* NB: First checked node is current node, then search to end of list*/
|
|
|
|
if(0!=LLDnodeFind(typeListID,compareStringNode,(void *)pType))
|
|
{ /* empty list or 'typename' not found */
|
|
strcpy(pName_lc, pCurrent->pName);
|
|
strtolower(pName_lc);
|
|
if((0==iFiltered)||((1==iFiltered)&&(0!=strcmp(pType_lc,pName_lc))))
|
|
{ /*ie Add if unfiltered or pass filter(name!=typename) */
|
|
LLDstringAdd(typeListID,pType);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
pCurrent = pCurrent->pNext;
|
|
}
|
|
printList(pCon,typeListID);
|
|
freeList(typeListID);
|
|
}
|
|
/*-----------------------------------------------------------------------
|
|
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;
|
|
pBueffel[0]='\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);
|
|
pBueffel[0]='\0';
|
|
iNum = 0;
|
|
}
|
|
}
|
|
}
|
|
pCurrent = pCurrent->pNext;
|
|
}
|
|
|
|
/* write final entries */
|
|
strcat(pBueffel,"\r\n");
|
|
SCWrite(pCon,pBueffel,eStatus);
|
|
}
|
|
/*-----------------------------------------------------------------------
|
|
printType prints only those objects whose descriptor match the type given
|
|
-------------------------------------------------------------------------*/
|
|
static void printType(SicsInterp *pSics, SConnection *pCon, char *typeName)
|
|
{
|
|
CommandList *pCurrent;
|
|
Tcl_DString txt;
|
|
char pBueffel[256];
|
|
int iNum = 0;
|
|
|
|
assert(pSics);
|
|
assert(pCon);
|
|
|
|
Tcl_DStringInit(&txt);
|
|
pBueffel[0] = '\0';
|
|
pCurrent = pSics->pCList;
|
|
while(pCurrent)
|
|
{
|
|
if(pCurrent->pData != NULL)
|
|
{
|
|
if(iHasType(pCurrent->pData,typeName))
|
|
{
|
|
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");
|
|
Tcl_DStringAppend(&txt,pBueffel,-1);
|
|
pBueffel[0]='\0';
|
|
iNum = 0;
|
|
}
|
|
}
|
|
}
|
|
pCurrent = pCurrent->pNext;
|
|
}
|
|
|
|
/* write final entries */
|
|
strcat(pBueffel,"\r\n");
|
|
SCWrite(pCon,Tcl_DStringValue(&txt),eStatus);
|
|
Tcl_DStringFree(&txt);
|
|
}
|
|
/*--------------------------------------------------------------------------*/
|
|
int ListObjects(SConnection *pCon, SicsInterp *pSics, void *pData,
|
|
int argc, char *argv[])
|
|
{
|
|
char pType[256];
|
|
int i;
|
|
|
|
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;
|
|
}
|
|
/* Start Mod by Paul Hathaway May 2004 */
|
|
if(0 == strcmp(argv[1],"types"))
|
|
{
|
|
printAllTypes(pSics,pCon,1);
|
|
return 1;
|
|
}
|
|
/* End Mod by Paul Hathaway May 2004*/
|
|
|
|
|
|
/*
|
|
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 not recognized",eError);
|
|
return 0;
|
|
}
|
|
/*
|
|
match
|
|
*/
|
|
if(strcmp(argv[1],"match") == 0)
|
|
{
|
|
printMatch(pSics,pCon,argv[2]);
|
|
return 1;
|
|
}
|
|
|
|
/* Start Mod by Paul Hathaway May 2004 */
|
|
/*
|
|
* type-based dir
|
|
*/
|
|
if(0 == strcmp(argv[1],"type"))
|
|
{
|
|
if(0==strcmp(argv[2],"*"))
|
|
{
|
|
printAllTypes(pSics,pCon,0);
|
|
return 1;
|
|
}
|
|
strcpy(pType,argv[2]);
|
|
/* Cater for multi-word types eg 'Environment Monitor' */
|
|
if(argc > 3)
|
|
{
|
|
for(i=3;i<argc;i++)
|
|
{
|
|
strcat(pType," ");
|
|
strcat(pType,argv[i]);
|
|
}
|
|
}
|
|
printType(pSics,pCon,pType);
|
|
return 1;
|
|
}
|
|
/* End Mod by Paul Hathaway May 2004*/
|
|
|
|
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;
|
|
}
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
/* printList: Print contents of an LLDstring list
|
|
* Envisaged to be used by other extensions/refactoring utilising dynamic
|
|
* linked list module. May extend toallow different output formats
|
|
* (eg multi/single column) via switches
|
|
*/
|
|
static void printList(SConnection *pCon, int listID)
|
|
{
|
|
char pBueffel[MAXBUF];
|
|
int retCode;
|
|
|
|
if(0!=LLDnodePtr2First(listID))
|
|
{
|
|
do
|
|
{
|
|
retCode = LLDstringData(listID,NULL);
|
|
if ((MAXBUF-3) > retCode) {
|
|
retCode = LLDstringData(listID,pBueffel);
|
|
strcat(pBueffel,"\r\n");
|
|
SCWrite(pCon,pBueffel,eStatus);
|
|
}
|
|
} while(0!=LLDnodePtr2Next(listID));
|
|
}
|
|
}
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
static void freeList(int listID)
|
|
{
|
|
do {
|
|
LLDstringDelete(listID);
|
|
} while(0!=LLDnodePtr2First(listID));
|
|
LLDdelete(listID);
|
|
}
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
void RemoveStartupCommands(void)
|
|
{
|
|
CommandList *pCurrent, *pNext;
|
|
pCurrent = pServ->pSics->pCList;
|
|
while(pCurrent)
|
|
{
|
|
pNext = pCurrent->pNext;
|
|
if (pCurrent->startupOnly) {
|
|
RemoveCommand(pServ->pSics, pCurrent->pName);
|
|
}
|
|
pCurrent = pNext;
|
|
}
|
|
}
|