[gq-commit] gq/src xmlparse.c,1.2,1.3
Status: Beta
Brought to you by:
sur5r
From: <bi...@us...> - 2003-10-05 02:14:04
|
Update of /cvsroot/gqclient/gq/src In directory sc8-pr-cvs1:/tmp/cvs-serv15400 Modified Files: xmlparse.c Log Message: when reading a pref-UTF-8 version of ~/.gq, convert previously badly or not at all) escaped special XML chars into their properly escaped versions before passing to the XML parser Index: xmlparse.c =================================================================== RCS file: /cvsroot/gqclient/gq/src/xmlparse.c,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** xmlparse.c 4 Oct 2003 10:09:02 -0000 1.2 --- xmlparse.c 5 Oct 2003 02:13:59 -0000 1.3 *************** *** 27,30 **** --- 27,34 ---- #include <string.h> + #include <sys/types.h> + #include <sys/stat.h> + #include <unistd.h> + #include <gtk/gtk.h> *************** *** 306,310 **** struct parser_context ctx; struct tagstack *stack = new_tagstack(); ! int rc; int handler_is_local = 0; #if GTK_MAJOR >= 2 --- 310,317 ---- struct parser_context ctx; struct tagstack *stack = new_tagstack(); ! struct stat sfile; ! char *cfgbuf; ! GString *newbuf; ! int rc, i, changed; int handler_is_local = 0; #if GTK_MAJOR >= 2 *************** *** 341,344 **** --- 348,428 ---- rewind(fp); + + if (strstr(firstline, "encoding=") == NULL ) { + /* more nastiness... since this config file was written by a + pre-UTF-8 version of GQ, it's not going to have <>&'" escaped + properly... fudge this now: silently rewrite the config + file on disk, then reopen it for the XML parser */ + changed = 0; + stat(file, &sfile); + cfgbuf = g_malloc(sfile.st_size + 1); + fread(cfgbuf, 1, sfile.st_size, fp); + cfgbuf[sfile.st_size] = 0; + newbuf = g_string_sized_new(sfile.st_size + 128); + for(i = 0; cfgbuf[i]; i++) { + switch(cfgbuf[i]) { + case '\\': + /* < and > used to be escaped with a \ */ + if(cfgbuf[i + 1] == '<') { + g_string_append(newbuf, "<"); + changed++; + i++; + } + else if(cfgbuf[i + 1] == '>') { + g_string_append(newbuf, ">"); + changed++; + i++; + } + else + g_string_append_c(newbuf, cfgbuf[i]); + break; + case '"': + /* this wasn't escaped at all */ + if(i > strlen(firstline)) { + /* only escape it if it's not in the + first line *argh* */ + g_string_append(newbuf, """); + changed++; + } + else + g_string_append_c(newbuf, '"'); + break; + case '\'': + /* neither was this */ + g_string_append(newbuf, "'"); + changed++; + break; + case '&': + /* if it's not the start of an XML escape, it + must be an illegal & */ + if(strncmp(cfgbuf + i + 1, "amp;", 4) && + strncmp(cfgbuf + i + 1, "lt;", 3) && + strncmp(cfgbuf + i + 1, "gt;", 3) && + strncmp(cfgbuf + i + 1, "apos;", 5) && + strncmp(cfgbuf + i + 1, "quot;", 5)) { + g_string_append(newbuf, "&"); + changed++; + } + else + g_string_append_c(newbuf, cfgbuf[i]); + break; + default: + g_string_append_c(newbuf, cfgbuf[i]); + } + } + + if(changed) { + fclose(fp); + fp = fopen(file, "w"); + fwrite(newbuf->str, 1, newbuf->len, fp); + fclose(fp); + fp = fopen(file, "r"); + } + else + rewind(fp); + + g_free(cfgbuf); + g_string_free(newbuf, TRUE); + } xmlParserCtxtPtr parser = xmlCreateIOParserCtxt(handler, |