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