ylwrap 6.8 KB

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