genmsgc.awk revision 131826
1156952Sume#
2156952Sume# Copyright (c) 2001-2003
3156952Sume# Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4156952Sume# 	All rights reserved.
5156952Sume#
6156952Sume# Redistribution and use in source and binary forms, with or without
7156952Sume# modification, are permitted provided that the following conditions
8156952Sume# are met:
9156952Sume# 1. Redistributions of source code must retain the above copyright
10156952Sume#    notice, this list of conditions and the following disclaimer.
11156952Sume# 2. Redistributions in binary form must reproduce the above copyright
12156952Sume#    notice, this list of conditions and the following disclaimer in the
13156952Sume#    documentation and/or other materials provided with the distribution.
14156952Sume#
15156952Sume# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16156952Sume# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17156952Sume# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18156952Sume# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19156952Sume# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20156952Sume# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21156952Sume# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22156952Sume# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23156952Sume# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24156952Sume# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25156952Sume# SUCH DAMAGE.
26156952Sume#
27156952Sume# Author: Hartmut Brandt <harti@freebsd.org>
28156952Sume#
29156952Sume# $Begemot: libunimsg/netnatm/msg/genmsgc.awk,v 1.6 2004/07/08 08:22:04 brandt Exp $
30156952Sume#
31156952Sume# Generate message functions.
32156952Sume#
33156952Sumefunction begin() {
34156952Sume}
35156952Sume
36156952Sumefunction first_entry() {
37156952Sume	print "/* This file was created automatically"
38156952Sume	print " * Source file: " id
39156952Sume	print " */"
40156952Sume	print ""
41156952Sume	print "#include <sys/types.h>"
42156952Sume	print "#include <sys/param.h>"
43156952Sume	print ""
44156952Sume	print "#ifdef _KERNEL"
45156952Sume	print "#include <sys/libkern.h>"
46156952Sume	print "#else"
47156952Sume	print "#include <string.h>"
48156952Sume	print "#endif"
49156952Sume	print "#include <netnatm/unimsg.h>"
50156952Sume	print "#include <netnatm/msg/unistruct.h>"
51156952Sume	print "#include <netnatm/msg/unimsglib.h>"
52156952Sume	print "#include <netnatm/msg/priv.h>"
53156952Sume	print "#include <netnatm/msg/privmsg.c>"
54156952Sume}
55156956Sume
56156956Sumefunction end() {
57156952Sume	print ""
58156952Sume	print "const struct msgdecl *uni_msgtable[256] = {"
59156952Sume	for(i = 0; i < 256; i++) {
60156956Sume		if(decl[i] == "") {
61156952Sume			printf "\t&decl_unknown,"
62156952Sume		} else {
63156952Sume			printf "\t&%s,", decl[i]
64156952Sume		}
65156952Sume		printf "\t/* 0x%02x */\n", i
66156952Sume	}
67156952Sume	print "};"
68156952Sume}
69156952Sume
70156952Sumefunction start_message() {
71156952Sume}
72156956Sume
73156952Sumefunction end_message() {
74156952Sume		gen_print()
75156952Sume		gen_check()
76156952Sume		gen_encode()
77156952Sume		gen_decode()
78156952Sume		gen_reg()
79156952Sume}
80156952Sume
81156952Sumefunction gen_print() {
82156952Sume	print ""
83156956Sume	print "static void"
84156952Sume	print "print_" msg "(struct uni_" msg " *msg, struct unicx *cx)"
85156952Sume	print "{"
86156952Sume	if(msgrep) {
87156952Sume		print "\tu_int i;"
88156952Sume		print ""
89156952Sume	}
90156952Sume	for(i = 0; i < cnt; i++) {
91156952Sume		ie = iename[i]
92156952Sume		uie = toupper(iename[i])
93156952Sume		if(ierep[i]) {
94156952Sume			print "\tif(msg->" ie "_repeat.h.present & UNI_IE_PRESENT)"
95156952Sume			print "\t\tuni_print_ie_internal(UNI_IE_REPEAT, (union uni_ieall *)&msg->" ie "_repeat, cx);"
96156952Sume		}
97156952Sume		if(ienum[i] == "-") {
98156952Sume			print "\tif(msg->" ie ".h.present & UNI_IE_PRESENT)"
99156952Sume			print "\t\tuni_print_ie_internal(UNI_IE_" uie ", (union uni_ieall *)&msg->" ie ", cx);"
100156952Sume		} else {
101156952Sume			print "\tfor(i = 0; i < " ienum[i] "; i++)"
102156952Sume			print "\t\tif(msg->" ie "[i].h.present & UNI_IE_PRESENT)"
103156952Sume			print "\t\t\tuni_print_ie_internal(UNI_IE_" uie ", (union uni_ieall *)&msg->" ie "[i], cx);"
104156952Sume		}
105156952Sume	}
106156952Sume	print "}"
107156952Sume}
108156952Sume
109156952Sumefunction gen_check() {
110156952Sume	print ""
111156952Sume	print "static int"
112156952Sume	print "check_" msg "(struct uni_" msg " *m, struct unicx *cx)"
113156952Sume	print "{"
114156956Sume	print "\tint ret = 0;"
115156952Sume	if(msgrep) {
116156952Sume		print "\tu_int i;"
117156952Sume	}
118156952Sume	print ""
119156952Sume	for(i = 0; i < cnt; i++) {
120156952Sume		ie = iename[i]
121156952Sume		if(ierep[i]) {
122156952Sume			if(iecond[i] == "1") {
123156952Sume				print "\tret |= uni_check_ie(UNI_IE_REPEAT, (union uni_ieall *)&m->" ie "_repeat, cx);"
124156952Sume			} else {
125156952Sume				print "\tif(!(" iecond[i] "))"
126156952Sume				print "\t\tret |= IE_ISPRESENT(m->" ie "_repeat);"
127156952Sume				print "\telse"
128156952Sume				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