xref: /freebsd/contrib/ntp/sntp/libevent/build-aux/ylwrap (revision f0574f5cf69e168cc4ea71ebbe5fdec9ec9a3dfe)
12b15cb3dSCy Schubert#! /bin/sh
22b15cb3dSCy Schubert# ylwrap - wrapper for lex/yacc invocations.
32b15cb3dSCy Schubert
4*f0574f5cSXin LIscriptversion=2013-01-12.17; # UTC
52b15cb3dSCy Schubert
6*f0574f5cSXin LI# Copyright (C) 1996-2014 Free Software Foundation, Inc.
72b15cb3dSCy Schubert#
82b15cb3dSCy Schubert# Written by Tom Tromey <tromey@cygnus.com>.
92b15cb3dSCy Schubert#
102b15cb3dSCy Schubert# This program is free software; you can redistribute it and/or modify
112b15cb3dSCy Schubert# it under the terms of the GNU General Public License as published by
122b15cb3dSCy Schubert# the Free Software Foundation; either version 2, or (at your option)
132b15cb3dSCy Schubert# any later version.
142b15cb3dSCy Schubert#
152b15cb3dSCy Schubert# This program is distributed in the hope that it will be useful,
162b15cb3dSCy Schubert# but WITHOUT ANY WARRANTY; without even the implied warranty of
172b15cb3dSCy Schubert# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
182b15cb3dSCy Schubert# GNU General Public License for more details.
192b15cb3dSCy Schubert#
202b15cb3dSCy Schubert# You should have received a copy of the GNU General Public License
212b15cb3dSCy Schubert# along with this program.  If not, see <http://www.gnu.org/licenses/>.
222b15cb3dSCy Schubert
232b15cb3dSCy Schubert# As a special exception to the GNU General Public License, if you
242b15cb3dSCy Schubert# distribute this file as part of a program that contains a
252b15cb3dSCy Schubert# configuration script generated by Autoconf, you may include it under
262b15cb3dSCy Schubert# the same distribution terms that you use for the rest of that program.
272b15cb3dSCy Schubert
282b15cb3dSCy Schubert# This file is maintained in Automake, please report
292b15cb3dSCy Schubert# bugs to <bug-automake@gnu.org> or send patches to
302b15cb3dSCy Schubert# <automake-patches@gnu.org>.
312b15cb3dSCy Schubert
32*f0574f5cSXin LIget_dirname ()
33*f0574f5cSXin LI{
34*f0574f5cSXin LI  case $1 in
35*f0574f5cSXin LI    */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36*f0574f5cSXin LI    # Otherwise,  we want the empty string (not ".").
37*f0574f5cSXin LI  esac
38*f0574f5cSXin LI}
39*f0574f5cSXin LI
40*f0574f5cSXin LI# guard FILE
41*f0574f5cSXin LI# ----------
42*f0574f5cSXin LI# The CPP macro used to guard inclusion of FILE.
43*f0574f5cSXin LIguard ()
44*f0574f5cSXin LI{
45*f0574f5cSXin LI  printf '%s\n' "$1"                                                    \
46*f0574f5cSXin LI    | sed                                                               \
47*f0574f5cSXin LI        -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'   \
48*f0574f5cSXin LI        -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'                        \
49*f0574f5cSXin LI        -e 's/__*/_/g'
50*f0574f5cSXin LI}
51*f0574f5cSXin LI
52*f0574f5cSXin LI# quote_for_sed [STRING]
53*f0574f5cSXin LI# ----------------------
54*f0574f5cSXin LI# Return STRING (or stdin) quoted to be used as a sed pattern.
55*f0574f5cSXin LIquote_for_sed ()
56*f0574f5cSXin LI{
57*f0574f5cSXin LI  case $# in
58*f0574f5cSXin LI    0) cat;;
59*f0574f5cSXin LI    1) printf '%s\n' "$1";;
60*f0574f5cSXin LI  esac \
61*f0574f5cSXin LI    | sed -e 's|[][\\.*]|\\&|g'
62*f0574f5cSXin LI}
63*f0574f5cSXin LI
642b15cb3dSCy Schubertcase "$1" in
652b15cb3dSCy Schubert  '')
66*f0574f5cSXin LI    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
672b15cb3dSCy Schubert    exit 1
682b15cb3dSCy Schubert    ;;
692b15cb3dSCy Schubert  --basedir)
702b15cb3dSCy Schubert    basedir=$2
712b15cb3dSCy Schubert    shift 2
722b15cb3dSCy Schubert    ;;
732b15cb3dSCy Schubert  -h|--h*)
742b15cb3dSCy Schubert    cat <<\EOF
752b15cb3dSCy SchubertUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
762b15cb3dSCy Schubert
772b15cb3dSCy SchubertWrapper for lex/yacc invocations, renaming files as desired.
782b15cb3dSCy Schubert
792b15cb3dSCy Schubert  INPUT is the input file
802b15cb3dSCy Schubert  OUTPUT is one file PROG generates
812b15cb3dSCy Schubert  DESIRED is the file we actually want instead of OUTPUT
822b15cb3dSCy Schubert  PROGRAM is program to run
832b15cb3dSCy Schubert  ARGS are passed to PROG
842b15cb3dSCy Schubert
852b15cb3dSCy SchubertAny number of OUTPUT,DESIRED pairs may be used.
862b15cb3dSCy Schubert
872b15cb3dSCy SchubertReport bugs to <bug-automake@gnu.org>.
882b15cb3dSCy SchubertEOF
892b15cb3dSCy Schubert    exit $?
902b15cb3dSCy Schubert    ;;
912b15cb3dSCy Schubert  -v|--v*)
922b15cb3dSCy Schubert    echo "ylwrap $scriptversion"
932b15cb3dSCy Schubert    exit $?
942b15cb3dSCy Schubert    ;;
952b15cb3dSCy Schubertesac
962b15cb3dSCy Schubert
972b15cb3dSCy Schubert
982b15cb3dSCy Schubert# The input.
99*f0574f5cSXin LIinput=$1
1002b15cb3dSCy Schubertshift
101*f0574f5cSXin LI# We'll later need for a correct munging of "#line" directives.
102*f0574f5cSXin LIinput_sub_rx=`get_dirname "$input" | quote_for_sed`
103*f0574f5cSXin LIcase $input in
1042b15cb3dSCy Schubert  [\\/]* | ?:[\\/]*)
1052b15cb3dSCy Schubert    # Absolute path; do nothing.
1062b15cb3dSCy Schubert    ;;
1072b15cb3dSCy Schubert  *)
1082b15cb3dSCy Schubert    # Relative path.  Make it absolute.
109*f0574f5cSXin LI    input=`pwd`/$input
1102b15cb3dSCy Schubert    ;;
1112b15cb3dSCy Schubertesac
112*f0574f5cSXin LIinput_rx=`get_dirname "$input" | quote_for_sed`
1132b15cb3dSCy Schubert
114*f0574f5cSXin LI# Since DOS filename conventions don't allow two dots,
115*f0574f5cSXin LI# the DOS version of Bison writes out y_tab.c instead of y.tab.c
116*f0574f5cSXin LI# and y_tab.h instead of y.tab.h. Test to see if this is the case.
117*f0574f5cSXin LIy_tab_nodot=false
118*f0574f5cSXin LIif test -f y_tab.c || test -f y_tab.h; then
119*f0574f5cSXin LI  y_tab_nodot=true
120*f0574f5cSXin LIfi
121*f0574f5cSXin LI
122*f0574f5cSXin LI# The parser itself, the first file, is the destination of the .y.c
123*f0574f5cSXin LI# rule in the Makefile.
124*f0574f5cSXin LIparser=$1
125*f0574f5cSXin LI
126*f0574f5cSXin LI# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
127*f0574f5cSXin LI# instance, we rename #include "y.tab.h" into #include "parse.h"
128*f0574f5cSXin LI# during the conversion from y.tab.c to parse.c.
129*f0574f5cSXin LIsed_fix_filenames=
130*f0574f5cSXin LI
131*f0574f5cSXin LI# Also rename header guards, as Bison 2.7 for instance uses its header
132*f0574f5cSXin LI# guard in its implementation file.
133*f0574f5cSXin LIsed_fix_header_guards=
134*f0574f5cSXin LI
135*f0574f5cSXin LIwhile test $# -ne 0; do
136*f0574f5cSXin LI  if test x"$1" = x"--"; then
1372b15cb3dSCy Schubert    shift
1382b15cb3dSCy Schubert    break
1392b15cb3dSCy Schubert  fi
140*f0574f5cSXin LI  from=$1
141*f0574f5cSXin LI  # Handle y_tab.c and y_tab.h output by DOS
142*f0574f5cSXin LI  if $y_tab_nodot; then
143*f0574f5cSXin LI    case $from in
144*f0574f5cSXin LI      "y.tab.c") from=y_tab.c;;
145*f0574f5cSXin LI      "y.tab.h") from=y_tab.h;;
146*f0574f5cSXin LI    esac
147*f0574f5cSXin LI  fi
1482b15cb3dSCy Schubert  shift
149*f0574f5cSXin LI  to=$1
150*f0574f5cSXin LI  shift
151*f0574f5cSXin LI  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
152*f0574f5cSXin LI  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
1532b15cb3dSCy Schubertdone
1542b15cb3dSCy Schubert
1552b15cb3dSCy Schubert# The program to run.
156*f0574f5cSXin LIprog=$1
1572b15cb3dSCy Schubertshift
1582b15cb3dSCy Schubert# Make any relative path in $prog absolute.
159*f0574f5cSXin LIcase $prog in
1602b15cb3dSCy Schubert  [\\/]* | ?:[\\/]*) ;;
161*f0574f5cSXin LI  *[\\/]*) prog=`pwd`/$prog ;;
1622b15cb3dSCy Schubertesac
1632b15cb3dSCy Schubert
1642b15cb3dSCy Schubertdirname=ylwrap$$
165*f0574f5cSXin LIdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
166*f0574f5cSXin LItrap "ret=129; $do_exit" 1
167*f0574f5cSXin LItrap "ret=130; $do_exit" 2
168*f0574f5cSXin LItrap "ret=141; $do_exit" 13
169*f0574f5cSXin LItrap "ret=143; $do_exit" 15
1702b15cb3dSCy Schubertmkdir $dirname || exit 1
1712b15cb3dSCy Schubert
1722b15cb3dSCy Schubertcd $dirname
1732b15cb3dSCy Schubert
1742b15cb3dSCy Schubertcase $# in
1752b15cb3dSCy Schubert  0) "$prog" "$input" ;;
1762b15cb3dSCy Schubert  *) "$prog" "$@" "$input" ;;
1772b15cb3dSCy Schubertesac
1782b15cb3dSCy Schubertret=$?
1792b15cb3dSCy Schubert
1802b15cb3dSCy Schubertif test $ret -eq 0; then
181*f0574f5cSXin LI  for from in *
182*f0574f5cSXin LI  do
183*f0574f5cSXin LI    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
1842b15cb3dSCy Schubert    if test -f "$from"; then
1852b15cb3dSCy Schubert      # If $2 is an absolute path name, then just use that,
186*f0574f5cSXin LI      # otherwise prepend '../'.
187*f0574f5cSXin LI      case $to in
188*f0574f5cSXin LI        [\\/]* | ?:[\\/]*) target=$to;;
189*f0574f5cSXin LI        *) target=../$to;;
1902b15cb3dSCy Schubert      esac
1912b15cb3dSCy Schubert
192*f0574f5cSXin LI      # Do not overwrite unchanged header files to avoid useless
193*f0574f5cSXin LI      # recompilations.  Always update the parser itself: it is the
194*f0574f5cSXin LI      # destination of the .y.c rule in the Makefile.  Divert the
195*f0574f5cSXin LI      # output of all other files to a temporary file so we can
196*f0574f5cSXin LI      # compare them to existing versions.
197*f0574f5cSXin LI      if test $from != $parser; then
198*f0574f5cSXin LI        realtarget=$target
199*f0574f5cSXin LI        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
2002b15cb3dSCy Schubert      fi
2012b15cb3dSCy Schubert
202*f0574f5cSXin LI      # Munge "#line" or "#" directives.  Don't let the resulting
203*f0574f5cSXin LI      # debug information point at an absolute srcdir.  Use the real
204*f0574f5cSXin LI      # output file name, not yy.lex.c for instance.  Adjust the
205*f0574f5cSXin LI      # include guards too.
206*f0574f5cSXin LI      sed -e "/^#/!b"                           \
207*f0574f5cSXin LI          -e "s|$input_rx|$input_sub_rx|"       \
208*f0574f5cSXin LI          -e "$sed_fix_filenames"               \
209*f0574f5cSXin LI          -e "$sed_fix_header_guards"           \
210*f0574f5cSXin LI        "$from" >"$target" || ret=$?
2112b15cb3dSCy Schubert
212*f0574f5cSXin LI      # Check whether files must be updated.
213*f0574f5cSXin LI      if test "$from" != "$parser"; then
2142b15cb3dSCy Schubert        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
215*f0574f5cSXin LI          echo "$to is unchanged"
2162b15cb3dSCy Schubert          rm -f "$target"
2172b15cb3dSCy Schubert        else
218*f0574f5cSXin LI          echo "updating $to"
2192b15cb3dSCy Schubert          mv -f "$target" "$realtarget"
2202b15cb3dSCy Schubert        fi
2212b15cb3dSCy Schubert      fi
2222b15cb3dSCy Schubert    else
223*f0574f5cSXin LI      # A missing file is only an error for the parser.  This is a
224*f0574f5cSXin LI      # blatant hack to let us support using "yacc -d".  If -d is not
225*f0574f5cSXin LI      # specified, don't fail when the header file is "missing".
226*f0574f5cSXin LI      if test "$from" = "$parser"; then
2272b15cb3dSCy Schubert        ret=1
2282b15cb3dSCy Schubert      fi
2292b15cb3dSCy Schubert    fi
2302b15cb3dSCy Schubert  done
2312b15cb3dSCy Schubertfi
2322b15cb3dSCy Schubert
2332b15cb3dSCy Schubert# Remove the directory.
2342b15cb3dSCy Schubertcd ..
2352b15cb3dSCy Schubertrm -rf $dirname
2362b15cb3dSCy Schubert
2372b15cb3dSCy Schubertexit $ret
2382b15cb3dSCy Schubert
2392b15cb3dSCy Schubert# Local Variables:
2402b15cb3dSCy Schubert# mode: shell-script
2412b15cb3dSCy Schubert# sh-indentation: 2
2422b15cb3dSCy Schubert# eval: (add-hook 'write-file-hooks 'time-stamp)
2432b15cb3dSCy Schubert# time-stamp-start: "scriptversion="
2442b15cb3dSCy Schubert# time-stamp-format: "%:y-%02m-%02d.%02H"
2452b15cb3dSCy Schubert# time-stamp-time-zone: "UTC"
2462b15cb3dSCy Schubert# time-stamp-end: "; # UTC"
2472b15cb3dSCy Schubert# End:
248