From 26a6df065245ec2991941bee9f1a14e078842788 Mon Sep 17 00:00:00 2001 From: "W. Eric Norum" Date: Tue, 2 Sep 2003 20:07:18 +0000 Subject: [PATCH] Clean up handling of redefinitions. --- src/iocsh/iocsh.cpp | 48 +++++++++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/src/iocsh/iocsh.cpp b/src/iocsh/iocsh.cpp index 8f3608cb8..72c1703a9 100644 --- a/src/iocsh/iocsh.cpp +++ b/src/iocsh/iocsh.cpp @@ -127,42 +127,48 @@ void epicsShareAPI iocshRegisterVariable (const iocshVarDef *piocshVarDef) { struct iocshVariable *l, *p, *n; int i; - /* var */ + int found; static const iocshArg varArg0 = { "[variable",iocshArgString}; static const iocshArg varArg1 = { "[value]]",iocshArgString}; static const iocshArg *varArgs[2] = {&varArg0, &varArg1}; static const iocshFuncDef varFuncDef = {"var",2,varArgs}; iocshTableLock (); - if (iocshVariableHead == NULL) - iocshRegister(&varFuncDef,varCallFunc); - while((piocshVarDef!=NULL) && (piocshVarDef->name!=NULL) && (*piocshVarDef->name!='\0')) { + while ((piocshVarDef != NULL) + && (piocshVarDef->name != NULL) + && (*piocshVarDef->name != '\0')) { + if (iocshVariableHead == NULL) + iocshRegister(&varFuncDef,varCallFunc); + found = 0; for (l = NULL, p = iocshVariableHead ; p != NULL ; l = p, p = p->next) { i = strcmp (piocshVarDef->name, p->pVarDef->name); if (i == 0) { + errlogPrintf("Warning -- iocshRegisterVariable redefining %s.\n", piocshVarDef->name); p->pVarDef = piocshVarDef; - iocshTableUnlock (); - return; + found = 1; + break; } if (i < 0) break; } - n = (struct iocshVariable *)callocMustSucceed (1, sizeof *n, "iocshRegisterVariable"); - if (!registryAdd(iocshVarID, piocshVarDef->name, (void *)n)) { - free (n); - iocshTableUnlock (); - errlogPrintf ("iocshRegisterVariable failed to add %s\n", piocshVarDef->name); - return; + if (!found) { + n = (struct iocshVariable *)callocMustSucceed(1, sizeof *n, "iocshRegisterVariable"); + if (!registryAdd(iocshVarID, piocshVarDef->name, (void *)n)) { + free(n); + iocshTableUnlock(); + errlogPrintf("iocshRegisterVariable failed to add %s.\n", piocshVarDef->name); + return; + } + if (l == NULL) { + n->next = iocshVariableHead; + iocshVariableHead = n; + } + else { + n->next = l->next; + l->next = n; + } + n->pVarDef = piocshVarDef; } - if (l == NULL) { - n->next = iocshVariableHead; - iocshVariableHead = n; - } - else { - n->next = l->next; - l->next = n; - } - n->pVarDef = piocshVarDef; piocshVarDef++; } iocshTableUnlock ();