From 717c9f150ded0fed572d27f68db07f94a9fe70e8 Mon Sep 17 00:00:00 2001 From: Pierre Habouzit Date: Fri, 30 Mar 2007 11:24:40 +0200 Subject: [PATCH] proper handling of regex lists. Signed-off-by: Pierre Habouzit --- charset.cpkg | 4 ++-- init.c | 2 +- lib-lib/rx.c | 35 ++++++++++++++++++++++++++++++++++- lib-lib/rx.h | 15 ++++++++++----- lib-mime/mime.cpkg | 6 ++++-- 5 files changed, 51 insertions(+), 11 deletions(-) diff --git a/charset.cpkg b/charset.cpkg index 42eba4d..b2b4d79 100644 --- a/charset.cpkg +++ b/charset.cpkg @@ -128,13 +128,13 @@ static void charset_onchange(const char *cset) void charset_hook(rx_t local, const string_t alias) { rx_set_template(local, alias); - rx_list_append(&charset_hooks, local); + rx_list_add2(&charset_hooks, &local); RETURN(); }; void iconv_hook(rx_t local, const string_t alias) { rx_set_template(local, alias); - rx_list_append(&iconv_hooks, local); + rx_list_add2(&iconv_hooks, &local); RETURN(); }; }; diff --git a/init.c b/init.c index 775cb21..9d6908b 100644 --- a/init.c +++ b/init.c @@ -582,7 +582,7 @@ static int remove_from_rx_list(rx_t **l, const char *str) } l = rx_lookup(l, str); - if (l) { + if (*l) { rx_t *r = rx_list_pop(l); rx_delete(&r); return 0; diff --git a/lib-lib/rx.c b/lib-lib/rx.c index 79ec5b8..d89d234 100644 --- a/lib-lib/rx.c +++ b/lib-lib/rx.c @@ -148,7 +148,40 @@ rx_t **rx_lookup(rx_t **l, const char *pat) l = &(*l)->next; } - return NULL; + return l; +} + +void rx_list_add(rx_t **l, rx_t *rxp) +{ + l = rx_lookup(l, rxp->pattern); + if (*l) { + rx_t *r = rx_list_pop(l); + rx_delete(&r); + } + rx_list_push(l, rxp); +} + +void rx_list_add2(rx_t **l, rx_t **rxp) +{ + l = rx_lookup(l, (*rxp)->pattern); + if (*l) { + rx_t *r = rx_list_pop(l); + rx_delete(&r); + } + if (m_strisempty((*rxp)->tpl)) { + rx_delete(rxp); + } else { + rx_list_push(l, *rxp); + } +} + +void rx_list_remove(rx_t **l, const rx_t *r) +{ + l = rx_lookup(l, r->pattern); + if (*l) { + rx_t *tmp = rx_list_pop(l); + rx_delete(&tmp); + } } int rx_sanitize_string(char *dst, ssize_t n, const char *src) diff --git a/lib-lib/rx.h b/lib-lib/rx.h index 81352ad..fcaba32 100644 --- a/lib-lib/rx.h +++ b/lib-lib/rx.h @@ -49,18 +49,23 @@ typedef struct rx_t { } rx_t; rx_t *rx_compile(const char*, int); -int rx_validate(const char*, char*, ssize_t); -void rx_set_template(rx_t *, const char*); void rx_wipe(rx_t *); DO_DELETE(rx_t, rx); - DO_SLIST(rx_t, rx, rx_delete); +int rx_validate(const char*, char*, ssize_t); +void rx_set_template(rx_t *, const char*); +int rx_sanitize_string(char *, ssize_t, const char *); + /* for handling lists */ +rx_t **rx_lookup(rx_t**, const char*); /* lookup pattern */ + int rx_list_match(rx_t *, const char*); /* match all items list agains string */ +void rx_list_add(rx_t **, rx_t *); +void rx_list_remove(rx_t **, const rx_t *); + int rx_list_match2(rx_t *l, const char *s, char *dst, int dlen); -rx_t **rx_lookup(rx_t**, const char*); /* lookup pattern */ -int rx_sanitize_string(char *, ssize_t, const char *); +void rx_list_add2(rx_t **, rx_t **); #define REGCOMP(X,Y,Z) regcomp(X, Y, REG_WORDS|REG_EXTENDED|(Z)) #define REGEXEC(X,Y) regexec(X, Y, 0, NULL, 0) diff --git a/lib-mime/mime.cpkg b/lib-mime/mime.cpkg index 1e97613..3da646a 100644 --- a/lib-mime/mime.cpkg +++ b/lib-mime/mime.cpkg @@ -67,7 +67,8 @@ rx_t *SpamList = NULL, *NoSpamList = NULL; void spam(rx_t rx, const string_t tpl) { rx_set_template(rx, tpl); - rx_list_append(&SpamList, rx); + rx_list_add2(&SpamList, &rx); + rx_list_remove(&NoSpamList, rx); RETURN(); }; @@ -77,7 +78,8 @@ rx_t *SpamList = NULL, *NoSpamList = NULL; rx_list_wipe(&NoSpamList); rx_delete(&rx); } else { - rx_list_append(&NoSpamList, rx); + rx_list_remove(&SpamList, rx); + rx_list_add2(&NoSpamList, &rx); } RETURN(); }; -- 2.20.1