Subject: bug#7711: Patch for bug #7687

From: Flo <[email-masked]>
Date: Wed, 22 Dec 2010 20:00:51 +0100

What problem is solved by this patch
===========================================

GNU bug report #7687
23.1.50; shell-quote-argument does not correctly quote newlines

A copy of that bug report is at the end of this Email

Why does the patch solve the probem
===================================

Before shell-quote-argument quoted everything except POSIX
filename characters with a leading backslash. The result of
shell-quote-argument was that a new line in the input was
'quoted' with a backslash. A backslashed followed by a
newline however means line continuation to POSIX shells. It
is discarded by the shell. So the result after the shell was
the empty string. The new version of shell-quote-argument
treats newlines specially. They are quoted with single
quotes.

Same information again put differently:

old version:

input to shell-quote-argument: a newline

output of shell-quote-argument = input to the shell:
backslash followed by a newline

result of shell's quoting mechanism: the emtpy string (due
to line continuation)

new version:

input to shell-quote-argument: a newline

output of shell-quote-argument = input to the shell: newline
in single quotes

result of shell's quoting mechanism: a newline

Change Log
===========
subr.el does not yet have a change log, so I put it here

shell-quote-argument now correctly quotes newlines for POSIX
shells

Effect on other systems
========================

shell-quote-argument already distinguishes between different
system types using system-type. I only changed the part that
is concerned about POSIX shells.

diff: old version (23.1.50) first, new version second
=====================================================

$ gunzip /usr/share/emacs/23.1.50/lisp/subr.el.gz -c | diff -c -
/home/emp8118035/.emacs.d/patches/subr.el
*** STDIN Wed Dec 22 19:27:58 2010
--- /home/emp8118035/.emacs.d/patches/subr.el Mon Dec 20 17:28:30 2010
***************
*** 2391,2403 ****
  	(concat "\"" result (substring argument start) "\""))
      (if (equal argument "")
          "''"
!       ;; Quote everything except POSIX filename characters.
!       ;; This should be safe enough even for really weird shells.
!       (let ((result "") (start 0) end)
          (while (string-match "[^-0-9a-zA-Z_./]" 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))))))

--- 2391,2407 ----
  	(concat "\"" result (substring argument start) "\""))
      (if (equal argument "")
          "''"
!       ;; Quote everything except POSIX filename characters. This
should be safe
!       ;; enough even for really weird shells. Quoting is done with backslash,
!       ;; except for newlines, which are quoted with single quotes; a
!       ;; backslash-newline sequence is interpreted as line-continuation by the
!       ;; shell and thus effectivly being ignored.
!       (let ((result "") (start 0) end ch)
          (while (string-match "[^-0-9a-zA-Z_./]" argument start)
            (setq end (match-beginning 0)
+                 ch (substring argument end (1+ end))
                  result (concat result (substring argument start end)
!                                (if (string= ch "\n") "'\n'" (concat "\\" ch)))
                  start (1+ end)))
          (concat result (substring argument start))))))

Copy of the bug report
===========================================

GNU bug report logs - #7687
23.1.50; shell-quote-argument does not correctly quote newlines

Please write in English if possible, because the Emacs maintainers
usually do not have translators to read other languages for them.

Your bug report will be posted to the emacs-pretest-bug <at> gnu.org
mailing list.

Please describe exactly what actions triggered the bug
and the precise symptoms of the bug:

When I run the following script:

----------------------------------
#!/bin/bash
emacs -Q --batch eval='(shell-command (concat "echo "
(shell-quote-argument "line1\nline2") " >tmpfile"))'
cat tmpfile
----------------------------------

I expect the following content within 'tmpfile', i.e. exactly the
string I passed to shell-quote-argument.

-------------
line1
line2
-------------

Instead I get this:

-------------
line1line2
-------------

I have not made any modification to Emacs.

I made a patch and will send it as described in the Emacs manual in
chapter 'Sending Patches for GNU Emacs'.

If Emacs crashed, and you have the Emacs process in the gdb debugger,
please include the output from the following gdb commands:
    `bt full' and `xbacktrace'.
If you would like to further debug the crash, please read the file
/usr/share/emacs/23.1.50/etc/DEBUG for instructions.

In GNU Emacs 23.1.50.1 (i486-pc-linux-gnu, GTK+ Version 2.18.0)
 of 2009-09-27 on palmer, modified by Debian
 (emacs-snapshot package, version 1:20090909-1)
Windowing system distributor `The X.Org Foundation', version 11.0.10706000
configured using `configure  '--build' 'i486-linux-gnu' '--host'
'i486-linux-gnu' '--prefix=/usr' '--sharedstatedir=/var/lib'
'--libexecdir=/usr/lib' '--localstatedir=/var'
'--infodir=/usr/share/info' '--mandir=/usr/share/man' '--with-pop=yes'
'--enable-locallisppath=/etc/emacs-snapshot:/etc/emacs:/usr/local/share/emacs/23.1.50/site-lisp:/usr/local/share/emacs/site-lisp:/usr/share/emacs/23.1.50/site-lisp:/usr/share/emacs/site-lisp'
'--with-x=yes' '--with-x-toolkit=gtk' 'build_alias=i486-linux-gnu'
'host_alias=i486-linux-gnu' 'CFLAGS=-DDEBIAN
-DSITELOAD_PURESIZE_EXTRA=5000 -g -O2' 'LDFLAGS=-g -Wl,--as-needed'
'CPPFLAGS=''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: en_US.utf8
  value of $XMODIFIERS: nil
  locale-coding-system: utf-8-unix
  default enable-multibyte-characters: t

Major mode: Info

Minor modes in effect:
  tooltip-mode: t
  tool-bar-mode: t
  mouse-wheel-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  global-auto-composition-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Recent input:
M-x b u g - r e p o <tab> C-g C-h i C-h r C-s b u g
C-s <return> <return> 2 C-v SPC M-x r e p o r t - e
m a c s - b u <tab> <return>

Recent messages:
uncompressing emacs-6.gz...done
uncompressing emacs-7.gz...done
uncompressing emacs-8.gz...done
Mark saved where search started
uncompressing emacs.gz...done
uncompressing emacs-6.gz...done
uncompressing emacs.gz...done
uncompressing emacs-6.gz...done
uncompressing emacs.gz...done
uncompressing emacs-6.gz...done

Load-path shadows:
/usr/share/emacs/23.1.50/site-lisp/debian-startup hides
/usr/share/emacs/site-lisp/debian-startup
/usr/share/emacs/site-lisp/rst hides /usr/share/emacs/23.1.50/lisp/textmodes/rst