From 53b0bb0782763dc8d535365d4acfaaa1ac2a897e Mon Sep 17 00:00:00 2001
From: Mark Olesen <Mark.Olesen@esi-group.com>
Date: Tue, 18 Sep 2018 18:25:29 +0200
Subject: [PATCH] CONFIG: fix odd behaviour in bash completion (issue #1011)

- The test condition

      [ -n "$cur" -a ... ]

  fails if $cur starts with '-le', which bash interprets as a further
  test op. Splitting the test condition solves the problem:

      [ -n "$cur" ] && [ ... ]
---
 etc/config.sh/bash_completion | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/etc/config.sh/bash_completion b/etc/config.sh/bash_completion
index afa2f7c60a6..d2ea0b0315f 100644
--- a/etc/config.sh/bash_completion
+++ b/etc/config.sh/bash_completion
@@ -109,7 +109,7 @@ _of_complete_()
     local choices
 
     case ${prev} in
-    -help | -help-full | -doc | -doc-source)
+    -help | -help-compat | -help-full | -doc | -doc-source)
         # These options are usage - we can stop now.
         COMPREPLY=()
         return 0
@@ -139,10 +139,13 @@ _of_complete_()
             # Treat ',' like space so '-a, -all' parses like '-a | -all'
             # Options with '=' (Eg, -mode=ugo) are not handled very well.
             local helpText=$($appName -help-full 2>/dev/null | \
-                sed -n -e 's/,/ /g' -e 's/=.*$/=/' -e '/^  *-/p')
+                sed -ne 's/,/ /g' -e 's/=.*$/=/' -e '/^  *-/p')
 
-            if [ -n "$helpText" ]
+            if [ -z "$helpText" ]
             then
+                echo "Error calling $appName" 1>&2
+                choices="false"  # Mark failure to prevent repeating again
+            else
                 # Array of options with args
                 local argOpts=($(awk '/^ {0,4}-[a-z]/ && /</ {print $1}' <<< "$helpText"))
 
@@ -150,9 +153,6 @@ _of_complete_()
                 local boolOpts=($(awk '/^ {0,4}-[a-z]/ && !/</ {print $1}' <<< "$helpText"))
 
                 choices="${argOpts[@]} | ${boolOpts[@]}"
-            else
-                echo "Error calling $appName" 1>&2
-                choices="false"  # Mark failure to prevent repeating again
             fi
             _of_complete_cache_[$appName]="$choices"
             ## echo "generated $appName = $choices" 1>&2   # Debugging
@@ -169,7 +169,7 @@ _of_complete_()
                 # Option with unknown type of arg - set to files.
                 # Not always correct but can still navigate path if needed...
                 COMPREPLY=($(compgen -f -- ${cur}))
-            elif [ -n "$cur" -a "${cur#-}" = "${cur}" ]
+            elif [ -n "$cur" ] && [ "${cur#-}" = "${cur}" ]
             then
                 # Already started a (non-empty) word that isn't an option,
                 # in which case revert to filenames.
-- 
GitLab