Re: RFC: [email protected]

"Tony R. Ambardar" <tonya-1KtHxNhk3+Ww5LPnMra/[email protected]> Fri, 12 Sep 2003 16:54:07 -0700 (PDT)
Newsgroups gmane.comp.gnu.mingw.patches
Message-ID <[email protected]>
The attached patch replaces the one I posted earlier. In addition to
dealing with backslashes correctly, it also works around some
quoting and escaping problems due to the bizarre Windows command-line
parsing that takes place when CreateProcess() is used. The affected file,
w32/subproc/sub_proc.c, could use some clean-up but I'd like feedback from
testers first.

Attached also is a Makefile that includes some test cases from the
MinGW BUGS list. I think all of them should be fixed now.

Cheers,
Tony Ambardar

On Fri, 5 Sep 2003, Tony R. Ambardar wrote:

>
> Greetings,
>
> (* Note: my environment is fresh MinGW-3.0.0-rc4 + MSYS-1.0.10-rc1 )
>
> I've noticed a serious problem in the way the latest mingw32-make (3.80-3)
> handles quoting of arguments passed to sub-shells. Please run the test
> Makefile below using "mingw32-make". The correct output should be
> "a/\b\c\\\" under your favourite unix but "sed" will error out instead.
>
> Re-run using "make --debug=j -f Makefile.test" and notice in the quoted
> argument to "sh.exe -c" that backslashes are not being escaped themselves
> by backslashes. So the actual command "sed" sees is 's|\|/|', which
> explains the failure.
>
> The attached (minimal) patch to win32/subproc/sub_proc.c attempts to
> correct this.  I've rebuilt and tested that it does the right thing, and
> it shouldn't affect the MKS, Cygwin or DOS shells either.
>
> I really think the logic of that code section needs to be rewritten, but I
> don't have the time to install and test under 3 different environments :(.
>
> Just out of curiosity, are the MinGW changes for Make-3.80 getting folded
> back into the GNU sources anytime?
>
> Regards and many thanks to all the MinGW developers,
> Tony Ambardar
>
>
> -------8<--------------8<-- Test Makefile --8<-----------8<-------
>
> # This build rule fails unless the make program correctly
> # deals with backslashes in the sub-shell command line.
>
> quote:
> 	echo 'a\\b\c\\\' | sed -e 's|\\|/|'
>
> -------8<--------------8<-- Test Makefile --8<-----------8<-------
>
>
make-mingw-quote-escape.patch (text/plain, 1 KB)
--- mingw32-make-3.80.0-3/w32/subproc/sub_proc.c.orig	Sun Aug 31 17:58:42 2003
+++ mingw32-make-3.80.0-3/w32/subproc/sub_proc.c	Fri Sep 12 00:22:03 2003
@@ -973,11 +973,14 @@
 				 */
 				bytes_required += (backslash_count + 1);
 				backslash_count = 0;
+				*enclose_in_quotes_i = 1;
 				break;
 
 #if !defined(HAVE_MKS_SHELL) && !defined(HAVE_CYGWIN_SHELL)
 			case '\\':
 				backslash_count++;
+				bytes_required += 1;
+				*enclose_in_quotes_i = 1;
 				break;
 #endif
 	/*
@@ -1088,6 +1091,11 @@
 #if !defined(HAVE_MKS_SHELL) && !defined(HAVE_CYGWIN_SHELL)
 			} else if (*p == '\\') {
 				backslash_count++;
+
+				while(backslash_count) {
+					*(command_line_i++) = '\\';
+					backslash_count--;
+				}
 			} else {
 				backslash_count = 0;
 #endif
@@ -1105,8 +1113,9 @@
 			 * Add one \ for each \ that precedes the
 			 * closing ".
 			 */
-			while(backslash_count--) {
+			while(backslash_count) {
 				*(command_line_i++) = '\\';
+				backslash_count--;
 			};
 #endif
 			*(command_line_i++) = '\"';
Makefile.maketest (text/plain, 1.3 KB)
# This build rule fails unless the make program correctly

# deals with backslashes in the sub-shell command line.



.PHONY : maketest



maketest:

	@echo Pipe and sed test:

	@echo 'Result should be:'

	@echo 'a/\b\c\\\'

	echo 'a\\b\c\\\' | sed -e 's|\\|/|' 



	@echo



	@echo printf, double quotes test:

	@echo 'Should be:' 

	@echo '\\MoreCharacters'

	printf "\\\\\\\\MoreCharacters\\n"

	@echo 'Should be:' 

	@echo '\\'

	printf "\\\\\\\\"

	

	@echo

	@echo



	@echo echo, double quotes:

	@echo 'Should be:'

	@echo '\\\\MoreCharacters'

	echo "\\\\\\\\MoreCharacters"

	@echo 'Should be:'

	@echo '\\\\'

	echo "\\\\\\\\"



	@echo

	@echo



	@echo echo, no quotes:

	@echo 'Should be:'

	@echo '\\\\MoreCharacters'

	echo \\\\\\\\MoreCharacters

	@echo 'Should be:'

	@echo '\\\\'

	echo \\\\\\\\



	@echo

	@echo



	@echo Should be a literal double quote:

	printf 'abc"xyz'

	@printf "\n\n"



	@echo Should be a literal double quote:

	printf 'abc\"xyz\n\n'



	@echo Should be a newline:

	printf 'abc\nxyz'

	@printf "\n\n"



	@echo Should be a literal backslash and double quote:

	printf 'abc\\"xyz'

	@printf "\n\n"



	@echo Should be a literal double quote and n

	printf 'abc"nxyz'

	@printf "\n\n"



	@echo Workaround:

	printf 'abc'"\""'xyz'