xref: /netbsd-src/external/bsd/flex/dist/build-aux/ylwrap (revision 30da1778c39cfb1c029d9367865462f181007762)
1*30da1778Schristos#! /bin/sh
2*30da1778Schristos# ylwrap - wrapper for lex/yacc invocations.
3*30da1778Schristos
4*30da1778Schristosscriptversion=2013-01-12.17; # UTC
5*30da1778Schristos
6*30da1778Schristos# Copyright (C) 1996-2014 Free Software Foundation, Inc.
7*30da1778Schristos#
8*30da1778Schristos# Written by Tom Tromey <tromey@cygnus.com>.
9*30da1778Schristos#
10*30da1778Schristos# This program is free software; you can redistribute it and/or modify
11*30da1778Schristos# it under the terms of the GNU General Public License as published by
12*30da1778Schristos# the Free Software Foundation; either version 2, or (at your option)
13*30da1778Schristos# any later version.
14*30da1778Schristos#
15*30da1778Schristos# This program is distributed in the hope that it will be useful,
16*30da1778Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
17*30da1778Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*30da1778Schristos# GNU General Public License for more details.
19*30da1778Schristos#
20*30da1778Schristos# You should have received a copy of the GNU General Public License
21*30da1778Schristos# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22*30da1778Schristos
23*30da1778Schristos# As a special exception to the GNU General Public License, if you
24*30da1778Schristos# distribute this file as part of a program that contains a
25*30da1778Schristos# configuration script generated by Autoconf, you may include it under
26*30da1778Schristos# the same distribution terms that you use for the rest of that program.
27*30da1778Schristos
28*30da1778Schristos# This file is maintained in Automake, please report
29*30da1778Schristos# bugs to <bug-automake@gnu.org> or send patches to
30*30da1778Schristos# <automake-patches@gnu.org>.
31*30da1778Schristos
32*30da1778Schristosget_dirname ()
33*30da1778Schristos{
34*30da1778Schristos  case $1 in
35*30da1778Schristos    */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36*30da1778Schristos    # Otherwise,  we want the empty string (not ".").
37*30da1778Schristos  esac
38*30da1778Schristos}
39*30da1778Schristos
40*30da1778Schristos# guard FILE
41*30da1778Schristos# ----------
42*30da1778Schristos# The CPP macro used to guard inclusion of FILE.
43*30da1778Schristosguard ()
44*30da1778Schristos{
45*30da1778Schristos  printf '%s\n' "$1"                                                    \
46*30da1778Schristos    | sed                                                               \
47*30da1778Schristos        -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'   \
48*30da1778Schristos        -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'                        \
49*30da1778Schristos        -e 's/__*/_/g'
50*30da1778Schristos}
51*30da1778Schristos
52*30da1778Schristos# quote_for_sed [STRING]
53*30da1778Schristos# ----------------------
54*30da1778Schristos# Return STRING (or stdin) quoted to be used as a sed pattern.
55*30da1778Schristosquote_for_sed ()
56*30da1778Schristos{
57*30da1778Schristos  case $# in
58*30da1778Schristos    0) cat;;
59*30da1778Schristos    1) printf '%s\n' "$1";;
60*30da1778Schristos  esac \
61*30da1778Schristos    | sed -e 's|[][\\.*]|\\&|g'
62*30da1778Schristos}
63*30da1778Schristos
64*30da1778Schristoscase "$1" in
65*30da1778Schristos  '')
66*30da1778Schristos    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
67*30da1778Schristos    exit 1
68*30da1778Schristos    ;;
69*30da1778Schristos  --basedir)
70*30da1778Schristos    basedir=$2
71*30da1778Schristos    shift 2
72*30da1778Schristos    ;;
73*30da1778Schristos  -h|--h*)
74*30da1778Schristos    cat <<\EOF
75*30da1778SchristosUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
76*30da1778Schristos
77*30da1778SchristosWrapper for lex/yacc invocations, renaming files as desired.
78*30da1778Schristos
79*30da1778Schristos  INPUT is the input file
80*30da1778Schristos  OUTPUT is one file PROG generates
81*30da1778Schristos  DESIRED is the file we actually want instead of OUTPUT
82*30da1778Schristos  PROGRAM is program to run
83*30da1778Schristos  ARGS are passed to PROG
84*30da1778Schristos
85*30da1778SchristosAny number of OUTPUT,DESIRED pairs may be used.
86*30da1778Schristos
87*30da1778SchristosReport bugs to <bug-automake@gnu.org>.
88*30da1778SchristosEOF
89*30da1778Schristos    exit $?
90*30da1778Schristos    ;;
91*30da1778Schristos  -v|--v*)
92*30da1778Schristos    echo "ylwrap $scriptversion"
93*30da1778Schristos    exit $?
94*30da1778Schristos    ;;
95*30da1778Schristosesac
96*30da1778Schristos
97*30da1778Schristos
98*30da1778Schristos# The input.
99*30da1778Schristosinput=$1
100*30da1778Schristosshift
101*30da1778Schristos# We'll later need for a correct munging of "#line" directives.
102*30da1778Schristosinput_sub_rx=`get_dirname "$input" | quote_for_sed`
103*30da1778Schristoscase $input in
104*30da1778Schristos  [\\/]* | ?:[\\/]*)
105*30da1778Schristos    # Absolute path; do nothing.
106*30da1778Schristos    ;;
107*30da1778Schristos  *)
108*30da1778Schristos    # Relative path.  Make it absolute.
109*30da1778Schristos    input=`pwd`/$input
110*30da1778Schristos    ;;
111*30da1778Schristosesac
112*30da1778Schristosinput_rx=`get_dirname "$input" | quote_for_sed`
113*30da1778Schristos
114*30da1778Schristos# Since DOS filename conventions don't allow two dots,
115*30da1778Schristos# the DOS version of Bison writes out y_tab.c instead of y.tab.c
116*30da1778Schristos# and y_tab.h instead of y.tab.h. Test to see if this is the case.
117*30da1778Schristosy_tab_nodot=false
118*30da1778Schristosif test -f y_tab.c || test -f y_tab.h; then
119*30da1778Schristos  y_tab_nodot=true
120*30da1778Schristosfi
121*30da1778Schristos
122*30da1778Schristos# The parser itself, the first file, is the destination of the .y.c
123*30da1778Schristos# rule in the Makefile.
124*30da1778Schristosparser=$1
125*30da1778Schristos
126*30da1778Schristos# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
127*30da1778Schristos# instance, we rename #include "y.tab.h" into #include "parse.h"
128*30da1778Schristos# during the conversion from y.tab.c to parse.c.
129*30da1778Schristossed_fix_filenames=
130*30da1778Schristos
131*30da1778Schristos# Also rename header guards, as Bison 2.7 for instance uses its header
132*30da1778Schristos# guard in its implementation file.
133*30da1778Schristossed_fix_header_guards=
134*30da1778Schristos
135*30da1778Schristoswhile test $# -ne 0; do
136*30da1778Schristos  if test x"$1" = x"--"; then
137*30da1778Schristos    shift
138*30da1778Schristos    break
139*30da1778Schristos  fi
140*30da1778Schristos  from=$1
141*30da1778Schristos  # Handle y_tab.c and y_tab.h output by DOS
142*30da1778Schristos  if $y_tab_nodot; then
143*30da1778Schristos    case $from in
144*30da1778Schristos      "y.tab.c") from=y_tab.c;;
145*30da1778Schristos      "y.tab.h") from=y_tab.h;;
146*30da1778Schristos    esac
147*30da1778Schristos  fi
148*30da1778Schristos  shift
149*30da1778Schristos  to=$1
150*30da1778Schristos  shift
151*30da1778Schristos  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
152*30da1778Schristos  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
153*30da1778Schristosdone
154*30da1778Schristos
155*30da1778Schristos# The program to run.
156*30da1778Schristosprog=$1
157*30da1778Schristosshift
158*30da1778Schristos# Make any relative path in $prog absolute.
159*30da1778Schristoscase $prog in
160*30da1778Schristos  [\\/]* | ?:[\\/]*) ;;
161*30da1778Schristos  *[\\/]*) prog=`pwd`/$prog ;;
162*30da1778Schristosesac
163*30da1778Schristos
164*30da1778Schristosdirname=ylwrap$$
165*30da1778Schristosdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
166*30da1778Schristostrap "ret=129; $do_exit" 1
167*30da1778Schristostrap "ret=130; $do_exit" 2
168*30da1778Schristostrap "ret=141; $do_exit" 13
169*30da1778Schristostrap "ret=143; $do_exit" 15
170*30da1778Schristosmkdir $dirname || exit 1
171*30da1778Schristos
172*30da1778Schristoscd $dirname
173*30da1778Schristos
174*30da1778Schristoscase $# in
175*30da1778Schristos  0) "$prog" "$input" ;;
176*30da1778Schristos  *) "$prog" "$@" "$input" ;;
177*30da1778Schristosesac
178*30da1778Schristosret=$?
179*30da1778Schristos
180*30da1778Schristosif test $ret -eq 0; then
181*30da1778Schristos  for from in *
182*30da1778Schristos  do
183*30da1778Schristos    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
184*30da1778Schristos    if test -f "$from"; then
185*30da1778Schristos      # If $2 is an absolute path name, then just use that,
186*30da1778Schristos      # otherwise prepend '../'.
187*30da1778Schristos      case $to in
188*30da1778Schristos        [\\/]* | ?:[\\/]*) target=$to;;
189*30da1778Schristos        *) target=../$to;;
190*30da1778Schristos      esac
191*30da1778Schristos
192*30da1778Schristos      # Do not overwrite unchanged header files to avoid useless
193*30da1778Schristos      # recompilations.  Always update the parser itself: it is the
194*30da1778Schristos      # destination of the .y.c rule in the Makefile.  Divert the
195*30da1778Schristos      # output of all other files to a temporary file so we can
196*30da1778Schristos      # compare them to existing versions.
197*30da1778Schristos      if test $from != $parser; then
198*30da1778Schristos        realtarget=$target
199*30da1778Schristos        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
200*30da1778Schristos      fi
201*30da1778Schristos
202*30da1778Schristos      # Munge "#line" or "#" directives.  Don't let the resulting
203*30da1778Schristos      # debug information point at an absolute srcdir.  Use the real
204*30da1778Schristos      # output file name, not yy.lex.c for instance.  Adjust the
205*30da1778Schristos      # include guards too.
206*30da1778Schristos      sed -e "/^#/!b"                           \
207*30da1778Schristos          -e "s|$input_rx|$input_sub_rx|"       \
208*30da1778Schristos          -e "$sed_fix_filenames"               \
209*30da1778Schristos          -e "$sed_fix_header_guards"           \
210*30da1778Schristos        "$from" >"$target" || ret=$?
211*30da1778Schristos
212*30da1778Schristos      # Check whether files must be updated.
213*30da1778Schristos      if test "$from" != "$parser"; then
214*30da1778Schristos        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
215*30da1778Schristos          echo "$to is unchanged"
216*30da1778Schristos          rm -f "$target"
217*30da1778Schristos        else
218*30da1778Schristos          echo "updating $to"
219*30da1778Schristos          mv -f "$target" "$realtarget"
220*30da1778Schristos        fi
221*30da1778Schristos      fi
222*30da1778Schristos    else
223*30da1778Schristos      # A missing file is only an error for the parser.  This is a
224*30da1778Schristos      # blatant hack to let us support using "yacc -d".  If -d is not
225*30da1778Schristos      # specified, don't fail when the header file is "missing".
226*30da1778Schristos      if test "$from" = "$parser"; then
227*30da1778Schristos        ret=1
228*30da1778Schristos      fi
229*30da1778Schristos    fi
230*30da1778Schristos  done
231*30da1778Schristosfi
232*30da1778Schristos
233*30da1778Schristos# Remove the directory.
234*30da1778Schristoscd ..
235*30da1778Schristosrm -rf $dirname
236*30da1778Schristos
237*30da1778Schristosexit $ret
238*30da1778Schristos
239*30da1778Schristos# Local Variables:
240*30da1778Schristos# mode: shell-script
241*30da1778Schristos# sh-indentation: 2
242*30da1778Schristos# eval: (add-hook 'write-file-hooks 'time-stamp)
243*30da1778Schristos# time-stamp-start: "scriptversion="
244*30da1778Schristos# time-stamp-format: "%:y-%02m-%02d.%02H"
245*30da1778Schristos# time-stamp-time-zone: "UTC"
246*30da1778Schristos# time-stamp-end: "; # UTC"
247*30da1778Schristos# End:
248