unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: Fixing Windows and DOS command line argument quoting
@ 2011-04-27  0:58 Ben Key
  2011-04-27  1:25 ` Daniel Colascione
  0 siblings, 1 reply; 15+ messages in thread
From: Ben Key @ 2011-04-27  0:58 UTC (permalink / raw
  To: Emacs-devel

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

Daniel Colascione writes:

> After another reading of cmdproxy.c, I also see that the
> CreateProcess path also doesn't expand environment %variable%
> references, and that doesn't fall back to cmd if the command
> to be executed contains them.  While we could expand these
> variables, doing so would move us even closer to
> reimplementing half of cmd.exe.

You are exaggerating a great deal.  It is a single function call,
ExpandEnvironmentStrings, documented at
http://msdn.microsoft.com/en-us/library/ms724265%28v=vs.85%29.aspx.
Patching cmdproxy.c to use ExpandEnvironmentStrings before calling
CreateProcess would add at most 10 lines of code.  This is by no means
"reimplementing half of cmd.exe."

[-- Attachment #2: Type: text/html, Size: 868 bytes --]

^ permalink raw reply	[flat|nested] 15+ messages in thread
* Fixing Windows and DOS command line argument quoting
@ 2011-04-25  2:09 Daniel Colascione
  2011-04-25  6:41 ` Eli Zaretskii
  0 siblings, 1 reply; 15+ messages in thread
From: Daniel Colascione @ 2011-04-25  2:09 UTC (permalink / raw
  To: Emacs development discussions


[-- 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 --]

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2011-04-27  1:25 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-04-27  0:58 Fixing Windows and DOS command line argument quoting Ben Key
2011-04-27  1:25 ` Daniel Colascione
  -- strict thread matches above, loose matches on Subject: below --
2011-04-25  2:09 Daniel Colascione
2011-04-25  6:41 ` 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

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).