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.

path.functions 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # $Id$
  2. # Copyright (c) 2000,2001 Peter B. West <pbwest@powerup.com.au>
  3. # N.B The following PATH manipulation functions
  4. # 1) MUST NOT have a vertical bar `|' in any specified path
  5. # 2) Handle the empty path "", but do not equate the empty path with "."
  6. # These functions have been written to cope with spaces in the file paths.
  7. # Clear path element given as arg from PATH
  8. # N.B. path element MUST NOT contain a vertical bar `|'
  9. rem_path() {
  10. if [ $# -lt 1 ]; then return; fi
  11. rem_vpath PATH "$@"
  12. }
  13. # Clear path element(s) given as arg 2 (3 ...) from the path named in arg1
  14. # N.B. path element MUST NOT contain a vertical bar `|'
  15. rem_vpath() {
  16. if [ $# -lt 2 ]; then return; fi
  17. eval _AP_=\"\$$1\"
  18. _P_=$1
  19. shift
  20. while [ $# -ge 1 ]; do
  21. _RP_="$1"
  22. shift
  23. _AP_=`echo $_AP_|
  24. sed ':loop
  25. {s|:'"$_RP_"':|:|g
  26. t loop
  27. }
  28. s|^'"$_RP_"':||
  29. s|:'"$_RP_"'$||
  30. s|^'"$_RP_"'$||'`
  31. eval $_P_=\"$_AP_\"
  32. done
  33. unset _AP_ _P_ _RP_
  34. }
  35. # To path named in $1, append path element arg(s), replacing all existing
  36. # instances in the named path
  37. # N.B. path elements MUST NOT contain a vertical bar `|'
  38. app_vpath() {
  39. _VP_=$1; shift
  40. eval _VAL_=\"\$$_VP_\"
  41. for el
  42. do
  43. rem_vpath _VAL_ "$el"
  44. _VAL_="${_VAL_:+${_VAL_}:}$el"
  45. done
  46. eval ${_VP_}=\"$_VAL_\"
  47. export $_VP_
  48. unset _VP_ _VAL_
  49. }
  50. # Append path element(s) given as args to PATH, replacing all existing
  51. # instances in the PATH
  52. # N.B. path elements MUST NOT contain a vertical bar `|'
  53. app_path() {
  54. app_vpath PATH "$@"
  55. }
  56. # To path named in $1, prepend path element arg(s), replacing all existing
  57. # instances in the named path. Elements will be appear in the PATH in
  58. # argument order.
  59. # N.B. path elements MUST NOT contain a vertical bar `|'
  60. pre_vpath() {
  61. _VP_=$1; shift
  62. eval _VAL_=\"\$$_VP_\"
  63. _elx_=0
  64. while [ $# -gt 0 ]
  65. do
  66. _elx_=`expr $_elx_ + 1`
  67. eval _VAL_$_elx_=\"\$1\"
  68. shift
  69. done
  70. while [ $_elx_ -gt 0 ]
  71. do
  72. eval _el_=\"\$_VAL_$_elx_\"
  73. unset _VAL_$_elx_
  74. _elx_=`expr $_elx_ - 1`
  75. rem_vpath _VAL_ "$_el_"
  76. _VAL_="$_el_${_VAL_:+:${_VAL_}}"
  77. done
  78. eval ${_VP_}=\"$_VAL_\"
  79. export $_VP_
  80. unset _VP_ _VAL_
  81. }
  82. # Prepend path element(s) given as args to PATH, replacing all existing
  83. # instances in the PATH. N.B. elements will be appear in the PATH in
  84. # REVERSE argument order.
  85. # N.B. path elements MUST NOT contain a vertical bar `|'
  86. pre_path() {
  87. pre_vpath PATH "$@"
  88. }
  89. # This function is no longer used. Originally written for clean_vpath
  90. # in order to process the arguments to pre_vpath in reverse order, it
  91. # has been superseded by the modification to pre_vpath which processes
  92. # its args in reverse order so that the argument list order will be
  93. # preserved at the front of the path
  94. # echo arguments in reverse order. Args echoed are surrounded by
  95. # single quotes - to provide these successfully to a subsequent function
  96. # or command, submit the lot to an eval.
  97. # E.g. eval command `reverse "arg 1" arg2 "This is arg 3"`
  98. # will supply three arguments to the echo.
  99. # This will not preserve multiple spaces in the individal arguments.
  100. # For that, use
  101. # eval command "`reverse 'arg 1' arg2 'This is arg 3'`"
  102. # I.e. use single quotes around the individual args, and surround the
  103. # whole with double quotes; or
  104. # var=`reverse "arg 1" arg2 "This is arg 3"`
  105. # eval command "$var"
  106. # This will BREAK if the arguments contain \<char> sequences which represent
  107. # valid escaped characters, e.g. \\ or \t
  108. # Variables used: _ix_ _o_ _v_ _stk_1 .. _stk_$#
  109. reverseargs() {
  110. _ix_=0
  111. while [ $# -gt 0 ]
  112. do
  113. _ix_=`expr $_ix_ + 1`
  114. eval _stk_$_ix_=\"\$1\"
  115. shift
  116. done
  117. # No need to worry about the extra space at the beginning
  118. while [ $_ix_ -gt 0 ]
  119. do
  120. eval _v_=\"\$_stk_$_ix_\"
  121. unset _stk_$_ix_
  122. _o_="$_o_ '$_v_'"
  123. _ix_=`expr $_ix_ - 1`
  124. done
  125. echo "$_o_"
  126. unset _o_ _v_ _ix_
  127. }
  128. # Clean a path - run pre_vpath with every current element of the path
  129. # in reverse order
  130. # This removes all duplicates in the path, and leaves the first instance
  131. # of a path element in its original relative place - later ones are deleted.
  132. clean_vpath() {
  133. _CVP_=$1; shift
  134. # _CVP_ contains the name of the path
  135. eval _CVAL_=\"\$$_CVP_\"
  136. # _CVAL_ contains the value of the path
  137. _CVAL_=`echo "$_CVAL_"|sed 's/^/"/
  138. s/:/" "/g
  139. s/$/"/'`
  140. eval pre_vpath $_CVP_ "$_CVAL_"
  141. unset _CVAL_ _CVP_
  142. }
  143. clean_path() {
  144. clean_vpath PATH
  145. }