Skip to content

Commit

Permalink
Provide wrapped version of diff3
Browse files Browse the repository at this point in the history
Issue: #78
  • Loading branch information
dspinellis committed Aug 20, 2017
1 parent 68db8d7 commit 71e5fd0
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 4 deletions.
Empty file added simple-shell/diff3-0.success
Empty file.
5 changes: 5 additions & 0 deletions simple-shell/diff3-1.success
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
====3
1:0a
2:0a
3:1c
a
7 changes: 7 additions & 0 deletions simple-shell/diff3-2-stdin1.success
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
====
1:1,2c
a
b
2:0a
3:1c
b
6 changes: 6 additions & 0 deletions simple-shell/diff3-2-stdin2.success
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
====2
1:1c
3:1c
b
2:1c
a
6 changes: 6 additions & 0 deletions simple-shell/diff3-2.success
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
====2
1:1c
3:1c
b
2:1c
a
8 changes: 8 additions & 0 deletions simple-shell/diff3-3.success
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
====
1:1,2c
a
c
2:1c
b
3:1c
c
1 change: 1 addition & 0 deletions unix-tools/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cat
cmp
coreutils/*
diff
diff3
etc/*
gawk/*
gnulib/*
Expand Down
12 changes: 8 additions & 4 deletions unix-tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ make:
$(MAKE) -C coreutils
$(MAKE) -C grep

build-install: cat cmp diff tee make
build-install: cat cmp diff diff3 tee make
mkdir -p ../build/libexec/dgsh ../build/bin
cp bash/bash ../build/libexec/dgsh/
rm -f ../build/bin/dgsh
Expand All @@ -103,11 +103,11 @@ build-install: cat cmp diff tee make
cp grep/src/grep ../build/libexec/dgsh/
cp grep/src/egrep ../build/libexec/dgsh/
cp grep/src/fgrep ../build/libexec/dgsh/
chmod 755 cat cmp diff tee
cp -p cat cmp diff tee ../build/libexec/dgsh/
chmod 755 cat cmp diff diff3 tee
cp -p cat cmp diff diff3 tee ../build/libexec/dgsh/
./install-wrapped.sh $$(cd .. ; pwd)/build

install: cat cmp diff tee make
install: cat cmp diff diff3 tee make
$(MAKE) -C bash install
rm -f $(DESTDIR)$(PREFIX)/bin/dgsh
ln $(DESTDIR)$(DGSHPATH)/bash $(DESTDIR)$(PREFIX)/bin/dgsh || install $(DESTDIR)$(DGSHPATH)/bash $(DESTDIR)$(PREFIX)/bin/dgsh
Expand All @@ -117,6 +117,7 @@ install: cat cmp diff tee make
install cat $(DESTDIR)$(DGSHPATH)
install cmp $(DESTDIR)$(DGSHPATH)
install diff $(DESTDIR)$(DGSHPATH)
install diff3 $(DESTDIR)$(DGSHPATH)
install tee $(DESTDIR)$(DGSHPATH)
./install-wrapped.sh
rm -rf $(DESTDIR)$(MANDUMPDIR)
Expand All @@ -130,6 +131,9 @@ cmp: cmp.sh
diff: diff.sh
install $? $@

diff3: diff3.sh
install $? $@

tee: tee.sh
install $? $@

Expand Down
62 changes: 62 additions & 0 deletions unix-tools/diff3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env bash
#!dgsh
#
# Dgsh-compatible wrapping of the GNU diff3 command
# Depending on the arguments, the command can accept 0-3 inputs
#


# For the parsing of long options see
# https://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options/7680682#7680682
optspec=":AeE3xXimaTL:v-:"
while getopts "$optspec" optchar; do
case "${optchar}" in
-)
case "${OPTARG}" in
# Long options with mandatory arguments. In these the argument
# can appear separated from the option name, and must be removed.
diff-program|label)
OPTIND=$(($OPTIND + 1))
;;
esac
;;
esac
done

# Examine the number of remaining (non-option) arguments
case $(($# - $OPTIND + 1)) in
0)
# Exactly three inputs are required
exec dgsh-wrap /usr/bin/diff3 "$@" '<|' '<|' '<|'
;;
1)
if [[ "${!OPTIND}" = '-' ]] ; then
# Two inputs (excluding stdin) are required
exec dgsh-wrap -I /usr/bin/diff3 "$@" '<|' '<|'
else
# Two inputs (including stdin) are required
exec dgsh-wrap /usr/bin/diff3 "$@" '<|' '<|'
fi
;;
2)
ARG2=$((OPTIND + 1))
if [[ "${!OPTIND}" = '-' ]] || [[ "${!ARG2}" = '-' ]] ; then
# One (non-stdin) input is required
exec dgsh-wrap -I /usr/bin/diff3 "$@" '<|'
else
# One (stdin) input is required
exec dgsh-wrap /usr/bin/diff3 "$@" -
fi
;;
*)
ARG2=$((OPTIND + 1))
ARG3=$((OPTIND + 2))
if [[ "${!OPTIND}" = '-' ]] || [[ "${!ARG2}" = '-' ]] || [[ "${!ARG3}" = '-' ]] ; then
# One (stdin) input is required
exec dgsh-wrap /usr/bin/diff3 "$@"
else
# No input is required
exec dgsh-wrap -i 0 /usr/bin/diff3 "$@"
fi
;;
esac
8 changes: 8 additions & 0 deletions unix-tools/run_all_simple_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,11 @@ there" |
./run_simple_test.sh $PSDIR cmp1-same1 'echo -n | cmp - /dev/null'
./run_simple_test.sh $PSDIR cmp1-same2 'echo -n | cmp /dev/null -'
./run_simple_test.sh $PSDIR cmp0 'cmp /dev/null /dev/null'

# Wrapped diff3
./run_simple_test.sh $PSDIR diff3-3 '{{ echo a ; echo b ; echo c; }} | diff3'
./run_simple_test.sh $PSDIR diff3-2 '{{ echo a ; echo b ; }} | diff3 /dev/null'
./run_simple_test.sh $PSDIR diff3-2-stdin1 '{{ echo a ; echo b ; }} | diff3 - /dev/null'
./run_simple_test.sh $PSDIR diff3-2-stdin2 '{{ echo a ; echo b ; }} | diff3 /dev/null -'
./run_simple_test.sh $PSDIR diff3-1 '{{ echo a ; }} | diff3 /dev/null /dev/null'
./run_simple_test.sh $PSDIR diff3-0 'diff3 /dev/null /dev/null /dev/null'
6 changes: 6 additions & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ <h2>Adapted tools</h2>
<td>1</td>
<td>Typically two inputs. Compare an arbitrary number of input streams with the <code>--from-file</code> or <code>--to-file</code> options</td>
</tr>
<tr>
<td>diff3</td>
<td>0&mdash;3</td>
<td>1</td>
<td></td>
</tr>
<tr>
<td>grep</td>
<td>0&mdash;2</td>
Expand Down

0 comments on commit 71e5fd0

Please sign in to comment.