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