168 lines
5.2 KiB
C
168 lines
5.2 KiB
C
/**
|
|
* This is the header file for the new (as of 2007) style SICS objects
|
|
*
|
|
* copyright: see file COPYRIGHT
|
|
*
|
|
* Mark Koennecke, July 2007
|
|
*/
|
|
#include <sics.h>
|
|
#include "assert.h"
|
|
#include "ifile.h"
|
|
#include "sicsobj.h"
|
|
#include "sicshipadaba.h"
|
|
/*--------------------------------------------------------------------------*/
|
|
static void DefaultKill(void *data){
|
|
return;
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
pSICSOBJ MakeSICSOBJ(char *name, char *class){
|
|
pSICSOBJ pNew = NULL;
|
|
|
|
pNew = (pSICSOBJ)malloc(sizeof(SICSOBJ));
|
|
if(pNew == NULL){
|
|
return NULL;
|
|
}
|
|
memset(pNew,0,sizeof(SICSOBJ));
|
|
pNew->pDes = CreateDescriptor(class);
|
|
pNew->objectNode = MakeHipadabaNode(name, HIPNONE, 1);
|
|
if(pNew->pDes == NULL || pNew->objectNode == NULL){
|
|
free(pNew);
|
|
return(NULL);
|
|
}
|
|
pNew->pDes->parNode = pNew->objectNode;
|
|
pNew->KillPrivate = DefaultKill;
|
|
return pNew;
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
void KillSICSOBJ(void *data){
|
|
pSICSOBJ self = (pSICSOBJ)data;
|
|
if(self == NULL){
|
|
return;
|
|
}
|
|
if(self->pDes != NULL){
|
|
DeleteDescriptor(self->pDes);
|
|
}
|
|
if(self->KillPrivate != NULL && self->pPrivate != NULL){
|
|
self->KillPrivate(self->pPrivate);
|
|
}
|
|
free(self);
|
|
}
|
|
/*===========================================================================*/
|
|
static int assignPar(pHdb node, SConnection *pCon, char *data){
|
|
char error[132], buffer[256];
|
|
int status;
|
|
|
|
status = readHdbValue(&node->value,data, error, 132);
|
|
if(status != 1){
|
|
snprintf(buffer,255,"ERROR: error parsing %s: %s",
|
|
node->name, error);
|
|
SCWrite(pCon,buffer,eError);
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
static int invokeOBJFunction(pSICSOBJ object, pHdb commandNode, SConnection *pCon,
|
|
int argc, char *argv[]){
|
|
int status, i, count = 0;
|
|
pHdb currentPar = NULL;
|
|
SICSOBJFunc pFunc = NULL;
|
|
pHdb parArray[64];
|
|
|
|
/*
|
|
* assign parameters and fill parameter array for function at the same
|
|
* time. Be lenient about missing parameters: Then the old values will
|
|
* be used.
|
|
*/
|
|
for(i = 0, currentPar = commandNode->child;
|
|
i < argc && currentPar != NULL;
|
|
i++, currentPar = currentPar->next){
|
|
if(argv[i] != NULL){
|
|
status = assignPar(currentPar,pCon, argv[i]);
|
|
}
|
|
if(status != 1){
|
|
return status;
|
|
}
|
|
parArray[i] = currentPar;
|
|
count++;
|
|
}
|
|
|
|
pFunc = (SICSOBJFunc)commandNode->value.v.obj;
|
|
if(pFunc == NULL){
|
|
SCWrite(pCon,"ERROR: internal error, function not found",eError);
|
|
return 0;
|
|
}
|
|
status = pFunc(object, pCon, parArray,count);
|
|
return status;
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
int InvokeSICSOBJ(SConnection *pCon, SicsInterp *pSics, void *pData,
|
|
int argc, char *argv[]){
|
|
pSICSOBJ self = NULL;
|
|
int status;
|
|
pHdb parNode;
|
|
char buffer[132];
|
|
|
|
self = (pSICSOBJ)pData;
|
|
assert(self != NULL);
|
|
|
|
if(argc < 1){
|
|
SCWrite(pCon,"ERROR: Nothing to process",eError);
|
|
return -1;
|
|
}
|
|
parNode = GetHipadabaNode(self->objectNode,argv[1]);
|
|
if(parNode != NULL && parNode->value.dataType == HIPFUNC){
|
|
status = invokeOBJFunction(self, parNode, pCon, argc-2, &argv[2]);
|
|
} else {
|
|
status = ProcessSICSHdbPar(self->objectNode,pCon, argv[0],
|
|
argc-1,&argv[1]);
|
|
}
|
|
if(status == -1){
|
|
snprintf(buffer,131,"ERROR: no command or parameter found for key: %s",
|
|
argv[1]);
|
|
SCWrite(pCon,buffer,eError);
|
|
status = 0;
|
|
}
|
|
|
|
return status;
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
pSICSOBJ SetupSICSOBJ(SConnection *pCon,SicsInterp *pSics, void *pData,
|
|
int argc, char *argv[]){
|
|
pSICSOBJ pNew = NULL;
|
|
int status;
|
|
|
|
if(argc < 3){
|
|
SCWrite(pCon,"ERROR: not enough arguments to InstallSICSOBJ",eError);
|
|
return NULL;
|
|
}
|
|
pNew = MakeSICSOBJ(argv[1], argv[2]);
|
|
if(pNew == NULL){
|
|
SCWrite(pCon,"ERROR: out of memory creating new SICS object",eError);
|
|
return NULL;
|
|
}
|
|
status = AddCommand(pSics,
|
|
argv[1],
|
|
InvokeSICSOBJ,
|
|
KillSICSOBJ,
|
|
pNew);
|
|
if(status != 1){
|
|
KillSICSOBJ(pNew);
|
|
SCPrintf(pCon,eError,"ERROR: failed create duplicate command %s", argv[1]);
|
|
return NULL;
|
|
}
|
|
return pNew;
|
|
}
|
|
/*---------------------------------------------------------------------------*/
|
|
int InstallSICSOBJ(SConnection *pCon,SicsInterp *pSics, void *pData,
|
|
int argc, char *argv[]){
|
|
pSICSOBJ pNew = NULL;
|
|
|
|
pNew = SetupSICSOBJ(pCon, pSics, pData, argc, argv);
|
|
if(pNew == NULL){
|
|
return 0;
|
|
} else {
|
|
return 1;
|
|
}
|
|
}
|