This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project. See the Cygwin home page for more information.
[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index] [Subject Index] [Author Index] [Thread Index]

Re: AW: how to use emacs in -batch mode from bash?



On Wed, 17 Feb 1999 16:24:23 -0500, Christopher Faylor <cgf@cygnus.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@sourceware.cygnus.com