1#
2# Copyright (c) 2001-2003
3# Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4# 	All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions
8# are met:
9# 1. Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11# 2. Redistributions in binary form must reproduce the above copyright
12#    notice, this list of conditions and the following disclaimer in the
13#    documentation and/or other materials provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25# SUCH DAMAGE.
26#
27# Author: Hartmut Brandt <harti@freebsd.org>
28#
29# $Begemot: libunimsg/netnatm/msg/genmsgc.awk,v 1.6 2004/07/08 08:22:04 brandt Exp $
30#
31# Generate message functions.
32#
33function begin() {
34}
35
36function first_entry() {
37	print "/* This file was created automatically"
38	print " * Source file: " id
39	print " */"
40	print ""
41	print "#include <sys/types.h>"
42	print "#include <sys/param.h>"
43	print ""
44	print "#ifdef _KERNEL"
45	print "#include <sys/libkern.h>"
46	print "#else"
47	print "#include <string.h>"
48	print "#endif"
49	print "#include <netnatm/unimsg.h>"
50	print "#include <netnatm/msg/unistruct.h>"
51	print "#include <netnatm/msg/unimsglib.h>"
52	print "#include <netnatm/msg/priv.h>"
53	print "#include <netnatm/msg/privmsg.c>"
54}
55
56function end() {
57	print ""
58	print "const struct msgdecl *uni_msgtable[256] = {"
59	for(i = 0; i < 256; i++) {
60		if(decl[i] == "") {
61			printf "\t&decl_unknown,"
62		} else {
63			printf "\t&%s,", decl[i]
64		}
65		printf "\t/* 0x%02x */\n", i
66	}
67	print "};"
68}
69
70function start_message() {
71}
72
73function end_message() {
74		gen_print()
75		gen_check()
76		gen_encode()
77		gen_decode()
78		gen_reg()
79}
80
81function gen_print() {
82	print ""
83	print "static void"
84	print "print_" msg "(struct uni_" msg " *msg, struct unicx *cx)"
85	print "{"
86	if(msgrep) {
87		print "\tu_int i;"
88		print ""
89	}
90	for(i = 0; i < cnt; i++) {
91		ie = iename[i]
92		uie = toupper(iename[i])
93		if(ierep[i]) {
94			print "\tif(msg->" ie "_repeat.h.present & UNI_IE_PRESENT)"
95			print "\t\tuni_print_ie_internal(UNI_IE_REPEAT, (union uni_ieall *)&msg->" ie "_repeat, cx);"
96		}
97		if(ienum[i] == "-") {
98			print "\tif(msg->" ie ".h.present & UNI_IE_PRESENT)"
99			print "\t\tuni_print_ie_internal(UNI_IE_" uie ", (union uni_ieall *)&msg->" ie ", cx);"
100		} else {
101			print "\tfor(i = 0; i < " ienum[i] "; i++)"
102			print "\t\tif(msg->" ie "[i].h.present & UNI_IE_PRESENT)"
103			print "\t\t\tuni_print_ie_internal(UNI_IE_" uie ", (union uni_ieall *)&msg->" ie "[i], cx);"
104		}
105	}
106	print "}"
107}
108
109function gen_check() {
110	print ""
111	print "static int"
112	print "check_" msg "(struct uni_" msg " *m, struct unicx *cx)"
113	print "{"
114	print "\tint ret = 0;"
115	if(msgrep) {
116		print "\tu_int i;"
117	}
118	print ""
119	for(i = 0; i < cnt; i++) {
120		ie = iename[i]
121		if(ierep[i]) {
122			if(iecond[i] == "1") {
123				print "\tret |= uni_check_ie(UNI_IE_REPEAT, (union uni_ieall *)&m->" ie "_repeat, cx);"
124			} else {
125				print "\tif(!(" iecond[i] "))"
126				print "\t\tret |= IE_ISPRESENT(m->" ie "_repeat);"
127				print "\telse"
128				print "\t\tret |= uni_check_ie(UNI_IE_REPEAT, (union uni_ieall *)&m->" ie "_repeat, cx);"
129			}
130		}
131		if(ienum[i] == "-") {
132			if(iecond[i] == "1") {
133				print "\tret |= uni_check_ie(UNI_IE_" toupper(ie) ", (union uni_ieall *)&m->" ie ", cx);"
134			} else {
135				print "\tif(!(" iecond[i] "))"
136				print "\t\tret |= IE_ISPRESENT(m->" ie ");"
137				print "\telse"
138				print "\t\tret |= uni_check_ie(UNI_IE_" toupper(ie) ", (union uni_ieall *)&m->" ie ", cx);"
139			}
140		} else {
141	    		print "\tfor(i = 0; i < " ienum[i]" ; i++) {"
142			if(iecond[i] == "1") {
143				print "\t\tret |= uni_check_ie(UNI_IE_" toupper(ie) ", (union uni_ieall *)&m->" ie "[i], cx);"
144			} else {
145				print "\t\tif(!(" iecond[i] "))"
146				print "\t\t\tret |= IE_ISPRESENT(m->" ie "[i]);"
147				print "\t\telse"
148				print "\t\t\tret |= uni_check_ie(UNI_IE_" toupper(ie) ", (union uni_ieall *)&m->" ie "[i], cx);"
149			}
150			print "\t}"
151		}
152	}
153	print ""
154	print "\treturn ret;"
155	print "}"
156}
157
158function gen_encode() {
159	print ""
160	print "static int"
161	print "encode_" msg "(struct uni_msg *msg, struct uni_" msg " *p, struct unicx *cx)"
162	print "{"
163	print "\tu_int mlen;"
164	if(msgrep) {
165		print "\tu_int i;"
166	}
167	print ""
168	print "\tif(uni_encode_msg_hdr(msg, &p->hdr, UNI_" toupper(msg) ", cx, &mlen))"
169	print "\t\treturn (-2);"
170	print ""
171	for(i = 0; i < cnt; i++) {
172		ie = iename[i]
173		if(ierep[i]) {
174			print "\tif((p->" ie "_repeat.h.present & UNI_IE_PRESENT) &&"
175	   		print "\t   uni_encode_ie(UNI_IE_" toupper(ie) ", msg, (union uni_ieall *)&p->" ie "_repeat, cx))"
176			print "\t\treturn (0x10000000 + UNI_IE_" toupper(ie) ");"
177		}
178		if(ienum[i] == "-") {
179			print "\tif((p->" ie ".h.present & UNI_IE_PRESENT) &&"
180	   		print "\t   uni_encode_ie(UNI_IE_" toupper(ie) ", msg, (union uni_ieall *)&p->" ie ", cx))"
181			print "\t\treturn (UNI_IE_" toupper(ie) ");"
182		} else {
183			print "\tfor(i = 0; i < " ienum[i] "; i++)"
184			print "\t\tif((p->" ie "[i].h.present & UNI_IE_PRESENT) &&"
185	   		print "\t\t   uni_encode_ie(UNI_IE_" toupper(ie) ", msg, (union uni_ieall *)&p->" ie "[i], cx))"
186			print "\t\treturn ((i << 16) + UNI_IE_" toupper(ie) ");"
187		}
188	}
189	print ""
190	print "\tmsg->b_buf[mlen+0] = ((msg->b_wptr-msg->b_rptr)-mlen-2) >> 8;"
191	print "\tmsg->b_buf[mlen+1] = ((msg->b_wptr-msg->b_rptr)-mlen-2) >> 0;"
192 	print ""
193	print "\treturn (0);"
194	print "}"
195}
196
197function gen_decode() {
198	print ""
199	print "static int"
200	print "decode_" msg "(struct uni_" msg " *out, struct uni_msg *msg,"
201	print "    enum uni_ietype ie, struct uni_iehdr *hdr, u_int ielen,"
202	print "    struct unicx *cx)"
203	print "{"
204	if (msgrep) {
205		print "	u_int i;"
206		print ""
207	}
208	print "	switch (ie) {"
209
210	rep=0
211	for (i = 0; i < cnt; i++) {
212		ie = iename[i]
213		print ""
214	  	print "	  case UNI_IE_" toupper(ie) ":"
215		if (iecond[i] != "1") {
216			print "		if (!(" iecond[i] "))"
217			print "			return (DEC_ILL);"
218		}
219		if (ierep[i]) {
220			rep=1
221			print "		if (IE_ISPRESENT(cx->repeat))"
222			print "			out->" ie "_repeat = cx->repeat;"
223		}
224		if (ienum[i] == "-") {
225			print "		out->" ie ".h = *hdr;"
226			print "		if (hdr->present & UNI_IE_ERROR)"
227			print "			return (DEC_ERR);"
228			print "		if(uni_decode_ie_body(UNI_IE_"toupper(ie)", (union uni_ieall *)&out->"ie", msg, ielen, cx))"
229			print "			return (DEC_ERR);"
230
231		} else {
232			print "		for(i = 0; i < " ienum[i] "; i++)"
233			print "			if (!IE_ISPRESENT(out->" ie "[i])) {"
234			print "				out->" ie "[i].h = *hdr;"
235			print "				if (hdr->present & UNI_IE_ERROR)"
236			print "					return (DEC_ERR);"
237			print "				if(uni_decode_ie_body(UNI_IE_"toupper(ie)", (union uni_ieall *)&out->"ie"[i], msg, ielen, cx))"
238			print "					return (DEC_ERR);"
239			print "				break;"
240			print "			}"
241		}
242		print "		break;"
243	}
244	if(rep) {
245		print ""
246		print "	  case UNI_IE_REPEAT:"
247		print "		cx->repeat.h = *hdr;"
248		print "		if (hdr->present & UNI_IE_ERROR)"
249		print "			return (DEC_ERR);"
250		print "		if (uni_decode_ie_body(UNI_IE_REPEAT, (union uni_ieall *)&cx->repeat, msg, ielen, cx))"
251		print "			return (DEC_ERR);"
252		print "		break;"
253	}
254
255	print ""
256	print "	  default:"
257	print "		return (DEC_ILL);"
258	print "	}"
259	print "	return (DEC_OK);"
260	print "}"
261}
262
263function gen_reg() {
264	print ""
265	print "static const struct msgdecl decl_" msg " = {"
266	print "\t0,"
267	print "\t\"" msg "\","
268	print "\t(uni_msg_print_f)print_" msg ","
269	print "\t(uni_msg_check_f)check_" msg ","
270	print "\t(uni_msg_encode_f)encode_" msg ","
271	print "\t(uni_msg_decode_f)decode_" msg
272	print "};"
273	decl[code] = "decl_" msg
274}
275