- A couple of small fixes for memory and initialization problems.

This is to make valgrind happy


SKIPPED:
	psi/amorscan.c
	psi/el734hp.c
	psi/psi.c
	psi/tasscan.c
This commit is contained in:
koennecke
2005-01-12 08:42:39 +00:00
parent 5e05be17e0
commit ef1de4589c
35 changed files with 2659 additions and 1203 deletions

749
stdscan.c Normal file
View File

@ -0,0 +1,749 @@
/*--------------------------------------------------------------------------
S T A N D A R D S C A N
This is a library of scan functions for the SICS standard scan.
copyright: see copyright.h
Extracted from scan.c: Mark Koennecke, November 2004
----------------------------------------------------------------------------*/
#include "sics.h"
#include <stdio.h>
#include <arpa/inet.h>
#include <tcl.h>
#include <math.h>
#include "fortify.h"
#include "sdynar.h"
#include "dynstring.h"
#include "stringdict.h"
#include "status.h"
#include "sicsvar.h"
#include "counter.h"
#include "scan.h"
#include "scan.i"
#include "splitter.h"
#include "danu.h"
#include "userscan.h"
#include "motor.h"
#include "nxscript.h"
#include "nxutil.h"
#include "site.h"
#include "lld.h"
/*---------------------------------------------------------------------------*/
int WriteHeader(pScanData self)
{
int i, iRet;
FILE *fd;
char pBuffer[512], pError[512], *pPtr, *pName;
CommandList *pCom = NULL;
pSicsVariable pVar = NULL;
pDummy pDum = NULL;
pIDrivable pDriv = NULL;
float fVal;
pMotor pMot = NULL;
pVarEntry pScanVar = NULL;
void *pVoid = NULL;
assert(self->pSics);
assert(self->pCon);
/* open data file */
self->fd = fopen(self->pFile,"w");
if(!self->fd)
{
SCWrite(self->pCon,"ERROR: cannot write data file",eError);
return 0;
}
/* open header description file */
fd = fopen(self->pHeaderFile,"r");
if(!fd)
{
SCWrite(self->pCon,"ERROR: cannot open header description file",eError);
return 0;
}
/* loop through description file and act along the way */
while(fgets(pBuffer,511,fd) != NULL)
{
pPtr = strstr(pBuffer,"!!VAR(");
if(pPtr) /* handle a Sics variable */
{
/* extract the name */
pName = pPtr + 6; /* first char of name */
*pPtr = '\0'; /* this is the starter of the line */
pPtr = pName;
while( (*pPtr != '\0') && (*pPtr != ')') )
{
pPtr++;
}
*pPtr = '\0';
/* find the variable */
pCom = FindCommand(self->pSics,pName);
if(!pCom)
{
sprintf(pError,"ERROR: variable %s NOT found",pName);
SCWrite(self->pCon,pError,eError);
continue;
}
pVar = (pSicsVariable)pCom->pData;
if(!pVar)
{
sprintf(pError,"ERROR: variable %s NOT found",pName);
SCWrite(self->pCon,pError,eError);
continue;
}
switch(pVar->eType)
{
case veFloat:
sprintf(pError,"%f",pVar->fVal);
break;
case veInt:
sprintf(pError,"%d",pVar->iVal);
break;
case veText:
sprintf(pError,"%s",pVar->text);
break;
}
/* finally write */
fprintf(self->fd,"%s %s\n",pBuffer,pError);
continue;
}/* end variable */
/*------- Drivable */
pPtr = strstr(pBuffer,"!!DRIV(");
if(pPtr)
{
/* extract the name */
pName = pPtr + 7; /* first char of name */
*pPtr = '\0'; /* this is the starter of the line */
pPtr = pName;
while( (*pPtr != '\0') && (*pPtr != ')') )
{
pPtr++;
}
*pPtr = '\0';
/* find the variable */
pCom = FindCommand(self->pSics,pName);
if(!pCom)
{
sprintf(pError,"ERROR: variable %s NOT found",pName);
SCWrite(self->pCon,pError,eError);
continue;
}
pDum = (pDummy)pCom->pData;
if(!pDum)
{
sprintf(pError,"ERROR: variable %s is NOT drivable",pName);
SCWrite(self->pCon,pError,eError);
continue;
}
pDriv = (pIDrivable)pDum->pDescriptor->GetInterface(pDum,DRIVEID);
if(!pDriv)
{
sprintf(pError,"ERROR: variable %s is NOT drivable",pName);
SCWrite(self->pCon,pError,eError);
continue;
}
fVal = pDriv->GetValue(pDum,self->pCon);
fprintf(self->fd,"%s %f\n",pBuffer,fVal);
continue;
} /* end drive */
/*------- zero point */
pPtr = strstr(pBuffer,"!!ZERO(");
if(pPtr)
{
/* extract the name */
pName = pPtr + 7; /* first char of name */
*pPtr = '\0'; /* this is the starter of the line */
pPtr = pName;
while( (*pPtr != '\0') && (*pPtr != ')') )
{
pPtr++;
}
*pPtr = '\0';
/* find the motor */
pMot = FindMotor(self->pSics,pName);
if(!pMot)
{
sprintf(pError,"ERROR: motor %s NOT found",pName);
SCWrite(self->pCon,pError,eError);
continue;
}
iRet = MotorGetPar(pMot,"softzero",&fVal);
if(!iRet)
{
SCWrite(self->pCon,"ERROR: failed to read zero point",eError);
continue;
}
fprintf(self->fd,"%s %f\n",pBuffer,fVal);
continue;
} /* end zero point */
/* ------- date */
pPtr = strstr(pBuffer,"!!DATE!!");
if(pPtr)
{
*pPtr = '\0';
SNXFormatTime(pError,511);
fprintf(self->fd,"%s %s\n",pBuffer,pError);
continue;
}
/*-------- filename */
pPtr = strstr(pBuffer,"!!FILE!!");
if(pPtr)
{
*pPtr = '\0';
fprintf(self->fd,"%s %s\n",pBuffer,self->pFile);
continue;
}
/*------------ scanzero */
pPtr = strstr(pBuffer,"!!SCANZERO!!");
if(pPtr)
{
*pPtr = '\0';
/* write zero point of first scan variable if motor */
DynarGet(self->pScanVar,0,&pVoid);
pScanVar = (pVarEntry)pVoid;
if(pScanVar)
{
pMot = NULL;
pMot = FindMotor(self->pSics,ScanVarName(pScanVar));
if(pMot != NULL)
{
MotorGetPar(pMot,"softzero",&fVal);
fprintf(self->fd,"%s zero = %8.3f\n",ScanVarName(pScanVar),
fVal);
}
}
}
/* --------- plain text */
fprintf(self->fd,"%s",pBuffer);
} /* end while */
/* remember position for seeking to it for writing data */
self->lPos = ftell(self->fd);
fclose(fd);
fclose(self->fd);
self->fd = NULL;
return 1;
}
/*--------------------------------------------------------------------------*/
int WriteScanPoints(pScanData self, int iPoint)
{
int i, i2;
char pLine[512], pItem[30], pInfo[512];
pVarEntry pVar = NULL;
pCountEntry pData = NULL;
void *pPtr = NULL;
assert(self->pCon);
assert(self->pSics);
/* reopen file */
self->fd = fopen(self->pFile,"r+");
if(!self->fd)
{
SCWrite(self->pCon,
"ERROR: Failed to reopen scan file, aborting scan",
eError);
return 0;
}
/* jump to end of header */
fseek(self->fd,self->lPos, SEEK_SET);
if(self->iChannel != 0)
{
fprintf(self->fd,"WARNING: Scanning monitor %d\n",self->iChannel);
}
/* make the data header */
sprintf(pLine,"%-5s","NP");
strcpy(pInfo,"Scanning Variables: ");
for(i = 0; i < self->iScanVar;i++)
{
DynarGet(self->pScanVar,i,&pPtr);
pVar = (pVarEntry)pPtr;
if(pVar)
{
sprintf(pItem,"%-9.9s ",ScanVarName(pVar));
strcat(pLine,pItem);
sprintf(pItem,"%s, ",ScanVarName(pVar));
strcat(pInfo,pItem);
}
}
strcat(pLine," Counts ");
strcat(pLine,"Monitor1 ");
strcat(pLine,"Monitor2 ");
strcat(pLine,"Monitor3 ");
strcat(pLine,"Time ");
sprintf(pItem,"\n%d Points,",self->iNP);
strcat(pInfo,pItem);
if(self->iMode == eTimer)
{
strcat(pInfo," Mode: Timer,");
}
else
{
strcat(pInfo," Mode: Monitor,");
}
sprintf(pItem," Preset %f",self->fPreset);
strcat(pInfo,pItem);
fprintf(self->fd,"%s\n",pInfo);
fprintf(self->fd,"%s\n",pLine);
/* now the scan points */
for(i = 0; i < self->iCounts; i++)
{
sprintf(pLine,"%-5d",i);
/* print vars */
for(i2 = 0; i2 < self->iScanVar; i2++)
{
DynarGet(self->pScanVar,i2,&pPtr);
pVar = (pVarEntry)pPtr;
if(pVar)
{
sprintf(pItem,"%-10.3f",GetScanVarPos(pVar,i));
strcat(pLine,pItem);
}
}
/* print Counts & Monitor */
DynarGet(self->pCounts,i,&pPtr);
pData = (pCountEntry)pPtr;
if(pData)
{
sprintf(pItem," %-12ld",pData->lCount);
strcat(pLine,pItem);
sprintf(pItem,"%-12ld",pData->Monitors[0]);
strcat(pLine,pItem);
sprintf(pItem,"%-12ld",pData->Monitors[1]);
strcat(pLine,pItem);
sprintf(pItem,"%-12ld",pData->Monitors[2]);
strcat(pLine,pItem);
sprintf(pItem,"%-6.1f",pData->fTime);
strcat(pLine,pItem);
}
fprintf(self->fd,"%s\n",pLine);
}
/* done */
fprintf(self->fd,"END-OF-DATA\n");
fclose(self->fd);
self->fd = NULL;
return 1;
}
/*--------------------------------------------------------------------------*/
int PrepareScan(pScanData self)
{
pVarEntry pVar = NULL;
void *pDings;
int i, iRet;
float fVal;
char pBueffel[512];
char pMessage[1024];
assert(self);
assert(self->iNP > 0);
assert(self->pCon);
/* check boundaries of scan variables and allocate storage */
for(i = 0; i < self->iScanVar; i++)
{
DynarGet(self->pScanVar,i,&pDings);
pVar = (pVarEntry)pDings;
if(pVar)
{
/* start value */
fVal = ScanVarStart(pVar);
iRet = pVar->pInter->CheckLimits(pVar->pObject,
fVal,pBueffel,511);
if(!iRet)
{
sprintf(pMessage,"ERROR: %s, scan aborted",pBueffel);
SCWrite(self->pCon,pBueffel,eError);
return 0;
}
/* end value */
fVal = pVar->fStart + (self->iNP - 1) * ScanVarStep(pVar);
iRet = pVar->pInter->CheckLimits(pVar->pObject,
fVal,pBueffel,511);
if(!iRet)
{
sprintf(pMessage,"ERROR: %s, scan aborted",pBueffel);
SCWrite(self->pCon,pBueffel,eError);
return 0;
}
InitScanVar(pVar);
}
else
{
SCWrite(self->pCon,
"WARNING: Internal error, no scan variable, I try to continue",
eWarning);
}
pVar = NULL;
} /* end for */
/* configure counter */
SetCounterMode((pCounter)self->pCounterData,self->iMode);
SetCounterPreset((pCounter)self->pCounterData, self->fPreset);
self->iCounts = 0;
return 1;
}
/*--------------------------------------------------------------------------*/
int NonCheckPrepare(pScanData self)
{
pVarEntry pVar = NULL;
void *pDings;
int i, iRet;
float fVal;
char pBueffel[512];
char pMessage[1024];
assert(self);
assert(self->iNP > 0);
assert(self->pCon);
/* allocate storage for scan variables */
for(i = 0; i < self->iScanVar; i++)
{
DynarGet(self->pScanVar,i,&pDings);
pVar = (pVarEntry)pDings;
if(pVar)
{
/* start value */
fVal = ScanVarStart(pVar);
InitScanVar(pVar);
}
else
{
SCWrite(self->pCon,
"WARNING: Internal error, no scan variable, I try to continue",
eWarning);
}
pVar = NULL;
} /* end for */
/* configure counter */
SetCounterMode((pCounter)self->pCounterData,self->iMode);
SetCounterPreset((pCounter)self->pCounterData, self->fPreset);
self->iCounts = 0;
return 1;
}
/*------------------------------------------------------------------------*/
static int CollectScanDataIntern(pScanData self, int iPoint, int jochenFlag)
{
pVarEntry pVar = NULL;
void *pDings;
int i, iRet, status;
char pStatus[512], pItem[20];
char pHead[512];
float fVal;
CountEntry sCount;
assert(self);
assert(self->pCon);
InitCountEntry(&sCount);
/* prepare output header */
sprintf(pHead,"%-5.5s","NP");
sprintf(pStatus,"%-5d",iPoint);
/* loop over all scan variables */
status = 1;
for(i = 0; i < self->iScanVar; i++)
{
DynarGet(self->pScanVar,i,&pDings);
pVar = (pVarEntry)pDings;
if(pVar)
{
if(jochenFlag == 1 &&
strcmp(pVar->pObject->pDescriptor->name, "Motor") == 0)
{
MotorGetSoftPosition((pMotor)pVar->pObject,self->pCon,&fVal);
}
else
{
fVal = pVar->pInter->GetValue(pVar->pObject,self->pCon);
}
AppendScanVar(pVar,fVal);
sprintf(pItem,"%-10.10s",ScanVarName(pVar));
strcat(pHead,pItem);
sprintf(pItem,"%-10.3f",fVal);
strcat(pStatus,pItem);
}
}
/* store counter data */
sCount = CollectCounterData(self);
/*
format header
*/
strcat(pHead,"Counts ");
sprintf(pItem,"%-15d",sCount.lCount);
strcat(pStatus,pItem);
strcat(pHead,"Monitor1 ");
sprintf(pItem,"%-12d",sCount.Monitors[0]);
strcat(pStatus,pItem);
strcat(pHead,"Monitor2 ");
sprintf(pItem,"%-12d",sCount.Monitors[1]);
strcat(pStatus,pItem);
strcat(pHead,"Monitor3 ");
sprintf(pItem,"%-12d",sCount.Monitors[2]);
strcat(pStatus,pItem);
strcat(pHead,"Time ");
sprintf(pItem,"%-6.1f",sCount.fTime);
strcat(pStatus,pItem);
/* write progress */
strcat(pHead,"\n");
strcat(pStatus,"\n");
SCWrite(self->pCon,pHead,eWarning);
SCWrite(self->pCon,pStatus,eWarning);
return 1;
}
/*---------------------------------------------------------------------------*/
int CollectScanData(pScanData self, int iPoint)
{
return CollectScanDataIntern(self,iPoint,0);
}
/*--------------------------------------------------------------------------*/
int CollectScanDataJochen(pScanData self, int iPoint)
{
return CollectScanDataIntern(self,iPoint,1);
}
/*--------------------------------------------------------------------------*/
static int StartToDrive(pScanData self, int iPoint)
{
pVarEntry pVar = NULL;
void *pDings;
int i, iRet, status;
assert(self);
assert(self->pCon);
/* loop over all scan variables */
status = 1;
for(i = 0; i < self->iScanVar; i++)
{
DynarGet(self->pScanVar,i,&pDings);
pVar = (pVarEntry)pDings;
if(pVar)
{
iRet = StartScanVar(pVar,self->pCon,iPoint);
if(!iRet)
{
status = 0;
break;
}
}
}
return status;
}
/*------------------------------------------------------------------------*/
int ScanDrive(pScanData self, int iPoint)
{
int iRet;
long lTask;
int status;
iRet = StartToDrive(self,iPoint);
if(!iRet)
{
SCWrite(self->pCon,"ERROR: Cannot Drive, Scan aborted",eError);
status = 0;
}
else
{
status = 1;
}
/* wait for finish */
lTask = GetDevexecID(pServ->pExecutor);
if(lTask > 0)
{
TaskWait(pServ->pTasker,lTask);
}
return status;
}
/*--------------------------------------------------------------------------*/
int ScanCount(pScanData self, int iPoint)
{
pDummy pDum;
int iRet;
long lTask;
pDum = (pDummy)self->pCounterData;
iRet = StartDevice(pServ->pExecutor,
"ScanCounter",
pDum->pDescriptor,
self->pCounterData,
self->pCon,
self->fPreset);
if(!iRet)
{
SCWrite(self->pCon,"ERROR: Cannot Count, Scan aborted",eError);
return 0;
}
SetStatus(eCounting);
/* wait for finish */
lTask = GetDevexecID(pServ->pExecutor);
if(lTask > 0);
{
TaskWait(pServ->pTasker,lTask);
}
return 1;
}
/*====================== script invocation functions ====================*/
static pDynString GetStandardInvocation(pScanData self, char *function){
pDynString result = NULL;
char value[132];
result = CreateDynString(80,80);
if(result == NULL){
SCWrite(self->pCon,"ERROR: out of memory in scan invocation",eError);
return NULL;
}
if(StringDictGet(self->scanFunctions,function,value,131) != 1){
snprintf(value,131,"ERROR: scan function %s not found",function);
SCWrite(self->pCon,value,eError);
DeleteDynString(result);
return NULL;
}
DynStringCopy(result,value);
DynStringConcatChar(result,' ');
DynStringConcat(result, self->objectName);
DynStringConcatChar(result,' ');
value[0] = '\0';
StringDictGet(self->scanFunctions,"userdata",value,131);
DynStringConcat(result,value);
DynStringConcatChar(result,' ');
return result;
}
/*-----------------------------------------------------------------------*/
static int StandardScriptInvoke(pScanData self, char *function){
pDynString command = NULL;
int status;
command = GetStandardInvocation(self,function);
if(command == NULL){
return 0;
}
status = InterpExecute(self->pSics, self->pCon,
GetCharArray(command));
DeleteDynString(command);
if(status != 1) {
return 0;
}
return status;
}
/*-----------------------------------------------------------------------*/
static int StandardScriptInvokeWithPoint(pScanData self,
char *function, int iPoint){
pDynString command = NULL;
int status;
char pNumber[50];
command = GetStandardInvocation(self,function);
if(command == NULL){
return 0;
}
snprintf(pNumber,49,"%d",iPoint);
DynStringConcat(command,pNumber);
status = InterpExecute(self->pSics, self->pCon,
GetCharArray(command));
DeleteDynString(command);
if(status != 1) {
return 0;
}
return status;
}
/*------------------------------------------------------------------------*/
int ScriptWriteHeader(pScanData self){
return StandardScriptInvoke(self,"writeheader");
}
/*---------------------------------------------------------------------*/
int ScriptPrepareScan(pScanData self){
return StandardScriptInvoke(self,"prepare");
}
/*-----------------------------------------------------------------------*/
int ScriptScanDrive(pScanData self, int iPoint){
return StandardScriptInvokeWithPoint(self,"drive",iPoint);
}
/*------------------------------------------------------------------------*/
int ScriptScanCount(pScanData self, int iPoint){
return StandardScriptInvokeWithPoint(self,"count",iPoint);
}
/*-------------------------------------------------------------------------*/
int ScriptScanCollect(pScanData self, int iPoint){
return StandardScriptInvokeWithPoint(self,"collect",iPoint);
}
/*----------------------------------------------------------------------*/
int ScriptWriteScanPoints(pScanData self, int iPoint){
return StandardScriptInvokeWithPoint(self,"writepoint",iPoint);
}
/*---------------------------------------------------------------------*/
void ConfigureScript(pScanData self){
assert(self);
self->PrepareScan = ScriptPrepareScan;
self->WriteHeader = ScriptWriteHeader;
self->WriteScanPoints = ScriptWriteScanPoints;
self->ScanDrive = ScriptScanDrive;
self->ScanCount = ScriptScanCount;
self->CollectScanData = ScriptScanCollect;
}
/*======================================================================*/
int StandardScanWrapper(SConnection *pCon, SicsInterp *pSics, void *pData,
int argc, char *argv[]){
pScanData self = NULL;
int iPoint;
char pError[132];
if(argc < 4) {
SCWrite(pCon,"ERROR: not enough arguments to stdscan",eError);
return 0;
}
strtolower(argv[1]);
self = (pScanData)FindCommandData(pSics,argv[2],"ScanObject");
assert(self);
if(strcmp(argv[1],"writeheader") == 0){
return WriteHeader(self);
} else if(strcmp(argv[1],"prepare") == 0){
return PrepareScan(self);
}
/*
from here on we need a scan point
*/
if(argc < 5) {
SCWrite(pCon,"ERROR: not enough arguments to stdscan",eError);
return 0;
}
iPoint = atoi(argv[4]);
if(strcmp(argv[1],"drive") == 0){
return ScanDrive(self,iPoint);
} else if(strcmp(argv[1],"count") == 0){
return ScanCount(self,iPoint);
} else if(strcmp(argv[1],"collect") == 0){
return CollectScanData(self,iPoint);
} else if(strcmp(argv[1],"writepoint") == 0){
return WriteScanPoints(self,iPoint);
} else {
snprintf(pError,131,"ERROR: subcommand %s to stdscan not found",
argv[1]);
SCWrite(pCon,pError,eError);
return 0;
}
return 0;
}