diff options
author | John Southworth <john.southworth@vyatta.com> | 2011-08-02 12:45:09 -0500 |
---|---|---|
committer | John Southworth <john.southworth@vyatta.com> | 2011-08-02 12:45:09 -0500 |
commit | 078992d5bca135f6389cc37bf867ebbefb1ff72b (patch) | |
tree | b3c9569e216574a4b544e561a1c4debc855c3e21 | |
parent | 65d10befec7daac9e1c600073fca0e912c188f23 (diff) | |
download | vyatta-op-078992d5bca135f6389cc37bf867ebbefb1ff72b.tar.gz vyatta-op-078992d5bca135f6389cc37bf867ebbefb1ff72b.zip |
Reorganize the bash completion and command interpreter scripts into easier to manage chunks. vyatta-op-run no longer exists in /etc/bash_completion.d/ as it is not a completion script it should not be here
-rw-r--r-- | Makefile.am | 6 | ||||
-rw-r--r-- | etc/bash_completion.d/vyatta-op | 178 | ||||
-rw-r--r-- | functions/interpreter/vyatta-common | 82 | ||||
-rw-r--r-- | functions/interpreter/vyatta-image-complete | 142 | ||||
-rw-r--r-- | functions/interpreter/vyatta-op-run (renamed from etc/bash_completion.d/vyatta-op-run) | 26 |
5 files changed, 264 insertions, 170 deletions
diff --git a/Makefile.am b/Makefile.am index 2c190c5..0cc0758 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,6 @@ defaultdir = /etc/default default_DATA = etc/default/vyatta completiondir = /etc/bash_completion.d completion_DATA = etc/bash_completion.d/vyatta-op -completion_DATA += etc/bash_completion.d/vyatta-op-run opdir = $(datadir)/vyatta-op/templates bin_sudo_usersdir = $(bindir)/sudo-users @@ -10,6 +9,11 @@ funcdir = $(datadir)/vyatta-op/functions func_DATA = functions/tech-support func_DATA += functions/tech-support-brief +interpdir = $(datadir)/vyatta-op/functions/interpreter +interp_DATA = functions/interpreter/vyatta-op-run +interp_DATA += functions/interpreter/vyatta-common +interp_DATA += functions/interpreter/vyatta-image-complete + bin_SCRIPTS = scripts/vyatta-show-interfaces bin_SCRIPTS += scripts/vyatta-show-interfaces.pl bin_SCRIPTS += scripts/vyatta-show-version diff --git a/etc/bash_completion.d/vyatta-op b/etc/bash_completion.d/vyatta-op index b1f8643..340c316 100644 --- a/etc/bash_completion.d/vyatta-op +++ b/etc/bash_completion.d/vyatta-op @@ -1,5 +1,4 @@ # vyatta bash operational mode completion - # **** 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 @@ -73,7 +72,14 @@ if [[ "$VYATTA_USER_LEVEL_DIR" == "/opt/vyatta/etc/shell/level/users" ]]; then _vyatta_operator_allowed=( $(cat /opt/vyatta/etc/shell/level/users/allowed-op) ) fi -source /etc/bash_completion.d/vyatta-op-run +#source /etc/bash_completion.d/vyatta-op-run + +declare -a functions +functions=( /opt/vyatta/share/vyatta-op/functions/interpreter/* ) + +for file in "${functions[@]}";do + source $file; +done # $1: label # #2...: strings @@ -86,21 +92,6 @@ _vyatta_op_debug () done } -_vyatta_op_is_elem_of () { - local elem=$1 - local -a olist - eval "olist=( \"\${$2[@]}\" )" - for e in ${olist[*]}; do - if [[ "$e" == "$elem" ]]; then - echo -n '1' - return - fi - done - echo -n '0' - return -} - - # this is needed to provide original "default completion" behavior. # see "vyatta-cfg" completion script for details. _vyatta_op_default_expand () @@ -115,29 +106,6 @@ _vyatta_op_default_expand () fi } -_vyatta_op_init () -{ - # empty and default line compeletion - complete -E -F _vyatta_op_expand - complete -D -F _vyatta_op_default_expand - - # create the top level aliases for the unambiguous portions of the commands - # this is the only place we need an entire enumerated list of the subcommands - for cmd in $( ls /opt/vyatta/share/vyatta-op/templates/ ); do - for pos in $(seq 1 ${#cmd}); do - case ${cmd:0:$pos} in - for|do|done|if|fi|case|while|tr ) - continue ;; - *) ;; - esac - complete -F _vyatta_op_expand ${cmd:0:$pos} - eval alias ${cmd:0:$pos}=\'_vyatta_op_run ${cmd:0:$pos}\' - done - done - - shopt -s histverify -} - # $1: label # $2...: help _vyatta_op_print_help () @@ -245,7 +213,7 @@ _vyatta_op_set_completions () else if [[ "$VYATTA_USER_LEVEL_DIR" == "/opt/vyatta/etc/shell/level/admin" ]]; then completions+=( ${allowed[i]} ) - elif [[ $(_vyatta_op_is_elem_of ${allowed[i]} _vyatta_operator_allowed) == "1" ]];then + elif is_elem_of ${allowed[i]} _vyatta_operator_allowed; then completions+=( ${allowed[i]} ) elif [[ $_vyatta_op_node_path == $vyatta_op_templates ]];then continue @@ -463,130 +431,6 @@ _vyatta_set_comptype () done } -_vyatta_image_is_file() -{ - local cur=$1 - cur=${cur/:/} - topdir=${cur%%/*} - cur=${cur#$topdir/} - if [[ $topdir == "running" ]]; then - cur="/${cur}" - elif [[ $topdir == "disk-install" ]]; then - cur="/live/image/${cur}" - else - cur="/live/image/boot/${topdir}/live-rw/${cur}" - fi - if [[ -f ${cur} ]]; then - return 0; - else - return 1; - fi -} - -_vyatta_image_file_complete() -{ - if _vyatta_image_is_file ${COMP_WORDS[(( ${#COMP_WORDS[@]}-2 ))]}; then - _vyatta_op_completions=( "" " " ) - return 0; - fi - _vyatta_image_path_complete -} - -_vyatta_image_path_complete() -{ - compopt -o nospace - local -a reply - if _vyatta_image_is_file $cur ; then - foundfile=1 - _vyatta_op_completions=( "${cur} " ) - return 0; - fi - if [[ ${cur} == "" ]]; then - reply=( $(compgen -d /live/image/boot/ | grep -v grub) ) - for i in `seq 0 $[${#reply[@]}-1]`; do - file=${reply[$i]} - reply[$i]=${file/#\/live\/image\/boot\//} - reply[$i]="${reply[$i]}://config/" - done - reply+=( "running://config/" ) - if [[ -d /live/image/opt/vyatta/etc/config || -d /live/image/config ]]; then - reply+=( "disk-install://" ) - fi - else - if ! [[ $cur =~ .*:\/\/ ]]; then - if [[ $cur =~ .*:\/ ]]; then - cur=${cur/:\//} - fi - if [[ $cur =~ .*: ]]; then - cur=${cur/:/} - fi - local isrunningimg=$(compgen -W "running" -- ${cur}) - local isdiskinstall=$(compgen -W "disk-install" -- ${cur}) - if [[ $isrunningimg == "running" ]];then - cur="/" - elif [[ $isdiskinstall == "disk-install" ]]; then - cur="/live/image/" - else - cur="/live/image/boot/${cur}" - fi - reply=( $(compgen -f ${cur}) ) - for i in `seq 0 $[${#reply[@]}-1]`; do - file=${reply[$i]} - if [[ $isrunningimg == "running" ]];then - reply[$i]="running://config/" - elif [[ $isdiskinstall == "disk-install" ]]; then - reply[$i]="disk-install://" - else - reply[$i]=${file/#\/live\/image\/boot\//} - if [[ -d /live/image/boot/${reply[$i]} ]]; then - reply[$i]="${reply[$i]/#\//}://config/" - fi - fi - done - else - cur=${cur/:/} - topdir=${cur%%/*} - cur=${cur#$topdir//} - if [[ $topdir == "running" ]]; then - cur="/${cur}" - elif [[ $topdir == "disk-install" ]]; then - cur="/live/image/${cur}" - else - cur="/live/image/boot/${topdir}/live-rw/${cur}" - fi - reply=( $(compgen -f ${cur}) ) - # for loop from _filedirs() in /etc/bash_completion - for ((i=0; i < ${#reply[@]}; i++)); do - if [[ ${cur:0:1} != "'" ]]; then - [[ -d ${reply[i]} ]] && reply[i]="${reply[i]}"/ - if [[ ${cur:0:1} == '"' ]]; then - reply[i]=${reply[i]//\\/\\\\} - reply[i]=${reply[i]//\"/\\\"} - reply[i]=${reply[i]//\$/\\\$} - else - reply[i]=$(printf %q ${reply[i]}) - fi - fi - done - for i in `seq 0 $[${#reply[@]}-1]`; do - file=${reply[$i]} - if [[ $topdir == "running" ]]; then - reply[$i]=${file/#\//"$topdir://"} - elif [[ $topdir == "disk-install" ]]; then - reply[$i]=${file/#\/live\/image\//$"$topdir://"} - else - reply[$i]=${file/#\/live\/image\/boot\/$topdir/"$topdir://"} - reply[$i]=${reply[$i]/\/live-rw\/} - fi - done - fi - fi - _vyatta_op_completions=( "${reply[@]}" ) - return 0 -} - - - nullglob_save=$( shopt -p nullglob ) shopt -s nullglob for f in ${vyatta_datadir}/vyatta-op/functions/allowed/* ; do @@ -602,10 +446,6 @@ fi _vyatta_op_init $@ -if [[ "$VYATTA_USER_LEVEL_DIR" == "/opt/vyatta/etc/shell/level/users" ]]; then - PS1='\u@\h> ' -fi - ### Local Variables: ### mode: shell-script ### End: diff --git a/functions/interpreter/vyatta-common b/functions/interpreter/vyatta-common new file mode 100644 index 0000000..0a98d56 --- /dev/null +++ b/functions/interpreter/vyatta-common @@ -0,0 +1,82 @@ +# vyatta bash completion common functions + +# **** 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. +# +# A copy of the GNU General Public License is available as +# `/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution +# or on the World Wide Web at `http://www.gnu.org/copyleft/gpl.html'. +# You can also obtain it by writing to the Free Software Foundation, +# Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, +# MA 02110-1301, USA. +# +# Author: Vyatta +# Description: bash completion common functions +# +# **** End License **** + +get_prefix_filtered_list () +{ + # $1: prefix + # $2: \@list + # $3: \@filtered + declare -a olist + local pfx=$1 + pfx=${pfx#\"} + eval "olist=( \"\${$2[@]}\" )" + local idx=0 + for elem in "${olist[@]}"; do + local sub=${elem#$pfx} + if [ "$elem" == "$sub" ] && [ -n "$pfx" ]; then + continue + fi + eval "$3[$idx]=\"$elem\"" + (( idx++ )) + done +} + +get_prefix_filtered_list2 () +{ + # $1: prefix + # $2: \@list + # $3: \@filtered + # $4: \@list2 + # $5: \@filtered2 + declare -a olist + local pfx=$1 + pfx=${pfx#\"} + eval "olist=( \"\${$2[@]}\" )" + eval "local orig_len=\${#$2[@]}" + local orig_idx=0 + local idx=0 + for (( orig_idx = 0; orig_idx < orig_len; orig_idx++ )); do + eval "local elem=\${$2[$orig_idx]}" + eval "local elem2=\${$4[$orig_idx]}" + local sub=${elem#$pfx} + if [ "$elem" == "$sub" ] && [ -n "$pfx" ]; then + continue + fi + eval "$3[$idx]=\"$elem\"" + eval "$5[$idx]=\"$elem2\"" + (( idx++ )) + done +} + +is_elem_of () { + local elem=$1 + local -a olist + eval "olist=( \"\${$2[@]}\" )" + for e in ${olist[*]}; do + if [[ "$e" == "$elem" ]]; then + return 0 + fi + done + return 1 +} diff --git a/functions/interpreter/vyatta-image-complete b/functions/interpreter/vyatta-image-complete new file mode 100644 index 0000000..d9dc240 --- /dev/null +++ b/functions/interpreter/vyatta-image-complete @@ -0,0 +1,142 @@ +# vyatta bash operational mode image name completions +# **** 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: John Southworth +# Date: 2011 +# Description: setup bash completion for image file names +# +# **** End License **** +_vyatta_image_is_file() +{ + local cur=$1 + cur=${cur/:/} + topdir=${cur%%/*} + cur=${cur#$topdir/} + if [[ $topdir == "running" ]]; then + cur="/${cur}" + elif [[ $topdir == "disk-install" ]]; then + cur="/live/image/${cur}" + else + cur="/live/image/boot/${topdir}/live-rw/${cur}" + fi + if [[ -f ${cur} ]]; then + return 0; + else + return 1; + fi +} + +_vyatta_image_file_complete() +{ + if _vyatta_image_is_file ${COMP_WORDS[(( ${#COMP_WORDS[@]}-2 ))]}; then + _vyatta_op_completions=( "" " " ) + return 0; + fi + _vyatta_image_path_complete +} + + +_vyatta_image_path_complete() +{ + compopt -o nospace + local -a reply + if _vyatta_image_is_file $cur ; then + foundfile=1 + _vyatta_op_completions=( "${cur} " ) + return 0; + fi + if [[ ${cur} == "" ]]; then + reply=( $(compgen -d /live/image/boot/ | grep -v grub) ) + for i in `seq 0 $[${#reply[@]}-1]`; do + file=${reply[$i]} + reply[$i]=${file/#\/live\/image\/boot\//} + reply[$i]="${reply[$i]}://config/" + done + reply+=( "running://config/" ) + if [[ -d /live/image/opt/vyatta/etc/config || -d /live/image/config ]]; then + reply+=( "disk-install://" ) + fi + else + if ! [[ $cur =~ .*:\/\/ ]]; then + if [[ $cur =~ .*:\/ ]]; then + cur=${cur/:\//} + fi + if [[ $cur =~ .*: ]]; then + cur=${cur/:/} + fi + local isrunningimg=$(compgen -W "running" -- ${cur}) + local isdiskinstall=$(compgen -W "disk-install" -- ${cur}) + if [[ $isrunningimg == "running" ]];then + cur="/" + elif [[ $isdiskinstall == "disk-install" ]]; then + cur="/live/image/" + else + cur="/live/image/boot/${cur}" + fi + reply=( $(compgen -f ${cur}) ) + for i in `seq 0 $[${#reply[@]}-1]`; do + file=${reply[$i]} + if [[ $isrunningimg == "running" ]];then + reply[$i]="running://config/" + elif [[ $isdiskinstall == "disk-install" ]]; then + reply[$i]="disk-install://" + else + reply[$i]=${file/#\/live\/image\/boot\//} + if [[ -d /live/image/boot/${reply[$i]} ]]; then + reply[$i]="${reply[$i]/#\//}://config/" + fi + fi + done + else + cur=${cur/:/} + topdir=${cur%%/*} + cur=${cur#$topdir//} + if [[ $topdir == "running" ]]; then + cur="/${cur}" + elif [[ $topdir == "disk-install" ]]; then + cur="/live/image/${cur}" + else + cur="/live/image/boot/${topdir}/live-rw/${cur}" + fi + reply=( $(compgen -f ${cur}) ) + # for loop from _filedirs() in /etc/bash_completion + for ((i=0; i < ${#reply[@]}; i++)); do + if [[ ${cur:0:1} != "'" ]]; then + [[ -d ${reply[i]} ]] && reply[i]="${reply[i]}"/ + if [[ ${cur:0:1} == '"' ]]; then + reply[i]=${reply[i]//\\/\\\\} + reply[i]=${reply[i]//\"/\\\"} + reply[i]=${reply[i]//\$/\\\$} + else + reply[i]=$(printf %q ${reply[i]}) + fi + fi + done + for i in `seq 0 $[${#reply[@]}-1]`; do + file=${reply[$i]} + if [[ $topdir == "running" ]]; then + reply[$i]=${file/#\//"$topdir://"} + elif [[ $topdir == "disk-install" ]]; then + reply[$i]=${file/#\/live\/image\//$"$topdir://"} + else + reply[$i]=${file/#\/live\/image\/boot\/$topdir/"$topdir://"} + reply[$i]=${reply[$i]/\/live-rw\/} + fi + done + fi + fi + _vyatta_op_completions=( "${reply[@]}" ) + return 0 +} diff --git a/etc/bash_completion.d/vyatta-op-run b/functions/interpreter/vyatta-op-run index 68c9562..33de2f1 100644 --- a/etc/bash_completion.d/vyatta-op-run +++ b/functions/interpreter/vyatta-op-run @@ -19,6 +19,32 @@ # # **** End License **** +_vyatta_op_init () +{ + # empty and default line compeletion + complete -E -F _vyatta_op_expand + complete -D -F _vyatta_op_default_expand + + # create the top level aliases for the unambiguous portions of the commands + # this is the only place we need an entire enumerated list of the subcommands + for cmd in $( ls /opt/vyatta/share/vyatta-op/templates/ ); do + for pos in $(seq 1 ${#cmd}); do + case ${cmd:0:$pos} in + for|do|done|if|fi|case|while|tr ) + continue ;; + *) ;; + esac + complete -F _vyatta_op_expand ${cmd:0:$pos} + eval alias ${cmd:0:$pos}=\'_vyatta_op_run ${cmd:0:$pos}\' + done + done + + shopt -s histverify + if [[ "$VYATTA_USER_LEVEL_DIR" == "/opt/vyatta/etc/shell/level/users" ]]; then + PS1='\u@\h> ' + fi +} + _vyatta_op_get_node_def_field () { local file=$1 field=$2 |