#include "trio.h"
 
 #include "xml_conversions.h"
+#include "escape.h"
 
 
 
 /* XMLCtx->eventsOutGlobal[XMLCtx->position].any.prog = edg_wll_from_string_to_string(XMLCtx); */
 char *edg_wll_from_string_to_string(edg_wll_XML_ctx *XMLCtx)
 {
-       return(XMLCtx->char_buf);
+       char *s;
+
+       s = edg_wll_UnescapeXML((const char *) XMLCtx->char_buf);
+       edg_wll_freeBuf(XMLCtx);
+       return ret;
 } 
 
 
 /* XMLCtx->eventsOutGlobal[XMLCtx->position].any.jobId = edg_wll_from_string_to_dgJobId(XMLCtx); */
 edg_wlc_JobId edg_wll_from_string_to_jobid(edg_wll_XML_ctx *XMLCtx)
 {
-       edg_wlc_JobId out;
+       edg_wlc_JobId out = NULL;
+       char *s;
 
-       edg_wlc_JobIdParse(XMLCtx->char_buf, &out);
+       s = edg_wll_UnescapeXML((const char *) XMLCtx->char_buf);
+       if (s) {
+          edg_wlc_JobIdParse(s, &out);
+          free(s);
+       }
        edg_wll_freeBuf(XMLCtx); 
 
        return(out);
 
 edg_wll_NotifId edg_wll_from_string_to_notifid(edg_wll_XML_ctx *XMLCtx)
 {
-       edg_wll_NotifId out;
+       edg_wll_NotifId out = NULL;
+       char *s;
 
-       edg_wll_NotifIdParse(XMLCtx->char_buf, &out);
+       s = edg_wll_UnescapeXML((const char *) XMLCtx->char_buf);
+       if (s) {
+          edg_wll_NotifIdParse(s, &out);
+          free(s);
+       }
        edg_wll_freeBuf(XMLCtx); 
 
        return(out);
 
 edg_wll_JobStatCode edg_wll_from_string_to_edg_wll_JobStatCode(edg_wll_XML_ctx *XMLCtx)
 {
-       edg_wll_JobStatCode out;
+       edg_wll_JobStatCode out = EDG_WLL_JOB_UNDEF;
+       char *s;
        
-       out = edg_wll_StringToStat(XMLCtx->char_buf);
+       s = edg_wll_UnescapeXML((const char *) XMLCtx->char_buf);
+       if (s) {
+          out = edg_wll_StringToStat(s);
+          free(s);
+       }
        edg_wll_freeBuf(XMLCtx); 
 
        return(out);
        edg_wll_from_string_to_int(XMLCtx);                             */
 int edg_wll_from_string_to_int(edg_wll_XML_ctx *XMLCtx)
 {
-        int out;
+        int out = -1;
+       char *s;
 
-        out = atoi(XMLCtx->char_buf);
+       s = edg_wll_UnescapeXML((const char *) XMLCtx->char_buf);
+       if (s) {
+          out = atoi(s);
+          free(s);
+       }
         edg_wll_freeBuf(XMLCtx);
 
         return(out);
        edg_wll_from_string_to_int(XMLCtx);                             */
 float edg_wll_from_string_to_float(edg_wll_XML_ctx *XMLCtx)
 {
-        float out;
+        float out = -1;
+       char *s;
 
-        out = strtof(XMLCtx->char_buf, (char **) NULL);
+       s = edg_wll_UnescapeXML((const char *) XMLCtx->char_buf);
+       if (s) {
+          out = strtof(s, (char **) NULL);
+          free(s);
+       }
         edg_wll_freeBuf(XMLCtx);
 
         return(out);
 
 long edg_wll_from_string_to_long(edg_wll_XML_ctx *XMLCtx)
 {
-        long out;
+        long out = -1;
+       char *s;
 
-        out = atol(XMLCtx->char_buf);
+       s = edg_wll_UnescapeXML((const char *) XMLCtx->char_buf);
+       if (s) {
+          out = atol(s);
+          free(s);
+       }
         edg_wll_freeBuf(XMLCtx);
 
         return(out);
 
 struct timeval edg_wll_from_string_to_timeval(edg_wll_XML_ctx *XMLCtx)
 {
-       struct timeval out;
+       struct timeval out = {0,0};
        char *needle, *nil;
+       char *s;
 
-       out.tv_sec  = strtol(XMLCtx->char_buf, &needle, 10);
-       out.tv_usec = strtol(needle+1, &nil, 10);
+       s = edg_wll_UnescapeXML((const char *) XMLCtx->char_buf);
+       if (s) {
+          out.tv_sec  = strtol(s, &needle, 10);
+          out.tv_usec = strtol(needle+1, &nil, 10);
+          free(s);
+       }
        edg_wll_freeBuf(XMLCtx);
 
        return(out);
 
 edg_wll_Source edg_wll_from_string_to_logsrc(edg_wll_XML_ctx *XMLCtx)
 {
-       edg_wll_Source  out = edg_wll_StringToSource(XMLCtx->char_buf);
-       
+       edg_wll_Source  out = EDG_WLL_SOURCE_NONE;
+       char *s;
+
+       s = edg_wll_UnescapeXML((const char *) XMLCtx->char_buf);
+       if (s) {
+          out = edg_wll_StringToSource(s);
+          free(s);
+       }
        edg_wll_freeBuf(XMLCtx);
        return(out);
 }
 
 static void char_handler(void *data, const char *s, int len)
 {
        edg_wll_XML_ctx *XMLCtx = data;
-        int i, found = -1, temp_len1;
-        char *temp_s, *temp_s1;
+        int i, found = -1;
+        char *temp_s;
 
 
         /* if date are only spaces, t\, \r, \n ... don't bother with them */
                         if (!isspace(s[i])) { found = i; break; }
         if (found == -1) return;
 
-        temp_s = malloc((len+1) * sizeof(char));
-
-        /* otherwise use them */
-        memcpy(temp_s,s,len);
-        temp_s[len] = 0;
-        temp_s1 = edg_wll_UnescapeXML((const char *) temp_s);
-        temp_len1 = strlen(temp_s1);
-
-        if (XMLCtx->char_buf_len) 
-               XMLCtx->char_buf = realloc(XMLCtx->char_buf,XMLCtx->char_buf_len+temp_len1 + 1);
-        else  
-               XMLCtx->char_buf = (char *) malloc(temp_len1 + 1);
-
-       memcpy(XMLCtx->char_buf+XMLCtx->char_buf_len,temp_s1,temp_len1 + 1);
-               XMLCtx->char_buf_len += temp_len1;
-        free(temp_s1);
-        free(temp_s);
+       temp_s = realloc(XMLCtx->char_buf, XMLCtx->char_buf_len + len + 1);
+       if (temp_s == NULL) {
+          /* XXX propagate ENOMEM somehow */
+          return;
+       }
+       XMLCtx->char_buf = temp_s;
+       memcpy(XMLCtx->char_buf + XMLCtx->char_buf_len, s, len);
+       XMLCtx->char_buf[XMLCtx->char_buf_len + len] = '\0';
+       XMLCtx->char_buf_len += len;
 }
 
 
                        break;
                default:
                        /* only level 2 tags should contain  text fields    */
+                       /* XXX is it necessary to unescape the data first? */
                        for (i=0; i < XMLCtx->char_buf_len; i++) 
                                if (!isspace(XMLCtx->char_buf[i])) unexpWarning()
                        edg_wll_freeBuf(XMLCtx);
                        if ( (XMLCtx->purgeResultGlobal.jobs[XMLCtx->position++] = 
                                edg_wll_from_string_to_string(XMLCtx)) == NULL )
                        {
+                               /* XXX char_buf contains an escaped value, unescaping is done within edg_wll_from_string_to_string(), which failed */
                                if (XMLCtx->errtxt) {
                                        asprintf(&e,"%s\n%s: invalid JobId at line %d",
                                                XMLCtx->errtxt, XMLCtx->char_buf,