]> git.pond.sub.org Git - empserver/blob - info/findsubj.pl
ceb33c82643e6ed093c7c8f2c43bcf6de0bbb38a
[empserver] / info / findsubj.pl
1 #!/usr/bin/perl
2 #
3 #  Empire - A multi-player, client/server Internet based war game.
4 #  Copyright (C) 1986-2007, Dave Pare, Jeff Bailey, Thomas Ruschak,
5 #                           Ken Stevens, Steve McClure
6 #
7 #  This program 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.
11 #
12 #  This program 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.
16 #
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
20 #
21 #  ---
22 #
23 #  See files README, COPYING and CREDITS in the root of the source
24 #  tree for related information and legal notices.  It is expected
25 #  that future projects/authors will amend these files as needed.
26 #
27 #  ---
28 #
29 #  findsubj.pl: Find info subjects, update subjects.mk
30
31 #  Known contributors to this file:
32 #     Ken Stevens (when it was still info.pl)
33 #     Markus Armbruster, 2006
34 #
35
36 # Usage: findsubj.pl INFO-FILE...
37 # Run it at the root of the build tree.  This updates the make include
38 # file subjects.mk, which guides the remaking of info index files.
39 #
40 #       --- Global variables ---
41 # @Subjects       Existing subjects
42 # $filename       The name of the current info file
43 # $chapter{TOPIC} TOPIC's chapter (first arg to .TH)
44 # $see_also{TOPIC}
45 #                 TOPIC's SEE ALSO items (.SA argument)
46 # $sanr{TOPIC}    Line number of TOPIC's .SA request
47 # $subjfil{SUBJECT}
48 #                 info files for SUBJECT separated by space
49 #
50 #     --- File handles ---
51 # F               Filehandle for info page sources and makefiles
52 #
53 #     --- Functions ---
54 #
55 # read_make_var   Read a variable value from a makefile
56 # parse_file      Read an info file
57 # parse_see_also  Create %subjfil from %see_also
58 # error           Print an integrity error to STDERR and exit with code 1.
59
60 use strict;
61 use warnings;
62
63 use Errno qw(ENOENT);
64
65 our (%chapter, %see_also, %sanr);
66 our ($filename, %subjfil);
67
68 # Get known subjects
69 our @Subjects = split(' ', read_make_var("subjects", "subjects.mk", ""));
70 # Get source directory
71 my $srcdir = read_make_var("srcdir", "GNUmakefile");
72
73 # Parse the .t files
74 for my $f (@ARGV) {
75     parse_file("$f");
76 }
77
78 # Create %subjfil from %see_also
79 for my $t (sort keys %see_also) {
80     parse_see_also($t);
81 }
82
83 # Update @Subjects from %subjfil
84 for my $t (@Subjects) {
85     print STDERR "WARNING: The subject $t has been removed.\n"
86         unless exists $subjfil{$t};
87 }
88 for my $t (keys %subjfil) {
89     unless (grep(/^$t$/, @Subjects)) {
90         print STDERR "WARNING: $t is a NEW subject\n";
91         my $fname = "info/$t.t";
92         if (-e $fname) {
93             print STDERR "File $fname exists\n";
94             exit 1;
95         }
96     }
97 }
98 @Subjects = sort keys %subjfil;
99
100 # Update subjects.mk
101 open(F, ">subjects.mk")
102     or die "Can't open subjects.mk for writing: $!";
103 print F "subjects := " . join(' ', @Subjects) . "\n";
104 for my $t (@Subjects) {
105     print F "info/$t.t:$subjfil{$t}\n";
106 }
107 close(F);
108
109 exit 0;
110
111 # Read a variable value from a makefile
112 sub read_make_var {
113     my ($var, $fname, $dflt) = @_;
114     my $val;
115
116     unless (open(F, "<$fname")) {
117         return $dflt if $! == ENOENT and defined $dflt;
118         die "Can't open $fname: $!";
119     }
120     while (<F>) {
121         if (/^[ \t]*\Q$var\E[ \t]:?=*(.*)/) {
122             $val = $1;
123             last;
124         }
125     }
126     close(F);
127     $val or die "Can't find $var in $fname";
128     return $val;
129 }
130
131 # Read an info file
132 # Parse .TH into %chapter and .SA into %see_also, %sanr
133 sub parse_file {
134     ($filename) = @_;
135     my $topic;
136
137     $topic = $filename;
138     $topic =~ s,.*/([^/]*)\.t$,$1,;
139     
140     open(F, "<$filename")
141         or die "Can't open $filename: $!";
142   
143     $_ = <F>;
144     if (/^\.TH (\S+) (\S.+\S)$/) {
145         $chapter{$topic} = $1;
146     } else {
147         error("The first line in the file must be a .TH request");
148     }
149
150     while (<F>) {
151         last if /^\.SA/;
152     }
153
154     if ($_) {
155         if (/^\.SA "([^\"]*)"/) {
156             $see_also{$topic} = $1;
157             $sanr{$topic} = $.;
158         } else {
159             error("Incorrect .SA Syntax.  Syntax should be '.SA \"item1, item2\"'");
160         }
161
162         while (<F>) {
163             error("Multiple .SA requests.  Each file may contain at most one.") if /^\.SA/;
164         }
165     } else {
166         error(".SA request is missing");
167     }
168
169     close F;
170 }
171
172 # Create %subjfil from %see_also
173 sub parse_see_also {
174     my ($topic) = @_;
175     my @see_also = split(/, /, $see_also{$topic});
176     my $wanted = $chapter{$topic};
177     my $found;                 # found a subject?
178
179     $wanted = undef if $wanted eq 'Concept' or $wanted eq 'Command';
180     $filename = "$srcdir/$topic";
181
182     for (@see_also) {
183         if (!exists $see_also{$_}) { # is this entry a subject?
184             $subjfil{$_} .= " info/$topic.t";
185             $found = 1;
186         }
187         if ($wanted && $_ eq $wanted) {
188             $wanted = undef;
189         }
190     }
191
192     $. = $sanr{$topic};
193     error("No subject listed in .SA") unless $found;
194     error("Chapter $wanted not listed in .SA") if $wanted;
195 }
196
197 # Print an integrity error message and exit with code 1
198 sub error {
199     my ($error) = @_;
200
201     print STDERR "findsubj.pl:$filename:$.: $error\n";
202     exit 1;
203 }