diff options
author | 2013-09-04 15:11:06 +0200 | |
---|---|---|
committer | 2013-09-04 15:12:09 +0200 | |
commit | b13760493c3cc09fa9c466eb2b0832020a59cd4f (patch) | |
tree | 1bd79f6764082f295e362253366b9daeee962f3e /files | |
parent | git-commit-overlay: reinit git repo, use config vars (diff) | |
download | R_overlay-b13760493c3cc09fa9c466eb2b0832020a59cd4f.tar.gz R_overlay-b13760493c3cc09fa9c466eb2b0832020a59cd4f.tar.bz2 R_overlay-b13760493c3cc09fa9c466eb2b0832020a59cd4f.zip |
config/: add hookrc file, files/shlib: itertools
* hook-related configuration
* files/shlib/itertools: provides line_iterator(), which is required to read
${GIT_REMOTES}
Diffstat (limited to 'files')
-rw-r--r-- | files/shlib/itertools.sh | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/files/shlib/itertools.sh b/files/shlib/itertools.sh new file mode 100644 index 0000000..86d7697 --- /dev/null +++ b/files/shlib/itertools.sh @@ -0,0 +1,72 @@ +#!/bin/sh +# -*- coding: utf-8 -*- +# R overlay -- shell functions, list iterators +# Copyright (C) 2013 André Erdmann <dywi@mailerd.de> +# Distributed under the terms of the GNU General Public License; +# either version 2 of the License, or (at your option) any later version. +# +# +# --- functions provided by this file --- +# +# void generic_iterator ( +# item_separator, *words, +# **F_ITER=echo, **ITER_SKIP_EMPTY=y, **ITER_UNPACK_ITEM=n, +# **F_ITER_ON_ERROR=return +# ) +# @iterator line_iterator ( *lines ) +# +# --- variables provided by this file --- +# +# @private __HAVE_ITERTOOLS_FUNCTIONS__ +# +# --- END HEADER --- + +if [ -z "${__HAVE_ITERTOOLS_FUNCTIONS__-}" ]; then +readonly __HAVE_ITERTOOLS_FUNCTIONS__=y + + +# void generic_iterator ( +# item_separator, *words, +# **F_ITER=echo, **ITER_SKIP_EMPTY=y, **ITER_UNPACK_ITEM=n, +# **F_ITER_ON_ERROR=return +# ) +# DEFINES @iterator <item_separator> <iterator_name> +# +# Iterates over a list of items separated by item_separator. +# All words are interpreted as "one big list". +# +# Calls F_ITER ( item ) for each item and F_ITER_ON_ERROR() if F_ITER +# returns a non-zero value. +# The items will be unpacked if ITER_UNPACK_ITEM is set to 'y', +# otherwise the item is interpreted as one word (default 'n'). +# +# Empty items will be ignored if ITER_SKIP_EMPTY is set to 'y', which +# is the default behavior. +# +# Examples: see the specific iterator function(s) below. +# +generic_iterator() { + : ${IFS_DEFAULT?} + local IFS="${1?}" + shift + set -- $* + IFS="${IFS_DEFAULT}" + local item + for item; do + if [ -z "${item}" ] && [ "${ITER_SKIP_EMPTY:-y}" = "y" ]; then + true + elif [ "${ITER_UNPACK_ITEM:-n}" = "y" ]; then + ${F_ITER:-echo} ${item} || ${F_ITER_ON_ERROR:-return} + else + ${F_ITER:-echo} "${item}" || ${F_ITER_ON_ERROR:-return} + fi + done + return 0 +} + +# @iterator <newline> line_iterator +line_iterator() { + generic_iterator "${IFS_NEWLINE?}" "$@" +} + +fi # __HAVE_ITERTOOLS_FUNCTIONS__ |