]> git.ipfire.org Git - thirdparty/man-pages.git/blob - scripts/check_unbalanced_macros.sh
check_unbalanced_macros.sh: A script to look for unbalanced *roff/man macro pairs
[thirdparty/man-pages.git] / scripts / check_unbalanced_macros.sh
1 #!/bin/bash
2 #
3 # check_unbalanced_macros.sh
4 #
5 # Dec 2007, Michael Kerrisk
6 #
7 # Look for unbalanced pairs of macros in man page source files, with
8 # $1 and $2 specifying the macro pair. These arguments should
9 # _not_ include the leading dot (.) in the macro.
10 # As output, the program prints the line numbers containing each macro,
11 # and if an unbalanced macro is detected, the string "UNBALANCED!"
12 # is printed.
13 #
14 # Example usage:
15 #
16 # sh check_unbalanced_macros.sh nf fi */*.[1-8]
17 # sh check_unbalanced_macros.sh RS RE */*.[1-8]
18 # sh check_unbalanced_macros.sh EX EE */*.[1-8]
19 #
20 ######################################################################
21 #
22 # (C) Copyright 2020, Michael Kerrisk
23 # This program is free software; you can redistribute it and/or
24 # modify it under the terms of the GNU General Public License
25 # as published by the Free Software Foundation; either version 2
26 # of the License, or (at your option) any later version.
27 #
28 # This program is distributed in the hope that it will be useful,
29 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 # GNU General Public License for more details
32 # (http://www.gnu.org/licenses/gpl-2.0.html).
33 #
34
35 if test $# -lt 4; then
36 echo "Usage: $0 opener closer pages..."
37 exit 1
38 fi
39
40 opener="$1"
41 closer="$2"
42 shift 2
43
44 for f in $@; do
45 if egrep "^\.($opener|$closer)" $f > /dev/null; then
46 echo "================== $f"
47
48 nl -ba $f |
49 awk 'BEGIN { level = 0 }
50
51 $2 == "'".$opener"'" { level++ }
52
53 $2 == "'".$opener"'" || $2 == "'".$closer"'" {
54 printf "%s %s %d", $1, $2, level
55 if (level == 0)
56 print " UNBALANCED!"
57 else
58 print ""
59 }
60
61 $2 == "'".$closer"'" { level-- }
62
63 END {
64 if (level != 0)
65 print "UNBALANCED!"
66 }'
67 fi
68 done