]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.list
wpa_supplicant: Add ctrl_interface
[people/stevee/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 assign "${list}" "${!list} $@"
48 else
49 assign "${list}" "$@"
50 fi
51 }
52
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
75 list_remove() {
76 local list=${1}
77 shift
78
79 assert isset list
80 assert [ ${list} != "list" ]
81
82 local ret=${EXIT_ERROR}
83
84 local _list k
85 for k in ${!list}; do
86 if list_match ${k} "$@"; then
87 ret=${EXIT_OK}
88 continue
89 fi
90
91 _list="${_list} ${k}"
92 done
93
94 assign "${list}" "${_list}"
95
96 return ${ret}
97 }
98
99 list_sort() {
100 local i
101 for i in "$@"; do
102 print "${i}"
103 done | sort | tr '\n' ' '
104 print
105 }
106
107 list_unique() {
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
118 list_match() {
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
130 list_is_empty() {
131 local list="${1}"
132
133 [ ! -n "${!list}" ]
134 }
135
136 list_length() {
137 local length=0
138
139 local i
140 for i in "$@"; do
141 length=$(( ${length} + 1 ))
142 done
143
144 print "${length}"
145 }
146
147 # Count how often $1 occurs in the list.
148 list_count() {
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 }
163
164 list_join() {
165 local list=${1}
166 local delim=${2}
167
168 assert isset list
169 assert isset delim
170 assert [ ${list} != "list" ]
171
172 local ret
173 printf -v ret "${delim}%s" ${!list}
174
175 print "${ret:${#delim}}"
176 }
177
178 list_reverse() {
179 local reversed arg
180 for arg in "$@"; do
181 reversed="${arg} ${reversed}"
182 done
183
184 print "${reversed}"
185 return ${EXIT_OK}
186 }
187
188 list_head() {
189 local arg
190 for arg in "$@"; do
191 print "${arg}"
192 return ${EXIT_OK}
193 done
194
195 return ${EXIT_ERROR}
196 }
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 }