]> git.ipfire.org Git - people/stevee/network.git/commitdiff
Add functions to handle lists very easily.
authorMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Aug 2012 13:50:53 +0000 (13:50 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Wed, 8 Aug 2012 13:50:53 +0000 (13:50 +0000)
functions.list [new file with mode: 0644]
functions.util

diff --git a/functions.list b/functions.list
new file mode 100644 (file)
index 0000000..d6d5305
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>.       #
+#                                                                             #
+###############################################################################
+#
+# 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}"
+}
index 7c230e5fce767f4e285092bebcbf4e7039cb5016..76e5ba1209d01deec73d4c3f9a19f7993ac0f6f7 100644 (file)
@@ -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() {