xref: /netbsd-src/external/bsd/ntp/dist/sntp/libevent/build-aux/ylwrap (revision 3e3909fe3ffe5d33a1ab87329085041d20fd552e)
18585484eSchristos#! /bin/sh
28585484eSchristos# ylwrap - wrapper for lex/yacc invocations.
38585484eSchristos
4*3e3909feSchristosscriptversion=2013-01-12.17; # UTC
58585484eSchristos
6*3e3909feSchristos# Copyright (C) 1996-2014 Free Software Foundation, Inc.
78585484eSchristos#
88585484eSchristos# Written by Tom Tromey <tromey@cygnus.com>.
98585484eSchristos#
108585484eSchristos# This program is free software; you can redistribute it and/or modify
118585484eSchristos# it under the terms of the GNU General Public License as published by
128585484eSchristos# the Free Software Foundation; either version 2, or (at your option)
138585484eSchristos# any later version.
148585484eSchristos#
158585484eSchristos# This program is distributed in the hope that it will be useful,
168585484eSchristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
178585484eSchristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
188585484eSchristos# GNU General Public License for more details.
198585484eSchristos#
208585484eSchristos# You should have received a copy of the GNU General Public License
218585484eSchristos# along with this program.  If not, see <http://www.gnu.org/licenses/>.
228585484eSchristos
238585484eSchristos# As a special exception to the GNU General Public License, if you
248585484eSchristos# distribute this file as part of a program that contains a
258585484eSchristos# configuration script generated by Autoconf, you may include it under
268585484eSchristos# the same distribution terms that you use for the rest of that program.
278585484eSchristos
288585484eSchristos# This file is maintained in Automake, please report
298585484eSchristos# bugs to <bug-automake@gnu.org> or send patches to
308585484eSchristos# <automake-patches@gnu.org>.
318585484eSchristos
32*3e3909feSchristosget_dirname ()
33*3e3909feSchristos{
34*3e3909feSchristos  case $1 in
35*3e3909feSchristos    */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36*3e3909feSchristos    # Otherwise,  we want the empty string (not ".").
37*3e3909feSchristos  esac
38*3e3909feSchristos}
39*3e3909feSchristos
40*3e3909feSchristos# guard FILE
41*3e3909feSchristos# ----------
42*3e3909feSchristos# The CPP macro used to guard inclusion of FILE.
43*3e3909feSchristosguard ()
44*3e3909feSchristos{
45*3e3909feSchristos  printf '%s\n' "$1"                                                    \
46*3e3909feSchristos    | sed                                                               \
47*3e3909feSchristos        -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'   \
48*3e3909feSchristos        -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'                        \
49*3e3909feSchristos        -e 's/__*/_/g'
50*3e3909feSchristos}
51*3e3909feSchristos
52*3e3909feSchristos# quote_for_sed [STRING]
53*3e3909feSchristos# ----------------------
54*3e3909feSchristos# Return STRING (or stdin) quoted to be used as a sed pattern.
55*3e3909feSchristosquote_for_sed ()
56*3e3909feSchristos{
57*3e3909feSchristos  case $# in
58*3e3909feSchristos    0) cat;;
59*3e3909feSchristos    1) printf '%s\n' "$1";;
60*3e3909feSchristos  esac \
61*3e3909feSchristos    | sed -e 's|[][\\.*]|\\&|g'
62*3e3909feSchristos}
63*3e3909feSchristos
648585484eSchristoscase "$1" in
658585484eSchristos  '')
66*3e3909feSchristos    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
678585484eSchristos    exit 1
688585484eSchristos    ;;
698585484eSchristos  --basedir)
708585484eSchristos    basedir=$2
718585484eSchristos    shift 2
728585484eSchristos    ;;
738585484eSchristos  -h|--h*)
748585484eSchristos    cat <<\EOF
758585484eSchristosUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
768585484eSchristos
778585484eSchristosWrapper for lex/yacc invocations, renaming files as desired.
788585484eSchristos
798585484eSchristos  INPUT is the input file
808585484eSchristos  OUTPUT is one file PROG generates
818585484eSchristos  DESIRED is the file we actually want instead of OUTPUT
828585484eSchristos  PROGRAM is program to run
838585484eSchristos  ARGS are passed to PROG
848585484eSchristos
858585484eSchristosAny number of OUTPUT,DESIRED pairs may be used.
868585484eSchristos
878585484eSchristosReport bugs to <bug-automake@gnu.org>.
888585484eSchristosEOF
898585484eSchristos    exit $?
908585484eSchristos    ;;
918585484eSchristos  -v|--v*)
928585484eSchristos    echo "ylwrap $scriptversion"
938585484eSchristos    exit $?
948585484eSchristos    ;;
958585484eSchristosesac
968585484eSchristos
978585484eSchristos
988585484eSchristos# The input.
99*3e3909feSchristosinput=$1
1008585484eSchristosshift
101*3e3909feSchristos# We'll later need for a correct munging of "#line" directives.
102*3e3909feSchristosinput_sub_rx=`get_dirname "$input" | quote_for_sed`
103*3e3909feSchristoscase $input in
1048585484eSchristos  [\\/]* | ?:[\\/]*)
1058585484eSchristos    # Absolute path; do nothing.
1068585484eSchristos    ;;
1078585484eSchristos  *)
1088585484eSchristos    # Relative path.  Make it absolute.
109*3e3909feSchristos    input=`pwd`/$input
1108585484eSchristos    ;;
1118585484eSchristosesac
112*3e3909feSchristosinput_rx=`get_dirname "$input" | quote_for_sed`
1138585484eSchristos
114*3e3909feSchristos# Since DOS filename conventions don't allow two dots,
115*3e3909feSchristos# the DOS version of Bison writes out y_tab.c instead of y.tab.c
116*3e3909feSchristos# and y_tab.h instead of y.tab.h. Test to see if this is the case.
117*3e3909feSchristosy_tab_nodot=false
118*3e3909feSchristosif test -f y_tab.c || test -f y_tab.h; then
119*3e3909feSchristos  y_tab_nodot=true
120*3e3909feSchristosfi
121*3e3909feSchristos
122*3e3909feSchristos# The parser itself, the first file, is the destination of the .y.c
123*3e3909feSchristos# rule in the Makefile.
124*3e3909feSchristosparser=$1
125*3e3909feSchristos
126*3e3909feSchristos# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
127*3e3909feSchristos# instance, we rename #include "y.tab.h" into #include "parse.h"
128*3e3909feSchristos# during the conversion from y.tab.c to parse.c.
129*3e3909feSchristossed_fix_filenames=
130*3e3909feSchristos
131*3e3909feSchristos# Also rename header guards, as Bison 2.7 for instance uses its header
132*3e3909feSchristos# guard in its implementation file.
133*3e3909feSchristossed_fix_header_guards=
134*3e3909feSchristos
135*3e3909feSchristoswhile test $# -ne 0; do
136*3e3909feSchristos  if test x"$1" = x"--"; then
1378585484eSchristos    shift
1388585484eSchristos    break
1398585484eSchristos  fi
140*3e3909feSchristos  from=$1
141*3e3909feSchristos  # Handle y_tab.c and y_tab.h output by DOS
142*3e3909feSchristos  if $y_tab_nodot; then
143*3e3909feSchristos    case $from in
144*3e3909feSchristos      "y.tab.c") from=y_tab.c;;
145*3e3909feSchristos      "y.tab.h") from=y_tab.h;;
146*3e3909feSchristos    esac
147*3e3909feSchristos  fi
1488585484eSchristos  shift
149*3e3909feSchristos  to=$1
150*3e3909feSchristos  shift
151*3e3909feSchristos  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
152*3e3909feSchristos  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
1538585484eSchristosdone
1548585484eSchristos
1558585484eSchristos# The program to run.
156*3e3909feSchristosprog=$1
1578585484eSchristosshift
1588585484eSchristos# Make any relative path in $prog absolute.
159*3e3909feSchristoscase $prog in
1608585484eSchristos  [\\/]* | ?:[\\/]*) ;;
161*3e3909feSchristos  *[\\/]*) prog=`pwd`/$prog ;;
1628585484eSchristosesac
1638585484eSchristos
1648585484eSchristosdirname=ylwrap$$
165*3e3909feSchristosdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
166*3e3909feSchristostrap "ret=129; $do_exit" 1
167*3e3909feSchristostrap "ret=130; $do_exit" 2
168*3e3909feSchristostrap "ret=141; $do_exit" 13
169*3e3909feSchristostrap "ret=143; $do_exit" 15
1708585484eSchristosmkdir $dirname || exit 1
1718585484eSchristos
1728585484eSchristoscd $dirname
1738585484eSchristos
1748585484eSchristoscase $# in
1758585484eSchristos  0) "$prog" "$input" ;;
1768585484eSchristos  *) "$prog" "$@" "$input" ;;
1778585484eSchristosesac
1788585484eSchristosret=$?
1798585484eSchristos
1808585484eSchristosif test $ret -eq 0; then
181*3e3909feSchristos  for from in *
182*3e3909feSchristos  do
183*3e3909feSchristos    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
184ae49d4a4Schristos    if test -f "$from"; then
185ae49d4a4Schristos      # If $2 is an absolute path name, then just use that,
186*3e3909feSchristos      # otherwise prepend '../'.
187*3e3909feSchristos      case $to in
188*3e3909feSchristos        [\\/]* | ?:[\\/]*) target=$to;;
189*3e3909feSchristos        *) target=../$to;;
190ae49d4a4Schristos      esac
191ae49d4a4Schristos
192*3e3909feSchristos      # Do not overwrite unchanged header files to avoid useless
193*3e3909feSchristos      # recompilations.  Always update the parser itself: it is the
194*3e3909feSchristos      # destination of the .y.c rule in the Makefile.  Divert the
195*3e3909feSchristos      # output of all other files to a temporary file so we can
196*3e3909feSchristos      # compare them to existing versions.
197*3e3909feSchristos      if test $from != $parser; then
198*3e3909feSchristos        realtarget=$target
199*3e3909feSchristos        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
200ae49d4a4Schristos      fi
201ae49d4a4Schristos
202*3e3909feSchristos      # Munge "#line" or "#" directives.  Don't let the resulting
203*3e3909feSchristos      # debug information point at an absolute srcdir.  Use the real
204*3e3909feSchristos      # output file name, not yy.lex.c for instance.  Adjust the
205*3e3909feSchristos      # include guards too.
206*3e3909feSchristos      sed -e "/^#/!b"                           \
207*3e3909feSchristos          -e "s|$input_rx|$input_sub_rx|"       \
208*3e3909feSchristos          -e "$sed_fix_filenames"               \
209*3e3909feSchristos          -e "$sed_fix_header_guards"           \
210*3e3909feSchristos        "$from" >"$target" || ret=$?
211ae49d4a4Schristos
212*3e3909feSchristos      # Check whether files must be updated.
213*3e3909feSchristos      if test "$from" != "$parser"; then
2148585484eSchristos        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
215*3e3909feSchristos          echo "$to is unchanged"
2168585484eSchristos          rm -f "$target"
2178585484eSchristos        else
218*3e3909feSchristos          echo "updating $to"
2198585484eSchristos          mv -f "$target" "$realtarget"
2208585484eSchristos        fi
2218585484eSchristos      fi
2228585484eSchristos    else
223*3e3909feSchristos      # A missing file is only an error for the parser.  This is a
224*3e3909feSchristos      # blatant hack to let us support using "yacc -d".  If -d is not
225*3e3909feSchristos      # specified, don't fail when the header file is "missing".
226*3e3909feSchristos      if test "$from" = "$parser"; then
2278585484eSchristos        ret=1
2288585484eSchristos      fi
2298585484eSchristos    fi
2308585484eSchristos  done
2318585484eSchristosfi
2328585484eSchristos
2338585484eSchristos# Remove the directory.
2348585484eSchristoscd ..
2358585484eSchristosrm -rf $dirname
2368585484eSchristos
2378585484eSchristosexit $ret
2388585484eSchristos
2398585484eSchristos# Local Variables:
2408585484eSchristos# mode: shell-script
2418585484eSchristos# sh-indentation: 2
2428585484eSchristos# eval: (add-hook 'write-file-hooks 'time-stamp)
2438585484eSchristos# time-stamp-start: "scriptversion="
2448585484eSchristos# time-stamp-format: "%:y-%02m-%02d.%02H"
2458585484eSchristos# time-stamp-time-zone: "UTC"
2468585484eSchristos# time-stamp-end: "; # UTC"
2478585484eSchristos# End:
248