bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: David Millis
Subject: Re: [bug-gawk] Percent Signs in External Commands on Windows
Date: Wed, 11 Apr 2012 20:49:22 -0700 (PDT)

--- On Wed, 4/11/12, Eli Zaretskii <address@hidden> wrote:

> So in practice, you always hit (2.)
Exactly.

> 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.
Quite an easy way actually. This is what I've been saying all along about 
concatenating fodder quotes.
You protect your first and last quotes, buy _adding_ quotes whose sole purpose 
are to get eaten by CMD's stupid but predictable heuristic.
There's no inner-escaping to worry about, and the command string is otherwise 
unscathed.

# To run: "path\wget.exe" -O - -q "..."
command = "\"path\\wget.exe\" -O - -q \"...\"";
command = "\"" command "\""; # user adds fodder quotes
command | getline;

The original command's quotes survive intact.


> 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.
This is unrelated to CMD /C vs temp bat.
The only difference is whether first&last quotes get eaten or percents.
>From then on, CMD parses the arg/bat-line the same way.
So the remainder of this response is an aside...


Trying to get a quote character INTO a program's args is a different matter. On 
windows, globbing is handled within individual apps (in a manner decided at 
compile-time, per the links you offered) instead of the CMD shell. Unixy 
windows shells may add a layer of globing as well, but also irrelevant to this 
particular thread about CMD quirks.

> (b) says that it is sometimes impossible. One impossible
> situation is when you need to pass a single " character
> to a program.

The trick to passing an arg containing a quote will vary wildly depending on 
what programs are involved, but I'll have a go at a test case for giggles. :)
---
#include <stdio.h>
int main(int argc, const char* argv[]) {
  int i;
  for(i=0; i < argc; i++) {
    printf("arg %d: %s\n", i, argv[i]);
  }
}
---
c:\> PrintArgs.exe \"
arg 0: PrintArgs.exe
arg 1: "
Fodder quotes are optional, but unintrusive in this case.
c:\> cmd /c "PrintArgs.exe \""
arg 0: PrintArgs.exe
arg 1: "

A python example...
---
import os
os.system("PrintArgs.exe \\\"")
os.execl("c:\\windows\\system32\\cmd.exe", "/c", "PrintArgs.exe", "\\\"")
---
arg 0: PrintArgs.exe
arg 1: "
arg 0: PrintArgs.exe
arg 1: "
Academic note: During testing, bypassing the shell to execl() PrintArgs.exe 
directly with various combinations of slashes quotes and carets failed to 
convey the quote, so CMD helped it somehow; not that Gawk would ever do such a 
thing.


David Millis




reply via email to

[Prev in Thread] Current Thread [Next in Thread]