Refactor, prepare gperf things needed for the parse.
[apps/pfixtools.git] / tokens.sh
old mode 100644 (file)
new mode 100755 (executable)
similarity index 55%
rename from query.h
rename to tokens.sh
index f8df50a..9a1faaf
--- a/query.h
+++ b/tokens.sh
@@ -1,3 +1,12 @@
+#! /bin/sh -e
+
+die() {
+    echo "$@" 1>&2
+    exit 2
+}
+
+do_hdr() {
+    cat <<EOF
 /******************************************************************************/
 /*          postlicyd: a postfix policy daemon with a lot of features         */
 /*          ~~~~~~~~~                                                         */
 /*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
 /******************************************************************************/
 
-/*
- * Copyright © 2007 Pierre Habouzit
- */
-
-#ifndef POSTLICYD_QUERY_H
-#define POSTLICYD_QUERY_H
-
-#include "buffer.h"
-
-typedef struct query_t {
-    unsigned state : 4;
-    unsigned esmtp : 1;
-
-    const char *helo_name;
-    const char *queue_id;
-    const char *sender;
-    const char *recipient;
-    const char *recipient_count;
-    const char *client_address;
-    const char *client_name;
-    const char *rclient_name;
-    const char *instance;
-
-    /* postfix 2.2+ */
-    const char *sasl_method;
-    const char *sasl_username;
-    const char *sasl_sender;
-    const char *size;
-    const char *ccert_subject;
-    const char *ccert_issuer;
-    const char *ccsert_fingerprint;
-
-    /* postfix 2.3+ */
-    const char *encryption_protocol;
-    const char *encryption_cipher;
-    const char *encryption_keysize;
-    const char *etrn_domain;
-
-    buffer_t data;
-} query_t;
-
-static inline query_t *query_init(query_t *rq) {
-    p_clear(rq, 1);
-    buffer_init(&rq->data);
-    return rq;
+/*****     THIS FILE IS AUTOGENERATED DO NOT MODIFY DIRECTLY !    *****/
+
+EOF
+}
+
+do_h() {
+    do_hdr
+    cat <<EOF
+#ifndef POSTLICYD_TOKENS_H
+#define POSTLICYD_TOKENS_H
+
+typedef enum postlicyd_token {
+    PTK_UNKNOWN = -1,
+`grep_self "$0" | tr 'a-z-/' 'A-Z__' | sed -e 's/.*/    PTK_&,/'`
+    PTK_count,
+} postlicyd_token;
+
+extern const char *ptokens[PTK_count];
+
+__attribute__((pure))
+postlicyd_token tokenize(const char *s, ssize_t len);
+#endif /* MUTT_LIB_LUA_LUA_TOKEN_H */
+EOF
 }
-static inline void query_wipe(query_t *rq) {
-    buffer_wipe(&rq->data);
+
+do_tokens() {
+    while read tok; do
+        echo "$tok, PTK_`echo $tok | tr 'a-z-' 'A-Z_'`"
+    done
 }
-DO_NEW(query_t, query);
-DO_DELETE(query_t, query);
 
-#endif
+do_c() {
+    cat <<EOF | gperf -m16 -l -t -C -F",0" -Ntokenize_aux
+%{
+`do_hdr`
+
+#include "str.h"
+#include "tokens.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 *ptokens[PTK_count] = {
+`grep_self "$0" | sed -e 's/.*/    "&",/'`
+};
+
+postlicyd_token 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 : PTK_UNKNOWN;
+    } else {
+        return PTK_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 ############
+# postfix 2.1+
+## request
+## protocol_state
+## protocol_esmtp
+## helo_name
+## queue_id
+## sender
+## recipient
+## recipient_count
+## client_address
+## client_name
+## rclient_name
+## instance
+#
+# postfix 2.2+
+## sasl_method
+## sasl_username
+## sasl_sender
+## size
+## ccert_subject
+## ccert_issuer
+## ccsert_fingerprint
+#
+# postfix 2.3+
+## encryption_protocol
+## encryption_cipher
+## encryption_keysize
+## etrn_domain