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