Permalink
Browse files

function: add a map() utility function

map() runs the first argument (a function) with each of the remaining
arguments as $1. The return value of map() is only 0 if all the calls
succeeded.

Signed-off-by: Dave Reisner <dreisner@archlinux.org>
  • Loading branch information...
1 parent 5c466b4 commit 7f1517275d22a7ce6bc8cece7ae87fb187b439ea @falconindy committed Oct 7, 2012
Showing with 16 additions and 27 deletions.
  1. +15 −24 functions
  2. +1 −3 mkinitcpio
View
@@ -172,6 +172,14 @@ die() {
cleanup 1
}
+map() {
+ local r=0
+ for _ in "${@:2}"; do
+ "$1" "$_" || r=1
+ done
+ return $r
+}
+
in_array() {
# Search for an element in an array.
# $1: needle
@@ -258,10 +266,7 @@ add_all_modules() {
local mod mods
mapfile -t mods < <(all_modules "$@")
-
- for mod in "${mods[@]}"; do
- add_module "$mod"
- done
+ map add_module "${mods[@]}"
return $(( !${#mods[*]} ))
}
@@ -279,9 +284,7 @@ add_checked_modules() {
mapfile -t mods < <(all_modules "$@")
fi
- for mod in "${mods[@]}"; do
- add_module "$mod"
- done
+ map add_module "${mods[@]}"
return $(( !${#mods[*]} ))
}
@@ -306,7 +309,7 @@ add_module() {
# discovered and added.
# $1: module name
- local module= path= dep= deps= field= value=
+ local module= path= deps= field= value=
local ign_errors=0
if [[ $1 = *\? ]]; then
@@ -326,9 +329,7 @@ add_module() {
;;
depends)
IFS=',' read -r -a deps <<< "$value"
- for dep in "${deps[@]}"; do
- add_module "$dep"
- done
+ map add_module "${deps[@]}"
;;
firmware)
if [[ -e /usr/lib/firmware/$value ]]; then
@@ -559,19 +560,9 @@ parse_hook() {
# prior to the start of hook processing, and after each hook's build
# function is run.
- local item=
-
- for item in $MODULES; do
- add_module "$item"
- done
-
- for item in $BINARIES; do
- add_binary "$item"
- done
-
- for item in $FILES; do
- add_file "$item"
- done
+ map add_module $MODULES
+ map add_binary $BINARIES
+ map add_file $FILES
if [[ $SCRIPT ]]; then
add_runscript "$SCRIPT"
View
@@ -415,9 +415,7 @@ if [[ -r $_d_kmoduledir/modules.builtin ]]; then
unset modname path
fi
-for hook in "${_hooks[@]}"; do
- run_build_hook "$hook" || (( ++_builderrors ))
-done
+map run_build_hook "${_hooks[@]}" || (( ++_builderrors ))
# process config file
parse_hook

0 comments on commit 7f15172

Please sign in to comment.