- Adapted indenation to new agreed upon system
- Added support for second generation scriptcontext based counter
This commit is contained in:
272
definealias.c
272
definealias.c
@ -44,174 +44,156 @@
|
||||
#include "definealias.h"
|
||||
|
||||
typedef struct __Aitem {
|
||||
struct __Aitem *pNext;
|
||||
char *pName;
|
||||
char *pCmd;
|
||||
} AliasItem;
|
||||
struct __Aitem *pNext;
|
||||
char *pName;
|
||||
char *pCmd;
|
||||
} AliasItem;
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
char *TranslateAlias(AliasList *pAList, char *pCmd)
|
||||
{
|
||||
AliasItem *pAlias;
|
||||
char *TranslateAlias(AliasList * pAList, char *pCmd)
|
||||
{
|
||||
AliasItem *pAlias;
|
||||
|
||||
assert(pAList!=NULL && pCmd!=NULL);
|
||||
assert(pAList != NULL && pCmd != NULL);
|
||||
|
||||
pAlias=pAList->pFirst;
|
||||
while (pAlias!=NULL)
|
||||
{
|
||||
if (0 == strcmp(pAlias->pName, pCmd))
|
||||
{
|
||||
pCmd = pAlias->pCmd; /* note that there may be cascaded translations */
|
||||
}
|
||||
pAlias = pAlias->pNext;
|
||||
pAlias = pAList->pFirst;
|
||||
while (pAlias != NULL) {
|
||||
if (0 == strcmp(pAlias->pName, pCmd)) {
|
||||
pCmd = pAlias->pCmd; /* note that there may be cascaded translations */
|
||||
}
|
||||
return(pCmd);
|
||||
pAlias = pAlias->pNext;
|
||||
}
|
||||
return (pCmd);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
int RemoveAlias(AliasList *pAList, char *pCmd)
|
||||
{
|
||||
AliasItem *pAlias = NULL, *pPrev = NULL;
|
||||
int RemoveAlias(AliasList * pAList, char *pCmd)
|
||||
{
|
||||
AliasItem *pAlias = NULL, *pPrev = NULL;
|
||||
|
||||
assert(pAList!=NULL && pCmd!=NULL);
|
||||
assert(pAList != NULL && pCmd != NULL);
|
||||
|
||||
pPrev=(AliasItem *)pAList;
|
||||
pAlias=pAList->pFirst;
|
||||
while (pAlias != NULL && 0 != strcmp(pAlias->pName, pCmd))
|
||||
{
|
||||
pPrev = pAlias;
|
||||
pAlias = pAlias->pNext;
|
||||
}
|
||||
if (pAlias==NULL)
|
||||
{
|
||||
return 0; /* not found */
|
||||
}
|
||||
pPrev = (AliasItem *) pAList;
|
||||
pAlias = pAList->pFirst;
|
||||
while (pAlias != NULL && 0 != strcmp(pAlias->pName, pCmd)) {
|
||||
pPrev = pAlias;
|
||||
pAlias = pAlias->pNext;
|
||||
}
|
||||
if (pAlias == NULL) {
|
||||
return 0; /* not found */
|
||||
}
|
||||
|
||||
/* remove it */
|
||||
pPrev->pNext = pAlias->pNext;
|
||||
/* remove it */
|
||||
pPrev->pNext = pAlias->pNext;
|
||||
free(pAlias->pName);
|
||||
free(pAlias->pCmd);
|
||||
free(pAlias);
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
void FreeAliasList(AliasList * pAList)
|
||||
{
|
||||
AliasItem *pAlias = NULL, *pNext = NULL;
|
||||
|
||||
assert(pAList != NULL);
|
||||
|
||||
pAlias = pAList->pFirst;
|
||||
while (pAlias != NULL) {
|
||||
pNext = pAlias->pNext;
|
||||
free(pAlias->pName);
|
||||
free(pAlias->pCmd);
|
||||
free(pAlias);
|
||||
return(1);
|
||||
}
|
||||
/*------------------------------------------------------------------------*/
|
||||
void FreeAliasList(AliasList *pAList)
|
||||
{
|
||||
AliasItem *pAlias = NULL, *pNext = NULL;
|
||||
|
||||
assert(pAList!=NULL);
|
||||
|
||||
pAlias=pAList->pFirst;
|
||||
while (pAlias != NULL)
|
||||
{
|
||||
pNext=pAlias->pNext;
|
||||
free(pAlias->pName);
|
||||
free(pAlias->pCmd);
|
||||
free(pAlias);
|
||||
pAlias = pNext;
|
||||
}
|
||||
}
|
||||
/*------------------------------------------------------------------------*/
|
||||
char *CreateAlias(AliasList *pAList, char *pName, char *pTranslation)
|
||||
{ /* arguments must be lower case */
|
||||
AliasItem *pAlias = NULL, *pNew = NULL, *pTail = NULL;
|
||||
char *pCmd;
|
||||
|
||||
/* translate 2nd argument */
|
||||
pCmd=TranslateAlias(pAList, pTranslation);
|
||||
if (0==strcmp(pName, pCmd)) /* translation matches */
|
||||
{
|
||||
return "recursive alias not allowed";
|
||||
}
|
||||
|
||||
/* find last element pTail and check that alias does not yet exist */
|
||||
pTail = (AliasItem *)pAList;
|
||||
pAlias = pAList->pFirst;
|
||||
while (pAlias!=NULL)
|
||||
{
|
||||
pTail=pAlias;
|
||||
if (0 == strcmp(pAlias->pName, pName))
|
||||
{
|
||||
return "alias already exists";
|
||||
}
|
||||
pAlias = pAlias->pNext;
|
||||
}
|
||||
|
||||
/* allocate the list entry */
|
||||
pNew = malloc(sizeof(AliasItem));
|
||||
if (pNew!=NULL)
|
||||
{
|
||||
pNew->pNext = NULL;
|
||||
pNew->pName = strdup(pName);
|
||||
if (pNew->pName!=NULL)
|
||||
{
|
||||
pNew->pCmd = strdup(pCmd);
|
||||
if (pNew->pCmd!=NULL)
|
||||
{
|
||||
/* insert at tail */
|
||||
pTail->pNext = pNew;
|
||||
return NULL; /* o.k. */
|
||||
}
|
||||
}
|
||||
}
|
||||
return "not enough memory to create an alias";
|
||||
|
||||
pAlias = pNext;
|
||||
}
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
int DefineAlias(pSConnection pCon, SicsInterp *pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
char pBueffel[256];
|
||||
int iRet;
|
||||
CommandList *pCom = NULL;
|
||||
char *pErr;
|
||||
char *CreateAlias(AliasList * pAList, char *pName, char *pTranslation)
|
||||
{ /* arguments must be lower case */
|
||||
AliasItem *pAlias = NULL, *pNew = NULL, *pTail = NULL;
|
||||
char *pCmd;
|
||||
|
||||
if(!SCMatchRights(pCon,usMugger))
|
||||
{
|
||||
SCWrite(pCon,"ERROR: only managers may define aliases",
|
||||
eError);
|
||||
return 0;
|
||||
/* translate 2nd argument */
|
||||
pCmd = TranslateAlias(pAList, pTranslation);
|
||||
if (0 == strcmp(pName, pCmd)) { /* translation matches */
|
||||
return "recursive alias not allowed";
|
||||
}
|
||||
|
||||
/* find last element pTail and check that alias does not yet exist */
|
||||
pTail = (AliasItem *) pAList;
|
||||
pAlias = pAList->pFirst;
|
||||
while (pAlias != NULL) {
|
||||
pTail = pAlias;
|
||||
if (0 == strcmp(pAlias->pName, pName)) {
|
||||
return "alias already exists";
|
||||
}
|
||||
pAlias = pAlias->pNext;
|
||||
}
|
||||
|
||||
argtolower(argc,argv);
|
||||
|
||||
if(argc == 2) /* remove case */
|
||||
{
|
||||
iRet=RemoveAlias(&pSics->AList, argv[1]);
|
||||
if (iRet==0)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: alias not found",
|
||||
eError);
|
||||
return 0;
|
||||
/* allocate the list entry */
|
||||
pNew = malloc(sizeof(AliasItem));
|
||||
if (pNew != NULL) {
|
||||
pNew->pNext = NULL;
|
||||
pNew->pName = strdup(pName);
|
||||
if (pNew->pName != NULL) {
|
||||
pNew->pCmd = strdup(pCmd);
|
||||
if (pNew->pCmd != NULL) {
|
||||
/* insert at tail */
|
||||
pTail->pNext = pNew;
|
||||
return NULL; /* o.k. */
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
if(argc != 3)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: illegal number of arguments to DefineAlias",
|
||||
eError);
|
||||
}
|
||||
return "not enough memory to create an alias";
|
||||
|
||||
}
|
||||
|
||||
/*------------------------------------------------------------------------*/
|
||||
int DefineAlias(pSConnection pCon, SicsInterp * pSics, void *pData,
|
||||
int argc, char *argv[])
|
||||
{
|
||||
char pBueffel[256];
|
||||
int iRet;
|
||||
CommandList *pCom = NULL;
|
||||
char *pErr;
|
||||
|
||||
if (!SCMatchRights(pCon, usMugger)) {
|
||||
SCWrite(pCon, "ERROR: only managers may define aliases", eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
argtolower(argc, argv);
|
||||
|
||||
if (argc == 2) { /* remove case */
|
||||
iRet = RemoveAlias(&pSics->AList, argv[1]);
|
||||
if (iRet == 0) {
|
||||
SCWrite(pCon, "ERROR: alias not found", eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
pCom = FindCommand(pSics, argv[1]);
|
||||
if (pCom!=NULL)
|
||||
{
|
||||
SCWrite(pCon,"ERROR: an alias must not overwrite a command",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* remove the old alias, if any */
|
||||
RemoveAlias(&pSics->AList, argv[1]);
|
||||
|
||||
pErr=CreateAlias(&pSics->AList, argv[1], argv[2]);
|
||||
if (pErr!=NULL)
|
||||
{
|
||||
sprintf(pBueffel,"ERROR: %s", pErr);
|
||||
SCWrite(pCon,pBueffel,eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
if (argc != 3) {
|
||||
SCWrite(pCon, "ERROR: illegal number of arguments to DefineAlias",
|
||||
eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
pCom = FindCommand(pSics, argv[1]);
|
||||
if (pCom != NULL) {
|
||||
SCWrite(pCon, "ERROR: an alias must not overwrite a command", eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* remove the old alias, if any */
|
||||
RemoveAlias(&pSics->AList, argv[1]);
|
||||
|
||||
pErr = CreateAlias(&pSics->AList, argv[1], argv[2]);
|
||||
if (pErr != NULL) {
|
||||
sprintf(pBueffel, "ERROR: %s", pErr);
|
||||
SCWrite(pCon, pBueffel, eError);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user