* BIG rewrite of header handling and slimming of context_loop and session_loop; shuff...
[citadel.git] / webcit / subst.c
index f4fde0edb9787d7c2e8c1d3d1bbc5aeb40ddb043..6842a99b9b943bdabec2fdd87ac88661bafd5838 100644 (file)
@@ -32,9 +32,11 @@ HashList *Iterators;
 HashList *Conditionals;
 HashList *SortHash;
 
+int DumpTemplateI18NStrings = 0;
 int LoadTemplates = 0;
 int dbg_bactrace_template_errors = 0;
 WCTemplputParams NoCtx;
+StrBuf *I18nDump = NULL;
 
 #define SV_GETTEXT 1
 #define SV_CONDITIONAL 2
@@ -43,6 +45,20 @@ WCTemplputParams NoCtx;
 #define SV_SUBTEMPL 5
 #define SV_PREEVALUATED 6
 
+
+/*
+ * \brief Dynamic content for variable substitution in templates
+ */
+typedef struct _wcsubst {
+       ContextFilter Filter;
+       int wcs_type;                       /* which type of Substitution are we */
+       char wcs_key[32];                   /* copy of our hashkey for debugging */
+       StrBuf *wcs_value;                  /* if we're a string, keep it here */
+       long lvalue;                        /* type long? keep data here */
+       WCHandlerFunc wcs_function; /* funcion hook ???*/
+} wcsubst;
+
+
 typedef struct _WCTemplate {
        StrBuf *Data;
        StrBuf *FileName;
@@ -156,9 +172,14 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
                        Type, 
                        ChrPtr(Error));
        }
+/*
        if (Target == NULL) 
                return;
+*/
        WCC = WC;
+       if (WCC == NULL)
+               return;
+
        Header = NewStrBuf();
        if (TP->Tokens != NULL) 
        {
@@ -190,7 +211,7 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
                                                      Error,
                                                      eERROR), 1);
 */
-               WildFireSerializePayload(Header, WCC->HBuf, &WCC->nWildfireHeaders, NULL);
+               WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
        }
        else
        {
@@ -207,7 +228,7 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
                             ChrPtr(Error), 
                             ChrPtr(TP->Tokens->FlatToken));
                SerializeJson(Header, WildFireException(HKEY(__FILE__), __LINE__, Info, 1), 1);
-               WildFireSerializePayload(Header, WCC->HBuf, &WCC->nWildfireHeaders, NULL);
+               WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
        }
        FreeStrBuf(&Header);
        FreeStrBuf(&Info);
@@ -219,6 +240,45 @@ void LogTemplateError (StrBuf *Target, const char *Type, int ErrorPos, WCTemplpu
 }
 
 
+
+
+void LogError (StrBuf *Target, const char *Type, const char *Format, ...)
+{
+       wcsession *WCC;
+       StrBuf *Header;
+       StrBuf *Error;
+       StrBuf *Info;
+        va_list arg_ptr;
+
+       Info = NewStrBuf();
+       Error = NewStrBuf();
+
+        va_start(arg_ptr, Format);
+       StrBufVAppendPrintf(Error, Format, arg_ptr);
+       va_end(arg_ptr);
+
+       lprintf(1, ChrPtr(Error));
+
+       WCC = WC;
+       Header = NewStrBuf();
+
+
+       SerializeJson(Header, WildFireException(Type, strlen(Type),
+                                               0,
+                                               Info,
+                                               1), 1);
+       WildFireSerializePayload(Header, WCC->HBuf, &WCC->Hdr->nWildfireHeaders, NULL);
+       
+       FreeStrBuf(&Header);
+       FreeStrBuf(&Info);
+       FreeStrBuf(&Error);
+/*
+       if (dbg_bactrace_template_errors)
+               wc_backtrace(); 
+*/
+}
+
+
 void RegisterNS(const char *NSName, 
                long len, 
                int nMinArgs, 
@@ -960,8 +1020,11 @@ void StrBufAppendTemplate(StrBuf *Target,
                StrEscAppend(Target, Source, NULL, 0, 0);
                break;
        case 'J':
-         StrECMAEscAppend(Target, Source, NULL);
+               StrECMAEscAppend(Target, Source, NULL);
          break;
+       case 'U':
+               StrBufUrlescAppend(Target, Source, NULL);
+               break;
        default:
                StrBufAppendBuf(Target, Source, 0);
        }
@@ -1124,6 +1187,9 @@ TemplateParam *GetNextParameter(StrBuf *Buf, const char **pCh, const char *pe, W
               (*pch == ',' )||
               (*pch == '\n')) pch ++;
 
+       if (DumpTemplateI18NStrings && (Parm->Type == TYPE_GETTEXT)) {
+               StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", Parm->Start);
+       }
        *pCh = pch;
        return Parm;
 }
@@ -1236,6 +1302,9 @@ WCTemplateToken *NewTemplateSubstitute(StrBuf *Buf,
                        NewToken->Flags = 0;
                        break;
                }
+               if (DumpTemplateI18NStrings) {
+                       StrBufAppendPrintf(I18nDump, "_(\"%s\");\n", NewToken->Params[0]->Start);
+               }
                break;
        case SV_SUBTEMPL:
                if (NewToken->nParameters != 1) {
@@ -1741,14 +1810,14 @@ void RegisterITERATOR(const char *Name, long len,
        Put(Iterators, Name, len, It, NULL);
 }
 
-/* typedef struct _iteratestruct {
+typedef struct _iteratestruct {
        int GroupChange;
        int oddeven;
        const char *Key;
        long KeyLen;
        int n;
        int LastN;
-       }IterateStruct; */
+       }IterateStruct; 
 
 void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
 {
@@ -1830,14 +1899,14 @@ void tmpl_iterate_subtmpl(StrBuf *Target, WCTemplputParams *TP)
        SubTP.Filter.ControlContextType = CTX_ITERATE;
        SubTP.ControlContext = &Status;
        
+       if (HAVE_PARAM(2)) {
+               StartAt = GetTemplateTokenNumber(Target, TP, 2, 0);
+       }
        if (HAVE_PARAM(3)) {
-               StartAt = GetTemplateTokenNumber(Target, TP, 3, 0);
+               StepWidth = GetTemplateTokenNumber(Target, TP, 3, 0);
        }
        if (HAVE_PARAM(4)) {
-               StepWidth = GetTemplateTokenNumber(Target, TP, 4, 0);
-       }
-       if (HAVE_PARAM(5)) {
-               StopAt = GetTemplateTokenNumber(Target, TP, 5, -1);
+               StopAt = GetTemplateTokenNumber(Target, TP, 4, -1);
        }
        it = GetNewHashPos(List, StepWidth);
        if (StopAt < 0) {
@@ -2465,4 +2534,74 @@ InitModule_SUBST
        RegisterControlNS(HKEY("ITERATE:N"), 0, 0, tmplput_ITERATE_LASTN, CTX_ITERATE);
 }
 
+void
+ServerStartModule_SUBST
+(void)
+{
+       WirelessTemplateCache = NewHash(1, NULL);
+       WirelessLocalTemplateCache = NewHash(1, NULL);
+       LocalTemplateCache = NewHash(1, NULL);
+       TemplateCache = NewHash(1, NULL);
+
+       GlobalNS = NewHash(1, NULL);
+       Iterators = NewHash(1, NULL);
+       Conditionals = NewHash(1, NULL);
+       SortHash = NewHash(1, NULL);
+}
+
+void
+FinalizeModule_SUBST
+(void)
+{
+
+}
+
+void 
+ServerShutdownModule_SUBST
+(void)
+{
+       DeleteHash(&WirelessTemplateCache);
+       DeleteHash(&WirelessLocalTemplateCache);
+       DeleteHash(&TemplateCache);
+       DeleteHash(&LocalTemplateCache);
+
+       DeleteHash(&GlobalNS);
+       DeleteHash(&Iterators);
+       DeleteHash(&Conditionals);
+       DeleteHash(&SortHash);
+
+}
+
+
+void
+SessionNewModule_SUBST
+(wcsession *sess)
+{
+
+}
+
+void
+SessionAttachModule_SUBST
+(wcsession *sess)
+{
+       sess->vars = NewHash(1,NULL);
+}
+
+void
+SessionDetachModule_SUBST
+(wcsession *sess)
+{
+       DeleteHash(&sess->vars);
+}
+
+void 
+SessionDestroyModule_SUBST  
+(wcsession *sess)
+{
+
+}
+
+
+
+
 /*@}*/