diff options
Diffstat (limited to 'etc/bash_completion.d/vyatta-op-run')
-rw-r--r-- | etc/bash_completion.d/vyatta-op-run | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/etc/bash_completion.d/vyatta-op-run b/etc/bash_completion.d/vyatta-op-run new file mode 100644 index 0000000..053dc0e --- /dev/null +++ b/etc/bash_completion.d/vyatta-op-run @@ -0,0 +1,87 @@ + +# **** License **** +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# This code was originally developed by Vyatta, Inc. +# Portions created by Vyatta are Copyright (C) 2006, 2007 Vyatta, Inc. +# All Rights Reserved. +# +# Author: Tom Grennan +# Date: 2007 +# Description: setup bash completion for Vyatta operational commands +# +# **** End License **** + +_vyatta_op_get_node_def_field () +{ + local file=$1 field=$2 + + sed -n '/^'"$field"':/,$ { +# strip field name and hold rest of line + s/[a-z]*: *// + h + :b +# at EOF, print hold buffer and quit + $ { x; p; q } +# input next line + n +# if start of another field def, print hold buf and quit + /^[a-z]*:/ { x; p; q } +# add to hold buf and branch to input next line + H + bb + }' $file +} + +_vyatta_op_run () +{ + local -i estat + local tpath=$vyatta_op_templates + local restore_shopts=$( shopt -p extglob nullglob | tr \\n \; ) + shopt -s extglob nullglob + + _vyatta_op_last_comp=${_vyatta_op_last_comp_init} + false; estat=$? + + i=1 + for arg in "$@" + do + if [ -f "$tpath/$arg/node.def" ] ; then + tpath+=/$arg + elif [ -f $tpath/node.tag/node.def ] ; then + tpath+=/node.tag + else + echo "Invalid command" >&2 + eval $restore_shopts + return 1 + fi + let "i+=1" + done + + local run_cmd=$(_vyatta_op_get_node_def_field $tpath/node.def run) + local ret=0 + if [ -n "$run_cmd" ]; then + if [[ -t 1 && "$1" == "show" && \ + ! $run_cmd =~ ^\(LESSOPEN=\|less\|pager\|tail\|/opt/vyatta/bin/vyatta-tshark-interface-port.pl\).* ]] ; then + eval "($run_cmd) | ${VYATTA_PAGER:-cat}" + else + eval "$run_cmd" + fi + else + echo "Incomplete command" >&2 + ret=1 + fi + eval $restore_shopts + return $ret +} + +### Local Variables: +### mode: shell-script +### End: |