Debian Codegraph @

helper/check-ram-titles

raw
1 #! /usr/bin/perl
2 use warnings;
3 use strict;
4 use integer;
5 use FindBin;
6 use lib $FindBin::RealBin;
7 use Def;
8
9 # This helper script ensures that ram titles are consistent and correct
10 # throughout debram.txt, that the titles are indented and ordered
11 # correctly, and that the cross-references are sane.
12 #
13 # usage: check-ram-titles [-ch] { debram.txt }
14 #
15 # If no debram.txt filename is supplied, the script examines the main
16 # debram.txt file in the expected place relative to the script. The -c
17 # option yields a ram count.
18 #
19 #
20
21 our $usage = <<END;
22 usage: $0 [-csh] { debram.txt }
23 -c count rams
24 -s run on cmdsel.txt, instead
25 -h print this usage message
26 END
27
28 my $linepat;
29 {
30 my $p1 = qr/\d{${Def::ndig}}/o;
31 my $p2 = qr/\S(?:.*?\S)??/o;
32 $linepat = qr/^($p1) ( *)($p2)(?: (-+>) ($p2))?\s*$/o;
33 }
34 my $shortbar_pat;
35 my $longbar_pat ;
36 {
37 my $w1 = $Def::width_shortbar ;
38 my $w2 = $Def::width_shortbar + 1;
39 $shortbar_pat = qr /^-{$w1}$/o;
40 $longbar_pat = qr /^-{$w2}/o ;
41 }
42
43 # Read command-line arguments and options.
44 my @opt;
45 my @arg;
46 push @{ /^-\S/ ? \@opt : \@arg }, $_ for @ARGV;
47 my %opt = map {
48 my $o = $_;
49 map { substr( $o, $_, 1 ) => 1 } 1 .. length($o)-1
50 } @opt;
51 if ( $opt{'?'} || $opt{h} || @arg > 1 ) {
52 print $usage;
53 exit 0;
54 }
55 my $debram;
56 $debram = shift @arg if @arg;
57 $debram = $opt{s} ? $Def::cmdsel_txt : $Def::debram_txt
58 unless defined $debram;
59
60 my @r1; # head titles
61 my @r2; # body titles
62 my @r3; # tail titles
63
64 my $pat_count = $opt{s} ? qr// : qr/ \(\d+\)/;
65
66 sub ramlvl ($) { $Def::ndig - @{ [ $_[0] =~ /0/g ] } }
67
68 # Parse a title line from head or tail.
69 sub parse ($@) {
70
71 # Are these main-body title lines?
72 my $isbody = shift;
73
74 # Divide each line into fields.
75 my @line;
76 for ( @_ ) {
77 my( $ram, $sp, $title, $xrefs, $ramlvl, $uctitle, @xref );
78 if ( $isbody ) {
79 ( $ram, $title ) =
80 /^(\d{${Def::ndig}}) (\S(?:.*?\S)??)${pat_count}\s*$/o
81 or die
82 "$0: parse: badly formed title line in the main body\n$_\n";
83 }
84 else {
85 my $arrow;
86 ( $ram, $sp, $title, $arrow, $xrefs ) = /$linepat/o
87 or die "$0: parse: badly formed ToC/xref line\n$_\n";
88 if ( defined $arrow ) {
89 length($ram) + length($sp) + length($title)
90 + length($arrow) + 2 == $Def::pos_arrowhead
91 or die "$0: parse: arrow too short or too long\n$_\n";
92 }
93 defined( $xrefs ) or $xrefs = '';
94 @xref = split ' ', $xrefs;
95 }
96 $ramlvl = ramlvl $ram;
97 length( $title ) <= $Def::maxlen_title
98 or die "$0: parse: title too long\n$_\n";
99 $uctitle = uc $title;
100 if ( $isbody ) {
101 $uctitle eq $title
102 or die "$0: parse: lower-case char in title\n$_\n";
103 }
104 else {
105 my $ramlvl1 = $ramlvl + ( $opt{s} && $ramlvl > 1 ? -1 : 0 );
106 length( $sp ) == $Def::ind_shift * ($ramlvl1-1)
107 or die "$0: parse: bad indent in\n$_\n";
108 }
109 push @line, {
110 isbody => $isbody,
111 ram => $ram,
112 ramlvl => $ramlvl,
113 title => $title,
114 uctitle => $uctitle,
115 xref => \@xref,
116 };
117 }
118
119 # Mark each line as endram or parent ram. Check ram ordering.
120 for my $i ( 0 .. $#line-1 ) {
121 $line[$i+1]{ram} gt $line[$i]{ram}
122 or die "$0: parse: misordered rams near $line[$i]{ram}\n";
123 $line[$i]{endram} = $isbody
124 || $line[$i+1]{ramlvl} <= $line[$i]{ramlvl};
125 }
126 $line[-1]{endram} = 1 if @line;
127
128 # Hash the rams.
129 my %ram = map { $_->{ram} => $_ } @line;
130
131 # Guard against stray or unsorted cross-references.
132 for my $ram ( keys %ram ) {
133 my $ram1 = $ram{$ram};
134 my $parent = $ram;
135 $parent =~ s/^(.*)[^0]/${1}0/;
136 my $ram_prev;
137 my $n_match_prev = $Def::ndig;
138 for my $xref ( @{ $ram1->{xref} } ) {
139 $ram{$xref}
140 or die "$0: parse: xref to unknown ram $xref\n";
141 $xref eq $parent
142 and die "$0: parse: xref to ${ram}'s immediate parent\n";
143 my $errmsg
144 = "$0: parse: misordered xrefs from $ram\n";
145 my $j = 0; # count of matching digits.
146 ++$j while $j < $Def::ndig
147 && substr( $ram, $j, 1 ) eq substr( $xref, $j, 1 );
148 $j < $Def::ndig && $j <= $n_match_prev or die $errmsg;
149 if ( $j < $n_match_prev ) {
150 $n_match_prev = $j;
151 $ram_prev = $xref;
152 next;
153 }
154 defined( $ram_prev ) && $ram_prev ge $xref and die $errmsg;
155 $ram_prev = $xref;
156 }
157 }
158
159 return %ram;
160
161 }
162
163 # Read the title lines in from each of debram.txt's three sections:
164 # head; body; tail.
165 open F, '<', $debram;
166 1 while !eof(F) && <F> ne (
167 $opt{s} ? $Def::mark_commands : $Def::mark_toc
168 );
169 unless ( $opt{s} ) { <F>; <F>; }
170 {
171 my $wasblank = 0;
172 while ( !eof(F) ) {
173 local $_ = <F>;
174 if ( my( $ram ) = /^(\d{${Def::ndig}})/o ) {
175 push @r1, $_;
176 if ( $opt{s} ) {
177 my $ramlvl = ramlvl $ram;
178 ( $ramlvl <= 1 && $wasblank == 1 ) ||
179 ( $ramlvl > 1 && $wasblank == 0 )
180 or die
181 "$0: blank line is wrong above ram $ram in the Contents\n";
182 $wasblank = 0;
183 }
184 }
185 elsif ( $opt{s} ) {
186 if ( !/\S/ ) { ++$wasblank }
187 elsif ( /^\*{${Def::ndig}} \S/ ) { $wasblank = 0 }
188 else { last }
189 $wasblank <= 1
190 or die "$0: the Contents have a double blank line\n";
191 }
192 else { last }
193 }
194 }
195 1 while !eof(F) && <F> ne (
196 $opt{s} ? $Def::mark_commands : $Def::mark_main_body
197 );
198 {
199 my $intitle = 0;
200 while ( !eof(F) ) {
201 local $_ = <F>;
202 $intitle = !$intitle, next if /$longbar_pat/o;
203 next unless $intitle;
204 if ( /^\d{${Def::ndig}}/o ) { push @r2, $_ }
205 elsif ( !( $opt{s} && /\S/ ) ) { last }
206 }
207 }
208 unless ( $opt{s} ) {
209 for ( 1, 2 ) { 1 while !eof(F) && <F> =~ /\S/ }
210 {
211 while ( !eof(F) ) {
212 local $_ = <F>;
213 if ( /^\d{${Def::ndig}}/o ) { push @r3, $_ }
214 else { last }
215 }
216 }
217 }
218 !eof(F) or die "$0: the file is missing some parts or is not even a "
219 . ( $opt{s} ? 'cmdsel.txt' : 'debram.txt' ) . "\n";
220 close F;
221
222 # Parse the three sections:
223 my %r1 = parse 0, @r1;
224 my %r2 = parse 1, @r2;
225 my %r3 = parse 0, @r3;
226
227 # Ensure that the rams in each section are the same, with the same
228 # titles, and that no title is duplicated.
229 for my $r (
230 $opt{s}
231 ? ( [ \%r1 ] )
232 : ( [ \%r1, \%r3 ], [ \%r3, \%r1 ] )
233 ) {
234 my( $ra, $rb ) = @$r;
235 for my $ram ( sort keys %$ra ) {
236 my $ram1 = $ra->{$ram};
237 my $title = $ram1->{title};
238 my $uctitle = $ram1->{uctitle};
239 my $endram = $ram1->{endram};
240 (
241 !defined($rb) ||
242 ( $rb->{$ram} && $title eq $rb->{$ram}{title} )
243 ) && (
244 !$endram ||
245 ( $r2{$ram} && $uctitle eq $r2{$ram}{title} )
246 ) or die "$0: title mismatch, ram $ram\n";
247 }
248 }
249 for my $ram ( sort keys %r2 ) {
250 my $ram1 = $r2{$ram};
251 my $title = $ram1->{title};
252 ( $r1{$ram} && $r1{$ram}{endram} && $title eq $r1{$ram}{uctitle} )
253 && (
254 $opt{s} ||
255 ( $r3{$ram} && $r3{$ram}{endram} && $title eq $r3{$ram}{uctitle} )
256 )
257 or die "$0: title mismatch, ram $ram\n";
258 }
259 {
260 my %uctitle = ();
261 for my $ram ( keys %r1 ) {
262 my $uctitle = $r1{$ram}{uctitle};
263 exists $uctitle{$uctitle}
264 and die "$0: duplicate title\n$uctitle\n";
265 $uctitle{$uctitle} = $ram;
266 }
267 }
268
269 # Check that no ram lacks a parent.
270 for my $ram ( keys %r1 ) {
271 my $parent = $ram;
272 $parent =~ s/^(0*[1-9]\d*)([1-9])(0*)/${1}0${3}/;
273 $r1{$parent} or die "$0: ram $ram lacks a parent $parent\n";
274 }
275
276 # Print ram counts if -c.
277 if ( $opt{c} ) {
278 my $n_end = 0;
279 my $n_parent = 0;
280 ++( $_->{endram} ? $n_end : $n_parent ) for values %r1;
281 printf
282 "%${Def::ndig_ram_count}d end rams\n" .
283 "%${Def::ndig_ram_count}d parent rams\n" .
284 "%${Def::ndig_ram_count}d total rams\n",
285 $n_end, $n_parent, $n_end + $n_parent;
286 }
287