You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
6.7KB

  1. #! /bin/sh
  2. # ylwrap - wrapper for lex/yacc invocations.
  3. scriptversion=2016-01-11.22; # UTC
  4. # Copyright (C) 1996-2017 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 x"$1" = x"--"; 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. dirname=ylwrap$$
  144. do_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
  145. trap "ret=129; $do_exit" 1
  146. trap "ret=130; $do_exit" 2
  147. trap "ret=141; $do_exit" 13
  148. trap "ret=143; $do_exit" 15
  149. mkdir $dirname || exit 1
  150. cd $dirname
  151. case $# in
  152. 0) "$prog" "$input" ;;
  153. *) "$prog" "$@" "$input" ;;
  154. esac
  155. ret=$?
  156. if test $ret -eq 0; then
  157. for from in *
  158. do
  159. to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
  160. if test -f "$from"; then
  161. # If $2 is an absolute path name, then just use that,
  162. # otherwise prepend '../'.
  163. case $to in
  164. [\\/]* | ?:[\\/]*) target=$to;;
  165. *) target=../$to;;
  166. esac
  167. # Do not overwrite unchanged header files to avoid useless
  168. # recompilations. Always update the parser itself: it is the
  169. # destination of the .y.c rule in the Makefile. Divert the
  170. # output of all other files to a temporary file so we can
  171. # compare them to existing versions.
  172. if test $from != $parser; then
  173. realtarget=$target
  174. target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
  175. fi
  176. # Munge "#line" or "#" directives. Don't let the resulting
  177. # debug information point at an absolute srcdir. Use the real
  178. # output file name, not yy.lex.c for instance. Adjust the
  179. # include guards too.
  180. sed -e "/^#/!b" \
  181. -e "s|$input_rx|$input_sub_rx|" \
  182. -e "$sed_fix_filenames" \
  183. -e "$sed_fix_header_guards" \
  184. "$from" >"$target" || ret=$?
  185. # Check whether files must be updated.
  186. if test "$from" != "$parser"; then
  187. if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
  188. echo "$to is unchanged"
  189. rm -f "$target"
  190. else
  191. echo "updating $to"
  192. mv -f "$target" "$realtarget"
  193. fi
  194. fi
  195. else
  196. # A missing file is only an error for the parser. This is a
  197. # blatant hack to let us support using "yacc -d". If -d is not
  198. # specified, don't fail when the header file is "missing".
  199. if test "$from" = "$parser"; then
  200. ret=1
  201. fi
  202. fi
  203. done
  204. fi
  205. # Remove the directory.
  206. cd ..
  207. rm -rf $dirname
  208. exit $ret
  209. # Local Variables:
  210. # mode: shell-script
  211. # sh-indentation: 2
  212. # eval: (add-hook 'write-file-hooks 'time-stamp)
  213. # time-stamp-start: "scriptversion="
  214. # time-stamp-format: "%:y-%02m-%02d.%02H"
  215. # time-stamp-time-zone: "UTC0"
  216. # time-stamp-end: "; # UTC"
  217. # End: