Bash#!/bin/sh
readonly escape="$(printf "\033")"
readonly help="Usage: ${0##*/} [OPTION]... [FILE1] [FILE2]
Compares words in two files and report differences, with color support.
Mandatory arguments to long options are mandatory for short options too.
-h, --help display this help and exit
-v, --version output version information and exit
--color[=WHEN] color output; WHEN is 'never', 'always', or 'auto';
plain --color means --color='auto'
Other options are the same as for 'wdiff'."
readonly version="cwdiff 1.1.0"
if [ -z "$WDIFF" ]
then
WDIFF=wdiff
fi
status=0 wdiff_options= color=never default_sd=1 default_ed=1 default_si=1 default_ei=1 sd='[-' ed='-]' si='{+' ei='+}'
options="$(getopt -s sh -n "${0##*/}" -o "hvC123isapltw:x:y:z:nd" -l "help,version,copyright,color::,no-deleted,no-inserted,no-common,ignore-case,statistics,auto-pager,printer,less-mode,terminal,start-delete:,end-delete:,start-insert:,end-insert:,avoid-wraps,diff-input" -- "$@")"
if [ $? -ne 0 ]
then
status=2
fi
eval set -- "$options"
while [ "$1" != -- ]
do
case "$1" in
-h|--help)
echo "$help"
exit 0
;;
-v|--version)
echo "$version"
exit 0
;;
--color)
case "$2" in
never)
color=never
;;
''|auto)
color=auto
;;
always)
color=always
;;
*)
echo "${0##*/}: invalid color '$2'" >&2
status=2
;;
esac
shift 2
;;
-C|--copyright)
exec $WDIFF -C
;;
-1|--no-deleted)
wdiff_options="${wdiff_options}1"
shift
;;
-2|--no-inserted)
wdiff_options="${wdiff_options}2"
shift
;;
-3|--no-common)
wdiff_options="${wdiff_options}3"
shift
;;
-i|--ignore-case)
wdiff_options="${wdiff_options}i"
shift
;;
-s|--statistics)
wdiff_options="${wdiff_options}s"
shift
;;
-a|--auto-paper)
wdiff_options="${wdiff_options}a"
if [ -t 1 ]
then
if [ $default_sd -eq 1 ]
then
sd=
fi
if [ $default_ed -eq 1 ]
then
ed=
fi
if [ $default_si -eq 1 ]
then
si=
fi
if [ $default_ei -eq 1 ]
then
ei=
fi
fi
shift
;;
-p|--printer)
wdiff_options="${wdiff_options}p"
if [ $default_sd -eq 1 ]
then
sd=
fi
if [ $default_ed -eq 1 ]
then
ed=
fi
if [ $default_si -eq 1 ]
then
si=
fi
if [ $default_ei -eq 1 ]
then
ei=
fi
shift
;;
-l|--less-mode)
wdiff_options="${wdiff_options}l"
if [ $default_sd -eq 1 ]
then
sd=
fi
if [ $default_ed -eq 1 ]
then
ed=
fi
if [ $default_si -eq 1 ]
then
si=
fi
if [ $default_ei -eq 1 ]
then
ei=
fi
shift
;;
-t|--terminal)
wdiff_options="${wdiff_options}t"
if [ $default_sd -eq 1 ]
then
sd=
fi
if [ $default_ed -eq 1 ]
then
ed=
fi
if [ $default_si -eq 1 ]
then
si=
fi
if [ $default_ei -eq 1 ]
then
ei=
fi
shift
;;
-w|--start-delete)
default_sd=0
sd="$2"
shift 2
;;
-x|--end-delete)
default_ed=0
ed="$2"
shift 2
;;
-y|--start-insert)
default_si=0
si="$2"
shift 2
;;
-z|--end-insert)
default_ei=0
ei="$2"
shift 2
;;
-n|--avoid-wraps)
wdiff_options="${wdiff_options}n"
shift
;;
-d|--diff-input)
wdiff_options="${wdiff_options}d"
shift
;;
esac
done
if [ $status -ne 0 ]
then
echo "${0##*/}: Try '${0##*/} --help' for more information." >&2
exit 2
fi
if [ -n "$wdiff_options" ]
then
wdiff_options="-$wdiff_options"
fi
if [ $color = always -o \( $color = auto -a -t 1 \) ]
then
exec $WDIFF $wdiff_options -w "$escape[31m$sd" -x "$ed$escape[m" -y "$escape[32m$si" -z "$ei$escape[m" "$@"
else
exec $WDIFF $wdiff_options -w "$sd" -x "$ed" -y "$si" -z "$ei" "$@"
fi