[tools/bgpscanner] Handle arguments quoting properly

pull/2/head
Lorenzo Cogotti 3 years ago
parent c23a29d2da
commit f9e7ab9972

@ -106,7 +106,7 @@ chkint() {
}
isip4() {
for i in 1 2 3 4; do
case $(echo "$1" | cut -d. -f"$i") in
case $(printf %s\\n "$1" | cut -d. -f"$i") in
[0-9]|[0-9][0-9]|[0-1][0-9][0-9]|2[0-4][0-9]|25[0-5]) ;;
*) return 1;;
esac
@ -114,7 +114,7 @@ isip4() {
}
isip6() {
test "$( \
echo "$1" | \
printf %s\\n "$1" | \
grep -Ec '^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]).){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$' \
)" -eq 1
}
@ -165,22 +165,22 @@ templatef() {
[ -n "$2" ] && "$2" "$UNQ"
[ $? -ne 0 ] && exit 1
echo "$UNQ"
printf %s\\n "$UNQ"
done > "$TPL" || die "'$1': File conversion failed"
echo "$TPL"
}
append() {
echo "${1:+${1}${3:+ ${3}} }${2}"
}
quote () { printf %s\\n "$1" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/'/"; }
append() { printf %s\\n "${1:+${1}${3:+ ${3}} }${2}"; }
TERM=""
EXPR=""
appendtermargs() {
_T=$1; shift
TERM="$_T ( $* )"
TERM="$_T \( $* \)"
}
appendtermfiles() {
@ -188,7 +188,7 @@ appendtermfiles() {
for i in "$@"; do _L=$(append "$_L" "$_T $i" "-or"); done
[ -n "$TERM" ] && TERM="( $TERM -or $_L )" || TERM="$_L"
[ -n "$TERM" ] && TERM="\( $TERM -or $_L \)" || TERM="$_L"
}
pushterm() {
@ -201,12 +201,12 @@ exprcompile() {
pushterm
for i in $ATTRS; do TERM=$(append "$TERM" "-attr $i" "-or"); done
[ "$(echo "$ATTRS" | wc -w)" -gt 1 ] && TERM="( $TERM )"
[ "$(echo "$ATTRS" | wc -w)" -gt 1 ] && TERM="\( $TERM \)"
pushterm
[ -n "$NCOMMS" ] && TERM="-not -communities ( $NCOMMS )"
[ -n "$COMMS" ] && TERM=$(append "$TERM" "-all-communities ( $COMMS )" "-or")
[ -n "$NCOMMS" ] && [ -n "$COMMS" ] && TERM="( $TERM )"
[ -n "$NCOMMS" ] && TERM="-not -communities \( $NCOMMS \)"
[ -n "$COMMS" ] && TERM=$(append "$TERM" "-all-communities \( $COMMS \)" "-or")
[ -n "$NCOMMS" ] && [ -n "$COMMS" ] && TERM="\( $TERM \)"
pushterm
[ -n "$EXACT" ] && appendtermargs -exact $EXACT
@ -229,10 +229,15 @@ exprcompile() {
[ -n "$REXPS" ] && TERM="-aspath \"$REXPS\""
[ -n "$NREXPS" ] && TERM=$(append "$TERM" "-not -aspath \"$NREXPS\"" "-or")
[ -n "$REXPS" ] && [ -n "$NREXPS" ] && TERM="( $TERM )"
[ -n "$REXPS" ] && [ -n "$NREXPS" ] && TERM="\( $TERM \)"
pushterm
}
execute() {
eval "set -- $OPTS -- "$@" $EXPR"
"$CMD" "$TGT" "$@"
}
GOTEXACT=""; GOTRELATED=""; GOTSUBNETS=""; GOTSUPNETS=""
while getopts "a:A:cde:E:fi:I:lLo:m:M:p:P:r:R:s:S:t:T:u:U:" o; do
case $o in
@ -273,10 +278,10 @@ while getopts "a:A:cde:E:fi:I:lLo:m:M:p:P:r:R:s:S:t:T:u:U:" o; do
o) OUTREDIR="$OPTARG"
;;
p) chkrexp "$OPTARG"
REXPS=$(append "$REXPS" "$(echo "$OPTARG" | tr "?" ".")" "|")
REXPS=$(append "$REXPS" "$(printf %s\\n "$OPTARG" | tr "?" ".")" "|")
;;
P) chkrexp "$OPTARG"
NREXPS=$(append "$NREXPS" "$(echo "$OPTARG" | tr "?" "." )" "|")
NREXPS=$(append "$NREXPS" "$(printf %s\\n "$OPTARG" | tr "?" "." )" "|")
;;
r) chkprefix "$OPTARG"
GOTRELATED=y; RELATED=$(append "$RELATED" "$OPTARG")
@ -321,6 +326,9 @@ exprcompile
[ -n "$OUTREDIR" ] && OPTS=$(append "$OPTS" "-o $OUTREDIR")
CMD="exec"
[ -n "$PRETEND" ] && [ "$PRETEND" = 1 ] || [ "$PRETEND" = y ] || [ "$PRETEND" = yes ] && CMD="echo"
if [ -n "$PRETEND" ] && [ "$PRETEND" = 1 ] || [ "$PRETEND" = y ] || [ "$PRETEND" = yes ]; then
CMD="echo"
EXPR=$(quote "$EXPR")
fi
"$CMD" "$TGT" $OPTS -- "$@" $EXPR
execute "$@"

Loading…
Cancel
Save