summaryrefslogtreecommitdiffstats
path: root/xml/rng-helper.in
blob: 634abdfcde6281f8dff966685934c8a94e9cfb9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!@BASH_PATH@
#
# Copyright 2014-2024 the Pacemaker project contributors
#
# The version control history for this file may have further details.
#
# This source code is licensed under the GNU General Public License version 2
# or later (GPLv2+) WITHOUT ANY WARRANTY.
#

# @COMPAT "next" schemas are deprecated since 2.1.5
list_candidates() {
    ls -1 "${1}.rng" "${1}"-[0-9]*.rng "${1}"-next.rng 2>/dev/null
}

version_from_filename() {
    vff_filename="$1"

    case "$vff_filename" in
        *-*.rng)
            echo "$vff_filename" | sed -e 's/.*-\(.*\).rng/\1/'
            ;;
        *)
            # special case for bare ${base}.rng, no -0.1's around anyway
            echo 0.1
            ;;
    esac
}

filename_from_version() {
    ffv_version="$1"
    ffv_base="$2"

    if [ "$ffv_version" = "0.1" ]; then
        echo "${ffv_base}.rng"
    else
        echo "${ffv_base}-${ffv_version}.rng"
    fi
}

# Convert version string (e.g. 2.10) into integer (e.g. 2010) for comparisons
int_version() {
    echo "$1" | awk -F. '{ printf("%d%03d\n", $1,$2); }';
}

# Find the (sub-)schema that best matches a desired version.
#
# Version numbers are assumed to be in the format X.Y,
# where X and Y are integers, and Y is no more than 3 digits,
# or the special value "next".
#
# @COMPAT "next" schemas are deprecated since 2.1.5
best_match() {
    # (Sub-)schema name (e.g. "resources")
    local base="$1"

    # Desired version (e.g. "1.0" or "next")
    local target="$2"

    # If not empty, append the best match as an XML externalRef to this file
    # (otherwise, just echo the best match).
    local destination="$3"

    # Arbitrary text to print before XML (generally spaces to indent)
    local prefix="$4"

    best="0.0"
    for rng in $(list_candidates "${base}"); do
        case ${rng} in
            ${base}-${target}.rng)
                # We found exactly what was requested
                best=${target}
                break
                ;;
            *-next.rng)
                # "Next" schemas cannot be a best match unless directly requested
                ;;
            *)
                v=$(version_from_filename "${rng}")
	        if [ $(int_version "${v}") -gt $(int_version "${best}") ]; then
                    # This version beats the previous best match

                    if [ "${target}" = "next" ]; then
                        best=${v}
                    elif [ $(int_version "${v}") -lt $(int_version "${target}") ]; then
                        # This value is best only if it's still less than the target
                        best=${v}
                    fi
                fi
                ;;
        esac
    done

    if [ "$best" != "0.0" ]; then
        found=$(filename_from_version "$best" "$base")
        if [ -z "$destination" ]; then
            echo "$(basename $found)"
        else
            echo "${prefix}<externalRef href=\"$(basename $found)\"/>" >> "$destination"
        fi
        return 0
    fi
    return 1
}

version_diff() {
    # diff fails with ec=2 if no predecessor is found;
    # this uses '=' GNU extension to sed, if that's not available,
    # one can use: hline=`echo "$${p}" | grep -Fn "$${hunk}" | cut -d: -f1`;
    # XXX: use line information from hunk to avoid "not detected" for ambiguity
    for p in $*; do
        set $(echo "$p" | tr '-' ' ')
        echo "### *-$2.rng vs. predecessor"

        for v in *-"$2".rng; do
            echo "#### $v vs. predecessor"

            b=$(echo "$v" | cut -d- -f1)
            old=$(best_match "$b" "$1")
            p=$(diff -u "$old" "$v" 2>/dev/null)

            case $? in
                1)
                    echo "$p" | sed -n -e '/^@@ /!d;=;p' -e ':l;n;/^\([- ]\|+.*<[^ />]\+\([^/>]\+="ID\|>$$\)\)/bl;s/^[+ ]\(.*\)/\1/p' |
                    while read -r hline; do
                        if read -r h; then
                            read -r i
                        else
                            break
                        fi

                        iline=$(grep -Fn "$i" "$v" | cut -d: -f1)

                        if [ "$(echo "$iline" | wc -l)" = "1" ]; then
                            ctxt=$({ sed -n -e "1,$((iline - 1))p" "$v"
                                     echo "<inject id=\"GOAL\"/>$i"
                                     sed -n -e "$((iline + 1)),$ p" "$v"
                                   } | xsltproc --param skip 1 context-of.xsl -)
                        else
                            ctxt="(not detected)"
                        fi

                        echo "$p" | sed -n -e "$((hline - 2)),$hline!d" -e '/^\(+++\|---\)/p'
                        echo "$h context: $ctxt"
                        echo "$p" | sed -n -e "1,${hline}d" -e '/^\(---\|@@ \)/be;p;d;:e;n;be'
                    done

                    ;;

                2)
                    echo "##### $v has no predecessor"
                    ;;

            esac
        done
    done
}

build_api_rng() {
    local FILENAME="$1"
    local VERSION="$2"

    shift 2

    cat <<EOF >"$FILENAME"
<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <start>
    <element name="pacemaker-result">
      <attribute name="api-version"> <text /> </attribute>
      <attribute name="request"> <text /> </attribute>
      <optional>
        <choice>
EOF
    for RNG in "$@"; do
        best_match "api/$RNG" "$VERSION" "$FILENAME" "          "
    done
    cat <<EOF >>"$FILENAME"
        </choice>
      </optional>
EOF
    best_match api/status "$VERSION" "$FILENAME" "      "
    cat <<EOF >>"$FILENAME"
    </element>
  </start>
</grammar>
EOF
}

build_cib_rng() {
    local FILENAME="$1"
    local VERSION="$2"

    shift 2
    cat <<EOF >"$FILENAME"
<?xml version="1.0" encoding="UTF-8"?>
<grammar xmlns="http://relaxng.org/ns/structure/1.0" datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
  <start>
    <element name="cib">
EOF
    best_match cib "$VERSION" "$FILENAME" "      "
    cat <<EOF >>"$FILENAME"
      <element name="configuration">
        <interleave>
EOF
    for RNG in "$@"; do
        best_match "$RNG" "$VERSION" "$FILENAME" "          "
    done
    cat <<EOF >>"$FILENAME"
        </interleave>
      </element>
      <optional>
        <element name="status">
EOF
    best_match status "$VERSION" "$FILENAME" "          "
    cat <<EOF >>"$FILENAME"
        </element>
      </optional>
    </element>
  </start>
</grammar>
EOF
}

# Allow building RNGs from a different directory
cd "$(dirname $0)"

case "$1" in
    match)
        # Using readlink allows building from a different directory
        best_match "$2" "$3" "$(readlink -f "$4")" "$5"
        ;;

    diff)
        shift
        version_diff "$@"
        ;;

    build_api_rng)
        build_api_rng "$2" "$3" "${@:4}"
        ;;

    build_cib_rng)
        build_cib_rng "$2" "$3" "${@:4}"
        ;;

    *)
        echo "Invalid command: $1"
        exit 1
        ;;
esac