PSI sics-cvs-psi_pre-ansto
This commit is contained in:
727
nxscript.c
Normal file
727
nxscript.c
Normal file
@@ -0,0 +1,727 @@
|
||||
/*-----------------------------------------------------------------------
|
||||
N X S C R I P T
|
||||
|
||||
This is a class for scripting the contents of NeXus files from
|
||||
SICS.
|
||||
|
||||
copyright: see file COPYRIGHT
|
||||
|
||||
Mark Koennecke, February 2003
|
||||
------------------------------------------------------------------------*/
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <tcl.h>
|
||||
#include <unistd.h>
|
||||
#include "fortify.h"
|
||||
#include "sics.h"
|
||||
#include "splitter.h"
|
||||
#include "HistMem.h"
|
||||
#include "motor.h"
|
||||
#include "counter.h"
|
||||
#include "udpquieck.h"
|
||||
#include "nxdict.h"
|
||||
#include "nxscript.h"
|
||||
/*============== a personal data structure ============================*/
|
||||
typedef struct {
|
||||
pObjectDescriptor pDes;
|
||||
NXhandle fileHandle;
|
||||
NXdict dictHandle;
|
||||
} NXScript, *pNXScript;
|
||||
/*======================== Action =======================================*/
|
||||
static int handleFileOperations(SConnection *pCon, pNXScript self,
|
||||
int argc, char *argv[]){
|
||||
int status,i, iVal;
|
||||
NXaccess access;
|
||||
char buffer[512];
|
||||
|
||||
if(strcmp(argv[1],"close") == 0){
|
||||
/*
|
||||
close everything! and send a message to trigger file synchronisation
|
||||
to the central server
|
||||
*/
|
||||
i = 511;
|
||||
iVal = NX_CHAR;
|
||||
NXgetattr(self->fileHandle,"file_name",buffer,&i,&iVal);
|
||||
NXclose(&self->fileHandle);
|
||||
NXDclose(self->dictHandle,NULL);
|
||||
self->fileHandle = NULL;
|
||||
self->dictHandle = NULL;
|
||||
SendQuieck(QUIECK,buffer);
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
} else if(strcmp(argv[1],"reopen") == 0){
|
||||
access = NXACC_RDWR;
|
||||
} else if(strcmp(argv[1],"create4") == 0){
|
||||
access = NXACC_CREATE4;
|
||||
} else if(strcmp(argv[1],"create5") == 0){
|
||||
access = NXACC_CREATE5;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
if(argc < 4){
|
||||
SCWrite(pCon,"ERROR: insufficient number of arguments for file operation",
|
||||
eError);
|
||||
return 1;
|
||||
}
|
||||
/*
|
||||
be considerate: close files left open
|
||||
*/
|
||||
if(self->fileHandle != NULL){
|
||||
NXclose(&self->fileHandle);
|
||||
self->fileHandle = NULL;
|
||||
}
|
||||
if(self->dictHandle != NULL){
|
||||
NXDclose(self->dictHandle, NULL);
|
||||
self->dictHandle = NULL;
|
||||
}
|
||||
/*
|
||||
now initialize ourselves
|
||||
*/
|
||||
unlink(argv[2]); /* kill file for overwrite */
|
||||
status = NXopen(argv[2],access,&self->fileHandle);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to open %s",argv[2]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
status = NXDinitfromfile(argv[3],&self->dictHandle);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to open dictionary %s",argv[3]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
SCSendOK(pCon);
|
||||
return 1;
|
||||
}
|
||||
/*----------------------------------------------------------------------*/
|
||||
static void putMotor(SConnection *pCon, SicsInterp *pSics, pNXScript self,
|
||||
int argc, char *argv[]){
|
||||
int status;
|
||||
pMotor brumm = NULL;
|
||||
float fVal;
|
||||
char buffer[132], dummy[256];
|
||||
|
||||
if(argc < 4){
|
||||
SCWrite(pCon,"ERROR: insufficient number of arguments to putmotor",
|
||||
eError);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
find motor
|
||||
*/
|
||||
brumm = (pMotor)FindCommandData(pSics,argv[3],"Motor");
|
||||
if(!brumm){
|
||||
sprintf(buffer,"ERROR: motor %s not found!", argv[3]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
do position
|
||||
*/
|
||||
status = MotorGetSoftPosition(brumm, pCon,&fVal);
|
||||
if(!status){
|
||||
sprintf(buffer,"ERROR: failed to read position of %s", argv[3]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
status = NXDputalias(self->fileHandle,self->dictHandle,argv[2],&fVal);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to write %s with alias %s",
|
||||
argv[3],argv[2]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
if alias_null is available: write zero point
|
||||
*/
|
||||
strcpy(buffer,argv[2]);
|
||||
strcat(buffer,"_null");
|
||||
if(NXDget(self->dictHandle,buffer,dummy,255)){
|
||||
MotorGetPar(brumm,"softzero",&fVal);
|
||||
status = NXDputalias(self->fileHandle,self->dictHandle,buffer, &fVal);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to write %s zero with alias %s",
|
||||
argv[3],argv[2]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*---------------------------------------------------------------------*/
|
||||
static void putCounter(SConnection *pCon, SicsInterp *pSics, pNXScript self,
|
||||
int argc, char *argv[]){
|
||||
pCounter cter = NULL;
|
||||
float fVal;
|
||||
long counts;
|
||||
char buffer[256], newAlias[256], dummy[80];
|
||||
int status, i, icounts;
|
||||
CounterMode eMode;
|
||||
|
||||
if(argc < 4){
|
||||
SCWrite(pCon,"ERROR: insufficient number of arguments to putcounter",
|
||||
eError);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
find counter
|
||||
*/
|
||||
cter = (pCounter)FindCommandData(pSics,argv[3],"SingleCounter");
|
||||
if(!cter){
|
||||
sprintf(buffer,"ERROR: counter %s not found!", argv[3]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
do preset
|
||||
*/
|
||||
fVal = GetCounterPreset(cter);
|
||||
strcpy(newAlias,argv[2]);
|
||||
strcat(newAlias,"_preset");
|
||||
status = NXDputalias(self->fileHandle,self->dictHandle,newAlias,&fVal);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to write preset to %s", newAlias);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
|
||||
/*
|
||||
do countmode
|
||||
*/
|
||||
eMode = GetCounterMode(cter);
|
||||
strcpy(newAlias,argv[2]);
|
||||
strcat(newAlias,"_mode");
|
||||
if(eMode == eTimer){
|
||||
strcpy(dummy,"timer");
|
||||
} else {
|
||||
strcpy(dummy,"monitor");
|
||||
}
|
||||
status = NXDputalias(self->fileHandle,self->dictHandle,newAlias,dummy);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to write counter mode to %s", newAlias);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
|
||||
/*
|
||||
do time
|
||||
*/
|
||||
fVal = GetCountTime(cter,pCon);
|
||||
strcpy(newAlias,argv[2]);
|
||||
strcat(newAlias,"_time");
|
||||
if(NXDget(self->dictHandle,newAlias,dummy,79)){
|
||||
status = NXDputalias(self->fileHandle,self->dictHandle,newAlias,&fVal);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to write count time to %s", newAlias);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
do counter and monitors
|
||||
*/
|
||||
for(i = 0; i < 10; i++){
|
||||
sprintf(newAlias,"%s_%2.2d",argv[2],i);
|
||||
if(NXDget(self->dictHandle,newAlias,dummy,79)){
|
||||
counts = GetMonitor(cter,i,pCon);
|
||||
icounts = (int)counts;
|
||||
status = NXDputalias(self->fileHandle,self->dictHandle,newAlias,
|
||||
&icounts);
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
/*----------------------------------------------------------------------
|
||||
The sequence of things is important in here: The code for updating
|
||||
the dimensions variables also applies the time binning to the length.
|
||||
Thus subsets can only be checked for after that. And then we can allocate
|
||||
memory.
|
||||
-------------------------------------------------------------------------*/
|
||||
static void putHistogramMemory(SConnection *pCon, SicsInterp *pSics,
|
||||
pNXScript self,
|
||||
int argc, char *argv[]){
|
||||
pHistMem mem = NULL;
|
||||
int status, start, length, iDim[MAXDIM], rank, i, subset = 0;
|
||||
HistInt *iData = NULL;
|
||||
char buffer[256], dummy[40], value[20];
|
||||
const float *timeBin;
|
||||
int timeLength;
|
||||
|
||||
if(argc < 4){
|
||||
SCWrite(pCon,"ERROR: insufficient number of arguments to puthm",
|
||||
eError);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
find Histogram Memory
|
||||
*/
|
||||
mem = (pHistMem)FindCommandData(pSics,argv[3],"HistMem");
|
||||
if(!mem){
|
||||
sprintf(buffer,"ERROR: HistMem %s not found!", argv[3]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
default: everything
|
||||
*/
|
||||
start = 0;
|
||||
length = GetHistLength(mem);
|
||||
|
||||
/*
|
||||
update the dimension variables in the dictionary
|
||||
*/
|
||||
GetHistDim(mem,iDim,&rank);
|
||||
for(i = 0; i < rank; i++){
|
||||
sprintf(dummy,"dim%1.1d", i);
|
||||
sprintf(value,"%d",iDim[i]);
|
||||
status = NXDupdate(self->dictHandle,dummy,value);
|
||||
if(status == 0) {
|
||||
NXDadd(self->dictHandle,dummy,value);
|
||||
}
|
||||
}
|
||||
timeBin = GetHistTimeBin(mem,&timeLength);
|
||||
if(timeLength > 2){
|
||||
sprintf(dummy,"%d",timeLength);
|
||||
status = NXDupdate(self->dictHandle,"timedim",dummy);
|
||||
if(status == 0) {
|
||||
NXDadd(self->dictHandle,"timedim",dummy);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
check for further arguments specifying a subset
|
||||
*/
|
||||
if(argc > 5){
|
||||
subset = 1;
|
||||
status = Tcl_GetInt(InterpGetTcl(pSics),argv[4],&start);
|
||||
if(status != TCL_OK){
|
||||
sprintf(buffer,"ERROR: failed to convert %s to integer",
|
||||
argv[4]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
status = Tcl_GetInt(InterpGetTcl(pSics),argv[5],&length);
|
||||
if(status != TCL_OK){
|
||||
sprintf(buffer,"ERROR: failed to convert %s to integer",
|
||||
argv[5]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
read HM
|
||||
*/
|
||||
if(subset){
|
||||
iData = (HistInt *)malloc(length*sizeof(HistInt));
|
||||
if(!iData){
|
||||
SCWrite(pCon,"ERROR: out of memory for reading histogram memory",
|
||||
eError);
|
||||
return;
|
||||
}
|
||||
memset(iData,0,length*sizeof(HistInt));
|
||||
status = GetHistogramDirect(mem,pCon,0,start,start+length,iData,
|
||||
length*sizeof(HistInt));
|
||||
}else{
|
||||
/*
|
||||
status = GetHistogram(mem,pCon,0,start,length,iData,
|
||||
length*sizeof(HistInt));
|
||||
*/
|
||||
iData = GetHistogramPointer(mem,pCon);
|
||||
if(iData == NULL){
|
||||
status = 0;
|
||||
} else {
|
||||
status = 1;
|
||||
}
|
||||
}
|
||||
if(!status){
|
||||
SCWrite(pCon,"ERROR: failed to read histogram memory",eError);
|
||||
if(subset){
|
||||
free(iData);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
finally: write
|
||||
*/
|
||||
status = NXDputalias(self->fileHandle, self->dictHandle,argv[2],iData);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to write histogram memory data");
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
|
||||
if(subset){
|
||||
free(iData);
|
||||
}
|
||||
SCSendOK(pCon);
|
||||
|
||||
return;
|
||||
}
|
||||
/*-------------------------------------------------------------------*/
|
||||
static void putTimeBinning(SConnection *pCon, SicsInterp *pSics,
|
||||
pNXScript self,
|
||||
int argc, char *argv[]){
|
||||
pHistMem mem = NULL;
|
||||
int status, timeLength;
|
||||
char buffer[256], defString[512], dummy[40];
|
||||
const float *timeBin;
|
||||
|
||||
if(argc < 4){
|
||||
SCWrite(pCon,"ERROR: insufficient number of arguments to puttimebinning",
|
||||
eError);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
find Histogram Memory
|
||||
*/
|
||||
mem = (pHistMem)FindCommandData(pSics,argv[3],"HistMem");
|
||||
if(!mem){
|
||||
sprintf(buffer,"ERROR: HistMem %s not found!", argv[3]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
build definition string
|
||||
*/
|
||||
status = NXDget(self->dictHandle,argv[2],buffer,254);
|
||||
if(!status){
|
||||
sprintf(buffer,"ERROR: alias %s for time binning not found",
|
||||
argv[2]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
timeBin = GetHistTimeBin(mem,&timeLength);
|
||||
sprintf(defString,"%s -dim {%d} ",buffer,timeLength);
|
||||
|
||||
/*
|
||||
write
|
||||
*/
|
||||
status = NXDputdef(self->fileHandle, self->dictHandle,
|
||||
defString,(void *)timeBin);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to write time binning");
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
SCSendOK(pCon);
|
||||
return;
|
||||
}
|
||||
/*----------------------------------------------------------------------*/
|
||||
static void putArray(SConnection *pCon, SicsInterp *pSics,
|
||||
pNXScript self,
|
||||
int argc, char *argv[]){
|
||||
float *data = NULL;
|
||||
int length, i, status;
|
||||
char num[20];
|
||||
char buffer[256], defString[512], *varData;
|
||||
Tcl_Interp *tcl = NULL;
|
||||
double dVal;
|
||||
|
||||
if(argc < 5){
|
||||
SCWrite(pCon,"ERROR: insufficient number of arguments to array",
|
||||
eError);
|
||||
return;
|
||||
}
|
||||
tcl = InterpGetTcl(pSics);
|
||||
assert(tcl != NULL);
|
||||
|
||||
/*
|
||||
get array length
|
||||
*/
|
||||
status = Tcl_GetInt(tcl,argv[4],&length);
|
||||
if(status = TCL_OK){
|
||||
sprintf(buffer,"ERROR: failed to convert %s to integer",argv[4]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return;
|
||||
}
|
||||
|
||||
/*
|
||||
allocate
|
||||
*/
|
||||
if(length > 0){
|
||||
data = (float *)malloc(length*sizeof(float));
|
||||
}
|
||||
if(data == NULL){
|
||||
SCWrite(pCon,"ERROR: out of memory or invalid length",eError);
|
||||
return;
|
||||
}
|
||||
memset(data,0,length*sizeof(float));
|
||||
|
||||
/*
|
||||
try getting data
|
||||
*/
|
||||
for(i = 0; i < length; i++){
|
||||
sprintf(num,"%d",i);
|
||||
varData = Tcl_GetVar2(tcl,argv[3],num,0);
|
||||
if(varData != NULL){
|
||||
status = Tcl_GetDouble(tcl,varData,&dVal);
|
||||
if(status = TCL_OK){
|
||||
sprintf(buffer,"ERROR: failed to convert %s to double",
|
||||
varData);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
data[i] = (float)dVal;
|
||||
} else {
|
||||
sprintf(buffer,"WARNING: failed to find array element %d", i);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
build definition string
|
||||
*/
|
||||
status = NXDget(self->dictHandle,argv[2],buffer,254);
|
||||
if(!status){
|
||||
sprintf(buffer,"ERROR: alias %s for array not found",
|
||||
argv[2]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
free(data);
|
||||
return;
|
||||
}
|
||||
sprintf(defString,"%s -dim {%d} ",buffer,length);
|
||||
|
||||
/*
|
||||
write it!
|
||||
*/
|
||||
status = NXDputdef(self->fileHandle,self->dictHandle,defString,data);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to write array");
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
free(data);
|
||||
SCSendOK(pCon);
|
||||
}
|
||||
/*----------------------------------------------------------------------*/
|
||||
static void putGlobal(SConnection *pCon, SicsInterp *pSics,
|
||||
pNXScript self,
|
||||
int argc, char *argv[]){
|
||||
char value[1024];
|
||||
int status;
|
||||
|
||||
if(argc < 4){
|
||||
SCWrite(pCon,"ERROR: insufficient number of arguments to putglobal",
|
||||
eError);
|
||||
return;
|
||||
}
|
||||
|
||||
Arg2Text(argc-3,&argv[3],value,1023);
|
||||
status = NXputattr(self->fileHandle,argv[2],value,strlen(value),
|
||||
NX_CHAR);
|
||||
if(status != NX_OK){
|
||||
SCWrite(pCon,"ERROR: failed to write attribute",eError);
|
||||
}
|
||||
SCSendOK(pCon);
|
||||
}
|
||||
/*-----------------------------------------------------------------------*/
|
||||
static int handlePut(SConnection *pCon, SicsInterp *pSics, pNXScript self,
|
||||
int argc, char *argv[]){
|
||||
int status;
|
||||
char buffer[1024], defString[1024], numBuf[25];
|
||||
double dVal;
|
||||
float fVal;
|
||||
|
||||
/*============ */
|
||||
if(strcmp(argv[1],"putfloat") == 0){
|
||||
if(argc < 4){
|
||||
SCWrite(pCon,"ERROR: insufficient number of arguments to putfloat",
|
||||
eError);
|
||||
return 1;
|
||||
}
|
||||
status = Tcl_GetDouble(InterpGetTcl(pSics),argv[3],&dVal);
|
||||
if(status != TCL_OK){
|
||||
sprintf(buffer,"ERROR: failed to convert %s to float",
|
||||
argv[3]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return 1;
|
||||
}
|
||||
fVal = (float)dVal;
|
||||
status = NXDputalias(self->fileHandle, self->dictHandle,
|
||||
argv[2],&fVal);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to write %f to alias %s",
|
||||
fVal, argv[2]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
return 1;
|
||||
} else if (strcmp(argv[1],"puttext") == 0){
|
||||
/*====================*/
|
||||
if(argc < 4){
|
||||
SCWrite(pCon,"ERROR: insufficient number of arguments to putfloat",
|
||||
eError);
|
||||
return 1;
|
||||
}
|
||||
Arg2Text(argc-3,&argv[3],buffer,1023);
|
||||
status = NXDget(self->dictHandle,argv[2],defString,1023);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: alias %s not found in puttext",
|
||||
argv[2]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
return 1;
|
||||
}
|
||||
if(strlen(defString) < 900){
|
||||
strcat(defString," -dim {");
|
||||
sprintf(numBuf,"%d",strlen(buffer)+1);
|
||||
strcat(defString,numBuf);
|
||||
strcat(defString," }");
|
||||
} else {
|
||||
SCWrite(pCon,"ERROR: out of definition string space in puttext",
|
||||
eError);
|
||||
return 1;
|
||||
}
|
||||
status = NXDputdef(self->fileHandle,self->dictHandle,
|
||||
defString,buffer);
|
||||
if(status != NX_OK){
|
||||
sprintf(buffer,"ERROR: failed to write alias %s",
|
||||
argv[2]);
|
||||
SCWrite(pCon,buffer,eError);
|
||||
}
|
||||
return 1;
|
||||
} else if(strcmp(argv[1],"putmot") == 0){
|
||||
/*=========== */
|
||||
putMotor(pCon,pSics,self,argc,argv);
|
||||
return 1;
|
||||
} else if(strcmp(argv[1],"putcounter") == 0){
|
||||
/* ================*/
|
||||
putCounter(pCon,pSics,self, argc,argv);
|
||||
return 1;
|
||||
}else if(strcmp(argv[1],"puthm") == 0){
|
||||
/*=================*/
|
||||
putHistogramMemory(pCon,pSics,self,argc, argv);
|
||||
return 1;
|
||||
}else if(strcmp(argv[1],"puttimebinning") == 0){
|
||||
/*=================*/
|
||||
putTimeBinning(pCon,pSics,self,argc,argv);
|
||||
}else if(strcmp(argv[1],"putarray") == 0){
|
||||
/*================*/
|
||||
putArray(pCon,pSics,self,argc,argv);
|
||||
}else if(strcmp(argv[1],"putglobal") == 0){
|
||||
/*===============*/
|
||||
putGlobal(pCon,pSics,self,argc,argv);
|
||||
} else {
|
||||
SCWrite(pCon,"ERROR: put command not recognised",eError);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*----------------------------------------------------------------------*/
|
||||
static void makeLink(SConnection *pCon, SicsInterp *pSics,
|
||||
pNXScript self,
|
||||
int argc, char *argv[]){
|
||||
int status;
|
||||
|
||||
if(argc < 4){
|
||||
SCWrite(pCon,"ERROR: insufficient number of arguments to makelink",
|
||||
eError);
|
||||
return;
|
||||
}
|
||||
|
||||
status = NXDaliaslink(self->fileHandle, self->dictHandle,
|
||||
argv[2],argv[3]);
|
||||
if(status != NX_OK){
|
||||
SCWrite(pCon,"ERROR: linking failed",eError);
|
||||
return;
|
||||
}
|
||||
|
||||
SCSendOK(pCon);
|
||||
}
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
int NXScriptAction(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[]){
|
||||
pNXScript self = (pNXScript)pData;
|
||||
|
||||
/*
|
||||
preliminary checks
|
||||
*/
|
||||
assert(self);
|
||||
if(!SCMatchRights(pCon,usUser)){
|
||||
return 1;
|
||||
}
|
||||
if(argc < 2){
|
||||
SCWrite(pCon,"ERROR: no keyword found",eError);
|
||||
return 1;
|
||||
}
|
||||
strtolower(argv[1]);
|
||||
|
||||
if(handleFileOperations(pCon,self,argc,argv)){
|
||||
return 1;
|
||||
}
|
||||
|
||||
/*
|
||||
if we are here, we can only continue if files are open
|
||||
*/
|
||||
if(self->fileHandle == NULL || self->dictHandle == NULL){
|
||||
SCWrite(pCon,"ERROR: cannot write, files not open",eError);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(strstr(argv[1],"put") != NULL){
|
||||
handlePut(pCon,pSics,self,argc,argv);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(strcmp(argv[1],"makelink") == 0){
|
||||
makeLink(pCon,pSics,self,argc,argv);
|
||||
return 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*=============== make it ==============================================*/
|
||||
static void KillNXScript(void *pData){
|
||||
pNXScript self = (pNXScript)pData;
|
||||
if(self == NULL){
|
||||
return;
|
||||
}
|
||||
if(self->pDes){
|
||||
DeleteDescriptor(self->pDes);
|
||||
}
|
||||
if(self->fileHandle){
|
||||
NXclose(&self->fileHandle);
|
||||
}
|
||||
if(self->dictHandle){
|
||||
NXDclose(self->dictHandle, NULL);
|
||||
}
|
||||
free(self);
|
||||
}
|
||||
/*----------------------------------------------------------------------*/
|
||||
int MakeNXScript(SConnection *pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[]){
|
||||
pNXScript self = NULL;
|
||||
int status;
|
||||
|
||||
self = (pNXScript)malloc(sizeof(NXScript));
|
||||
if(self == NULL){
|
||||
SCWrite(pCon,"ERROR: no memory for NXscript creation",eError);
|
||||
return 0;
|
||||
}
|
||||
memset(self,0,sizeof(NXScript));
|
||||
self->pDes = CreateDescriptor("NXScript");
|
||||
if(self->pDes == NULL){
|
||||
SCWrite(pCon,"ERROR: no memory for NXscript creation",eError);
|
||||
free(self);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
create with with a default name if none specified
|
||||
*/
|
||||
if(argc > 1){
|
||||
status = AddCommand(pSics,argv[1],NXScriptAction,KillNXScript,self);
|
||||
} else {
|
||||
status = AddCommand(pSics,"nxscript",NXScriptAction,KillNXScript,self);
|
||||
}
|
||||
if(!status){
|
||||
SCWrite(pCon,"ERROR: duplicate NXScript object not created",eError);
|
||||
KillNXScript(self);
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user