]>
Commit | Line | Data |
---|---|---|
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 | function list_append() { | |
26 | local list=${1} | |
27 | shift | |
28 | ||
29 | assert isset list | |
30 | ||
31 | if [ -n "${!list}" ]; then | |
32 | printf -v ${list} "${!list} $@" | |
33 | else | |
34 | printf -v ${list} "$@" | |
35 | fi | |
36 | } | |
37 | ||
38 | function list_remove() { | |
39 | local list=${1} | |
40 | shift | |
41 | ||
42 | assert isset list | |
43 | ||
44 | local _list k | |
45 | for k in ${!list}; do | |
46 | listmatch ${k} $@ && continue | |
47 | ||
48 | _list="${_list} ${k}" | |
49 | done | |
50 | ||
51 | eval "${list}=\"${_list}\"" | |
52 | } | |
53 | ||
54 | function list_sort() { | |
55 | local i | |
56 | for i in $@; do | |
57 | print "${i}" | |
58 | done | sort | tr '\n' ' ' | |
59 | ||
60 | } | |
61 | ||
62 | function list_unique() { | |
63 | local items item | |
64 | for item in $@; do | |
65 | # Check if the item has already been processed. | |
66 | list_match "${item}" ${items} && continue | |
67 | ||
68 | list_append items "${item}" | |
69 | print "${item}" | |
70 | done | |
71 | } | |
72 | ||
73 | function list_match() { | |
74 | local match=${1} | |
75 | shift | |
76 | ||
77 | local i | |
78 | for i in $@; do | |
79 | [ "${match}" = "${i}" ] && return ${EXIT_OK} | |
80 | done | |
81 | ||
82 | return ${EXIT_ERROR} | |
83 | } | |
84 | ||
85 | function list_length() { | |
86 | local length=0 | |
87 | ||
88 | local i | |
89 | for i in $@; do | |
90 | length=$(( ${length} + 1 )) | |
91 | done | |
92 | ||
93 | print "${length}" | |
94 | } | |
95 | ||
96 | # Count how often $1 occurs in the list. | |
97 | function list_count() { | |
98 | local what=${1} | |
99 | shift | |
100 | ||
101 | local counter=0 | |
102 | ||
103 | local arg | |
104 | for arg in $@; do | |
105 | if [ "${arg}" = "${what}" ]; then | |
106 | counter=$(( ${counter} + 1 )) | |
107 | fi | |
108 | done | |
109 | ||
110 | print "${counter}" | |
111 | } | |
112 | ||
113 | function list_join() { | |
114 | local list=${1} | |
115 | local delim=${2} | |
116 | ||
117 | local ret | |
118 | printf -v ret "${delim}%s" ${!list} | |
119 | ||
120 | print "${ret:${#delim}}" | |
121 | } |