]>
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 | |
2212045f | 32 | for arg in "$@"; do |
1e6f187e MT |
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 | |
2212045f | 63 | for arg in "$@"; do |
d9a6c183 MT |
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 | |
2212045f | 86 | if list_match ${k} "$@"; then |
d9a6c183 MT |
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 | 100 | local i |
2212045f | 101 | for i in "$@"; do |
e726ef8d MT |
102 | print "${i}" |
103 | done | sort | tr '\n' ' ' | |
104 | ||
105 | } | |
106 | ||
1c6a4e30 | 107 | list_unique() { |
805da540 | 108 | local items item |
2212045f | 109 | for item in "$@"; do |
805da540 MT |
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 | |
2212045f | 123 | for i in "$@"; do |
e726ef8d MT |
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 | |
2212045f | 140 | for i in "$@"; do |
e726ef8d MT |
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 | |
2212045f | 155 | for arg in "$@"; do |
55ea0266 MT |
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 | 179 | local reversed arg |
2212045f | 180 | for arg in "$@"; do |
6396ccab MT |
181 | reversed="${arg} ${reversed}" |
182 | done | |
183 | ||
184 | print "${reversed}" | |
185 | return ${EXIT_OK} | |
186 | } | |
7ad5252c | 187 | |
1c6a4e30 | 188 | list_head() { |
7ad5252c | 189 | local arg |
2212045f | 190 | for arg in "$@"; do |
7ad5252c MT |
191 | print "${arg}" |
192 | return ${EXIT_OK} | |
193 | done | |
194 | ||
195 | return ${EXIT_ERROR} | |
196 | } | |
60b1f378 MT |
197 | |
198 | list_directory() { | |
199 | local dir="${1}" | |
200 | ||
201 | # Only works for directories | |
202 | if [ ! -d "${dir}" ]; then | |
203 | return ${EXIT_ERROR} | |
204 | fi | |
205 | ||
206 | local path | |
207 | for path in ${dir}/*; do | |
208 | if [ -e "${path}" ]; then | |
209 | basename "${path}" | |
210 | fi | |
211 | done | |
212 | ||
213 | return ${EXIT_OK} | |
214 | } |