From f4589158572ab35969b9bccf801ea07e115705e1 Mon Sep 17 00:00:00 2001 From: tv Date: Thu, 6 Aug 2015 19:37:27 +0200 Subject: cac: import netmask_to_prefix from stockholm --- cac | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/cac b/cac index 3f6e501..c6ab4e4 100755 --- a/cac +++ b/cac @@ -45,6 +45,20 @@ sed ' s/~/%7e/g ' } +netmask_to_prefix() {( +#! /bin/sh +set -euf + +netmask=$1 + +binaryNetmask=$(echo $1 | sed 's/^/obase=2;/;s/\./;/g' | bc | tr -d \\n) +binaryPrefix=$(echo $binaryNetmask | sed -n 's/^\(1*\)0*$/\1/p') +if ! echo $binaryPrefix | grep -q .; then + echo $0: bad netmask: $netmask >&2 + exit 4 +fi +printf %s $binaryPrefix | tr -d 0 | wc -c +)} # cac_resources_cache=${cac_resources_cache-$HOME/tmp/cac_resources_cache.json} @@ -196,7 +210,7 @@ __cac_cli__generatenetworking() {( gateway=$(echo $server | jq -r .gateway) nameserver=8.8.8.8 netmask=$(echo $server | jq -r .netmask) - prefix=$(netmask-to-prefix $netmask) + prefix=$(netmask_to_prefix $netmask) #printf '# Generated file: %s generatenetworking %s %s\n' "$0" "$1" "$2" #printf '# on %s\n' "$(date -Is)" -- cgit v1.2.3