xref: /openbsd/sbin/isakmpd/genfields.sh (revision 73471bf0)
1#	$OpenBSD: genfields.sh,v 1.10 2014/01/22 03:09:31 deraadt Exp $
2#	$EOM: genfields.sh,v 1.5 1999/04/02 01:15:55 niklas Exp $
3
4#
5# Copyright (c) 1998, 1999, 2001 Niklas Hallqvist.  All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27
28#
29# This code was written under funding by Ericsson Radio Systems.
30#
31
32base=`basename $1`
33upcased_name=`echo $base |tr a-z A-Z`
34
35awk=${AWK:-awk}
36
37locase_function='function locase (str) {
38  cmd = "echo " str " |tr A-Z a-z"
39  cmd | getline retval;
40  close (cmd);
41  return retval;
42}'
43
44$awk "
45$locase_function
46"'
47BEGIN {
48  print "/* DO NOT EDIT-- this file is automatically generated.  */\n"
49  print "#ifndef _'$upcased_name'_H_"
50  print "#define _'$upcased_name'_H_\n"
51
52  print "#include \"field.h\"\n"
53
54  print "struct constant_map;\n"
55}
56
57/^#/ {
58  next
59}
60
61/^\./ {
62  printf ("#define %s_SZ %d\n", prefix, off)
63  size[prefix] = off
64  next
65}
66
67/^[^ 	]/ {
68  prefix = $1
69  printf ("extern struct field %s_fld[];\n\n", locase(prefix));
70  if ($3)
71    {
72      off = size[$3]
73    }
74  else
75    {
76      off = 0
77    }
78  i = 0
79  next
80}
81
82/^[ 	]/ && $1 {
83  printf ("#define %s_%s_OFF %d\n", prefix, $1, off)
84  if ($3)
85    {
86      printf ("#define %s_%s_LEN %d\n", prefix, $1, $3)
87    }
88  if ($4)
89    {
90      printf ("extern struct constant_map *%s_%s_maps[];\n", locase(prefix),
91              locase($1))
92    }
93  if ($2 == "raw")
94    {
95      printf ("#define GET_%s_%s(buf, val) ", prefix, $1)
96      printf ("field_get_raw (%s_fld + %d, buf, val)\n", locase(prefix), i)
97      printf ("#define SET_%s_%s(buf, val) ", prefix, $1)
98      printf ("field_set_raw (%s_fld + %d, buf, val)\n", locase(prefix), i)
99    }
100  else
101    {
102      printf ("#define GET_%s_%s(buf) field_get_num (%s_fld + %d, buf)\n",
103              prefix, $1, locase(prefix), i)
104      printf ("#define SET_%s_%s(buf, val) ", prefix, $1)
105      printf ("field_set_num (%s_fld + %d, buf, val)\n", locase(prefix), i)
106    }
107  off += $3
108  i++
109  next
110}
111
112{
113    print
114}
115
116END {
117  printf ("\n")
118  print "#endif /* _'$upcased_name'_H_ */"
119}
120' <$1.fld >$base.h
121
122$awk "
123$locase_function
124"'
125BEGIN {
126  print "/* DO NOT EDIT-- this file is automatically generated.  */\n"
127  print "#include \"constants.h\""
128  print "#include \"field.h\""
129  print "#include \"'$base'.h\""
130  print "#include \"isakmp_num.h\""
131  print "#include \"ipsec_num.h\"\n"
132}
133
134/^#/ {
135  next
136}
137
138/^\./ {
139  print "	{ 0, 0, 0, 0, 0 }\n};\n"
140  size[prefix] = off
141  for (map in maps)
142    {
143      printf ("struct constant_map *%s_%s_maps[] = {\n", locase(prefix),
144             locase(map))
145      printf ("	%s, 0\n};\n", maps[map])
146    }
147  next
148}
149
150/^[^ 	]/ {
151  prefix = $1
152  printf ("struct field %s_fld[] = {\n", locase(prefix))
153  if ($3)
154    {
155      off = size[$3]
156    }
157  else
158    {
159      off = 0
160    }
161  delete maps
162  next
163}
164
165/^[ 	]/ && $1 {
166  if ($4)
167    {
168      maps_name = locase(prefix)"_"locase($1)"_maps"
169      maps[$1] = $4
170    }
171  else
172    {
173      maps_name = "0"
174    }
175  printf ("	{ \"%s\", %d, %d, %s, %s },\n", $1, off, $3, $2, maps_name)
176  off += $3
177  next
178}
179
180{
181  print
182}
183' <$1.fld >$base.c
184