ylwrap 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. #! /bin/sh
  2. # ylwrap - wrapper for lex/yacc invocations.
  3. scriptversion=2013-01-12.17; # UTC
  4. # Copyright (C) 1996-2014 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. # The parser itself, the first file, is the destination of the .y.c
  100. # rule in the Makefile.
  101. parser=$1
  102. # A sed program to s/FROM/TO/g for all the FROM/TO so that, for
  103. # instance, we rename #include "y.tab.h" into #include "parse.h"
  104. # during the conversion from y.tab.c to parse.c.
  105. sed_fix_filenames=
  106. # Also rename header guards, as Bison 2.7 for instance uses its header
  107. # guard in its implementation file.
  108. sed_fix_header_guards=
  109. while test $# -ne 0; do
  110. if test x"$1" = x"--"; then
  111. shift
  112. break
  113. fi
  114. from=$1
  115. shift
  116. to=$1
  117. shift
  118. sed_fix_filenames="${sed_fix_filenames}s|"`quote_for_sed "$from"`"|$to|g;"
  119. sed_fix_header_guards="${sed_fix_header_guards}s|"`guard "$from"`"|"`guard "$to"`"|g;"
  120. done
  121. # The program to run.
  122. prog=$1
  123. shift
  124. # Make any relative path in $prog absolute.
  125. case $prog in
  126. [\\/]* | ?:[\\/]*) ;;
  127. *[\\/]*) prog=`pwd`/$prog ;;
  128. esac
  129. dirname=ylwrap$$
  130. do_exit="cd '`pwd`' && rm -rf $dirname > /dev/null 2>&1;"' (exit $ret); exit $ret'
  131. trap "ret=129; $do_exit" 1
  132. trap "ret=130; $do_exit" 2
  133. trap "ret=141; $do_exit" 13
  134. trap "ret=143; $do_exit" 15
  135. mkdir $dirname || exit 1
  136. cd $dirname
  137. case $# in
  138. 0) "$prog" "$input" ;;
  139. *) "$prog" "$@" "$input" ;;
  140. esac
  141. ret=$?
  142. if test $ret -eq 0; then
  143. for from in *
  144. do
  145. to=`printf '%s\n' "$from" | sed "$sed_fix_filenames"`
  146. if test -f "$from"; then
  147. # If $2 is an absolute path name, then just use that,
  148. # otherwise prepend '../'.
  149. case $to in
  150. [\\/]* | ?:[\\/]*) target=$to;;
  151. *) target=../$to;;
  152. esac
  153. # Do not overwrite unchanged header files to avoid useless
  154. # recompilations. Always update the parser itself: it is the
  155. # destination of the .y.c rule in the Makefile. Divert the
  156. # output of all other files to a temporary file so we can
  157. # compare them to existing versions.
  158. if test $from != $parser; then
  159. realtarget=$target
  160. target=tmp-`printf '%s\n' "$target" | sed 's|.*[\\/]||g'`
  161. fi
  162. # Munge "#line" or "#" directives. Don't let the resulting
  163. # debug information point at an absolute srcdir. Use the real
  164. # output file name, not yy.lex.c for instance. Adjust the
  165. # include guards too.
  166. sed -e "/^#/!b" \
  167. -e "s|$input_rx|$input_sub_rx|" \
  168. -e "$sed_fix_filenames" \
  169. -e "$sed_fix_header_guards" \
  170. "$from" >"$target" || ret=$?
  171. # Check whether files must be updated.
  172. if test "$from" != "$parser"; then
  173. if test -f "$realtarget" && cmp -s "$realtarget" "$target"; then
  174. echo "$to is unchanged"
  175. rm -f "$target"
  176. else
  177. echo "updating $to"
  178. mv -f "$target" "$realtarget"
  179. fi
  180. fi
  181. else
  182. # A missing file is only an error for the parser. This is a
  183. # blatant hack to let us support using "yacc -d". If -d is not
  184. # specified, don't fail when the header file is "missing".
  185. if test "$from" = "$parser"; then
  186. ret=1
  187. fi
  188. fi
  189. done
  190. fi
  191. # Remove the directory.
  192. cd ..
  193. rm -rf $dirname
  194. exit $ret
  195. # Local Variables:
  196. # mode: shell-script
  197. # sh-indentation: 2
  198. # eval: (add-hook 'write-file-hooks 'time-stamp)
  199. # time-stamp-start: "scriptversion="
  200. # time-stamp-format: "%:y-%02m-%02d.%02H"
  201. # time-stamp-time-zone: "UTC"
  202. # time-stamp-end: "; # UTC"
  203. # End: