]> git.ipfire.org Git - people/stevee/network.git/blob - src/functions/functions.list
Remove the function keyword which is a bashism
[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 shift
29
30 local arg
31 for arg in $@; do
32 list_append_one "${list}" "${arg}"
33 done
34 }
35
36 list_append_one() {
37 assert [ $# -eq 2 ]
38
39 local list="${1}"
40 shift
41
42 assert isset list
43
44 if [ -n "${!list}" ]; then
45 printf -v ${list} -- "${!list} $@"
46 else
47 printf -v ${list} -- "$@"
48 fi
49 }
50
51 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 list_sort() {
68 local i
69 for i in $@; do
70 print "${i}"
71 done | sort | tr '\n' ' '
72 print
73 }
74
75 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
86 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 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 }
108
109 # Count how often $1 occurs in the list.
110 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 }
125
126 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 }
135
136 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 }
145
146 list_head() {
147 local arg
148 for arg in $@; do
149 print "${arg}"
150 return ${EXIT_OK}
151 done
152
153 return ${EXIT_ERROR}
154 }