1#!/bin/bash
2
3# A small script to run xmllint on the Diameter XML files (after doing some
4# fixups to those files).
5#
6# Copyright 2016 Jeff Morriss <jeff.morriss.ws [AT] gmail.com>
7#
8# Wireshark - Network traffic analyzer
9# By Gerald Combs <gerald@wireshark.org>
10# Copyright 1998 Gerald Combs
11# SPDX-License-Identifier: GPL-2.0-or-later
12
13if ! type -p sed > /dev/null
14then
15	echo "'sed' is needed to run $0." 1>&2
16	# Exit cleanly because we don't want pre-commit to fail just because
17	# someone doesn't have the tools...
18	exit 0
19fi
20if ! type -p xmllint > /dev/null
21then
22	echo "'xmllint' is needed to run $0." 1>&2
23	# Exit cleanly because we don't want pre-commit to fail just because
24	# someone doesn't have the tools...
25	exit 0
26fi
27
28# Ideally this would work regardless of our cwd
29if [ ! -r diameter/dictionary.xml ]
30then
31	echo "Couldn't find diameter/dictionary.xml" 1>&2
32	exit 1
33fi
34if [ ! -r diameter/dictionary.dtd ]
35then
36	echo "Couldn't find diameter/dictionary.dtd" 1>&2
37	exit 1
38fi
39
40if ! tmpdir=$(mktemp -d); then
41	echo "Could not create temporary directory" >&2
42	exit 1
43fi
44trap 'rm -rf "$tmpdir"' EXIT
45
46# First edit all the AVP names that start with "3GPP" to indicate "TGPP".
47# XML doesn't allow ID's to start with a digit but:
48#   1) We don't *really* care if it's valid XML
49#   2) (but) we do want to use xmllint to find problems
50#   3) (and) users see the AVP names.  Showing them "TGPP" instead of "3GPP"
51#      is annoying enough to warrant this extra work.
52
53# Declare and populate associative exceptions array
54declare -A exceptions=(
55        ["3GPP"]="TGPP"
56        ["5QI"]="FiveQI"
57)
58
59# Loop through the exceptions, building the sed options
60sedopts=
61for e in ${!exceptions[@]}; do
62        sedopts="${sedopts}s/name=\"$e/name=\"${exceptions[$e]}/;"
63done
64
65# Delete the last character, i.e., the trailing semicolon
66sedopts=${sedopts%?}
67
68cp diameter/dictionary.dtd "$tmpdir" || exit 1
69for f in diameter/*.xml
70do
71        sed "${sedopts}" "$f" > "$tmpdir/${f##*/}" || exit 1
72done
73
74xmllint --noout --noent --postvalid "$tmpdir/dictionary.xml" &&
75	echo "Diameter dictionary is (mostly) valid XML."
76
77#
78# Editor modelines  -  https://www.wireshark.org/tools/modelines.html
79#
80# Local variables:
81# c-basic-offset: 8
82# tab-width: 8
83# indent-tabs-mode: t
84# End:
85#
86# vi: set shiftwidth=8 tabstop=8 noexpandtab:
87# :indentSize=8:tabSize=8:noTabs=false:
88#
89