1b60292e0Smrg#! /bin/sh
2f46a6179Smrg# ylwrap - wrapper for lex/yacc invocations.
3f46a6179Smrg
4bfe6082cSmrgscriptversion=2018-03-07.03; # UTC
5f46a6179Smrg
6a57d84feSmrg# Copyright (C) 1996-2021 Free Software Foundation, Inc.
7f46a6179Smrg#
8f46a6179Smrg# Written by Tom Tromey <tromey@cygnus.com>.
9f46a6179Smrg#
10f46a6179Smrg# This program is free software; you can redistribute it and/or modify
11f46a6179Smrg# it under the terms of the GNU General Public License as published by
12f46a6179Smrg# the Free Software Foundation; either version 2, or (at your option)
13f46a6179Smrg# any later version.
14f46a6179Smrg#
15f46a6179Smrg# This program is distributed in the hope that it will be useful,
16f46a6179Smrg# but WITHOUT ANY WARRANTY; without even the implied warranty of
17f46a6179Smrg# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18f46a6179Smrg# GNU General Public License for more details.
19f46a6179Smrg#
20f46a6179Smrg# You should have received a copy of the GNU General Public License
21bfe6082cSmrg# along with this program.  If not, see <https://www.gnu.org/licenses/>.
22f46a6179Smrg
23f46a6179Smrg# As a special exception to the GNU General Public License, if you
24f46a6179Smrg# distribute this file as part of a program that contains a
25f46a6179Smrg# configuration script generated by Autoconf, you may include it under
26f46a6179Smrg# the same distribution terms that you use for the rest of that program.
27f46a6179Smrg
28f46a6179Smrg# This file is maintained in Automake, please report
29f46a6179Smrg# bugs to <bug-automake@gnu.org> or send patches to
30f46a6179Smrg# <automake-patches@gnu.org>.
31f46a6179Smrg
32c82dfdfbSmrgget_dirname ()
33c82dfdfbSmrg{
34c82dfdfbSmrg  case $1 in
35c82dfdfbSmrg    */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36c82dfdfbSmrg    # Otherwise,  we want the empty string (not ".").
37c82dfdfbSmrg  esac
38c82dfdfbSmrg}
39c82dfdfbSmrg
40c82dfdfbSmrg# guard FILE
41c82dfdfbSmrg# ----------
42c82dfdfbSmrg# The CPP macro used to guard inclusion of FILE.
43c82dfdfbSmrgguard ()
44c82dfdfbSmrg{
45c82dfdfbSmrg  printf '%s\n' "$1"                                                    \
46c82dfdfbSmrg    | sed                                                               \
47c82dfdfbSmrg        -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'   \
48c82dfdfbSmrg        -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'                        \
49c82dfdfbSmrg        -e 's/__*/_/g'
50c82dfdfbSmrg}
51c82dfdfbSmrg
52c82dfdfbSmrg# quote_for_sed [STRING]
53c82dfdfbSmrg# ----------------------
54c82dfdfbSmrg# Return STRING (or stdin) quoted to be used as a sed pattern.
55c82dfdfbSmrgquote_for_sed ()
56c82dfdfbSmrg{
57c82dfdfbSmrg  case $# in
58c82dfdfbSmrg    0) cat;;
59c82dfdfbSmrg    1) printf '%s\n' "$1";;
60c82dfdfbSmrg  esac \
61c82dfdfbSmrg    | sed -e 's|[][\\.*]|\\&|g'
62c82dfdfbSmrg}
63c82dfdfbSmrg
64f46a6179Smrgcase "$1" in
65f46a6179Smrg  '')
66c82dfdfbSmrg    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
67f46a6179Smrg    exit 1
68f46a6179Smrg    ;;
69f46a6179Smrg  --basedir)
70f46a6179Smrg    basedir=$2
71f46a6179Smrg    shift 2
72f46a6179Smrg    ;;
73f46a6179Smrg  -h|--h*)
74f46a6179Smrg    cat <<\EOF
75f46a6179SmrgUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
76f46a6179Smrg
77f46a6179SmrgWrapper for lex/yacc invocations, renaming files as desired.
78f46a6179Smrg
79f46a6179Smrg  INPUT is the input file
80f46a6179Smrg  OUTPUT is one file PROG generates
81f46a6179Smrg  DESIRED is the file we actually want instead of OUTPUT
82f46a6179Smrg  PROGRAM is program to run
83f46a6179Smrg  ARGS are passed to PROG
84f46a6179Smrg
85f46a6179SmrgAny number of OUTPUT,DESIRED pairs may be used.
86f46a6179Smrg
87f46a6179SmrgReport bugs to <bug-automake@gnu.org>.
88f46a6179SmrgEOF
89f46a6179Smrg    exit $?
90f46a6179Smrg    ;;
91f46a6179Smrg  -v|--v*)
92f46a6179Smrg    echo "ylwrap $scriptversion"
93f46a6179Smrg    exit $?
94f46a6179Smrg    ;;
95f46a6179Smrgesac
96f46a6179Smrg
97f46a6179Smrg
98f46a6179Smrg# The input.
99c82dfdfbSmrginput=$1
100f46a6179Smrgshift
101c82dfdfbSmrg# We'll later need for a correct munging of "#line" directives.
102c82dfdfbSmrginput_sub_rx=`get_dirname "$input" | quote_for_sed`
103c82dfdfbSmrgcase $input in
104f46a6179Smrg  [\\/]* | ?:[\\/]*)
105f46a6179Smrg    # Absolute path; do nothing.
106f46a6179Smrg    ;;
107f46a6179Smrg  *)
108f46a6179Smrg    # Relative path.  Make it absolute.
109c82dfdfbSmrg    input=`pwd`/$input
110f46a6179Smrg    ;;
111f46a6179Smrgesac
112c82dfdfbSmrginput_rx=`get_dirname "$input" | quote_for_sed`
113c82dfdfbSmrg
114c82dfdfbSmrg# Since DOS filename conventions don't allow two dots,
115c82dfdfbSmrg# the DOS version of Bison writes out y_tab.c instead of y.tab.c
116c82dfdfbSmrg# and y_tab.h instead of y.tab.h. Test to see if this is the case.
117c82dfdfbSmrgy_tab_nodot=false
118c82dfdfbSmrgif test -f y_tab.c || test -f y_tab.h; then
119c82dfdfbSmrg  y_tab_nodot=true
120c82dfdfbSmrgfi
121c82dfdfbSmrg
122c82dfdfbSmrg# The parser itself, the first file, is the destination of the .y.c
123c82dfdfbSmrg# rule in the Makefile.
124c82dfdfbSmrgparser=$1
125c82dfdfbSmrg
126c82dfdfbSmrg# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
127c82dfdfbSmrg# instance, we rename #include "y.tab.h" into #include "parse.h"
128c82dfdfbSmrg# during the conversion from y.tab.c to parse.c.
129c82dfdfbSmrgsed_fix_filenames=
130f46a6179Smrg
131c82dfdfbSmrg# Also rename header guards, as Bison 2.7 for instance uses its header
132c82dfdfbSmrg# guard in its implementation file.
133c82dfdfbSmrgsed_fix_header_guards=
134c82dfdfbSmrg
135c82dfdfbSmrgwhile test $# -ne 0; do
136c82dfdfbSmrg  if test x"$1" = x"--"; then
137f46a6179Smrg    shift
138f46a6179Smrg    break
139f46a6179Smrg  fi
140c82dfdfbSmrg  from=$1
141c82dfdfbSmrg  # Handle y_tab.c and y_tab.h output by DOS
142c82dfdfbSmrg  if $y_tab_nodot; then
143c82dfdfbSmrg    case $from in
144c82dfdfbSmrg      "y.tab.c") from=y_tab.c;;
145c82dfdfbSmrg      "y.tab.h") from=y_tab.h;;
146c82dfdfbSmrg    esac
147c82dfdfbSmrg  fi
148f46a6179Smrg  shift
149c82dfdfbSmrg  to=$1
150c82dfdfbSmrg  shift
151c82dfdfbSmrg  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
152c82dfdfbSmrg  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
153f46a6179Smrgdone
154f46a6179Smrg
155f46a6179Smrg# The program to run.
156c82dfdfbSmrgprog=$1
157f46a6179Smrgshift
158f46a6179Smrg# Make any relative path in $prog absolute.
159c82dfdfbSmrgcase $prog in
160f46a6179Smrg  [\\/]* | ?:[\\/]*) ;;
161c82dfdfbSmrg  *[\\/]*) prog=`pwd`/$prog ;;
162f46a6179Smrgesac
163f46a6179Smrg
164f46a6179Smrgdirname=ylwrap$$
165c82dfdfbSmrgdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
166c82dfdfbSmrgtrap "ret=129; $do_exit" 1
167c82dfdfbSmrgtrap "ret=130; $do_exit" 2
168c82dfdfbSmrgtrap "ret=141; $do_exit" 13
169c82dfdfbSmrgtrap "ret=143; $do_exit" 15
170f46a6179Smrgmkdir $dirname || exit 1
171f46a6179Smrg
172f46a6179Smrgcd $dirname
173f46a6179Smrg
174f46a6179Smrgcase $# in
175f46a6179Smrg  0) "$prog" "$input" ;;
176f46a6179Smrg  *) "$prog" "$@" "$input" ;;
177f46a6179Smrgesac
178f46a6179Smrgret=$?
179f46a6179Smrg
180f46a6179Smrgif test $ret -eq 0; then
181c82dfdfbSmrg  for from in *
182c82dfdfbSmrg  do
183c82dfdfbSmrg    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
184f46a6179Smrg    if test -f "$from"; then
185f46a6179Smrg      # If $2 is an absolute path name, then just use that,
186c82dfdfbSmrg      # otherwise prepend '../'.
187c82dfdfbSmrg      case $to in
188c82dfdfbSmrg        [\\/]* | ?:[\\/]*) target=$to;;
189c82dfdfbSmrg        *) target=../$to;;
190f46a6179Smrg      esac
191f46a6179Smrg
192c82dfdfbSmrg      # Do not overwrite unchanged header files to avoid useless
193c82dfdfbSmrg      # recompilations.  Always update the parser itself: it is the
194c82dfdfbSmrg      # destination of the .y.c rule in the Makefile.  Divert the
195c82dfdfbSmrg      # output of all other files to a temporary file so we can
196c82dfdfbSmrg      # compare them to existing versions.
197c82dfdfbSmrg      if test $from != $parser; then
198c82dfdfbSmrg        realtarget=$target
199c82dfdfbSmrg        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
200f46a6179Smrg      fi
201c82dfdfbSmrg
202c82dfdfbSmrg      # Munge "#line" or "#" directives.  Don't let the resulting
203c82dfdfbSmrg      # debug information point at an absolute srcdir.  Use the real
204c82dfdfbSmrg      # output file name, not yy.lex.c for instance.  Adjust the
205c82dfdfbSmrg      # include guards too.
206c82dfdfbSmrg      sed -e "/^#/!b"                           \
207c82dfdfbSmrg          -e "s|$input_rx|$input_sub_rx|"       \
208c82dfdfbSmrg          -e "$sed_fix_filenames"               \
209c82dfdfbSmrg          -e "$sed_fix_header_guards"           \
210c82dfdfbSmrg        "$from" >"$target" || ret=$?
211c82dfdfbSmrg
212c82dfdfbSmrg      # Check whether files must be updated.
213c82dfdfbSmrg      if test "$from" != "$parser"; then
214c82dfdfbSmrg        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
215c82dfdfbSmrg          echo "$to is unchanged"
216c82dfdfbSmrg          rm -f "$target"
217c82dfdfbSmrg        else
218c82dfdfbSmrg          echo "updating $to"
219f46a6179Smrg          mv -f "$target" "$realtarget"
220f46a6179Smrg        fi
221f46a6179Smrg      fi
222f46a6179Smrg    else
223c82dfdfbSmrg      # A missing file is only an error for the parser.  This is a
224c82dfdfbSmrg      # blatant hack to let us support using "yacc -d".  If -d is not
225c82dfdfbSmrg      # specified, don't fail when the header file is "missing".
226c82dfdfbSmrg      if test "$from" = "$parser"; then
227f46a6179Smrg        ret=1
228f46a6179Smrg      fi
229f46a6179Smrg    fi
230f46a6179Smrg  done
231f46a6179Smrgfi
232f46a6179Smrg
233f46a6179Smrg# Remove the directory.
234f46a6179Smrgcd ..
235f46a6179Smrgrm -rf $dirname
236f46a6179Smrg
237f46a6179Smrgexit $ret
238f46a6179Smrg
239f46a6179Smrg# Local Variables:
240f46a6179Smrg# mode: shell-script
241f46a6179Smrg# sh-indentation: 2
242bfe6082cSmrg# eval: (add-hook 'before-save-hook 'time-stamp)
243f46a6179Smrg# time-stamp-start: "scriptversion="
244f46a6179Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
2455c4ee2b5Smrg# time-stamp-time-zone: "UTC0"
246690143ccSmrg# time-stamp-end: "; # UTC"
247f46a6179Smrg# End:
248