No empty .Rs/.Re
[netbsd-mini2440.git] / gnu / dist / texinfo / util / fixref.gawk
blobf0e3cf6d60628d4c3048fb30785abd9f685e4474
1 #! /usr/local/bin/gawk -f
3 # fixref.awk --- fix xrefs in texinfo documents
4 # Copyright, 1991, Arnold David Robbins, arnold@skeeve.atl.ga.us
5 # Copyright, 1998, Arnold David Robbins, arnold@gnu.org
7 # FIXREF is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # FIXREF is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 # Updated: Jul 21 1992 --- change unknown
22 # Updated: Jul 18 1997 --- bug fix
24 # usage: gawk -f fixref.awk input-file > output-file
25 # or if you have #!: fixref.awk input-file > output-file
27 # Limitations:
28 # 1. no more than one cross reference on a line
29 # 2. cross references may not cross a newline
31 BEGIN \
33 # we make two passes over the file. To do that we artificially
34 # tweak the argument vector to do a variable assignment
36 if (ARGC != 2) {
37 printf("usage: %s texinfo-file\n", ARGV[0]) > "/dev/stderr"
38 exit 1
40 ARGV[2] = "pass=2"
41 ARGV[3] = ARGV[1]
42 ARGC = 4
44 # examine paragraphs
45 RS = ""
47 heading = "@(chapter|appendix|unnumbered|(appendix(sec|subsec|subsubsec))|section|subsection|subsubsection|unnumberedsec|heading|top)"
49 pass = 1
51 # put space between paragraphs on output
52 ORS = "\n\n"
55 pass == 1 && NF == 0 { next }
57 # pass == 1 && /@node/ \
58 # bug fix 7/18/96
59 pass == 1 && /^@node/ \
61 lname = name = ""
62 n = split($0, lines, "\n")
63 for (i = 1; i <= n; i++) {
64 if (lines[i] ~ ("^" heading)) {
65 sub(heading, "", lines[i])
66 sub(/^[ \t]*/, "", lines[i])
67 lname = lines[i]
68 # printf "long name is '%s'\n", lines[i]
69 } else if (lines[i] ~ /@node/) {
70 sub(/@node[ \t]*/, "", lines[i])
71 sub(/[ \t]*,.*$/, "", lines[i])
72 name = lines[i]
73 # printf "node name is '%s'\n", lines[i]
76 if (name && lname)
77 names[name] = lname
78 else if (lname)
79 printf("node name for %s missing!\n", lname) > "/dev/stderr"
80 else
81 printf("long name for %s missing!\n", name) > "/dev/stderr"
83 if (name ~ /:/)
84 printf("node `%s' contains a `:'\n", name) > "/dev/stderr"
86 if (lname) {
87 if (lname ~ /:/)
88 printf("name `%s' contains a `:'\n", lname) > "/dev/stderr"
89 else if (lname ~ /,/) {
90 printf("name `%s' contains a `,'\n", lname) > "/dev/stderr"
91 gsub(/,/, " ", lname)
92 names[name] = lname # added 7/18/97
97 pass == 2 && /@(x|px)?ref{/ \
99 # split the paragraph into lines
100 # write them out one by one after fixing them
101 n = split($0, lines, "\n")
102 for (i = 1; i <= n; i++)
103 if (lines[i] ~ /@(x|px)?ref{/) {
104 res = updateref(lines[i])
105 printf "%s\n", res
106 } else
107 printf "%s\n", lines[i]
109 printf "\n" # avoid ORS
110 next
113 function updateref(orig, refkind, line)
115 line = orig # work on a copy
117 # find the beginning of the reference
118 match(line, "@(x|px)?ref{")
119 refkind = substr(line, RSTART, RLENGTH)
121 # pull out just the node name
122 sub(/.*ref{/, "", line)
123 sub(/}.*$/, "", line)
124 sub(/,.*/, "", line)
126 # debugging
127 # printf("found ref to node '%s'\n", line) > "/dev/stderr"
129 # If the node name and the section name are the same
130 # we don't want to bother doing this.
132 if (! (line in names)) # sanity checking
133 printf("no long name for %s\n", line) > "/dev/stderr"
134 else if (names[line] != line && names[line] !~ /[:,]/) {
135 # build up new ref
136 newref = refkind line ", ," names[line] "}"
137 pat = refkind line "[^}]*}"
139 sub(pat, newref, orig)
142 return orig
145 pass == 2 { print }