summaryrefslogtreecommitdiff
path: root/etc/bash_completion.d/vyatta-op-run
blob: 8e73773dbad3846c68d3e4abd316a9a794da51e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
    local cmd_regex="^(LESSOPEN=|less|pager|tail|/opt/vyatta/bin/vyatta-tshark-interface-port.pl).*"
    if [ -n "$run_cmd" ]; then
      if [[ -t 1 &&  "$1" == "show" && ! $run_cmd =~ $cmd_regex ]] ; 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: