]>
Commit | Line | Data |
---|---|---|
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 | 25 | list_append() { |
e726ef8d | 26 | local list=${1} |
1e6f187e | 27 | assert isset list |
c8c968a8 | 28 | assert [ ${list} != "list" ] |
1e6f187e MT |
29 | shift |
30 | ||
31 | local arg | |
32 | for arg in $@; do | |
33 | list_append_one "${list}" "${arg}" | |
34 | done | |
35 | } | |
36 | ||
1c6a4e30 | 37 | list_append_one() { |
1e6f187e MT |
38 | assert [ $# -eq 2 ] |
39 | ||
40 | local list="${1}" | |
e726ef8d MT |
41 | shift |
42 | ||
43 | assert isset list | |
c8c968a8 | 44 | assert [ ${list} != "list" ] |
e726ef8d | 45 | |
7aa696d1 | 46 | if [ -n "${!list}" ]; then |
042ccabd | 47 | assign "${list}" "${!list} $@" |
7aa696d1 | 48 | else |
042ccabd | 49 | assign "${list}" "$@" |
7aa696d1 | 50 | fi |
e726ef8d MT |
51 | } |
52 | ||
d9a6c183 MT |
53 | # Appends one or more elements to the list only if they are not on the list, yet |
54 | list_append_unique() { | |
55 | local list=${1} | |
56 | shift | |
57 | ||
58 | assert isset list | |
59 | ||
60 | local ret=${EXIT_ERROR} | |
61 | ||
62 | local arg | |
63 | for arg in $@; do | |
64 | if ! list_match ${arg} ${!list}; then | |
65 | list_append_one ${list} "${arg}" | |
66 | ret=${EXIT_OK} | |
67 | fi | |
68 | done | |
69 | ||
70 | return ${ret} | |
71 | } | |
72 | ||
73 | # Removes all matching items from the list | |
74 | # Returns OK if at least one match was found and ERROR when not | |
1c6a4e30 | 75 | list_remove() { |
e726ef8d MT |
76 | local list=${1} |
77 | shift | |
78 | ||
79 | assert isset list | |
c8c968a8 | 80 | assert [ ${list} != "list" ] |
e726ef8d | 81 | |
d9a6c183 MT |
82 | local ret=${EXIT_ERROR} |
83 | ||
e726ef8d MT |
84 | local _list k |
85 | for k in ${!list}; do | |
d9a6c183 MT |
86 | if list_match ${k} $@; then |
87 | ret=${EXIT_OK} | |
88 | continue | |
89 | fi | |
e726ef8d MT |
90 | |
91 | _list="${_list} ${k}" | |
92 | done | |
93 | ||
bc1f050c | 94 | assign "${list}" "${_list}" |
d9a6c183 MT |
95 | |
96 | return ${ret} | |
e726ef8d MT |
97 | } |
98 | ||
1c6a4e30 | 99 | list_sort() { |
e726ef8d MT |
100 | local i |
101 | for i in $@; do | |
102 | print "${i}" | |
103 | done | sort | tr '\n' ' ' | |
104 | ||
105 | } | |
106 | ||
1c6a4e30 | 107 | list_unique() { |
805da540 MT |
108 | local items item |
109 | for item in $@; do | |
110 | # Check if the item has already been processed. | |
111 | list_match "${item}" ${items} && continue | |
112 | ||
113 | list_append items "${item}" | |
114 | print "${item}" | |
115 | done | |
116 | } | |
117 | ||
1c6a4e30 | 118 | list_match() { |
e726ef8d MT |
119 | local match=${1} |
120 | shift | |
121 | ||
122 | local i | |
123 | for i in $@; do | |
124 | [ "${match}" = "${i}" ] && return ${EXIT_OK} | |
125 | done | |
126 | ||
127 | return ${EXIT_ERROR} | |
128 | } | |
129 | ||
d9a6c183 MT |
130 | list_is_empty() { |
131 | local list="${1}" | |
132 | ||
133 | [ ! -n "${!list}" ] | |
134 | } | |
135 | ||
1c6a4e30 | 136 | list_length() { |
e726ef8d MT |
137 | local length=0 |
138 | ||
139 | local i | |
140 | for i in $@; do | |
141 | length=$(( ${length} + 1 )) | |
142 | done | |
143 | ||
144 | print "${length}" | |
145 | } | |
55ea0266 MT |
146 | |
147 | # Count how often $1 occurs in the list. | |
1c6a4e30 | 148 | list_count() { |
55ea0266 MT |
149 | local what=${1} |
150 | shift | |
151 | ||
152 | local counter=0 | |
153 | ||
154 | local arg | |
155 | for arg in $@; do | |
156 | if [ "${arg}" = "${what}" ]; then | |
157 | counter=$(( ${counter} + 1 )) | |
158 | fi | |
159 | done | |
160 | ||
161 | print "${counter}" | |
162 | } | |
805243f6 | 163 | |
1c6a4e30 | 164 | list_join() { |
805243f6 MT |
165 | local list=${1} |
166 | local delim=${2} | |
167 | ||
c8c968a8 JS |
168 | assert isset list |
169 | assert isset delim | |
170 | assert [ ${list} != "list" ] | |
171 | ||
805243f6 MT |
172 | local ret |
173 | printf -v ret "${delim}%s" ${!list} | |
174 | ||
175 | print "${ret:${#delim}}" | |
176 | } | |
6396ccab | 177 | |
1c6a4e30 | 178 | list_reverse() { |
6396ccab MT |
179 | local reversed arg |
180 | for arg in $@; do | |
181 | reversed="${arg} ${reversed}" | |
182 | done | |
183 | ||
184 | print "${reversed}" | |
185 | return ${EXIT_OK} | |
186 | } | |
7ad5252c | 187 | |
1c6a4e30 | 188 | list_head() { |
7ad5252c MT |
189 | local arg |
190 | for arg in $@; do | |
191 | print "${arg}" | |
192 | return ${EXIT_OK} | |
193 | done | |
194 | ||
195 | return ${EXIT_ERROR} | |
196 | } |