Whamcloud - gitweb
b=23428 Fix lustre built with --enable-lu_ref
[fs/lustre-release.git] / lustre / doc / postbar
1 #! /usr/bin/perl
2 # postbar - Massage chbar.sh output into valid LaTeX
3 # Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
4 # Use is subject to license terms.
5 #
6 # Gord Eagle <gord@clusterfs.com>, 2002-08-10
7
8 my $progname = $0;
9 $progname =~ s|^.*/||;
10 my $CHANGE_ENVIRONMENT = '\\\\(begin|end)\\{([^\\}]+)\\}';
11 my (@envname, @envdepth, @envbuf);
12 my $phony_preamble = 0;
13 my $cbdepth = 0;
14 my $cbfound = 0;
15
16 # Tell whether an environment cannot have arbitrary changebars.
17 sub fragile_environment
18 {
19     my ($env) = @_;
20     return $env ne 'document';
21 }
22
23
24 # Tell whether we can hava arbitrary stuff.
25 sub toplevel
26 {
27     my ($env) = @_;
28     return $env eq 'document';
29 }
30
31
32 sub out
33 {
34     my (@msg) = @_;
35     if ($#envbuf < 0 || toplevel($envname[0])) {
36         print @msg;
37     } else {
38         $envbuf[0] .= join('', @msg);
39     }
40 }
41
42
43 # Leave an environment.
44 sub end_environment
45 {
46     my ($env) = @_;
47
48     #out("%$progname end $env\n");
49     if ($envname[0] ne $env) {
50         die "Expecting \\end{$envname[0]} but got \\end{$env}\n";
51     }
52
53     if ($cbfound) {
54         # Did we find a changebar?
55         $cbfound = !toplevel($envname[1]);
56         if (!$cbfound) {
57             # We found one, and the parent environment is the top level.
58             if ($cbdepth == $envdepth[0]) {
59                 # There was no change in depth, so mark the environment.
60                 $envbuf[0] = "\\cbstart{}%$progname\n" . $envbuf[0];
61                 out("\\cbend{}%$progname\n");
62             } elsif ($envdepth[0] > $cbdepth) {
63                 # There were more ends in the environment, so append them.
64                 for (my $i = 0; $i < $envdepth[0] - $cbdepth; $i ++) {
65                     out("\\cbend{}%$progname\n");
66                 }
67             } else {
68                 # There were more starts, so prepend them.
69                 my $starts;
70                 for (my $i = 0; $i < $cbdepth - $envdepth[0]; $i ++) {
71                     $starts .= "\\cbstart{}%$progname\n";
72                 }
73                 $envbuf[0] = $starts . $envbuf[0];
74             }
75         }
76     }
77
78     # Drop the environment from the list.
79     shift(@envname);
80     shift(@envdepth);
81     out(shift(@envbuf));
82 }
83
84
85 while ($_ = <STDIN>) {
86     chomp;
87     my $env;
88     if (!/\\begin.*\\end/ && /$CHANGE_ENVIRONMENT/o) {
89         $env = $2;
90         if ($1 eq 'begin') {
91             # Enter the new environment.
92             unshift(@envname, $env);
93             unshift(@envdepth, $cbdepth);
94             unshift(@envbuf, '');
95             #out("%$progname depth=$cbdepth, $#envname ($env)\n");
96         } elsif (!$phony_preamble) {
97             out("$_\n");
98             end_environment($env);
99             next;
100         }
101     }   
102
103     if ($#envname >= 0 && /^\\documentclass/) {
104         $phony_preamble = 1;
105     }
106
107     if ($phony_preamble) {
108         # Comment out and ignore the redundant preambles.
109         out("%$progname $_\n");
110         $phony_preamble = 0 if ($env eq 'document');
111         next;
112     } elsif ($#envname >= 0) {
113         # Track the current changebar depth.
114         if (/^\\cbstart/) {
115             $cbdepth ++;
116             if (!toplevel($envname[0])) {
117                 $cbfound = 1;
118                 out("%$progname $_\n");
119                 next;
120             }
121         } elsif (/^\\cbend/) {
122             if ($cbdepth == 0) {
123                 die "$progname: Too many \\cbend{}s\n";
124             }
125             $cbdepth --;
126             if (!toplevel($envname[0])) {
127                 $cbfound = 1;
128                 out("%$progname $_\n");
129                 next;
130             }
131         } elsif (/^\\cbdelete/ && fragile_environment($envname[0])) {
132             # What to do with delete bars?
133             out("%$progname $_\n");
134             next;
135         }
136         out("$_\n");
137     } else {
138         out("$_\n");
139         # Add the options to the usepackage.
140         if (/^\\usepackage.*\{changebar\}$/) {
141             # Prevent PostScript dictionary overflow errors.
142             out("\\def\\cb\@maxpoint{15}\n");
143
144             # Show the bars.
145             out("\\outerbarstrue\n");
146         }
147     }
148
149     if (defined($env)) {
150     }
151 }
152
153 exit(0);