summaryrefslogtreecommitdiff
path: root/contrib/gdiffmk/gdiffmk.sh
blob: 68e5b8f588d85bf2ff219a798d6c86fb7e5ade8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#! /bin/sh
# Copyright (C) 2004, 2005 Free Software Foundation, Inc.
# Written by Mike Bianchi <MBianchi@Foveal.com <mailto:MBianchi@Foveal.com>>

# This file is part of the gdiffmk utility, which is part of groff.

# groff is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# groff is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
# License for more details.

# You should have received a copy of the GNU General Public License
# along with groff; see the files COPYING and LICENSE in the top
# directory of the groff source.  If not, write to the Free Software
# Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# This file is part of GNU gdiffmk.


cmd=$( basename $0 )

function Usage {
	if test "$#" -gt 0
	then
		echo >&2 "${cmd}:  $@"
	fi
	echo >&2 "\

Usage:  ${cmd} [ OPTIONS ] FILE1 FILE2 [ OUTPUT ]
Place difference marks into the new version of a groff/nroff/troff document.
FILE1 and FILE2 are compared, using \`diff', and FILE2 is output with
groff \`.mc' requests added to indicate how it is different from FILE1.

  FILE1   Previous version of the groff file.  \`-' means standard input.
  FILE2   Current version of the groff file.   \`-' means standard input.
          Either FILE1 or FILE2 can be standard input, but not both.
  OUTPUT  Copy of FILE2 with \`.mc' commands added.
          \`-' means standard output (the default).

OPTIONS:
  -a ADDMARK     Mark for added groff source lines.    Default: \`+'.
  -c CHANGEMARK  Mark for changed groff source lines.  Default: \`|'.
  -d DELETEMARK  Mark for deleted groff source lines.  Default: \`*'.

  -D             Show the deleted portions from changed and deleted text.
                  Default delimiting marks:  \`[[' .... \`]]'.
  -B             By default, the deleted texts marked by the \`-D' option end
                  with an added troff \`.br' command.  This option prevents
                  the added \`.br'.
  -M MARK1 MARK2 Change the delimiting marks for the \`-D' option.

  -x DIFFCMD     Use a different diff(1) command;
                  one that accepts the \`-Dname' option, such as GNU diff.
  --version      Print version information on the standard output and exit.
  --help         Print this message on the standard error.
"
	exit 255
}


function Exit {
	exitcode=$1
	shift
	for arg
	do
		echo >&2 "${cmd}:  $1"
		shift
	done
	exit ${exitcode}
}

#	Usage:  FileRead  exit_code  filename
#
#	Check for existence and readability of given file name.
#	If not found or not readable, print message and exit with EXIT_CODE.
function FileRead {
	case "$2" in
	-)
		return
		;;
	esac

	if test ! -e "$2"
	then
		Exit $1 "File \`$2' not found."
	fi
	if test ! -r "$2"
	then
		Exit $1 "File \`$2' not readable."
	fi
}


#	Usage:  FileCreate  exit_code  filename
#
#	Create the given filename if it doesn't exist.
#	If unable to create or write, print message and exit with EXIT_CODE.
function FileCreate {
	case "$2" in
	-)
		return
		;;
	esac

	if ! touch "$2" 2>/dev/null
	then
		if test ! -e "$2"
		then
			Exit $1 "File \`$2' not created; " \
			  "Cannot write directory \`$( dirname "$2" )'."
		fi
		Exit $1 "File \`$2' not writeable."
	fi
}

function WouldClobber {
	case "$2" in
	-)
		return
		;;
	esac

	if test "$1" -ef "$3"
	then
		Exit 3 \
		  "The $2 and OUTPUT arguments both point to the same file," \
		  "\`$1', and it would be overwritten."
	fi
}

ADDMARK='+'
CHANGEMARK='|'
DELETEMARK='*'
MARK1='[['
MARK2=']]'

function RequiresArgument {
	#	Process flags that take either concatenated or
	#	separated values.
	case "$1" in
	-??*)
		expr "$1" : '-.\(.*\)'
		return 1
		;;
	esac

	if test "$#" -lt 2
	then
		Exit 255 "Option \`$1' requires a value."
	fi

	echo "$2"
	return 0
}

badoption=
DIFFCMD=diff
D_option=
br=.br
for OPTION
do
	case "${OPTION}" in
	-a*)
		ADDMARK=$( RequiresArgument "${OPTION}" $2 )		&&
			shift
		;;
	-c*)
		CHANGEMARK=$( RequiresArgument "${OPTION}" $2 )		&&
			shift
		;;
	-d*)
		DELETEMARK=$( RequiresArgument "${OPTION}" $2 )		&&
			shift
		;;
	-D )
		D_option=D_option
		;;
	-M* )
		MARK1=$( RequiresArgument "${OPTION}" $2 )		&&
			shift
		if [ $# -lt 2 ]
		then
			Usage "Option \`-M' is missing the MARK2 value."
		fi
		MARK2=$2
		shift
		;;
	-B )
		br=.
		;;
	-x* )
		DIFFCMD=$( RequiresArgument "${OPTION}" $2 )		&&
			shift
		;;
	--version)
		echo "GNU ${cmd} (groff) version @VERSION@"
		exit 0
		;;
	--help)
		Usage
		;;
	--)
		#	What follows  --  are file arguments
		shift
		break
		;;
	-)
		break
		;;
	-*)
		badoption="${cmd}:  invalid option \`$1'"
		;;
	*)
		break
		;;
	esac
	shift
done

${DIFFCMD} -Dx /dev/null /dev/null >/dev/null 2>&1  ||
	Usage "The \`${DIFFCMD}' program does not accept"	\
		"the required \`-Dname' option.
Use GNU diff instead.  See the \`-x DIFFCMD' option."

if test -n "${badoption}"
then
	Usage "${badoption}"
fi

if test "$#" -lt 2  -o  "$#" -gt 3
then
	Usage "Incorrect number of arguments."
fi

if test "1$1" = 1-  -a  "2$2" = 2-
then
	Usage "Both FILE1 and FILE2 are \`-'."
fi

FILE1=$1
FILE2=$2

FileRead 1 "${FILE1}"
FileRead 2 "${FILE2}"

if test "$#" = 3
then
	case "$3" in
	-)
		#	output goes to standard output
		;;
	*)
		#	output goes to a file
		WouldClobber "${FILE1}" FILE1 "$3"
		WouldClobber "${FILE2}" FILE2 "$3"

		FileCreate 3 "$3"
		exec >$3
		;;
	esac
fi

#	To make a very unlikely label even more unlikely ...
label=__diffmk_$$__

sed_script='
		/^#ifdef '"${label}"'/,/^#endif \/\* '"${label}"'/ {
		  /^#ifdef '"${label}"'/          s/.*/.mc '"${ADDMARK}"'/
		  /^#endif \/\* '"${label}"'/     s/.*/.mc/
		  p
		  d
		}
		/^#ifndef '"${label}"'/,/^#endif \/\* [!not ]*'"${label}"'/ {
		  /^#else \/\* '"${label}"'/,/^#endif \/\* '"${label}"'/ {
		    /^#else \/\* '"${label}"'/    s/.*/.mc '"${CHANGEMARK}"'/
		    /^#endif \/\* '"${label}"'/   s/.*/.mc/
		    p
		    d
		  }
		  /^#endif \/\* \(not\|!\) '"${label}"'/ {
		   s/.*/.mc '"${DELETEMARK}"'/p
		   a\
.mc
		  }
		  d
		}
		p
	'

if [ ${D_option} ]
then
	sed_script='
		/^#ifdef '"${label}"'/,/^#endif \/\* '"${label}"'/ {
		  /^#ifdef '"${label}"'/          s/.*/.mc '"${ADDMARK}"'/
		  /^#endif \/\* '"${label}"'/     s/.*/.mc/
		  p
		  d
		}
		/^#ifndef '"${label}"'/,/^#endif \/\* [!not ]*'"${label}"'/ {
		  /^#ifndef '"${label}"'/ {
		   i\
'"${MARK1}"'
		   d
		  }
		  /^#else \/\* '"${label}"'/ ! {
		   /^#endif \/\* [!not ]*'"${label}"'/ ! {
		    p
		    d
		   }
		  }
		  /^#else \/\* '"${label}"'/,/^#endif \/\* '"${label}"'/ {
		    /^#else \/\* '"${label}"'/ {
		     i\
'"${MARK2}"'\
'"${br}"'
		     s/.*/.mc '"${CHANGEMARK}"'/
		     a\
.mc '"${CHANGEMARK}"'
		     d
		    }
		    /^#endif \/\* '"${label}"'/   s/.*/.mc/
		    p
		    d
		  }
		  /^#endif \/\* \(not\|!\) '"${label}"'/ {
		   i\
'"${MARK2}"'\
'"${br}"'
		   s/.*/.mc '"${DELETEMARK}"'/p
		   a\
.mc
		  }
		  d
		}
		p
	'
fi

diff -D"${label}" -- ${FILE1} ${FILE2}  |
	sed -n "${sed_script}"

# EOF