4e1abb35d0d6bb7e2dcfb884046f55e602419295
[apps/pfixtools.git] / postlicyd / 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 PFIXTOOLS_TOKENS_H
50 #define PFIXTOOLS_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         sed -e '/__gnu_inline__/d;s/\<\(__\|\)inline\>//g'
75 %{
76 `do_hdr`
77
78 #include "str.h"
79 #include "tokens.h"
80
81 static const struct tok *
82 tokenize_aux(const char *str, unsigned int len);
83
84 %}
85 struct tok { const char *name; int val; };
86 %%
87 `grep_self "$0" | do_tokens`
88 %%
89
90 const char *ptokens[PTK_count] = {
91 `grep_self "$0" | sed -e 's/.*/    "&",/'`
92 };
93
94 postlicyd_token tokenize(const char *s, ssize_t len)
95 {
96     if (len < 0)
97         len = m_strlen(s);
98
99     if (len) {
100         const struct tok *res = tokenize_aux(s, len);
101         return res ? res->val : PTK_UNKNOWN;
102     } else {
103         return PTK_UNKNOWN;
104     }
105 }
106 EOF
107 }
108
109 grep_self() {
110     grep '^## ' "$1" | cut -d' ' -f2
111 }
112
113 trap "rm -f $1" 1 2 3 15
114 rm -f $1
115 case "$1" in
116     *.h) do_h > $1;;
117     *.c) do_c > $1;;
118     *)  die "you must ask for the 'h' or 'c' generation";;
119 esac
120 chmod -w $1
121
122 exit 0
123
124 ############ Put tokens here ############
125 # postfix 2.1+
126 ## request
127 ## protocol_name
128 ## protocol_state
129 ## helo_name
130 ## queue_id
131 ## sender
132 ## recipient
133 ## recipient_count
134 ## client_address
135 ## client_name
136 ## reverse_client_name
137 ## instance
138 #
139 # postfix 2.2+
140 ## sasl_method
141 ## sasl_username
142 ## sasl_sender
143 ## size
144 ## ccert_subject
145 ## ccert_issuer
146 ## ccert_fingerprint
147 #
148 # postfix 2.3+
149 ## encryption_protocol
150 ## encryption_cipher
151 ## encryption_keysize
152 ## etrn_domain
153 #
154 # postfix 2.5+
155 ## stress
156 #
157 # request value
158 ## smtpd_access_policy
159 #
160 # protocol_name values
161 ## SMTP
162 ## ESMTP
163 #
164 # protocol_state values
165 ## CONNECT
166 ## EHLO
167 ## HELO
168 ## MAIL
169 ## RCPT
170 ## DATA
171 ## END-OF-MESSAGE
172 ## VRFY
173 ## ETRN