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