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