ylwrap 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. #! /bin/sh
  2. # ylwrap - wrapper for lex/yacc invocations.
  3. scriptversion=2012-07-14.08; # 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. get_dirname ()
  28. {
  29. case $1 in
  30. */*|*\\*) printf '%s\n' "$1" | sed -e 's|\([\\/]\)[^\\/]*$|\1|';;
  31. # Otherwise, we want the empty string (not ".").
  32. esac
  33. }
  34. # guard FILE
  35. # ----------
  36. # The CPP macro used to guard inclusion of FILE.
  37. guard()
  38. {
  39. printf '%s\n' "$from" \
  40. | sed \
  41. -e 'y/abcdefghijklmnopqrstuvwxyz/ABCDEFGHIJKLMNOPQRSTUVWXYZ/'\
  42. -e 's/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ]/_/g'
  43. }
  44. # quote_for_sed [STRING]
  45. # ----------------------
  46. # Return STRING (or stdin) quoted to be used as a sed pattern.
  47. quote_for_sed ()
  48. {
  49. case $# in
  50. 0) cat;;
  51. 1) printf '%s\n' "$1";;
  52. esac \
  53. | sed -e 's|[][\\.*]|\\&|g'
  54. }
  55. case "$1" in
  56. '')
  57. echo "$0: No files given. Try '$0 --help' for more information." 1>&2
  58. exit 1
  59. ;;
  60. --basedir)
  61. basedir=$2
  62. shift 2
  63. ;;
  64. -h|--h*)
  65. cat <<\EOF
  66. Usage: ylwrap [--help|--version] INPUT [OUTPUT DESIRED]... -- PROGRAM [ARGS]...
  67. Wrapper for lex/yacc invocations, renaming files as desired.
  68. INPUT is the input file
  69. OUTPUT is one file PROG generates
  70. DESIRED is the file we actually want instead of OUTPUT
  71. PROGRAM is program to run
  72. ARGS are passed to PROG
  73. Any number of OUTPUT,DESIRED pairs may be used.
  74. Report bugs to <bug-automake@gnu.org>.
  75. EOF
  76. exit $?
  77. ;;
  78. -v|--v*)
  79. echo "ylwrap $scriptversion"
  80. exit $?
  81. ;;
  82. esac
  83. # The input.
  84. input="$1"
  85. shift
  86. # We'll later need for a correct munging of "#line" directives.
  87. input_sub_rx=`get_dirname "$input" | quote_for_sed`
  88. case "$input" in
  89. [\\/]* | ?:[\\/]*)
  90. # Absolute path; do nothing.
  91. ;;
  92. *)
  93. # Relative path. Make it absolute.
  94. input="`pwd`/$input"
  95. ;;
  96. esac
  97. input_rx=`get_dirname "$input" | quote_for_sed`
  98. # Since DOS filename conventions don't allow two dots,
  99. # the DOS version of Bison writes out y_tab.c instead of y.tab.c
  100. # and y_tab.h instead of y.tab.h. Test to see if this is the case.
  101. y_tab_nodot=false
  102. if test -f y_tab.c || test -f y_tab.h; then
  103. y_tab_nodot=true
  104. fi
  105. # The parser itself, the first file, is the destination of the .y.c
  106. # rule in the Makefile.
  107. parser=$1
  108. # A sed program to s/FROM/TO/g for all the FROM/TO so that, for
  109. # instance, we rename #include "y.tab.h" into #include "parse.h"
  110. # during the conversion from y.tab.c to parse.c.
  111. rename_sed=
  112. while test "$#" -ne 0; do
  113. if test "$1" = "--"; then
  114. shift
  115. break
  116. fi
  117. from=$1
  118. # Handle y_tab.c and y_tab.h output by DOS
  119. if $y_tab_nodot; then
  120. case $from in
  121. "y.tab.c") from=y_tab.c;;
  122. "y.tab.h") from=y_tab.h;;
  123. esac
  124. fi
  125. shift
  126. to=$1
  127. shift
  128. rename_sed="${rename_sed}s|"`quote_for_sed "$from"`"|$to|g;"
  129. done
  130. # The program to run.
  131. prog="$1"
  132. shift
  133. # Make any relative path in $prog absolute.
  134. case "$prog" in
  135. [\\/]* | ?:[\\/]*) ;;
  136. *[\\/]*) prog="`pwd`/$prog" ;;
  137. esac
  138. # FIXME: add hostname here for parallel makes that run commands on
  139. # other machines. But that might take us over the 14-char limit.
  140. dirname=ylwrap$$
  141. do_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
  142. trap "ret=129; $do_exit" 1
  143. trap "ret=130; $do_exit" 2
  144. trap "ret=141; $do_exit" 13
  145. trap "ret=143; $do_exit" 15
  146. mkdir $dirname || exit 1
  147. cd $dirname
  148. case $# in
  149. 0) "$prog" "$input" ;;
  150. *) "$prog" "$@" "$input" ;;
  151. esac
  152. ret=$?
  153. if test $ret -eq 0; then
  154. for from in *
  155. do
  156. to=`printf '%s\n' "$from" | sed "$rename_sed"`
  157. if test -f "$from"; then
  158. # If $2 is an absolute path name, then just use that,
  159. # otherwise prepend '../'.
  160. case $to in
  161. [\\/]* | ?:[\\/]*) target=$to;;
  162. *) target="../$to";;
  163. esac
  164. # Do not overwrite unchanged header files to avoid useless
  165. # recompilations. Always update the parser itself: it is the
  166. # destination of the .y.c rule in the Makefile. Divert the
  167. # output of all other files to a temporary file so we can
  168. # compare them to existing versions.
  169. if test $from != $parser; then
  170. realtarget="$target"
  171. target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
  172. fi
  173. # Munge "#line" or "#" directives. Don't let the resulting
  174. # debug information point at an absolute srcdir. Use the real
  175. # output file name, not yy.lex.c for instance. Adjust the
  176. # include guards too.
  177. FROM=`guard "$from"`
  178. TARGET=`guard "$to"`
  179. sed -e "/^#/!b" -e "s|$input_rx|$input_sub_rx|" -e "$rename_sed" \
  180. -e "s|$FROM|$TARGET|" "$from" >"$target" || ret=$?
  181. # Check whether files must be updated.
  182. if test "$from" != "$parser"; then
  183. if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
  184. echo "$to is unchanged"
  185. rm -f "$target"
  186. else
  187. echo "updating $to"
  188. mv -f "$target" "$realtarget"
  189. fi
  190. fi
  191. else
  192. # A missing file is only an error for the parser. This is a
  193. # blatant hack to let us support using "yacc -d". If -d is not
  194. # specified, don't fail when the header file is "missing".
  195. if test "$from" = "$parser"; then
  196. ret=1
  197. fi
  198. fi
  199. done
  200. fi
  201. # Remove the directory.
  202. cd ..
  203. rm -rf $dirname
  204. exit $ret
  205. # Local Variables:
  206. # mode: shell-script
  207. # sh-indentation: 2
  208. # eval: (add-hook 'write-file-hooks 'time-stamp)
  209. # time-stamp-start: "scriptversion="
  210. # time-stamp-format: "%:y-%02m-%02d.%02H"
  211. # time-stamp-time-zone: "UTC"
  212. # time-stamp-end: "; # UTC"
  213. # End: