unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Samer Masterson <samer@samertm.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 8531@debbugs.gnu.org
Subject: bug#8531: 24.0.50;
Date: Sun, 15 Mar 2015 04:58:58 -0700	[thread overview]
Message-ID: <E1YX7En-0005Lc-GR@debbugs.gnu.org> (raw)
In-Reply-To: <8762q8xe3i.fsf@gmail.com>

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 7410 bytes --]

Hi,

This is the updated patch. Thanks in advance for the review.

Just to clarify, the bug is that

    $ ls Vid\éos

doesn't do the right thing in eshell (it preserves the backslash if
the character after it is non-special). This diverges from POSIX and
Bash.

New behavior:

Outside of a double quoted string, the backslash escapes the following
charater if that character is special. Otherwise it simply returns the
character afterwards (the backslash is ignored).

I expanded `eshell-parse-backslash's behavior to match Bash for quoted
strings as well. Inside a double quoted string, the backslash escapes
the following character if that character is special, otherwise the
backslash is preserved.

Best,
Samer

Patch below:
[PATCH] Fix bug#8531

* eshell/esh-arg.el (eshell-parse-argument-hook): Update comment.
(eshell-parse-backslash): Return escaped character after backslash
if it is special.  Otherwise, if the backslash is not in a quoted
string, ignore the backslash and return the character after; if
the backslash is in a quoted string, return the backslash and the
character after.  (bug#8531)
* automated/eshell.el (eshell-test/escape-nonspecial)
(eshell-test/escape-nonspecial-unicode)
(eshell-test/escape-nonspecial-quoted)
(eshell-test/escape-special-quoted): Add tests for new
`eshell-parse-backslash' behavior.  (bug#8531)
---
 lisp/ChangeLog           |  9 +++++++++
 lisp/eshell/esh-arg.el   | 49 +++++++++++++++++++++---------------------------
 test/ChangeLog           |  8 ++++++++
 test/automated/eshell.el | 31 ++++++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 28 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index a9cf1b0..209382d 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,12 @@
+2015-03-15  Samer Masterson  <samer@samertm.com>
+
+	* eshell/esh-arg.el (eshell-parse-argument-hook): Update comment.
+	(eshell-parse-backslash): Return escaped character after backslash
+	if it is special.  Otherwise, if the backslash is not in a quoted
+	string, ignore the backslash and return the character after; if
+	the backslash is in a quoted string, return the backslash and the
+	character after.  (bug#8531)
+
 2015-03-14  Michael R. Mauger  <michael@mauger.com>
 
 	* progmodes/sql.el: Version 3.5
diff --git a/lisp/eshell/esh-arg.el b/lisp/eshell/esh-arg.el
index 5c7d7ca..a5f697f 100644
--- a/lisp/eshell/esh-arg.el
+++ b/lisp/eshell/esh-arg.el
@@ -89,7 +89,7 @@ yield the values intended."
 	  (goto-char (match-end 0))
 	  (eshell-finish-arg)))))
 
-   ;; backslash before a special character means escape it
+   ;; parse backslash and the character after
    'eshell-parse-backslash
 
    ;; text beginning with ' is a literally quoted
@@ -305,34 +305,27 @@ If the character is itself a backslash, it needs no escaping."
 	  (string ?\\ char)))))
 
 (defun eshell-parse-backslash ()
-  "Parse a single backslash (\) character, which might mean escape.
-It only means escape if the character immediately following is a
-special character that is not itself a backslash."
+  "Parse a single backslash (\\) character and the character after.
+If the character after the backslash is special, always ignore
+the backslash and return the escaped character.
+
+Otherwise, if the backslash is not in quoted string, the
+backslash is ignored and the character after is returned.  If the
+backslash is in a quoted string, the backslash and the character
+after are both returned."
   (when (eq (char-after) ?\\)
-    (if (eshell-looking-at-backslash-return (point))
-	(throw 'eshell-incomplete ?\\)
-      (if (and (not (eq (char-after (1+ (point))) ?\\))
-	       (if eshell-current-quoted
-		   (memq (char-after (1+ (point)))
-			 eshell-special-chars-inside-quoting)
-		 (memq (char-after (1+ (point)))
-		       eshell-special-chars-outside-quoting)))
-	  (progn
-	    (forward-char 2)
-	    (list 'eshell-escape-arg
-		  (char-to-string (char-before))))
-	;; allow \\<RET> to mean a literal "\" character followed by a
-	;; normal return, rather than a backslash followed by a line
-	;; continuation (i.e., "\\ + \n" rather than "\ + \\n").  This
-	;; is necessary because backslashes in Eshell are not special
-	;; unless they either precede something special, or precede a
-	;; backslash that precedes something special.  (Mainly this is
-	;; done to make using backslash on Windows systems more
-	;; natural-feeling).
-	(if (eshell-looking-at-backslash-return (1+ (point)))
-	    (forward-char))
-	(forward-char)
-	"\\"))))
+    (when (eshell-looking-at-backslash-return (point))
+	(throw 'eshell-incomplete ?\\))
+    (forward-char 2) ; Move one char past the backslash.
+    ;; If the char is in a quote, backslash only has special meaning
+    ;; if it is escaping a special char.
+    (if eshell-current-quoted
+        (if (memq (char-before) eshell-special-chars-inside-quoting)
+            (list 'eshell-escape-arg (char-to-string (char-before)))
+          (concat "\\" (char-to-string (char-before))))
+      (if (memq (char-before) eshell-special-chars-outside-quoting)
+          (list 'eshell-escape-arg (char-to-string (char-before)))
+        (char-to-string (char-before))))))
 
 (defun eshell-parse-literal-quote ()
   "Parse a literally quoted string.  Nothing has special meaning!"
diff --git a/test/ChangeLog b/test/ChangeLog
index 6a474e1..ff2cd60 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,11 @@
+2015-03-15  Samer Masterson  <samer@samertm.com>
+
+	* automated/eshell.el (eshell-test/escape-nonspecial)
+	(eshell-test/escape-nonspecial-unicode)
+	(eshell-test/escape-nonspecial-quoted)
+	(eshell-test/escape-special-quoted): Add tests for new
+	`eshell-parse-backslash' behavior.  (bug#8531)
+
 2015-03-10  Jackson Ray Hamilton  <jackson@jacksonrayhamilton.com>
 
 	* indent/js-indent-init-dynamic.js: Fix spelling error.
diff --git a/test/automated/eshell.el b/test/automated/eshell.el
index d51355f..81898db 100644
--- a/test/automated/eshell.el
+++ b/test/automated/eshell.el
@@ -166,6 +166,37 @@ e.g. \"{(+ 1 2)} 3\" => 3"
    (eshell-command-result-p "+ 1 2; + $_ 4"
                              "3\n6\n")))
 
+(ert-deftest eshell-test/escape-nonspecial ()
+  "Test that \"\\c\" and \"c\" are equivalent when \"c\" is not a
+special character."
+  (with-temp-eshell
+   (eshell-command-result-p "echo he\\llo"
+                            "hello\n")))
+
+(ert-deftest eshell-test/escape-nonspecial-unicode ()
+  "Test that \"\\c\" and \"c\" are equivalent when \"c\" is a
+unicode character (unicode characters are nonspecial by
+definition)."
+  (with-temp-eshell
+   (eshell-command-result-p "echo Vid\\éos"
+                            "Vidéos\n")))
+
+(ert-deftest eshell-test/escape-nonspecial-quoted ()
+  "Test that the backslash is preserved for escaped nonspecial
+chars"
+  (with-temp-eshell
+   (eshell-command-result-p "echo \"h\\i\""
+                            ;; Backslashes are doubled for regexp.
+                            "h\\\\i\n")))
+
+(ert-deftest eshell-test/escape-special-quoted ()
+  "Test that the backslash is not preserved for escaped special
+chars"
+  (with-temp-eshell
+   (eshell-command-result-p "echo \"h\\\\i\""
+                            ;; Backslashes are doubled for regexp.
+                            "h\\\\i\n")))
+
 (ert-deftest eshell-test/command-running-p ()
   "Modeline should show no command running"
   (with-temp-eshell





  parent reply	other threads:[~2015-03-15 11:58 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-21  7:23 bug#8531: 24.0.50; shell-quote-argument shouldn't escape special characters Thierry Volpiatto
2011-04-21 10:33 ` Eli Zaretskii
2011-04-21 12:10   ` Thierry Volpiatto
2011-04-21 13:04     ` Eli Zaretskii
2011-04-21 13:31       ` Thierry Volpiatto
2011-04-21 13:58         ` Eli Zaretskii
2011-04-21 16:34           ` Thierry Volpiatto
2011-04-21 23:14             ` Glenn Morris
2011-04-22  5:53               ` Eli Zaretskii
2011-04-22  7:10                 ` Glenn Morris
2011-04-22  8:03                   ` Eli Zaretskii
2011-04-22  6:03               ` Thierry Volpiatto
2011-04-22  6:15                 ` Eli Zaretskii
2011-04-22  9:21                   ` Thierry Volpiatto
2014-12-08  9:34 ` bug#8531: 24.0.50; samer
2014-12-08 18:46   ` Stefan Monnier
2014-12-09  0:15     ` samer
2014-12-09  0:41       ` Lars Magne Ingebrigtsen
2014-12-09  2:11         ` samer
2014-12-09  2:14           ` Lars Magne Ingebrigtsen
2014-12-09 22:11           ` samer
2014-12-09 22:21             ` Lars Magne Ingebrigtsen
2014-12-09 22:30               ` samer
2014-12-08 16:48 ` bug#8531: 24.0.50; shell-quote-argument shouldn't escape special characters samer
2015-02-24 10:51 ` bug#8531: 24.0.50; Samer Masterson
2015-03-03 15:52   ` Eli Zaretskii
     [not found]     ` <1425472710.1450.3@mail.samertm.com>
     [not found]       ` <83h9u0psn8.fsf@gnu.org>
2015-04-06  3:50         ` Samer Masterson
2015-03-15 11:58 ` Samer Masterson [this message]
2015-04-09  2:32   ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=E1YX7En-0005Lc-GR@debbugs.gnu.org \
    --to=samer@samertm.com \
    --cc=8531@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).