[m-rev.] for review: gdbrun bug fix

Fergus Henderson fjh at cs.mu.OZ.AU
Thu Apr 26 20:31:54 AEST 2001


On 26-Apr-2001, Zoltan Somogyi <zs at cs.mu.OZ.AU> wrote:
> For review by Fergus.
> 
> tools/gdbrun:
> 	Fix a bug: quote the arguments properly.
> 
> Index: gdbrun
> ===================================================================
> RCS file: /home/mercury1/repository/mercury/tools/gdbrun,v
> retrieving revision 1.1
> diff -u -b -r1.1 gdbrun
> --- gdbrun	2001/01/18 01:19:29	1.1
> +++ gdbrun	2001/04/26 09:19:08
> @@ -12,6 +12,17 @@
>  tmpfile=/tmp/gdbrun.$$
>  trap "rm -f $tmpfile" 0 1 2 3 13 15
>  shift
> -echo run "$@" > "$tmpfile"
> +echo -n "run " > "$tmpfile"
> +for arg in "$@"
> +do
> +	if echo "$arg" | grep " " > /dev/null
> +	then
> +		echo -n "\"$arg\" " >> $tmpfile
> +	else
> +		echo -n "$arg " >> $tmpfile
> +	fi
> +done
> +echo >> $tmpfile
> +

Thanks for the bug fix.

The quoting is still not 100% correct, though.
To do it properly you'd have to also quote stuff containing "#"
or ">" and escape stuff containing "$" or "\" and maybe more.
Handling # and > is easy enough, but I don't know whether it's
worth bothering with handling "$" and "\" -- perhaps just
put an XXX comment.

"echo -n" is not portable.  This really sucks :-(
In this case you can fix it by building up
the command in a shell variable:

	runargs=""
	for arg in "$@"; do
		if echo "$arg" | grep " " > /dev/null; then
			runargs="$runargs\"$arg\" "
		else
			runargs="$runargs$arg "
		fi
	done
	echo "run $runargs" > "$tmpfile"

Also I'd write the test for spaces using "case" rather than
"echo ... | grep ... > /dev/null":

	runargs=""
	for arg in "$@"; do
		case "$arg" in
		*" "*)	runargs="$runargs\"$arg\" " ;;
		*)	runargs="$runargs$arg " ;;
		esac
	done
	echo "run $runargs" > "$tmpfile"

This is a tad more efficient, since it invokes fewer processes,
and is also a little neater IMHO. 

Handling > and # too is easy enough, just use a [...]
pattern, although getting the backslashes in the right
places is tricky.  I think the following works.

	runargs=""
	for arg in "$@"; do
		case "$arg" in
		*[\>\#\ ]*)	runargs="$runargs\"$arg\" " ;;
		*)		runargs="$runargs$arg " ;;
		esac
	done
	echo "run $runargs" > "$tmpfile"

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list