From d163abf993d1fa85d8172e3a4b698ba90f2a113e Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Mon, 24 Sep 2012 15:03:53 +0200
Subject: [PATCH] bash_completion: Enable extglob while parsing file
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

In older versions of GNU Bash extended patterns, such as β€œ@(…)”, are only
available with the β€œextglob” shell option. As pointed out in [1] and [2],
β€œextglob” must be enabled while parsing the code. Therefore the flag must be
enabled at the beginning of the script and be reset to its original value at
the end as to not interfere with other code on shell initialization.

[1] http://unix.stackexchange.com/questions/45957
[2] http://mywiki.wooledge.org/glob

Reported by Sascha Lucas.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Bernardo Dal Seno <bdalseno@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
(cherry picked from commit 893ad76d18e1a8286247e06c8451bbd4831cff8f)
---
 autotools/build-bash-completion | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/autotools/build-bash-completion b/autotools/build-bash-completion
index 365dad98a..0cae30158 100755
--- a/autotools/build-bash-completion
+++ b/autotools/build-bash-completion
@@ -622,6 +622,11 @@ def main():
   buf = StringIO()
   sw = utils.ShellWriter(buf)
 
+  # Remember original state of extglob and enable it (required for pattern
+  # matching; must be enabled while parsing script)
+  sw.Write("gnt_shopt_extglob=$(shopt -p extglob || :)")
+  sw.Write("shopt -s extglob")
+
   WritePreamble(sw)
 
   # gnt-* scripts
@@ -638,6 +643,10 @@ def main():
   WriteCompletion(sw, "%s/burnin" % constants.TOOLSDIR, "_ganeti_burnin",
                   opts=burnin.OPTIONS, args=burnin.ARGUMENTS)
 
+  # Reset extglob to original value
+  sw.Write("[[ -n \"$gnt_shopt_extglob\" ]] && $gnt_shopt_extglob")
+  sw.Write("unset gnt_shopt_extglob")
+
   print buf.getvalue()
 
 
-- 
GitLab