# (C) 2010 magicant

# Completion script for the "cat" command.
# Supports POSIX 2008, GNU coreutils 8.4, FreeBSD 8.1, OpenBSD 4.8, NetBSD 5.0,
# Mac OS X 10.6.4, SunOS 5.10, HP-UX 11i.

function completion/cat {

	case $("${WORDS[1]}" --version 2>/dev/null) in
		(*'coreutils'*) typeset type=GNU ;;
		(*)             typeset type=$(uname 2>/dev/null) ;;
	esac
	case $type in
		(GNU) typeset long=true ;;
		(*)   typeset long= ;;
	esac

	typeset OPTIONS POSIXOPTIONS ADDOPTIONS ARGOPT PREFIX
	POSIXOPTIONS=( #>#
	"u; disable buffering"
	) #<#

	ADDOPTIONS=()
	case $type in (GNU|*BSD|Darwin|SunOS|HP-UX)
		ADDOPTIONS=( #>#
		"b ${long:+--number-nonblank}; print line number for each non-empty line"
		"n ${long:+--number}; print line number for each line"
		"v ${long:+--show-nonprinting}; print unprintable characters like ^I or M-C"
		) #<#

		case $type in
		(GNU)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"A ${long:+--show-all}; make everything visible"
			"E ${long:+--show-ends}; print \$ at end of each line"
			"e; like -vE: make everything visible but tabs"
			"T ${long:+--show-tabs}; print tabs as ^I"
			"t; like -vT: make everything visible but newlines"
			) #<#
			;;
		(SunOS)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"e; print \$ at end of each line (with -v)"
			"t; print tabs and form-feeds as ^I and ^L (with -v)"
			) #<#
			;;
		(HP-UX)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"e; like -v and print \$ at end of each line"
			"t; like -v and print tabs and form-feeds as ^I and ^L"
			) #<#
			;;
		(*)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"e; like -v and print \$ at end of each line"
			"t; like -v and print tabs as ^I"
			) #<#
			;;
		esac

		case $type in
		(SunOS)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"s; suppress error messages"
			) #<#
			;;
		(HP-UX)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"r; squeeze adjacent empty lines into one"
			"s; suppress error messages"
			) #<#
			;;
		(*)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"s; squeeze adjacent empty lines into one"
			) #<#
			;;
		esac

		case $type in
		(GNU)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"--help"
			"--version"
			) #<#
			;;
		(NetBSD)
			ADDOPTIONS=("$ADDOPTIONS" #>#
			"f; print regular files only"
			"l; acquire lock on the standard output while printing"
			) #<#
			;;
		esac
	esac

	OPTIONS=("$POSIXOPTIONS" "$ADDOPTIONS")
	unset POSIXOPTIONS ADDOPTIONS

	command -f completion//parseoptions ${long:+-es}
	case $ARGOPT in
	(-)
		command -f completion//completeoptions
		;;
	(*)
		complete -f
		;;
	esac

}


# vim: set ft=sh ts=8 sts=8 sw=8 noet:
