subcommand: Fix some format string issues.
[gsu.git] / subcommand
index 71f06141abbdd7f0f0b81068abb2037190abec99..34553b808970e92ae29f5f2dbce060c1cc2f6ae7 100644 (file)
@@ -255,34 +255,47 @@ com_prefs()
                        printf "# optional"
                        ;;
                esac
-               printf " $option_type: $description"
+               printf "%s: %s" "$option_type" "$description"
                if [[ "$required" != "yes" && "$required" != "true" ]]; then
-                       printf " [$default_value]"
+                       printf " [%s]" "$default_value"
                fi
                echo
                [[ -n "$help_text" ]] && sed -e '/^[    ]*$/d; s/^[     ]*/#    /g' <<< "$help_text"
-               printf "$name=$val"
+               printf "%s=%s" "$name" "$val"
                [[ "$val" == "$default_value" ]] && printf " # default"
                echo
        done
 }
 
+_gsu_isatty()
+{(
+       exec 3<&1
+       stty 0<&3 &> /dev/null
+)}
+
 complete_prefs()
 {
        gsu_complete_options "$com_prefs_options" "$@"
 }
 
-_gsu_man_txt="
+_gsu_man_txt='
 Print the manual.
 
-Usage: man"
+Usage: man
+
+If stdout associated with a terminal device, output is piped to
+$PAGER. If $PAGER is unset, less(1) is assumed.
+'
 
 com_man()
 {
        local equal_signs="=================================================="
        local minus_signs="--------------------------------------------------"
-       local com num
+       local com num pager='cat'
 
+       _gsu_isatty && pager="${PAGER:-less}"
+       [[ "$pager" == 'less' ]] && export LESS=${LESS-RI}
+       {
        echo "$gsu_name (_${gsu_banner_txt}_) manual"
        echo "${equal_signs:0:${#gsu_name} + ${#gsu_banner_txt} + 16}"
        echo
@@ -310,6 +323,7 @@ com_man()
                $0 help $com
                echo
        done
+       } | $pager
        ret=$GSU_SUCCESS
 }
 
@@ -337,7 +351,7 @@ the subcommand.
 
 com_help()
 {
-       local a b ere tab='     '
+       local ere tab=' '
 
        _gsu_get_command_regex
        ere="$result"
@@ -346,11 +360,11 @@ com_help()
                gsu_short_msg "### $gsu_name -- $gsu_banner_txt ###"
                _gsu_usage 2>&1
                {
-                       printf "com_help()\n$_gsu_help_txt" | head -n 4; echo "--"
-                       printf "com_man()\n$_gsu_man_txt" | head -n 4; echo "--"
-                       printf "com_prefs()\n$_gsu_prefs_txt" | head -n 4; echo "--"
-                       printf "com_complete()\n$_gsu_complete_txt" | head -n 4; echo "--"
-                       grep -EA 2 "$ere" $0
+                       printf "com_help()\n%s" "$_gsu_help_txt" | head -n 4; echo "--"
+                       printf "com_man()\n%s" "$_gsu_man_txt" | head -n 4; echo "--"
+                       printf "com_prefs()\n%s" "$_gsu_prefs_txt" | head -n 4; echo "--"
+                       printf "com_complete()\n%s" "$_gsu_complete_txt" | head -n 4; echo "--"
+                       grep -EA 2 "$ere" "$0"
                } | grep -v -- '--' \
                        | sed -En "/$ere/"'!d
                                # remove everything but the command name