ylwrap 6.2 KB

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