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