]> Savannah Git Hosting - gnulib.git/commitdiff
csharpcomp-script: Handle directories with spaces correctly.
authorBruno Haible <bruno@clisp.org>
Thu, 10 Oct 2024 17:39:36 +0000 (19:39 +0200)
committerBruno Haible <bruno@clisp.org>
Thu, 10 Oct 2024 17:40:16 +0000 (19:40 +0200)
Reported by Michele Locati <michele@locati.it>.

* build-aux/csharpcomp.sh.in (command_for_print, command_for_eval,
options_csc_for_print, options_csc_for_eval, sources_csc_for_print,
sources_csc_for_eval): New variables.
(sed_protect_1, sed_protect_2a, sed_protect_2b, sed_protect_2c,
sed_protect_3a, sed_protect_3b): New variables, copied from
build-aux/x-to-1.in.
(func_add_word_to_command): New function, copied from
build-aux/x-to-1.in.
(func_add_word_to_options_csc, func_add_word_to_sources_csc): New
functions.
(options_csc, sources_csc): Remove variables. Use
func_add_word_to_options_csc, func_add_word_to_sources_csc instead of
augmenting them.
Use options_csc_for_print, options_csc_for_eval, sources_csc_for_print,
sources_csc_for_eval when invoking csc.
* build-aux/csharpexec.sh.in (sed_quote_subst): Remove unused variable.

ChangeLog
build-aux/csharpcomp.sh.in
build-aux/csharpexec.sh.in

index b427c5033cb1858b64c1049287de8871fcc7432f..0b47332948000a54a41b65cd287f031a6e1c02c3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,24 @@
+2024-10-10  Bruno Haible  <bruno@clisp.org>
+
+       csharpcomp-script: Handle directories with spaces correctly.
+       Reported by Michele Locati <michele@locati.it>.
+       * build-aux/csharpcomp.sh.in (command_for_print, command_for_eval,
+       options_csc_for_print, options_csc_for_eval, sources_csc_for_print,
+       sources_csc_for_eval): New variables.
+       (sed_protect_1, sed_protect_2a, sed_protect_2b, sed_protect_2c,
+       sed_protect_3a, sed_protect_3b): New variables, copied from
+       build-aux/x-to-1.in.
+       (func_add_word_to_command): New function, copied from
+       build-aux/x-to-1.in.
+       (func_add_word_to_options_csc, func_add_word_to_sources_csc): New
+       functions.
+       (options_csc, sources_csc): Remove variables. Use
+       func_add_word_to_options_csc, func_add_word_to_sources_csc instead of
+       augmenting them.
+       Use options_csc_for_print, options_csc_for_eval, sources_csc_for_print,
+       sources_csc_for_eval when invoking csc.
+       * build-aux/csharpexec.sh.in (sed_quote_subst): Remove unused variable.
+
 2024-10-10  Bruno Haible  <bruno@clisp.org>
 
        java{comp,exec}-script, csharp{comp,exec}-script: Improve debugging.
index 58b53c6bffaa60c6a512386ae4c83fcbf3a8d229..2dec12af7e0657ea7291c252f29ce7182e666a84 100644 (file)
@@ -64,21 +64,58 @@ func_tmpdir ()
   }
 }
 
+# In order to construct a command that invokes csc, we need 'eval', because
+# some of the arguments may contain spaces.
+command_for_print=
+command_for_eval=
+options_csc_for_print=
+options_csc_for_eval=
+sources_csc_for_print=
+sources_csc_for_eval=
+# Protecting special characters, hiding them from 'eval':
+# Double each backslash.
+sed_protect_1='s/\\/\\\\/g'
+# Escape each dollar, backquote, double-quote.
+sed_protect_2a='s/\$/\\$/g'
+sed_protect_2b='s/`/\\`/g'
+sed_protect_2c='s/"/\\"/g'
+# Add double-quotes at the beginning and end of the word.
+sed_protect_3a='1s/^/"/'
+sed_protect_3b='$s/$/"/'
+func_add_word_to_command ()
+{
+  command_for_print="${command_for_print:+$command_for_print }$1"
+  word_protected=`echo "$1" | sed -e "$sed_protect_1" -e "$sed_protect_2a" -e "$sed_protect_2b" -e "$sed_protect_2c" -e "$sed_protect_3a" -e "$sed_protect_3b"`
+  command_for_eval="${command_for_eval:+$command_for_eval }$word_protected"
+}
+func_add_word_to_options_csc ()
+{
+  options_csc_for_print="${options_csc_for_print:+$options_csc_for_print }$1"
+  word_protected=`echo "$1" | sed -e "$sed_protect_1" -e "$sed_protect_2a" -e "$sed_protect_2b" -e "$sed_protect_2c" -e "$sed_protect_3a" -e "$sed_protect_3b"`
+  options_csc_for_eval="${options_csc_for_eval:+$options_csc_for_eval }$word_protected"
+}
+func_add_word_to_sources_csc ()
+{
+  sources_csc_for_print="${sources_csc_for_print:+$sources_csc_for_print }$1"
+  word_protected=`echo "$1" | sed -e "$sed_protect_1" -e "$sed_protect_2a" -e "$sed_protect_2b" -e "$sed_protect_2c" -e "$sed_protect_3a" -e "$sed_protect_3b"`
+  sources_csc_for_eval="${sources_csc_for_eval:+$sources_csc_for_eval }$word_protected"
+}
+
 sed_quote_subst='s/\([|&;<>()$`"'"'"'*?[#~=%   \\]\)/\\\1/g'
+
 options_mcs=
-options_csc="-nologo"
 sources=
-sources_csc=
+func_add_word_to_options_csc "-nologo"
 while test $# != 0; do
   case "$1" in
     -o)
       case "$2" in
         *.dll)
           options_mcs="$options_mcs -target:library"
-          options_csc="$options_csc -target:library"
+          func_add_word_to_options_csc "-target:library"
           ;;
         *.exe)
-          options_csc="$options_csc -target:exe"
+          func_add_word_to_options_csc "-target:exe"
           ;;
       esac
       options_mcs="$options_mcs -out:"`echo "$2" | sed -e "$sed_quote_subst"`
@@ -90,7 +127,7 @@ while test $# != 0; do
           arg=`cygpath -w "$arg"`
           ;;
       esac
-      options_csc="$options_csc -out:"`echo "$arg" | sed -e "$sed_quote_subst"`
+      func_add_word_to_options_csc "-out:$arg"
       shift
       ;;
     -L)
@@ -103,20 +140,20 @@ while test $# != 0; do
           arg=`cygpath -w "$arg"`
           ;;
       esac
-      options_csc="$options_csc -lib:"`echo "$arg" | sed -e "$sed_quote_subst"`
+      func_add_word_to_options_csc "-lib:$arg"
       shift
       ;;
     -l)
       options_mcs="$options_mcs -reference:"`echo "$2" | sed -e "$sed_quote_subst"`
-      options_csc="$options_csc -reference:"`echo "$2" | sed -e "$sed_quote_subst"`".dll"
+      func_add_word_to_options_csc "-reference:$2.dll"
       shift
       ;;
     -O)
-      options_csc="$options_csc -optimize+"
+      func_add_word_to_options_csc "-optimize+"
       ;;
     -g)
       options_mcs="$options_mcs -debug"
-      options_csc="$options_csc -debug+"
+      func_add_word_to_options_csc "-debug+"
       ;;
     -*)
       echo "csharpcomp: unknown option '$1'" 1>&2
@@ -132,7 +169,7 @@ while test $# != 0; do
           arg=`cygpath -w "$arg"`
           ;;
       esac
-      options_csc="$options_csc -resource:"`echo "$arg" | sed -e "$sed_quote_subst"`
+      func_add_word_to_options_csc "-resource:$arg"
       ;;
     *.cs)
       sources="$sources "`echo "$1" | sed -e "$sed_quote_subst"`
@@ -144,7 +181,7 @@ while test $# != 0; do
           arg=`cygpath -w "$arg"`
           ;;
       esac
-      sources_csc="$sources_csc "`echo "$arg" | sed -e "$sed_quote_subst"`
+      func_add_word_to_sources_csc "$arg"
       ;;
     *)
       echo "csharpcomp: unknown type of argument '$1'" 1>&2
@@ -179,25 +216,29 @@ else
         arg=`cygpath -w "$arg"`
         ;;
     esac
-    options_csc="$options_csc -lib:"`echo "$arg" | sed -e "$sed_quote_subst"`
+    func_add_word_to_options_csc "-lib:$arg"
     for file in `cd "$dotnet_runtime_dir" && echo [ABCDEFGHIJKLMNOPQRSTUVWXYZ]*.dll`; do
       case "$file" in
         *.Native.*) ;;
-        *) options_csc="$options_csc -reference:"`echo "$file" | sed -e "$sed_quote_subst"` ;;
+        *) func_add_word_to_options_csc "-reference:$file" ;;
       esac
     done
+    func_add_word_to_command dotnet
     csc="$dotnet_sdk_dir/Roslyn/bincore/csc.dll"
     case "@build_os@" in
       cygwin*)
         csc=`cygpath -w "$csc"`
         ;;
     esac
-    test -z "$CSHARP_VERBOSE" || echo dotnet "$csc" $options_csc $sources_csc 1>&2
-    exec dotnet "$csc" $options_csc $sources_csc
+    func_add_word_to_command "$csc"
+    test -z "$CSHARP_VERBOSE" || echo "$command_for_print $options_csc_for_print $sources_csc_for_print" 1>&2
+    eval "$command_for_eval $options_csc_for_eval $sources_csc_for_eval"
+    exit $?
   else
     if test -n "@HAVE_DOTNET_CSC@" || test -n "@HAVE_CSC@"; then
-      test -z "$CSHARP_VERBOSE" || echo csc $options_csc $sources_csc 1>&2
-      exec csc $options_csc $sources_csc
+      test -z "$CSHARP_VERBOSE" || echo "csc $options_csc_for_print $sources_csc_for_print" 1>&2
+      eval "csc $options_csc_for_eval $sources_csc_for_eval"
+      exit $?
     else
       echo 'C# compiler not found, try installing mono or dotnet, then reconfigure' 1>&2
       exit 1
index c2ccd3b64532b7633d59cecd0bbdca21a897c15f..a7a5a277c0915eb1acd7f1d0ca236068a230916b 100644 (file)
@@ -59,7 +59,6 @@ func_tmpdir ()
   }
 }
 
-sed_quote_subst='s/\([|&;<>()$`"'"'"'*?[#~=%   \\]\)/\\\1/g'
 libdirs_mono=
 libdirs_dotnet=
 prog=