summaryrefslogtreecommitdiff
path: root/gnulib-tool
blob: d7b6d3391257f494dc237f34c12a0c3c3ed4be1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#! /bin/sh
#
# Copyright (C) 2002-2024 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#

# This program dispatches among
#   - the shell implementation and
#   - the Python implementation
# of gnulib-tool, according to the environment variable GNULIB_TOOL_IMPL.

# The environment variable GNULIB_TOOL_IMPL can have four possible values:
#   - GNULIB_TOOL_IMPL=sh      chooses the shell implementation.
#   - GNULIB_TOOL_IMPL=py      chooses the Python implementation.
#   - GNULIB_TOOL_IMPL=        chooses the default (currently sh).
#   - GNULIB_TOOL_IMPL=sh+py   runs both implementations and compares the
#                              results.

progname=$0

# func_exit STATUS
# exits with a given status.
# This function needs to be used, rather than 'exit', when a 'trap' handler is
# in effect that refers to $?.
func_exit ()
{
  (exit $1); exit $1
}

# func_fatal_error message
# outputs to stderr a fatal error message, and terminates the program.
# Input:
# - progname                 name of this program
func_fatal_error ()
{
  echo "$progname: *** $1" 1>&2
  echo "$progname: *** Stop." 1>&2
  func_exit 1
}

# func_readlink SYMLINK
# outputs the target of the given symlink.
if (type readlink) > /dev/null 2>&1; then
  func_readlink ()
  {
    # Use the readlink program from GNU coreutils.
    readlink "$1"
  }
else
  func_readlink ()
  {
    # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
    # would do the wrong thing if the link target contains " -> ".
    LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
  }
fi

# func_gnulib_dir
# locates the directory where the gnulib repository lives
# Input:
# - progname                 name of this program
# Sets variables
# - self_abspathname         absolute pathname of gnulib-tool
# - gnulib_dir               absolute pathname of gnulib repository
func_gnulib_dir ()
{
  case "$progname" in
    /* | ?:*) self_abspathname="$progname" ;;
    */*) self_abspathname=`pwd`/"$progname" ;;
    *)
      # Look in $PATH.
      # Iterate through the elements of $PATH.
      # We use IFS=: instead of
      #   for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`
      # because the latter does not work when some PATH element contains spaces.
      # We use a canonicalized $pathx instead of $PATH, because empty PATH
      # elements are by definition equivalent to '.', however field splitting
      # according to IFS=: loses empty fields in many shells:
      #   - /bin/sh on OSF/1 and Solaris loses all empty fields (at the
      #     beginning, at the end, and in the middle),
      #   - /bin/sh on IRIX and /bin/ksh on IRIX and OSF/1 lose empty fields
      #     at the beginning and at the end,
      #   - GNU bash, /bin/sh on AIX and HP-UX, and /bin/ksh on AIX, HP-UX,
      #     Solaris lose empty fields at the end.
      # The 'case' statement is an optimization, to avoid evaluating the
      # explicit canonicalization command when $PATH contains no empty fields.
      self_abspathname=
      if test "$PATH_SEPARATOR" = ";"; then
        # On Windows, programs are searched in "." before $PATH.
        pathx=".;$PATH"
      else
        # On Unix, we have to convert empty PATH elements to ".".
        pathx="$PATH"
        case :$PATH: in
          *::*)
            pathx=`echo ":$PATH:" | sed -e 's/:::*/:.:/g' -e 's/^://' -e 's/:\$//'`
            ;;
        esac
      fi
      saved_IFS="$IFS"
      IFS="$PATH_SEPARATOR"
      for d in $pathx; do
        IFS="$saved_IFS"
        test -z "$d" && d=.
        if test -x "$d/$progname" && test ! -d "$d/$progname"; then
          self_abspathname="$d/$progname"
          break
        fi
      done
      IFS="$saved_IFS"
      if test -z "$self_abspathname"; then
        func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
      fi
      ;;
  esac
  while test -h "$self_abspathname"; do
    # Resolve symbolic link.
    linkval=`func_readlink "$self_abspathname"`
    test -n "$linkval" || break
    case "$linkval" in
      /* | ?:* ) self_abspathname="$linkval" ;;
      * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
    esac
  done
  gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
}

func_gnulib_dir

case "$GNULIB_TOOL_IMPL" in
  '' | sh)
    exec "$gnulib_dir/gnulib-tool.sh" "$@" ;;
  py)
    exec "$gnulib_dir/gnulib-tool.py" "$@" ;;
  sh+py)
    case " $* " in
      *" --import"* | *" --add-import"* | *" --remove-import"* | *" --update"* | *" --copy-file"*)
        # A gnulib-tool invocation that produces files in the current directory.
        # Create a temporary directory in the parent directory.
        tmpdir=`cd .. && pwd`
        {
          # Use the mktemp program if available. If not available, hide the error
          # message.
          tmp=`(umask 077 && mktemp -d "$tmpdir/glpyXXXXXX") 2>/dev/null` &&
          test -n "$tmp" && test -d "$tmp"
        } ||
        {
          # Use a simple mkdir command. It is guaranteed to fail if the directory
          # already exists.
          tmp=$tmpdir/glpy$$
          (umask 077 && mkdir "$tmp")
        } ||
        {
          echo "$progname: cannot create a temporary directory in $tmpdir" >&2
          func_exit 1
        }
        # Copy the current directory into the the temporary directory.
        { tar cf - . | (cd "$tmp" && tar xf -); } ||
        {
          echo "$progname: failed to clone the current directory" >&2
          func_exit 1
        }
        # Execute gnulib-tool.py in the clone directory.
        (cd "$tmp" && "$gnulib_dir/gnulib-tool.py" "$@" >"$tmp-py-out" 2>"$tmp-py-err")
        pyrc=$?
        # Execute gnulib-tool.sh in the current directory.
        "$gnulib_dir/gnulib-tool.sh" "$@" >"$tmp-sh-out" 2>"$tmp-sh-err"
        shrc=$?
        if test $shrc != 0; then
          if test $pyrc = 0; then
            func_fatal_error "gnulib-tool.sh failed but gnulib-tool.py succeeded! Inspect $tmp-sh-err and $tmp-py-err."
          else
            cat "$tmp-sh-out"
            cat "$tmp-sh-err" >&2
            rm -rf "$tmp" "$tmp-sh-out" "$tmp-sh-err" "$tmp-py-out" "$tmp-py-err"
            exit $shrc
          fi
        fi
        if test $pyrc != 0; then
          func_fatal_error "gnulib-tool.sh succeeded but gnulib-tool.py failed! Inspect $tmp/ and $tmp-py-err."
        fi
        # Compare the two results on the file system.
        diff -r -q . "$tmp" >/dev/null ||
          func_fatal_error "gnulib-tool.py produced different files than gnulib-tool.sh! Compare `pwd` and $tmp."
        # Compare the two outputs.
        diff -q "$tmp-sh-out" "$tmp-py-out" >/dev/null ||
          func_fatal_error "gnulib-tool.py produced different output than gnulib-tool.sh! Compare $tmp-sh-out and $tmp-py-out."
        # Same results.
        cat "$tmp-sh-out"
        cat "$tmp-sh-err" >&2
        rm -rf "$tmp" "$tmp-sh-out" "$tmp-sh-err" "$tmp-py-out" "$tmp-py-err"
        exit 0
        ;;
      *" --create-testdir"* | *" --create-megatestdir"*)
        # A gnulib-tool invocation that produces a new directory with files.
        # Extract the --dir value.
        dir=`echo " $* " | sed -n -e 's/^.* --dir=//p' | sed -e 's/ .*//'`
        if test -z "$dir"; then
          dir=`echo " $* " | sed -n -e 's/^.* --dir  *//p' | sed -e 's/ .*//'`
          if test -z "$dir"; then
            func_fatal_error "could not extract --dir value"
          fi
        fi
        # Find another directory name.
        tmp="$dir-glpy$$"
        # Execute gnulib-tool.py, creating a different directory.
        "$gnulib_dir/gnulib-tool.py" "$@" --dir="$tmp" >"$tmp-py-out" 2>"$tmp-py-err"
        pyrc=$?
        # Execute gnulib-tool.sh, creating the intended directory.
        "$gnulib_dir/gnulib-tool.sh" "$@" >"$tmp-sh-out" 2>"$tmp-sh-err"
        shrc=$?
        if test $shrc != 0; then
          if test $pyrc = 0; then
            func_fatal_error "gnulib-tool.sh failed but gnulib-tool.py succeeded! Inspect $tmp-sh-err and $tmp-py-err."
          else
            cat "$tmp-sh-out"
            cat "$tmp-sh-err" >&2
            rm -rf "$tmp" "$tmp-sh-out" "$tmp-sh-err" "$tmp-py-out" "$tmp-py-err"
            exit $shrc
          fi
        fi
        if test $pyrc != 0; then
          func_fatal_error "gnulib-tool.sh succeeded but gnulib-tool.py failed! Inspect $tmp/ and $tmp-py-err."
        fi
        # Compare the two results on the file system.
        diff -r -q "$dir" "$tmp" >/dev/null ||
          func_fatal_error "gnulib-tool.py produced different files than gnulib-tool.sh! Compare $dir and $tmp."
        # Compare the two outputs.
        diff -q "$tmp-sh-out" "$tmp-py-out" >/dev/null ||
          func_fatal_error "gnulib-tool.py produced different output than gnulib-tool.sh! Compare $tmp-sh-out and $tmp-py-out."
        # Same results.
        cat "$tmp-sh-out"
        cat "$tmp-sh-err" >&2
        rm -rf "$tmp" "$tmp-sh-out" "$tmp-sh-err" "$tmp-py-out" "$tmp-py-err"
        exit 0
        ;;
      *)
        # A gnulib-tool invocation that produces only output, no files.
        tmp="glpy$$"
        # Execute gnulib-tool.py.
        "$gnulib_dir/gnulib-tool.py" "$@" >"$tmp-py-out" 2>"$tmp-py-err"
        pyrc=$?
        # Execute gnulib-tool.sh.
        "$gnulib_dir/gnulib-tool.sh" "$@" >"$tmp-sh-out" 2>"$tmp-sh-err"
        shrc=$?
        if test $shrc != 0; then
          if test $pyrc = 0; then
            func_fatal_error "gnulib-tool.sh failed but gnulib-tool.py succeeded! Inspect $tmp-sh-err and $tmp-py-err."
          else
            cat "$tmp-sh-out"
            cat "$tmp-sh-err" >&2
            rm -rf "$tmp-sh-out" "$tmp-sh-err" "$tmp-py-out" "$tmp-py-err"
            exit $shrc
          fi
        fi
        if test $pyrc != 0; then
          func_fatal_error "gnulib-tool.sh succeeded but gnulib-tool.py failed! Inspect $tmp-py-err."
        fi
        # Compare the two outputs.
        diff -q "$tmp-sh-out" "$tmp-py-out" >/dev/null ||
          func_fatal_error "gnulib-tool.py produced different output than gnulib-tool.sh! Compare $tmp-sh-out and $tmp-py-out."
        # Same results.
        cat "$tmp-sh-out"
        cat "$tmp-sh-err" >&2
        rm -rf "$tmp-sh-out" "$tmp-sh-err" "$tmp-py-out" "$tmp-py-err"
        exit 0
        ;;
    esac
    ;;
  *)
    func_fatal_error "invalid value of GNULIB_TOOL_IMPL: $GNULIB_TOOL_IMPL" ;;
esac