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/parseie.awk,v 1.3 2003/09/19 11:58:15 hbb Exp $
30121934Sharti#
31121934Sharti# Parse the IE definition file
32121934Sharti#
33121934Shartimatch($0, "Begemot:")!=0 {
34121934Sharti	gsub("^[^$]*", "")
35121934Sharti	gsub("[^$]*$", "")
36121934Sharti	id = $0
37121934Sharti	next
38121934Sharti}
39121934Sharti
40121934Sharti/^#/ {
41121934Sharti	next
42121934Sharti}
43121934ShartiNF == 0 {
44121934Sharti	next
45121934Sharti}
46121934Sharti
47121934ShartiBEGIN {
48121934Sharti	iecnt = 0
49121934Sharti	id = " * ???"
50121934Sharti	begin()
51121934Sharti}
52121934Sharti
53121934ShartiEND {
54121934Sharti	end()
55121934Sharti}
56121934Sharti
57121934Sharti#
58121934Sharti# Syntax is:
59121934Sharti# element <name> <code> <coding> [<maxlen> [<options>*]]
60121934Sharti#
61121934Sharti$1=="element" {
62121934Sharti	if(iecnt == 0) first_element()
63121934Sharti	if(NF < 4) {
64121934Sharti		error("Bad number of args: " $0)
65121934Sharti	}
66121934Sharti	ie = $2
67121934Sharti	file = $2
68121934Sharti	number = parse_hex($3)
69121934Sharti	coding = $4
70121934Sharti	if(coding == "itu") {
71121934Sharti		ncoding = 0
72121934Sharti	} else if(coding == "net") {
73121934Sharti		ncoding = 3
74121934Sharti	} else {
75121934Sharti		error("bad coding " coding)
76121934Sharti	}
77121934Sharti	if(NF == 4) {
78121934Sharti		element_default()
79121934Sharti		file=""
80121934Sharti	} else {
81121934Sharti		len = $5
82121934Sharti		parse_options()
83121934Sharti		element()
84121934Sharti	}
85121934Sharti	ies[iecnt] = ie
86121934Sharti	codings[iecnt] = coding
87121934Sharti	files[iecnt] = file
88121934Sharti	iecnt++
89121934Sharti	next
90121934Sharti}
91121934Sharti
92121934Sharti{
93121934Sharti	error("Bad line: " $0)
94121934Sharti}
95121934Sharti
96121934Shartifunction parse_options() {
97121934Sharti	access = 0
98121934Sharti	cond = ""
99121934Sharti	for(i = 6; i <= NF; i++) {
100121934Sharti		if($i == "access") {
101121934Sharti			access = 1
102121934Sharti		} else if($i == "-") {
103121934Sharti		} else if(index($i, "file=") == 1) {
104121934Sharti			file=substr($i, 6)
105121934Sharti		} else {
106121934Sharti			if(cond != "") {
107121934Sharti				error("Too many conditions: "$0)
108121934Sharti			}
109121934Sharti			cond = $i
110121934Sharti		}
111121934Sharti	}
112121934Sharti}
113121934Sharti
114121934Shartifunction parse_hex(str,		n)
115121934Sharti{
116121934Sharti	n = 0
117121934Sharti	if(substr(str,1,2) != "0x") {
118121934Sharti		error("bad hex number" str)
119121934Sharti	}
120121934Sharti	for(i = 3; i <= length(str); i++) {
121121934Sharti		c = substr(str,i,1)
122121934Sharti		if(match(c,"[0-9]") != 0) {
123121934Sharti			n = 16 * n + c
124121934Sharti		} else if(match(c,"[a-f]")) {
125121934Sharti			if(c == "a") n = 16 * n + 10
126121934Sharti			if(c == "b") n = 16 * n + 11
127121934Sharti			if(c == "c") n = 16 * n + 12
128121934Sharti			if(c == "d") n = 16 * n + 13
129121934Sharti			if(c == "e") n = 16 * n + 14
130121934Sharti			if(c == "f") n = 16 * n + 15
131121934Sharti		} else if(match(c,"[A-F]")) {
132121934Sharti			if(c == "A") n = 16 * n + 10
133121934Sharti			if(c == "B") n = 16 * n + 11
134121934Sharti			if(c == "C") n = 16 * n + 12
135121934Sharti			if(c == "D") n = 16 * n + 13
136121934Sharti			if(c == "E") n = 16 * n + 14
137121934Sharti			if(c == "F") n = 16 * n + 15
138121934Sharti		} else {
139121934Sharti			error("bad hex digit '" c "'")
140121934Sharti		}
141121934Sharti	}
142121934Sharti	return n
143121934Sharti}
144121934Sharti
145121934Sharti# function error(str)
146121934Sharti# {
147121934Sharti# 	print "error:" str >"/dev/stderr"
148121934Sharti# 	exit 1
149121934Sharti# }
150121934Sharti
151