1121934Sharti#
2121934Sharti# Copyright (c) 2001-2003
3121934Sharti# Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4121934Sharti# 	All rights reserved.
5121934Sharti#
6121934Sharti# Redistribution and use in source and binary forms, with or without
7121934Sharti# modification, are permitted provided that the following conditions
8121934Sharti# are met:
9121934Sharti# 1. Redistributions of source code must retain the above copyright
10121934Sharti#    notice, this list of conditions and the following disclaimer.
11121934Sharti# 2. Redistributions in binary form must reproduce the above copyright
12121934Sharti#    notice, this list of conditions and the following disclaimer in the
13121934Sharti#    documentation and/or other materials provided with the distribution.
14121934Sharti#
15121934Sharti# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16121934Sharti# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17121934Sharti# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18121934Sharti# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19121934Sharti# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20121934Sharti# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21121934Sharti# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22121934Sharti# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23121934Sharti# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24121934Sharti# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25121934Sharti# SUCH DAMAGE.
26121934Sharti#
27121934Sharti# Author: Hartmut Brandt <harti@freebsd.org>
28121934Sharti#
29131826Sharti# $Begemot: libunimsg/netnatm/msg/genmsgc.awk,v 1.6 2004/07/08 08:22:04 brandt Exp $
30121934Sharti#
31121934Sharti# Generate message functions.
32121934Sharti#
33121934Shartifunction begin() {
34121934Sharti}
35121934Sharti
36121934Shartifunction first_entry() {
37121934Sharti	print "/* This file was created automatically"
38121934Sharti	print " * Source file: " id
39121934Sharti	print " */"
40121934Sharti	print ""
41121934Sharti	print "#include <sys/types.h>"
42121934Sharti	print "#include <sys/param.h>"
43121934Sharti	print ""
44121934Sharti	print "#ifdef _KERNEL"
45121934Sharti	print "#include <sys/libkern.h>"
46121934Sharti	print "#else"
47121934Sharti	print "#include <string.h>"
48121934Sharti	print "#endif"
49121934Sharti	print "#include <netnatm/unimsg.h>"
50121934Sharti	print "#include <netnatm/msg/unistruct.h>"
51121934Sharti	print "#include <netnatm/msg/unimsglib.h>"
52121934Sharti	print "#include <netnatm/msg/priv.h>"
53121934Sharti	print "#include <netnatm/msg/privmsg.c>"
54121934Sharti}
55121934Sharti
56121934Shartifunction end() {
57121934Sharti	print ""
58121934Sharti	print "const struct msgdecl *uni_msgtable[256] = {"
59121934Sharti	for(i = 0; i < 256; i++) {
60121934Sharti		if(decl[i] == "") {
61121934Sharti			printf "\t&decl_unknown,"
62121934Sharti		} else {
63121934Sharti			printf "\t&%s,", decl[i]
64121934Sharti		}
65121934Sharti		printf "\t/* 0x%02x */\n", i
66121934Sharti	}
67121934Sharti	print "};"
68121934Sharti}
69121934Sharti
70121934Shartifunction start_message() {
71121934Sharti}
72121934Sharti
73121934Shartifunction end_message() {
74121934Sharti		gen_print()
75121934Sharti		gen_check()
76121934Sharti		gen_encode()
77121934Sharti		gen_decode()
78121934Sharti		gen_reg()
79121934Sharti}
80121934Sharti
81121934Shartifunction gen_print() {
82121934Sharti	print ""
83121934Sharti	print "static void"
84121934Sharti	print "print_" msg "(struct uni_" msg " *msg, struct unicx *cx)"
85121934Sharti	print "{"
86121934Sharti	if(msgrep) {
87121934Sharti		print "\tu_int i;"
88121934Sharti		print ""
89121934Sharti	}
90121934Sharti	for(i = 0; i < cnt; i++) {
91121934Sharti		ie = iename[i]
92121934Sharti		uie = toupper(iename[i])
93121934Sharti		if(ierep[i]) {
94121934Sharti			print "\tif(msg->" ie "_repeat.h.present & UNI_IE_PRESENT)"
95121934Sharti			print "\t\tuni_print_ie_internal(UNI_IE_REPEAT, (union uni_ieall *)&msg->" ie "_repeat, cx);"
96121934Sharti		}
97121934Sharti		if(ienum[i] == "-") {
98121934Sharti			print "\tif(msg->" ie ".h.present & UNI_IE_PRESENT)"
99121934Sharti			print "\t\tuni_print_ie_internal(UNI_IE_" uie ", (union uni_ieall *)&msg->" ie ", cx);"
100121934Sharti		} else {
101121934Sharti			print "\tfor(i = 0; i < " ienum[i] "; i++)"
102121934Sharti			print "\t\tif(msg->" ie "[i].h.present & UNI_IE_PRESENT)"
103121934Sharti			print "\t\t\tuni_print_ie_internal(UNI_IE_" uie ", (union uni_ieall *)&msg->" ie "[i], cx);"
104121934Sharti		}
105121934Sharti	}
106121934Sharti	print "}"
107121934Sharti}
108121934Sharti
109121934Shartifunction gen_check() {
110121934Sharti	print ""
111121934Sharti	print "static int"
112121934Sharti	print "check_" msg "(struct uni_" msg " *m, struct unicx *cx)"
113121934Sharti	print "{"
114121934Sharti	print "\tint ret = 0;"
115121934Sharti	if(msgrep) {
116121934Sharti		print "\tu_int i;"
117121934Sharti	}
118121934Sharti	print ""
119121934Sharti	for(i = 0; i < cnt; i++) {
120121934Sharti		ie = iename[i]
121121934Sharti		if(ierep[i]) {
122121934Sharti			if(iecond[i] == "1") {
123121934Sharti				print "\tret |= uni_check_ie(UNI_IE_REPEAT, (union uni_ieall *)&m->" ie "_repeat, cx);"
124121934Sharti			} else {
125121934Sharti				print "\tif(!(" iecond[i] "))"
126121934Sharti				print "\t\tret |= IE_ISPRESENT(m->" ie "_repeat);"
127121934Sharti				print "\telse"
128121934Sharti				print "\t\tret |= uni_check_ie(UNI_IE_REPEAT, (union uni_ieall *)&m->" ie "_repeat, cx);"
129121934Sharti			}
130121934Sharti		}
131121934Sharti		if(ienum[i] == "-") {
132121934Sharti			if(iecond[i] == "1") {
133121934Sharti				print "\tret |= uni_check_ie(UNI_IE_" toupper(ie) ", (union uni_ieall *)&m->" ie ", cx);"
134121934Sharti			} else {
135121934Sharti				print "\tif(!(" iecond[i] "))"
136121934Sharti				print "\t\tret |= IE_ISPRESENT(m->" ie ");"
137121934Sharti				print "\telse"
138121934Sharti				print "\t\tret |= uni_check_ie(UNI_IE_" toupper(ie) ", (union uni_ieall *)&m->" ie ", cx);"
139121934Sharti			}
140121934Sharti		} else {
141121934Sharti	    		print "\tfor(i = 0; i < " ienum[i]" ; i++) {"
142121934Sharti			if(iecond[i] == "1") {
143121934Sharti				print "\t\tret |= uni_check_ie(UNI_IE_" toupper(ie) ", (union uni_ieall *)&m->" ie "[i], cx);"
144121934Sharti			} else {
145121934Sharti				print "\t\tif(!(" iecond[i] "))"
146121934Sharti				print "\t\t\tret |= IE_ISPRESENT(m->" ie "[i]);"
147121934Sharti				print "\t\telse"
148121934Sharti				print "\t\t\tret |= uni_check_ie(UNI_IE_" toupper(ie) ", (union uni_ieall *)&m->" ie "[i], cx);"
149121934Sharti			}
150121934Sharti			print "\t}"
151121934Sharti		}
152121934Sharti	}
153121934Sharti	print ""
154121934Sharti	print "\treturn ret;"
155121934Sharti	print "}"
156121934Sharti}
157121934Sharti
158121934Shartifunction gen_encode() {
159121934Sharti	print ""
160121934Sharti	print "static int"
161121934Sharti	print "encode_" msg "(struct uni_msg *msg, struct uni_" msg " *p, struct unicx *cx)"
162121934Sharti	print "{"
163121934Sharti	print "\tu_int mlen;"
164121934Sharti	if(msgrep) {
165121934Sharti		print "\tu_int i;"
166121934Sharti	}
167121934Sharti	print ""
168121934Sharti	print "\tif(uni_encode_msg_hdr(msg, &p->hdr, UNI_" toupper(msg) ", cx, &mlen))"
169121934Sharti	print "\t\treturn (-2);"
170121934Sharti	print ""
171121934Sharti	for(i = 0; i < cnt; i++) {
172121934Sharti		ie = iename[i]
173121934Sharti		if(ierep[i]) {
174121934Sharti			print "\tif((p->" ie "_repeat.h.present & UNI_IE_PRESENT) &&"
175121934Sharti	   		print "\t   uni_encode_ie(UNI_IE_" toupper(ie) ", msg, (union uni_ieall *)&p->" ie "_repeat, cx))"
176121934Sharti			print "\t\treturn (0x10000000 + UNI_IE_" toupper(ie) ");"
177121934Sharti		}
178121934Sharti		if(ienum[i] == "-") {
179121934Sharti			print "\tif((p->" ie ".h.present & UNI_IE_PRESENT) &&"
180121934Sharti	   		print "\t   uni_encode_ie(UNI_IE_" toupper(ie) ", msg, (union uni_ieall *)&p->" ie ", cx))"
181121934Sharti			print "\t\treturn (UNI_IE_" toupper(ie) ");"
182121934Sharti		} else {
183121934Sharti			print "\tfor(i = 0; i < " ienum[i] "; i++)"
184121934Sharti			print "\t\tif((p->" ie "[i].h.present & UNI_IE_PRESENT) &&"
185121934Sharti	   		print "\t\t   uni_encode_ie(UNI_IE_" toupper(ie) ", msg, (union uni_ieall *)&p->" ie "[i], cx))"
186121934Sharti			print "\t\treturn ((i << 16) + UNI_IE_" toupper(ie) ");"
187121934Sharti		}
188121934Sharti	}
189121934Sharti	print ""
190121934Sharti	print "\tmsg->b_buf[mlen+0] = ((msg->b_wptr-msg->b_rptr)-mlen-2) >> 8;"
191121934Sharti	print "\tmsg->b_buf[mlen+1] = ((msg->b_wptr-msg->b_rptr)-mlen-2) >> 0;"
192121934Sharti 	print ""
193121934Sharti	print "\treturn (0);"
194121934Sharti	print "}"
195121934Sharti}
196121934Sharti
197121934Shartifunction gen_decode() {
198121934Sharti	print ""
199121934Sharti	print "static int"
200121934Sharti	print "decode_" msg "(struct uni_" msg " *out, struct uni_msg *msg,"
201121934Sharti	print "    enum uni_ietype ie, struct uni_iehdr *hdr, u_int ielen,"
202121934Sharti	print "    struct unicx *cx)"
203121934Sharti	print "{"
204121934Sharti	if (msgrep) {
205121934Sharti		print "	u_int i;"
206121934Sharti		print ""
207121934Sharti	}
208121934Sharti	print "	switch (ie) {"
209121934Sharti
210121934Sharti	rep=0
211121934Sharti	for (i = 0; i < cnt; i++) {
212121934Sharti		ie = iename[i]
213121934Sharti		print ""
214121934Sharti	  	print "	  case UNI_IE_" toupper(ie) ":"
215121934Sharti		if (iecond[i] != "1") {
216121934Sharti			print "		if (!(" iecond[i] "))"
217121934Sharti			print "			return (DEC_ILL);"
218121934Sharti		}
219121934Sharti		if (ierep[i]) {
220121934Sharti			rep=1
221121934Sharti			print "		if (IE_ISPRESENT(cx->repeat))"
222121934Sharti			print "			out->" ie "_repeat = cx->repeat;"
223121934Sharti		}
224121934Sharti		if (ienum[i] == "-") {
225121934Sharti			print "		out->" ie ".h = *hdr;"
226121934Sharti			print "		if (hdr->present & UNI_IE_ERROR)"
227121934Sharti			print "			return (DEC_ERR);"
228121934Sharti			print "		if(uni_decode_ie_body(UNI_IE_"toupper(ie)", (union uni_ieall *)&out->"ie", msg, ielen, cx))"
229121934Sharti			print "			return (DEC_ERR);"
230121934Sharti
231121934Sharti		} else {
232121934Sharti			print "		for(i = 0; i < " ienum[i] "; i++)"
233121934Sharti			print "			if (!IE_ISPRESENT(out->" ie "[i])) {"
234121934Sharti			print "				out->" ie "[i].h = *hdr;"
235121934Sharti			print "				if (hdr->present & UNI_IE_ERROR)"
236121934Sharti			print "					return (DEC_ERR);"
237121934Sharti			print "				if(uni_decode_ie_body(UNI_IE_"toupper(ie)", (union uni_ieall *)&out->"ie"[i], msg, ielen, cx))"
238121934Sharti			print "					return (DEC_ERR);"
239121934Sharti			print "				break;"
240121934Sharti			print "			}"
241121934Sharti		}
242121934Sharti		print "		break;"
243121934Sharti	}
244121934Sharti	if(rep) {
245121934Sharti		print ""
246121934Sharti		print "	  case UNI_IE_REPEAT:"
247121934Sharti		print "		cx->repeat.h = *hdr;"
248121934Sharti		print "		if (hdr->present & UNI_IE_ERROR)"
249121934Sharti		print "			return (DEC_ERR);"
250121934Sharti		print "		if (uni_decode_ie_body(UNI_IE_REPEAT, (union uni_ieall *)&cx->repeat, msg, ielen, cx))"
251121934Sharti		print "			return (DEC_ERR);"
252121934Sharti		print "		break;"
253121934Sharti	}
254121934Sharti
255121934Sharti	print ""
256121934Sharti	print "	  default:"
257121934Sharti	print "		return (DEC_ILL);"
258121934Sharti	print "	}"
259121934Sharti	print "	return (DEC_OK);"
260121934Sharti	print "}"
261121934Sharti}
262121934Sharti
263121934Shartifunction gen_reg() {
264121934Sharti	print ""
265121934Sharti	print "static const struct msgdecl decl_" msg " = {"
266121934Sharti	print "\t0,"
267121934Sharti	print "\t\"" msg "\","
268121934Sharti	print "\t(uni_msg_print_f)print_" msg ","
269121934Sharti	print "\t(uni_msg_check_f)check_" msg ","
270121934Sharti	print "\t(uni_msg_encode_f)encode_" msg ","
271121934Sharti	print "\t(uni_msg_decode_f)decode_" msg
272121934Sharti	print "};"
273121934Sharti	decl[code] = "decl_" msg
274121934Sharti}
275