rc.func 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ##
  2. ## @l_prefix@/etc/rc.func -- Run-Command Helper Functions
  3. ## Copyright (c) 2000-2002 Cable & Wireless Deutschland GmbH
  4. ## Copyright (c) 2000-2002 The OpenPKG Project <http://www.openpkg.org/>
  5. ## Copyright (c) 2000-2002 Ralf S. Engelschall <rse@engelschall.com>
  6. ##
  7. ## Permission to use, copy, modify, and distribute this software for
  8. ## any purpose with or without fee is hereby granted, provided that
  9. ## the above copyright notice and this permission notice appear in all
  10. ## copies.
  11. ##
  12. ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  13. ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  14. ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
  16. ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  17. ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  18. ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  19. ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  20. ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  21. ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  23. ## SUCH DAMAGE.
  24. ##
  25. #
  26. # display an error message.
  27. #
  28. # Usage: opErr <message>
  29. # Example: opErr "invalid command line"
  30. #
  31. opErr () {
  32. echo "OpenPKG:ERROR: $*" 1>&2
  33. exit 1
  34. }
  35. #
  36. # display a warning message.
  37. #
  38. # Usage: opWarn <message>
  39. # Example: opWarn "unknown variable"
  40. #
  41. opWarn () {
  42. echo "OpenPKG:WARNING: $*" 1>&2
  43. }
  44. #
  45. # append (or optionally prepend) one or more directories (optionally
  46. # have to be existing) to a colon-separated path variable. In case a
  47. # directory already exists, it is first removed.
  48. #
  49. # Usage: opPathAdd [-p] [-e] <variable> <dir> [<dir> ...]
  50. # Example: opPathAdd -e PATH /bin /sbin /usr/bin /usr/sbin /usr/ccs/bin
  51. #
  52. opPathAdd () {
  53. _prepend=0
  54. _exists=0
  55. while [ $# -gt 0 ]; do
  56. case $1 in
  57. -p ) _prepend=1; shift ;;
  58. -e ) _exists=1; shift ;;
  59. * ) break ;;
  60. esac
  61. done
  62. _var="$1"
  63. shift
  64. _edit_del=""
  65. _edit_add=""
  66. for _dir in "$@"; do
  67. if [ ".${_exists}" = .1 ] && [ ! -d "${_dir}" ]; then
  68. continue
  69. fi
  70. _edit_del="${_edit_del} -e 's;^${_dir}\$;;' -e 's;^${_dir}:;;'"
  71. _edit_del="${_edit_del} -e 's;:${_dir}:;:;' -e 's;:${_dir}\$;;'"
  72. if [ ".${_prepend}" = .0 ]; then
  73. _edit_add="${_edit_add} -e 's;\$;:${_dir};'"
  74. else
  75. _edit_add="-e 's;^;${_dir}:;' ${_edit_add}"
  76. fi
  77. done
  78. if [ ".${_edit_del}${_edit_add}" != . ]; then
  79. eval "${_var}=\`echo \"\$${_var}\" | sed ${_edit_del} ${_edit_add}\`"
  80. fi
  81. unset _prepend _exists _var _edit_del _edit_add _dir
  82. }
  83. #
  84. # remove one or more directories from a colon-separated path variable
  85. #
  86. # Usage: opPathDel <variable> <dir> [<dir> ...]
  87. # Example: opPathDel PATH /bin /sbin /usr/bin /usr/sbin /usr/ccs/bin
  88. #
  89. opPathDel () {
  90. _var="$1"
  91. shift
  92. _edit=""
  93. for _dir in "$@"; do
  94. _edit="${_edit} -e 's;^${_dir}\$;;' -e 's;^${_dir}:;;'"
  95. _edit="${_edit} -e 's;:${_dir}:;:;' -e 's;:${_dir}\$;;'"
  96. done
  97. eval "${_var}=\`echo \"\$${_var}\" | sed ${_edit}\`"
  98. unset _var _edit _dir
  99. }
  100. #
  101. # check whether a variable contains a positive "Yes" value.
  102. #
  103. # Usage: opVarIsYes <variable>
  104. # Example: if opVarIsYes foo; then ...
  105. #
  106. opVarIsYes () {
  107. _var="${1}"
  108. eval "_val=\"\$${_var}\""
  109. case "${_val}" in
  110. [Yy][Ee][Ss] | [Tt][Rr][Uu][Ee] | [Oo][Nn] | 1 )
  111. unset _var _val
  112. return 0
  113. ;;
  114. [Nn][Oo] | [Ff][Aa][Ll][Ss][Ee] | [Oo][Ff][Ff] | 0 )
  115. unset _var _val
  116. return 1
  117. ;;
  118. *)
  119. opWarn "variable \$${_var} is not set properly."
  120. unset _var _val
  121. return 1
  122. ;;
  123. esac
  124. }
  125. #
  126. # check whether a service is enabled.
  127. #
  128. # Usage: opServiceEnabled <variable>
  129. # Example: if opServiceEnabled openssh; then ...
  130. #
  131. opServiceEnabled () {
  132. opVarIsYes ${1}_enable
  133. }
  134. #
  135. # generate temporary file directory
  136. #
  137. # Usage: opTmpDirGen <program>
  138. # Example: opTmpDirGen openssh
  139. #
  140. opTmpDirGen () {
  141. _tmpdir="@l_prefix@/RPM/TMP/${1}"
  142. rm -rf ${_tmpdir} >/dev/null 2>&1 || true
  143. mkdir ${_tmpdir} >/dev/null 2>&1 || true
  144. chmod 700 ${_tmpdir} >/dev/null 2>&1 || true
  145. }
  146. #
  147. # set filename of temporary file
  148. #
  149. # Usage: opTmpDirFile <program> <filename> <variable>
  150. # Example: opTmpDirFile openssh 0 tmpfile
  151. #
  152. opTmpDirFile () {
  153. _tmpdir="@l_prefix@/RPM/TMP/${1}"
  154. eval "${3}=\"${_tmpdir}/${2}\""
  155. touch "${_tmpdir}/${2}" >/dev/null 2>&1 || true
  156. chmod 700 "${_tmpdir}/${2}" >/dev/null 2>&1 || true
  157. }
  158. #
  159. # delete temporary file directory
  160. #
  161. # Usage: opTmpDirDel <program>
  162. # Example: opTmpDirDel openssh
  163. #
  164. opTmpDirDel () {
  165. _tmpdir="@l_prefix@/RPM/TMP/${1}"
  166. rm -rf ${_tmpdir} >/dev/null 2>&1 || true
  167. }