nutools/lib/ulib/base.compat

90 lines
2.5 KiB
Bash

##@cooked comments # -*- coding: utf-8 mode: sh -*- vim:sw=4:sts=4:et:ai:si:sta:fenc=utf-8
## Fonctions de base: support des fonctions obsolètes et des versions de bash < 4.x
##@cooked nocomments
##@include base.core
##@include base.quote
##@include base.string
##@include base.bool
uprovide base.compat
## Fonctions obsolètes
# base.core
urequire base.core
function setx2() { setxx "$@"; }
function rawecho() { recho "$@"; }
function rawecho_() { recho_ "$@"; }
function quote_arg() { _qval "$@"; }
function quoted_arg() { qvalm "$@"; }
function quoted_args() { qvals "$@"; }
function set_var() { setv "$@"; }
function set_var_cmd() { echo_setv "$@"; }
function set_var_literal() { eval "$1=$2"; }
# base.quote
urequire base.quote
function quote_awk() { _qawk "$@"; }
function quoted_awk() { qawk "$@"; }
function quote_seds() { qseds "$@"; }
function quote_form() { _qform "$@"; }
function quoted_form() { qform "$@"; }
## Compatibilité avec bash >=2.x
if [ -n "$BASH_VERSINFO" -a "${BASH_VERSINFO[0]}" -lt 4 ]; then
# base.string
urequire base.string
function strlower() { tr A-Z a-z <<<"$*"; }
function strlower1() {
local str="$*"
local h="${str:0:1}" r="${str:1}"
echo "$(tr A-Z a-z <<<"$h")$r"
}
function strlowers() {
local -a vs; local v
for v in "$@"; do
vs=("${vs[@]}" "$(strlower1 "$v")")
done
echo "${vs[*]}"
}
function strupper() { tr a-z A-Z <<<"$*"; }
function strupper1() {
local str="$*"
local h="${str:0:1}" r="${str:1}"
echo "$(tr a-z A-Z <<<"$h")$r"
}
function struppers() {
local -a vs; local v
for v in "$@"; do
vs=("${vs[@]}" "$(strupper1 "$v")")
done
echo "${vs[*]}"
}
# base.bool
urequire base.bool
function is_yes() {
case "$1" in
o|oui|y|yes|v|vrai|t|true|on) return 0;;
O|OUI|Y|YES|V|VRAI|T|TRUE|ON) return 0;;
esac
isnum "$1" && [ "$1" -ne 0 ] && return 0
# pour les perfs, n'utiliser strlower que ici
case "$(strlower "$1")" in
o|oui|y|yes|v|vrai|t|true|on) return 0;;
esac
return 1
}
function is_no() {
case "$1" in
n|non|no|f|faux|false|off) return 0;;
N|NON|NO|F|FAUX|FALSE|OFF) return 0;;
esac
isnum "$1" && [ "$1" -eq 0 ] && return 0
case "$(strlower "$1")" in
n|non|no|f|faux|false|off) return 0;;
esac
return 1
}
fi