- make definealias aliases persistent

This commit is contained in:
zolliker
2011-08-29 14:38:27 +00:00
parent defbae719f
commit 2469129270
2 changed files with 76 additions and 16 deletions

View File

@ -41,6 +41,9 @@
#include <assert.h> #include <assert.h>
#include <string.h> #include <string.h>
#include "fortify.h" #include "fortify.h"
#include "sics.h"
#include "strlutil.h"
#include "dynstring.h"
#include "definealias.h" #include "definealias.h"
typedef struct __Aitem { typedef struct __Aitem {
@ -153,47 +156,104 @@ char *CreateAlias(AliasList * pAList, char *pName, char *pTranslation)
int DefineAlias(pSConnection pCon, SicsInterp * pSics, void *pData, int DefineAlias(pSConnection pCon, SicsInterp * pSics, void *pData,
int argc, char *argv[]) int argc, char *argv[])
{ {
char pBueffel[256]; char arg1[256];
char arg2[256];
int iRet; int iRet;
CommandList *pCom = NULL; CommandList *pCom = NULL;
char *pErr; 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)) { if (!SCMatchRights(pCon, usMugger)) {
SCWrite(pCon, "ERROR: only managers may define aliases", eError); SCWrite(pCon, "ERROR: only managers may define aliases", eError);
return 0; return 0;
} }
argtolower(argc, argv);
if (argc == 2) { /* remove case */ if (argc == 2) { /* remove case */
iRet = RemoveAlias(&pSics->AList, argv[1]); iRet = RemoveAlias(&pSics->AList, arg1);
if (iRet == 0) { if (iRet == 0) {
SCWrite(pCon, "ERROR: alias not found", eError); SCWrite(pCon, "ERROR: alias not found", eError);
return 0; return 0;
} }
return 1; return 1;
} }
if (argc != 3) {
SCWrite(pCon, "ERROR: illegal number of arguments to DefineAlias",
eError);
return 0;
}
pCom = FindCommand(pSics, argv[1]); pCom = FindCommand(pSics, arg1);
if (pCom != NULL) { /* 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); SCWrite(pCon, "ERROR: an alias must not overwrite a command", eError);
return 0; return 0;
} }
/* remove the old alias, if any */ /* 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) { if (pErr != NULL) {
snprintf(pBueffel,255, "ERROR: %s", pErr); SCPrintf(pCon, eError, "ERROR: %s", pErr);
SCWrite(pCon, pBueffel, eError);
return 0; return 0;
} }
return 1; 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);
}

2
ofac.c
View File

@ -28,6 +28,7 @@ static void InitGeneral(void)
INIT(StatisticsInit); INIT(StatisticsInit);
INIT(InitializerInit); INIT(InitializerInit);
INIT(SaveHdbInit); /* must be after InitializerInit */ INIT(SaveHdbInit); /* must be after InitializerInit */
INIT(DefineAliasInit);
INIT(SctInit); INIT(SctInit);
INIT(SctDriveAdapterInit); INIT(SctDriveAdapterInit);
INIT(SctDriveObjInit); INIT(SctDriveObjInit);
@ -69,7 +70,6 @@ static void InitIniCommands(SicsInterp * pInter)
PCMD("ClientPut", ClientPut); PCMD("ClientPut", ClientPut);
PCMD("config", ConfigCon); PCMD("config", ConfigCon);
PCMD("db", SICSDebug); PCMD("db", SICSDebug);
PCMD("DefineAlias", DefineAlias);
PCMD("Dir", ListObjects); PCMD("Dir", ListObjects);
PCMD("dolater", MakeCron); PCMD("dolater", MakeCron);
PCMD("DrivableInvoke", TclDrivableInvoke); PCMD("DrivableInvoke", TclDrivableInvoke);