1f806c370ec6b85d30483046c671c0177db9105c
[apps/pfixtools.git] / postlicyd / param_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 CONTRIBUTORS ``AS IS'' AND ANY EXPRESS   */
29 /*  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED         */
30 /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE    */
31 /*  DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY         */
32 /*  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL        */
33 /*  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS   */
34 /*  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)     */
35 /*  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,       */
36 /*  STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN  */
37 /*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
38 /*  POSSIBILITY OF SUCH DAMAGE.                                               */
39 /******************************************************************************/
40
41
42 /*****     THIS FILE IS AUTOGENERATED DO NOT MODIFY DIRECTLY !    *****/
43
44 EOF
45 }
46
47 do_h() {
48     do_hdr
49     cat <<EOF
50 #ifndef PFIXTOOLS_PARAMS_TOKENS_H
51 #define PFIXTOOLS_PARAMS_TOKENS_H
52
53 typedef enum param_token {
54     ATK_UNKNOWN = -1,
55 `grep_self "$0" | tr 'a-z-/' 'A-Z__' | sed -e 's/.*/    ATK_&,/'`
56     ATK_count,
57 } param_token;
58
59 extern const char *atokens[ATK_count];
60
61 __attribute__((pure))
62 param_token param_tokenize(const char *s, ssize_t len);
63 #endif /* PFIXTOOLS_PARAMS_TOKENS_H */
64 EOF
65 }
66
67 do_tokens() {
68     while read tok; do
69         echo "$tok, ATK_`echo $tok | tr 'a-z-' 'A-Z_'`"
70     done
71 }
72
73 do_c() {
74     this=`basename "$0"`
75     cat <<EOF | gperf -m16 -l -t -C -F",0" -Ntokenize_aux | \
76         sed -e '/__gnu_inline__/d;s/\<\(__\|\)inline\>//g'
77 %{
78 `do_hdr`
79
80 #include "str.h"
81 #include "`echo "${this%.sh}"`.h"
82
83 static const struct tok *
84 tokenize_aux(const char *str, unsigned int len);
85
86 %}
87 struct tok { const char *name; int val; };
88 %%
89 `grep_self "$0" | do_tokens`
90 %%
91
92 const char *atokens[ATK_count] = {
93 `grep_self "$0" | sed -e 's/.*/    "&",/'`
94 };
95
96 param_token param_tokenize(const char *s, ssize_t len)
97 {
98     if (len < 0)
99         len = m_strlen(s);
100
101     if (len) {
102         const struct tok *res = tokenize_aux(s, len);
103         return res ? res->val : ATK_UNKNOWN;
104     } else {
105         return ATK_UNKNOWN;
106     }
107 }
108 EOF
109 }
110
111 grep_self() {
112     ( ( grep 'filter_param_register(.*, *"' *.c | sed 's/.*filter_param_register(.*, *"\(.*\)" *).*/\1/' ) &&
113       ( grep 'config_param_register( *".*' config.c | sed 's/.*config_param_register( *"\(.*\)".*/\1/' ) ) | sort | uniq
114 }
115
116 trap "rm -f $1" 1 2 3 15
117 rm -f $1
118 case "$1" in
119     *.h) do_h > $1;;
120     *.c) do_c > $1;;
121     *)  die "you must ask for the 'h' or 'c' generation";;
122 esac
123 chmod -w $1
124
125 exit 0