fixnavi.pl 6.39 KB
Newer Older
1
#! /usr/bin/perl
2

3
use warnings;
4 5
use strict;

Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
sub visitDir($)
{
    my ($dir) = @_;
    my @ret = ();
    my @subret = ();
    opendir DIR, $dir or die "$dir: $!\n";
    my @ents = readdir DIR;
    closedir DIR;
    for my $ent (grep !/^\./, @ents) {
        my $ret = $dir."/".$ent;
        if (-d $ret) {
            push @subret, &visitDir($ret);
        } elsif ($ret =~ /\.qdoc$/) {
            push @ret, $ret;
        }
    }
    return @ret, @subret;
}

25 26 27 28 29 30 31 32 33
my @files = ();
my %defines = ();
for (@ARGV) {
    if (/^-D(.*)$/) {
        $defines{$1} = 1;
    } elsif (/^-/) {
        printf STDERR "Unknown option '".$_."'\n";
        exit 1;
    } else {
Oswald Buddenhagen's avatar
Oswald Buddenhagen committed
34 35 36 37 38
        if (-d $_) {
            push @files, visitDir($_);
        } else {
            push @files, $_;
        }
39 40 41
    }
}

42
int(@files) or die "usage: $0 [-D<define>]... <qdoc-file>...\n";
43

44 45 46
my @toc = ();
my %title2page = ();
my $doctitle = "";
47 48
my %prev_skips = ();
my %next_skips = ();
49 50 51 52
my %prev_define_skips = ();
my %next_define_skips = ();
my %prev_polarity_skips = ();
my %next_polarity_skips = ();
53
for my $file (@files) {
54
    my ($curpage, $inhdr, $havetoc, $intoc, $inif) = ("", 0, 0, 0, 0);
55 56 57
    my ($define_skip, $polarity_skip, $skipping) = ("", 0, 0);
    my ($prev_define_skip, $prev_polarity_skip, $prev_skip,
        $next_define_skip, $next_polarity_skip, $next_skip) = ("", 0, "", "", 0, "");
58 59 60
    open FILE, $file or die "File $file cannot be opened.\n";
    while (<FILE>) {
        if (/^\h*\\if\h+defined\h*\(\h*(\H+)\h*\)/) {
61 62
            die "Nested \\if at $file:$.\n" if ($inif);
            $inif = 1;
63
            $skipping = !defined($defines{$1});
64
            if ($inhdr) {
65 66 67 68
                $define_skip = $1;
                $polarity_skip = $skipping;
            }
        } elsif (/^\h*\\else/) {
69
            die "Unmatched \\else in $file:$.\n" if (!$inif);
70 71
            $skipping = 1 - $skipping;
        } elsif (/^\h*\\endif/) {
72 73
            die "Unmatched \\endif in $file:$.\n" if (!$inif);
            $inif = 0;
74
            $skipping = 0;
75
            $define_skip = "";
76 77 78
        } elsif (keys(%title2page) == 1 && /^\h*\\section1 Table Of Contents/) {
            $havetoc = 1;
        } elsif ($havetoc && /^\h*\\list/) {
79
            $intoc++;
80 81 82
        } elsif ($intoc) {
            if (/^\h*\\endlist/) {
                $intoc--;
83
            } elsif (!$skipping && /^\h*\\li\h+\\l\h*{(.*)}$/) {
84 85 86
                push @toc, $1;
            }
        } elsif ($inhdr) {
87 88 89 90 91 92
            if (/^\h*\\previouspage\h+(\H+)/) {
                $prev_skip = $1 if ($skipping);
                ($prev_define_skip, $prev_polarity_skip) = ($define_skip, $polarity_skip);
            } elsif (/^\h*\\nextpage\h+(\H+)/) {
                $next_skip = $1 if ($skipping);
                ($next_define_skip, $next_polarity_skip) = ($define_skip, $polarity_skip);
93 94 95 96 97 98
            } elsif (/^\h*\\page\h+(\H+)/) {
                $curpage = $1;
            } elsif (/^\h*\\title\h+(.+)$/) {
                if ($curpage eq "") {
                    die "Title '$1' appears in no \\page.\n";
                }
99 100 101 102 103 104 105 106 107
                if (length($prev_define_skip)) {
                    ($prev_define_skips{$1}, $prev_polarity_skips{$1}, $prev_skips{$1}) =
                            ($prev_define_skip, $prev_polarity_skip, $prev_skip);
                    $prev_define_skip = $prev_skip = "";
                }
                if (length($next_define_skip)) {
                    ($next_define_skips{$1}, $next_polarity_skips{$1}, $next_skips{$1}) =
                            ($next_define_skip, $next_polarity_skip, $next_skip);
                    $next_define_skip = $next_skip = "";
108 109 110 111
                }
                $title2page{$1} = $curpage;
                $doctitle = $1 if (!$doctitle);
                $curpage = "";
112
                $inhdr = 0;
113 114
            }
        } else {
115
            if (/^\h*\\contentspage\b/) {
116
                $havetoc = 0;
117
                $inhdr = 1;
118 119 120
            }
        }
    }
121 122
    die "Missing \\title in $file\n" if ($inhdr);
    die "Unclosed TOC in $file\n" if ($intoc);
123
    close FILE;
124 125 126 127 128 129 130 131 132 133 134
}

my %prev = ();
my %next = ();
my $last = $doctitle;
for my $title (@toc) {
    $next{$last} = $title2page{$title};
    $prev{$title} = $title2page{$last};
    $last = $title;
}

135 136 137 138 139 140 141 142 143
for my $file (@files) {
    open IN, $file or die "File $file cannot be opened a second time?!\n";
    open OUT, '>'.$file.".out" or die "File $file.out cannot be created.\n";
    my $cutting = 0;
    while (<IN>) {
        if (!$cutting) {
            if (/^\h*\\contentspage/) {
                $cutting = 1;
            }
144
        } else {
145
            if (/^\h*\\title\h+(.+)$/) {
146 147 148
                if (defined($prev_define_skips{$1})) {
                    print OUT "    \\if defined(".$prev_define_skips{$1}.")\n";
                    if ($prev_polarity_skips{$1}) {
149 150 151 152 153
                        print OUT "    \\previouspage ".$prev_skips{$1} if ($prev_skips{$1});
                        print OUT "    \\else\n";
                    }
                }
                print OUT "    \\previouspage ".$prev{$1} if ($prev{$1});
154 155
                if (defined($prev_define_skips{$1})) {
                    if (!$prev_polarity_skips{$1}) {
156 157 158 159 160 161
                        print OUT "    \\else\n";
                        print OUT "    \\previouspage ".$prev_skips{$1} if ($prev_skips{$1});
                    }
                    print OUT "    \\endif\n";
                }
                print OUT "    \\page ".$title2page{$1};
162 163 164
                if (defined($next_define_skips{$1})) {
                    print OUT "    \\if defined(".$next_define_skips{$1}.")\n";
                    if ($next_polarity_skips{$1}) {
165 166 167 168 169
                        print OUT "    \\nextpage ".$next_skips{$1} if ($next_skips{$1});
                        print OUT "    \\else\n";
                    }
                }
                print OUT "    \\nextpage ".$next{$1} if ($next{$1});
170 171
                if (defined($next_define_skips{$1})) {
                    if (!$next_polarity_skips{$1}) {
172 173 174 175 176 177 178 179 180 181
                        print OUT "    \\else\n";
                        print OUT "    \\nextpage ".$next_skips{$1} if ($next_skips{$1});
                    }
                    print OUT "    \\endif\n";
                }
                print OUT "\n";
                $cutting = 0;
            } else {
                next;
            }
182
        }
183
        print OUT $_;
184
    }
185 186 187
    close OUT;
    close IN;
    rename($file.".out", $file) or die "Cannot replace $file with new version.\n";
188
}