Whamcloud - gitweb
b=20748
[fs/lustre-release.git] / lustre / doc / chbar.sh
1 #!/bin/sh
2 #       Gadget to take two LaTeX files and produce a third which
3 #       has changebars highlighting the difference between them.
4 #
5 # Version 1.2
6 # Author:
7 #       Don Ward, Careful Computing (don@careful.co.uk)
8 # v1.0  April 1989
9 # v1.1  Feb 93  Amended to use changebar.sty (v3.0) and dvips
10 # v1.2  Aug 95  Added support for LaTeX209/LaTeX2e
11 #               Added RCS support to retrive old files
12
13 CMD=`basename $0`
14
15 SED=sed
16 RM="rm -f"
17 DIFF=diff
18 ED=ed
19 AWK=awk
20 GREP=grep
21 MV=mv
22 CAT=cat
23 MKDIR=mkdir
24 CO="co"
25
26 TMPDIR=${TMP-/tmp}/$CMD.$$
27 trap 'test $DEBUG = NO && rm -rf $TMPDIR' 0 1 2 3 6 7 13 15
28 mkdir $TMPDIR || { echo "cannot create directory \`$TMPDIR'." >&2; exit 1; }
29 TMPFILE=${TMPDIR}/$CMD.$$
30 SED_CMD_FILE=$TMPFILE.sed
31
32 usage()
33 {
34 $CAT << _END_
35 Usage:
36   $CMD [-hgG] [-d dir] old new [output]
37         default output is stdout
38
39   $CMD [-hgG] [-d dir] old
40         new file on stdin, output on stdout
41
42   $CMD [-hgG] -d dir -r rev files
43         old file retrieved using RCS
44
45   Gadget to take two LaTeX files and produce a third which
46   has changebars highlighting the difference between them.
47   Changebars are inserted for differences after '\begin{document}'.
48
49   Feature: \`new' can not be named \`-'.
50
51   Options are:
52   -d dir : Write the output to file  \`dir/new', if \`new' is given or
53            to file \`dir/old'.
54            If \`dir' does not exist, it is created.
55            If \`output' is given, it is discarded.
56
57   -r rev : If the LaTeX \`files' are kept under control of the
58            Revision Control System RCS, the old files of
59            the revision \`rev' can be retrived.
60            \`rev' is specified using the RCS conventions.
61            This option must be used together with the \`-d dir' option.
62            \`files' must be a nonempty list of files.
63
64   -h     : Print this info text.
65   -g     : Print some debugging info.
66   -G     : Even more debug info.
67
68   Version 1.2: August 3. 1995
69 _END_
70 exit 1
71 }
72
73 # parse options and arguments
74 DEBUG="NO"
75 DIR=
76 REV=
77 # process options
78 while getopts d:r:gGh i $*
79 do
80   case $i in
81         d ) DIR=$OPTARG;;
82         r ) REV=$OPTARG;;
83         g ) DEBUG="YES" ;;
84         G ) set -x; DEBUG="YES";;
85         h | \
86         * ) usage ;;
87   esac
88 done
89
90 shift `expr $OPTIND - 1`
91
92 case $# in
93   1 ) OLD=$1; NEW="-"; OUT=""   ;;
94   2 ) OLD=$1; NEW=$2;  OUT=""   ;;
95   3 ) OLD=$1; NEW=$2;  OUT="$3" ;;
96   * ) usage ;;
97 esac
98
99 # check correct options
100 if [ ! -z "$DIR" ]
101 then
102   [ -d $DIR ] || $MKDIR $DIR
103 fi
104
105 if [ ! -z "$REV" ]
106 then
107   [ -z "$DIR" ] && usage
108   FILES=$*
109 else
110   FILES=$NEW
111 fi
112
113 # do the work
114 for NEW in $FILES
115 do
116   if [ ! -z "$DIR" ]
117   then
118     if [ $NEW = "-" ]
119     then
120       OUT=$DIR/$OLD
121     else
122       OUT=$DIR/$NEW
123     fi
124   fi
125   if [ ! -z "$REV" ]
126   then
127     OLD=${TMPFILE}.old
128     $CO -p"$REV" -q $NEW > $OLD
129   fi
130
131   [ $DEBUG = "YES" ] && echo "OLD=\`$OLD' NEW=\`$NEW' OUT=\`$OUT'"
132
133   # gather some info about the file
134   # Since we have for sure only the name of the OLD file, ...
135   $GREP "^\\\\begin{document}" $OLD > /dev/null
136   if [ $? -eq 0 ]
137   then
138     [ $DEBUG = "YES" ] && echo "contains a \\begin{document}"
139     HAS_BEGIN_DOC="YES"
140   else
141     [ $DEBUG = "YES" ] && echo "contains no \\begin{document}"
142     HAS_BEGIN_DOC="NO"
143   fi
144
145   # Method to do the work:
146   # 1   Use diff to get an ed script to go from file1 to file2.
147   # 2   Breath on it a bit (with sed) to insert changebar commands.
148   # 3   Apply modified ed script to produce (nearly) the output.
149   # 4   Use awk to insert the changebars option into the \documentstyle
150   #     and to handle changebar commands inside verbatim environments.
151   # 5     Remove changebars before \begin{document} with sed
152
153   #     SED commands to edit ED commands to edit old file
154   $CAT > $SED_CMD_FILE <<\_END_
155 /^\.$/i\
156 \\cbend{}%
157 /^[0-9][0-9]*[ac]$/a\
158 \\cbstart{}%
159 /^[0-9][0-9]*,[0-9][0-9]*[ac]$/a\
160 \\cbstart{}%
161 /^[0-9][0-9]*d$/a\
162 i\
163 \\cbdelete{}%\
164 .
165 /^[0-9][0-9]*,[0-9][0-9]*d$/a\
166 i\
167 \\cbdelete{}%\
168 .
169 _END_
170
171   # note DIFF accepts `-' as stdin
172   $DIFF -b -e $OLD $NEW | \
173         ( $SED -f $SED_CMD_FILE ; echo w ${TMPFILE}.1 ; echo q ) | \
174         $ED - $OLD
175
176   #     AWK commands to insert Changebars style and to protect
177   #     changebar commands in verbatim environments
178   #       and to tell what driver is in use; we assume the `dvips' driver
179
180   $AWK '
181   BEGIN {kind=""; # we saw now \documentXXX[]{}
182   }
183   /^\\documentstyle/{
184     kind = "209";
185     if (index($0, "changebar") == 0 ) {
186       opts = index($0, "[")
187       if (opts > 0)
188         printf "%schangebar,%s\n",substr($0,1,opts),substr($0,opts+1)
189       else
190         printf "\\documentstyle[changebar]%s\n", substr($0,15)
191       next
192     }
193   }
194   /^\\documentclass/{
195     kind = "2e";
196     printf "%s\n", $0
197     printf "\\usepackage[dvips]{changebar}\n"
198     next
199   }
200   /\\begin{document}/ {if (kind == "209" ) {print "\\driver{dvips}"}}
201   /\\begin{verbatim}/{++nesting}
202   /\\end{verbatim}/{--nesting}
203   /\\cbstart{}%|\\cbend{}%|\cbdelete{}%/ {
204     if ( nesting > 0) {
205   #     changebar command in a verbatim environment: Temporarily exit,
206   #     do the changebar command and reenter.
207   #
208   #     The obvious ( printf "\\end{verbatim}%s\\begin{verbatim} , $0 )
209   #     leaves too much vertical space around the changed line(s).
210   #     The following magic seeems to work
211   #
212         print  "\\end{verbatim}\\nointerlineskip"
213         print  "\\vskip -\\ht\\strutbox\\vskip -\\ht\\strutbox"
214         printf "\\vbox to 0pt{\\vskip \\ht\\strutbox%s\\vss}\n", $0
215         print  "\\begin{verbatim}"
216         next
217         }
218   }
219   { print $0 }
220   '  ${TMPFILE}.1 > ${TMPFILE}.2
221
222   # if a \begin{document} is contained in the file,
223   # remove the changebar commands before them
224
225   if [ $HAS_BEGIN_DOC = "YES" ]
226   then
227     SED_CMD="1,/\\\\begin{document}/s/\(\\\\cb[sed][tne][adl][^{}]*{}%\)$/%%\1/"
228     $SED "$SED_CMD" ${TMPFILE}.2 > ${TMPFILE}.3
229   else
230     $CAT ${TMPFILE}.2 > ${TMPFILE}.3
231   fi
232   if [ -z "$OUT" ]
233   then
234     $CAT ${TMPFILE}.3
235  else
236     $MV ${TMPFILE}.3 $OUT
237   fi
238
239 done
240
241 [ $DEBUG =  "NO" ] && $RM ${TMPFILE}.*
242
243 ###############################################################