1#! /bin/sh
2# quote.test - make sure that shell metacharacters do not blow up libtool
3
4# Test script header.
5need_prefix=no
6if test -z "$srcdir"; then
7  srcdir=`echo "$0" | sed 's%/[^/]*$%%'`
8  test "$srcdir" = "$0" && srcdir=.
9  test "${VERBOSE+set}" != "set" && VERBOSE=yes
10fi
11. $srcdir/defs || exit 1
12
13# Do the torture test.
14status=0
15
16echo=echo
17if test "X`($echo '\t') 2>/dev/null`" = 'X\t'; then :
18else
19  # The Solaris, AIX, and Digital Unix default echo programs unquote
20  # backslashes.  This makes it impossible to quote backslashes using
21  #   echo "$something" | sed 's/\\/\\\\/g'
22  #
23  # So, first we look for a working echo in the user's PATH.
24  IFS="${IFS= 	}"; save_ifs="$IFS"; IFS="${IFS}:"
25  for dir in $PATH /usr/ucb; do
26    if test -f $dir/echo && test "X`($dir/echo '\t') 2>/dev/null`" = 'X\t'; then
27      echo="$dir/echo"
28      break
29    fi
30  done
31  IFS="$save_ifs"
32
33  if test "X$echo" = Xecho; then
34    # We didn't find a better echo, so look for alternatives.
35    if test "X`(print -r '\t') 2>/dev/null`" = 'X\t'; then
36      # This shell has a builtin print -r that does the trick.
37      echo='print -r'
38    elif test -f /bin/ksh && test "X$CONFIG_SHELL" != X/bin/ksh; then
39      # If we have ksh, try running $0 again with it.
40      CONFIG_SHELL=/bin/ksh
41      export CONFIG_SHELL
42      exec "$CONFIG_SHELL" "$0" --no-reexec ${1+"$@"}
43    else
44      # Try using printf.
45      echo='printf %s\n'
46      if test "X`($echo '\t') 2>/dev/null`" = 'X\t'; then :
47      else
48        # Oops.  We lost completely, so just stick with echo.
49        echo=echo
50      fi
51    fi
52  fi
53fi
54
55# Extract $wl from the libtool configuration
56eval `$libtool --config | grep '^wl='`
57
58for mode in compile link install; do
59  $echo "== $mode mode"
60
61  # Unfortunately, without an array data type, it is nearly impossible
62  # to protect libtool from metacharacters in filenames.  So, we just
63  # try metacharacters in the options it needs to pass to other programs.
64
65  # preargs and postargs need to go through libtool unmodified.
66  case "$mode" in
67  compile)
68    preargs="$CC -c"
69    preflag=
70    match_preflag=
71    flag="-DVAR="
72    postargs="foo.c"
73    ;;
74
75  link)
76    preargs="$CC -o hell -g -O"
77    preflag=-Wl,
78    match_preflag="$wl"
79    flag="-someflag="
80    postargs="foo.o"
81    ;;
82
83  install)
84    preargs="install -c"
85    preflag=
86    match_preflag=
87    flag="--something="
88    postargs="hell /usr/local/bin/hell"
89    ;;
90  esac
91
92  # Trivial.
93  $echo "= trying: no quoting"
94  result=`$libtool -n --mode=$mode $preargs ${preflag}"${flag}test" $postargs` || status=1
95  # We used to have the contents of $match in the case statement,
96  # without an intermediate variable, but it would fail on at least
97  # Solaris' and HP-UX's /bin/sh.  Ugh!
98  # We must nost attempt to match $preargs in the output, because libtool
99  # may modify them.  For example, on Cygwin, ``libtool --mode=link gcc -o
100  # foo foo.o''  becomes ``gcc -o foo.exe foo.o''.
101  match="${match_preflag}${flag}test "
102  case "$result" in
103  *"$match"*)
104    $echo "= passed: $result"
105    ;;
106  *)
107    $echo "= failed: $result"
108    status=1
109    ;;
110  esac
111
112  # Metacharacters that should be backslashified.
113  for mchar in \\ \" \` \$; do
114    $echo "= trying: \\$mchar quoting"
115    result=`$libtool -n --mode=$mode $preargs ${preflag}"${flag}${mchar}test${mchar}" $postargs` || status=1
116    match="${match_preflag}${flag}\\${mchar}test\\${mchar} "
117    case "$result" in
118    *"$match"*)
119      $echo "= passed: $result"
120      ;;
121    *)
122      $echo "= failed: $result"
123      status=1
124      ;;
125    esac
126  done
127
128  # Metacharacters that should be double quoted.
129  for mchar in "[" "]" "~" "#" "^" "&" "*" "(" ")" "{" "}" "|" ";" "<" ">" "?" \
130      "'" " " "	"; do
131
132    $echo "= trying: \"$mchar\" quoting"
133    result=`$libtool -n --mode=$mode $preargs ${preflag}"${flag}${mchar}test${mchar}" $postargs` || status=1
134    match="${match_preflag}\"${flag}${mchar}test${mchar}\" "
135    case "$result" in
136    *"$match"*)
137      $echo "= passed: $result"
138      ;;
139    *)
140      $echo "= failed: $result"
141      status=1
142      ;;
143    esac
144  done
145done
146
147exit $status
148