#compdef timeout

autoload -U is-at-least

_timeout() {
    typeset -A opt_args
    typeset -a _arguments_options
    local ret=1

    if is-at-least 5.2; then
        _arguments_options=(-s -S -C)
    else
        _arguments_options=(-s -C)
    fi

    local context curcontext="$curcontext" state line
    _arguments "${_arguments_options[@]}" : \
'-k+[also send a KILL signal if COMMAND is still running this long after the initial signal was sent]: :_default' \
'--kill-after=[also send a KILL signal if COMMAND is still running this long after the initial signal was sent]: :_default' \
'-s+[specify the signal to be sent on timeout; SIGNAL may be a name like '\''HUP'\'' or a number; see '\''kill -l'\'' for a list of signals]:SIGNAL:_default' \
'--signal=[specify the signal to be sent on timeout; SIGNAL may be a name like '\''HUP'\'' or a number; see '\''kill -l'\'' for a list of signals]:SIGNAL:_default' \
'-f[when not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out]' \
'--foreground[when not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out]' \
'-p[exit with the same status as COMMAND, even when the command times out]' \
'--preserve-status[exit with the same status as COMMAND, even when the command times out]' \
'-v[diagnose to stderr any signal sent upon timeout]' \
'--verbose[diagnose to stderr any signal sent upon timeout]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
':duration -- a floating point number with an optional suffix\: '\''s'\'' for seconds (the default), '\''m'\'' for minutes, '\''h'\'' for hours or '\''d'\'' for days ; a duration of 0 disables the associated timeout:_default' \
':command -- a command to execute with optional arguments:_command_names -e' \
&& ret=0
}

(( $+functions[_timeout_commands] )) ||
_timeout_commands() {
    local commands; commands=()
    _describe -t commands 'timeout commands' commands "$@"
}

if [ "$funcstack[1]" = "_timeout" ]; then
    _timeout "$@"
else
    compdef _timeout timeout
fi
