Tokenize params too.
[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 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_PARAMS_TOKENS_H
50 #define PFIXTOOLS_PARAMS_TOKENS_H
51
52 typedef enum param_token {
53     ATK_UNKNOWN = -1,
54 `grep_self "$0" | tr 'a-z-/' 'A-Z__' | sed -e 's/.*/    ATK_&,/'`
55     ATK_count,
56 } param_token;
57
58 extern const char *atokens[ATK_count];
59
60 __attribute__((pure))
61 param_token param_tokenize(const char *s, ssize_t len);
62 #endif /* PFIXTOOLS_PARAMS_TOKENS_H */
63 EOF
64 }
65
66 do_tokens() {
67     while read tok; do
68         echo "$tok, ATK_`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 *atokens[ATK_count] = {
92 `grep_self "$0" | sed -e 's/.*/    "&",/'`
93 };
94
95 param_token param_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 : ATK_UNKNOWN;
103     } else {
104         return ATK_UNKNOWN;
105     }
106 }
107 EOF
108 }
109
110 grep_self() {
111     ( ( grep 'filter_param_register(.*, *"' *.c | sed 's/.*filter_param_register(.*, *"\(.*\)" *).*/\1/' ) &&
112       ( grep 'config_param_register( *".*' config.c | sed 's/.*config_param_register( *"\(.*\)".*/\1/' ) ) | sort | uniq
113 }
114
115 trap "rm -f $1" 1 2 3 15
116 rm -f $1
117 case "$1" in
118     *.h) do_h > $1;;
119     *.c) do_c > $1;;
120     *)  die "you must ask for the 'h' or 'c' generation";;
121 esac
122 chmod -w $1
123
124 exit 0