]> git.ipfire.org Git - people/ms/network.git/blame - src/functions/functions.list
Use autotools.
[people/ms/network.git] / src / functions / functions.list
CommitLineData
e726ef8d
MT
1#!/bin/bash
2###############################################################################
3# #
4# IPFire.org - A linux based firewall #
5# Copyright (C) 2012 IPFire Network Development Team #
6# #
7# This program is free software: you can redistribute it and/or modify #
8# it under the terms of the GNU General Public License as published by #
9# the Free Software Foundation, either version 3 of the License, or #
10# (at your option) any later version. #
11# #
12# This program is distributed in the hope that it will be useful, #
13# but WITHOUT ANY WARRANTY; without even the implied warranty of #
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15# GNU General Public License for more details. #
16# #
17# You should have received a copy of the GNU General Public License #
18# along with this program. If not, see <http://www.gnu.org/licenses/>. #
19# #
20###############################################################################
21#
22# Functions for nice handling of lists.
23#
24
25function list_append() {
26 local list=${1}
27 shift
28
29 assert isset list
30
7aa696d1 31 if [ -n "${!list}" ]; then
50a9620c 32 printf -v ${list} -- "${!list} $@"
7aa696d1 33 else
50a9620c 34 printf -v ${list} -- "$@"
7aa696d1 35 fi
e726ef8d
MT
36}
37
38function list_remove() {
39 local list=${1}
40 shift
41
42 assert isset list
43
44 local _list k
45 for k in ${!list}; do
46 listmatch ${k} $@ && continue
47
48 _list="${_list} ${k}"
49 done
50
51 eval "${list}=\"${_list}\""
52}
53
54function list_sort() {
55 local i
56 for i in $@; do
57 print "${i}"
58 done | sort | tr '\n' ' '
59 print
60}
61
805da540
MT
62function list_unique() {
63 local items item
64 for item in $@; do
65 # Check if the item has already been processed.
66 list_match "${item}" ${items} && continue
67
68 list_append items "${item}"
69 print "${item}"
70 done
71}
72
e726ef8d
MT
73function list_match() {
74 local match=${1}
75 shift
76
77 local i
78 for i in $@; do
79 [ "${match}" = "${i}" ] && return ${EXIT_OK}
80 done
81
82 return ${EXIT_ERROR}
83}
84
85function list_length() {
86 local length=0
87
88 local i
89 for i in $@; do
90 length=$(( ${length} + 1 ))
91 done
92
93 print "${length}"
94}
55ea0266
MT
95
96# Count how often $1 occurs in the list.
97function list_count() {
98 local what=${1}
99 shift
100
101 local counter=0
102
103 local arg
104 for arg in $@; do
105 if [ "${arg}" = "${what}" ]; then
106 counter=$(( ${counter} + 1 ))
107 fi
108 done
109
110 print "${counter}"
111}
805243f6
MT
112
113function list_join() {
114 local list=${1}
115 local delim=${2}
116
117 local ret
118 printf -v ret "${delim}%s" ${!list}
119
120 print "${ret:${#delim}}"
121}
6396ccab
MT
122
123function list_reverse() {
124 local reversed arg
125 for arg in $@; do
126 reversed="${arg} ${reversed}"
127 done
128
129 print "${reversed}"
130 return ${EXIT_OK}
131}