xref: /netbsd-src/external/gpl3/gdb/dist/ylwrap (revision 9d2109275e137f9027abb05e859af639830e18f5)
1597410b8Schristos#! /bin/sh
2597410b8Schristos# ylwrap - wrapper for lex/yacc invocations.
3597410b8Schristos
4*9d210927Schristosscriptversion=2013-01-12.17; # UTC
5597410b8Schristos
6*9d210927Schristos# Copyright (C) 1996-2014 Free Software Foundation, Inc.
7597410b8Schristos#
8597410b8Schristos# Written by Tom Tromey <tromey@cygnus.com>.
9597410b8Schristos#
10597410b8Schristos# This program is free software; you can redistribute it and/or modify
11597410b8Schristos# it under the terms of the GNU General Public License as published by
12597410b8Schristos# the Free Software Foundation; either version 2, or (at your option)
13597410b8Schristos# any later version.
14597410b8Schristos#
15597410b8Schristos# This program is distributed in the hope that it will be useful,
16597410b8Schristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
17597410b8Schristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18597410b8Schristos# GNU General Public License for more details.
19597410b8Schristos#
20597410b8Schristos# You should have received a copy of the GNU General Public License
21597410b8Schristos# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22597410b8Schristos
23597410b8Schristos# As a special exception to the GNU General Public License, if you
24597410b8Schristos# distribute this file as part of a program that contains a
25597410b8Schristos# configuration script generated by Autoconf, you may include it under
26597410b8Schristos# the same distribution terms that you use for the rest of that program.
27597410b8Schristos
28597410b8Schristos# This file is maintained in Automake, please report
29597410b8Schristos# bugs to <bug-automake@gnu.org> or send patches to
30597410b8Schristos# <automake-patches@gnu.org>.
31597410b8Schristos
32*9d210927Schristosget_dirname ()
33*9d210927Schristos{
34*9d210927Schristos  case $1 in
35*9d210927Schristos    */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36*9d210927Schristos    # Otherwise,  we want the empty string (not ".").
37*9d210927Schristos  esac
38*9d210927Schristos}
39*9d210927Schristos
40*9d210927Schristos# guard FILE
41*9d210927Schristos# ----------
42*9d210927Schristos# The CPP macro used to guard inclusion of FILE.
43*9d210927Schristosguard ()
44*9d210927Schristos{
45*9d210927Schristos  printf '%s\n' "$1"                                                    \
46*9d210927Schristos    | sed                                                               \
47*9d210927Schristos        -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'   \
48*9d210927Schristos        -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'                        \
49*9d210927Schristos        -e 's/__*/_/g'
50*9d210927Schristos}
51*9d210927Schristos
52*9d210927Schristos# quote_for_sed [STRING]
53*9d210927Schristos# ----------------------
54*9d210927Schristos# Return STRING (or stdin) quoted to be used as a sed pattern.
55*9d210927Schristosquote_for_sed ()
56*9d210927Schristos{
57*9d210927Schristos  case $# in
58*9d210927Schristos    0) cat;;
59*9d210927Schristos    1) printf '%s\n' "$1";;
60*9d210927Schristos  esac \
61*9d210927Schristos    | sed -e 's|[][\\.*]|\\&|g'
62*9d210927Schristos}
63*9d210927Schristos
64597410b8Schristoscase "$1" in
65597410b8Schristos  '')
66*9d210927Schristos    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
67597410b8Schristos    exit 1
68597410b8Schristos    ;;
69597410b8Schristos  --basedir)
70597410b8Schristos    basedir=$2
71597410b8Schristos    shift 2
72597410b8Schristos    ;;
73597410b8Schristos  -h|--h*)
74597410b8Schristos    cat <<\EOF
75597410b8SchristosUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
76597410b8Schristos
77597410b8SchristosWrapper for lex/yacc invocations, renaming files as desired.
78597410b8Schristos
79597410b8Schristos  INPUT is the input file
80597410b8Schristos  OUTPUT is one file PROG generates
81597410b8Schristos  DESIRED is the file we actually want instead of OUTPUT
82597410b8Schristos  PROGRAM is program to run
83597410b8Schristos  ARGS are passed to PROG
84597410b8Schristos
85597410b8SchristosAny number of OUTPUT,DESIRED pairs may be used.
86597410b8Schristos
87597410b8SchristosReport bugs to <bug-automake@gnu.org>.
88597410b8SchristosEOF
89597410b8Schristos    exit $?
90597410b8Schristos    ;;
91597410b8Schristos  -v|--v*)
92597410b8Schristos    echo "ylwrap $scriptversion"
93597410b8Schristos    exit $?
94597410b8Schristos    ;;
95597410b8Schristosesac
96597410b8Schristos
97597410b8Schristos
98597410b8Schristos# The input.
99*9d210927Schristosinput=$1
100597410b8Schristosshift
101*9d210927Schristos# We'll later need for a correct munging of "#line" directives.
102*9d210927Schristosinput_sub_rx=`get_dirname "$input" | quote_for_sed`
103*9d210927Schristoscase $input in
104597410b8Schristos  [\\/]* | ?:[\\/]*)
105597410b8Schristos    # Absolute path; do nothing.
106597410b8Schristos    ;;
107597410b8Schristos  *)
108597410b8Schristos    # Relative path.  Make it absolute.
109*9d210927Schristos    input=`pwd`/$input
110597410b8Schristos    ;;
111597410b8Schristosesac
112*9d210927Schristosinput_rx=`get_dirname "$input" | quote_for_sed`
113597410b8Schristos
114*9d210927Schristos# The parser itself, the first file, is the destination of the .y.c
115*9d210927Schristos# rule in the Makefile.
116*9d210927Schristosparser=$1
117*9d210927Schristos
118*9d210927Schristos# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
119*9d210927Schristos# instance, we rename #include "y.tab.h" into #include "parse.h"
120*9d210927Schristos# during the conversion from y.tab.c to parse.c.
121*9d210927Schristossed_fix_filenames=
122*9d210927Schristos
123*9d210927Schristos# Also rename header guards, as Bison 2.7 for instance uses its header
124*9d210927Schristos# guard in its implementation file.
125*9d210927Schristossed_fix_header_guards=
126*9d210927Schristos
127*9d210927Schristoswhile test $# -ne 0; do
128*9d210927Schristos  if test x"$1" = x"--"; then
129597410b8Schristos    shift
130597410b8Schristos    break
131597410b8Schristos  fi
132*9d210927Schristos  from=$1
133597410b8Schristos  shift
134*9d210927Schristos  to=$1
135*9d210927Schristos  shift
136*9d210927Schristos  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
137*9d210927Schristos  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
138597410b8Schristosdone
139597410b8Schristos
140597410b8Schristos# The program to run.
141*9d210927Schristosprog=$1
142597410b8Schristosshift
143597410b8Schristos# Make any relative path in $prog absolute.
144*9d210927Schristoscase $prog in
145597410b8Schristos  [\\/]* | ?:[\\/]*) ;;
146*9d210927Schristos  *[\\/]*) prog=`pwd`/$prog ;;
147597410b8Schristosesac
148597410b8Schristos
149597410b8Schristosdirname=ylwrap$$
150*9d210927Schristosdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
151*9d210927Schristostrap "ret=129; $do_exit" 1
152*9d210927Schristostrap "ret=130; $do_exit" 2
153*9d210927Schristostrap "ret=141; $do_exit" 13
154*9d210927Schristostrap "ret=143; $do_exit" 15
155597410b8Schristosmkdir $dirname || exit 1
156597410b8Schristos
157597410b8Schristoscd $dirname
158597410b8Schristos
159597410b8Schristoscase $# in
160597410b8Schristos  0) "$prog" "$input" ;;
161597410b8Schristos  *) "$prog" "$@" "$input" ;;
162597410b8Schristosesac
163597410b8Schristosret=$?
164597410b8Schristos
165597410b8Schristosif test $ret -eq 0; then
166*9d210927Schristos  for from in *
167*9d210927Schristos  do
168*9d210927Schristos    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
169597410b8Schristos    if test -f "$from"; then
170597410b8Schristos      # If $2 is an absolute path name, then just use that,
171*9d210927Schristos      # otherwise prepend '../'.
172*9d210927Schristos      case $to in
173*9d210927Schristos        [\\/]* | ?:[\\/]*) target=$to;;
174*9d210927Schristos        *) target=../$to;;
175597410b8Schristos      esac
176597410b8Schristos
177*9d210927Schristos      # Do not overwrite unchanged header files to avoid useless
178*9d210927Schristos      # recompilations.  Always update the parser itself: it is the
179*9d210927Schristos      # destination of the .y.c rule in the Makefile.  Divert the
180*9d210927Schristos      # output of all other files to a temporary file so we can
181*9d210927Schristos      # compare them to existing versions.
182*9d210927Schristos      if test $from != $parser; then
183*9d210927Schristos        realtarget=$target
184*9d210927Schristos        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
185597410b8Schristos      fi
186597410b8Schristos
187*9d210927Schristos      # Munge "#line" or "#" directives.  Don't let the resulting
188*9d210927Schristos      # debug information point at an absolute srcdir.  Use the real
189*9d210927Schristos      # output file name, not yy.lex.c for instance.  Adjust the
190*9d210927Schristos      # include guards too.
191*9d210927Schristos      sed -e "/^#/!b"                           \
192*9d210927Schristos          -e "s|$input_rx|$input_sub_rx|"       \
193*9d210927Schristos          -e "$sed_fix_filenames"               \
194*9d210927Schristos          -e "$sed_fix_header_guards"           \
195*9d210927Schristos        "$from" >"$target" || ret=$?
196597410b8Schristos
197*9d210927Schristos      # Check whether files must be updated.
198*9d210927Schristos      if test "$from" != "$parser"; then
199597410b8Schristos        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
200*9d210927Schristos          echo "$to is unchanged"
201597410b8Schristos          rm -f "$target"
202597410b8Schristos        else
203*9d210927Schristos          echo "updating $to"
204597410b8Schristos          mv -f "$target" "$realtarget"
205597410b8Schristos        fi
206597410b8Schristos      fi
207597410b8Schristos    else
208*9d210927Schristos      # A missing file is only an error for the parser.  This is a
209*9d210927Schristos      # blatant hack to let us support using "yacc -d".  If -d is not
210*9d210927Schristos      # specified, don't fail when the header file is "missing".
211*9d210927Schristos      if test "$from" = "$parser"; then
212597410b8Schristos        ret=1
213597410b8Schristos      fi
214597410b8Schristos    fi
215597410b8Schristos  done
216597410b8Schristosfi
217597410b8Schristos
218597410b8Schristos# Remove the directory.
219597410b8Schristoscd ..
220597410b8Schristosrm -rf $dirname
221597410b8Schristos
222597410b8Schristosexit $ret
223597410b8Schristos
224597410b8Schristos# Local Variables:
225597410b8Schristos# mode: shell-script
226597410b8Schristos# sh-indentation: 2
227597410b8Schristos# eval: (add-hook 'write-file-hooks 'time-stamp)
228597410b8Schristos# time-stamp-start: "scriptversion="
229597410b8Schristos# time-stamp-format: "%:y-%02m-%02d.%02H"
230597410b8Schristos# time-stamp-time-zone: "UTC"
231597410b8Schristos# time-stamp-end: "; # UTC"
232597410b8Schristos# End:
233