Implement replace and replace_sub to allow for keyword/value/replacement
authorRoy Marples <roy@marples.name>
Tue, 28 Oct 2014 21:54:58 +0000 (21:54 +0000)
committerRoy Marples <roy@marples.name>
Tue, 28 Oct 2014 21:54:58 +0000 (21:54 +0000)
resolvconf.conf.5.in
resolvconf.in

index cea4dc38269c3eb3a1e04ce57f09ecd3388effc2..c6ee6c391ad6e209629cb660e7914087b2a9b3d0 100644 (file)
@@ -94,6 +94,34 @@ Useful for VPN domains.
 This is equivalent to the
 .Nm resolvconf -p
 option.
+.It Sy replace
+Is a space separated list of replacement keywords. The syntax is this:
+.Va $keyword Ns / Ns Va $match Ns / Ns Va $replacement
+.Pp
+Example, given this resolv.conf:
+.D1 domain foo.org
+.D1 search foo.org dead.beef
+.D1 nameserver 1.2.3.4
+.D1 nameserver 2.3.4.5
+and this configuaration:
+.D1 replace="search/foo*/bar.com nameserver/1.2.3.4/5.6.7.8 nameserver/2.3.4.5/"
+you would get this resolv.conf instead:
+.D1 domain foo.org
+.D1 search bar.com
+.D1 nameserver 5.6.7.8
+.It Sy replace_sub
+Works the same way as
+.Sy replace
+except it works on each space separated value rather than the whole line,
+so it's useful for the replacing a single domain within the search directive.
+Using the same example resolv.conf and changing
+.Sy replace
+to
+.Sy replace_sub ,
+you would get this resolv.conf instead:
+.D1 domain foo.org
+.D1 search bar.com dead.beef
+.D1 nameserver 5.6.7.8
 .It Sy state_dir
 Override the default state directory of
 .Pa @VARDIR@ .
index d96ce0bee2f53e0eedd4b4c91b0bdf331dd6dd74..e4c2747fbfb4e673374f734c2cf040c7553f5d73 100644 (file)
@@ -354,6 +354,47 @@ echo_append()
        echo
 }
 
+replace()
+{
+       local r= k= f= v= val= sub=
+
+       while read -r keyword value; do
+               for r in $replace; do
+                       k="${r%%/*}"
+                       r="${r#*/}"
+                       f="${r%%/*}"
+                       r="${r#*/}"
+                       v="${r%%/*}"
+                       case "$keyword" in
+                       $k)
+                               case "$value" in
+                               $f) value="$v";;
+                               esac
+                               ;;
+                       esac
+               done
+               val=
+               for sub in $value; do
+                       for r in $replace_sub; do
+                               k="${r%%/*}"
+                               r="${r#*/}"
+                               f="${r%%/*}"
+                               r="${r#*/}"
+                               v="${r%%/*}"
+                               case "$keyword" in
+                               $k)
+                                       case "$sub" in
+                                       $f) sub="$v";;
+                                       esac
+                                       ;;
+                               esac
+                       done
+                       val="$val${val:+ }$sub"
+               done
+               printf "%s %s\n" "$keyword" "$val"
+       done
+}
+
 make_vars()
 {
        local newdomains= d= dn= newns= ns=
@@ -369,7 +410,7 @@ make_vars()
                eval "$(echo_prepend | parse_resolv)"
        fi
        if [ -z "$VFLAG" ]; then
-               eval "$(list_resolv -l "$@" | parse_resolv)"
+               eval "$(list_resolv -l "$@" | replace | parse_resolv)"
        fi
        if [ -n "$name_servers_append" -o -n "$search_domains_append" ]; then
                eval "$(echo_append | parse_resolv)"