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