Blob Blame History Raw
From 92a4443a0f9645a6147ded3d3d799eb264c4af38 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= <jklimes@redhat.com>
Date: Mon, 14 Apr 2014 13:00:35 +0200
Subject: [PATCH 1/1] cli/bash-completion: escape spaces in profile names (rh
 #1041901) (bgo #709426)

We need to escape spaces and quotes in connection names, so that a connection
name containing spaces (quotes) is regarded as a single argument by bash.
See e.g. http://stackoverflow.com/questions/1146098/properly-handling-spaces-and-quotes-in-bash-completion

https://bugzilla.redhat.com/show_bug.cgi?id=1041901
https://bugzilla.gnome.org/show_bug.cgi?id=709426
(cherry picked from commit b911d663d8a9df739a9311efe99c21af362c5738)
---
 cli/completion/nmcli | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/cli/completion/nmcli b/cli/completion/nmcli
index 9ed1c03..e5a1555 100644
--- a/cli/completion/nmcli
+++ b/cli/completion/nmcli
@@ -11,6 +11,36 @@ _nmcli_list_nl()
 {
     local IFS=$'\n'
     COMPREPLY=( $( compgen -W '$1' -- $cur ) )
+
+    # Now escape special characters (spaces, single and double quotes),
+    # so that the argument is really regarded a single argument by bash.
+    # See http://stackoverflow.com/questions/1146098/properly-handling-spaces-and-quotes-in-bash-completion
+    local escaped_single_quote="'\''"
+    local i=0
+    local entry
+    for entry in ${COMPREPLY[*]}
+    do
+        if [[ "${cur:0:1}" == "'" ]]; then
+            # started with single quote, escaping only other single quotes
+            # [']bla'bla"bla\bla bla --> [']bla'\''bla"bla\bla bla
+            COMPREPLY[$i]="${entry//\'/${escaped_single_quote}}"
+        elif [[ "${cur:0:1}" == '"' ]]; then
+            # started with double quote, escaping all double quotes and all backslashes
+            # ["]bla'bla"bla\bla bla --> ["]bla'bla\"bla\\bla bla
+            entry="${entry//\\/\\\\}"
+            entry="${entry//\"/\\\"}"
+            COMPREPLY[$i]="$entry"
+        else
+            # no quotes in front, escaping _everything_
+            # [ ]bla'bla"bla\bla bla --> [ ]bla\'bla\"bla\\bla\ bla
+            entry="${entry//\\/\\\\}"
+            entry="${entry//\'/\'}"
+            entry="${entry//\"/\\\"}"
+            entry="${entry// /\\ }"
+            COMPREPLY[$i]="$entry"
+        fi
+        (( i++ ))
+    done
 }
 
 _nmcli_con_id()
-- 
1.9.3