X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=subcommand;h=e544ab0252cc0b96eecefc7f489ed0d9892cb468;hb=4047e938ebff10557898cb43148e70ce9414c324;hp=956fb5c865ad3ae780ac7344917345879380aa8e;hpb=46d356bc055978057542e999892ec6e0cf1c0153;p=gsu.git diff --git a/subcommand b/subcommand index 956fb5c..e544ab0 100644 --- a/subcommand +++ b/subcommand @@ -96,11 +96,12 @@ gsu_check_arg_count() # [[ -n "$o_c" ]] && echo "The -c option was given with arg $o_c" gsu_getopts() { - local i c tab=' ' cr=' + local -i i + local c c1 c2 tab=' ' cr=' ' gsu_check_arg_count $# 1 1 - if (($ret < 0)); then + if ((ret < 0)); then gsu_err_msg exit 1 fi @@ -126,7 +127,7 @@ gsu_getopts() result="local _gsu_getopts_opt" for ((i=0; i < ${#1}; i++)); do c1=${1:$i:1} - c2=${1:$(($i + 1)):1} + c2=${1:$((i + 1)):1} result+=" o_$c1=" if [[ "$c2" = ":" ]]; then let i++ @@ -141,7 +142,7 @@ gsu_getopts() " for ((i=0; i < ${#1}; i++)); do c1=${1:$i:1} - c2=${1:$(($i + 1)):1} + c2=${1:$((i + 1)):1} result+="$tab$tab$c1) o_$c1=" if [[ "$c2" = ":" ]]; then result+="\"\$OPTARG\"" @@ -175,7 +176,7 @@ _gsu_print_available_commands() for cmd in $cmds; do printf '%s' "$cmd" let ++count - if (($count % 4)); then + if ((count % 4)); then printf '\t' ((${#cmd} < 8)) && printf '\t' else @@ -226,9 +227,9 @@ com_prefs() gsu_getopts "$com_prefs_options" eval "$result" - (($ret < 0)) && return + ((ret < 0)) && return gsu_check_arg_count $# 0 0 - (($ret < 0)) && return + ((ret < 0)) && return if [[ "$o_e" == "true" ]]; then ret=-$E_GSU_MKDIR @@ -237,7 +238,7 @@ com_prefs() (($? != 0)) && return ret=-$E_GSU_EDITOR result="${EDITOR:-vi}" - "$result" "$conf" + "$result" $conf (($? != 0)) && return ret=$GSU_SUCCESS return @@ -256,7 +257,7 @@ com_prefs() printf "# optional" ;; esac - printf "%s: %s" "$option_type" "$description" + printf " %s: %s" "$option_type" "$description" if [[ "$required" != "yes" && "$required" != "true" ]]; then printf " [%s]" "$default_value" fi @@ -294,7 +295,7 @@ Print the manual. Usage: man [-m ] [-b ] --m: Set output format (text, roff or html). Default: text. +-m: Set output format (text, roff or html). Default: roff. -b: Use the specified browser. Implies html mode. If stdout is not associated with a terminal device, the command @@ -310,9 +311,6 @@ file is displayed as a page in the web browser. If -b is not given, the command stored in the $BROWSER environment variable is executed with the path to the temporary file as an argument. If $BROWSER is unset, elinks(1) is assumed. - -It is recommended to specify the output format with -m as the default -mode might change in future versions of gsu. ' _gsu_read_line() @@ -523,7 +521,7 @@ com_man() o_m='html' browser="$o_b" elif [[ -z "$o_m" ]]; then - o_m='text' + o_m='roff' fi _gsu_isatty && isatty='true' || isatty='false' @@ -598,7 +596,7 @@ com_man() _gsu_available_commands for com in $result; do num=${#com} - (($num < 4)) && num=4 + ((num < 4)) && num=4 echo "${minus_signs:0:$num}" echo "$com" echo "${minus_signs:0:$num}" @@ -739,7 +737,12 @@ com_complete() local -a candidates; candidates=(\$($0 complete "\$COMP_CWORD" "\${COMP_WORDS[@]}")); - COMPREPLY=(\$(compgen -W "\${candidates[*]}" -- "\$cur")); + if ((\$? == 0)); then + COMPREPLY=(\$(compgen -W "\${candidates[*]}" -- "\$cur")); + else + compopt -o filenames; + COMPREPLY=(\$(compgen -fd -- "\$cur")); + fi EOF ret=$GSU_SUCCESS return @@ -747,8 +750,8 @@ EOF cword="$1" gsu_is_a_number "$cword" - (($ret < 0)) && return - if (($cword <= 1)); then + ((ret < 0)) && return + if ((cword <= 1)); then _gsu_available_commands echo "${result}" ret=$GSU_SUCCESS @@ -757,8 +760,9 @@ EOF shift words=("$@") cmd="${words[1]}" - ret=$GSU_SUCCESS # It's not an error if no completer was defined - [[ "$(type -t "complete_$cmd")" != "function" ]] && return + # if no completer is defined for this subcommand we exit unsuccessfully + # to let the generic completer above fall back to file name completion. + [[ "$(type -t "complete_$cmd")" != "function" ]] && exit 1 "complete_$cmd" "$cword" "${words[@]}" # ignore errors, they would only clutter the completion output ret=$GSU_SUCCESS @@ -780,18 +784,18 @@ gsu_cword_is_option_parameter() local -a words result= - (($cword == 0)) && return + ((cword == 0)) && return ((${#opts} < 2)) && return shift 2 words=("$@") - prev="${words[$(($cword - 1))]}" + prev="${words[$((cword - 1))]}" [[ ! "$prev" == -* ]] && return n=$((${#opts} - 1)) for ((i=0; i <= $n; i++)); do opt="${opts:$i:1}" - [[ "${opts:$(($i + 1)):1}" != ":" ]] && continue + [[ "${opts:$((i + 1)):1}" != ":" ]] && continue let i++ [[ ! "$prev" =~ ^-.*$opt$ ]] && continue result="$opt" @@ -822,13 +826,13 @@ gsu_get_unnamed_arg_num() shift 2 words=("$@") cur="${words[$cword]}" - prev="${words[$(($cword - 1))]}" + prev="${words[$((cword - 1))]}" result=-1 [[ "$cur" == -* ]] && return [[ "$prev" == -* ]] && [[ "$opts" == *${prev#-}:* ]] && return for ((i=1; i <= $cword; i++)); do - prev="${words[$(($i - 1))]}" + prev="${words[$((i - 1))]}" cur="${words[$i]}" [[ "$cur" == -* ]] && continue if [[ "$prev" == -* ]]; then @@ -838,7 +842,7 @@ gsu_get_unnamed_arg_num() fi let n++ done - result="$(($n - 1))" + result="$((n - 1))" } # Entry point for all gsu-based scripts. @@ -869,7 +873,7 @@ gsu() shift if [[ "$(type -t "com_$arg")" == 'function' ]]; then "com_$arg" "$@" - if (("$ret" < 0)); then + if ((ret < 0)); then gsu_err_msg exit 1 fi @@ -878,6 +882,6 @@ gsu() ret=-$E_GSU_BAD_COMMAND result="$arg" gsu_err_msg - _gsu_print_available_commands + _gsu_print_available_commands 1>&2 exit 1 }