]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | # A simple print statement | |
23 | print() { | |
24 | local fmt=${1}; shift | |
25 | ||
26 | printf -- "${fmt}\n" "$@" | |
27 | } | |
28 | ||
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 | ||
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 | |
43 | args() { | |
44 | echo "$@" | xargs printf "%s\n" | |
45 | } | |
46 | ||
47 | unquote() { | |
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 | ||
62 | quote() { | |
63 | print "\"%s\"" "$@" | |
64 | } | |
65 | ||
66 | strip() { | |
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 | ||
78 | # Print a pretty error message | |
79 | error() { | |
80 | echo -e " ${CLR_RED_B}ERROR${CLR_RESET} : $@" >&2 | |
81 | } | |
82 | ||
83 | error_log() { | |
84 | log ERROR "$@" | |
85 | } | |
86 | ||
87 | # Print a pretty warn message | |
88 | warning() { | |
89 | echo -e " ${CLR_YELLOW_B}WARNING${CLR_RESET}: $@" >&2 | |
90 | } | |
91 | ||
92 | warning_log() { | |
93 | log WARNING "$@" | |
94 | } | |
95 | ||
96 | # Speedup function to avoid a call of the basename binary | |
97 | basename() { | |
98 | echo "${1##*/}" | |
99 | } | |
100 | ||
101 | format() { | |
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 | ||
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 | ||
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 | ||
171 | assign() { | |
172 | local key=${1} | |
173 | assert isset key | |
174 | shift | |
175 | ||
176 | format "${key}" "%s" "$@" | |
177 | } | |
178 | ||
179 | fread() { | |
180 | local file=${1} | |
181 | assert isset file | |
182 | ||
183 | [ -r "${file}" ] || return ${EXIT_ERROR} | |
184 | ||
185 | print "$(<${file})" | |
186 | } | |
187 | ||
188 | fwrite() { | |
189 | local file=${1} | |
190 | assert isset file | |
191 | shift | |
192 | ||
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 | ||
206 | if [ -e "${file}" ] && [ ! -w "${file}" ]; then | |
207 | log ERROR "${file}: No such file" | |
208 | return ${EXIT_ERROR} | |
209 | fi | |
210 | ||
211 | print "%s" "$@" >> ${file} 2>/dev/null | |
212 | } | |
213 | ||
214 | file_delete() { | |
215 | local file=${1} | |
216 | ||
217 | unlink "${file}" 2>/dev/null | |
218 | } | |
219 | ||
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 | ||
251 | file_to_log() { | |
252 | local level="${1}" | |
253 | assert isset level | |
254 | ||
255 | local file="${2}" | |
256 | assert file_exists "${file}" | |
257 | ||
258 | local line | |
259 | while read line; do | |
260 | log "${level}" "${line}" | |
261 | done < "${file}" | |
262 | } | |
263 | ||
264 | make_directory() { | |
265 | local path="${1}" | |
266 | ||
267 | # Exit silently when directory already exists | |
268 | if [ -d "${path}" ]; then | |
269 | return ${EXIT_OK} | |
270 | fi | |
271 | ||
272 | if ! mkdir -p "${path}"; then | |
273 | log ERROR "Could not create directory ${path}" | |
274 | return ${EXIT_ERROR} | |
275 | fi | |
276 | ||
277 | log DEBUG "Created directory ${path}" | |
278 | return ${EXIT_OK} | |
279 | } | |
280 | ||
281 | make_parent_directory() { | |
282 | local path="${1}" | |
283 | ||
284 | make_directory "$(dirname "${path}")" | |
285 | } | |
286 | ||
287 | enabled() { | |
288 | local param=${1} | |
289 | ||
290 | list_match "${!param}" yes on true 1 | |
291 | } | |
292 | ||
293 | disabled() { | |
294 | local param="${1}" | |
295 | ||
296 | list_match "${!param}" no off false 0 | |
297 | } | |
298 | ||
299 | mac_generate() { | |
300 | local b="$(random 12)" | |
301 | ||
302 | # Remove multicast bit | |
303 | # and set address is software assigned | |
304 | local first_byte=$(( 0x${b:0:2} & 0xfe )) | |
305 | first_byte=$(( ${first_byte} | 0x02 )) | |
306 | ||
307 | local output | |
308 | printf -v output "%02x" "${first_byte}" | |
309 | ||
310 | output="${output}:${b:2:2}:${b:4:2}:${b:6:2}:${b:8:2}:${b:10:2}" | |
311 | ||
312 | # Check if output is valid | |
313 | assert mac_is_valid "${output}" | |
314 | ||
315 | echo "${output}" | |
316 | } | |
317 | ||
318 | mac_format() { | |
319 | local mac=${1} | |
320 | assert isset mac | |
321 | ||
322 | # Remove all colons and make the rest lowercase. | |
323 | mac=${mac//:/} | |
324 | mac=${mac,,} | |
325 | ||
326 | local output | |
327 | if [ "${#mac}" = "12" ]; then | |
328 | # Add colons (:) to mac address | |
329 | output=${mac:0:2} | |
330 | local i | |
331 | for i in 2 4 6 8 10; do | |
332 | output="${output}:${mac:${i}:2}" | |
333 | done | |
334 | else | |
335 | output=${mac} | |
336 | fi | |
337 | ||
338 | assert mac_is_valid ${output} | |
339 | ||
340 | print "${output}" | |
341 | } | |
342 | ||
343 | mac_is_valid() { | |
344 | local mac=${1} | |
345 | ||
346 | [[ ${mac} =~ ^([0-9a-f]{2}\:){5}[0-9a-f]{2}$ ]] | |
347 | } | |
348 | ||
349 | # Converts the given string to lowercase and returns true if it is a valid FQDN | |
350 | fqdn_is_valid() { | |
351 | local fqdn="${1}" | |
352 | ||
353 | if grep -qP "^(?!:\/\/)(?=.{1,255}$)((.{1,63}\.){1,127}(?![0-9]*$)[a-z0-9-]+\.?)$" <<< "${fqdn,,}"; then | |
354 | return ${EXIT_TRUE} | |
355 | fi | |
356 | ||
357 | return ${EXIT_FALSE} | |
358 | } | |
359 | ||
360 | uuid() { | |
361 | echo $(</proc/sys/kernel/random/uuid) | |
362 | } | |
363 | ||
364 | abs() { | |
365 | local val=${1} | |
366 | ||
367 | if [ ${val} -lt 0 ]; then | |
368 | (( val *= -1 )) | |
369 | fi | |
370 | ||
371 | echo ${val} | |
372 | } | |
373 | ||
374 | rand() { | |
375 | local uuid="$(uuid)" | |
376 | echo "${uuid//-/}" | |
377 | } | |
378 | ||
379 | random() { | |
380 | local length="${1:-8}" | |
381 | ||
382 | local random | |
383 | while [ ${#random} -lt ${length} ]; do | |
384 | random="${random}$(rand)" | |
385 | done | |
386 | ||
387 | echo "${random:0:${length}}" | |
388 | } | |
389 | ||
390 | isset() { | |
391 | local var=${1} | |
392 | ||
393 | [ -n "${!var}" ] | |
394 | } | |
395 | ||
396 | isoneof() { | |
397 | local var=${!1} | |
398 | shift | |
399 | ||
400 | list_match "${var}" "$@" | |
401 | } | |
402 | ||
403 | isbool() { | |
404 | local var=${1} | |
405 | ||
406 | isoneof ${var} 0 1 no yes on off true false | |
407 | } | |
408 | ||
409 | isinteger() { | |
410 | local var=${!1} | |
411 | ||
412 | [[ ${var} =~ ^[0-9]+$ ]] | |
413 | } | |
414 | ||
415 | ismac() { | |
416 | local mac=${!1} | |
417 | ||
418 | mac_is_valid ${mac} | |
419 | } | |
420 | ||
421 | isipaddress() { | |
422 | local addr=${!1} | |
423 | ||
424 | ip_is_valid ${addr} | |
425 | } | |
426 | ||
427 | mtu_is_valid() { | |
428 | local proto=${1} | |
429 | local mtu=${2} | |
430 | ||
431 | case ${proto} in | |
432 | ethernet|ipv4) | |
433 | [ ${mtu} -ge 576 ] && [ ${mtu} -le 9000 ] | |
434 | ;; | |
435 | ipv6) | |
436 | [ ${mtu} -ge 1280 ] && [ ${mtu} -le 9000 ] | |
437 | ;; | |
438 | *) | |
439 | error "${proto} is not a valid proto" | |
440 | return ${EXIT_ERROR} | |
441 | ;; | |
442 | esac | |
443 | } | |
444 | ||
445 | backtrace() { | |
446 | local start=1 | |
447 | ||
448 | echo # Empty line | |
449 | error_log "Backtrace (most recent call in first line):" | |
450 | ||
451 | local i source | |
452 | for i in $(seq ${start} ${#BASH_SOURCE[*]}); do | |
453 | [ -z "${FUNCNAME[${i}]}" ] && continue | |
454 | ||
455 | # Print called binary with arguments. | |
456 | if [ "${FUNCNAME[${i}]}" == "main" ]; then | |
457 | local args="$(list_reverse ${BASH_ARGV[*]})" | |
458 | printf -v source "%20s" "$0" | |
459 | error_log " ${source} ${args}" | |
460 | continue | |
461 | fi | |
462 | ||
463 | source=${BASH_SOURCE[$(( ${i} + 1 ))]} | |
464 | error_log " $(printf "%20s" "'${FUNCNAME[${i}]}'") called from ${source:-<shell>}:${BASH_LINENO[${i}]}" | |
465 | done | |
466 | } | |
467 | ||
468 | assert() { | |
469 | local assertion="$@" | |
470 | ||
471 | if ! ${assertion}; then | |
472 | error_log "Assertion '${assertion}' failed." | |
473 | backtrace | |
474 | exit ${EXIT_ERROR_ASSERT} | |
475 | fi | |
476 | ||
477 | return ${EXIT_OK} | |
478 | } | |
479 | ||
480 | # This function checks, if the given argument is an assert error | |
481 | # exit code. If this is the case, the script will halt immediately. | |
482 | assert_check_retval() { | |
483 | local ret=${1} | |
484 | ||
485 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
486 | exit ${EXIT_ERROR_ASSERT} | |
487 | fi | |
488 | ||
489 | return ${ret} | |
490 | } | |
491 | ||
492 | # This function executes the given command and inverses the return code | |
493 | not() { | |
494 | local command="$@" | |
495 | ||
496 | ${command} && return ${EXIT_FALSE} || return ${EXIT_TRUE} | |
497 | } | |
498 | ||
499 | exec_cmd() { | |
500 | local cmd=$@ | |
501 | ||
502 | log DEBUG "Running command: ${cmd}" | |
503 | ||
504 | DEBUG=${DEBUG} \ | |
505 | LOG_DISABLE_STDOUT="${LOG_DISABLE_STDOUT}" \ | |
506 | LOG_FACILITY="${LOG_FACILITY}" \ | |
507 | ${SHELL} ${cmd} | |
508 | local ret=$? | |
509 | ||
510 | #log DEBUG "Returned with code '${ret}'" | |
511 | ||
512 | if [ ${ret} -eq ${EXIT_ERROR_ASSERT} ]; then | |
513 | error_log "Stopping parent process due to assertion error in child process: ${cmd}" | |
514 | exit ${EXIT_ERROR_ASSERT} | |
515 | fi | |
516 | ||
517 | return ${ret} | |
518 | } | |
519 | ||
520 | cmd() { | |
521 | local cmd=$@ | |
522 | ||
523 | log DEBUG "Running command: ${cmd}" | |
524 | ||
525 | env -i -- \ | |
526 | HOME="${HOME}" \ | |
527 | PATH="${PATH}" \ | |
528 | TERM="${TERM}" \ | |
529 | ${cmd} | |
530 | local ret=$? | |
531 | ||
532 | case "${ret}" in | |
533 | ${EXIT_OK}) | |
534 | return ${EXIT_OK} | |
535 | ;; | |
536 | *) | |
537 | log DEBUG "Returned with code '${ret}'" | |
538 | return ${ret} | |
539 | ;; | |
540 | esac | |
541 | } | |
542 | ||
543 | cmd_quiet() { | |
544 | cmd "$@" &>/dev/null | |
545 | } | |
546 | ||
547 | cmd_exec() { | |
548 | local cmd=( "$@" ) | |
549 | ||
550 | log DEBUG "Exec'ing command: ${cmd}" | |
551 | ||
552 | exec "${cmd[@]}" | |
553 | ||
554 | log ERROR "Could not exec-ute: ${cmd}" | |
555 | exit ${EXIT_ERROR} | |
556 | } | |
557 | ||
558 | cmd_not_implemented() { | |
559 | assert false "not implemented" | |
560 | } | |
561 | ||
562 | # Executes the given command in background | |
563 | cmd_background() { | |
564 | cmd_quiet "$@" & | |
565 | } | |
566 | ||
567 | # Prints the PID of the process that was started last | |
568 | cmd_background_get_pid() { | |
569 | print "${!}" | |
570 | } | |
571 | ||
572 | cmd_background_result() { | |
573 | local pids=$@ | |
574 | ||
575 | wait ${pids} | |
576 | } | |
577 | ||
578 | # Increase security of the read command | |
579 | read() { | |
580 | builtin read -r "$@" | |
581 | } | |
582 | ||
583 | seq() { | |
584 | if [ $# -eq 2 ]; then | |
585 | eval echo {${1}..${2}} | |
586 | elif [ $# -eq 3 ]; then | |
587 | eval echo {${1}..${3}..${2}} | |
588 | fi | |
589 | } | |
590 | ||
591 | range() { | |
592 | eval echo {0..$(( ${1} - 1 ))} | |
593 | } | |
594 | ||
595 | count() { | |
596 | local i=0 | |
597 | ||
598 | while read; do | |
599 | ((i++)) | |
600 | done | |
601 | ||
602 | echo ${i} | |
603 | } | |
604 | ||
605 | which() { | |
606 | type -P "$@" | |
607 | } | |
608 | ||
609 | # Prints the number of seconds since epoch. | |
610 | timestamp() { | |
611 | date -u "+%s" | |
612 | } | |
613 | ||
614 | beautify_time() { | |
615 | local value=${1} | |
616 | ||
617 | local unit | |
618 | local limit | |
619 | for unit in s m h d w; do | |
620 | case "${unit}" in | |
621 | s|m|h) | |
622 | limit=60 | |
623 | ;; | |
624 | d) | |
625 | limit=24 | |
626 | ;; | |
627 | w) | |
628 | limit=7 | |
629 | ;; | |
630 | esac | |
631 | ||
632 | [ ${value} -lt ${limit} ] && break | |
633 | ||
634 | value=$(( ${value} / ${limit} )) | |
635 | done | |
636 | ||
637 | echo "${value}${unit}" | |
638 | } | |
639 | ||
640 | beautify_bytes() { | |
641 | local value=${1} | |
642 | ||
643 | local unit | |
644 | local limit=1024 | |
645 | for unit in B k M G T; do | |
646 | [ ${value} -lt ${limit} ] && break | |
647 | value=$(( ${value} / ${limit} )) | |
648 | done | |
649 | ||
650 | echo "${value}${unit}" | |
651 | } | |
652 | ||
653 | module_load() { | |
654 | local module=${1} | |
655 | ||
656 | # Do nothing if the module is already loaded | |
657 | if [ -d "/sys/module/${module//-/_}" ]; then | |
658 | return ${EXIT_OK} | |
659 | fi | |
660 | ||
661 | log DEBUG "Loading kernel module ${module}" | |
662 | modprobe "${module}" | |
663 | } | |
664 | ||
665 | binary_exists() { | |
666 | local binary=${1} | |
667 | ||
668 | if [ -n "$(type -p ${binary})" ]; then | |
669 | return ${EXIT_OK} | |
670 | fi | |
671 | ||
672 | return ${EXIT_ERROR} | |
673 | } | |
674 | ||
675 | function_exists() { | |
676 | local function="${1}" | |
677 | ||
678 | if [ "$(type -t "${function}")" = "function" ]; then | |
679 | return ${EXIT_TRUE} | |
680 | fi | |
681 | ||
682 | return ${EXIT_FALSE} | |
683 | } | |
684 | ||
685 | process_kill() { | |
686 | local process=${1} | |
687 | ||
688 | if ! isinteger process; then | |
689 | process=$(pidof ${process}) | |
690 | fi | |
691 | ||
692 | local pid | |
693 | local sig | |
694 | for pid in ${process}; do | |
695 | for sig in 15 9; do | |
696 | [ -d "/proc/${pid}" ] || break | |
697 | ||
698 | kill -${sig} ${pid} | |
699 | sleep 1 | |
700 | done | |
701 | done | |
702 | } | |
703 | ||
704 | dec() { | |
705 | local hex=${1} | |
706 | ||
707 | if [ "${hex:0:2}" != "0x" ]; then | |
708 | hex="0x${hex}" | |
709 | fi | |
710 | ||
711 | printf "%d\n" "${hex}" | |
712 | } | |
713 | ||
714 | chr() { | |
715 | local char="${1}" | |
716 | ||
717 | [ ${char} -lt 256 ] || return ${EXIT_ERROR} | |
718 | ||
719 | printf "\\$(( ${char} / 64 * 100 + ${char} % 64 / 8 * 10 + ${char} % 8 ))\n" | |
720 | } | |
721 | ||
722 | ord() { | |
723 | LC_CTYPE="C" printf "%d\n" "'${1}" | |
724 | } | |
725 | ||
726 | hex() { | |
727 | printf "%X\n" "${1}" | |
728 | } | |
729 | ||
730 | network_is_running() { | |
731 | # Check, if the network service is running. | |
732 | service_is_active network | |
733 | } | |
734 | ||
735 | contains_spaces() { | |
736 | local var="$@" | |
737 | ||
738 | # Eliminate spaces. | |
739 | local var2=${var// /} | |
740 | ||
741 | if [ ${#var} -ne ${#var2} ]; then | |
742 | return ${EXIT_TRUE} | |
743 | fi | |
744 | ||
745 | return ${EXIT_FALSE} | |
746 | } | |
747 | ||
748 | contains_non_ascii_characters() { | |
749 | local value="$@" | |
750 | ||
751 | # Strip away all ASCII characters | |
752 | local non_ascii="${value//[[:ascii:]]/}" | |
753 | ||
754 | if isset non_ascii; then | |
755 | return ${EXIT_TRUE} | |
756 | fi | |
757 | ||
758 | return ${EXIT_FALSE} | |
759 | } | |
760 | ||
761 | string_match() { | |
762 | local match=${1} | |
763 | local string=${2} | |
764 | ||
765 | [[ ${string} =~ ${match} ]] && return ${EXIT_TRUE} || return ${EXIT_FALSE} | |
766 | } | |
767 | ||
768 | string_split() { | |
769 | local string="$@" | |
770 | ||
771 | local pos=0 | |
772 | while [ ${pos} -lt ${#string} ]; do | |
773 | print "${string:${pos}:1}" | |
774 | pos=$(( ${pos} + 1 )) | |
775 | done | |
776 | ||
777 | return ${EXIT_OK} | |
778 | } | |
779 | ||
780 | string_reverse() { | |
781 | local string="$@" | |
782 | ||
783 | local output | |
784 | local pos=0 | |
785 | while [ ${pos} -lt ${#string} ]; do | |
786 | output="${string:${pos}:1}${output}" | |
787 | pos=$(( ${pos} + 1 )) | |
788 | done | |
789 | ||
790 | print "${output}" | |
791 | return ${EXIT_OK} | |
792 | } | |
793 | ||
794 | dec2bin() { | |
795 | local number="${1}" | |
796 | ||
797 | local output | |
798 | ||
799 | local i div | |
800 | for i in 7 6 5 4 3 2 1; do | |
801 | div=$(( 2 ** ${i} )) | |
802 | ||
803 | if [ $(( ${number} / ${div} )) -eq 1 ]; then | |
804 | output="${output}1" | |
805 | else | |
806 | output="${output}0" | |
807 | fi | |
808 | number="$(( ${number} % ${div} ))" | |
809 | done | |
810 | ||
811 | if [ $(( ${number} % 2 )) -eq 1 ]; then | |
812 | output="${output}1" | |
813 | else | |
814 | output="${output}0" | |
815 | fi | |
816 | ||
817 | print "${output}" | |
818 | } | |
819 | ||
820 | bin2dec() { | |
821 | local string="${1}" | |
822 | local number=0 | |
823 | ||
824 | local pos=0 char | |
825 | while [ ${pos} -lt ${#string} ]; do | |
826 | char="${string:${pos}:1}" | |
827 | pos=$(( ${pos} + 1 )) | |
828 | ||
829 | number=$(( ${number} << 1 )) | |
830 | ||
831 | case "${char}" in | |
832 | 0) ;; | |
833 | 1) | |
834 | number=$(( ${number} + 1 )) | |
835 | ;; | |
836 | *) | |
837 | assert false "Invalid character: ${char}" | |
838 | ;; | |
839 | esac | |
840 | done | |
841 | ||
842 | print "${number}" | |
843 | return ${EXIT_OK} | |
844 | } | |
845 | ||
846 | char2bin() { | |
847 | local dec="$(ord "${1}")" | |
848 | ||
849 | dec2bin "${dec}" | |
850 | } | |
851 | ||
852 | bin2char() { | |
853 | local dec="$(bin2dec "$@")" | |
854 | ||
855 | chr "${dec}" | |
856 | } | |
857 | ||
858 | bin2hex() { | |
859 | local dec="$(bin2dec "$@")" | |
860 | ||
861 | dec2hex "${dec}" | |
862 | } | |
863 | ||
864 | hex2bin() { | |
865 | local dec="$(hex2dec "$@")" | |
866 | ||
867 | dec2bin "${dec}" | |
868 | } | |
869 | ||
870 | hex2dec() { | |
871 | local hex="${1}" | |
872 | ||
873 | # Prepend 0x if necessary. | |
874 | [ "${hex:0:2}" = "0x" ] || hex="0x${hex}" | |
875 | ||
876 | printf "%d\n" "${hex}" | |
877 | } | |
878 | ||
879 | dec2hex() { | |
880 | printf "%02x\n" "${1}" | |
881 | } | |
882 | ||
883 | # This function just copy config files | |
884 | copy() { | |
885 | assert [ $# -eq 2 ] | |
886 | ||
887 | local src=${1} | |
888 | local dst=${2} | |
889 | ||
890 | # Check if we can read from the source | |
891 | if [ ! -r "${src}" ]; then | |
892 | log ERROR "Cannot read ${src}" | |
893 | return ${EXIT_ERROR} | |
894 | fi | |
895 | ||
896 | # Check if ${dst} is a directory | |
897 | if [ -d "${dst}" ]; then | |
898 | log ERROR "${dst} is a directory" | |
899 | return ${EXIT_ERROR} | |
900 | fi | |
901 | ||
902 | # Create destination directory if it doesn't exist, yet | |
903 | if ! make_parent_directory "${dst}"; then | |
904 | return ${EXIT_ERROR} | |
905 | fi | |
906 | ||
907 | if ! fread "${src}" > "${dst}"; then | |
908 | log ERROR "Could not copy data from ${src} to ${dst}" | |
909 | return ${EXIT_ERROR} | |
910 | fi | |
911 | } | |
912 | ||
913 | normalize() { | |
914 | local string="$@" | |
915 | ||
916 | tr -sc [:alnum:] "-" < <(printf "%s" "${string,,}") | |
917 | } | |
918 | ||
919 | get_driver_from_path() { | |
920 | local path="${1}" | |
921 | ||
922 | if file_exists "${path}"; then | |
923 | # Resolve symlink | |
924 | local driver="$(readlink "${path}")" | |
925 | ||
926 | # Print module name | |
927 | basename "${driver}" | |
928 | return ${EXIT_OK} | |
929 | fi | |
930 | ||
931 | return ${EXIT_ERROR} | |
932 | } |