ylwrap revision 256281
1207340Sjkim#! /bin/sh
2207340Sjkim# ylwrap - wrapper for lex/yacc invocations.
3207340Sjkim
4207340Sjkimscriptversion=2011-08-25.18; # UTC
5207340Sjkim
6207340Sjkim# Copyright (C) 1996-2012 Free Software Foundation, Inc.
7217365Sjkim#
8306536Sjkim# Written by Tom Tromey <tromey@cygnus.com>.
9207340Sjkim#
10207340Sjkim# This program is free software; you can redistribute it and/or modify
11217365Sjkim# it under the terms of the GNU General Public License as published by
12217365Sjkim# the Free Software Foundation; either version 2, or (at your option)
13217365Sjkim# any later version.
14217365Sjkim#
15217365Sjkim# This program is distributed in the hope that it will be useful,
16217365Sjkim# but WITHOUT ANY WARRANTY; without even the implied warranty of
17217365Sjkim# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18217365Sjkim# GNU General Public License for more details.
19217365Sjkim#
20217365Sjkim# You should have received a copy of the GNU General Public License
21217365Sjkim# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22217365Sjkim
23217365Sjkim# As a special exception to the GNU General Public License, if you
24217365Sjkim# distribute this file as part of a program that contains a
25207340Sjkim# configuration script generated by Autoconf, you may include it under
26217365Sjkim# the same distribution terms that you use for the rest of that program.
27217365Sjkim
28217365Sjkim# This file is maintained in Automake, please report
29207340Sjkim# bugs to <bug-automake@gnu.org> or send patches to
30217365Sjkim# <automake-patches@gnu.org>.
31217365Sjkim
32217365Sjkimcase "$1" in
33217365Sjkim  '')
34217365Sjkim    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
35217365Sjkim    exit 1
36217365Sjkim    ;;
37217365Sjkim  --basedir)
38217365Sjkim    basedir=$2
39217365Sjkim    shift 2
40217365Sjkim    ;;
41217365Sjkim  -h|--h*)
42217365Sjkim    cat <<\EOF
43207340SjkimUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
44207344Sjkim
45207340SjkimWrapper for lex/yacc invocations, renaming files as desired.
46207340Sjkim
47207340Sjkim  INPUT is the input file
48207340Sjkim  OUTPUT is one file PROG generates
49207340Sjkim  DESIRED is the file we actually want instead of OUTPUT
50207340Sjkim  PROGRAM is program to run
51207340Sjkim  ARGS are passed to PROG
52207340Sjkim
53207340SjkimAny number of OUTPUT,DESIRED pairs may be used.
54207340Sjkim
55207340SjkimReport bugs to <bug-automake@gnu.org>.
56207340SjkimEOF
57207340Sjkim    exit $?
58207340Sjkim    ;;
59207340Sjkim  -v|--v*)
60207340Sjkim    echo "ylwrap $scriptversion"
61207340Sjkim    exit $?
62281075Sdim    ;;
63207340Sjkimesac
64207340Sjkim
65207340Sjkimget_dirname ()
66207340Sjkim{
67207340Sjkim  case $1 in
68207340Sjkim    */*|*\\*) printf '%s\n' "$1" | sed -e 's,\([\\/]\)[^\\/]*$,\1,';;
69207340Sjkim    # Otherwise,  we want the empty string (not ".").
70207340Sjkim  esac
71207340Sjkim}
72281075Sdim
73207340Sjkimquote_for_sed ()
74207340Sjkim{
75207340Sjkim  # FIXME: really we should care about more than '.' and '\'.
76207340Sjkim  sed -e 's,[\\.],\\&,g'
77207340Sjkim}
78207340Sjkim
79207340Sjkim# The input.
80207340Sjkiminput="$1"
81207340Sjkimshift
82207340Sjkim# We'll later need for a correct munging of "#line" directives.
83207340Sjkiminput_sub_rx=`get_dirname "$input" | quote_for_sed`
84281075Sdimcase "$input" in
85207340Sjkim  [\\/]* | ?:[\\/]*)
86207340Sjkim    # Absolute path; do nothing.
87207340Sjkim    ;;
88207340Sjkim  *)
89281075Sdim    # Relative path.  Make it absolute.
90207340Sjkim    input="`pwd`/$input"
91281075Sdim    ;;
92207340Sjkimesac
93207340Sjkim
94306536Sjkimpairlist=
95207340Sjkimwhile test "$#" -ne 0; do
96207340Sjkim  if test "$1" = "--"; then
97306536Sjkim    shift
98306536Sjkim    break
99207340Sjkim  fi
100207340Sjkim  pairlist="$pairlist $1"
101207340Sjkim  shift
102207340Sjkimdone
103207340Sjkim
104207340Sjkim# The program to run.
105207340Sjkimprog="$1"
106207340Sjkimshift
107207340Sjkim# Make any relative path in $prog absolute.
108207340Sjkimcase "$prog" in
109207340Sjkim  [\\/]* | ?:[\\/]*) ;;
110207340Sjkim  *[\\/]*) prog="`pwd`/$prog" ;;
111207340Sjkimesac
112207340Sjkim
113207340Sjkim# FIXME: add hostname here for parallel makes that run commands on
114207340Sjkim# other machines.  But that might take us over the 14-char limit.
115207340Sjkimdirname=ylwrap$$
116207340Sjkimdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
117207340Sjkimtrap "ret=129; $do_exit" 1
118207340Sjkimtrap "ret=130; $do_exit" 2
119207340Sjkimtrap "ret=141; $do_exit" 13
120207340Sjkimtrap "ret=143; $do_exit" 15
121207340Sjkimmkdir $dirname || exit 1
122207340Sjkim
123207340Sjkimcd $dirname
124207340Sjkim
125207340Sjkimcase $# in
126207340Sjkim  0) "$prog" "$input" ;;
127207340Sjkim  *) "$prog" "$@" "$input" ;;
128207340Sjkimesac
129207340Sjkimret=$?
130207340Sjkim
131207340Sjkimif test $ret -eq 0; then
132207340Sjkim  set X $pairlist
133207340Sjkim  shift
134207340Sjkim  first=yes
135207340Sjkim  # Since DOS filename conventions don't allow two dots,
136207340Sjkim  # the DOS version of Bison writes out y_tab.c instead of y.tab.c
137207340Sjkim  # and y_tab.h instead of y.tab.h. Test to see if this is the case.
138207340Sjkim  y_tab_nodot="no"
139207340Sjkim  if test -f y_tab.c || test -f y_tab.h; then
140207340Sjkim    y_tab_nodot="yes"
141207340Sjkim  fi
142207340Sjkim
143207340Sjkim  input_rx=`get_dirname "$input" | quote_for_sed`
144228110Sjkim
145228110Sjkim  while test "$#" -ne 0; do
146207340Sjkim    from="$1"
147207340Sjkim    # Handle y_tab.c and y_tab.h output by DOS
148207340Sjkim    if test $y_tab_nodot = "yes"; then
149207340Sjkim      if test $from = "y.tab.c"; then
150207340Sjkim        from="y_tab.c"
151228110Sjkim      else
152207340Sjkim        if test $from = "y.tab.h"; then
153207340Sjkim          from="y_tab.h"
154207340Sjkim        fi
155207340Sjkim      fi
156207340Sjkim    fi
157207340Sjkim    if test -f "$from"; then
158207340Sjkim      # If $2 is an absolute path name, then just use that,
159228110Sjkim      # otherwise prepend '../'.
160207340Sjkim      case "$2" in
161207340Sjkim        [\\/]* | ?:[\\/]*) target="$2";;
162207340Sjkim        *) target="../$2";;
163207340Sjkim      esac
164207340Sjkim
165207340Sjkim      # We do not want to overwrite a header file if it hasn't
166207340Sjkim      # changed.  This avoid useless recompilations.  However the
167228110Sjkim      # parser itself (the first file) should always be updated,
168207340Sjkim      # because it is the destination of the .y.c rule in the
169207340Sjkim      # Makefile.  Divert the output of all other files to a temporary
170207340Sjkim      # file so we can compare them to existing versions.
171207340Sjkim      if test $first = no; then
172207340Sjkim        realtarget="$target"
173207340Sjkim        target="tmp-`echo $target | sed s/.*[\\/]//g`"
174207340Sjkim      fi
175207340Sjkim      # Munge "#line" or "#" directives.
176207340Sjkim      # We don't want the resulting debug information to point at
177207340Sjkim      # an absolute srcdir.
178207340Sjkim      # We want to use the real output file name, not yy.lex.c for
179207340Sjkim      # instance.
180207340Sjkim      # We want the include guards to be adjusted too.
181207340Sjkim      FROM=`echo "$from" | sed \
182228110Sjkim            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
183207340Sjkim            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
184207340Sjkim      TARGET=`echo "$2" | sed \
185207340Sjkim            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
186207340Sjkim            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
187207340Sjkim
188207340Sjkim      sed -e "/^#/!b" -e "s,$input_rx,$input_sub_rx," -e "s,$from,$2," \
189207340Sjkim          -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
190207340Sjkim
191207340Sjkim      # Check whether header files must be updated.
192207340Sjkim      if test $first = no; then
193207340Sjkim        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
194207340Sjkim          echo "$2" is unchanged
195207340Sjkim          rm -f "$target"
196207340Sjkim        else
197207340Sjkim          echo updating "$2"
198207340Sjkim          mv -f "$target" "$realtarget"
199207340Sjkim        fi
200207340Sjkim      fi
201207340Sjkim    else
202207340Sjkim      # A missing file is only an error for the first file.  This
203207340Sjkim      # is a blatant hack to let us support using "yacc -d".  If -d
204207340Sjkim      # is not specified, we don't want an error when the header
205207340Sjkim      # file is "missing".
206207340Sjkim      if test $first = yes; then
207207340Sjkim        ret=1
208207340Sjkim      fi
209207340Sjkim    fi
210207340Sjkim    shift
211207340Sjkim    shift
212207340Sjkim    first=no
213207340Sjkim  done
214207340Sjkimelse
215207340Sjkim  ret=$?
216207340Sjkimfi
217207340Sjkim
218207340Sjkim# Remove the directory.
219207340Sjkimcd ..
220207340Sjkimrm -rf $dirname
221207340Sjkim
222207340Sjkimexit $ret
223207340Sjkim
224207340Sjkim# Local Variables:
225207340Sjkim# mode: shell-script
226207340Sjkim# sh-indentation: 2
227207340Sjkim# eval: (add-hook 'write-file-hooks 'time-stamp)
228207340Sjkim# time-stamp-start: "scriptversion="
229207340Sjkim# time-stamp-format: "%:y-%02m-%02d.%02H"
230207340Sjkim# time-stamp-time-zone: "UTC"
231207340Sjkim# time-stamp-end: "; # UTC"
232207340Sjkim# End:
233207340Sjkim