xref: /freebsd/contrib/ntp/sntp/libevent/build-aux/ylwrap (revision 2b15cb3d0922bd70ea592f0da9b4a5b167f4d53f)
1*2b15cb3dSCy Schubert#! /bin/sh
2*2b15cb3dSCy Schubert# ylwrap - wrapper for lex/yacc invocations.
3*2b15cb3dSCy Schubert
4*2b15cb3dSCy Schubertscriptversion=2009-04-28.21; # UTC
5*2b15cb3dSCy Schubert
6*2b15cb3dSCy Schubert# Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
7*2b15cb3dSCy Schubert# 2007, 2009 Free Software Foundation, Inc.
8*2b15cb3dSCy Schubert#
9*2b15cb3dSCy Schubert# Written by Tom Tromey <tromey@cygnus.com>.
10*2b15cb3dSCy Schubert#
11*2b15cb3dSCy Schubert# This program is free software; you can redistribute it and/or modify
12*2b15cb3dSCy Schubert# it under the terms of the GNU General Public License as published by
13*2b15cb3dSCy Schubert# the Free Software Foundation; either version 2, or (at your option)
14*2b15cb3dSCy Schubert# any later version.
15*2b15cb3dSCy Schubert#
16*2b15cb3dSCy Schubert# This program is distributed in the hope that it will be useful,
17*2b15cb3dSCy Schubert# but WITHOUT ANY WARRANTY; without even the implied warranty of
18*2b15cb3dSCy Schubert# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19*2b15cb3dSCy Schubert# GNU General Public License for more details.
20*2b15cb3dSCy Schubert#
21*2b15cb3dSCy Schubert# You should have received a copy of the GNU General Public License
22*2b15cb3dSCy Schubert# along with this program.  If not, see <http://www.gnu.org/licenses/>.
23*2b15cb3dSCy Schubert
24*2b15cb3dSCy Schubert# As a special exception to the GNU General Public License, if you
25*2b15cb3dSCy Schubert# distribute this file as part of a program that contains a
26*2b15cb3dSCy Schubert# configuration script generated by Autoconf, you may include it under
27*2b15cb3dSCy Schubert# the same distribution terms that you use for the rest of that program.
28*2b15cb3dSCy Schubert
29*2b15cb3dSCy Schubert# This file is maintained in Automake, please report
30*2b15cb3dSCy Schubert# bugs to <bug-automake@gnu.org> or send patches to
31*2b15cb3dSCy Schubert# <automake-patches@gnu.org>.
32*2b15cb3dSCy Schubert
33*2b15cb3dSCy Schubertcase "$1" in
34*2b15cb3dSCy Schubert  '')
35*2b15cb3dSCy Schubert    echo "$0: No files given.  Try \`$0 --help' for more information." 1>&2
36*2b15cb3dSCy Schubert    exit 1
37*2b15cb3dSCy Schubert    ;;
38*2b15cb3dSCy Schubert  --basedir)
39*2b15cb3dSCy Schubert    basedir=$2
40*2b15cb3dSCy Schubert    shift 2
41*2b15cb3dSCy Schubert    ;;
42*2b15cb3dSCy Schubert  -h|--h*)
43*2b15cb3dSCy Schubert    cat <<\EOF
44*2b15cb3dSCy SchubertUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
45*2b15cb3dSCy Schubert
46*2b15cb3dSCy SchubertWrapper for lex/yacc invocations, renaming files as desired.
47*2b15cb3dSCy Schubert
48*2b15cb3dSCy Schubert  INPUT is the input file
49*2b15cb3dSCy Schubert  OUTPUT is one file PROG generates
50*2b15cb3dSCy Schubert  DESIRED is the file we actually want instead of OUTPUT
51*2b15cb3dSCy Schubert  PROGRAM is program to run
52*2b15cb3dSCy Schubert  ARGS are passed to PROG
53*2b15cb3dSCy Schubert
54*2b15cb3dSCy SchubertAny number of OUTPUT,DESIRED pairs may be used.
55*2b15cb3dSCy Schubert
56*2b15cb3dSCy SchubertReport bugs to <bug-automake@gnu.org>.
57*2b15cb3dSCy SchubertEOF
58*2b15cb3dSCy Schubert    exit $?
59*2b15cb3dSCy Schubert    ;;
60*2b15cb3dSCy Schubert  -v|--v*)
61*2b15cb3dSCy Schubert    echo "ylwrap $scriptversion"
62*2b15cb3dSCy Schubert    exit $?
63*2b15cb3dSCy Schubert    ;;
64*2b15cb3dSCy Schubertesac
65*2b15cb3dSCy Schubert
66*2b15cb3dSCy Schubert
67*2b15cb3dSCy Schubert# The input.
68*2b15cb3dSCy Schubertinput="$1"
69*2b15cb3dSCy Schubertshift
70*2b15cb3dSCy Schubertcase "$input" in
71*2b15cb3dSCy Schubert  [\\/]* | ?:[\\/]*)
72*2b15cb3dSCy Schubert    # Absolute path; do nothing.
73*2b15cb3dSCy Schubert    ;;
74*2b15cb3dSCy Schubert  *)
75*2b15cb3dSCy Schubert    # Relative path.  Make it absolute.
76*2b15cb3dSCy Schubert    input="`pwd`/$input"
77*2b15cb3dSCy Schubert    ;;
78*2b15cb3dSCy Schubertesac
79*2b15cb3dSCy Schubert
80*2b15cb3dSCy Schubertpairlist=
81*2b15cb3dSCy Schubertwhile test "$#" -ne 0; do
82*2b15cb3dSCy Schubert  if test "$1" = "--"; then
83*2b15cb3dSCy Schubert    shift
84*2b15cb3dSCy Schubert    break
85*2b15cb3dSCy Schubert  fi
86*2b15cb3dSCy Schubert  pairlist="$pairlist $1"
87*2b15cb3dSCy Schubert  shift
88*2b15cb3dSCy Schubertdone
89*2b15cb3dSCy Schubert
90*2b15cb3dSCy Schubert# The program to run.
91*2b15cb3dSCy Schubertprog="$1"
92*2b15cb3dSCy Schubertshift
93*2b15cb3dSCy Schubert# Make any relative path in $prog absolute.
94*2b15cb3dSCy Schubertcase "$prog" in
95*2b15cb3dSCy Schubert  [\\/]* | ?:[\\/]*) ;;
96*2b15cb3dSCy Schubert  *[\\/]*) prog="`pwd`/$prog" ;;
97*2b15cb3dSCy Schubertesac
98*2b15cb3dSCy Schubert
99*2b15cb3dSCy Schubert# FIXME: add hostname here for parallel makes that run commands on
100*2b15cb3dSCy Schubert# other machines.  But that might take us over the 14-char limit.
101*2b15cb3dSCy Schubertdirname=ylwrap$$
102*2b15cb3dSCy Schuberttrap "cd '`pwd`'; rm -rf $dirname > /dev/null 2>&1" 1 2 3 15
103*2b15cb3dSCy Schubertmkdir $dirname || exit 1
104*2b15cb3dSCy Schubert
105*2b15cb3dSCy Schubertcd $dirname
106*2b15cb3dSCy Schubert
107*2b15cb3dSCy Schubertcase $# in
108*2b15cb3dSCy Schubert  0) "$prog" "$input" ;;
109*2b15cb3dSCy Schubert  *) "$prog" "$@" "$input" ;;
110*2b15cb3dSCy Schubertesac
111*2b15cb3dSCy Schubertret=$?
112*2b15cb3dSCy Schubert
113*2b15cb3dSCy Schubertif test $ret -eq 0; then
114*2b15cb3dSCy Schubert  set X $pairlist
115*2b15cb3dSCy Schubert  shift
116*2b15cb3dSCy Schubert  first=yes
117*2b15cb3dSCy Schubert  # Since DOS filename conventions don't allow two dots,
118*2b15cb3dSCy Schubert  # the DOS version of Bison writes out y_tab.c instead of y.tab.c
119*2b15cb3dSCy Schubert  # and y_tab.h instead of y.tab.h. Test to see if this is the case.
120*2b15cb3dSCy Schubert  y_tab_nodot="no"
121*2b15cb3dSCy Schubert  if test -f y_tab.c || test -f y_tab.h; then
122*2b15cb3dSCy Schubert    y_tab_nodot="yes"
123*2b15cb3dSCy Schubert  fi
124*2b15cb3dSCy Schubert
125*2b15cb3dSCy Schubert  # The directory holding the input.
126*2b15cb3dSCy Schubert  input_dir=`echo "$input" | sed -e 's,\([\\/]\)[^\\/]*$,\1,'`
127*2b15cb3dSCy Schubert  # Quote $INPUT_DIR so we can use it in a regexp.
128*2b15cb3dSCy Schubert  # FIXME: really we should care about more than `.' and `\'.
129*2b15cb3dSCy Schubert  input_rx=`echo "$input_dir" | sed 's,\\\\,\\\\\\\\,g;s,\\.,\\\\.,g'`
130*2b15cb3dSCy Schubert
131*2b15cb3dSCy Schubert  while test "$#" -ne 0; do
132*2b15cb3dSCy Schubert    from="$1"
133*2b15cb3dSCy Schubert    # Handle y_tab.c and y_tab.h output by DOS
134*2b15cb3dSCy Schubert    if test $y_tab_nodot = "yes"; then
135*2b15cb3dSCy Schubert      if test $from = "y.tab.c"; then
136*2b15cb3dSCy Schubert    	from="y_tab.c"
137*2b15cb3dSCy Schubert      else
138*2b15cb3dSCy Schubert    	if test $from = "y.tab.h"; then
139*2b15cb3dSCy Schubert    	  from="y_tab.h"
140*2b15cb3dSCy Schubert    	fi
141*2b15cb3dSCy Schubert      fi
142*2b15cb3dSCy Schubert    fi
143*2b15cb3dSCy Schubert    if test -f "$from"; then
144*2b15cb3dSCy Schubert      # If $2 is an absolute path name, then just use that,
145*2b15cb3dSCy Schubert      # otherwise prepend `../'.
146*2b15cb3dSCy Schubert      case "$2" in
147*2b15cb3dSCy Schubert    	[\\/]* | ?:[\\/]*) target="$2";;
148*2b15cb3dSCy Schubert    	*) target="../$2";;
149*2b15cb3dSCy Schubert      esac
150*2b15cb3dSCy Schubert
151*2b15cb3dSCy Schubert      # We do not want to overwrite a header file if it hasn't
152*2b15cb3dSCy Schubert      # changed.  This avoid useless recompilations.  However the
153*2b15cb3dSCy Schubert      # parser itself (the first file) should always be updated,
154*2b15cb3dSCy Schubert      # because it is the destination of the .y.c rule in the
155*2b15cb3dSCy Schubert      # Makefile.  Divert the output of all other files to a temporary
156*2b15cb3dSCy Schubert      # file so we can compare them to existing versions.
157*2b15cb3dSCy Schubert      if test $first = no; then
158*2b15cb3dSCy Schubert	realtarget="$target"
159*2b15cb3dSCy Schubert	target="tmp-`echo $target | sed s/.*[\\/]//g`"
160*2b15cb3dSCy Schubert      fi
161*2b15cb3dSCy Schubert      # Edit out `#line' or `#' directives.
162*2b15cb3dSCy Schubert      #
163*2b15cb3dSCy Schubert      # We don't want the resulting debug information to point at
164*2b15cb3dSCy Schubert      # an absolute srcdir; it is better for it to just mention the
165*2b15cb3dSCy Schubert      # .y file with no path.
166*2b15cb3dSCy Schubert      #
167*2b15cb3dSCy Schubert      # We want to use the real output file name, not yy.lex.c for
168*2b15cb3dSCy Schubert      # instance.
169*2b15cb3dSCy Schubert      #
170*2b15cb3dSCy Schubert      # We want the include guards to be adjusted too.
171*2b15cb3dSCy Schubert      FROM=`echo "$from" | sed \
172*2b15cb3dSCy Schubert            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
173*2b15cb3dSCy Schubert            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
174*2b15cb3dSCy Schubert      TARGET=`echo "$2" | sed \
175*2b15cb3dSCy Schubert            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
176*2b15cb3dSCy Schubert            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
177*2b15cb3dSCy Schubert
178*2b15cb3dSCy Schubert      sed -e "/^#/!b" -e "s,$input_rx,," -e "s,$from,$2," \
179*2b15cb3dSCy Schubert          -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
180*2b15cb3dSCy Schubert
181*2b15cb3dSCy Schubert      # Check whether header files must be updated.
182*2b15cb3dSCy Schubert      if test $first = no; then
183*2b15cb3dSCy Schubert	if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
184*2b15cb3dSCy Schubert	  echo "$2" is unchanged
185*2b15cb3dSCy Schubert	  rm -f "$target"
186*2b15cb3dSCy Schubert	else
187*2b15cb3dSCy Schubert          echo updating "$2"
188*2b15cb3dSCy Schubert          mv -f "$target" "$realtarget"
189*2b15cb3dSCy Schubert        fi
190*2b15cb3dSCy Schubert      fi
191*2b15cb3dSCy Schubert    else
192*2b15cb3dSCy Schubert      # A missing file is only an error for the first file.  This
193*2b15cb3dSCy Schubert      # is a blatant hack to let us support using "yacc -d".  If -d
194*2b15cb3dSCy Schubert      # is not specified, we don't want an error when the header
195*2b15cb3dSCy Schubert      # file is "missing".
196*2b15cb3dSCy Schubert      if test $first = yes; then
197*2b15cb3dSCy Schubert        ret=1
198*2b15cb3dSCy Schubert      fi
199*2b15cb3dSCy Schubert    fi
200*2b15cb3dSCy Schubert    shift
201*2b15cb3dSCy Schubert    shift
202*2b15cb3dSCy Schubert    first=no
203*2b15cb3dSCy Schubert  done
204*2b15cb3dSCy Schubertelse
205*2b15cb3dSCy Schubert  ret=$?
206*2b15cb3dSCy Schubertfi
207*2b15cb3dSCy Schubert
208*2b15cb3dSCy Schubert# Remove the directory.
209*2b15cb3dSCy Schubertcd ..
210*2b15cb3dSCy Schubertrm -rf $dirname
211*2b15cb3dSCy Schubert
212*2b15cb3dSCy Schubertexit $ret
213*2b15cb3dSCy Schubert
214*2b15cb3dSCy Schubert# Local Variables:
215*2b15cb3dSCy Schubert# mode: shell-script
216*2b15cb3dSCy Schubert# sh-indentation: 2
217*2b15cb3dSCy Schubert# eval: (add-hook 'write-file-hooks 'time-stamp)
218*2b15cb3dSCy Schubert# time-stamp-start: "scriptversion="
219*2b15cb3dSCy Schubert# time-stamp-format: "%:y-%02m-%02d.%02H"
220*2b15cb3dSCy Schubert# time-stamp-time-zone: "UTC"
221*2b15cb3dSCy Schubert# time-stamp-end: "; # UTC"
222*2b15cb3dSCy Schubert# End:
223