parsemsg.awk revision 131827
1254721Semaste#
2254721Semaste# Copyright (c) 2001-2003
3254721Semaste# Fraunhofer Institute for Open Communication Systems (FhG Fokus).
4254721Semaste# 	All rights reserved.
5254721Semaste#
6254721Semaste# Redistribution and use in source and binary forms, with or without
7254721Semaste# modification, are permitted provided that the following conditions
8254721Semaste# are met:
9254721Semaste# 1. Redistributions of source code must retain the above copyright
10254721Semaste#    notice, this list of conditions and the following disclaimer.
11254721Semaste# 2. Redistributions in binary form must reproduce the above copyright
12254721Semaste#    notice, this list of conditions and the following disclaimer in the
13254721Semaste#    documentation and/or other materials provided with the distribution.
14254721Semaste#
15254721Semaste# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16254721Semaste# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17254721Semaste# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18254721Semaste# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19254721Semaste# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20254721Semaste# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21254721Semaste# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22254721Semaste# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23254721Semaste# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24254721Semaste# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25254721Semaste# SUCH DAMAGE.
26254721Semaste#
27254721Semaste# Author: Hartmut Brandt <harti@freebsd.org>
28254721Semaste#
29254721Semaste# $Begemot: libunimsg/netnatm/msg/parsemsg.awk,v 1.3 2003/09/19 11:58:15 hbb Exp $
30254721Semaste#
31254721Semaste# Parse the message definition file
32254721Semaste#
33254721Semastematch($0, "Begemot:")!=0 {
34254721Semaste	gsub("^[^$]*", "")
35254721Semaste	gsub("[^$]*$", "")
36254721Semaste	id = $0
37254721Semaste	next
38254721Semaste}
39254721Semaste
40254721Semaste/^#/ {
41254721Semaste	next
42254721Semaste}
43254721SemasteNF == 0 {
44254721Semaste	next
45254721Semaste}
46254721SemasteBEGIN {
47254721Semaste	state=0
48	id = " * ???"
49	mcnt=0
50	begin()
51}
52END {
53	end()
54}
55
56state==0 && $1=="start" {
57	if(NF < 3) error("bad number of fields in message start "$0)
58	state = 1
59	msg = $2
60	code = parse_hex($3)
61	messages[mcnt] = msg
62	msgcond[mcnt] = $4
63	msgrep = 0
64	msgrepie = 0
65	cnt = 0
66	if(mcnt == 0) first_entry()
67	start_message()
68	next
69}
70
71state==1 && $1=="end" {
72	state=0
73	mcnt++
74	end_message()
75	next
76}
77state==1 {
78	iename[cnt]=$1
79	if($2 == "") $2="-"
80	if(match($2, "[A-Za-z][A-Za-z0-9_]*/R") == 1) {
81		ienum[cnt]=substr($2, 1, length($2)-2)
82		ierep[cnt]=1
83		msgrepie=1
84	} else {
85		ierep[cnt]=0
86		ienum[cnt]=$2
87	}
88	if(ienum[cnt] != "-") msgrep = 1
89	if($3 == "" || $3 == "-") {
90		$3 = "1"
91	} else {
92		gsub("[a-zA-Z][a-zA-Z0-9]*", "cx->&", $3)
93	}
94	iecond[cnt] = $3
95	cnt++
96	next
97}
98
99{
100	error("bad line: "$0)
101}
102
103function parse_hex(str,		n)
104{
105	n = 0
106	if(substr(str,1,2) != "0x") {
107		error("bad hex number" str)
108	}
109	for(i = 3; i <= length(str); i++) {
110		c = substr(str,i,1)
111		if(match(c,"[0-9]") != 0) {
112			n = 16 * n + c
113		} else if(match(c,"[a-f]")) {
114			if(c == "a") n = 16 * n + 10
115			if(c == "b") n = 16 * n + 11
116			if(c == "c") n = 16 * n + 12
117			if(c == "d") n = 16 * n + 13
118			if(c == "e") n = 16 * n + 14
119			if(c == "f") n = 16 * n + 15
120		} else if(match(c,"[A-F]")) {
121			if(c == "A") n = 16 * n + 10
122			if(c == "B") n = 16 * n + 11
123			if(c == "C") n = 16 * n + 12
124			if(c == "D") n = 16 * n + 13
125			if(c == "E") n = 16 * n + 14
126			if(c == "F") n = 16 * n + 15
127		} else {
128			error("bad hex digit '" c "'")
129		}
130	}
131	return n
132}
133
134function error(str)
135{
136	print "error:" str >"/dev/stderr"
137	exit 1
138}
139