Bug 54285 - Ant task exec arg value does not quote properly for MSWindows
Summary: Ant task exec arg value does not quote properly for MSWindows
Status: NEW
Alias: None
Product: Ant
Classification: Unclassified
Component: Core tasks (show other bugs)
Version: 1.8.0
Hardware: PC Windows XP
: P2 normal (vote)
Target Milestone: ---
Assignee: Ant Notifications List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-12-12 19:24 UTC by Vincent Belaïche
Modified: 2013-02-21 05:19 UTC (History)
0 users



Attachments
Java code proposal to escape properly double quotes for MSWindows (5.32 KB, application/octet-stream)
2012-12-12 19:24 UTC, Vincent Belaïche
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Vincent Belaïche 2012-12-12 19:24:56 UTC
Created attachment 29747 [details]
Java code proposal to escape properly double quotes for MSWindows

I want to pass to some application the following string:\\
{noformat}
<link rel="icon" type="image/png" size="48x48" href="path/to/my/favion.png" />
{noformat}

So I use the following:\\
{noformat} 
<exec executable="some_executable.exe">
<arg value="&lt;link rel=&quote;icon&quote; type=&quote;image/png&quote; size=&quote;48x48&quote; href=&quote;path/to/my/favion.png&quote; /&gt;"/>
</exec>
{noformat}

However, the application gets this instead:\\
{noformat}
<link rel=icon type=image/png size=48x48 href=path/to/my/favion.png />
{noformat}

Double quotes were stripped by MSWindows because they were not properly escaped in the command line by Ant.

I wrote a piece of code to do escaping of double quotes properly, and I attached it to the issue. There are two implementation proposed for the double quote escaping, on is based on regexps, and the other is plain character processing --- probably more efficient, but maybe a little more obscure.
Comment 1 Vincent Belaïche 2012-12-12 19:26:11 UTC
I had initially submitted the bug to the wrong bug tracer on Jira, and this is why the markup are Jira markups.
Comment 2 Vincent Belaïche 2012-12-24 12:10:10 UTC
One more thing: concerning the quote escaping, the code which I submitted contains two flavours of implementation one in ForcedQuote which is based on plain string manipulations, and one in ConditionalQuote which is based on regexps.

I mention this because in addition ForcedQuote does quoting always, and ConditionalQuote does it only when needed by content. 

So it is a bit confusing as in fact there is no relationship between "forced versus conditional on content" quoting on the one hand, and "based on plain string manipulation versus using regexps" on the other hand: it would actually be possible to have an implementation doing the quoting only when needed by content and based only on elementary string manipulation, or to have an implementation based on regexp and doing the quoting always.

I provided these two implementation just to show that the problem can be solved in a number of manner depending on what you want to get, and how you want to get it.

Please note also that I did double quote escaping based on use of backslash, however this is not the sole technique, one can also replace each double quotes by 3 of them, that happen to work as long as there aren't any preceding backslash.
Comment 3 Vincent Belaïche 2013-02-12 06:56:17 UTC
This bug is duplicate with Bug 23273.

I don't know which one should be closed with "mark as duplicated" and which one has to be kept alive until the problem is solved.
Comment 4 Vincent Belaïche 2013-02-21 05:19:12 UTC
Hi,

After more thoughts I think that the code which I have provided is over-complex, and there is a simpler solution as follows to double-quote a string which is to replace all backslash and double quote respectively by \\ and \"

Examples:

a\b         is double quoted as "a\\b"
a"b         is double quoted as "a\"b"
a\\b        is double quoted as "a\\\\b"
"ab"        is double quoted as "\"ab\""

this means that if you call a command like this:

my-command "a\\b" "a\"b" "a\\\\b" "\"ab\"" 

then the argument passed to my-command are the following (one per line):

arg 1=a\b
arg 2=a"b
arg 4=a\\b
arg 5="ab"

One more nasty stuff is how to quote the percent sign. Imagine that you have an environment variable TOTO that is unset, then

my-command "a%TOTO%b"

will pass argument 1 as follows

arg 1=a%TOTO%b

but if now TOTO is set to xxx, then the same

my-command "a%TOTO%b"

will pass

arg 1=axxxb

this is why, not to take any risks percent signs should also be quoted, and the way to do that is to replace any % by ^% outside of the double-quotes so

a%TOTO%b is double quoted as "a"^%"TOTO"^%"b"
a%%%b    is double quoted as "a"^%^%^%"b"

I will prepare some updated code proposal for MSDosQuoter to reflex these new thoughts.