2 # Copyright (c) 2001-2003
3 # Fraunhofer Institute for Open Communication Systems (FhG Fokus).
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
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.
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
27 # Author: Hartmut Brandt <harti@freebsd.org>
29 # $Begemot: libunimsg/netnatm/msg/genmsgc.awk,v 1.6 2004/07/08 08:22:04 brandt Exp $
31 # Generate message functions.
36 function first_entry() {
37 print "/* This file was created automatically"
38 print " * Source file: " id
41 print "#include <sys/types.h>"
42 print "#include <sys/param.h>"
44 print "#ifdef _KERNEL"
45 print "#include <sys/libkern.h>"
47 print "#include <string.h>"
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>"
58 print "const struct msgdecl *uni_msgtable[256] = {"
59 for(i = 0; i < 256; i++) {
61 printf "\t&decl_unknown,"
63 printf "\t&%s,", decl[i]
65 printf "\t/* 0x%02x */\n", i
70 function start_message() {
73 function end_message() {
81 function gen_print() {
84 print "print_" msg "(struct uni_" msg " *msg, struct unicx *cx)"
90 for(i = 0; i < cnt; i++) {
92 uie = toupper(iename[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);"
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);"
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);"
109 function gen_check() {
112 print "check_" msg "(struct uni_" msg " *m, struct unicx *cx)"
114 print "\tint ret = 0;"
119 for(i = 0; i < cnt; i++) {
122 if(iecond[i] == "1") {
123 print "\tret |= uni_check_ie(UNI_IE_REPEAT, (union uni_ieall *)&m->" ie "_repeat, cx);"
125 print "\tif(!(" iecond[i] "))"
126 print "\t\tret |= IE_ISPRESENT(m->" ie "_repeat);"
128 print "\t\tret |= uni_check_ie(UNI_IE_REPEAT, (union uni_ieall *)&m->" ie "_repeat, cx);"
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);"
135 print "\tif(!(" iecond[i] "))"
136 print "\t\tret |= IE_ISPRESENT(m->" ie ");"
138 print "\t\tret |= uni_check_ie(UNI_IE_" toupper(ie) ", (union uni_ieall *)&m->" ie ", cx);"
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);"
145 print "\t\tif(!(" iecond[i] "))"
146 print "\t\t\tret |= IE_ISPRESENT(m->" ie "[i]);"
148 print "\t\t\tret |= uni_check_ie(UNI_IE_" toupper(ie) ", (union uni_ieall *)&m->" ie "[i], cx);"
154 print "\treturn ret;"
158 function gen_encode() {
161 print "encode_" msg "(struct uni_msg *msg, struct uni_" msg " *p, struct unicx *cx)"
163 print "\tu_int mlen;"
168 print "\tif(uni_encode_msg_hdr(msg, &p->hdr, UNI_" toupper(msg) ", cx, &mlen))"
169 print "\t\treturn (-2);"
171 for(i = 0; i < cnt; 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) ");"
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) ");"
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) ");"
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;"
193 print "\treturn (0);"
197 function gen_decode() {
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)"
208 print " switch (ie) {"
211 for (i = 0; i < cnt; i++) {
214 print " case UNI_IE_" toupper(ie) ":"
215 if (iecond[i] != "1") {
216 print " if (!(" iecond[i] "))"
217 print " return (DEC_ILL);"
221 print " if (IE_ISPRESENT(cx->repeat))"
222 print " out->" ie "_repeat = cx->repeat;"
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);"
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);"
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);"
257 print " return (DEC_ILL);"
259 print " return (DEC_OK);"
265 print "static const struct msgdecl decl_" msg " = {"
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
273 decl[code] = "decl_" msg