X-Git-Url: https://code.citadel.org/?a=blobdiff_plain;f=webcit%2Fsubst.c;h=b54e921ee9946b3c1c738ad9576f96dd26bb83ab;hb=HEAD;hp=a3063e6f3168e099ed97e58f7615dacf9ecfd786;hpb=3c1ebe35ca680369335a5440bc96785178c61a33;p=citadel.git diff --git a/webcit/subst.c b/webcit/subst.c index a3063e6f3..f6725cd58 100644 --- a/webcit/subst.c +++ b/webcit/subst.c @@ -1,5 +1,6 @@ -// Spaghetti, technical debt, and an unmaintainable big mess. -// No one knows how this works. This is why we started over with WebCit-NG. +// This is a template substitution engine, which began with good intentions, but ended up becoming +// more complex and unmaintainable than what it replaced. We are barely hanging on with this until +// webcit-ng replaces webcit classic. #include "sysdep.h" #include @@ -14,7 +15,7 @@ #define SHOW_ME_VAPPEND_PRINTF #include "webcit.h" -#include "webserver.h" + extern char *static_dirs[PATH_MAX]; /* Disk representation */ @@ -638,7 +639,12 @@ void StrBufAppendTemplate(StrBuf *Target, StrEscAppend(Target, Source, NULL, 0, 2); break; case 'X': - StrEscAppend(Target, Source, NULL, 0, 0); + if (!IsEmptyStr(ChrPtr(Source))) { + StrEscAppend(Target, Source, NULL, 0, 0); + if (pFmt[1]) { + StrBufAppendBufPlain(Target, pFmt, -1, 1); + } + } break; case 'J': StrECMAEscAppend(Target, Source, NULL); @@ -1587,22 +1593,16 @@ void InitTemplateCache(void) /* Primary Template set... */ StrBufPrintf(Dir, "%s/t", static_dirs[0]); - LoadTemplateDir(Dir, - TemplateCache, - Key); + LoadTemplateDir(Dir, TemplateCache, Key); /* User local Template set */ StrBufPrintf(Dir, "%s/t", static_dirs[1]); - LoadTemplateDir(Dir, - LocalTemplateCache, - Key); + LoadTemplateDir(Dir, LocalTemplateCache, Key); /* Debug Templates, just to be loaded while debugging. */ StrBufPrintf(Dir, "%s/dbg", static_dirs[0]); - LoadTemplateDir(Dir, - TemplateCache, - Key); + LoadTemplateDir(Dir, TemplateCache, Key); Templates[0] = TemplateCache; Templates[1] = LocalTemplateCache; @@ -1615,13 +1615,7 @@ void InitTemplateCache(void) void *vTemplate; At = GetNewHashPos(Templates[i], 0); - while (GetNextHashPos(Templates[i], - At, - &KLen, - &Key, - &vTemplate) && - (vTemplate != NULL)) - { + while (GetNextHashPos(Templates[i], At, &KLen, &Key, &vTemplate) && (vTemplate != NULL)) { load_template(NULL, (WCTemplate *)vTemplate); } DeleteHashPos(&At); @@ -1684,17 +1678,11 @@ int EvaluateToken(StrBuf *Target, int state, WCTemplputParams **TPP) if (TP->Tokens->nParameters >= 6) { if (EvaluateConditional(Target, 0, state, TPP)) { GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen); - StrBufAppendBufPlain(Target, - AppendMe, - AppendMeLen, - 0); + StrBufAppendBufPlain(Target, AppendMe, AppendMeLen, 0); } else{ GetTemplateTokenString(Target, TP, 4, &AppendMe, &AppendMeLen); - StrBufAppendBufPlain(Target, - AppendMe, - AppendMeLen, - 0); + StrBufAppendBufPlain(Target, AppendMe, AppendMeLen, 0); } if (*TPP != TP) { @@ -1702,9 +1690,7 @@ int EvaluateToken(StrBuf *Target, int state, WCTemplputParams **TPP) } } else { - LogTemplateError( - Target, "Conditional", ERR_NAME, TP, - "needs at least 6 Params!"); + LogTemplateError( Target, "Conditional", ERR_NAME, TP, "needs at least 6 Params!"); } break; case SV_SUBTEMPL: @@ -1764,8 +1750,7 @@ const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams ChrPtr(Tmpl->FileName)); pTmpl = duplicate_template(Tmpl); if(load_template(Target, pTmpl) == NULL) { - StrBufAppendPrintf( - Target, + StrBufAppendPrintf( Target, "
\nError loading Template [%s]\n See Logfile for details\n
\n", ChrPtr(Tmpl->FileName)); FreeWCTemplate(pTmpl); @@ -1780,30 +1765,22 @@ const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams state = eNext; while (!done) { if (i >= pTmpl->nTokensUsed) { - StrBufAppendBufPlain(Target, - pData, - len - (pData - pS), 0); + StrBufAppendBufPlain(Target, pData, len - (pData - pS), 0); done = 1; } else { int TokenRc = 0; - StrBufAppendBufPlain( - Target, pData, - pTmpl->Tokens[i]->pTokenStart - pData, 0); + StrBufAppendBufPlain( Target, pData, pTmpl->Tokens[i]->pTokenStart - pData, 0); TPtr->Tokens = pTmpl->Tokens[i]; TPtr->nArgs = pTmpl->Tokens[i]->nParameters; TokenRc = EvaluateToken(Target, TokenRc, &TPtr); - if (TokenRc > 0) - { + if (TokenRc > 0) { state = eSkipTilEnd; } - else if (TokenRc < 0) - { - if ((TPtr != &TP) && - (TPtr->ExitCTXID == -TokenRc)) - { + else if (TokenRc < 0) { + if ((TPtr != &TP) && (TPtr->ExitCTXID == -TokenRc)) { UnStackDynamicContext(Target, &TPtr); } TokenRc = 0; @@ -1823,13 +1800,10 @@ const StrBuf *ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, WCTemplputParams pTmpl->Tokens[i]->Flags, TokenRc, &TPtr); - if (-rc == TokenRc) - { + if (-rc == TokenRc) { TokenRc = 0; state = eNext; - if ((TPtr != &TP) && - (TPtr->ExitCTXID == - rc)) - { + if ((TPtr != &TP) && (TPtr->ExitCTXID == - rc)) { UnStackDynamicContext(Target, &TPtr); } } @@ -1872,8 +1846,7 @@ const StrBuf *DoTemplate(const char *templatename, long len, StrBuf *Target, WCT Static = TemplateCache; StaticLocal = LocalTemplateCache; - if (len == 0) - { + if (len == 0) { syslog(LOG_WARNING, "Can't to load a template with empty name!\n"); StrBufAppendPrintf(Target, "
\nCan't to load a template with empty name!\n
"); return textPlainType;