From: Michael Tremer Date: Wed, 8 Aug 2012 13:50:53 +0000 (+0000) Subject: Add functions to handle lists very easily. X-Git-Tag: 005~56 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=e726ef8d07f8f164fa42f59a8fbe19a1b9d6090e;p=network.git Add functions to handle lists very easily. --- diff --git a/functions.list b/functions.list new file mode 100644 index 00000000..d6d5305e --- /dev/null +++ b/functions.list @@ -0,0 +1,79 @@ +#!/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 + + eval "${list}=\"${!list} $@\"" +} + +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_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}" +} diff --git a/functions.util b/functions.util index 7c230e5f..76e5ba12 100644 --- a/functions.util +++ b/functions.util @@ -44,35 +44,18 @@ function warning_log() { log WARNING "$@" } +# The next three functions are kept for backwards +# compatibility. The need to be dropped at some time. function listsort() { - local i - for i in $@; do - echo "${i}" - done | sort | tr '\n' ' ' - echo + list_sort $@ } function listmatch() { - local match=${1} - shift - - local i - for i in $@; do - [ "${match}" = "${i}" ] && return ${EXIT_OK} - done - - return ${EXIT_ERROR} + list_match $@ } function listlength() { - local length=0 - - local i - for i in $@; do - length=$(( ${length} + 1 )) - done - - echo "${length}" + list_length $@ } # Speedup function to avoid a call of the basename binary @@ -83,7 +66,7 @@ function basename() { function enabled() { local param=${1} - listmatch "${!param}" yes on true 1 + list_match "${!param}" yes on true 1 } function mac_generate() { @@ -160,7 +143,7 @@ function isoneof() { local var=${!1} shift - listmatch "${var}" "$@" + list_match "${var}" "$@" } function isbool() {