Copy --version support from gnulib-tool to posix-modules.
[gnulib.git] / posix-modules
1 #!/bin/sh
2 #
3 # Copyright (C) 2002-2008 Free Software Foundation, Inc.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 progname=$0
20 package=gnulib
21
22 # func_usage
23 # outputs to stdout the --help usage message.
24 func_usage ()
25 {
26   echo "\
27 Usage: posix-modules
28
29 Report bugs to <bug-gnulib@gnu.org>."
30 }
31
32 # func_version
33 # outputs to stdout the --version message.
34 func_version ()
35 {
36   func_gnulib_dir
37   if test -d "$gnulib_dir"/.git \
38      && (git --version) >/dev/null 2>/dev/null \
39      && (date --version) >/dev/null 2>/dev/null; then
40     # gnulib checked out from git.
41     sed_extract_first_date='/^Date/{
42 s/^Date:[        ]*//p
43 q
44 }'
45     date=`cd "$gnulib_dir" && git log ChangeLog | sed -n -e "$sed_extract_first_date"`
46     # Turn "Fri Mar 21 07:16:51 2008 -0600" into "Mar 21 2008 07:16:51 -0600".
47     sed_year_before_time='s/^[^ ]* \([^ ]*\) \([0-9]*\) \([0-9:]*\) \([0-9]*\) /\1 \2 \4 \3 /'
48     date=`echo "$date" | sed -e "$sed_year_before_time"`
49     # Use GNU date to compute the time in GMT.
50     date=`date -d "$date" -u +"%Y-%m-%d %H:%M:%S"`
51     version=' '`cd "$gnulib_dir" && ./build-aux/git-version-gen /dev/null | sed -e 's/-dirty/-modified/'`
52   else
53     if test -d "$gnulib_dir"/CVS \
54        && (cvs --version) >/dev/null 2>/dev/null; then
55       # gnulib checked out from CVS.
56       sed_extract_first_date='/^date: /{
57 s/^date: \([0-9][0-9][0-9][0-9]\).\([0-9][0-9]\).\([0-9][0-9]\) \([0-9][0-9]:[0-9][0-9]:[0-9][0-9]\).*/\1-\2-\3 \4/p
58 q
59 }'
60       date=`cd "$gnulib_dir" && cvs log -N ChangeLog 2>/dev/null | sed -n -e "$sed_extract_first_date"`
61     else
62       # gnulib copy without versioning information.
63       date=`sed -e 's/ .*//;q' "$gnulib_dir"/ChangeLog`
64     fi
65     version=
66   fi
67   year=`"$gnulib_dir"/build-aux/mdate-sh "$self_abspathname" | sed 's,^.* ,,'`
68   echo "\
69 gnulib-tool (GNU $package $date)$version
70 Copyright (C) $year Free Software Foundation, Inc.
71 License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
72 This is free software: you are free to change and redistribute it.
73 There is NO WARRANTY, to the extent permitted by law.
74
75 Written by" "Bruno Haible"
76 }
77
78 # func_exit STATUS
79 # exits with a given status.
80 # This function needs to be used, rather than 'exit', when a 'trap' handler is
81 # in effect that refers to $?.
82 func_exit ()
83 {
84   (exit $1); exit $1
85 }
86
87 # func_gnulib_dir
88 # locates the directory where the gnulib repository lives
89 # Sets variables
90 # - self_abspathname         absolute pathname of gnulib-tool
91 # - gnulib_dir               absolute pathname of gnulib repository
92 func_gnulib_dir ()
93 {
94   case "$0" in
95     /*) self_abspathname="$0" ;;
96     */*) self_abspathname=`pwd`/"$0" ;;
97     *)
98       # Look in $PATH.
99       # Iterate through the elements of $PATH.
100       # We use IFS=: instead of
101       #   for d in `echo ":$PATH:" | sed -e 's/:::*/:.:/g' | sed -e 's/:/ /g'`
102       # because the latter does not work when some PATH element contains spaces.
103       # We use a canonicalized $pathx instead of $PATH, because empty PATH
104       # elements are by definition equivalent to '.', however field splitting
105       # according to IFS=: loses empty fields in many shells:
106       #   - /bin/sh on OSF/1 and Solaris loses all empty fields (at the
107       #     beginning, at the end, and in the middle),
108       #   - /bin/sh on IRIX and /bin/ksh on IRIX and OSF/1 lose empty fields
109       #     at the beginning and at the end,
110       #   - GNU bash, /bin/sh on AIX and HP-UX, and /bin/ksh on AIX, HP-UX,
111       #     Solaris lose empty fields at the end.
112       # The 'case' statement is an optimization, to avoid evaluating the
113       # explicit canonicalization command when $PATH contains no empty fields.
114       self_abspathname=
115       if test "${PATH_SEPARATOR+set}" != set; then
116         func_tmpdir
117         { echo "#! /bin/sh"; echo "exit 0"; } > "$tmp"/conf.sh
118         chmod +x "$tmp"/conf.sh
119         if (PATH="/nonexistent;$tmp"; conf.sh) >/dev/null 2>&1; then
120           PATH_SEPARATOR=';'
121         else
122           PATH_SEPARATOR=:
123         fi
124         rm -rf "$tmp"
125       fi
126       if test "$PATH_SEPARATOR" = ";"; then
127         # On Windows, programs are searched in "." before $PATH.
128         pathx=".;$PATH"
129       else
130         # On Unix, we have to convert empty PATH elements to ".".
131         pathx="$PATH"
132         case :$PATH: in
133           *::*)
134             pathx=`echo ":$PATH:" | sed -e 's/:::*/:.:/g' -e 's/^://' -e 's/:\$//'`
135             ;;
136         esac
137       fi
138       save_IFS="$IFS"
139       IFS="$PATH_SEPARATOR"
140       for d in $pathx; do
141         IFS="$save_IFS"
142         test -z "$d" && d=.
143         if test -x "$d/$0" && test ! -d "$d/$0"; then
144           self_abspathname="$d/$0"
145           break
146         fi
147       done
148       IFS="$save_IFS"
149       if test -z "$self_abspathname"; then
150         func_fatal_error "could not locate the gnulib-tool program - how did you invoke it?"
151       fi
152       ;;
153   esac
154   while test -h "$self_abspathname"; do
155     # Resolve symbolic link.
156     linkval=`func_readlink "$self_abspathname"`
157     test -n "$linkval" || break
158     case "$linkval" in
159       /* ) self_abspathname="$linkval" ;;
160       * ) self_abspathname=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`/"$linkval" ;;
161     esac
162   done
163   gnulib_dir=`echo "$self_abspathname" | sed -e 's,/[^/]*$,,'`
164 }
165
166 # func_tmpdir
167 # creates a temporary directory.
168 # Sets variable
169 # - tmp             pathname of freshly created temporary directory
170 func_tmpdir ()
171 {
172   # Use the environment variable TMPDIR, falling back to /tmp. This allows
173   # users to specify a different temporary directory, for example, if their
174   # /tmp is filled up or too small.
175   : ${TMPDIR=/tmp}
176   {
177     # Use the mktemp program if available. If not available, hide the error
178     # message.
179     tmp=`(umask 077 && mktemp -d "$TMPDIR/glXXXXXX") 2>/dev/null` &&
180     test -n "$tmp" && test -d "$tmp"
181   } ||
182   {
183     # Use a simple mkdir command. It is guaranteed to fail if the directory
184     # already exists.  $RANDOM is bash specific and expands to empty in shells
185     # other than bash, ksh and zsh.  Its use does not increase security;
186     # rather, it minimizes the probability of failure in a very cluttered /tmp
187     # directory.
188     tmp=$TMPDIR/gl$$-$RANDOM
189     (umask 077 && mkdir "$tmp")
190   } ||
191   {
192     echo "$0: cannot create a temporary directory in $TMPDIR" >&2
193     func_exit 1
194   }
195 }
196
197 # func_fatal_error message
198 # outputs to stderr a fatal error message, and terminates the program.
199 func_fatal_error ()
200 {
201   echo "gnulib-tool: *** $1" 1>&2
202   echo "gnulib-tool: *** Stop." 1>&2
203   func_exit 1
204 }
205
206 # func_readlink SYMLINK
207 # outputs the target of the given symlink.
208 if (type -p readlink) > /dev/null 2>&1; then
209   func_readlink ()
210   {
211     # Use the readlink program from GNU coreutils.
212     readlink "$1"
213   }
214 else
215   func_readlink ()
216   {
217     # Use two sed invocations. A single sed -n -e 's,^.* -> \(.*\)$,\1,p'
218     # would do the wrong thing if the link target contains " -> ".
219     LC_ALL=C ls -l "$1" | sed -e 's, -> ,#%%#,' | sed -n -e 's,^.*#%%#\(.*\)$,\1,p'
220   }
221 fi
222
223 # Command-line option processing.
224 while test $# -gt 0; do
225   case "$1" in
226     --help | --hel | --he | --h )
227       func_usage
228       exit $? ;;
229     --version | --versio | --versi | --vers | --ver | --ve | --v )
230       func_version
231       exit $? ;;
232     -* )
233       echo "posix-modules: unknown option $1" 1>&2
234       echo "Try 'posix-modules --help' for more information." 1>&2
235       exit 1 ;;
236     * )
237       echo "posix-modules: too many arguments" 1>&2
238       echo "Try 'posix-modules --help' for more information." 1>&2
239       exit 1 ;;
240   esac
241 done
242
243 (
244   # Get the header modules.
245   LC_ALL=C grep -h '^Gnulib module: ' "$gnulib_dir"/doc/posix-headers/* 2>/dev/null \
246     | sed -e 's,^Gnulib module: ,,'
247   # Get the function modules.
248   LC_ALL=C grep -h '^Gnulib module: ' "$gnulib_dir"/doc/posix-functions/* 2>/dev/null \
249     | sed -e 's,^Gnulib module: ,,'
250   # Then filter out the words "---", ",", "and", "or" and remove *-gnu modules.
251 ) | sed -e 's/,/ /g' | LC_ALL=C sort | LC_ALL=C uniq \
252   | { # Then filter out the words "---", "and", "or" and remove *-gnu modules.
253       tr ' ' '\012' | sed -e '/^---$/d' -e '/^and$/d' -e '/^or$/d' -e '/-gnu$/d'
254     } \
255   | LC_ALL=C sort | LC_ALL=C uniq