From owner-ntemacs-users@june  Mon Sep 23 08:16:46 1996
X-VM-v5-Data: ([nil nil nil nil nil nil nil nil nil]
	[nil "Mon" "23" "September" "1996" "10:26:28" "-0400" "Kenneth L. Degrant, II" "kdegrant@earthlink.net" nil "28" "19.34 and Shell" "^From:" nil nil "9" nil nil nil nil]
	nil)
Received: from joker.cs.washington.edu (joker.cs.washington.edu [128.95.1.42]) by june.cs.washington.edu (8.7.5/7.2ju) with SMTP id IAA26377 for <voelker@june.cs.washington.edu>; Mon, 23 Sep 1996 08:16:46 -0700
Received: from june.cs.washington.edu (june.cs.washington.edu [128.95.1.4]) by joker.cs.washington.edu (8.6.12/7.2ws+) with ESMTP id IAA23335 for <voelker@joker.cs.washington.edu>; Mon, 23 Sep 1996 08:16:45 -0700
Received: from hungary.it.earthlink.net (hungary-c.it.earthlink.net [204.119.177.64]) by june.cs.washington.edu (8.7.5/7.2ju) with ESMTP id HAA24655 for <ntemacs-users@cs.washington.edu>; Mon, 23 Sep 1996 07:23:20 -0700
Received: from a12051.dracon.harris.com (a12051.nss.harris.com [151.114.22.138]) by hungary.it.earthlink.net (8.7.5/8.7.3) with SMTP id HAA13407 for <ntemacs-users@cs.washington.edu>; Mon, 23 Sep 1996 07:23:15 -0700 (PDT)
Message-ID: <32469E14.2D4@earthlink.net>
Organization: Harris Corporation
X-Mailer: Mozilla 2.01 (Win95; I)
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
From: "Kenneth L. Degrant II" <kdegrant@earthlink.net>
To: ntemacs-users@cs.washington.edu
Subject: 19.34 and Shell
Date: Mon, 23 Sep 1996 10:26:28 -0400

When I went from 19.33 to 19.34, I started having problems
spawning grep, or compile, with

"start-process-shell-command"

I gave it the name of a bat file which prints the args, and I got
argv0 = mybat.bat one two three four......
argv1 = 
argv.....

What am I doing wrong, or what is wrong.  For now I am going to go
back to 19.33.

Kenneth


-- 
------------------------------------------------------------------------------
Kenneth L. DeGrant II                          E-Mail:  
kdegrant@earthlink.net
Software Engineer                                       
kdegrant@harris.com
Harris Corporation                             Phone:   (910) 856-5445
Greensboro Operations                          Fax:     (910) 852-1820
3708 Alliance Drive                       
Greensboro, North Carolina  27407          
http://home.earthlink.net/~kdegrant
------------------------------------------------------------------------------

From andrewi@harlequin.co.uk  Fri Jul 26 07:56:59 1996
X-VM-v5-Data: ([nil nil nil nil nil nil nil nil nil]
	[nil "Fri" "26" "July" "1996" "15:55:29" "+0100" "Andrew Innes" "andrewi@harlequin.co.uk" nil "96" "Re: [preilly@rokchr.shr.dec.com: VC checkin failure]" "^From:" nil nil "7" nil nil nil nil]
	nil)
Received: from holly.cam.harlequin.co.uk (holly.cam.harlequin.co.uk [193.128.4.58]) by june.cs.washington.edu (8.7.5/7.2ju) with SMTP id HAA08360 for <voelker@cs.washington.edu>; Fri, 26 Jul 1996 07:56:57 -0700
Received: from propos.long.harlequin.co.uk by holly.cam.harlequin.co.uk; Fri, 26 Jul 1996 15:56:54 +0100
Received: from woozle.long.harlequin.co.uk (woozle [193.128.93.77]) by propos.long.harlequin.co.uk (8.6.12/8.6.12) with SMTP id PAA23293; Fri, 26 Jul 1996 15:55:29 +0100
Message-Id: <199607261455.PAA23293@propos.long.harlequin.co.uk>
In-Reply-To: <199607260454.VAA39246@joker.cs.washington.edu> 	(voelker@cs.washington.edu)
From: Andrew Innes <andrewi@harlequin.co.uk>
To: voelker@cs.washington.edu
Cc: rms@gnu.ai.mit.edu
Subject: Re: [preilly@rokchr.shr.dec.com: VC checkin failure]
Date: Fri, 26 Jul 1996 15:55:29 +0100

On Thu, 25 Jul 1996 21:02:27 -0700 (PDT), voelker@cs.washington.edu (Geoff Voelker) said:
>The short story then is that sometimes we want arguments quoted (when
>they truly are arguments with whitespace in them) and at other times
>we don't (when they are entire commands and arguments concatenated
>together into one string but separated by whitespace), and it is not
>obvious how to distinguish these cases inside Emacs.  If arguments to
>commands were always broken up into argv components by the time it
>reaches child_setup (e.g., the grep command, even though an argument
>to the shell, would be broken up into its components, too), then I
>think we could safely set win32-quote-process-args to t by default.
>But this seems impossible to ensure.

Thinking about it from the Unix perspective, the argv array given to
child_setup is already correct because the shell expects a single arg
containing the entire command following -c, which it will interpret as
if it was typed in at the command line and so construct the proper argv
array for grep.

If the standard shell on NT had a sufficiently powerful quoting and
escaping syntax, as the Unix shells do, then this would work on NT as
well even though we have to flatten the array into a single string - any
quoting in the original command arg could be passed through to the shell
by proper escaping.

As it is now, for shell commands the correct behaviour is to have
win32-quote-process-args turned off, because cmd.exe will interpret the
rest of its command line (after consuming things it believes are command
switches for it) as a command line typed by the user.  (When using a
different shell, though, the rule might well be different.)

When starting other processes directly, usually the right thing is to
have win32-quote-process-args enabled - we have to assume that the
caller has constructed the argv array as they intend the subprocess to
see it (if they haven't, then things would fail on Unix), so in this
case, Emacs should quote the args as necessary to ensure the subprocess
reconstructs the args as intended.  In general this isn't possible
because there is no standard or universally used convention for parsing
the command line, but most of the time putting double quotes around an
arg if it contains spaces is correct.

It would have been nice if all places in lisp code which run commmands
via a shell process used shell-command or shell-command-on-region (so we
could set win32-quote-process-args there), but a quick perusal of the
lisp dir shows lots of examples where code does

   (call-process shell-file-name ... "-c" args)

or similar things.  It is too much to expect all packages to go through
the shell-command interface, so we need to deal with this at a lower
level than shell-command.

[One possibility: If we could insist that people use a specific shell on
NT (not cmd.exe), then we could solve the shell command problem that
way.  Or more likely, we would provide a subprocess "helper" application
that NT Emacs always used to invoke whatever program is really intended.
This ends up using extra process resources unnecessarily in many cases.]


>Alternatively, we could have the NT specific code check to see if the
>command it is spawning is the shell (and the second argument is the
>equivalent of "/c"), and have that code automatically break up into
>components the command to be executed in the shell (assuming that
>words that should go together are already quoted).  Otherwise, we
>would quote any arguments with whitespace in them (that are not
>already quoted).  Andrew, can you think of a case where this wouldn't
>work very well?

No - I think something along these lines will be necessary (and this is
the best suggestion I've seen so far).

However, it occurs to me that we should really provide a way for the
user to specify what kind of quoting behaviour is required in each case.
We know that the correct quoting really depends on which shell program
is being used (and not just shells - some individual programs may
require special handling as well).

I suggest adding a hook or handler mechansim of some sort (to both
start-process and call-process) to allow lisp code to modify the arg
list before it is passed to child_setup.  That way, the lisp code could
implement proper quoting, or splitting of args etc, based on the program
name or whatever criteria was considered appropriate.

This would even allow working with packages that directly call "sh",
"rm", or other Unix-specific programs - if a user wanted to, they could
intercept invocations of specific programs and invoke an appropriate
substitute instead.  In the case of call-process, they might even want
to implement the functionality entirely in lisp (eg. you could imagine
ls-lisp working this way), but this is probably going too far.

Then child_setup et al would not need to worry about this issue -
win32-quote-process-args would go, and sys_spawnve would always simply
concatenate the args together separated by spaces.

Does this sound like a reasonable solution?

AndrewI

From andrewi@harlequin.co.uk  Fri Jul 26 08:52:39 1996
X-VM-v5-Data: ([nil nil nil nil nil nil nil nil nil]
	[nil "Fri" "26" "July" "1996" "16:52:29" "+0100" "Andrew Innes" "andrewi@harlequin.co.uk" nil "27" "Re: [preilly@rokchr.shr.dec.com: VC checkin failure]" "^From:" nil nil "7" nil nil nil nil]
	nil)
Received: from holly.cam.harlequin.co.uk (holly.cam.harlequin.co.uk [193.128.4.58]) by june.cs.washington.edu (8.7.5/7.2ju) with SMTP id IAA13939 for <voelker@cs.washington.edu>; Fri, 26 Jul 1996 08:52:37 -0700
Received: from propos.long.harlequin.co.uk by holly.cam.harlequin.co.uk; Fri, 26 Jul 1996 16:52:34 +0100
Received: from woozle.long.harlequin.co.uk (woozle [193.128.93.77]) by propos.long.harlequin.co.uk (8.6.12/8.6.12) with SMTP id QAA26541; Fri, 26 Jul 1996 16:52:29 +0100
Message-Id: <199607261552.QAA26541@propos.long.harlequin.co.uk>
In-Reply-To: <199607261455.PAA23293@propos.long.harlequin.co.uk> (andrewi)
From: Andrew Innes <andrewi@harlequin.co.uk>
To: andrewi@harlequin.co.uk
Cc: voelker@cs.washington.edu, rms@gnu.ai.mit.edu
Subject: Re: [preilly@rokchr.shr.dec.com: VC checkin failure]
Date: Fri, 26 Jul 1996 16:52:29 +0100

On Fri, 26 Jul 1996 15:55:29 +0100, Andrew Innes <andrewi> said:
  [ .. ]
>However, it occurs to me that we should really provide a way for the
>user to specify what kind of quoting behaviour is required in each case.
>We know that the correct quoting really depends on which shell program
>is being used (and not just shells - some individual programs may
>require special handling as well).
>
>I suggest adding a hook or handler mechansim of some sort (to both
>start-process and call-process) to allow lisp code to modify the arg
>list before it is passed to child_setup.  That way, the lisp code could
>implement proper quoting, or splitting of args etc, based on the program
>name or whatever criteria was considered appropriate.

An obvious way of adding a "hook" mechansim would be to introduce a
variable `process-name-arg-handler-alist' (say), which relates regexps
(matching on program name) to handler functions that would return the
actual argument list (including the program name) to be used.

If the handler was `t' then "standard" quoting of args with spaces would
be performed; `nil' would mean use the original arg list directly.

There is no reason this mechansim (if accepted) shouldn't be provided
for all platforms, although it is most useful for Windows (and DOS, and
maybe VMS).

AndrewI

From andrewi@harlequin.co.uk  Mon Jun 30 05:38:30 1997
X-VM-v5-Data: ([nil nil nil nil t nil nil nil nil]
	[nil "Mon" "30" "June" "1997" "13:37:50" "+0100" "Andrew Innes" "andrewi@harlequin.co.uk" "<199706301237.NAA04641@propos.long.harlequin.co.uk>" "74" "Re: cmdproxy" "^From:" nil nil "6" nil nil nil nil]
	nil)
Received: from holly.cam.harlequin.co.uk (holly.cam.harlequin.co.uk [193.128.4.58]) by june.cs.washington.edu (8.8.5+CS/7.2ju) with ESMTP id FAA19465 for <voelker@cs.washington.edu>; Mon, 30 Jun 1997 05:38:26 -0700
Received: from propos.long.harlequin.co.uk (propos.long.harlequin.co.uk [193.128.93.50]) by holly.cam.harlequin.co.uk (8.8.4/8.7.3) with ESMTP id NAA17861 for <voelker@cs.washington.edu>; Mon, 30 Jun 1997 13:38:21 +0100 (BST)
Received: from woozle.long.harlequin.co.uk (woozle.long.harlequin.co.uk [193.128.93.77]) by propos.long.harlequin.co.uk (8.8.4/8.6.12) with SMTP id NAA04641; Mon, 30 Jun 1997 13:37:50 +0100 (BST)
Message-Id: <199706301237.NAA04641@propos.long.harlequin.co.uk>
In-reply-to: <199706291958.MAA21150@joker.cs.washington.edu> 	(voelker@cs.washington.edu)
From: Andrew Innes <andrewi@harlequin.co.uk>
To: voelker@cs.washington.edu
Subject: Re: cmdproxy
Date: Mon, 30 Jun 1997 13:37:50 +0100 (BST)

On Sun, 29 Jun 1997 12:45:46 -0700 (PDT), voelker@cs.washington.edu (Geoff Voelker) said:
>Andrew,
>
>Below is my current version of cmdproxy.c.  I have concentrated so far
>on the case where "-c" is specified on the command line and it can
>execute the specified command directly.  I think that this case is
>done, although you might want to look at it yourself.  I had a couple
>of questions about the other cases, though.

Great, thanks.  I've actually cleaned up some of the other cases myself,
in a spare moment, so our efforts should dovetail nicely.

>When cmdproxy is used as a helper app for DOS programs, how is it that
>argv[0] is not the result of GetModuleFileName, i.e., how is it that
>the following predicate should evaluate to true:
>
>    if (GetModuleFileName (NULL, modname, sizeof (modname)) &&
>	strcmp (modname, argv[0]) != 0)

This question (and the one below) relates to changes I've made to
sys_spawnve in Emacs.  Emacs now detects not only DOS binaries, but also
Win32 applications that have been compiled using the Cygnus gcc port.

When asked to run a DOS program, Emacs now explicitly changes progname
to "cmdproxy.exe" while leaving cmdline unchanged.  Because of an
interesting feature of CreateProcess, Emacs thus starts cmdproxy.exe
even though the command line refers to the DOS program.  cmdproxy can
thus tell it was being invoked in this way, since MSVC sets up argv
purely by parsing the command line.

I did it this way, rather than changing the command line to add "-c",
partly to avoid disturbing the command line in any way (DOS programs are
more likely to interpret their command line in unusual ways), and
because in future cmdproxy might try to do extra things when starting a
DOS program on Windows 95.

>Secondly, when cmdproxy goes through its arguments, it assumes that
>the argv entry following the -c is the command.  But this does not
>seem correct if Emacs is quoting process arguments.  For example, if
>cmdproxy were invoked as
>
>	cmdproxy -c "grep ""some text"" *.c"
>
>then the argv entry after the '-c' is 'grep "some'.  For the case
>where cmdproxy can execute the command directly, I have it parse the
>string returned by GetCommandLine and determine the command itself,
>unquoting the quoting done by Emacs along the way.  But I think that
>this should be done for the case where a real shell has to execute the
>command, too.

This is where another change to sys_spawnve comes in.  By inspecting the
MSVC startup code, I discovered that MSVC uses backslash to escape
embedded quotes.  (Actually, it appears that MSVC intended to allow
quotes to be doubled for the same purpose, but because of a bug, quotes
have to be tripled for this to work!)

However, the Cygnus startup code only recognises doubled quotes.  (I
plan to submit a change to them sometime to allow backslash as an escape
character as well.  For now, this creates an additional barrier to
interoperability between Cygnus apps and "normal" apps.)

So Emacs now changes its quoting rule (by default) according to whether
it thinks the Win32 app is a Cygnus app or not.  If not, backslash is
used for escaping - this happens for cmdproxy, which is why the next
argv entry after "-c" contains the correct command line.

Since the embedded command line passed in this way is often typed
directly by the user, it seemed reasonable for cmdproxy to not make any
attempt to parse it (except to find which program to spawn, if it can do
so directly).  Well, actually it might be useful to parse the embedded
command line (allowing both methods of escaping) and possibly
reconstruct it using the appropriate method as Emacs does.

AndrewI

