Lot of work on postlicyd, also make the repository a general postfix-addons repo.
[apps/pfixtools.git] / tokens.sh
1 #! /bin/sh -e
2
3 die() {
4     echo "$@" 1>&2
5     exit 2
6 }
7
8 do_hdr() {
9     cat <<EOF
10 /******************************************************************************/
11 /*          postlicyd: a postfix policy daemon with a lot of features         */
12 /*          ~~~~~~~~~                                                         */
13 /*  ________________________________________________________________________  */
14 /*                                                                            */
15 /*  Redistribution and use in source and binary forms, with or without        */
16 /*  modification, are permitted provided that the following conditions        */
17 /*  are met:                                                                  */
18 /*                                                                            */
19 /*  1. Redistributions of source code must retain the above copyright         */
20 /*     notice, this list of conditions and the following disclaimer.          */
21 /*  2. Redistributions in binary form must reproduce the above copyright      */
22 /*     notice, this list of conditions and the following disclaimer in the    */
23 /*     documentation and/or other materials provided with the distribution.   */
24 /*  3. The names of its contributors may not be used to endorse or promote    */
25 /*     products derived from this software without specific prior written     */
26 /*     permission.                                                            */
27 /*                                                                            */
28 /*  THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND   */
29 /*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE     */
30 /*  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR        */
31 /*  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS    */
32 /*  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR    */
33 /*  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF      */
34 /*  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  */
35 /*  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN   */
36 /*  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)   */
37 /*  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF    */
38 /*  THE POSSIBILITY OF SUCH DAMAGE.                                           */
39 /******************************************************************************/
40
41 /*****     THIS FILE IS AUTOGENERATED DO NOT MODIFY DIRECTLY !    *****/
42
43 EOF
44 }
45
46 do_h() {
47     do_hdr
48     cat <<EOF
49 #ifndef POSTLICYD_TOKENS_H
50 #define POSTLICYD_TOKENS_H
51
52 typedef enum postlicyd_token {
53     PTK_UNKNOWN = -1,
54 `grep_self "$0" | tr 'a-z-/' 'A-Z__' | sed -e 's/.*/    PTK_&,/'`
55     PTK_count,
56 } postlicyd_token;
57
58 extern const char *ptokens[PTK_count];
59
60 __attribute__((pure))
61 postlicyd_token tokenize(const char *s, ssize_t len);
62 #endif /* MUTT_LIB_LUA_LUA_TOKEN_H */
63 EOF
64 }
65
66 do_tokens() {
67     while read tok; do
68         echo "$tok, PTK_`echo $tok | tr 'a-z-' 'A-Z_'`"
69     done
70 }
71
72 do_c() {
73     cat <<EOF | gperf -m16 -l -t -C -F",0" -Ntokenize_aux
74 %{
75 `do_hdr`
76
77 #include "str.h"
78 #include "tokens.h"
79
80 static const struct tok *
81 tokenize_aux(const char *str, unsigned int len);
82
83 %}
84 struct tok { const char *name; int val; };
85 %%
86 `grep_self "$0" | do_tokens`
87 %%
88
89 const char *ptokens[PTK_count] = {
90 `grep_self "$0" | sed -e 's/.*/    "&",/'`
91 };
92
93 postlicyd_token tokenize(const char *s, ssize_t len)
94 {
95     if (len < 0)
96         len = m_strlen(s);
97
98     if (len) {
99         const struct tok *res = tokenize_aux(s, len);
100         return res ? res->val : PTK_UNKNOWN;
101     } else {
102         return PTK_UNKNOWN;
103     }
104 }
105 EOF
106 }
107
108 grep_self() {
109     grep '^## ' "$1" | cut -d' ' -f2
110 }
111
112 trap "rm -f $1" 1 2 3 15
113 rm -f $1
114 case "$1" in
115     *.h) do_h > $1;;
116     *.c) do_c > $1;;
117     *)  die "you must ask for the 'h' or 'c' generation";;
118 esac
119 chmod -w $1
120
121 exit 0
122
123 ############ Put tokens here ############
124 # postfix 2.1+
125 ## request
126 ## protocol_name
127 ## protocol_state
128 ## helo_name
129 ## queue_id
130 ## sender
131 ## recipient
132 ## recipient_count
133 ## client_address
134 ## client_name
135 ## rclient_name
136 ## instance
137 #
138 # postfix 2.2+
139 ## sasl_method
140 ## sasl_username
141 ## sasl_sender
142 ## size
143 ## ccert_subject
144 ## ccert_issuer
145 ## ccsert_fingerprint
146 #
147 # postfix 2.3+
148 ## encryption_protocol
149 ## encryption_cipher
150 ## encryption_keysize
151 ## etrn_domain
152 #
153 # request value
154 ## smtpd_access_policy
155 #
156 # protocol_name values
157 ## SMTP
158 ## ESMTP
159 #
160 # protocol_state values
161 ## CONNECT
162 ## EHLO
163 ## HELO
164 ## MAIL
165 ## RCPT
166 ## DATA
167 ## END-OF-MESSAGE
168 ## VRFY
169 ## ETRN