unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Daniel Colascione <dan.colascione@gmail.com>
To: Emacs development discussions <emacs-devel@gnu.org>
Subject: Fixing Windows and DOS command line argument quoting
Date: Sun, 24 Apr 2011 19:09:31 -0700	[thread overview]
Message-ID: <4DB4D7DB.50101@gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 398 bytes --]

As I explain in [1], many Windows programs get command line argument
quoting wrong.  Emacs is one of these programs.  The attached patch
resolves the issue, and if there are no objections, I'll apply it to
both the Emacs 23 branch and the trunk in the next few days.

[1]
http://blogs.msdn.com/b/twistylittlepassagesallalike/archive/2011/04/23/everyone-quotes-arguments-the-wrong-way.aspx


[-- Attachment #1.2: quote-fix.patch --]
[-- Type: text/plain, Size: 2020 bytes --]

=== modified file 'lisp/subr.el'
--- lisp/subr.el	2011-04-19 13:44:55 +0000
+++ lisp/subr.el	2011-04-25 02:05:07 +0000
@@ -2507,19 +2507,33 @@
   "Quote ARGUMENT for passing as argument to an inferior shell."
   (if (or (eq system-type 'ms-dos)
           (and (eq system-type 'windows-nt) (w32-shell-dos-semantics)))
-      ;; Quote using double quotes, but escape any existing quotes in
-      ;; the argument with backslashes.
-      (let ((result "")
-	    (start 0)
-	    end)
-	(if (or (null (string-match "[^\"]" argument))
-		(< (match-end 0) (length argument)))
-	    (while (string-match "[\"]" argument start)
-	      (setq end (match-beginning 0)
-		    result (concat result (substring argument start end)
-				   "\\" (substring argument end (1+ end)))
-		    start (1+ end))))
-	(concat "\"" result (substring argument start) "\""))
+      
+      ;; First, quote argument so that CommandLineToArgvW will
+      ;; understand it.  Inside an argument, 2n or 2n + 1 backslashes
+      ;; followed by a double quote produces n backslashes.  In the
+      ;; former case, we also ends the argument.  In the latter case,
+      ;; we also produce a double-quote character and end the
+      ;; argument.  Backslashes not followed by a quotation mark are
+      ;; left alone. See
+      ;; http://msdn.microsoft.com/en-us/library/17w5ykft%28v=vs.85%29.aspx
+      ;; After we perform that level of quoting, replace shell
+      ;; metacharacters so that cmd won't mangle our argument.
+
+      (replace-regexp-in-string
+       "\\([()\"<>&|^]\\)"
+       "^\\1"
+       (concat
+        "\""
+        ;; escape backslashes at end of string
+        (replace-regexp-in-string
+         "\\(\\\\*\\)$"
+         "\\1\\1"
+         ;; escape backslashes and quotes in string body
+         (replace-regexp-in-string
+          "\\(\\\\*\\)\""
+          "\\1\\1\\\\\""
+          argument))
+        "\""))
     (if (equal argument "")
         "''"
       ;; Quote everything except POSIX filename characters.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

             reply	other threads:[~2011-04-25  2:09 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-04-25  2:09 Daniel Colascione [this message]
2011-04-25  6:41 ` Fixing Windows and DOS command line argument quoting Eli Zaretskii
2011-04-25  8:49   ` Daniel Colascione
2011-04-25  8:58     ` Jason Rumney
2011-04-25  9:15       ` Eli Zaretskii
2011-04-25  9:22       ` Daniel Colascione
2011-04-25 17:56         ` Daniel Colascione
2011-04-25 18:47           ` Eli Zaretskii
2011-04-26 10:44             ` Daniel Colascione
2011-04-26 17:50               ` Eli Zaretskii
2011-04-25  9:35     ` Eli Zaretskii
2011-04-25 18:24     ` Daniel Colascione
2011-04-25 18:48       ` Eli Zaretskii
  -- strict thread matches above, loose matches on Subject: below --
2011-04-27  0:58 Ben Key
2011-04-27  1:25 ` Daniel Colascione

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=4DB4D7DB.50101@gmail.com \
    --to=dan.colascione@gmail.com \
    --cc=emacs-devel@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).