]>
Commit | Line | Data |
---|---|---|
1848564d MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2010 Michael Tremer & Christian Schmidt # | |
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 | ||
97cb552e | 22 | # A simple print statement |
1c6a4e30 | 23 | print() { |
acc9efd5 MT |
24 | local fmt=${1}; shift |
25 | ||
40e3553f | 26 | printf -- "${fmt}\n" "$@" |
97cb552e MT |
27 | } |
28 | ||
67baa452 MT |
29 | print_indent() { |
30 | local i=${1} | |
31 | shift | |
32 | ||
33 | while (( i-- )); do | |
34 | printf "\t" | |
35 | done | |
36 | ||
37 | print "%s" "$@" | |
38 | } | |
39 | ||
cb965348 MT |
40 | # The args() function takes a number of arguments like |
41 | # var1="abc d" var2="abc" var3="abcd e" | |
42 | # and splits them into several arguments, devided by newline | |
1c6a4e30 | 43 | args() { |
cb965348 MT |
44 | echo "$@" | xargs printf "%s\n" |
45 | } | |
46 | ||
1c6a4e30 | 47 | unquote() { |
04854c77 MT |
48 | local var="$@" |
49 | ||
50 | if [ "${var:0:1}" = "\"" ]; then | |
51 | var=${var:1} | |
52 | fi | |
53 | ||
54 | local last=$(( ${#var} - 1 )) | |
55 | if [ ${last} -ge 0 ] && [ "${var:${last}:1}" = "\"" ]; then | |
56 | var=${var:0:${last}} | |
57 | fi | |
58 | ||
59 | print "${var}" | |
60 | } | |
61 | ||
1c6a4e30 | 62 | quote() { |
04854c77 MT |
63 | print "\"%s\"" "$@" |
64 | } | |
65 | ||
1c6a4e30 | 66 | strip() { |
fe52c5e0 MT |
67 | local value="$@" |
68 | ||
69 | # remove leading whitespace characters | |
70 | value="${value#"${value%%[![:space:]]*}"}" | |
71 | ||
72 | # remove trailing whitespace characters | |
73 | value="${value%"${value##*[![:space:]]}"}" | |
74 | ||
75 | print "${value}" | |
76 | } | |
77 | ||
1848564d | 78 | # Print a pretty error message |
1c6a4e30 | 79 | error() { |
fcbf6823 | 80 | echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2 |
1848564d MT |
81 | } |
82 | ||
1c6a4e30 | 83 | error_log() { |
1b7a1578 MT |
84 | log ERROR "$@" |
85 | } | |
86 | ||
1848564d | 87 | # Print a pretty warn message |
1c6a4e30 | 88 | warning() { |
fcbf6823 | 89 | echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2 |
1848564d MT |
90 | } |
91 | ||
1c6a4e30 | 92 | warning_log() { |
1b7a1578 MT |
93 | log WARNING "$@" |
94 | } | |
95 | ||
1848564d | 96 | # Speedup function to avoid a call of the basename binary |
1c6a4e30 | 97 | basename() { |
1848564d MT |
98 | echo "${1##*/}" |
99 | } | |
100 | ||
1c6a4e30 | 101 | format() { |
e5651e17 MT |
102 | local key=${1} |
103 | assert isset key | |
104 | ||
105 | local format=${2} | |
106 | assert isset format | |
107 | ||
108 | shift 2 | |
109 | ||
110 | printf -v "${key}" "${format}" "$@" | |
111 | } | |
112 | ||
d13929d4 MT |
113 | format_time() { |
114 | local s=${1} | |
115 | local ret m | |
116 | ||
117 | local units="s m h" | |
118 | ||
119 | local unit | |
120 | for unit in ${units}; do | |
121 | m=$(( ${s} % 60 )) | |
122 | s=$(( ${s} / 60 )) | |
123 | ||
124 | if [ ${m} -gt 0 ]; then | |
125 | ret="${m}${unit} ${ret}" | |
126 | fi | |
127 | done | |
128 | ||
129 | # Remove whitespace | |
130 | echo ${ret} | |
131 | } | |
132 | ||
b383499d MT |
133 | parse_time() { |
134 | local ret=0 | |
135 | ||
136 | local arg | |
137 | for arg in $@; do | |
138 | local unit | |
139 | ||
140 | case "${arg}" in | |
141 | *h|*m|*s) | |
142 | # Store unit | |
143 | unit="${arg: -1}" | |
144 | ||
145 | # Remove unit | |
146 | arg="${arg:0:-1}" | |
147 | ;; | |
148 | esac | |
149 | ||
150 | if ! isinteger arg; then | |
151 | return ${EXIT_ERROR} | |
152 | fi | |
153 | ||
154 | # Convert hours and minutes into seconds | |
155 | case "${unit}" in | |
156 | h) | |
157 | arg=$(( ${arg} * 3600 )) | |
158 | ;; | |
159 | m) | |
160 | arg=$(( ${arg} * 60 )) | |
161 | ;; | |
162 | esac | |
163 | ||
164 | # Add up everything | |
165 | ret=$(( ${ret} + ${arg} )) | |
166 | done | |
167 | ||
168 | print "${ret}" | |
169 | } | |
170 | ||
1c6a4e30 | 171 | assign() { |
b79ad79b MT |
172 | local key=${1} |
173 | assert isset key | |
174 | shift | |
175 | ||
e5651e17 | 176 | format "${key}" "%s" "$@" |
b79ad79b MT |
177 | } |
178 | ||
1c6a4e30 | 179 | fread() { |
b79ad79b MT |
180 | local file=${1} |
181 | assert isset file | |
182 | ||
183 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
184 | ||
185 | print "$(<${file})" | |
186 | } | |
187 | ||
1c6a4e30 | 188 | fwrite() { |
b79ad79b MT |
189 | local file=${1} |
190 | assert isset file | |
191 | shift | |
192 | ||
644d3bb8 MT |
193 | if ! print "%s" "$@" > ${file} 2>/dev/null; then |
194 | error "Could not write to file: ${file}" | |
195 | return ${EXIT_ERROR} | |
196 | fi | |
197 | ||
198 | return ${EXIT_OK} | |
199 | } | |
200 | ||
201 | fappend() { | |
202 | local file=${1} | |
203 | assert isset file | |
204 | shift | |
205 | ||
e1947a76 | 206 | if [ -e "${file}" ] && [ ! -w "${file}" ]; then |
8d4e0d52 MT |
207 | log ERROR "${file}: No such file" |
208 | return ${EXIT_ERROR} | |
209 | fi | |
210 | ||
211 | print "%s" "$@" >> ${file} 2>/dev/null | |
b79ad79b MT |
212 | } |
213 | ||
fa33d830 MT |
214 | file_delete() { |
215 | local file=${1} | |
216 | ||
217 | unlink "${file}" 2>/dev/null | |
218 | } | |
219 | ||
e1947a76 MT |
220 | file_exists() { |
221 | local file=${1} | |
222 | ||
223 | [ -e "${file}" ] && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
224 | } | |
225 | ||
226 | file_is_newer_than() { | |
227 | local file1="${1}" | |
228 | local file2="${2}" | |
229 | ||
230 | local age1=$(file_get_age "${file1}") | |
231 | local age2=$(file_get_age "${file2}") | |
232 | ||
233 | if [ ${age1} -gt ${age2} ]; then | |
234 | return ${EXIT_TRUE} | |
235 | else | |
236 | return ${EXIT_FALSE} | |
237 | fi | |
238 | } | |
239 | ||
240 | file_get_age() { | |
241 | local file="${1}" | |
242 | ||
243 | if [ -e "${file}" ]; then | |
244 | stat --format="%Y" "${file}" | |
245 | return $? | |
246 | fi | |
247 | ||
248 | return ${EXIT_ERROR} | |
249 | } | |
250 | ||
c041b631 MT |
251 | make_parent_dir() { |
252 | local path="${1}" | |
253 | ||
254 | local dirname="$(dirname "${path}")" | |
255 | mkdir -p "${dirname}" | |
256 | } | |
257 | ||
1c6a4e30 | 258 | enabled() { |
1848564d MT |
259 | local param=${1} |
260 | ||
e726ef8d | 261 | list_match "${!param}" yes on true 1 |
1848564d MT |
262 | } |
263 | ||
1c6a4e30 | 264 | mac_generate() { |
fb1416c6 MT |
265 | local b="$(random 12)" |
266 | ||
267 | # Remove multicast bit | |
268 | # and set address is software assigned | |
269 | local first_byte=$(( 0x${b:0:2} & 0xfe )) | |
270 | first_byte=$(( ${first_byte} | 0x02 )) | |
1848564d MT |
271 | |
272 | local output | |
fb1416c6 | 273 | printf -v output "%02x" "${first_byte}" |
790b7ec9 | 274 | |
fb1416c6 | 275 | output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}" |
1848564d MT |
276 | |
277 | # Check if output is valid | |
fb1416c6 | 278 | assert mac_is_valid "${output}" |
1848564d | 279 | |
790b7ec9 | 280 | echo "${output}" |
1848564d MT |
281 | } |
282 | ||
1c6a4e30 | 283 | mac_format() { |
18b43372 | 284 | local mac=${1} |
48bc31eb | 285 | assert isset mac |
18b43372 | 286 | |
48bc31eb MT |
287 | # Remove all colons and make the rest lowercase. |
288 | mac=${mac//:/} | |
289 | mac=${mac,,} | |
18b43372 | 290 | |
48bc31eb | 291 | local output |
18b43372 MT |
292 | if [ "${#mac}" = "12" ]; then |
293 | # Add colons (:) to mac address | |
294 | output=${mac:0:2} | |
295 | local i | |
296 | for i in 2 4 6 8 10; do | |
297 | output="${output}:${mac:${i}:2}" | |
298 | done | |
48bc31eb MT |
299 | else |
300 | output=${mac} | |
18b43372 MT |
301 | fi |
302 | ||
303 | assert mac_is_valid ${output} | |
304 | ||
48bc31eb | 305 | print "${output}" |
18b43372 MT |
306 | } |
307 | ||
1c6a4e30 | 308 | mac_is_valid() { |
1848564d MT |
309 | local mac=${1} |
310 | ||
311 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
312 | } | |
313 | ||
1c6a4e30 | 314 | uuid() { |
de543653 | 315 | echo $(</proc/sys/kernel/random/uuid) |
1848564d MT |
316 | } |
317 | ||
a24cff8f JS |
318 | abs() { |
319 | local val=${1} | |
320 | ||
321 | if [ ${val} -lt 0 ]; then | |
322 | (( val *= -1 )) | |
323 | fi | |
324 | ||
325 | echo ${val} | |
326 | } | |
327 | ||
fb1416c6 MT |
328 | rand() { |
329 | local uuid="$(uuid)" | |
330 | echo "${uuid//-/}" | |
331 | } | |
332 | ||
333 | random() { | |
334 | local length="${1:-8}" | |
335 | ||
336 | local random | |
337 | while [ ${#random} -lt ${length} ]; do | |
338 | random="${random}$(rand)" | |
339 | done | |
340 | ||
341 | echo "${random:0:${length}}" | |
342 | } | |
343 | ||
1c6a4e30 | 344 | isset() { |
1848564d MT |
345 | local var=${1} |
346 | ||
347 | [ -n "${!var}" ] | |
348 | } | |
349 | ||
1c6a4e30 | 350 | isoneof() { |
1848564d MT |
351 | local var=${!1} |
352 | shift | |
353 | ||
e726ef8d | 354 | list_match "${var}" "$@" |
1848564d MT |
355 | } |
356 | ||
1c6a4e30 | 357 | isbool() { |
1848564d MT |
358 | local var=${1} |
359 | ||
ec6afbdd | 360 | isoneof ${var} 0 1 no yes on off true false |
1848564d MT |
361 | } |
362 | ||
1c6a4e30 | 363 | isinteger() { |
1848564d MT |
364 | local var=${!1} |
365 | ||
366 | [[ ${var} =~ ^[0-9]+$ ]] | |
367 | } | |
368 | ||
1c6a4e30 | 369 | ismac() { |
1848564d MT |
370 | local mac=${!1} |
371 | ||
372 | mac_is_valid ${mac} | |
373 | } | |
374 | ||
1c6a4e30 | 375 | isipaddress() { |
fef4edaf MT |
376 | local addr=${!1} |
377 | ||
378 | ip_is_valid ${addr} | |
379 | } | |
380 | ||
48a64768 JS |
381 | mtu_is_valid() { |
382 | local proto=${1} | |
383 | local mtu=${2} | |
384 | ||
385 | case ${proto} in | |
386 | ipv4) | |
387 | [ ${mtu} -ge 576 ] && [ ${mtu} -le 9000 ] | |
388 | ;; | |
389 | ipv6) | |
390 | [ ${mtu} -ge 1280 ] && [ ${mtu} -le 9000 ] | |
391 | ;; | |
392 | *) | |
393 | error "${proto} is not a valid proto" | |
394 | return ${EXIT_ERROR} | |
395 | ;; | |
396 | esac | |
397 | } | |
398 | ||
1c6a4e30 | 399 | backtrace() { |
711ffac1 MT |
400 | local start=1 |
401 | ||
402 | echo # Empty line | |
403 | error_log "Backtrace (most recent call in first line):" | |
404 | ||
04608623 | 405 | local i source |
711ffac1 MT |
406 | for i in $(seq ${start} ${#BASH_SOURCE[*]}); do |
407 | [ -z "${FUNCNAME[${i}]}" ] && continue | |
6396ccab MT |
408 | |
409 | # Print called binary with arguments. | |
410 | if [ "${FUNCNAME[${i}]}" == "main" ]; then | |
411 | local args="$(list_reverse ${BASH_ARGV[*]})" | |
412 | printf -v source "%20s" "$0" | |
413 | error_log " ${source} ${args}" | |
414 | continue | |
415 | fi | |
711ffac1 | 416 | |
04608623 MT |
417 | source=${BASH_SOURCE[$(( ${i} + 1 ))]} |
418 | error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}" | |
711ffac1 MT |
419 | done |
420 | } | |
421 | ||
1c6a4e30 | 422 | assert() { |
1848564d MT |
423 | local assertion="$@" |
424 | ||
425 | if ! ${assertion}; then | |
4c670d7c | 426 | error_log "Assertion '${assertion}' failed." |
711ffac1 | 427 | backtrace |
cfbe0802 | 428 | exit ${EXIT_ERROR_ASSERT} |
1848564d MT |
429 | fi |
430 | ||
431 | return ${EXIT_OK} | |
432 | } | |
cad8bd85 | 433 | |
b0b2f995 MT |
434 | # This function checks, if the given argument is an assert error |
435 | # exit code. If this is the case, the script will halt immediately. | |
1c6a4e30 | 436 | assert_check_retval() { |
b0b2f995 MT |
437 | local ret=${1} |
438 | ||
439 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
440 | exit ${EXIT_ERROR_ASSERT} | |
441 | fi | |
442 | ||
443 | return ${ret} | |
444 | } | |
445 | ||
2bb20bbd SS |
446 | # This function executes the given command and inverses the return code |
447 | not() { | |
448 | local command="$@" | |
449 | ||
450 | ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE} | |
451 | } | |
452 | ||
1c6a4e30 | 453 | exec_cmd() { |
711ffac1 MT |
454 | local cmd=$@ |
455 | ||
456 | log DEBUG "Running command: ${cmd}" | |
457 | ||
b816e04b | 458 | DEBUG=${DEBUG} \ |
8c63fa13 MT |
459 | LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \ |
460 | LOG_FACILITY="${LOG_FACILITY}" \ | |
b816e04b | 461 | ${SHELL} ${cmd} |
711ffac1 MT |
462 | local ret=$? |
463 | ||
464 | #log DEBUG "Returned with code '${ret}'" | |
465 | ||
466 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
467 | error_log "Stopping parent process due to assertion error in child process: ${cmd}" | |
468 | exit ${EXIT_ERROR_ASSERT} | |
469 | fi | |
470 | ||
471 | return ${ret} | |
472 | } | |
473 | ||
1c6a4e30 | 474 | cmd() { |
b816e04b MT |
475 | local cmd=$@ |
476 | ||
477 | log DEBUG "Running command: ${cmd}" | |
478 | ||
4b75fc83 MT |
479 | env -i -- \ |
480 | HOME="${HOME}" \ | |
481 | PATH="${PATH}" \ | |
482 | TERM="${TERM}" \ | |
483 | ${cmd} | |
484 | local ret=$? | |
b816e04b | 485 | |
4b75fc83 MT |
486 | case "${ret}" in |
487 | ${EXIT_OK}) | |
488 | return ${EXIT_OK} | |
489 | ;; | |
490 | *) | |
491 | log DEBUG "Returned with code '${ret}'" | |
492 | return ${ret} | |
493 | ;; | |
494 | esac | |
b816e04b MT |
495 | } |
496 | ||
1c6a4e30 | 497 | cmd_quiet() { |
98146c00 | 498 | cmd $@ &>/dev/null |
3efecbb3 MT |
499 | } |
500 | ||
1c6a4e30 | 501 | cmd_exec() { |
f80ce052 MT |
502 | local cmd=$@ |
503 | ||
504 | log DEBUG "Exec'ing command: ${cmd}" | |
505 | ||
506 | exec ${cmd} | |
507 | ||
508 | log ERROR "Could not exec-ute: ${cmd}" | |
509 | exit ${EXIT_ERROR} | |
510 | } | |
511 | ||
1c6a4e30 | 512 | cmd_not_implemented() { |
2181765d MT |
513 | assert false "not implemented" |
514 | } | |
515 | ||
f5ee091e MT |
516 | # Executes the given command in background |
517 | cmd_background() { | |
518 | cmd_quiet $@ & | |
519 | } | |
520 | ||
521 | # Prints the PID of the process that was started last | |
522 | cmd_background_get_pid() { | |
523 | print "${!}" | |
524 | } | |
525 | ||
526 | cmd_background_result() { | |
527 | local pids=$@ | |
528 | ||
529 | wait ${pids} | |
530 | } | |
531 | ||
b8026986 | 532 | # Increase security of the read command |
1c6a4e30 | 533 | read() { |
b8026986 MT |
534 | builtin read -r $@ |
535 | } | |
536 | ||
1c6a4e30 | 537 | seq() { |
3efecbb3 MT |
538 | if [ $# -eq 2 ]; then |
539 | eval echo {${1}..${2}} | |
540 | elif [ $# -eq 3 ]; then | |
541 | eval echo {${1}..${3}..${2}} | |
542 | fi | |
543 | } | |
544 | ||
de72bd91 MT |
545 | range() { |
546 | eval echo {0..$(( ${1} - 1 ))} | |
547 | } | |
548 | ||
549 | count() { | |
550 | local i=0 | |
551 | ||
552 | while read; do | |
553 | ((i++)) | |
554 | done | |
555 | ||
556 | echo ${i} | |
557 | } | |
558 | ||
1c6a4e30 | 559 | which() { |
76e6cd51 MT |
560 | type -P $@ |
561 | } | |
562 | ||
fe52c5e0 | 563 | # Prints the number of seconds since epoch. |
1c6a4e30 | 564 | timestamp() { |
fe52c5e0 MT |
565 | date -u "+%s" |
566 | } | |
567 | ||
1c6a4e30 | 568 | beautify_time() { |
d82cf370 MT |
569 | local value=${1} |
570 | ||
571 | local unit | |
572 | local limit | |
573 | for unit in s m h d w; do | |
574 | case "${unit}" in | |
575 | s|m|h) | |
576 | limit=60 | |
577 | ;; | |
578 | d) | |
579 | limit=24 | |
580 | ;; | |
581 | w) | |
582 | limit=7 | |
583 | ;; | |
584 | esac | |
585 | ||
586 | [ ${value} -lt ${limit} ] && break | |
587 | ||
588 | value=$(( ${value} / ${limit} )) | |
589 | done | |
590 | ||
591 | echo "${value}${unit}" | |
592 | } | |
711ffac1 | 593 | |
1c6a4e30 | 594 | beautify_bytes() { |
711ffac1 MT |
595 | local value=${1} |
596 | ||
597 | local unit | |
598 | local limit=1024 | |
599 | for unit in B k M G T; do | |
600 | [ ${value} -lt ${limit} ] && break | |
601 | value=$(( ${value} / ${limit} )) | |
602 | done | |
603 | ||
604 | echo "${value}${unit}" | |
605 | } | |
943e3f7e | 606 | |
1c6a4e30 | 607 | module_load() { |
943e3f7e MT |
608 | local module=${1} |
609 | ||
dd891ec4 MT |
610 | # Do nothing if the module is already loaded |
611 | if [ -d "/sys/module/${module//-/_}" ]; then | |
612 | return ${EXIT_OK} | |
943e3f7e | 613 | fi |
dd891ec4 MT |
614 | |
615 | log DEBUG "Loading kernel module ${module}" | |
616 | modprobe "${module}" | |
943e3f7e | 617 | } |
6b3f9c85 | 618 | |
1c6a4e30 | 619 | binary_exists() { |
6b3f9c85 MT |
620 | local binary=${1} |
621 | ||
622 | if [ -n "$(type -p ${binary})" ]; then | |
623 | return ${EXIT_OK} | |
624 | fi | |
625 | ||
626 | return ${EXIT_ERROR} | |
627 | } | |
d76f5107 | 628 | |
1c6a4e30 | 629 | function_exists() { |
1e6f187e MT |
630 | local function="${1}" |
631 | ||
632 | if [ "$(type -t "${function}")" = "function" ]; then | |
633 | return ${EXIT_TRUE} | |
634 | fi | |
635 | ||
636 | return ${EXIT_FALSE} | |
637 | } | |
638 | ||
1c6a4e30 | 639 | process_kill() { |
d76f5107 MT |
640 | local process=${1} |
641 | ||
642 | if ! isinteger process; then | |
643 | process=$(pidof ${process}) | |
644 | fi | |
645 | ||
646 | local pid | |
647 | local sig | |
648 | for pid in ${process}; do | |
649 | for sig in 15 9; do | |
650 | [ -d "/proc/${pid}" ] || break | |
651 | ||
652 | kill -${sig} ${pid} | |
653 | sleep 1 | |
654 | done | |
655 | done | |
656 | } | |
feb76eaf | 657 | |
1c6a4e30 | 658 | dec() { |
feb76eaf MT |
659 | local hex=${1} |
660 | ||
661 | if [ "${hex:0:2}" != "0x" ]; then | |
662 | hex="0x${hex}" | |
663 | fi | |
664 | ||
665 | printf "%d\n" "${hex}" | |
666 | } | |
3a7fef62 | 667 | |
1c6a4e30 | 668 | chr() { |
5cf0edf9 MT |
669 | local char="${1}" |
670 | ||
671 | [ ${char} -lt 256 ] || return ${EXIT_ERROR} | |
672 | ||
673 | printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n" | |
674 | } | |
675 | ||
1c6a4e30 | 676 | ord() { |
5cf0edf9 MT |
677 | LC_CTYPE="C" printf "%d\n" "'${1}" |
678 | } | |
679 | ||
1c6a4e30 | 680 | hex() { |
5cf0edf9 MT |
681 | printf "%X\n" "${1}" |
682 | } | |
683 | ||
1c6a4e30 | 684 | network_is_running() { |
3a7fef62 MT |
685 | # Check, if the network service is running. |
686 | service_is_active network | |
687 | } | |
f80ce052 | 688 | |
1c6a4e30 | 689 | contains_spaces() { |
f80ce052 MT |
690 | local var="$@" |
691 | ||
692 | # Eliminate spaces. | |
693 | local var2=${var// /} | |
694 | ||
695 | if [ ${#var} -ne ${#var2} ]; then | |
696 | return ${EXIT_TRUE} | |
697 | fi | |
698 | ||
699 | return ${EXIT_FALSE} | |
0d645497 MT |
700 | } |
701 | ||
702 | string_match() { | |
703 | local match=${1} | |
704 | local string=${2} | |
705 | ||
706 | [[ ${string} =~ ${match} ]] && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
f80ce052 | 707 | } |
5cf0edf9 | 708 | |
1c6a4e30 | 709 | string_split() { |
5cf0edf9 MT |
710 | local string="$@" |
711 | ||
712 | local pos=0 | |
713 | while [ ${pos} -lt ${#string} ]; do | |
714 | print "${string:${pos}:1}" | |
715 | pos=$(( ${pos} + 1 )) | |
716 | done | |
717 | ||
718 | return ${EXIT_OK} | |
719 | } | |
720 | ||
1c6a4e30 | 721 | string_reverse() { |
5cf0edf9 MT |
722 | local string="$@" |
723 | ||
724 | local output | |
725 | local pos=0 | |
726 | while [ ${pos} -lt ${#string} ]; do | |
727 | output="${string:${pos}:1}${output}" | |
728 | pos=$(( ${pos} + 1 )) | |
729 | done | |
730 | ||
731 | print "${output}" | |
732 | return ${EXIT_OK} | |
733 | } | |
734 | ||
1c6a4e30 | 735 | dec2bin() { |
5cf0edf9 MT |
736 | local number="${1}" |
737 | ||
738 | local output | |
739 | ||
740 | local i div | |
741 | for i in 7 6 5 4 3 2 1; do | |
742 | div=$(( 2 ** ${i} )) | |
743 | ||
744 | if [ $(( ${number} / ${div} )) -eq 1 ]; then | |
745 | output="${output}1" | |
746 | else | |
747 | output="${output}0" | |
748 | fi | |
749 | number="$(( ${number} % ${div} ))" | |
750 | done | |
751 | ||
752 | if [ $(( ${number} % 2 )) -eq 1 ]; then | |
753 | output="${output}1" | |
754 | else | |
755 | output="${output}0" | |
756 | fi | |
757 | ||
758 | print "${output}" | |
759 | } | |
760 | ||
1c6a4e30 | 761 | bin2dec() { |
5cf0edf9 MT |
762 | local string="${1}" |
763 | local number=0 | |
764 | ||
765 | local pos=0 char | |
766 | while [ ${pos} -lt ${#string} ]; do | |
767 | char="${string:${pos}:1}" | |
768 | pos=$(( ${pos} + 1 )) | |
769 | ||
770 | number=$(( ${number} << 1 )) | |
771 | ||
772 | case "${char}" in | |
773 | 0) ;; | |
774 | 1) | |
775 | number=$(( ${number} + 1 )) | |
776 | ;; | |
777 | *) | |
778 | assert false "Invalid character: ${char}" | |
779 | ;; | |
780 | esac | |
781 | done | |
782 | ||
783 | print "${number}" | |
784 | return ${EXIT_OK} | |
785 | } | |
786 | ||
1c6a4e30 | 787 | char2bin() { |
5cf0edf9 MT |
788 | local dec="$(ord "${1}")" |
789 | ||
790 | dec2bin "${dec}" | |
791 | } | |
792 | ||
1c6a4e30 | 793 | bin2char() { |
5cf0edf9 MT |
794 | local dec="$(bin2dec "$@")" |
795 | ||
796 | chr "${dec}" | |
797 | } | |
798 | ||
1c6a4e30 | 799 | bin2hex() { |
5cf0edf9 MT |
800 | local dec="$(bin2dec "$@")" |
801 | ||
802 | dec2hex "${dec}" | |
803 | } | |
804 | ||
1c6a4e30 | 805 | hex2bin() { |
5cf0edf9 MT |
806 | local dec="$(hex2dec "$@")" |
807 | ||
808 | dec2bin "${dec}" | |
809 | } | |
810 | ||
1c6a4e30 | 811 | hex2dec() { |
5cf0edf9 MT |
812 | local hex="${1}" |
813 | ||
814 | # Prepend 0x if necessary. | |
815 | [ "${hex:0:2}" = "0x" ] || hex="0x${hex}" | |
816 | ||
817 | printf "%d\n" "${hex}" | |
818 | } | |
819 | ||
1c6a4e30 | 820 | dec2hex() { |
5cf0edf9 MT |
821 | printf "%02x\n" "${1}" |
822 | } | |
a95d16fc | 823 | |
10b53c87 | 824 | # This function just copy config files |
a95d16fc | 825 | copy() { |
a95d16fc JS |
826 | assert [ $# -eq 2 ] |
827 | ||
828 | local src=${1} | |
829 | local dst=${2} | |
830 | ||
287c2e60 MT |
831 | # Check if we can read from the source |
832 | if [ ! -r "${src}" ]; then | |
833 | log ERROR "Cannot read ${src}" | |
834 | return ${EXIT_ERROR} | |
835 | fi | |
836 | ||
a95d16fc | 837 | # Check if ${dst} is a directory |
1fade616 | 838 | if [ -d "${dst}" ]; then |
9c802e1d | 839 | log ERROR "${dst} is a directory" |
a95d16fc JS |
840 | return ${EXIT_ERROR} |
841 | fi | |
842 | ||
843 | if ! fread "${src}" > "${dst}"; then | |
844 | log ERROR "Could not copy data from ${src} to ${dst}" | |
845 | return ${EXIT_ERROR} | |
846 | fi | |
847 | } |