commit b1cd1b5ccd153c9ac9dd9577a8c7a53ef892c697
parent a6555720bd5a325452c152dcd6028dd89619102d
Author: Tomas Hlavaty <tom@logand.com>
Date: Fri, 28 Jan 2011 02:07:07 +0100
xpath_filter implemented
Diffstat:
M | w3mail.c | | | 71 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------ |
1 file changed, 65 insertions(+), 6 deletions(-)
diff --git a/w3mail.c b/w3mail.c
@@ -57,6 +57,64 @@ static int equal(char *str1, char *str2) {
return 0 == strcmp(str1, str2);
}
+static int sh_cp(char *fin, char *fout) {
+ return systemf("cp %s %s", fin, fout);
+}
+
+static int sh_tidy(char *fin, char *fout) {
+ // tidy fragile, no html5, tidy generator, not parser! replace
+ int x = systemf("tidy -q -n -c -asxml -o %s -f /dev/null %s", fout, fin);
+ return x == 0 || x == 1 ? 0 : x;
+}
+
+static int fix_xml(char *fin, char *fout) {
+ return systemf("~/picolisp/p @lib/xml.l -'xml (_xml)' <%s >%s", fin, fout);
+}
+
+static void fix_xpath(char *xpath) {
+ while(*xpath) {
+ if('"' == *xpath)
+ *xpath = '\'';
+ xpath++;
+ }
+}
+
+static int sh_xpath(char *fin, char *fout, char *xpath) {
+ char buf[BLEN];
+ strcpy(buf, xpath);
+ fix_xpath(buf);
+ return systemf("xmlstarlet sel -t -c \"%s\" <%s >%s", buf, fin, fout);
+}
+
+static int fix_namespace(char *fin, char *fout) {
+ // TODO pyx + p2x fragile, use something better
+ return systemf("cat %s | xmlstarlet pyx | grep -v Axmlns | xmlstarlet p2x >%s", fin, fout);
+}
+
+static int xpath_filter(char *fin, char *fout, char *xpath) {
+ if(sh_tidy(fin, fout)) {
+ if(fix_xml(fin, fout))
+ return 1;
+ else {
+ sh_cp(fout, fin);
+ if(sh_tidy(fin, fout))
+ return 1;
+ }
+ }
+ sh_cp(fout, fin);
+ if(fix_namespace(fin, fout))
+ return 1;
+ return sh_xpath(fin, fout, xpath);
+}
+
+static int custom_filter(char *fin, char *fout, char *cmd) {
+ return systemf("~/.w3mail/filter/%s <%s >%s", cmd, fin, fout);
+}
+
+static int default_filter(char *fin, char *fout) {
+ return systemf("~/.w3mail/filter/default <%s >%s", fin, fout);
+}
+
struct tidy {
char *fin;
char *fout;
@@ -75,16 +133,17 @@ static void cb_tidy(void *udata, FILE* in) {
sscanf(line, "%s %s %s %n", name, url, type, &n);
if(url[0] && head(url, x->url)) {
if(equal("xpath", type)) {
- if(!systemf("~/.w3mail/filter/xpath \"%s\" <%s >%s", &line[n], x->fin, x->fout))
- return;
- }
- else if(equal("filter", type)) {
- if(!systemf("~/.w3mail/filter/%s <%s >%s", &line[n], x->fin, x->fout))
+ if(!xpath_filter(x->fin, x->fout, &line[n]))
return;
}
+ else
+ if(equal("filter", type))
+ if(!custom_filter(x->fin, x->fout, &line[n]))
+ return;
}
}
- systemf("~/.w3mail/filter/default <%s >%s", x->fin, x->fout);
+ if(default_filter(x->fin, x->fout))
+ sh_cp(x->fin, x->fout);
}
static void tidy(char *fin, char *fout, char *url) {