bug-gawk
[Top][All Lists]

## Re: [bug-gawk] Percent Signs in External Commands on Windows

 From: Eli Zaretskii Subject: Re: [bug-gawk] Percent Signs in External Commands on Windows Date: Wed, 11 Apr 2012 11:51:19 +0300

> Date: Tue, 10 Apr 2012 23:46:07 -0700 (PDT)
>
> If /C or /K is specified, then the remainder of the command line after
> the switch is processed as a command line, where the following logic is
> used to process quote (") characters:
>
>     1.  If all of the following conditions are met, then quote characters
>         on the command line are preserved:
>
>         - no /S switch
>         - exactly two quote characters
>         - no special characters between the two quote characters,
>           where special is one of: &<>()@^|
>         - there are one or more whitespace characters between the
>           the two quote characters
>         - the string between the two quote characters is the name
>           of an executable file.
>
>     2.  Otherwise, old behavior is to see if the first character is
>         a quote character and if so, strip the leading character and
>         remove the last quote character on the command line, preserving
>         any text after the last quote character.
> ---
> MSDN repeats this.
> http://msdn.microsoft.com/en-us/library/bb490880.aspx
>
>
> Is this not basically what would happen?
> 1) Gawk code: system("WHATEVER"); # or "\"WHATEVER\""
> 2a) C code: popen/system/etc("WHATEVER");
> 2b) A shell, CMD, is tracked down and the func becomes...
> 2c) func-that-execs("path\\to\\cmd.exe", "/C", "WHATEVER");
> 3a) CMD then decides whether to eat a pair of quotes (no unescaping happens),
> 3b) and replaces percented words that match known variables, leaving others
> be.
> 3c) Then, as it would in batch, it tokenizes the commandline based on
> un-careted quotes/pipes/redirects/etc into programs and args (sans such
> carets),
> 3c) and spawns programs with their args otherwise unaltered.
> 4) Each program (or shell built-in) individually globs its own array of args,
> if desired.
>
> It sounds like you're saying APIs are messing with the contents of WHATEVER
> at 2c?

No.  I'm saying that what CMD does with quotes is insane.  I hope it
is clear to you that (1.) never happens in real-life use cases.  If
it's not clear, try coming up with a non-trivial command that
satisfies all of those conditions.

So in practice, you always hit (2.)  And what CMD does then is
hopelessly wrong, because it gives you no way of escape-protecting the
first and the last quote from being removed.  Try constructing a
command that includes quote characters which you need to end up in the
application, and you will see how hard that is, and how in some
situation it is downright impossible.

> As blanket advice, I'd just suggest experimenting with both fixes during
> development and warning that builds vary: parsing WHATEVER either as an
> argument to the shell, or as a line in a temporary shell script. The relevant
> situations on windows being [when a quoted path\program is given a quoted
> arg] or [when percents appear]. For ambitious users, I don't know where an
> optimized example of that check would be best offered up.

The problem with this good advice is that the number of people who use
the development version on MS-Windows is practically zero.  So I
cannot count on getting any significant feedback, unfortunately.

Thanks.