substscr 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/bin/sh
  2. # substscr - substitute parameters to create a file from a .in.
  3. #
  4. # Copyright (c) 1999, 2004 Joseph Samuel Myers.
  5. # All rights reserved.
  6. #
  7. # Redistribution and use in source and binary forms, with or without
  8. # modification, are permitted provided that the following conditions
  9. # are met:
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in the
  14. # documentation and/or other materials provided with the distribution.
  15. # 3. The name of the author may not be used to endorse or promote products
  16. # derived from this software without specific prior written permission.
  17. #
  18. # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  19. # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  20. # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. # SUCH DAMAGE.
  29. # First argument is some combination of:
  30. # r to generate makefile rules
  31. # g to generate the file in question
  32. # Second argument gives the style of comment:
  33. # c for C comment
  34. # h for generic # comment
  35. # m for manpage comment
  36. # n for none
  37. # s for shell comment
  38. # Third argument is the source file.
  39. # Fourth argument is the target file.
  40. # The target gets its execute permissions from the source.
  41. set -e
  42. if [ $# != 4 ]; then
  43. echo "usage: $0 action comment-style source target" >&2
  44. exit 1
  45. fi
  46. action="$1"
  47. style="$2"
  48. source="$3"
  49. target="$4"
  50. if echo $action |grep -q g; then
  51. echo "Generating $target from $source"
  52. rm -f $target
  53. case "$style" in
  54. (c)
  55. echo "/* Automatically generated from $source. Do not edit. */" >"$target"
  56. ;;
  57. (h)
  58. echo "# Automatically generated from $source. Do not edit." >"$target"
  59. ;;
  60. (m)
  61. echo ".\\\" Automatically generated from $source. Do not edit." >"$target"
  62. ;;
  63. (n)
  64. : >"$target"
  65. ;;
  66. (s)
  67. echo "#!/bin/sh" >"$target"
  68. echo "# Automatically generated from $source. Do not edit." >>"$target"
  69. ;;
  70. (*)
  71. echo "$0: unknown comment style $style" >&2
  72. exit 1
  73. esac
  74. sed -f subst.sed <"$source" >>"$target"
  75. if [ -x "$source" ]; then
  76. chmod a+x "$target"
  77. fi
  78. fi
  79. if echo $action |grep -q r; then
  80. # Append to subst.rules.
  81. cat >>subst.rules <<EOF
  82. $target: $source subst.sed substscr
  83. ./substscr g $style $source $target
  84. EOF
  85. fi