- Adapted indenation to new agreed upon system

- Added support for second generation scriptcontext based counter
This commit is contained in:
koennecke
2009-02-13 09:00:03 +00:00
parent a3dcad2bfa
commit 91d4af0541
405 changed files with 88101 additions and 88173 deletions

View File

@ -53,280 +53,270 @@
static int debug = 0;
/*-------------- The data stored for a single callback ------------------*/
typedef struct __CBItem{
long iID;
SICSCallBack pFunc;
void *pUserData;
KillFuncIT pKill;
int iEvent;
int killFlag;
struct __CBItem *next;
} CallBackItem, *pCallBackItem;
typedef struct __CBItem {
long iID;
SICSCallBack pFunc;
void *pUserData;
KillFuncIT pKill;
int iEvent;
int killFlag;
struct __CBItem *next;
} CallBackItem, *pCallBackItem;
/*--------------------- The interface datastructure ---------------------*/
typedef struct __ICallBack {
int iID;
pCallBackItem head;
} ICallBack;
/*------------------------------------------------------------------------*/
static int CheckPointer(pICallBack self)
{
if(self == NULL) return 0;
if(self->iID != CALLBACK)
{
return 0;
}
return 1;
}
/*-------------------------------------------------------------------------*/
typedef struct __ICallBack {
int iID;
pCallBackItem head;
} ICallBack;
pICallBack CreateCallBackInterface(void)
{
pICallBack pNew = NULL;
pNew = (pICallBack)malloc(sizeof(ICallBack));
if(!pNew)
{
return 0;
}
pNew->iID = CALLBACK;
pNew->head = NULL;
return pNew;
}
/*--------------------------------------------------------------------------*/
void DeleteCallBackInterface(pICallBack self)
{
int iRet;
pCallBackItem pItem, pTmp;
if(!CheckPointer(self))
{
return;
}
/* kill all userdata associated with callbacks */
pItem = self->head;
while(pItem != NULL)
{
pTmp = pItem->next;
if(pItem->pKill != NULL)
{
pItem->pKill(pItem->pUserData);
}
free(pItem);
pItem = pTmp;
}
free(self);
}
/*--------------------------------------------------------------------------*/
static void markUserdata4Kill(pICallBack self, void *pData)
{
pCallBackItem pItem = NULL;
pItem = self->head;
while(pItem != NULL){
if(pData != NULL && pItem->pUserData == pData)
{
pItem->killFlag = 1;
}
pItem = pItem->next;
}
}
/*-------------------------------------------------------------------------*/
static void cleanCallbackList(pICallBack self)
{
pCallBackItem toKill, current;
/*
* killing at the head
*/
while(self->head != NULL && self->head->killFlag == 0){
toKill = self->head;
self->head = toKill->next;
if(toKill->pKill != NULL){
toKill->pKill(toKill->pUserData);
}
free(toKill);
}
if(self->head == NULL){
return;
}
/*
* killing in the middle and the end
*/
current = self->head;
while(current->next != NULL){
if(current->next->killFlag == 1){
toKill = current->next;
current->next = toKill->next;
if(toKill->pKill != NULL){
toKill->pKill(toKill->pUserData);
}
free(toKill);
} else {
current = current->next;
}
}
}
/*--------------------------------------------------------------------------*/
int InvokeCallBack(pICallBack self, int iEvent, void *pEventData)
{
pCallBackItem pItem;
int iCurrent, iRet;
int iResult = 1, iKill = 0;;
if(!CheckPointer(self))
{
return 0;
}
pItem = self->head;
while(pItem != NULL)
{
if(pItem->iEvent == iEvent && pItem->killFlag == 0)
{
iRet = pItem->pFunc(iEvent, pEventData,pItem->pUserData);
if(iRet < 0)
{
pItem->killFlag = 1;
if(pItem->pUserData != NULL)
{
markUserdata4Kill(self, pItem->pUserData);
iKill = 1;
}
}
else if(iRet != 1)
{
iResult = 0;
}
}
pItem = pItem->next;
}
/* kill run */
if(iKill == 1){
cleanCallbackList(self);
}
return iResult;
}
/*--------------------------------------------------------------------------*/
static long lCount = 1L;
long RegisterCallback(pICallBack self, int iEvent,
SICSCallBack pFunc,
void *pUserData, KillFunc pKFunc)
{
pCallBackItem pItem = NULL;
if(!CheckPointer(self))
{
return 0;
}
pItem = calloc(1,sizeof(CallBackItem));
if(pItem == NULL){
return -1;
}
pItem->iID = lCount++;
assert(pFunc);
pItem->pFunc = pFunc;
pItem->iEvent = iEvent;
pItem->pUserData = pUserData;
pItem->pKill = pKFunc;
pItem->killFlag = 0;
pItem->next = self->head;
self->head = pItem;
if(debug){
printf("Registered callback at %p\n",self);
}
return pItem->iID;
}
/*------------------------------------------------------------------------*/
static void markById(pICallBack self, int lID)
static int CheckPointer(pICallBack self)
{
pCallBackItem pItem = NULL;
pItem = self->head;
while(pItem != NULL)
{
if(pItem->iID == lID)
{
pItem->killFlag = 1;
}
pItem = pItem->next;
}
}
/*-------------------------------------------------------------------------*/
int RemoveCallback(pICallBack self, long lID)
{
CallBackItem sItem;
int iCurrent;
if(!CheckPointer(self))
{
return 0;
}
markById(self,lID);
cleanCallbackList(self);
return 0;
}
/*--------------------------------------------------------------------------*/
int RemoveCallback2(pICallBack self, void *pUserData)
{
CallBackItem sItem;
int iCurrent;
if(!CheckPointer(self))
{
return 0;
}
markUserdata4Kill(self,pUserData);
cleanCallbackList(self);
return 1;
}
/*--------------------------------------------------------------------------*/
int RemoveCallbackCon(pICallBack self, SConnection *con)
{
pCallBackItem pItem;
SConnection *tst = NULL;
if(!CheckPointer(self))
{
return 0;
}
pItem = self->head;
while(pItem != NULL)
{
tst = (SConnection *)pItem->pUserData;
if(VerifyConnection(tst) && tst->ident == con->ident)
{
if(debug){
printf("Killing callback on connection.ident = %ld\n", con->ident);
}
pItem->killFlag = 1;
}
pItem = pItem->next;
}
cleanCallbackList(self);
return 1;
}
/*-------------------------------------------------------------------
a write function for the connection which writes to stdout
-------------------------------------------------------------------*/
static int CallbackWrite(SConnection *pCon,char *message, int outCode)
{
if(outCode >= eWarning)
{
fputs(message,stdout);
fputs("\n",stdout);
if (self == NULL)
return 0;
if (self->iID != CALLBACK) {
return 0;
}
return 1;
}
/*-------------------------------------------------------------------------*/
pICallBack CreateCallBackInterface(void)
{
pICallBack pNew = NULL;
pNew = (pICallBack) malloc(sizeof(ICallBack));
if (!pNew) {
return 0;
}
pNew->iID = CALLBACK;
pNew->head = NULL;
return pNew;
}
/*--------------------------------------------------------------------------*/
void DeleteCallBackInterface(pICallBack self)
{
int iRet;
pCallBackItem pItem, pTmp;
if (!CheckPointer(self)) {
return;
}
/* kill all userdata associated with callbacks */
pItem = self->head;
while (pItem != NULL) {
pTmp = pItem->next;
if (pItem->pKill != NULL) {
pItem->pKill(pItem->pUserData);
}
free(pItem);
pItem = pTmp;
}
free(self);
}
/*--------------------------------------------------------------------------*/
static void markUserdata4Kill(pICallBack self, void *pData)
{
pCallBackItem pItem = NULL;
pItem = self->head;
while (pItem != NULL) {
if (pData != NULL && pItem->pUserData == pData) {
pItem->killFlag = 1;
}
pItem = pItem->next;
}
}
/*-------------------------------------------------------------------------*/
static void cleanCallbackList(pICallBack self)
{
pCallBackItem toKill, current;
/*
* killing at the head
*/
while (self->head != NULL && self->head->killFlag == 0) {
toKill = self->head;
self->head = toKill->next;
if (toKill->pKill != NULL) {
toKill->pKill(toKill->pUserData);
}
free(toKill);
}
if (self->head == NULL) {
return;
}
/*
* killing in the middle and the end
*/
current = self->head;
while (current->next != NULL) {
if (current->next->killFlag == 1) {
toKill = current->next;
current->next = toKill->next;
if (toKill->pKill != NULL) {
toKill->pKill(toKill->pUserData);
}
free(toKill);
} else {
current = current->next;
}
}
}
/*--------------------------------------------------------------------------*/
int InvokeCallBack(pICallBack self, int iEvent, void *pEventData)
{
pCallBackItem pItem;
int iCurrent, iRet;
int iResult = 1, iKill = 0;;
if (!CheckPointer(self)) {
return 0;
}
pItem = self->head;
while (pItem != NULL) {
if (pItem->iEvent == iEvent && pItem->killFlag == 0) {
iRet = pItem->pFunc(iEvent, pEventData, pItem->pUserData);
if (iRet < 0) {
pItem->killFlag = 1;
if (pItem->pUserData != NULL) {
markUserdata4Kill(self, pItem->pUserData);
iKill = 1;
}
} else if (iRet != 1) {
iResult = 0;
}
}
pItem = pItem->next;
}
/* kill run */
if (iKill == 1) {
cleanCallbackList(self);
}
return iResult;
}
/*--------------------------------------------------------------------------*/
static long lCount = 1L;
long RegisterCallback(pICallBack self, int iEvent,
SICSCallBack pFunc, void *pUserData, KillFunc pKFunc)
{
pCallBackItem pItem = NULL;
if (!CheckPointer(self)) {
return 0;
}
pItem = calloc(1, sizeof(CallBackItem));
if (pItem == NULL) {
return -1;
}
pItem->iID = lCount++;
assert(pFunc);
pItem->pFunc = pFunc;
pItem->iEvent = iEvent;
pItem->pUserData = pUserData;
pItem->pKill = pKFunc;
pItem->killFlag = 0;
pItem->next = self->head;
self->head = pItem;
if (debug) {
printf("Registered callback at %p\n", self);
}
return pItem->iID;
}
/*------------------------------------------------------------------------*/
static void markById(pICallBack self, int lID)
{
pCallBackItem pItem = NULL;
pItem = self->head;
while (pItem != NULL) {
if (pItem->iID == lID) {
pItem->killFlag = 1;
}
pItem = pItem->next;
}
}
/*-------------------------------------------------------------------------*/
int RemoveCallback(pICallBack self, long lID)
{
CallBackItem sItem;
int iCurrent;
if (!CheckPointer(self)) {
return 0;
}
markById(self, lID);
cleanCallbackList(self);
return 0;
}
/*--------------------------------------------------------------------------*/
int RemoveCallback2(pICallBack self, void *pUserData)
{
CallBackItem sItem;
int iCurrent;
if (!CheckPointer(self)) {
return 0;
}
markUserdata4Kill(self, pUserData);
cleanCallbackList(self);
return 1;
}
/*--------------------------------------------------------------------------*/
int RemoveCallbackCon(pICallBack self, SConnection * con)
{
pCallBackItem pItem;
SConnection *tst = NULL;
if (!CheckPointer(self)) {
return 0;
}
pItem = self->head;
while (pItem != NULL) {
tst = (SConnection *) pItem->pUserData;
if (VerifyConnection(tst) && tst->ident == con->ident) {
if (debug) {
printf("Killing callback on connection.ident = %ld\n", con->ident);
}
pItem->killFlag = 1;
}
pItem = pItem->next;
}
cleanCallbackList(self);
return 1;
}
/*-------------------------------------------------------------------
a write function for the connection which writes to stdout
-------------------------------------------------------------------*/
static int CallbackWrite(SConnection * pCon, char *message, int outCode)
{
if (outCode >= eWarning) {
fputs(message, stdout);
fputs("\n", stdout);
}
return 1;
}
/*-----------------------------------------------------------------------
the actual callback function invoking the script
------------------------------------------------------------------------*/
@ -337,25 +327,24 @@ static int ScriptCallback(int iEvent, void *pEventData, void *pUserData)
int status;
pCon = SCCreateDummyConnection(pServ->pSics);
if(!pCon)
{
fprintf(stdout,"ERROR: failed to create dummy connection\n");
if (!pCon) {
fprintf(stdout, "ERROR: failed to create dummy connection\n");
return 0;
}
if(pUserData == NULL)
{
fprintf(stdout,"ERROR: ScriptCallback: no script to execute\n");
if (pUserData == NULL) {
fprintf(stdout, "ERROR: ScriptCallback: no script to execute\n");
return 0;
}
SCSetRights(pCon,usInternal);
status = InterpExecute(pServ->pSics,pCon,(char *)pUserData);
SCSetRights(pCon, usInternal);
status = InterpExecute(pServ->pSics, pCon, (char *) pUserData);
SCDeleteConnection(pCon);
return 1;
}
/*------------------------------------------------------------------------*/
int CallbackScript(SConnection *pCon, SicsInterp *pSics, void *pData,
int argc, char *argv[])
int CallbackScript(SConnection * pCon, SicsInterp * pSics, void *pData,
int argc, char *argv[])
{
long lID;
int iEvent, status;
@ -363,88 +352,76 @@ int CallbackScript(SConnection *pCon, SicsInterp *pSics, void *pData,
CommandList *pCom = NULL;
char pBuffer[132];
if(argc < 2)
{
SCWrite(pCon,"ERROR: insufficient number of arguments to callbackScript",
eError);
if (argc < 2) {
SCWrite(pCon,
"ERROR: insufficient number of arguments to callbackScript",
eError);
return 0;
}
/*
only managers may do this
*/
if(!SCMatchRights(pCon,usMugger))
{
only managers may do this
*/
if (!SCMatchRights(pCon, usMugger)) {
return 0;
}
strtolower(argv[1]);
if(strcmp(argv[1],"connect") == 0)
{
if(argc < 4)
{
SCWrite(pCon,"ERROR: not enough arguments to CallbackScript connect",
eError);
if (strcmp(argv[1], "connect") == 0) {
if (argc < 4) {
SCWrite(pCon,
"ERROR: not enough arguments to CallbackScript connect",
eError);
return 0;
}
strtolower(argv[2]);
pCom = FindCommand(pSics,argv[2]);
if(!pCom)
{
SCWrite(pCon,"ERROR: object to connect to not found",eError);
pCom = FindCommand(pSics, argv[2]);
if (!pCom) {
SCWrite(pCon, "ERROR: object to connect to not found", eError);
return 0;
}
pCall = GetCallbackInterface(pCom->pData);
if(!pCall)
{
SCWrite(pCon,"ERROR: object has no callback interface",eError);
if (!pCall) {
SCWrite(pCon, "ERROR: object has no callback interface", eError);
return 0;
}
iEvent = Text2Event(argv[3]);
if(iEvent < 0)
{
SCWrite(pCon,"ERROR: event type not known",eError);
if (iEvent < 0) {
SCWrite(pCon, "ERROR: event type not known", eError);
return 0;
}
Arg2Text(argc-4,&argv[4],pBuffer,131);
Arg2Text(argc - 4, &argv[4], pBuffer, 131);
lID = RegisterCallback(pCall,
iEvent,ScriptCallback,
strdup(pBuffer),free);
sprintf(pBuffer,"callback = %ld", lID);
SCWrite(pCon,pBuffer,eValue);
iEvent, ScriptCallback, strdup(pBuffer), free);
sprintf(pBuffer, "callback = %ld", lID);
SCWrite(pCon, pBuffer, eValue);
return 1;
}
else if(strcmp(argv[1],"remove") == 0)
{
if(argc < 4)
{
SCWrite(pCon,"ERROR: not enough arguments to CallbackScript remove",
eError);
} else if (strcmp(argv[1], "remove") == 0) {
if (argc < 4) {
SCWrite(pCon, "ERROR: not enough arguments to CallbackScript remove",
eError);
return 0;
}
strtolower(argv[2]);
pCom = FindCommand(pSics,argv[2]);
if(!pCom)
{
SCWrite(pCon,"ERROR: object to remove to not found",eError);
pCom = FindCommand(pSics, argv[2]);
if (!pCom) {
SCWrite(pCon, "ERROR: object to remove to not found", eError);
return 0;
}
pCall = GetCallbackInterface(pCom->pData);
if(!pCall)
{
SCWrite(pCon,"ERROR: object has no callback interface",eError);
if (!pCall) {
SCWrite(pCon, "ERROR: object has no callback interface", eError);
return 0;
}
status = Tcl_GetInt(InterpGetTcl(pSics),argv[3],&iEvent);
if(status != TCL_OK)
{
SCWrite(pCon,"ERROR: failed to convert callback ID to int",eError);
status = Tcl_GetInt(InterpGetTcl(pSics), argv[3], &iEvent);
if (status != TCL_OK) {
SCWrite(pCon, "ERROR: failed to convert callback ID to int", eError);
return 0;
}
RemoveCallback(pCall,(long)iEvent);
RemoveCallback(pCall, (long) iEvent);
SCSendOK(pCon);
return 1;
}
SCWrite(pCon,"ERROR: subcommand to CallbackScript not known",eError);
SCWrite(pCon, "ERROR: subcommand to CallbackScript not known", eError);
return 0;
}