WCHandlerFunc HandlerFunc;
}HashHandler;
+typedef enum _estate {
+ eNext,
+ eSkipTilEnd
+} TemplState;
+
void *load_template(StrBuf *Target, WCTemplate *NewTemplate);
-int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP);
+int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams **TPP);
return UnknownContext;
}
-void StackContext(WCTemplputParams *Super,
- WCTemplputParams *Sub,
- void *Context,
- CtxType ContextType,
- int nArgs,
- WCTemplateToken *Tokens)
+void StackDynamicContext(WCTemplputParams *Super,
+ WCTemplputParams *Sub,
+ void *Context,
+ CtxType ContextType,
+ int nArgs,
+ WCTemplateToken *Tokens,
+ WCConditionalFunc ExitCtx,
+ long ExitCTXID)
{
memset(Sub, 0, sizeof(WCTemplputParams));
Sub->Sub = Super->Sub;
Super->Sub = Sub;
}
+ if (Sub->Sub != NULL)
+ Sub->Sub->Super = Sub;
Sub->Super = Super;
Sub->Context = Context;
Sub->Filter.ContextType = ContextType;
Sub->nArgs = nArgs;
Sub->Tokens = Tokens;
+ Sub->ExitCtx = ExitCtx;
+ Sub->ExitCTXID = ExitCTXID;
}
void UnStackContext(WCTemplputParams *Sub)
{
Sub->Super->Sub = Sub->Sub;
}
+ if (Sub->Sub != NULL)
+ {
+ Sub->Sub->Super = Sub->Super;
+ }
+}
+void UnStackDynamicContext(StrBuf *Target, WCTemplputParams **TPP)
+{
+ WCTemplputParams *TP = *TPP;
+ WCTemplputParams *Super = TP->Super;
+ TP->ExitCtx(Target, TP);
+ *TPP = Super;
}
void *GetContextPayload(WCTemplputParams *TP, CtxType ContextType)
*/
}
-
-
-
void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
{
wcsession *WCC;
return 1;
LogTemplateError(
- Target, ErrType, ERR_PARM1, TP,
+ Target, ErrType, ERR_NAME, TP,
" WARNING: requires Context: [%s], have [%s]!",
ContextName(Need->ContextType),
ContextName(TP->Filter.ContextType));
StrBufPeek(Buf, pch, -1, '\0');
if (LoadTemplates > 1) {
syslog(1,
- "DBG: got param [%s] %d %d\n",
+ "DBG: got param [%s] "SIZE_T_FMT" "SIZE_T_FMT"\n",
pchs, pche - pchs, strlen(pchs)
);
}
* \param state are we in conditional state?
* \param ContextType what type of information does context giv us?
*/
-int EvaluateToken(StrBuf *Target, int state, WCTemplputParams *TP)
+int EvaluateToken(StrBuf *Target, int state, WCTemplputParams **TPP)
{
const char *AppendMe;
long AppendMeLen;
HashHandler *Handler;
void *vVar;
+ WCTemplputParams *TP = *TPP;
/* much output, since pName is not terminated...
syslog(1,"Doing token: %s\n",Token->pName);
case SV_CONDITIONAL: /** Forward conditional evaluation */
Handler = (HashHandler*) TP->Tokens->PreEval;
if (!CheckContext(Target, &Handler->Filter, TP, "Conditional")) {
- return -1;
+ return 0;
}
- return EvaluateConditional(Target, 1, state, TP);
+ return EvaluateConditional(Target, 1, state, TPP);
break;
case SV_NEG_CONDITIONAL: /** Reverse conditional evaluation */
Handler = (HashHandler*) TP->Tokens->PreEval;
if (!CheckContext(Target, &Handler->Filter, TP, "Conditional")) {
- return -1;
+ return 0;
}
- return EvaluateConditional(Target, 0, state, TP);
+ return EvaluateConditional(Target, 0, state, TPP);
break;
case SV_CUST_STR_CONDITIONAL: /** Conditional put custom strings from params */
Handler = (HashHandler*) TP->Tokens->PreEval;
if (!CheckContext(Target, &Handler->Filter, TP, "Conditional")) {
- return -1;
+ return 0;
}
if (TP->Tokens->nParameters >= 6) {
- if (EvaluateConditional(Target, 0, state, TP)) {
+ if (EvaluateConditional(Target, 0, state, TPP)) {
GetTemplateTokenString(Target, TP, 5, &AppendMe, &AppendMeLen);
StrBufAppendBufPlain(Target,
AppendMe,
AppendMeLen,
0);
}
+ if (*TPP != TP)
+ {
+ UnStackDynamicContext(Target, TPP);
+ }
}
else {
LogTemplateError(
case SV_PREEVALUATED:
Handler = (HashHandler*) TP->Tokens->PreEval;
if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
- return -1;
+ return 0;
}
Handler->HandlerFunc(Target, TP);
break;
if (GetHash(GlobalNS, TP->Tokens->pName, TP->Tokens->NameEnd, &vVar)) {
Handler = (HashHandler*) vVar;
if (!CheckContext(Target, &Handler->Filter, TP, "Token")) {
- return -1;
+ return 0;
}
else {
Handler->HandlerFunc(Target, TP);
{
WCTemplate *pTmpl = Tmpl;
int done = 0;
- int i, state;
+ int i;
+ TemplState state;
const char *pData, *pS;
long len;
WCTemplputParams TP;
+ WCTemplputParams *TPtr = &TP;
memcpy(&TP.Filter, &CallingTP->Filter, sizeof(ContextFilter));
pS = pData = ChrPtr(pTmpl->Data);
len = StrLength(pTmpl->Data);
i = 0;
- state = 0;
+ state = eNext;
while (!done) {
if (i >= pTmpl->nTokensUsed) {
StrBufAppendBufPlain(Target,
done = 1;
}
else {
+ int TokenRc;
+
StrBufAppendBufPlain(
Target, pData,
pTmpl->Tokens[i]->pTokenStart - pData, 0);
- TP.Tokens = pTmpl->Tokens[i];
- TP.nArgs = pTmpl->Tokens[i]->nParameters;
- state = EvaluateToken(Target, state, &TP);
+ TPtr->Tokens = pTmpl->Tokens[i];
+ TPtr->nArgs = pTmpl->Tokens[i]->nParameters;
- while ((state != 0) && (i+1 < pTmpl->nTokensUsed)) {
+ TokenRc = EvaluateToken(Target, TokenRc, &TPtr);
+ if (TokenRc > 0)
+ {
+ state = eSkipTilEnd;
+ }
+ else if (TokenRc < 0)
+ {
+ if ((TPtr != &TP) &&
+ (TPtr->ExitCTXID == -TokenRc))
+ {
+ UnStackDynamicContext(Target, &TPtr);
+ }
+ TokenRc = 0;
+ }
+
+ while ((state != eNext) && (i+1 < pTmpl->nTokensUsed)) {
/* condition told us to skip till its end condition */
i++;
- TP.Tokens = pTmpl->Tokens[i];
- TP.nArgs = pTmpl->Tokens[i]->nParameters;
+ TPtr->Tokens = pTmpl->Tokens[i];
+ TPtr->nArgs = pTmpl->Tokens[i]->nParameters;
if ((pTmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
- (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
- if (state == EvaluateConditional(
- Target,
- pTmpl->Tokens[i]->Flags,
- state,
- &TP))
- state = 0;
+ (pTmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL))
+ {
+ int rc;
+ rc = EvaluateConditional(
+ Target,
+ pTmpl->Tokens[i]->Flags,
+ TokenRc,
+ &TPtr);
+ if (-rc == TokenRc)
+ {
+ TokenRc = 0;
+ state = eNext;
+ if ((TPtr != &TP) &&
+ (TPtr->ExitCTXID == - rc))
+ {
+ UnStackDynamicContext(Target, &TPtr);
+ }
+ }
}
}
+
pData = pTmpl->Tokens[i++]->pTokenEnd + 1;
if (i > pTmpl->nTokensUsed)
done = 1;
/*-----------------------------------------------------------------------------
* Conditionals
*/
-int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams *TP)
+int EvaluateConditional(StrBuf *Target, int Neg, int state, WCTemplputParams **TPP)
{
ConditionalStruct *Cond;
int rc = 0;
int res;
+ WCTemplputParams *TP = *TPP;
if ((TP->Tokens->Params[0]->len == 1) &&
(TP->Tokens->Params[0]->Start[0] == 'X'))
- return (state != 0)?TP->Tokens->Params[1]->lvalue:0;
+ {
+ return - (TP->Tokens->Params[1]->lvalue);
+ }
Cond = (ConditionalStruct *) TP->Tokens->PreEval;
if (Cond == NULL) {
LogTemplateError(
Target, "Conditional", ERR_PARM1, TP,
"unknown!");
- return 1;
+ return 0;
}
if (!CheckContext(Target, &Cond->Filter, TP, "Conditional")) {
res = Cond->CondF(Target, TP);
if (res == Neg)
rc = TP->Tokens->Params[1]->lvalue;
+
if (LoadTemplates > 5)
syslog(1, "<%s> : %d %d==%d\n",
ChrPtr(TP->Tokens->FlatToken),
rc, res, Neg);
+
+ if (TP->Sub != NULL)
+ {
+ *TPP = TP->Sub;
+ }
return rc;
}
-void RegisterConditional(const char *Name, long len,
- int nParams,
- WCConditionalFunc CondF,
- int ContextRequired)
+void RegisterContextConditional(const char *Name, long len,
+ int nParams,
+ WCConditionalFunc CondF,
+ WCConditionalFunc ExitCtxCond,
+ int ContextRequired)
{
ConditionalStruct *Cond;
Cond->Filter.nMaxArgs = nParams;
Cond->Filter.nMinArgs = nParams;
Cond->CondF = CondF;
+ Cond->CondExitCtx = ExitCtxCond;
Cond->Filter.ContextType = ContextRequired;
Put(Conditionals, Name, len, Cond, NULL);
}
RegisterNamespace("LONGVECTOR", 1, 1, tmplput_long_vector, NULL, CTX_LONGVECTOR);
- RegisterConditional(HKEY("COND:CONTEXTSTR"), 3, ConditionalContextStr, CTX_STRBUF);
- RegisterConditional(HKEY("COND:CONTEXTSTRARR"), 4, ConditionalContextStrinArray, CTX_STRBUFARR);
- RegisterConditional(HKEY("COND:LONGVECTOR"), 4, ConditionalLongVector, CTX_LONGVECTOR);
+ RegisterConditional("COND:CONTEXTSTR", 3, ConditionalContextStr, CTX_STRBUF);
+ RegisterConditional("COND:CONTEXTSTRARR", 4, ConditionalContextStrinArray, CTX_STRBUFARR);
+ RegisterConditional("COND:LONGVECTOR", 4, ConditionalLongVector, CTX_LONGVECTOR);
- RegisterConditional(HKEY("COND:ITERATE:ISGROUPCHANGE"), 2,
+ RegisterConditional("COND:ITERATE:ISGROUPCHANGE", 2,
conditional_ITERATE_ISGROUPCHANGE,
CTX_ITERATE);
- RegisterConditional(HKEY("COND:ITERATE:LASTN"), 2,
+ RegisterConditional("COND:ITERATE:LASTN", 2,
conditional_ITERATE_LASTN,
CTX_ITERATE);
- RegisterConditional(HKEY("COND:ITERATE:FIRSTN"), 2,
+ RegisterConditional("COND:ITERATE:FIRSTN", 2,
conditional_ITERATE_FIRSTN,
CTX_ITERATE);