summaryrefslogtreecommitdiff
path: root/src/parseconf.sh
blob: aa5885d8117e4b820655bf12dc1d760060c85fc9 (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash

usage () {
    printf "Usage $0 "
    echo << "EOF"
OPTIONS... [FILTER]

Print the parsed config file filtering by the keys
Arguments:
    -f file         read configuration from a file, uses /dev/sdtin otherwise
    -v              only print values
    -c n            print the last [n]
EOF
}

getlevel() {
    for i in $*; do
        printf $i
        printf '.'
    done
}

parse_line() {
    [ $# == "0" ] && return

    local line="$@"
    local key=$1
    shift
    local value="$@"

    [ "$key" = "include" ] && parse $value && return
    [ "$key" = "]" ] && unset list[-1] && printf "\n" && return
    [ "$key" = "}" ] && unset level[-1] && return

    case ${value: -1} in 
        "{")
            level+=("$key")
            ;;
        "[")
            list+=("$key")
            printf "$(getlevel ${level[@]})$key:"
            ;;
        *)

            [ "${#list[@]}" == "0" ] && 
                printf "$(getlevel ${level[@]})$key:$value\n" ||
                printf "$line "
            ;;
    esac
}

# print the parsed values from the config file in key:value format
#
parse () {
    local file="$1"
    export level=()
    export list=()
    while IFS= read -r line; do
        # strip whitespace
        line=$(sed "s/^#.*$\|\s(\s\+)\|^\s\|\s^\|;*$//g" <<< "$line")
        parse_line $line
    done < "$file"
}

# Filter the parsed file for specific keys
#
filter () {
    local pattern=

    [ $# = 0 ] &&
        pattern=".*" ||
        pattern=$(sed "s/\*/[^:]*/g"<<< "$@")

    $print_keys && 
        pattern="s/^($pattern:.+)/\1/p" ||
        pattern="s/^$pattern:(.+)/\1/p"


    parse $CONF_FILE | sed -rn $pattern
}

# Use the env variable if exists
[ -z ${CONF_FILE} ] && CONF_FILE="/dev/stdin"

# initialise options
print_keys=true
count=

while getopts ":f:c:v" opt; do
    case "${opt}" in 
        f)
            [ "${OPTARG}" = "-" ] &&
                CONF_FILE="/dev/stdin" ||
                CONF_FILE="${OPTARG}"
            ;;

        v)
            print_keys=false
            ;;
        c)
            count="${OPTARG}"
            ;;
        *)
    esac
done

shift $((OPTIND-1))

if echo $@ | grep -q "."; then
    [ -z ${count} ] &&
        filter "$@" ||
        filter "$@" | tail -n $count
else 
    parse $CONF_FILE  
fi