prefix.eclass: extend the hprefixify to accept quotes.
authorBenda Xu <heroxbd@gentoo.org>
Mon, 4 Sep 2017 06:07:10 +0000 (15:07 +0900)
committerBenda Xu <heroxbd@gentoo.org>
Mon, 4 Sep 2017 06:27:34 +0000 (15:27 +0900)
  also apply the pattern substitution to beginning of lines.

eclass/prefix.eclass

index 56aedc81422ab0c3476df69b7d6897955e0b4c84..eb5843182f318369c2b9d4f2b6d9b595e054b7ea 100644 (file)
@@ -48,7 +48,8 @@ eprefixify() {
 }
 
 # @FUNCTION: hprefixify
-# @USAGE: [ -w <line match> ] [-e <extended regex> ] <list of files>
+# @USAGE: [ -w <line match> ] [ -e <extended regex> ] [ -q <quotation char> ]
+#      <list of files>
 # @DESCRIPTION:
 # Tries a set of heuristics to prefixify the given files. Dies if no
 # arguments are given, a file does not exist, or changing a file failed.
@@ -56,7 +57,10 @@ eprefixify() {
 # Additional extended regular expression can be passed by -e or
 # environment variable PREFIX_EXTRA_REGEX.  The default heuristics can
 # be constrained to lines that match a sed expression passed by -w or
-# environment variable PREFIX_LINE_MATCH.
+# environment variable PREFIX_LINE_MATCH.  Quotation characters can be
+# specified by -q or environment variable PREFIX_QUOTE_CHAR, unless
+# EPREFIX is empty.
+#
 # @EXAMPLE:
 # Only prefixify the 30th line,
 #   hprefixify -w 30 configure
@@ -64,18 +68,21 @@ eprefixify() {
 #   hprefixify -w "/PATH/" configure
 # Also delete all the /opt/gnu search paths,
 #   hprefixify -e "/\/opt\/gnu/d" configure
+# Quote the inserted EPREFIX
+#   hprefixify -q '"' etc/profile
 hprefixify() {
        use prefix || return 0
 
-       local PREFIX_EXTRA_REGEX PREFIX_LINE_MATCH xl=() x
+       local xl=() x
        while [[ $# -gt 0 ]]; do
                case $1 in
-                       -e)
-                               PREFIX_EXTRA_REGEX="$2"
+                       -e) local PREFIX_EXTRA_REGEX="$2"
+                               shift
+                               ;;
+                       -w) local PREFIX_LINE_MATCH="$2"
                                shift
                                ;;
-                       -w)
-                               PREFIX_LINE_MATCH="$2"
+                       -q) local PREFIX_QUOTE_CHAR="${EPREFIX:+$2}"
                                shift
                                ;;
                        *)
@@ -84,6 +91,8 @@ hprefixify() {
                esac
                shift
        done
+       local dirs="/(usr|lib(|[onx]?32|n?64)|etc|bin|sbin|var|opt|run)" \
+                 eprefix="${PREFIX_QUOTE_CHAR}${EPREFIX}${PREFIX_QUOTE_CHAR}"
 
        [[ ${#xl[@]} -lt 1 ]] && die "at least one file operand is required"
        einfo "Adjusting to prefix ${EPREFIX:-/}"
@@ -91,7 +100,8 @@ hprefixify() {
                if [[ -e ${x} ]] ; then
                        ebegin "  ${x##*/}"
                        sed -r \
-                               -e "${PREFIX_LINE_MATCH}s,([^[:alnum:]}\)\.])/(usr|lib(|[onx]?32|n?64)|etc|bin|sbin|var|opt|run),\1${EPREFIX}/\2,g" \
+                               -e "${PREFIX_LINE_MATCH}s,([^[:alnum:]}\)\.])${dirs},\1${eprefix}/\2,g" \
+                               -e "${PREFIX_LINE_MATCH}s,^${dirs},${eprefix}/\1," \
                                -e "${PREFIX_EXTRA_REGEX}" \
                                -i "${x}"
                        eend $? || die "failed to prefixify ${x}"