Hi!
As a further data point: I have found the following algorithm for
Windows shell quoting quite reliable under the following conditions:
* Windows 2000, Windows XP, or Windows 7
* the assembled command is executed using "system"
* both parent and child programs compiled by MinGW
I tested this algorithm quite thoroughly, but your mileage may vary.
In particular for redirecting output I have found it most reliable
to put the >"file name" in _front_ of the command. See quoteRedirectedCommand
below.
And now be prepared for (counter-)insanity and have fun!
best regards
Edwin
Special characters mentioned:
double_quote "
backslash \
caret ^
whitespace any ch with isspace(ch)
special_char any of these: less (<), greater (>), pipe (|),
ampersand (&), percent (%), caret (^)
Notation:
+= and + are used for string concatenation
Usage is like this:
system(double_quote + quoteCommand(double_quote + program_unquoted +
double_quote + space separated arguments quoted individually with
quoteArgument) + double_quote)
or
system(double_quote + quoteRedirectedCommand(double_quote +
program_unquoted + double_quote + space separated arguments quoted individually
with quoteArgument, filename_unquoted) + double_quote)
Here is the algorithm in pseudo-code:
String quoteArgument(String arg)
{
if (arg is the empty string)
{
quoted = double_quote + double_quote
}
else
{
quoted = empty string
pos = 0
while (pos < length of arg)
{
n = 0
while (arg[pos + n] is whitespace or backslash)
n = n + 1
if (n > 0)
{
quoted += double_quote
for each character ch in arg[pos..pos+n-1]
{
if (ch is backslash)
quoted += backslash + backslash
else
quoted += ch
}
quoted += double_quote
pos = pos + n
}
else
{
if (arg[pos] is double_quote)
{
quoted += backslash + double_quote
}
else
{
quoted += arg[pos]
}
pos = pos + 1
}
}
}
return quoted
}
String quoteCommand(String cmd)
{
even_number_of_quotes = true
quoted = empty string
for each character ch in cmd
{
if (ch is double_quote)
{
even_number_of_quotes = !even_number_of_quotes
}
else if ((ch is special_char) && even_number_of_quotes)
{
# quote characters that are special to cmd.exe:
quoted += caret
}
quoted += ch
# protect against expansion of %FOO% by turning it into %""FOO%:
if (ch is '%' and the following character is not double_quote or
backslash)
quoted += double_quote + double_quote
}
return quoted
}
String quoteRedirectedCommand(String cmd,
String filename)
{
return ">" + quoteCommand(double_quote + filename + double_quote + space +
cmd)
}
--
Empfehlen Sie GMX DSL Ihren Freunden und Bekannten und wir
belohnen Sie mit bis zu 50,- Euro! https://freundschaftswerbung.gmx.de
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php