xref: /netbsd-src/crypto/external/bsd/heimdal/dist/ylwrap (revision dd75ac5b443e967e26b4d18cc8cd5eb98512bfbf)
1929c70cbSchristos#! /bin/sh
2929c70cbSchristos# ylwrap - wrapper for lex/yacc invocations.
3929c70cbSchristos
4*dd75ac5bSchristosscriptversion=2018-03-07.03; # UTC
5929c70cbSchristos
6*dd75ac5bSchristos# Copyright (C) 1996-2021 Free Software Foundation, Inc.
7929c70cbSchristos#
8929c70cbSchristos# Written by Tom Tromey <tromey@cygnus.com>.
9929c70cbSchristos#
10929c70cbSchristos# This program is free software; you can redistribute it and/or modify
11929c70cbSchristos# it under the terms of the GNU General Public License as published by
12929c70cbSchristos# the Free Software Foundation; either version 2, or (at your option)
13929c70cbSchristos# any later version.
14929c70cbSchristos#
15929c70cbSchristos# This program is distributed in the hope that it will be useful,
16929c70cbSchristos# but WITHOUT ANY WARRANTY; without even the implied warranty of
17929c70cbSchristos# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18929c70cbSchristos# GNU General Public License for more details.
19929c70cbSchristos#
20929c70cbSchristos# You should have received a copy of the GNU General Public License
21*dd75ac5bSchristos# along with this program.  If not, see <https://www.gnu.org/licenses/>.
22929c70cbSchristos
23929c70cbSchristos# As a special exception to the GNU General Public License, if you
24929c70cbSchristos# distribute this file as part of a program that contains a
25929c70cbSchristos# configuration script generated by Autoconf, you may include it under
26929c70cbSchristos# the same distribution terms that you use for the rest of that program.
27929c70cbSchristos
28929c70cbSchristos# This file is maintained in Automake, please report
29929c70cbSchristos# bugs to <bug-automake@gnu.org> or send patches to
30929c70cbSchristos# <automake-patches@gnu.org>.
31929c70cbSchristos
32929c70cbSchristosget_dirname ()
33929c70cbSchristos{
34929c70cbSchristos  case $1 in
35929c70cbSchristos    */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
36929c70cbSchristos    # Otherwise,  we want the empty string (not ".").
37929c70cbSchristos  esac
38929c70cbSchristos}
39929c70cbSchristos
40929c70cbSchristos# guard FILE
41929c70cbSchristos# ----------
42929c70cbSchristos# The CPP macro used to guard inclusion of FILE.
43929c70cbSchristosguard ()
44929c70cbSchristos{
45929c70cbSchristos  printf '%s\n' "$1"                                                    \
46929c70cbSchristos    | sed                                                               \
47929c70cbSchristos        -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'   \
48929c70cbSchristos        -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'                        \
49929c70cbSchristos        -e 's/__*/_/g'
50929c70cbSchristos}
51929c70cbSchristos
52929c70cbSchristos# quote_for_sed [STRING]
53929c70cbSchristos# ----------------------
54929c70cbSchristos# Return STRING (or stdin) quoted to be used as a sed pattern.
55929c70cbSchristosquote_for_sed ()
56929c70cbSchristos{
57929c70cbSchristos  case $# in
58929c70cbSchristos    0) cat;;
59929c70cbSchristos    1) printf '%s\n' "$1";;
60929c70cbSchristos  esac \
61929c70cbSchristos    | sed -e 's|[][\\.*]|\\&|g'
62929c70cbSchristos}
63929c70cbSchristos
64929c70cbSchristoscase "$1" in
65929c70cbSchristos  '')
66929c70cbSchristos    echo "$0: No files given.  Try '$0 --help' for more information." 1>&2
67929c70cbSchristos    exit 1
68929c70cbSchristos    ;;
69929c70cbSchristos  --basedir)
70929c70cbSchristos    basedir=$2
71929c70cbSchristos    shift 2
72929c70cbSchristos    ;;
73929c70cbSchristos  -h|--h*)
74929c70cbSchristos    cat <<\EOF
75929c70cbSchristosUsage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
76929c70cbSchristos
77929c70cbSchristosWrapper for lex/yacc invocations, renaming files as desired.
78929c70cbSchristos
79929c70cbSchristos  INPUT is the input file
80929c70cbSchristos  OUTPUT is one file PROG generates
81929c70cbSchristos  DESIRED is the file we actually want instead of OUTPUT
82929c70cbSchristos  PROGRAM is program to run
83929c70cbSchristos  ARGS are passed to PROG
84929c70cbSchristos
85929c70cbSchristosAny number of OUTPUT,DESIRED pairs may be used.
86929c70cbSchristos
87929c70cbSchristosReport bugs to <bug-automake@gnu.org>.
88929c70cbSchristosEOF
89929c70cbSchristos    exit $?
90929c70cbSchristos    ;;
91929c70cbSchristos  -v|--v*)
92929c70cbSchristos    echo "ylwrap $scriptversion"
93929c70cbSchristos    exit $?
94929c70cbSchristos    ;;
95929c70cbSchristosesac
96929c70cbSchristos
97929c70cbSchristos
98929c70cbSchristos# The input.
99929c70cbSchristosinput=$1
100929c70cbSchristosshift
101929c70cbSchristos# We'll later need for a correct munging of "#line" directives.
102929c70cbSchristosinput_sub_rx=`get_dirname "$input" | quote_for_sed`
103929c70cbSchristoscase $input in
104929c70cbSchristos  [\\/]* | ?:[\\/]*)
105929c70cbSchristos    # Absolute path; do nothing.
106929c70cbSchristos    ;;
107929c70cbSchristos  *)
108929c70cbSchristos    # Relative path.  Make it absolute.
109929c70cbSchristos    input=`pwd`/$input
110929c70cbSchristos    ;;
111929c70cbSchristosesac
112929c70cbSchristosinput_rx=`get_dirname "$input" | quote_for_sed`
113929c70cbSchristos
114929c70cbSchristos# Since DOS filename conventions don't allow two dots,
115929c70cbSchristos# the DOS version of Bison writes out y_tab.c instead of y.tab.c
116929c70cbSchristos# and y_tab.h instead of y.tab.h. Test to see if this is the case.
117929c70cbSchristosy_tab_nodot=false
118929c70cbSchristosif test -f y_tab.c || test -f y_tab.h; then
119929c70cbSchristos  y_tab_nodot=true
120929c70cbSchristosfi
121929c70cbSchristos
122929c70cbSchristos# The parser itself, the first file, is the destination of the .y.c
123929c70cbSchristos# rule in the Makefile.
124929c70cbSchristosparser=$1
125929c70cbSchristos
126929c70cbSchristos# A sed program to s/FROM/TO/g for all the FROM/TO so that, for
127929c70cbSchristos# instance, we rename #include "y.tab.h" into #include "parse.h"
128929c70cbSchristos# during the conversion from y.tab.c to parse.c.
129929c70cbSchristossed_fix_filenames=
130929c70cbSchristos
131929c70cbSchristos# Also rename header guards, as Bison 2.7 for instance uses its header
132929c70cbSchristos# guard in its implementation file.
133929c70cbSchristossed_fix_header_guards=
134929c70cbSchristos
135929c70cbSchristoswhile test $# -ne 0; do
136929c70cbSchristos  if test x"$1" = x"--"; then
137929c70cbSchristos    shift
138929c70cbSchristos    break
139929c70cbSchristos  fi
140929c70cbSchristos  from=$1
141929c70cbSchristos  # Handle y_tab.c and y_tab.h output by DOS
142929c70cbSchristos  if $y_tab_nodot; then
143929c70cbSchristos    case $from in
144929c70cbSchristos      "y.tab.c") from=y_tab.c;;
145929c70cbSchristos      "y.tab.h") from=y_tab.h;;
146929c70cbSchristos    esac
147929c70cbSchristos  fi
148929c70cbSchristos  shift
149929c70cbSchristos  to=$1
150929c70cbSchristos  shift
151929c70cbSchristos  sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
152929c70cbSchristos  sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
153929c70cbSchristosdone
154929c70cbSchristos
155929c70cbSchristos# The program to run.
156929c70cbSchristosprog=$1
157929c70cbSchristosshift
158929c70cbSchristos# Make any relative path in $prog absolute.
159929c70cbSchristoscase $prog in
160929c70cbSchristos  [\\/]* | ?:[\\/]*) ;;
161929c70cbSchristos  *[\\/]*) prog=`pwd`/$prog ;;
162929c70cbSchristosesac
163929c70cbSchristos
164929c70cbSchristosdirname=ylwrap$$
165929c70cbSchristosdo_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
166929c70cbSchristostrap "ret=129; $do_exit" 1
167929c70cbSchristostrap "ret=130; $do_exit" 2
168929c70cbSchristostrap "ret=141; $do_exit" 13
169929c70cbSchristostrap "ret=143; $do_exit" 15
170929c70cbSchristosmkdir $dirname || exit 1
171929c70cbSchristos
172929c70cbSchristoscd $dirname
173929c70cbSchristos
174929c70cbSchristoscase $# in
175929c70cbSchristos  0) "$prog" "$input" ;;
176929c70cbSchristos  *) "$prog" "$@" "$input" ;;
177929c70cbSchristosesac
178929c70cbSchristosret=$?
179929c70cbSchristos
180929c70cbSchristosif test $ret -eq 0; then
181929c70cbSchristos  for from in *
182929c70cbSchristos  do
183929c70cbSchristos    to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
184929c70cbSchristos    if test -f "$from"; then
185929c70cbSchristos      # If $2 is an absolute path name, then just use that,
186929c70cbSchristos      # otherwise prepend '../'.
187929c70cbSchristos      case $to in
188929c70cbSchristos        [\\/]* | ?:[\\/]*) target=$to;;
189929c70cbSchristos        *) target=../$to;;
190929c70cbSchristos      esac
191929c70cbSchristos
192929c70cbSchristos      # Do not overwrite unchanged header files to avoid useless
193929c70cbSchristos      # recompilations.  Always update the parser itself: it is the
194929c70cbSchristos      # destination of the .y.c rule in the Makefile.  Divert the
195929c70cbSchristos      # output of all other files to a temporary file so we can
196929c70cbSchristos      # compare them to existing versions.
197929c70cbSchristos      if test $from != $parser; then
198929c70cbSchristos        realtarget=$target
199929c70cbSchristos        target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
200929c70cbSchristos      fi
201929c70cbSchristos
202929c70cbSchristos      # Munge "#line" or "#" directives.  Don't let the resulting
203929c70cbSchristos      # debug information point at an absolute srcdir.  Use the real
204929c70cbSchristos      # output file name, not yy.lex.c for instance.  Adjust the
205929c70cbSchristos      # include guards too.
206929c70cbSchristos      sed -e "/^#/!b"                           \
207929c70cbSchristos          -e "s|$input_rx|$input_sub_rx|"       \
208929c70cbSchristos          -e "$sed_fix_filenames"               \
209929c70cbSchristos          -e "$sed_fix_header_guards"           \
210929c70cbSchristos        "$from" >"$target" || ret=$?
211929c70cbSchristos
212929c70cbSchristos      # Check whether files must be updated.
213929c70cbSchristos      if test "$from" != "$parser"; then
214929c70cbSchristos        if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
215929c70cbSchristos          echo "$to is unchanged"
216929c70cbSchristos          rm -f "$target"
217929c70cbSchristos        else
218929c70cbSchristos          echo "updating $to"
219929c70cbSchristos          mv -f "$target" "$realtarget"
220929c70cbSchristos        fi
221929c70cbSchristos      fi
222929c70cbSchristos    else
223929c70cbSchristos      # A missing file is only an error for the parser.  This is a
224929c70cbSchristos      # blatant hack to let us support using "yacc -d".  If -d is not
225929c70cbSchristos      # specified, don't fail when the header file is "missing".
226929c70cbSchristos      if test "$from" = "$parser"; then
227929c70cbSchristos        ret=1
228929c70cbSchristos      fi
229929c70cbSchristos    fi
230929c70cbSchristos  done
231929c70cbSchristosfi
232929c70cbSchristos
233929c70cbSchristos# Remove the directory.
234929c70cbSchristoscd ..
235929c70cbSchristosrm -rf $dirname
236929c70cbSchristos
237929c70cbSchristosexit $ret
238929c70cbSchristos
239929c70cbSchristos# Local Variables:
240929c70cbSchristos# mode: shell-script
241929c70cbSchristos# sh-indentation: 2
242*dd75ac5bSchristos# eval: (add-hook 'before-save-hook 'time-stamp)
243929c70cbSchristos# time-stamp-start: "scriptversion="
244929c70cbSchristos# time-stamp-format: "%:y-%02m-%02d.%02H"
245929c70cbSchristos# time-stamp-time-zone: "UTC0"
246929c70cbSchristos# time-stamp-end: "; # UTC"
247929c70cbSchristos# End:
248