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