From 24691292708337dc48c695b6a28fdce021a5ca9c Mon Sep 17 00:00:00 2001 From: zolliker Date: Mon, 29 Aug 2011 14:38:27 +0000 Subject: [PATCH] - make definealias aliases persistent --- definealias.c | 90 ++++++++++++++++++++++++++++++++++++++++++--------- ofac.c | 2 +- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/definealias.c b/definealias.c index defec4eb..1f8b93e1 100644 --- a/definealias.c +++ b/definealias.c @@ -41,6 +41,9 @@ #include #include #include "fortify.h" +#include "sics.h" +#include "strlutil.h" +#include "dynstring.h" #include "definealias.h" typedef struct __Aitem { @@ -153,47 +156,104 @@ char *CreateAlias(AliasList * pAList, char *pName, char *pTranslation) int DefineAlias(pSConnection pCon, SicsInterp * pSics, void *pData, int argc, char *argv[]) { - char pBueffel[256]; + char arg1[256]; + char arg2[256]; int iRet; CommandList *pCom = NULL; char *pErr; + char *pCmd; + AliasItem *pAlias; + pDynString str; + + if (argc == 3) { + strlcpy(arg2, argv[2], sizeof arg2); + strtolower(arg2); + } else if (argc > 3 || argc < 2) { + SCWrite(pCon, "ERROR: illegal number of arguments to DefineAlias", + eError); + return 0; + } + + strlcpy(arg1, argv[1], sizeof arg1); + strtolower(arg1); + + if (argc == 3 && strcasecmp(arg1, "-show") == 0) { + for (pAlias = pSics->AList.pFirst; pAlias!=NULL; pAlias=pAlias->pNext) { + if (0 == strcmp(pAlias->pName, arg2)) { + SCWrite(pCon, pAlias->pCmd, eValue); + return 1; + } + } + SCWrite(pCon, "NA", eValue); + return 1; + } + + if (argc == 2 && strcasecmp(arg1, "-list") == 0) { + str = CreateDynString(60,63); + for (pAlias = pSics->AList.pFirst; pAlias!=NULL; pAlias=pAlias->pNext) { + DynStringConcat(str, pAlias->pName); + DynStringConcat(str, " "); + } + if (GetDynStringLength(str) > 0) { + DynStringBackspace(str); + } + SCWrite(pCon, GetCharArray(str), eValue); + DeleteDynString(str); + return 1; + } + + if (argc == 3 && strcasecmp(arg1, "-translate") == 0) { + pCmd = TranslateAlias(&pSics->AList, arg2); + SCWrite(pCon, pCmd, eValue); + return 1; + } 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]); + iRet = RemoveAlias(&pSics->AList, arg1); if (iRet == 0) { SCWrite(pCon, "ERROR: alias not found", 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) { + pCom = FindCommand(pSics, arg1); + /* do not complain when the command was a translated alias */ + if (pCom != NULL && strcmp(pCom->pName, arg1) == 0) { SCWrite(pCon, "ERROR: an alias must not overwrite a command", eError); return 0; } /* remove the old alias, if any */ - RemoveAlias(&pSics->AList, argv[1]); + RemoveAlias(&pSics->AList, arg1); - pErr = CreateAlias(&pSics->AList, argv[1], argv[2]); + pErr = CreateAlias(&pSics->AList, arg1, arg2); if (pErr != NULL) { - snprintf(pBueffel,255, "ERROR: %s", pErr); - SCWrite(pCon, pBueffel, eError); + SCPrintf(pCon, eError, "ERROR: %s", pErr); return 0; } return 1; } +/*------------------------------------------------------------------------*/ +static int AliasSaver(void *object, char *name, FILE * fil) +{ + AliasItem *pAlias; + + for (pAlias = pServ->pSics->AList.pFirst; pAlias != NULL; pAlias = pAlias->pNext) { + fprintf(fil, "definealias %s %s\n", pAlias->pName, pAlias->pCmd); + } + return 1; +} +/*------------------------------------------------------------------------*/ +void DefineAliasInit(void) { + Dummy *as; + as = CreateDummy("alias saver"); + as->pDescriptor->SaveStatus = AliasSaver; + AddCommandWithFlag(pServ->pSics, "definealias", DefineAlias, KillDummy, as, 0); +} diff --git a/ofac.c b/ofac.c index 140a4df7..7fd1f723 100644 --- a/ofac.c +++ b/ofac.c @@ -28,6 +28,7 @@ static void InitGeneral(void) INIT(StatisticsInit); INIT(InitializerInit); INIT(SaveHdbInit); /* must be after InitializerInit */ + INIT(DefineAliasInit); INIT(SctInit); INIT(SctDriveAdapterInit); INIT(SctDriveObjInit); @@ -69,7 +70,6 @@ static void InitIniCommands(SicsInterp * pInter) PCMD("ClientPut", ClientPut); PCMD("config", ConfigCon); PCMD("db", SICSDebug); - PCMD("DefineAlias", DefineAlias); PCMD("Dir", ListObjects); PCMD("dolater", MakeCron); PCMD("DrivableInvoke", TclDrivableInvoke);