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