Support gmake-3 and gmake-4
[openresolv] / libc.in
diff --git a/libc.in b/libc.in
index 5442c06831b517a8c8f9ac8a8d08b4f3e7ea4335..2e29e4fdb0bcc5c90035c173d9fb9d20ca393bc7 100644 (file)
--- a/libc.in
+++ b/libc.in
@@ -1,5 +1,5 @@
 #!/bin/sh
-# Copyright (c) 2007-2011 Roy Marples
+# Copyright (c) 2007-2012 Roy Marples
 # All rights reserved
 
 # libc subscriber for resolvconf
@@ -36,18 +36,18 @@ NL="
 # sed may not be available, and this is faster on small files
 key_get_value()
 {
-       local key="$1" value= x= line=
+       local key="$1" x= line=
 
        shift
        if [ $# -eq 0 ]; then
-               while read line; do
+               while read -r line; do
                        case "$line" in
                        "$key"*) echo "${line##$key}";;
                        esac
                done
        else
-               for x; do
-                       while read line; do
+               for x do
+                       while read -r line; do
                                case "$line" in
                                "$key"*) echo "${line##$key}";;
                                esac
@@ -56,6 +56,22 @@ key_get_value()
        fi
 }
 
+keys_remove()
+{
+       local key x line found
+
+       while read -r line; do
+               found=false
+               for key do
+                       case "$line" in
+                       "$key"*|"#"*|" "*|"     "*|"") found=true;;
+                       esac
+                       $found && break
+               done
+               $found || echo "$line"
+       done
+}
+
 # Support original resolvconf configuration layout
 # as well as the openresolv config file
 if [ -f "$SYSCONFDIR"/resolvconf.conf ]; then
@@ -64,11 +80,9 @@ elif [ -d "$SYSCONFDIR"/resolvconf ]; then
        SYSCONFDIR="$SYSCONFDIR/resolvconf/resolv.conf.d"
        base="$SYSCONFDIR/resolv.conf.d/base"
        if [ -f "$base" ]; then
-               name_servers="$(key_get_value "nameserver " "$base")"
-               search_domains="$(key_get_value "search " "$base")"
-               if [ -z "$search_domains" ]; then
-                       search_domains="$(key_get_value "domain " "$base")"
-               fi
+               prepend_nameservers="$(key_get_value "nameserver " "$base")"
+               domain="$(key_get_value "domain " "$base")"
+               prepend_search="$(key_get_value "search " "$base")"
                resolv_conf_options="$(key_get_value "options " "$base")"
                resolv_conf_sortlist="$(key_get_value "sortlist " "$base")"
        fi
@@ -79,10 +93,11 @@ elif [ -d "$SYSCONFDIR"/resolvconf ]; then
                resolv_conf_tail="$(cat "$SYSCONFDIR"/resolv.conf.d/tail)"
        fi
 fi
+: ${domain:=$DOMAIN}
 : ${resolv_conf:=/etc/resolv.conf}
 : ${libc_service:=nscd}
 : ${libc_restart:=@RESTARTCMD ${libc_service}@}
-: ${list_resolv:=@PREFIX@/sbin/resolvconf -l}
+: ${list_resolv:=@SBINDIR@/resolvconf -l}
 if [ "${resolv_conf_head-x}" = x -a -f "$SYSCONFDIR"/resolv.conf.head ]; then
        resolv_conf_head="$(cat "${SYSCONFDIR}"/resolv.conf.head)"
 fi
@@ -115,41 +130,50 @@ case "${resolv_conf_passthrough:-NO}" in
        newconf="$(cat "$newest")$NL"
        ;;
 *)
-       [ -z "$RESOLVCONF" ] && eval "$(@PREFIX@/sbin/resolvconf -v)"
-       newsearch="$(uniqify $search_domains $SEARCH $search_domains_append)"
+       [ -z "$RESOLVCONF" ] && eval "$(@SBINDIR@/resolvconf -v)"
+       newsearch="$(uniqify $prepend_search $SEARCH)"
        NS="$LOCALNAMESERVERS $NAMESERVERS"
-       newns="$(uniqify $name_servers $NS $name_servers_append)"
+       newns=
+       gotlocal=false
+       for n in $(uniqify $prepend_nameservers $NS); do
+               add=true
+               case "$n" in
+               127.*|0.0.0.0|255.255.255.255|::1) gotlocal=true;;
+               *)
+                       case "${resolv_conf_local_only:-YES}" in
+                       [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1)
+                               $gotlocal && add=false;;
+                       esac
+               esac
+               $add && newns="$newns $n"
+       done
 
        # Hold our new resolv.conf in a variable to save on temporary files
        newconf="# Generated by resolvconf$NL"
        if [ -n "$resolv_conf_head" ]; then
                newconf="$newconf$resolv_conf_head$NL"
        fi
-       [ -n "$newsearch" ] && newconf="${newconf}search $newsearch$NL"
+
+       [ -n "$domain" ] && newconf="${newconf}domain $domain$NL"
+       if [ -n "$newsearch" -a "$newsearch" != "$domain" ]; then
+               newconf="${newconf}search $newsearch$NL"
+       fi
        for n in $newns; do
                newconf="${newconf}nameserver $n$NL"
        done
 
-       # Now get any configured options
-       opts="$resolv_conf_options${resolv_conf_options:+ }"
-       opts="$opts$($list_resolv | key_get_value "options ")"
-       if [ -n "$opts" ]; then
-               newconf="${newconf}options"
-               for opt in $(uniqify $opts); do
-                       newconf="${newconf} $opt"
-               done
-               newconf="$newconf$NL"
+       # Now add anything we don't care about such as sortlist and options
+       stuff="$($list_resolv | keys_remove nameserver domain search)"
+       if [ -n "$stuff" ]; then
+               newconf="$newconf$stuff$NL"
        fi
 
-       # and any sortlists
-       opts="$resolv_conf_sortlist${resolv_conf_sortlist:+ }"
-       opts="$opts$($list_resolv | key_get_value "sortlist ")"
-       if [ -n "$opts" ]; then
-               newconf="${newconf}sortlist"
-               for opt in $(uniqify $opts); do
-                       newconf="${newconf} $opt"
-               done
-               newconf="$newconf$NL"
+       # Append any user defined ones
+       if [ -n "$resolv_conf_options" ]; then
+               newconf="${newconf}options $resolv_conf_options$NL"
+       fi
+       if [ -n "$resolv_conf_sortlist" ]; then
+               newconf="${newconf}sortlist $resolv_conf_sortlist$NL"
        fi
 
        if [ -n "$resolv_conf_tail" ]; then