poniedziałek, 24 września 2012

helper-functions

. /etc/default/multiseat

#
# private implementation of get_sect
#
# get given section's contents from the given config file
#
# $1 filename
# $2 section number
#
__get_sect()
{
    cat $1 | awk -v sect=$2 '
    BEGIN {
        SECT="^\\[" sect "\\]"
        NEXT_SECT="^\\[" (sect + 1) "\\]"
    }
    $0 ~ SECT, $0 ~ NEXT_SECT {
        if ($0 ~ SECT)
            next;
        if ($0 ~ NEXT_SECT)
            next;
        if (substr($1, 1, 1) == "#")
            next;
        print
    }
    '
}

#
# private implementation of get_conf
#
# get given key's contents from the given section
# from the given config file
#
# does not work for tables in the config file
#
# $1 filename
# $2 section name
# $3 key name
#
__get_conf()
{
    __get_sect $1 $2 | awk -F"=" -v key=$3 '
    BEGIN {
        KEY=key
    }
    $1==KEY {
        print substr($0, index($0, "=") + 1)
    }
    '
}

#
# private implementation of get_num_seats
#
# get number of seats from the default config file
#
# $1 filename
#
__get_num_seats()
{
    cat $1 | awk '
    BEGIN {
        count=0
    }
    /^\[[0-9]*\]/ {count++}
    END {
        print count - 1
    }
    '
}

#
# get given section's contents from the default config file
#
# $1 section name
#
get_sect()
{
    __get_sect $CONFIG $1
}

#
# private implementation of get_table
#
# $1 filename
# $2 section number
# $3 table name
# $4 size/entry switch [-size => size, <number> for entry]
#
__get_table()
{
    SIZE=0;
    ENTRY=-1

    if [ $4 == "-size" ];
    then
        SIZE=1;
    else
        ENTRY=$4
    fi

    RESULT=`__get_sect $1 $2 | awk -v table=$3 -v get_size=$SIZE -v entry=$ENTRY '
    BEGIN {
        print BEGIn
        TABLE=table "\\\=" "\\\("
        GET_SIZE=get_size
        ENTRY=entry
        size=0
    }
    $0 ~ TABLE, /^\)/ {
        if ($0 ~ TABLE)
            next;
        if ($0 ~ /^\)/)
            next;
        if (substr($1, 1, 1) == "#")
            next;
        if (get_size == 0 && size == entry){
                print
                exit
        }
        size++;
    }
    END {
        if (get_size)
            print size;
    }
    '`
    RESULT=`echo $RESULT | sed 's/^[ \t]*//g'`

    echo $RESULT
}

#
# get given key's contents from the given section
# from the default config file
#
# does not work for tables in the config file
#
# $1 section name
# $2 key name
#
get_conf()
{
    __get_conf $CONFIG $1 $2
}

#
# get number of seats from the default config file
#
get_num_seats()
{
    __get_num_seats $CONFIG
}

#
# get size of a given table in the given section of the config file or
# get specified entry of the table
#
# $1 section number
# $2 table name
# $3 size/entry switch [-size => size, <number> for entry]
#
get_table()
{
    __get_table $CONFIG $1 $2 $3
}

Escape characters alive forever

The multiseat config is never managed once and for ever...

I wanted the scripts to be as little dependent as possible, so for parsing the multiseat.conf file I used only bash&sed&awk, no fancy tools like python. The helper-functions have been written in the mawk dialect of awk, and the awk found in Debian used to be mawk. Alas, no longer. So there are two options to consider: modify the helper-functions, so that mawk is used explicitly instead of awk (and make sure mawk is installed), or modify the helper-functions to speak gnu awk found in Debian now. I chose the second option. In fact only some escaping craziness differs. Instead of one backslash now two or three (!) are required here and there. You can find the new script here.