]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - sys/contrib/ngatm/netnatm/msg/parsemsg.awk
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / sys / contrib / ngatm / netnatm / msg / parsemsg.awk
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/parsemsg.awk,v 1.3 2003/09/19 11:58:15 hbb Exp $
30 #
31 # Parse the message definition file
32 #
33 match($0, "Begemot:")!=0 {
34         gsub("^[^$]*", "")
35         gsub("[^$]*$", "")
36         id = $0
37         next
38 }
39
40 /^#/ {
41         next
42 }
43 NF == 0 {
44         next
45 }
46 BEGIN {
47         state=0
48         id = " * ???"
49         mcnt=0
50         begin()
51 }
52 END {
53         end()
54 }
55
56 state==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
71 state==1 && $1=="end" {
72         state=0
73         mcnt++
74         end_message()
75         next
76 }
77 state==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
103 function 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
134 function error(str)
135 {
136         print "error:" str >"/dev/stderr"
137         exit 1
138 }