all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Noam Postavsky <npostavs@users.sourceforge.net>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 19350@debbugs.gnu.org, Demi Obenour <demiobenour@gmail.com>
Subject: bug#19350: #19350 24.4; Incorrect quoting of %-signs for Windows command shell
Date: Mon, 3 Oct 2016 14:47:37 -0400	[thread overview]
Message-ID: <CAM-tV-9q6R32UcQ2na616F+dX3KfJ7BfR3ZGTzkUCLnO5wJQ9w@mail.gmail.com> (raw)
In-Reply-To: <83twemf550.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 1354 bytes --]

On Mon, Aug 15, 2016 at 11:01 AM, Eli Zaretskii <eliz@gnu.org> wrote:
> From my POV, as long as Emacs itself doesn't produce such shell
> commands and/or environment variables for any of its features,
> avoiding this becomes user's responsibility, just like when working at
> the shell prompt.  Of course, if we can find a reliable solution to
> the problem that doesn't take away features, that'd be better,

So I have been thinking about this. As far as I can tell, the %CD:~,0%
hack is the only way to solve it. We can make it conditional on having
command extensions enabled, see attached.

The probing command I used for the default value for
w32-shell-command-extensions causes an error while dumping ("Searching
for program: No such file or directory,
C:/Users/npostavs/src/emacs/emacs-25/nt/cmdproxy.exe") so I've
commented it out for now. With this patch,

(let ((w32-shell-command-extensions t)
      (process-environment (cl-list* "ca^=with-caret"
                                     "ca=without-caret"
                                     process-environment)))
  (insert (shell-command-to-string
           (format "echo %s %s %s"
                   "%ca%"
                   (shell-quote-argument "%ca%")
                   "%%CD:~,0%ca%%CD:~,0%"))))

gives

without-caret "%ca%" %ca%

If this approach makes sense I can fix up the patch.

[-- Attachment #2: escaping-percent.diff --]
[-- Type: text/plain, Size: 2091 bytes --]

diff --git i/lisp/subr.el w/lisp/subr.el
index e9e19d3..1d7d0dd 100644
--- i/lisp/subr.el
+++ w/lisp/subr.el
@@ -2753,15 +2753,31 @@ shell-quote-argument
             "\\1\\1\\\\\""
             argument)))
 
-    (if (string-match "[%!\"]" argument)
-        (concat
-         "^\""
-         (replace-regexp-in-string
-          "\\([%!()\"<>&|^]\\)"
-          "^\\1"
-          argument)
-         "^\"")
-      (concat "\"" argument "\"")))
+    ;; cmd.exe performs %var% expansion before anything else, so there
+    ;; is no way escape '%'s (escaping with '^' *appears* to work, but
+    ;; that's only because variables with carets in the name are
+    ;; rare).  If "Command Extensions" are enabled, it's possible to
+    ;; work around this limitation by appending "%CD:~,0%" to each
+    ;; '%'; it expands to the empty string and stops the preceding '%'
+    ;; from being interpreted as the beginning of a variable name.
+    (cond
+     ((and w32-shell-command-extensions (string-match "%" argument))
+      (concat
+       "^\""
+       (replace-regexp-in-string
+        "\\([!()\"<>&|^]\\)"
+        "^\\1"
+        (replace-regexp-in-string "%" "%%CD:~,0%" argument t t))
+       "^\""))
+     ((string-match "[%!\"]" argument)
+      (concat
+       "^\""
+       (replace-regexp-in-string
+        "\\([%!()\"<>&|^]\\)"
+        "^\\1"
+        argument)
+       "^\""))
+     (t (concat "\"" argument "\""))))
 
    (t
     (if (equal argument "")
diff --git i/lisp/w32-vars.el w/lisp/w32-vars.el
index c9317e4..3e76a86 100644
--- i/lisp/w32-vars.el
+++ w/lisp/w32-vars.el
@@ -65,6 +65,13 @@ w32-list-proportional-fonts
    :type '(repeat string)
    :group 'w32))
 
+(unless (eq system-type 'cygwin)
+  (defcustom w32-shell-command-extensions nil ;; (equal (shell-command-to-string "echo %%CD:~,0%") "%\n")
+    "If non-nil, assume the shell supports \"Command Extensions\".
+This is used by `shell-quote-argument' to safely escape \"%\"."
+    :type 'boolean
+    :group 'w32))
+
 ;; Want "menu" custom type for this.
 (defcustom w32-fixed-font-alist
   '("Font menu"

  parent reply	other threads:[~2016-10-03 18:47 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-12-11 18:43 bug#19350: 24.4; Incorrect quoting of %-signs for Windows command shell Demetrios Obenour
2016-08-10 13:00 ` bug#19350: #19350 " Noam Postavsky
2016-08-10 16:11   ` Eli Zaretskii
2016-08-11 23:11   ` Demi Obenour
2016-08-12  0:41     ` npostavs
     [not found]       ` <CAJEMUN_nkRgwCF7h03x=_jdHDLGqC+DBk0=3NNJhZTx5woW8gg@mail.gmail.com>
     [not found]         ` <CAJEMUN-HMMJu1FTDrKEmn_8nOaMhDeE8Te2JuE7jVdvuqLtbNQ@mail.gmail.com>
2016-08-15  0:44           ` Demi Obenour
2016-08-15  3:13             ` npostavs
2016-08-15 15:01               ` Eli Zaretskii
2016-08-18  2:02                 ` Demi Obenour
2016-08-18  2:21                   ` Noam Postavsky
2016-08-18 11:54                     ` Demi Obenour
2016-08-18 12:07                   ` Noam Postavsky
2016-08-18 14:17                     ` Eli Zaretskii
2016-10-03 18:47                 ` Noam Postavsky [this message]
2016-10-03 19:15                   ` Eli Zaretskii
     [not found]                     ` <CAJEMUN9xcizd9rNYiZueg4gjRAcPxh4V_JZdaSt5jr-P4LRe-A@mail.gmail.com>
2016-10-03 19:38                       ` Demi Obenour
2018-02-09  0:17                         ` Noam Postavsky

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

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

  git send-email \
    --in-reply-to=CAM-tV-9q6R32UcQ2na616F+dX3KfJ7BfR3ZGTzkUCLnO5wJQ9w@mail.gmail.com \
    --to=npostavs@users.sourceforge.net \
    --cc=19350@debbugs.gnu.org \
    --cc=demiobenour@gmail.com \
    --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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.