completion: Rework quoting in gsettings completion script

This is a partial revert and rework of commit
c79575362eeb8f8d24cd2c141928d7d8f22eeb94, for the `gsettings` script
only (the other completion scripts are fine).

I blindly added quoting to everything shellcheck told me to, without
testing it properly.

As it turns out, the `$schemadir` argument to `gsettings` invocations
was deliberately not quoted, so that it would expand to zero arguments
if unset, and two arguments (`--schemadir /some/path`) if set earlier in
the command-being-completed.

Quoting it meant that it expanded to one argument (the empty string) if
unset, which caused the `gsettings` subcommands to fail, and hence any
further tab completion to fail.

Fix that as suggested on https://www.shellcheck.net/wiki/SC2086 by
turning `schemadir` into an array, which either has zero members if
unset, or two members if set.

Signed-off-by: Philip Withnall <pwithnall@gnome.org>
This commit is contained in:
Philip Withnall 2024-04-17 17:40:55 +01:00
parent 295a6fb965
commit 70c0f3bff6
No known key found for this signature in database
GPG Key ID: DCDF5885B1F3ED73

View File

@ -26,13 +26,16 @@
####################################################################################################
__gsettings() {
local choices coffset schemadir="" gsettings_binary
local choices coffset schemadir=() gsettings_binary
gsettings_binary="$1"
if [ "${COMP_CWORD}" -gt 2 ]; then
if [ "${COMP_WORDS[1]}" = --schemadir ]; then
# this complexity is needed to perform correct tilde expansion
schemadir=$(eval "echo --schemadir ${COMP_WORDS[2]}")
# The eval complexity is needed to perform correct tilde expansion
# The array is needed to ensure we can keep it expanding to either zero
# or one option in the commands below, while keeping quoting correct.
# See https://www.shellcheck.net/wiki/SC2086
schemadir=("--schemadir" "$(eval "echo ${COMP_WORDS[2]}")")
coffset=2
else
coffset=0
@ -58,7 +61,7 @@ __gsettings() {
choices=$'list-schemas\nlist-relocatable-schemas\nlist-keys\nlist-children\nlist-recursively\nget\nrange\nset\nreset\nreset-recursively\nwritable\nmonitor'
;;
list-keys|list-children|list-recursively|reset-recursively)
choices="$("$gsettings_binary" "$schemadir" list-schemas 2> /dev/null)"$'\n'"$("$gsettings_binary" "$schemadir" list-relocatable-schemas 2> /dev/null | sed -e 's.$.:/.')"
choices="$("$gsettings_binary" "${schemadir[@]}" list-schemas 2> /dev/null)"$'\n'"$("$gsettings_binary" "${schemadir[@]}" list-relocatable-schemas 2> /dev/null | sed -e 's.$.:/.')"
;;
list-schemas)
# shellcheck disable=SC2207
@ -67,7 +70,7 @@ __gsettings() {
;;
get|range|set|reset|writable|monitor|describe)
choices="$("$gsettings_binary" "$schemadir" list-schemas 2> /dev/null | sed -e 's.$. .')"$'\n'"$("$gsettings_binary" "$schemadir" list-relocatable-schemas 2> /dev/null | sed -e 's.$.:/.')"
choices="$("$gsettings_binary" "${schemadir[@]}" list-schemas 2> /dev/null | sed -e 's.$. .')"$'\n'"$("$gsettings_binary" "${schemadir[@]}" list-relocatable-schemas 2> /dev/null | sed -e 's.$.:/.')"
;;
esac
;;
@ -75,11 +78,11 @@ __gsettings() {
3)
case "${COMP_WORDS[$((coffset+1))]}" in
set)
choices="$("$gsettings_binary" "$schemadir" list-keys "${COMP_WORDS[$((coffset+2))]}" 2> /dev/null | sed -e 's.$. .')"
choices="$("$gsettings_binary" "${schemadir[@]}" list-keys "${COMP_WORDS[$((coffset+2))]}" 2> /dev/null | sed -e 's.$. .')"
;;
get|range|reset|writable|monitor|describe)
choices="$("$gsettings_binary" "$schemadir" list-keys "${COMP_WORDS[$((coffset+2))]}" 2> /dev/null)"
choices="$("$gsettings_binary" "${schemadir[@]}" list-keys "${COMP_WORDS[$((coffset+2))]}" 2> /dev/null)"
;;
esac
;;
@ -88,7 +91,7 @@ __gsettings() {
case "${COMP_WORDS[$((coffset+2))]}" in
set)
# shellcheck disable=SC2207
range=($("$gsettings_binary" "$schemadir" range "${COMP_WORDS[$((coffset+2))]}" "${COMP_WORDS[$((coffset+3))]}" 2> /dev/null))
range=($("$gsettings_binary" "${schemadir[@]}" range "${COMP_WORDS[$((coffset+2))]}" "${COMP_WORDS[$((coffset+3))]}" 2> /dev/null))
case "${range[0]}" in
enum)
unset 'range[0]'