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