HashList *GlobalNS;
HashList *Iterators;
+HashList *Contitionals;
+
+#define SV_GETTEXT 1
+#define SV_CONDITIONAL 2
+#define SV_NEG_CONDITIONAL 3
typedef struct _WCTemplate {
StrBuf *Data;
ptr->wcs_value = strdup(Data);
}
+/**
+ * \brief Add a substitution variable (local to this session)
+ * \param keyname the replacementstring to substitute
+ * \param keytype the kind of the key
+ * \param format the format string ala printf
+ * \param ... the arguments to substitute in the formatstring
+ */
+void SVPutLong(char *keyname, size_t keylen, long Data)
+{
+ void *vPtr;
+ wcsubst *ptr = NULL;
+ struct wcsession *WCC = WC;
+
+
+ /**
+ * First look if we're doing a replacement of
+ * an existing key
+ */
+ /*PrintHash(WCC->vars, VarPrintTransition, VarPrintEntry);*/
+ if (GetHash(WCC->vars, keyname, keylen, &vPtr)) {
+ ptr = (wcsubst*)vPtr;
+ if (ptr->wcs_value != NULL)
+ free(ptr->wcs_value);
+ }
+ else /** Otherwise allocate a new one */
+ {
+ ptr = (wcsubst *) malloc(sizeof(wcsubst));
+ safestrncpy(ptr->wcs_key, keyname, sizeof ptr->wcs_key);
+ Put(WCC->vars, keyname, keylen, ptr, deletevar);
+ }
+
+ ptr->wcs_function = NULL;
+ ptr->wcs_type = WCS_LONG;
+ ptr->lvalue = Data;
+}
+
/**
* \brief Add a substitution variable (local to this session) that does a callback
* \param keyname the keystring to substitute
case WCS_STRBUF_REF:
StrBufAppendBuf(Target, (StrBuf*) ptr->wcs_function, 0);
break;
+ case WCS_LONG:
+ StrBufAppendPrintf(Target, "%l", ptr->lvalue);
+ break;
default:
lprintf(1,"WARNING: invalid value in SV-Hash at %s!", keyname);
}
TemplateParam *Param;
WCTemplateToken *NewToken = (WCTemplateToken*)malloc(sizeof(WCTemplateToken));
- NewToken->IsGettext = 0;
+ NewToken->Flags = 0;
NewToken->pTokenStart = pTmplStart;
NewToken->TokenStart = pTmplStart - pStart;
NewToken->TokenEnd = (pTmplEnd - pStart) - NewToken->TokenStart;
else break;
}
if((NewToken->NameEnd == 1) &&
- (NewToken->HaveParameters == 1) &&
- (NewToken->nParameters == 1) &&
- (*(NewToken->pName) == '_'))
- NewToken->IsGettext = 1;
+ (NewToken->HaveParameters == 1))
+
+ {
+ if ((NewToken->nParameters == 1) &&
+ (*(NewToken->pName) == '_'))
+ NewToken->Flags = SV_GETTEXT;
+ else if ((NewToken->nParameters >= 2) &&
+ (*(NewToken->pName) == '?'))
+ NewToken->Flags = SV_CONDITIONAL;
+ else if ((NewToken->nParameters >=2) &&
+ (*(NewToken->pName) == '!'))
+ NewToken->Flags = SV_NEG_CONDITIONAL;
+ }
}
else pch ++;
}
free(FreeMe);
}
-void EvaluateToken(StrBuf *Target, WCTemplateToken *Token, void *Context)
+
+int EvaluateConditional(WCTemplateToken *Token, void *Context, int Neg, int state)
+{
+ void *vConditional;
+ ConditionalStruct *Cond;
+
+ if ((Token->Params[0]->len == 1) &&
+ (Token->Params[0]->Start[0] == 'X'))
+ return (state != 0)?Token->Params[1]->lvalue:0;
+
+ if (!GetHash(Contitionals,
+ Token->Params[0]->Start,
+ Token->Params[0]->len,
+ &vConditional)) {
+ lprintf(1, "Conditional %s Not found!\n",
+ Token->Params[0]->Start);
+ }
+
+ Cond = (ConditionalStruct *) vConditional;
+
+ if (Cond == NULL) {
+ lprintf(1, "Conditional %s Not found!\n",
+ Token->Params[0]->Start);
+ return 0;
+ }
+ if (Token->nParameters < Cond->nParams) {
+ lprintf(1, "Conditional [%s] needs %ld Params!\n",
+ Token->Params[0]->Start,
+ Cond->nParams);
+ return 0;
+ }
+ if (Cond->CondF(Token, Context) == Neg)
+ return Token->Params[1]->lvalue;
+ return 0;
+}
+
+int EvaluateToken(StrBuf *Target, WCTemplateToken *Token, void *Context, int state)
{
void *vVar;
// much output, since pName is not terminated...
// lprintf(1,"Doing token: %s\n",Token->pName);
- if (Token->IsGettext)
+ if (Token->Flags == SV_GETTEXT) {
TmplGettext(Target, Token->nParameters, Token);
+ }
+ else if (Token->Flags == SV_CONDITIONAL) {
+ return EvaluateConditional(Token, Context, 1, state);
+ }
+ else if (Token->Flags == SV_NEG_CONDITIONAL) {
+ return EvaluateConditional(Token, Context, 0, state);
+ }
else if (GetHash(GlobalNS, Token->pName, Token->NameEnd, &vVar)) {
HashHandler *Handler;
Handler = (HashHandler*) vVar;
else {
print_value_of(Target, Token->pName, Token->NameEnd);
}
+ return 0;
}
void ProcessTemplate(WCTemplate *Tmpl, StrBuf *Target, void *Context)
{
int done = 0;
- int i;
+ int i, state;
const char *pData, *pS;
long len;
pS = pData = ChrPtr(Tmpl->Data);
len = StrLength(Tmpl->Data);
i = 0;
+ state = 0;
while (!done) {
if (i >= Tmpl->nTokensUsed) {
StrBufAppendBufPlain(Target,
StrBufAppendBufPlain(
Target, pData,
Tmpl->Tokens[i]->pTokenStart - pData, 0);
- EvaluateToken(Target, Tmpl->Tokens[i], Context);
+ state = EvaluateToken(Target, Tmpl->Tokens[i], Context, state);
+ while ((state != 0) && (i+1 < Tmpl->nTokensUsed)) {
+ /* condition told us to skip till its end condition */
+ i++;
+ if ((Tmpl->Tokens[i]->Flags == SV_CONDITIONAL) ||
+ (Tmpl->Tokens[i]->Flags == SV_NEG_CONDITIONAL)) {
+ if (state == EvaluateConditional(Tmpl->Tokens[i],
+ Context,
+ Tmpl->Tokens[i]->Flags,
+ state))
+ state = 0;
+ }
+ }
pData = Tmpl->Tokens[i++]->pTokenEnd + 1;
+ if (i >= Tmpl->nTokensUsed)
+ done = 1;
}
}
}
It->Destructor(List);
}
+int ConditionalVar(WCTemplateToken *Tokens, void *Context)
+{
+ void *vsubst;
+ wcsubst *subst;
+
+ if (!GetHash(WC->vars,
+ Tokens->Params[2]->Start,
+ Tokens->Params[2]->len,
+ &vsubst))
+ return 0;
+ subst = (wcsubst*) vsubst;
+ switch(subst->wcs_type) {
+ case WCS_STRING:
+ if (Tokens->nParameters < 4)
+ return 0;
+ return (strcmp(Tokens->Params[3]->Start, subst->wcs_value) == 0);
+ case WCS_SERVCMD:
+ lprintf(1, " -> Server [%s]\n", subst->wcs_value);////todo
+ return 0;
+ case WCS_FUNCTION:
+ return (subst->wcs_function!=NULL);
+ case WCS_STRBUF:
+ case WCS_STRBUF_REF:
+ if (Tokens->nParameters < 4)
+ return 0;
+ return (strcmp(Tokens->Params[3]->Start, ChrPtr((StrBuf*) subst->wcs_function)) == 0);
+ case WCS_LONG:
+ if (Tokens->nParameters < 4)
+ return (subst->lvalue != 0);
+ return (subst->lvalue == Tokens->Params[3]->lvalue);
+ default:
+ lprintf(1," WARNING: invalid type: [%ld]!\n", subst->wcs_type);
+ }
+ return 0;
+}
void RegisterITERATOR(const char *Name, long len,
HashList *StaticList,
Put(Iterators, Name, len, It, NULL);
}
+void RegisterConditional(const char *Name, long len,
+ int nParams,
+ WCConditionalFunc CondF)
+{
+ ConditionalStruct *Cond = (ConditionalStruct*)malloc(sizeof(ConditionalStruct));
+ Cond->nParams = nParams;
+ Cond->CondF = CondF;
+ Put(Contitionals, Name, len, Cond, NULL);
+}
+
void
InitModule_SUBST
(void)
RegisterNamespace("CURRENT_USER", 0, 0, tmplput_current_user);
RegisterNamespace("CURRENT_ROOM", 0, 0, tmplput_current_room);
RegisterNamespace("ITERATE", 2, 4, tmpl_iterate_subtmpl);
+ RegisterConditional(HKEY("COND:SUBST"), 3, ConditionalVar);
}
/*@}*/
int wcs_type; /**< which type of ??? */
char wcs_key[32]; /**< ??? what?*/
void *wcs_value; /**< ???? what?*/
+ long lvalue; /**< type long? keep data here */
void (*wcs_function)(void); /**< funcion hook ???*/
} wcsubst;
size_t TokenStart;
size_t TokenEnd;
const char *pTokenEnd;
- int IsGettext;
+ int Flags;
const char *pName;
size_t NameEnd;
TemplateParam *Params[MAXPARAM];
} WCTemplateToken;
+
typedef void (*WCHandlerFunc)(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context);
void RegisterNS(const char *NSName, long len,
int nMinArgs,
#define RegisterNamespace(a, b, c, d) RegisterNS(a, sizeof(a)-1, b, c, d)
+
+typedef int (*WCConditionalFunc)(WCTemplateToken *Token, void *Context);
+typedef struct _ConditionalStruct {
+ int nParams;
+ WCConditionalFunc CondF;
+} ConditionalStruct;
+void RegisterConditional(const char *Name, long len,
+ int nParams,
+ WCConditionalFunc CondF);
+
+
+
typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, void *Context);
typedef HashList *(*RetrieveHashlistFunc)(void);
typedef void (*HashDestructorFunc) (HashList *KillMe);
WCS_FUNCTION, /**< its a function callback */
WCS_SERVCMD, /**< its a command to send to the citadel server */
WCS_STRBUF, /**< its a strbuf we own */
- WCS_STRBUF_REF /**< its a strbuf we mustn't free */
+ WCS_STRBUF_REF, /**< its a strbuf we mustn't free */
+ WCS_LONG /**< its an integer */
};
/**
char content_type[SIZ]; /**< the content itself ???*/
char filename[SIZ]; /**< the filename hooked to this content ??? */
char *data; /**< the data pool; aka this content */
+ long lvalue; /**< if we put a long... */
};
/**
extern HashList *GlobalNS;
extern HashList *Iterators;
extern HashList *ZoneHash;
-
+extern HashList *Contitionals;
void InitialiseSemaphores(void);
void SVPut(char *keyname, size_t keylen, int keytype, char *Data);
#define svput(a, b, c) SVPut(a, sizeof(a) - 1, b, c)
+void SVPutLong(char *keyname, size_t keylen, long Data);
+#define svputlong(a, b) SVPutLong(a, sizeof(a) - 1, b)
void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...) __attribute__((__format__(__printf__,4,5)));
void SVPRINTF(char *keyname, int keytype, const char *format,...) __attribute__((__format__(__printf__,3,4)));
void SVCALLBACK(char *keyname, var_callback_fptr fcn_ptr);