From: Florent Bruneau Date: Tue, 9 Sep 2008 16:12:56 +0000 (+0200) Subject: Prepare tokenizer for filter names. X-Git-Url: http://git.madism.org/?p=apps%2Fpfixtools.git;a=commitdiff_plain;h=6aafe01d02acf2c2688ba7047b6f2f738afff50a Prepare tokenizer for filter names. Signed-off-by: Florent Bruneau --- diff --git a/.gitignore b/.gitignore index 10177d5..9d92621 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,8 @@ -/tokens.[hc] +*_tokens.[hc] -/postlicyd -/tst-rbl -/pfix-srsd +/postlicyd/postlicyd +/postlicyd/tst-rbl +/pfix-srsd/pfix-srsd *.[ao] .tags diff --git a/mk/common.mk b/mk/common.mk index 3fcb4e4..12e943b 100644 --- a/mk/common.mk +++ b/mk/common.mk @@ -6,13 +6,13 @@ CFLAGS += --std=gnu99 -D_GNU_SOURCE -I../ -I../common INSTALL_PROGS = $(addprefix install-,$(PROGRAMS)) +all: $(GENERATED) $(LIBS) $(PROGRAMS) | $(GENERATED) + install: all $(INSTALL_PROGS) $(INSTALL_PROGS): install-%: install $* $(DESTDIR)$(prefix)/sbin -all: $(GENERATED) $(LIBS) $(PROGRAMS) | $(GENERATED) - clean: $(RM) $(LIBS:=.a) $(PROGRAMS) $(TESTS) .*.o .*.dep diff --git a/postlicyd/Makefile b/postlicyd/Makefile index 5a0ab49..7331063 100644 --- a/postlicyd/Makefile +++ b/postlicyd/Makefile @@ -32,7 +32,7 @@ include ../mk/tc.mk PROGRAMS = postlicyd -GENERATED = tokens.h tokens.c +GENERATED = policy_tokens.h policy_tokens.c filter_tokens.h filter_tokens.c TESTS = test-rbl postlicyd_SOURCES = greylist.c rbl.c main-postlicyd.c $(GENERATED) ../common/lib.a diff --git a/postlicyd/filter_tokens.sh b/postlicyd/filter_tokens.sh new file mode 100755 index 0000000..f34d36d --- /dev/null +++ b/postlicyd/filter_tokens.sh @@ -0,0 +1,128 @@ +#! /bin/sh -e + +die() { + echo "$@" 1>&2 + exit 2 +} + +do_hdr() { + cat <//g' +%{ +`do_hdr` + +#include "str.h" +#include "`echo "${this%.sh}"`.h" + +static const struct tok * +tokenize_aux(const char *str, unsigned int len); + +%} +struct tok { const char *name; int val; }; +%% +`grep_self "$0" | do_tokens` +%% + +const char *ftokens[FTK_count] = { +`grep_self "$0" | sed -e 's/.*/ "&",/'` +}; + +filter_token filter_tokenize(const char *s, ssize_t len) +{ + if (len < 0) + len = m_strlen(s); + + if (len) { + const struct tok *res = tokenize_aux(s, len); + return res ? res->val : FTK_UNKNOWN; + } else { + return FTK_UNKNOWN; + } +} +EOF +} + +grep_self() { + grep '^## ' "$1" | cut -d' ' -f2 +} + +trap "rm -f $1" 1 2 3 15 +rm -f $1 +case "$1" in + *.h) do_h > $1;; + *.c) do_c > $1;; + *) die "you must ask for the 'h' or 'c' generation";; +esac +chmod -w $1 + +exit 0 + +############ Put tokens here ############ +## rbl +## greylist +## poisonous diff --git a/postlicyd/main-postlicyd.c b/postlicyd/main-postlicyd.c index deb90b3..950e581 100644 --- a/postlicyd/main-postlicyd.c +++ b/postlicyd/main-postlicyd.c @@ -39,7 +39,7 @@ #include "buffer.h" #include "common.h" #include "epoll.h" -#include "tokens.h" +#include "policy_tokens.h" #include "server.h" #define DAEMON_NAME "postlicyd" @@ -144,8 +144,8 @@ static int postfix_parsejob(query_t *query, char *p) for (vlen = p - v; vlen && isblank(v[vlen]); vlen--); p += 1; /* skip \n */ - vtk = tokenize(v, vlen); - switch (tokenize(k, klen)) { + vtk = policy_tokenize(v, vlen); + switch (policy_tokenize(k, klen)) { #define CASE(up, low) case PTK_##up: query->low = v; v[vlen] = '\0'; break; CASE(HELO_NAME, helo_name); CASE(QUEUE_ID, queue_id); diff --git a/postlicyd/tokens.sh b/postlicyd/policy_tokens.sh similarity index 95% rename from postlicyd/tokens.sh rename to postlicyd/policy_tokens.sh index 4e1abb3..1a6b0f7 100755 --- a/postlicyd/tokens.sh +++ b/postlicyd/policy_tokens.sh @@ -1,4 +1,4 @@ -#! /bin/sh -e +#! /bin/bash -e die() { echo "$@" 1>&2 @@ -58,7 +58,7 @@ typedef enum postlicyd_token { extern const char *ptokens[PTK_count]; __attribute__((pure)) -postlicyd_token tokenize(const char *s, ssize_t len); +postlicyd_token policy_tokenize(const char *s, ssize_t len); #endif /* MUTT_LIB_LUA_LUA_TOKEN_H */ EOF } @@ -70,13 +70,14 @@ do_tokens() { } do_c() { + this=`basename "$0"` cat <//g' %{ `do_hdr` #include "str.h" -#include "tokens.h" +#include "`echo "${this%.sh}"`.h" static const struct tok * tokenize_aux(const char *str, unsigned int len); @@ -91,7 +92,7 @@ const char *ptokens[PTK_count] = { `grep_self "$0" | sed -e 's/.*/ "&",/'` }; -postlicyd_token tokenize(const char *s, ssize_t len) +postlicyd_token policy_tokenize(const char *s, ssize_t len) { if (len < 0) len = m_strlen(s);