From patchwork Fri Feb 7 05:23:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ronan Pigott X-Patchwork-Id: 863927 Received: from out-187.mta0.migadu.com (out-187.mta0.migadu.com [91.218.175.187]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B131353A7 for ; Fri, 7 Feb 2025 05:24:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.187 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738905854; cv=none; b=eDET8S4OTQJgwBVBcTx8GKeSekeCAYnETYuDwhfTov3SSbHrbukmdSV+a2XHlWzXEu+pd+LIVxOn+dxrBbB2bphpCsPSkZDBaxb35Xt6UuDY8DITU3uU6IILP7PnRNMjyGj1JQSvTtUZLgg+9CkmrFtvOqO5eWspUhqTQgrYsTo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1738905854; c=relaxed/simple; bh=j0cRb5gWfdv8QZDuATQPHfkRFREKlmUjakaMO/q50Dw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=rf3MmJEI7F2djAgJRyQqlsZlsgZucCjYv63wDNx3IG2dTXs9pM/tJyblEXs3/dkERIRTTIbvSV90DRa1q4i6p61b93VtTWVwK8c9UrZuR+em5zeo2GtIX85V48xNKo2OeN2nrggejWmnZIF70OMctWVd9WVydkSwiOwI7zOTqZo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rjp.ie; spf=pass smtp.mailfrom=rjp.ie; dkim=pass (1024-bit key) header.d=rjp.ie header.i=@rjp.ie header.b=Lalm9s+f; arc=none smtp.client-ip=91.218.175.187 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=rjp.ie Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rjp.ie Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=rjp.ie header.i=@rjp.ie header.b="Lalm9s+f" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rjp.ie; s=key1; t=1738905843; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=5OTGBem+ySKepEObWZRsIR8pVwo/Xq3qwsKuWqxPnfw=; b=Lalm9s+flVNkr3/Tg2g+L1+7ubfqW1nEMCgiNd0Zxh9xy6dy5yyvD9NoJ6DBFPlE+Orjc2 pdzWjVCXCMC312bTycU04uOvgPS8UicCSj9B0xtWU+RxllQ6f98DGNaHdGsLnk7gALeAn3 ZYMX6mHwUMlpnyVM8KCjA6rQHILv1VA= From: Ronan Pigott To: linux-bluetooth@vger.kernel.org Cc: Ronan Pigott Subject: [PATCH BlueZ] zsh: use the correct completion return value Date: Thu, 6 Feb 2025 22:23:22 -0700 Message-ID: <20250207052345.63368-1-ronan@rjp.ie> Precedence: bulk X-Mailing-List: linux-bluetooth@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT The return value of a completion function is significant, if we fail to return success additional completers may be invoked when they otherwise should not be. Also cleanup up the zsh completion, removing the redundant definition of _bluetoothctl and using the _call_program helper where appropriate. --- completion/zsh/_bluetoothctl | 127 ++++++++++++++++------------------- 1 file changed, 58 insertions(+), 69 deletions(-) diff --git a/completion/zsh/_bluetoothctl b/completion/zsh/_bluetoothctl index 610ca2b8d59c..734036e596c1 100644 --- a/completion/zsh/_bluetoothctl +++ b/completion/zsh/_bluetoothctl @@ -1,97 +1,86 @@ #compdef bluetoothctl -__bluetoothctl() { - bluetoothctl "$@" 2>/dev/null -} - _bluezcomp_controller() { local -a controllers - bluetoothctl list | + _call_program bluez-controller bluetoothctl list | while read _ MAC NAME; do - controllers+="${MAC//:/\\:}:${NAME//:/\\:}" + controllers+=("${MAC//:/\\:}:${NAME//:/\\:}") done _describe -t controllers 'controller' controllers } _bluezcomp_device() { local -a devices - bluetoothctl devices | + _call_program bluez-device bluetoothctl devices | while read _ MAC NAME; do - devices+="${MAC//:/\\:}:${NAME//:/\\:}" + devices+=("${MAC//:/\\:}:${NAME//:/\\:}") done _describe -t devices 'device' devices } _bluezcomp_agentcap() { - local -a agent_options=(${(f)"$(__bluetoothctl agent help)"}) - agent_options=( "${(@)agent_options:#(on|off)}" ) - compadd -a agent_options + local -a agent_options=(${${(@f)"$(_call_program bluez-agent bluetoothctl agent help)"}:#(on|off)}) + compadd "$@" - -a agent_options } _bluetoothctl_agent() { - local -a agent_options=(${(f)"$(__bluetoothctl agent help)"}) - agent_options+=help - compadd -a agent_options + local -a agent_options=(help ${(@f)"$(_call_program bluez-agent bluetoothctl agent help)"}) + compadd "$@" - -a agent_options } -_bluetoothctl_advertise() { - local -a ad_options=(${(f)"$(__bluetoothctl advertise help)"}) - ad_options+=help - compadd -a ad_options -} +local -a toggle_commands=( + "discoverable" "pairable" "power" "scan" +) -_bluetoothctl() { - local -a toggle_commands=( - "discoverable" "pairable" "power" "scan" - ) +local -a controller_commands=( + "select" "show" +) - local -a controller_commands=( - "select" "show" - ) +local -a device_commands=( + "block" "connect" "disconnect" "info" + "pair" "remove" "trust" "unblock" "untrust" +) - local -a device_commands=( - "block" "connect" "disconnect" "info" - "pair" "remove" "trust" "unblock" "untrust" - ) +# Other commands may be handled by _bluetoothctl_$command +local -a all_commands=( "${(@f)$(_call_program bluetoothctl bluetoothctl --zsh-complete help)}" ) - # Other commands may be handled by _bluetoothctl_$command - local -a all_commands=( "${(@f)$(__bluetoothctl --zsh-complete help)}" ) +local curcontext=$curcontext state line ret=1 +_arguments -C \ + + '(info)' \ + {-h,--help}'[Show help message and exit]' \ + {-v,--version}'--version[Show version info and exit]' \ + + 'mod' \ + '(info)'{-a+,--agent=}'[Register agent handler]:agent:_bluezcomp_agentcap' \ + '(info)'{-t,--timeout}'[Timeout in seconds for non-interactive mode]' \ + '(info)'{-m,--monitor}'[Enable monitor output]' \ + + 'command' \ + '(info):command:->command' \ + '(info):: :->argument' - local curcontext=$curcontext state line ret=1 - _arguments -C \ - + '(info)' \ - {-h,--help}'[Show help message and exit]' \ - {-v,--version}'--version[Show version info and exit]' \ - + 'mod' \ - '(info)'{-a+,--agent=}'[Register agent handler]:agent:_bluezcomp_agentcap' \ - '(info)'{-t,--timeout}'[Timeout in seconds for non-interactive mode]' \ - '(info)'{-m,--monitor}'[Enable monitor output]' \ - + 'command' \ - '(info):command:->command' \ - '(info):: :->argument' - - if [[ $state == "command" ]]; then - _describe -t commands 'command' all_commands - elif [[ $state == "argument" ]]; then - if (( ! ${"${(@)all_commands%%:*}"[(I)${line[1]}]} )); then - _message "Unknown bluetoothctl command: $line[1]" - return 1; - fi - - curcontext="${curcontext%:*:*}:bluetoothctl-$line[1]:" - if ! _call_function ret _bluetoothctl_$line[1]; then - case $line[1] in - (${(~j.|.)toggle_commands}) - compadd on off - ;; - (${(~j.|.)device_commands}) - _bluezcomp_device - ;; - (${(~j.|.)controller_commands}) - _bluezcomp_controller - ;; - esac - fi - return ret +if [[ $state == "command" ]]; then + _describe -t commands 'command' all_commands +elif [[ $state == "argument" ]]; then + if (( ! ${"${(@)all_commands%%:*}"[(I)${line[1]}]} )); then + _message "Unknown bluetoothctl command: $line[1]" + return 1; fi -} && _bluetoothctl + + curcontext="${curcontext%:*:*}:bluetoothctl-$line[1]:" + if ! _call_function ret _bluetoothctl_$line[1]; then + case $line[1] in + (advertise) + compadd - help on off type && ret=0 + ;; + (${(~j.|.)toggle_commands}) + compadd on off && ret=0 + ;; + (${(~j.|.)device_commands}) + _bluezcomp_device && ret=0 + ;; + (${(~j.|.)controller_commands}) + _bluezcomp_controller && ret=0 + ;; + esac + fi + return ret +fi