#! /bin/sh

# Complain about code that returns a system error value without an associated
# verbose message.
#
# This script is a kluge and isn't run by default.

t=__wt.$$
trap 'rm -f $t' 0 1 2 3 13 15

cd ..

# Strip out a list of errors that will be flagged, but are OK.
error_ok()
{
	sed -e '/ERET(/d' \
	    -e '/WT_ERR_MSG(/d' \
	    -e '/WT_ERR_TEST(/d' \
	    -e '/WT_PANIC_ERR(/d' \
	    -e '/WT_PANIC_RET(/d' \
	    -e '/WT_RET_MSG(/d' \
	    -e '/\/intpack.i:.*EINVAL/d'\
	    -e '/\/intpack.i:.*ENOMEM/d'\
	    -e '/\/pack_impl.c:.*EINVAL/d'\
	    -e '/\/pack_impl.c:.*ENOMEM/d'\
	    -e '/\/pack_stream.c:.*ENOMEM/d'\
	    -e '/\/packing.i:.*EINVAL/d'\
	    -e '/__config_err(/d' \
	    -e '/__wt_block_panic(/d' \
	    -e '/__wt_err(/d' \
	    -e '/__wt_errx(/d' \
	    -e '/csv_error(/d' \
	    -e '/nop_error(/d' \
	    -e '/rotn_error(/d' \
	    -e '/zlib_error(/d'
}

# Loop through source files.
for f in `find ext src -name '*.[ci]'`; do
	if expr "$f" : 'ext/datasources/helium/helium.c' > /dev/null; then
		continue
	fi
	if expr "$f" : 'src/os_win/os_winerr.c' > /dev/null; then
		continue
	fi
	if expr "$f" : 'src/utilities/.*' > /dev/null; then
		continue
	fi

	# Strip include files,
	# then use the C preprocessor to strip comments,
	# then turn each file into a single line,
	# then chunk the file by semicolons,
	# then search for explicit error returns (ignoring EBUSY),
	# then prepend the file name to each line,
	# then skip known calls that include verbose messages.
	sed '/^#include/d' $f |
	${CC:-cc} -E - |
	tr -s '\012' ' ' | tr ';' '\012' |
	egrep -w 'EPERM|ENOENT|ESRCH|EINTR|EIO|ENXIO|E2BIG|ENOEXEC|EBADF|ECHILD|EDEADLK|ENOMEM|EACCES|EFAULT|ENOTBLK|EEXIST|EXDEV|ENODEV|ENOTDIR|EISDIR|EINVAL|ENFILE|EMFILE|ENOTTY|ETXTBSY|EFBIG|ENOSPC|ESPIPE|EROFS|EMLINK|EPIPE|EDOM|ERANGE|EAGAIN|EWOULDBLOCK|EINPROGRESS|EALREADY|ENOTSOCK|EDESTADDRREQ|EMSGSIZE|EPROTOTYPE|ENOPROTOOPT|EPROTONOSUPPORT|ESOCKTNOSUPPORT|EOPNOTSUPP|ENOTSUP|EPFNOSUPPORT|EAFNOSUPPORT|EADDRINUSE|EADDRNOTAVAIL|ENETDOWN|ENETUNREACH|ENETRESET|ECONNABORTED|ECONNRESET|ENOBUFS|EISCONN|ENOTCONN|ESHUTDOWN|ETOOMANYREFS|ETIMEDOUT|ECONNREFUSED|ELOOP|ENAMETOOLONG|EHOSTDOWN|EHOSTUNREACH|ENOTEMPTY|EPROCLIM|EUSERS|EDQUOT|ESTALE|EREMOTE|EBADRPC|ERPCMISMATCH|EPROGUNAVAIL|EPROGMISMATCH|EPROCUNAVAIL|ENOLCK|ENOSYS|EFTYPE|EAUTH|ENEEDAUTH|EIDRM|ENOMSG|EOVERFLOW|ECANCELED|EILSEQ|ENOATTR|EDOOFUS|EBADMSG|EMULTIHOP|ENOLINK|EPROTO|ENOTCAPABLE|ECAPMODE|ENOTRECOVERABLE|EOWNERDEAD|ELAST|ERESTART|EJUSTRETURN|ENOIOCTL|EDIRIOCTL' |
	sed -e 's/^[ 	]*//' \
	    -e "s;^;$f: ;" |
	    error_ok
done > $t

test -s $t && {
	echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
	echo 'Unexpected error usage.'
	echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="
	cat $t
	exit 1
}
exit 0
