1243730Srwatson#! /bin/sh
2243730Srwatson# ylwrap - wrapper for lex/yacc invocations.
3243730Srwatson
4243730Srwatsonscriptversion=2011-08-25.18; # UTC
5243730Srwatson
6243730Srwatson# Copyright (C) 1996-2012 Free Software Foundation, Inc.
7243730Srwatson#
8243730Srwatson# Written by Tom Tromey <tromey@cygnus.com>.
9243730Srwatson#
10243730Srwatson# This program is free software; you can redistribute it and/or modify
11243730Srwatson# it under the terms of the GNU General Public License as published by
12243730Srwatson# the Free Software Foundation; either version 2, or (at your option)
13243730Srwatson# any later version.
14243730Srwatson#
15243730Srwatson# This program is distributed in the hope that it will be useful,
16243730Srwatson# but WITHOUT ANY WARRANTY; without even the implied warranty of
17243730Srwatson# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18243730Srwatson# GNU General Public License for more details.
19243730Srwatson#
20243730Srwatson# You should have received a copy of the GNU General Public License
21243730Srwatson# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22243730Srwatson
23243730Srwatson# As a special exception to the GNU General Public License, if you
24243730Srwatson# distribute this file as part of a program that contains a
25243730Srwatson# configuration script generated by Autoconf, you may include it under
26243730Srwatson# the same distribution terms that you use for the rest of that program.
27243730Srwatson
28243730Srwatson# This file is maintained in Automake, please report
29243730Srwatson# bugs to <bug-automake@gnu.org> or send patches to
30243730Srwatson# <automake-patches@gnu.org>.
31243730Srwatson
32243730Srwatsoncase "$1" in
33243730Srwatson  '')
34243730Srwatson    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
35243730Srwatson    exit 1
36243730Srwatson    ;;
37243730Srwatson  --basedir)
38243730Srwatson    basedir=$2
39243730Srwatson    shift 2
40243730Srwatson    ;;
41243730Srwatson  -h|--h*)
42243730Srwatson    cat <<\EOF
43243730SrwatsonUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
44243730Srwatson
45243730SrwatsonWrapper for lex/yacc invocations, renaming files as desired.
46243730Srwatson
47243730Srwatson  INPUT is the input file
48243730Srwatson  OUTPUT is one file PROG generates
49243730Srwatson  DESIRED is the file we actually want instead of OUTPUT
50243730Srwatson  PROGRAM is program to run
51243730Srwatson  ARGS are passed to PROG
52243730Srwatson
53243730SrwatsonAny number of OUTPUT,DESIRED pairs may be used.
54243730Srwatson
55243730SrwatsonReport bugs to <bug-automake@gnu.org>.
56243730SrwatsonEOF
57243730Srwatson    exit $?
58243730Srwatson    ;;
59243730Srwatson  -v|--v*)
60243730Srwatson    echo "ylwrap $scriptversion"
61243730Srwatson    exit $?
62243730Srwatson    ;;
63243730Srwatsonesac
64243730Srwatson
65243730Srwatsonget_dirname ()
66243730Srwatson{
67243730Srwatson  case $1 in
68243730Srwatson    */*|*\\*) printf '%s\n' "$1" | sed -e 's,\([\\/]\)[^\\/]*$,\1,';;
69243730Srwatson    # Otherwise,  we want the empty string (not ".").
70243730Srwatson  esac
71243730Srwatson}
72243730Srwatson
73243730Srwatsonquote_for_sed ()
74243730Srwatson{
75243730Srwatson  # FIXME: really we should care about more than '.' and '\'.
76243730Srwatson  sed -e 's,[\\.],\\&,g'
77243730Srwatson}
78243730Srwatson
79243730Srwatson# The input.
80243730Srwatsoninput="$1"
81243730Srwatsonshift
82243730Srwatson# We'll later need for a correct munging of "#line" directives.
83243730Srwatsoninput_sub_rx=`get_dirname "$input" | quote_for_sed`
84243730Srwatsoncase "$input" in
85243730Srwatson  [\\/]* | ?:[\\/]*)
86243730Srwatson    # Absolute path; do nothing.
87243730Srwatson    ;;
88243730Srwatson  *)
89243730Srwatson    # Relative path.  Make it absolute.
90243730Srwatson    input="`pwd`/$input"
91243730Srwatson    ;;
92243730Srwatsonesac
93243730Srwatson
94243730Srwatsonpairlist=
95243730Srwatsonwhile test "$#" -ne 0; do
96243730Srwatson  if test "$1" = "--"; then
97243730Srwatson    shift
98243730Srwatson    break
99243730Srwatson  fi
100243730Srwatson  pairlist="$pairlist $1"
101243730Srwatson  shift
102243730Srwatsondone
103243730Srwatson
104243730Srwatson# The program to run.
105243730Srwatsonprog="$1"
106243730Srwatsonshift
107243730Srwatson# Make any relative path in $prog absolute.
108243730Srwatsoncase "$prog" in
109243730Srwatson  [\\/]* | ?:[\\/]*) ;;
110243730Srwatson  *[\\/]*) prog="`pwd`/$prog" ;;
111243730Srwatsonesac
112243730Srwatson
113243730Srwatson# FIXME: add hostname here for parallel makes that run commands on
114243730Srwatson# other machines.  But that might take us over the 14-char limit.
115243730Srwatsondirname=ylwrap$$
116243730Srwatsondo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
117243730Srwatsontrap "ret=129; $do_exit" 1
118243730Srwatsontrap "ret=130; $do_exit" 2
119243730Srwatsontrap "ret=141; $do_exit" 13
120243730Srwatsontrap "ret=143; $do_exit" 15
121243730Srwatsonmkdir $dirname || exit 1
122243730Srwatson
123243730Srwatsoncd $dirname
124243730Srwatson
125243730Srwatsoncase $# in
126243730Srwatson  0) "$prog" "$input" ;;
127243730Srwatson  *) "$prog" "$@" "$input" ;;
128243730Srwatsonesac
129243730Srwatsonret=$?
130243730Srwatson
131243730Srwatsonif test $ret -eq 0; then
132243730Srwatson  set X $pairlist
133243730Srwatson  shift
134243730Srwatson  first=yes
135243730Srwatson  # Since DOS filename conventions don't allow two dots,
136243730Srwatson  # the DOS version of Bison writes out y_tab.c instead of y.tab.c
137243730Srwatson  # and y_tab.h instead of y.tab.h. Test to see if this is the case.
138243730Srwatson  y_tab_nodot="no"
139243730Srwatson  if test -f y_tab.c || test -f y_tab.h; then
140243730Srwatson    y_tab_nodot="yes"
141243730Srwatson  fi
142243730Srwatson
143243730Srwatson  input_rx=`get_dirname "$input" | quote_for_sed`
144243730Srwatson
145243730Srwatson  while test "$#" -ne 0; do
146243730Srwatson    from="$1"
147243730Srwatson    # Handle y_tab.c and y_tab.h output by DOS
148243730Srwatson    if test $y_tab_nodot = "yes"; then
149243730Srwatson      if test $from = "y.tab.c"; then
150243730Srwatson        from="y_tab.c"
151243730Srwatson      else
152243730Srwatson        if test $from = "y.tab.h"; then
153243730Srwatson          from="y_tab.h"
154243730Srwatson        fi
155243730Srwatson      fi
156243730Srwatson    fi
157243730Srwatson    if test -f "$from"; then
158243730Srwatson      # If $2 is an absolute path name, then just use that,
159243730Srwatson      # otherwise prepend '../'.
160243730Srwatson      case "$2" in
161243730Srwatson        [\\/]* | ?:[\\/]*) target="$2";;
162243730Srwatson        *) target="../$2";;
163243730Srwatson      esac
164243730Srwatson
165243730Srwatson      # We do not want to overwrite a header file if it hasn't
166243730Srwatson      # changed.  This avoid useless recompilations.  However the
167243730Srwatson      # parser itself (the first file) should always be updated,
168243730Srwatson      # because it is the destination of the .y.c rule in the
169243730Srwatson      # Makefile.  Divert the output of all other files to a temporary
170243730Srwatson      # file so we can compare them to existing versions.
171243730Srwatson      if test $first = no; then
172243730Srwatson        realtarget="$target"
173243730Srwatson        target="tmp-`echo $target | sed s/.*[\\/]//g`"
174243730Srwatson      fi
175243730Srwatson      # Munge "#line" or "#" directives.
176243730Srwatson      # We don't want the resulting debug information to point at
177243730Srwatson      # an absolute srcdir.
178243730Srwatson      # We want to use the real output file name, not yy.lex.c for
179243730Srwatson      # instance.
180243730Srwatson      # We want the include guards to be adjusted too.
181243730Srwatson      FROM=`echo "$from" | sed \
182243730Srwatson            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
183243730Srwatson            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
184243730Srwatson      TARGET=`echo "$2" | sed \
185243730Srwatson            -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
186243730Srwatson            -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'`
187243730Srwatson
188243730Srwatson      sed -e "/^#/!b" -e "s,$input_rx,$input_sub_rx," -e "s,$from,$2," \
189243730Srwatson          -e "s,$FROM,$TARGET," "$from" >"$target" || ret=$?
190243730Srwatson
191243730Srwatson      # Check whether header files must be updated.
192243730Srwatson      if test $first = no; then
193243730Srwatson        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
194243730Srwatson          echo "$2" is unchanged
195243730Srwatson          rm -f "$target"
196243730Srwatson        else
197243730Srwatson          echo updating "$2"
198243730Srwatson          mv -f "$target" "$realtarget"
199243730Srwatson        fi
200243730Srwatson      fi
201243730Srwatson    else
202243730Srwatson      # A missing file is only an error for the first file.  This
203243730Srwatson      # is a blatant hack to let us support using "yacc -d".  If -d
204243730Srwatson      # is not specified, we don't want an error when the header
205243730Srwatson      # file is "missing".
206243730Srwatson      if test $first = yes; then
207243730Srwatson        ret=1
208243730Srwatson      fi
209243730Srwatson    fi
210243730Srwatson    shift
211243730Srwatson    shift
212243730Srwatson    first=no
213243730Srwatson  done
214243730Srwatsonelse
215243730Srwatson  ret=$?
216243730Srwatsonfi
217243730Srwatson
218243730Srwatson# Remove the directory.
219243730Srwatsoncd ..
220243730Srwatsonrm -rf $dirname
221243730Srwatson
222243730Srwatsonexit $ret
223243730Srwatson
224243730Srwatson# Local Variables:
225243730Srwatson# mode: shell-script
226243730Srwatson# sh-indentation: 2
227243730Srwatson# eval: (add-hook 'write-file-hooks 'time-stamp)
228243730Srwatson# time-stamp-start: "scriptversion="
229243730Srwatson# time-stamp-format: "%:y-%02m-%02d.%02H"
230243730Srwatson# time-stamp-time-zone: "UTC"
231243730Srwatson# time-stamp-end: "; # UTC"
232243730Srwatson# End:
233