From e9399b6990d48109f35ffd996cec9e865937173f Mon Sep 17 00:00:00 2001 From: Ferdi Franceschini Date: Wed, 13 Nov 2013 12:03:21 +1100 Subject: [PATCH] SICS-687 Add 'nxscript makenamedlink' command. --- nxdict.c | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++ nxscript.c | 29 +++++++++++++ 2 files changed, 149 insertions(+) diff --git a/nxdict.c b/nxdict.c index f4b9f3c5..fcdde680 100644 --- a/nxdict.c +++ b/nxdict.c @@ -1475,6 +1475,126 @@ NXstatus NXDinfoalias(NXhandle hFil, NXdict dict, char *pAlias, int *rank, return iRet; } +/*------------------------------------------------------------------------*/ +NXstatus NXDdefnamedlink(NXhandle hFil, NXdict dict, + char *pTarget, char *pVictim, char *newname) +{ + NXdict pDict; + ParDat pParseT, pParseV; + int iRet, i, iStat; + NXlink sLink; + + pDict = NXDIAssert(dict); + + +#ifdef DEFDEBUG + printf("Linking: %s\n", pVictim); + printf("To: %s\n", pTarget); +#endif + + + /* parse Victim */ + pParseV.iMayCreate = 0; + pParseV.pPtr = pVictim; + pParseV.iDepth = 0; + iRet = NXDIDefParse(hFil, pDict, &pParseV); + if (iRet == NX_ERROR) { + /* unwind and throw up */ + NXDIUnwind(hFil, pParseV.iDepth); + return NX_ERROR; + } + /* get link data */ + if (pParseV.iTerminal == TERMSDS) { + NXgetdataID(hFil, &sLink); + iRet = NXclosedata(hFil); + if (iRet != NX_OK) { + /* unwind and throw up */ + NXDIUnwind(hFil, pParseV.iDepth); + return NX_ERROR; + } + } else if (pParseV.iTerminal == TERMVG) { + NXgetgroupID(hFil, &sLink); + } else { + assert(0); /* serious programming error */ + } + /* Unwind */ + iRet = NXDIUnwind(hFil, pParseV.iDepth); + if (iRet != NX_OK) { + return NX_ERROR; + } + + /* parse Target */ + pParseT.iMayCreate = 1; + pParseT.pPtr = pTarget; + pParseT.iDepth = 0; + iRet = NXDIDefParse(hFil, pDict, &pParseT); + if (iRet == NX_ERROR) { + /* unwind and throw up */ + NXDIUnwind(hFil, pParseT.iDepth); + return NX_ERROR; + } + /* check it being a vGroup! */ + if (pParseT.iTerminal != TERMVG) { + NXReportError("ERROR: can link only into a vGroup"); + NXDIUnwind(hFil, pParseT.iDepth); + return NX_ERROR; + } + + /* link, finally */ + iRet = NXmakenamedlink(hFil, newname, &sLink); + /* Unwind anyway */ + iStat = NXDIUnwind(hFil, pParseT.iDepth); + if (iStat != NX_OK) { + return NX_ERROR; + } + return iStat; +} + +/*--------------------------------------------------------------------------*/ +NXstatus NXDaliasnamedlink(NXhandle hFil, NXdict dict, + char *pTarget, char *pVictim, char *newname) +{ + char pTargetDef[2048], pVictimDef[2048]; + int iRet; + NXdict pDict; + pDynString pRep1 = NULL, pRep2 = NULL; + + pDict = NXDIAssert(dict); + + /* get Target Definition String */ + iRet = NXDget(pDict, pTarget, pTargetDef, 2047); + if (iRet != NX_OK) { + sprintf(pTargetDef, "ERROR: alias %s not recognized", pTarget); + NXReportError(pTargetDef); + return NX_ERROR; + } + + /* get Victim definition string */ + iRet = NXDget(pDict, pVictim, pVictimDef, 2047); + if (iRet != NX_OK) { + sprintf(pTargetDef, "ERROR: alias %s not recognized", pTarget); + NXReportError(pTargetDef); + return NX_ERROR; + } + + /* do replacements */ + pRep1 = NXDItextreplace(dict, pTargetDef); + pRep2 = NXDItextreplace(dict, pVictimDef); + if ((!pRep1) || (!pRep2)) { + if (pRep1) + DeleteDynString(pRep1); + if (pRep2) + DeleteDynString(pRep2); + return NX_ERROR; + } + + /* call NXdeflin */ + iRet = NXDdefnamedlink(hFil, pDict, GetCharArray(pRep1), GetCharArray(pRep2), newname); + DeleteDynString(pRep1); + DeleteDynString(pRep2); + return iRet; +} + /*------------------------------------------------------------------------*/ NXstatus NXDdeflink(NXhandle hFil, NXdict dict, char *pTarget, char *pVictim) diff --git a/nxscript.c b/nxscript.c index ed3ad5c5..de704b75 100644 --- a/nxscript.c +++ b/nxscript.c @@ -1589,6 +1589,31 @@ static void makeLink(SConnection * pCon, SicsInterp * pSics, SCSendOK(pCon); } +/*----------------------------------------------------------------------*/ +static void makeNamedLink(SConnection * pCon, SicsInterp * pSics, + pNXScript self, int argc, char *argv[]) +{ + int status; + char pBueffel[256]; + + if (argc < 5) { + SCWrite(pCon, "ERROR: insufficient number of arguments to makenamedlink", + eLogError); + return; + } + + status = NXDaliasnamedlink(self->fileHandle, self->dictHandle, + argv[2], argv[3], argv[4]); + if (status != NX_OK) { + snprintf(pBueffel, 255, "ERROR: linking %s against %s as %s failed", + argv[2], argv[3], argv[4]); + SCWrite(pCon, pBueffel, eLogError); + return; + } + + SCSendOK(pCon); +} + /*----------------------------------------------------------------------*/ static void updateDictVar(SConnection * pCon, pNXScript self, int argc, char *argv[]) @@ -1698,6 +1723,10 @@ int NXScriptAction(SConnection * pCon, SicsInterp * pSics, void *pData, makeLink(pCon, pSics, self, argc, argv); return 1; } + if (strcmp(argv[1], "makenamedlink") == 0) { + makeNamedLink(pCon, pSics, self, argc, argv); + return 1; + } return 1; }