delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/1999/02/18/13:23:42

Delivered-To: listarch-cygwin AT sourceware DOT cygnus DOT com
Mailing-List: contact cygwin-help AT sourceware DOT cygnus DOT com; run by ezmlm
Sender: cygwin-owner AT sourceware DOT cygnus DOT com
Delivered-To: mailing list cygwin AT sourceware DOT cygnus DOT com
Date: Thu, 18 Feb 1999 13:22:07 GMT
Message-Id: <199902181322.NAA15032@gpo.cam.harlequin.co.uk>
From: Andrew Innes <andrewi AT harlequin DOT co DOT uk>
To: cgf AT cygnus DOT com
CC: F DOT J DOT Wright AT qmw DOT ac DOT uk, mike DOT fabian AT it-mannesmann DOT de,
Rolf DOT Sandau AT de DOT bosch DOT com, ntemacs-users AT cs DOT washington DOT edu,
cygwin AT sourceware DOT cygnus DOT com
In-reply-to: <19990217162423.A13997@cygnus.com> (message from Christopher
Faylor on Wed, 17 Feb 1999 16:24:23 -0500)
Subject: Re: AW: how to use emacs in -batch mode from bash?
References: <5B9BE15FBECDD111A1820000F843B87C16C16F AT bkmail1 DOT bk DOT bosch DOT de> <199902161547 DOT HAA28357 AT june DOT cs DOT washington DOT edu> <36C9ADFC DOT ABD3ACE4 AT Maths DOT QMW DOT ac DOT uk> <199902171909 DOT TAA28859 AT gpo DOT cam DOT harlequin DOT co DOT uk> <19990217162423 DOT A13997 AT cygnus DOT com>

On Wed, 17 Feb 1999 16:24:23 -0500, Christopher Faylor <cgf AT cygnus DOT com> said:
>On Wed, Feb 17, 1999 at 07:09:28PM +0000, Andrew Innes wrote:
>>However, we are now seeing the same problem occuring, this time on the
>>Cygnus side.  The Cygnus port of bash will be applying the normal shell
>>quoting rules to parse the command line typed by the user (or entered in
>>the shell script), to construct the list of arguments to pass to Emacs.
>>However, when bash invokes spawn() or exec() or some similiar library
>>function to actually invoke Emacs, it has to flatten the argument list
>>into a single string.  Clearly, the library function that does that is
>>assuming the subprocess will use the Cygnus quoting rules to reconstruct
>>the list of arguments.  That fails when an argument contains an embedded
>>quote and the application doesn't use the Cygnus rules, which is the
>>situation here.
>
>As far as I know, the method used to "quote a quote" in cygwin is the
>same as what is used in Visual C's libraries.  Here's a small program
>that I just wrote to test this:
>
>#include <stdio.h>
>main(int argc, char **argv)
>{
>    int i;
>    for (i = 0; i < argc; i++)
>        printf("arg %d: /%s/\n", i, argv[i]);
>}
>
>And, here's the result:
>
>c:\tmp>echoarg a b """"
>arg 0: /echoarg/
>arg 1: /a/
>arg 2: /b/
>arg 3: /"/

This example doesn't show up the difference, because the MSVC startup
code _does_ handle repeated quotes, but not in quite the same way (see
crt/src/stdargv.c in the MSVC library source for the gory details).

Here is a more revealing example:

d:\users\andrewi>echoarg "test a" "test ""b""" "test ""c"" d"
arg 0: /echoarg/
arg 1: /test a/
arg 2: /test "b"/
arg 3: /test "c/
arg 4: /d/

Note that arg 2 comes out as expected (fortuitously it turns out), but
arg 3 is split into two args by the MSVC code (and drops a quote in the
process), and not by the Cygwin code.  The reason is that MSVC sometimes
treats a doubled quote as the end of the argument.  To escape an
embedded quote reliably (at least in the absence of preceding
backslashes), you have to triple it like so:

d:\users\andrewi>echoarg "test a" "test """b"""" "test """c""" d"
arg 0: /echoargs/
arg 1: /test a/
arg 2: /test "b"/
arg 3: /test "c" d/

In fairness, this might not be a bug in the MSVC code, but a deliberate
feature.  It enables the following, slightly strange, method of
constructing arguments with whitespace:

d:\users\andrewi>echoarg "a and b "together
arg 0: /echoargs/
arg 1: /a and b together/

I can imagine that someone requested this behaviour, as a way to enable
DOS batch files to do things they couldn't otherwise easily do.

Anyway, the upshot of this mess is that the only really reliable way to
escape an embedded quote is to put a backslash before it (and double all
literal backslashes immediately preceding the embedded quote).  This is
what I refer to as the Microsoft quoting rule.

>>Note that this is a problem with bash that applies when it invokes any
>>application not compiled with the cygwin library, not just Emacs.
>>
>>I see two possible solutions to this general problem:
>>
>>1. Change the cygwin spawn/exec/whatever library functions to use the
>>Microsoft rules for escaping embedded quotes when running non-cygwin
>>applications (I believe they already detects when they are spawning
>>non-cygwin applications; if not, the method Emacs uses could be
>>reused for this).
>
>Cygwin does not know when it is running a non-cygwin application.  If it
>did we wouldn't go through this quoting mess at all.
>
>If Emacs is detecting this somehow, I'd love to hear how they do it.  I've
>wanted to put more smarts into spawn for some time.

In NT-Emacs, we examine the header of an executable, and if it is in PE
format, we walk the import table to see whether it implicitly links to
"cygwin.dll". (In the next release, I just check whether there is a dll
whose name starts "cygwin".)

>>2. Change the cygwin quoting rules to match the Microsoft ones.  This
>>would apply to spawn/exec and the startup code, and would cause some
>>breakage when mixing with applications compiled with old versions of
>>cygwin.
>
>See above.  As far as I can tell, cygwin is already compliant with Microsoft's
>rules.  That was the intent in this whole scheme, actually.

The Microsoft rules are unfortunately more complicated than they seem,
as shown above.  I believe the simplest rule to reliably escape embedded
quotes for MSVC-compiled programs is to use backslash, which is what
NT-Emacs does.

AndrewI


--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe AT sourceware DOT cygnus DOT com


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019