2 # Copyright (C) 2004-2018 Free Software Foundation, Inc.
3 # Written by Mike Bianchi <MBianchi@Foveal.com <mailto:MBianchi@Foveal.com>>
4 # Thanks to Peter Bray for debugging.
6 # This file is part of the gdiffmk utility, which is part of groff.
8 # groff is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # groff is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 # License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 # This file is part of GNU gdiffmk.
32 Usage: ${CMD} [ OPTIONS ] FILE1 FILE2 [ OUTPUT ]
33 Place difference marks into the new version of a groff/nroff/troff document.
34 FILE1 and FILE2 are compared, using 'diff', and FILE2 is output with
35 groff '.mc' requests added to indicate how it is different from FILE1.
37 FILE1 Previous version of the groff file. '-' means standard input.
38 FILE2 Current version of the groff file. '-' means standard input.
39 Either FILE1 or FILE2 can be standard input, but not both.
40 OUTPUT Copy of FILE2 with '.mc' commands added.
41 '-' means standard output (the default).
42 If the shell's 'test' does not support option -ef, OUTPUT
43 can only be the standard output.
46 -a ADDMARK Mark for added groff source lines. Default: '+'.
47 -c CHANGEMARK Mark for changed groff source lines. Default: '|'.
48 -d DELETEMARK Mark for deleted groff source lines. Default: '*'.
50 -D Show the deleted portions from changed and deleted text.
51 Default delimiting marks: '[[' .... ']]'.
52 -B By default, the deleted texts marked by the '-D' option end
53 with an added troff '.br' command. This option prevents
55 -M MARK1 MARK2 Change the delimiting marks for the '-D' option.
57 -x DIFFCMD Use a different diff(1) command;
58 one that accepts the '-Dname' option, such as GNU diff.
59 -s SEDCMD Use a different sed(1) command;
61 --version Print version information on the standard output and exit.
62 --help Print this message on the standard error.
79 # Usage: FileRead exit_code filename
81 # Check for existence and readability of given file name.
82 # If not found or not readable, print message and exit with EXIT_CODE.
92 Exit $1 "File '$2' not found."
96 Exit $1 "File '$2' not readable."
101 # Usage: FileCreate exit_code filename
103 # Create the given filename if it doesn't exist.
104 # If unable to create or write, print message and exit with EXIT_CODE.
112 touch "$2" 2>/dev/null
117 Exit $1 "File '$2' not created; " \
118 "Cannot write directory '`dirname "$2"`'."
120 Exit $1 "File '$2' not writeable."
131 # BASH_PROG is set to /bin/sh if bash was not found
132 if test "$HAVE_TEST_EF_OPTION" = "no" -a "$BASH_PROG" = "/bin/sh"
135 "Your shell does support test -ef, [OUTPUT] can only be the" \
138 if test "$1" -ef "$3"
141 "The $2 and OUTPUT arguments both point to the same file," \
142 "'$1', and it would be overwritten."
153 RequiresArgument () {
154 # Process flags that take either concatenated or
158 expr "$1" : '-.\(.*\)'
165 Exit 255 "Option '$1' requires a value."
172 HAVE_TEST_EF_OPTION=@HAVE_TEST_EF_OPTION@
173 BASH_PROG=@BASH_PROG@
184 ADDMARK=`RequiresArgument "${OPTION}" "$2"` &&
188 CHANGEMARK=`RequiresArgument "${OPTION}" "$2"` &&
192 DELETEMARK=`RequiresArgument "${OPTION}" "$2"` &&
199 MARK1=`RequiresArgument "${OPTION}" "$2"` &&
203 Usage "Option '-M' is missing the MARK2 value."
212 SEDCMD=`RequiresArgument "${OPTION}" "$2"` &&
216 DIFFCMD=`RequiresArgument "${OPTION}" "$2"` &&
220 echo "GNU ${CMD} (groff) version @VERSION@"
227 # What follows -- are file arguments
235 BADOPTION="${CMD}: invalid option '$1'"
244 ${DIFFCMD} -Dx /dev/null /dev/null >/dev/null 2>&1 ||
245 Usage "The '${DIFFCMD}' program does not accept" \
246 "the required '-Dname' option.
247 Use GNU diff instead. See the '-x DIFFCMD' option. You can also
248 install GNU diff as gdiff on your system"
250 if test -n "${BADOPTION}"
255 if test $# -lt 2 -o $# -gt 3
257 Usage "Incorrect number of arguments."
260 if test "1$1" = "1-" -a "2$2" = "2-"
262 Usage "Both FILE1 and FILE2 are '-'."
268 FileRead 1 "${FILE1}"
269 FileRead 2 "${FILE2}"
275 # output goes to standard output
278 # output goes to a file
279 WouldClobber "${FILE1}" FILE1 "$3"
280 WouldClobber "${FILE2}" FILE2 "$3"
288 # To make a very unlikely LABEL even more unlikely ...
292 /^#ifdef '"${LABEL}"'/,/^#endif \/\* '"${LABEL}"'/ {
293 /^#ifdef '"${LABEL}"'/ s/.*/.mc '"${ADDMARK}"'/
294 /^#endif \/\* '"${LABEL}"'/ s/.*/.mc/
298 /^#ifndef '"${LABEL}"'/,/^#endif \/\* [!not ]*'"${LABEL}"'/ {
299 /^#else \/\* '"${LABEL}"'/,/^#endif \/\* '"${LABEL}"'/ {
300 /^#else \/\* '"${LABEL}"'/ s/.*/.mc '"${CHANGEMARK}"'/
301 /^#endif \/\* '"${LABEL}"'/ s/.*/.mc/
305 /^#endif \/\* [!not ]*'"${LABEL}"'/ {
306 s/.*/.mc '"${DELETEMARK}"'/p
318 /^#ifdef '"${LABEL}"'/,/^#endif \/\* '"${LABEL}"'/ {
319 /^#ifdef '"${LABEL}"'/ s/.*/.mc '"${ADDMARK}"'/
320 /^#endif \/\* '"${LABEL}"'/ s/.*/.mc/
324 /^#ifndef '"${LABEL}"'/,/^#endif \/\* [!not ]*'"${LABEL}"'/ {
325 /^#ifndef '"${LABEL}"'/ {
330 /^#else \/\* '"${LABEL}"'/ !{
331 /^#endif \/\* [!not ]*'"${LABEL}"'/ !{
336 /^#else \/\* '"${LABEL}"'/,/^#endif \/\* '"${LABEL}"'/ {
337 /^#else \/\* '"${LABEL}"'/ {
341 s/.*/.mc '"${CHANGEMARK}"'/
343 .mc '"${CHANGEMARK}"'
346 /^#endif \/\* '"${LABEL}"'/ s/.*/.mc/
350 /^#endif \/\* [!not ]*'"${LABEL}"'/ {
354 s/.*/.mc '"${DELETEMARK}"'/p
364 ${DIFFCMD} -D"${LABEL}" -- "${FILE1}" "${FILE2}" |
365 ${SEDCMD} -n "${SED_SCRIPT}"