#!/bin/bash ############################################################################### # # # IPFire.org - A linux based firewall # # Copyright (C) 2012 IPFire Network Development Team # # # # This program is free software: you can redistribute it and/or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation, either version 3 of the License, or # # (at your option) any later version. # # # # 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. # # # # You should have received a copy of the GNU General Public License # # along with this program. If not, see . # # # ############################################################################### # # Functions for nice handling of lists. # function list_append() { local list=${1} shift assert isset list if [ -n "${!list}" ]; then printf -v ${list} -- "${!list} $@" else printf -v ${list} -- "$@" fi } function list_remove() { local list=${1} shift assert isset list local _list k for k in ${!list}; do listmatch ${k} $@ && continue _list="${_list} ${k}" done eval "${list}=\"${_list}\"" } function list_sort() { local i for i in $@; do print "${i}" done | sort | tr '\n' ' ' print } function list_unique() { local items item for item in $@; do # Check if the item has already been processed. list_match "${item}" ${items} && continue list_append items "${item}" print "${item}" done } function list_match() { local match=${1} shift local i for i in $@; do [ "${match}" = "${i}" ] && return ${EXIT_OK} done return ${EXIT_ERROR} } function list_length() { local length=0 local i for i in $@; do length=$(( ${length} + 1 )) done print "${length}" } # Count how often $1 occurs in the list. function list_count() { local what=${1} shift local counter=0 local arg for arg in $@; do if [ "${arg}" = "${what}" ]; then counter=$(( ${counter} + 1 )) fi done print "${counter}" } function list_join() { local list=${1} local delim=${2} local ret printf -v ret "${delim}%s" ${!list} print "${ret:${#delim}}" } function list_reverse() { local reversed arg for arg in $@; do reversed="${arg} ${reversed}" done print "${reversed}" return ${EXIT_OK} }