]> git.ipfire.org Git - people/stevee/network.git/blame - src/functions/functions.list
Remove the function keyword which is a bashism
[people/stevee/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
1c6a4e30 25list_append() {
e726ef8d 26 local list=${1}
1e6f187e
MT
27 assert isset list
28 shift
29
30 local arg
31 for arg in $@; do
32 list_append_one "${list}" "${arg}"
33 done
34}
35
1c6a4e30 36list_append_one() {
1e6f187e
MT
37 assert [ $# -eq 2 ]
38
39 local list="${1}"
e726ef8d
MT
40 shift
41
42 assert isset list
43
7aa696d1 44 if [ -n "${!list}" ]; then
50a9620c 45 printf -v ${list} -- "${!list} $@"
7aa696d1 46 else
50a9620c 47 printf -v ${list} -- "$@"
7aa696d1 48 fi
e726ef8d
MT
49}
50
1c6a4e30 51list_remove() {
e726ef8d
MT
52 local list=${1}
53 shift
54
55 assert isset list
56
57 local _list k
58 for k in ${!list}; do
59 listmatch ${k} $@ && continue
60
61 _list="${_list} ${k}"
62 done
63
64 eval "${list}=\"${_list}\""
65}
66
1c6a4e30 67list_sort() {
e726ef8d
MT
68 local i
69 for i in $@; do
70 print "${i}"
71 done | sort | tr '\n' ' '
72 print
73}
74
1c6a4e30 75list_unique() {
805da540
MT
76 local items item
77 for item in $@; do
78 # Check if the item has already been processed.
79 list_match "${item}" ${items} && continue
80
81 list_append items "${item}"
82 print "${item}"
83 done
84}
85
1c6a4e30 86list_match() {
e726ef8d
MT
87 local match=${1}
88 shift
89
90 local i
91 for i in $@; do
92 [ "${match}" = "${i}" ] && return ${EXIT_OK}
93 done
94
95 return ${EXIT_ERROR}
96}
97
1c6a4e30 98list_length() {
e726ef8d
MT
99 local length=0
100
101 local i
102 for i in $@; do
103 length=$(( ${length} + 1 ))
104 done
105
106 print "${length}"
107}
55ea0266
MT
108
109# Count how often $1 occurs in the list.
1c6a4e30 110list_count() {
55ea0266
MT
111 local what=${1}
112 shift
113
114 local counter=0
115
116 local arg
117 for arg in $@; do
118 if [ "${arg}" = "${what}" ]; then
119 counter=$(( ${counter} + 1 ))
120 fi
121 done
122
123 print "${counter}"
124}
805243f6 125
1c6a4e30 126list_join() {
805243f6
MT
127 local list=${1}
128 local delim=${2}
129
130 local ret
131 printf -v ret "${delim}%s" ${!list}
132
133 print "${ret:${#delim}}"
134}
6396ccab 135
1c6a4e30 136list_reverse() {
6396ccab
MT
137 local reversed arg
138 for arg in $@; do
139 reversed="${arg} ${reversed}"
140 done
141
142 print "${reversed}"
143 return ${EXIT_OK}
144}
7ad5252c 145
1c6a4e30 146list_head() {
7ad5252c
MT
147 local arg
148 for arg in $@; do
149 print "${arg}"
150 return ${EXIT_OK}
151 done
152
153 return ${EXIT_ERROR}
154}