From 6be359769832ca853b0d3e7ea9d24ccf58e7fde6 Mon Sep 17 00:00:00 2001 From: Jephte CLAIN Date: Mon, 29 Sep 2014 21:00:07 +0400 Subject: [PATCH] maj des inclusions --- lib/ulib/support/install-pubkeys.sh | 509 +++++++++++++--------------- lib/ulib/ulibsh | 6 +- ucrontab | 509 +++++++++++++--------------- 3 files changed, 490 insertions(+), 534 deletions(-) diff --git a/lib/ulib/support/install-pubkeys.sh b/lib/ulib/support/install-pubkeys.sh index 00e13ed..7286a60 100755 --- a/lib/ulib/support/install-pubkeys.sh +++ b/lib/ulib/support/install-pubkeys.sh @@ -63,11 +63,15 @@ export TMPDIR="${TMPDIR:-${TMP:-${TEMP:-/tmp}}}" [ -f /etc/nutoolsrc ] && . /etc/nutoolsrc [ -f ~/.nutoolsrc ] && . ~/.nutoolsrc +true ##@inc]base.init ##@inc[base.core ## Fonctions de base: fondement uprovide base.core +function echo_() { + echo -n "$*" +} function recho() { if [[ "${1:0:2}" == -[eEn] ]]; then echo -n - @@ -94,18 +98,6 @@ function _qval() { s="${s//\`/\\\`}" recho_ "$s" } -function qval() { - echo -n \" - _qval "$@" - echo \" -} -function qvalr() { - if [ -n "$*" ]; then - echo -n \" - _qval "$@" - echo n \" - fi -} function should_quote() { [ -z "$1" ] && return 0 local s="${*//[a-zA-Z0-9]/}" @@ -118,6 +110,31 @@ function should_quote() { s="${s//=/}" [ -n "$s" ] } +function qval() { + echo -n \" + _qval "$@" + echo \" +} +function qvalm() { + if should_quote "$*"; then + echo -n \" + _qval "$@" + echo \" + else + recho "$*" + fi +} +function qvalr() { + if [ -z "$*" ]; then + : + elif should_quote "$*"; then + echo -n \" + _qval "$@" + echo \" + else + recho "$*" + fi +} function qvals() { local arg first=1 for arg in "$@"; do @@ -131,6 +148,7 @@ function qvals() { fi first= done + [ -z "$first" ] && echo } function qlines() { sed "s/'/'\\\\''/g; s/.*/'&'/g" @@ -147,6 +165,14 @@ function _setv() { local __s_var="$1"; shift eval "$__s_var=\"$(_qval "$*")\"" } +function echo_setv() { + local __s_var="$1"; shift + if [[ "$__s_var" == *=* ]]; then + set -- "${__s_var#*=}" "$@" + __s_var="${__s_var%%=*}" + fi + echo "$__s_var=$(qvalr "$*")" +} function setx() { if [ "$1" == -a ]; then shift @@ -277,12 +303,96 @@ function err2out() { uprovide base.string urequire base.core +function straddp() { + local p="$1"; shift + echo "$p$*" +} +function strdelp() { + local p="$1"; shift + local str="$*" + echo "${str#$p}" +} +function strdelp2() { + local p="$1"; shift + local str="$*" + echo "${str##$p}" +} +function stradds() { + local s="$1"; shift + echo "$*$s" +} +function strdels() { + local s="$1"; shift + local str="$*" + echo "${str%$s}" +} +function strdels2() { + local s="$1"; shift + local str="$*" + echo "${str%%$s}" +} function strlower() { - echo ${*,,} + local str="$*" + echo "${str,,}" +} +function strlower1() { + local str="$*" + echo "${str,}" +} +function strlowers() { + local str="$*" + echo "${*,}" } function strupper() { - echo ${*^^} + local str="$*" + echo "${str^^}" } +function strupper1() { + local str="$*" + echo "${str^}" +} +function struppers() { + echo "${*^}" +} +function strmid() { + local range="$1"; shift + local str="$*" + if [[ "$range" == *:-* ]]; then + local max=${#str} + [ $max -eq 0 ] && return + local start="${range%%:*}" + [ -n "$start" ] || start=0 + while [ "$start" -lt 0 ]; do + start=$(($max$start)) + done + max=$(($max-$start)) + local length="${range#*:}" + while [ "$length" -lt 0 ]; do + length=$(($max$length)) + done + range="$start:$length" + fi + eval 'echo "${str:'" $range"'}"' +} +function strrepl() { + local pattern="$1"; shift + local repl="$1"; shift + local str="$*" + local cmd='echo "${str/' + if [ "${pattern#/}" != "$pattern" ]; then + pattern="${pattern#/}" + cmd="$cmd/" + elif [ "${pattern#\#}" != "$pattern" ]; then + pattern="${pattern#\#}" + cmd="$cmd#" + elif [ "${pattern#%}" != "$pattern" ]; then + pattern="${pattern#%}" + cmd="$cmd%" + fi + cmd="$cmd"'$pattern/$repl}"' + eval "$cmd" +} + function isnum() { [ ${#1} -gt 0 ] || return 1 local v="$1" @@ -319,6 +429,40 @@ function is_no() { isnum "$1" && [ "$1" -eq 0 ] && return 0 return 1 } +function yesval() { + is_yes "$1" && echo 1 +} + +function first_char() { + local str="$*" + echo "${str:0:1}" +} +function last_char() { + local str="$*" + echo "${str: -1:1}" +} +function first_chars() { + local str="$*" + recho "${str:0:$((${#1}-1))}" +} +function last_chars() { + local str="$*" + recho "${str:1}" +} +function first_char_is() { + [ "${1:0:1}" == "$2" ] +} +function last_char_is() { + [ "${1:$((-1)):1}" == "$2" ] +} +function beginswith() { + local str="$1" pattern="$2" + eval '[ "${str#$pattern}" != "$str" ]' +} +function endswith() { + local str="$1" pattern="$2" + eval '[ "${str%$pattern}" != "$str" ]' +} ##@inc]base.string ##@inc[base.array ## Fonctions de base: gestion des tableaux @@ -328,43 +472,87 @@ urequire base.core ##@inc[base.compat ## Fonctions de base: support des fonctions obsolètes et des versions de bash < 4.x uprovide base.compat -urequire base.string + + +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"; } + +function quote_awk() { _qawk "$@"; } +function quoted_awk() { qawk "$@"; } +function quote_seds() { qseds "$@"; } +function quote_form() { _qform "$@"; } +function quoted_form() { qform "$@"; } + if [ -n "$BASH_VERSINFO" -a "${BASH_VERSINFO[0]}" -lt 4 ]; then - if uprovided base.string; then - function strlower() { tr A-Z a-z <<<"$*"; } - function strupper() { tr a-z A-Z <<<"$*"; } - 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 - 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 + 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[*]}" + } + 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 + 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 ##@inc]base.compat uprovide base -urequire base.init base.core base.string base.array base.compat +urequire base.init base.core base.string base.quote base.split base.array base.compat + +UNAME_SYSTEM=`uname -s` +[ "${UNAME_SYSTEM#CYGWIN}" != "$UNAME_SYSTEM" ] && UNAME_SYSTEM=Cygwin +[ "${UNAME_SYSTEM#MINGW32}" != "$UNAME_SYSTEM" ] && UNAME_SYSTEM=Mingw +UNAME_MACHINE=`uname -m` +if [ -n "$UTOOLS_CHROOT" ]; then + [ -n "$UTOOLS_UNAME_SYSTEM" ] && eval "UNAME_SYSTEM=$UTOOLS_UNAME_SYSTEM" + [ -n "$UTOOLS_UNAME_MACHINE" ] && eval "UNAME_MACHINE=$UTOOLS_UNAME_MACHINE" +fi -function yesval() { - is_yes "$1" && echo 1 -} function setyesval() { is_yes "$2" && set_var "$1" 1 || set_var "$1" "" } @@ -377,35 +565,6 @@ function normyesvals() { is_yes "${!__nyv_yesvar}" && set_var "$__nyv_yesvar" 1 || set_var "$__nyv_yesvar" "" done } -function rawecho() { - local first - while [ "${1:0:1}" == "-" ]; do - echo_ - - first="${1:1}"; shift - set -- "$first" "$@" - done - echo "$*" -} -function rawecho_() { - local first - while [ "${1:0:1}" == "-" ]; do - echo_ - - first="${1:1}"; shift - set -- "$first" "$@" - done - echo_ "$*" -} -function quote_arg() { - local s="$1" - s="${s//\\/\\\\}" - s="${s//\"/\\\"}" - s="${s//\$/\\\$}" - s="${s//\`/\\\`}" - rawecho "$s" -} -function quoted_arg() { - should_quote "$1" && echo "\"$(quote_arg "$1")\"" || quote_arg "$1" -} function quote_in() { sed 's/\\/\\\\/g s/"/\\"/g @@ -421,13 +580,6 @@ function quote_sarg() { function quoted_sarg() { echo "'$(quote_sarg "$1")'" } -function quoted_args() { - local a s - for a in "$@"; do - s="${s:+$s }$(quoted_arg "$a")" - done - rawecho "$s" -} function quoted_sargs() { local a s for a in "$@"; do @@ -435,172 +587,6 @@ function quoted_sargs() { done rawecho "$s" } -function quote_awk() { - local s="$1" - s="${s//\\/\\\\}" - s="${s//\"/\\\"}" - s="${s// -/\\n}" - rawecho "$s" -} -function quoted_awk() { - rawecho "\"$(quote_awk "$1")\"" -} -function quote_seds() { - local s="$1" - s="${s//\\/\\\\}" - s="${s//\//\\/}" - rawecho "$s" -} -function quote_form() { - local s="$1" - s="${s//\%/%25}" - s="${s//+/%2B}" - s="${s//&/%26}" - s="${s//=/%3D}" - s="${s// /+}" - rawecho "$s" -} -function quoted_form() { - local n="${1%%=*}" - if [ "$n" != "$1" ]; then - local v="${1#*=}" - rawecho "$(quote_form "$n")=$(quote_form "$v")" - else - quote_form "$1" - fi -} -function first_char() { - rawecho "${1:0:1}" -} -function last_char() { - rawecho "${1:$((-1)):1}" -} -function first_chars() { - rawecho "${1:0:$((${#1}-1))}" -} -function last_chars() { - rawecho "${1:1}" -} -function first_char_is() { - [ "${1:0:1}" == "$2" ] -} -function last_char_is() { - [ "${1:$((-1)):1}" == "$2" ] -} -function beginswith() { - eval '[ "${1#'"$(quote_arg "$2")"'}" != "$1" ]' -} -function endswith() { - eval '[ "${1%'"$(quote_arg "$2")"'}" != "$1" ]' -} -function splitfsep() { - if [[ "$1" == *"$2"* ]]; then - set_var "${3:-first}" "${1%%$2*}" - set_var "${4:-second}" "${1#*$2}" - else - set_var "${3:-first}" "$1" - set_var "${4:-second}" - fi -} -function splitfsep2() { - if [[ "$1" == *"$2"* ]]; then - set_var "${3:-first}" "${1%%$2*}" - set_var "${4:-second}" "${1#*$2}" - else - set_var "${3:-first}" - set_var "${4:-second}" "$1" - fi -} -function splitlsep() { - if [[ "$1" == *"$2"* ]]; then - set_var "${3:-first}" "${1%$2*}" - set_var "${4:-second}" "${1##*$2}" - else - set_var "${3:-first}" "$1" - set_var "${4:-second}" - fi -} -function splitlsep2() { - if [[ "$1" == *"$2"* ]]; then - set_var "${3:-first}" "${1%$2*}" - set_var "${4:-second}" "${1##*$2}" - else - set_var "${3:-first}" - set_var "${4:-second}" "$1" - fi -} -function splitvar() { - splitfsep "$1" = "${2:-name}" "${3:-value}" -} -function splitname() { - splitlsep "$1" . "${2:-basename}" "${3:-ext}" -} -function splithost() { - splitfsep "$1" . "${2:-hostname}" "${3:-domain}" -} -function splituserhost() { - splitfsep2 "$1" @ "${2:-user}" "${3:-host}" -} -function splitpair() { - splitfsep "$1" : "${2:-src}" "${3:-dest}" -} -function splitproxy() { - local __sp_tmp __sp_host __sp_port __sp_creds __sp_user __sp_password - - __sp_tmp="${1#http://}" - if [[ "$__sp_tmp" == *@* ]]; then - __sp_creds="${__sp_tmp%%@*}" - __sp_tmp="${__sp_tmp#${__sp_creds}@}" - splitpair "$__sp_creds" __sp_user __sp_password - fi - __sp_tmp="${__sp_tmp%%/*}" - splitpair "$__sp_tmp" __sp_host __sp_port - [ -n "$__sp_port" ] || __sp_port=3128 - - set_var "${2:-host}" "$__sp_host" - set_var "${3:-port}" "$__sp_port" - set_var "${4:-user}" "$__sp_user" - set_var "${5:-password}" "$__sp_password" -} -function spliturl() { - local __su_tmp __su_scheme __su_creds __su_user __su_password __su_host __su_port __su_path - - __su_scheme="${1%%:*}" - __su_tmp="${1#${__su_scheme}://}" - if [[ "$__su_tmp" == */* ]]; then - __su_path="${__su_tmp#*/}" - __su_tmp="${__su_tmp%%/*}" - fi - if [[ "$__su_tmp" == *@* ]]; then - __su_creds="${__su_tmp%%@*}" - __su_tmp="${__su_tmp#${__su_creds}@}" - splitpair "$__su_creds" __su_user __su_password - fi - splitpair "$__su_tmp" __su_host __su_port - if [ -z "$__su_port" ]; then - [ "$__su_scheme" == "http" ] && __su_port=80 - [ "$__su_scheme" == "https" ] && __su_port=443 - [ "$__su_scheme" == "ftp" ] && __su_port=21 - fi - - set_var "${2:-scheme}" "$__su_scheme" - set_var "${3:-user}" "$__su_user" - set_var "${4:-password}" "$__su_password" - set_var "${5:-host}" "$__su_host" - set_var "${6:-port}" "$__su_port" - set_var "${7:-path}" "$__su_path" -} - -function set_var_cmd() { - echo "$1=$(quoted_arg "$2")" -} -function set_var() { - eval "$(set_var_cmd "$@")" -} -function set_var_literal() { - eval "$1=$2" -} function set_array_cmd() { [ $# -eq 1 ] && set -- "$1" "$1" @@ -1840,6 +1826,7 @@ function array_new(dest) { function array_newsize(dest, size, i) { dest[0] = 0 # forcer awk à considérer dest comme un tableau delete dest + size = int(size) for (i = 1; i <= size; i++) { dest[i] = "" } @@ -1869,11 +1856,12 @@ function array_add(dest, value, lastindex) { dest[lastindex + 1] = value } function array_deli(dest, i, l) { + i = int(i) if (i == 0) return l = length(dest) while (i < l) { dest[i] = dest[i + 1] - i = i + 1 + i++ } delete dest[l] } @@ -1929,11 +1917,11 @@ function key_index(value, values, ignoreCase, i) { if (ignoreCase) { value = tolower(value) for (i in values) { - if (tolower(values[i]) == value) return i + if (tolower(values[i]) == value) return int(i) } } else { for (i in values) { - if (values[i] == value) return i + if (values[i] == value) return int(i) } } return 0 @@ -1948,7 +1936,7 @@ function array2s(values, prefix, sep, suffix, noindices, first, i, s) { if (first) first = 0 else s = s sep if (!noindices) s = s "[" i "]=" - s = values[i] + s = s values[i] } s = s suffix return s @@ -2515,7 +2503,7 @@ HELP_OPTIONS= function genparse() { local -a names descs vars options - local i desc var option name value shortopt + local i desc var option name uname value shortopt for var in "$@"; do if [[ "$var" == *=* ]]; then @@ -2549,10 +2537,11 @@ function genparse() { i=0 while [ $i -lt ${#descs[*]} ]; do name="${names[$i]}" + uname="$(strupper "$name")" desc="${descs[$i]}" echo -n " -\${HELP_${name^^}_OPTION:- $desc\${HELP_${name^^}_DESC:+ - \${HELP_${name^^}_DESC// +\${HELP_${uname}_OPTION:- $desc\${HELP_${uname}_DESC:+ + \${HELP_${uname}_DESC// / }}}" i=$(($i + 1)) @@ -2690,9 +2679,6 @@ function utools_local() { done } -function echo_() { - echo -n "$*" -} function isatty() { tty -s <&1 } @@ -3436,15 +3422,6 @@ function myhost() { function myhostname() { hostname -s 2>/dev/null || echo "$MYHOSTNAME" } - -UNAME_SYSTEM=`uname -s` -[ "${UNAME_SYSTEM#CYGWIN}" != "$UNAME_SYSTEM" ] && UNAME_SYSTEM=Cygwin -[ "${UNAME_SYSTEM#MINGW32}" != "$UNAME_SYSTEM" ] && UNAME_SYSTEM=Mingw -UNAME_MACHINE=`uname -m` -if [ -n "$UTOOLS_CHROOT" ]; then - [ -n "$UTOOLS_UNAME_SYSTEM" ] && eval "UNAME_SYSTEM=$UTOOLS_UNAME_SYSTEM" - [ -n "$UTOOLS_UNAME_MACHINE" ] && eval "UNAME_MACHINE=$UTOOLS_UNAME_MACHINE" -fi ##@inc]../base ##@inc[../sysinfos ## Gestion des informations sur l'hôte local diff --git a/lib/ulib/ulibsh b/lib/ulib/ulibsh index 65c3194..a21fe32 100644 --- a/lib/ulib/ulibsh +++ b/lib/ulib/ulibsh @@ -59,6 +59,8 @@ function uprovide() { ULIBPROVIDED=("${ULIBPROVIDED[@]}" "$1") } +__ULIB_FORCE_RELOAD= + function urequire() { local __u_module __u_ulibdir __u_found [ -n "$*" ] || set DEFAULTS @@ -67,7 +69,7 @@ function urequire() { for __u_ulibdir in "${ULIBDIRS[@]}"; do if [ -f "$__u_ulibdir/$__u_module" ]; then __u_found=1 - if ! uprovided "$__u_module"; then + if [ -n "$__ULIB_FORCE_RELOAD" ] || ! uprovided "$__u_module"; then uprovide "$__u_module" source "$__u_ulibdir/$__u_module" || die fi @@ -77,7 +79,7 @@ function urequire() { if [ -z "$__u_found" -a "$__u_module" == DEFAULTS ]; then __u_found=1 for __u_module in base pretty sysinfos compat; do - if ! uprovided "$__u_module"; then + if [ -n "$__ULIB_FORCE_RELOAD" ] || ! uprovided "$__u_module"; then uprovide "$__u_module" source "$__u_ulibdir/$__u_module" || die fi diff --git a/ucrontab b/ucrontab index b4ad673..5690866 100755 --- a/ucrontab +++ b/ucrontab @@ -151,11 +151,15 @@ export TMPDIR="${TMPDIR:-${TMP:-${TEMP:-/tmp}}}" [ -f /etc/nutoolsrc ] && . /etc/nutoolsrc [ -f ~/.nutoolsrc ] && . ~/.nutoolsrc +true ##@inc]base.init ##@inc[base.core ## Fonctions de base: fondement uprovide base.core +function echo_() { + echo -n "$*" +} function recho() { if [[ "${1:0:2}" == -[eEn] ]]; then echo -n - @@ -182,18 +186,6 @@ function _qval() { s="${s//\`/\\\`}" recho_ "$s" } -function qval() { - echo -n \" - _qval "$@" - echo \" -} -function qvalr() { - if [ -n "$*" ]; then - echo -n \" - _qval "$@" - echo n \" - fi -} function should_quote() { [ -z "$1" ] && return 0 local s="${*//[a-zA-Z0-9]/}" @@ -206,6 +198,31 @@ function should_quote() { s="${s//=/}" [ -n "$s" ] } +function qval() { + echo -n \" + _qval "$@" + echo \" +} +function qvalm() { + if should_quote "$*"; then + echo -n \" + _qval "$@" + echo \" + else + recho "$*" + fi +} +function qvalr() { + if [ -z "$*" ]; then + : + elif should_quote "$*"; then + echo -n \" + _qval "$@" + echo \" + else + recho "$*" + fi +} function qvals() { local arg first=1 for arg in "$@"; do @@ -219,6 +236,7 @@ function qvals() { fi first= done + [ -z "$first" ] && echo } function qlines() { sed "s/'/'\\\\''/g; s/.*/'&'/g" @@ -235,6 +253,14 @@ function _setv() { local __s_var="$1"; shift eval "$__s_var=\"$(_qval "$*")\"" } +function echo_setv() { + local __s_var="$1"; shift + if [[ "$__s_var" == *=* ]]; then + set -- "${__s_var#*=}" "$@" + __s_var="${__s_var%%=*}" + fi + echo "$__s_var=$(qvalr "$*")" +} function setx() { if [ "$1" == -a ]; then shift @@ -365,12 +391,96 @@ function err2out() { uprovide base.string urequire base.core +function straddp() { + local p="$1"; shift + echo "$p$*" +} +function strdelp() { + local p="$1"; shift + local str="$*" + echo "${str#$p}" +} +function strdelp2() { + local p="$1"; shift + local str="$*" + echo "${str##$p}" +} +function stradds() { + local s="$1"; shift + echo "$*$s" +} +function strdels() { + local s="$1"; shift + local str="$*" + echo "${str%$s}" +} +function strdels2() { + local s="$1"; shift + local str="$*" + echo "${str%%$s}" +} function strlower() { - echo ${*,,} + local str="$*" + echo "${str,,}" +} +function strlower1() { + local str="$*" + echo "${str,}" +} +function strlowers() { + local str="$*" + echo "${*,}" } function strupper() { - echo ${*^^} + local str="$*" + echo "${str^^}" } +function strupper1() { + local str="$*" + echo "${str^}" +} +function struppers() { + echo "${*^}" +} +function strmid() { + local range="$1"; shift + local str="$*" + if [[ "$range" == *:-* ]]; then + local max=${#str} + [ $max -eq 0 ] && return + local start="${range%%:*}" + [ -n "$start" ] || start=0 + while [ "$start" -lt 0 ]; do + start=$(($max$start)) + done + max=$(($max-$start)) + local length="${range#*:}" + while [ "$length" -lt 0 ]; do + length=$(($max$length)) + done + range="$start:$length" + fi + eval 'echo "${str:'" $range"'}"' +} +function strrepl() { + local pattern="$1"; shift + local repl="$1"; shift + local str="$*" + local cmd='echo "${str/' + if [ "${pattern#/}" != "$pattern" ]; then + pattern="${pattern#/}" + cmd="$cmd/" + elif [ "${pattern#\#}" != "$pattern" ]; then + pattern="${pattern#\#}" + cmd="$cmd#" + elif [ "${pattern#%}" != "$pattern" ]; then + pattern="${pattern#%}" + cmd="$cmd%" + fi + cmd="$cmd"'$pattern/$repl}"' + eval "$cmd" +} + function isnum() { [ ${#1} -gt 0 ] || return 1 local v="$1" @@ -407,6 +517,40 @@ function is_no() { isnum "$1" && [ "$1" -eq 0 ] && return 0 return 1 } +function yesval() { + is_yes "$1" && echo 1 +} + +function first_char() { + local str="$*" + echo "${str:0:1}" +} +function last_char() { + local str="$*" + echo "${str: -1:1}" +} +function first_chars() { + local str="$*" + recho "${str:0:$((${#1}-1))}" +} +function last_chars() { + local str="$*" + recho "${str:1}" +} +function first_char_is() { + [ "${1:0:1}" == "$2" ] +} +function last_char_is() { + [ "${1:$((-1)):1}" == "$2" ] +} +function beginswith() { + local str="$1" pattern="$2" + eval '[ "${str#$pattern}" != "$str" ]' +} +function endswith() { + local str="$1" pattern="$2" + eval '[ "${str%$pattern}" != "$str" ]' +} ##@inc]base.string ##@inc[base.array ## Fonctions de base: gestion des tableaux @@ -416,43 +560,87 @@ urequire base.core ##@inc[base.compat ## Fonctions de base: support des fonctions obsolètes et des versions de bash < 4.x uprovide base.compat -urequire base.string + + +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"; } + +function quote_awk() { _qawk "$@"; } +function quoted_awk() { qawk "$@"; } +function quote_seds() { qseds "$@"; } +function quote_form() { _qform "$@"; } +function quoted_form() { qform "$@"; } + if [ -n "$BASH_VERSINFO" -a "${BASH_VERSINFO[0]}" -lt 4 ]; then - if uprovided base.string; then - function strlower() { tr A-Z a-z <<<"$*"; } - function strupper() { tr a-z A-Z <<<"$*"; } - 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 - 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 + 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[*]}" + } + 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 + 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 ##@inc]base.compat uprovide base -urequire base.init base.core base.string base.array base.compat +urequire base.init base.core base.string base.quote base.split base.array base.compat + +UNAME_SYSTEM=`uname -s` +[ "${UNAME_SYSTEM#CYGWIN}" != "$UNAME_SYSTEM" ] && UNAME_SYSTEM=Cygwin +[ "${UNAME_SYSTEM#MINGW32}" != "$UNAME_SYSTEM" ] && UNAME_SYSTEM=Mingw +UNAME_MACHINE=`uname -m` +if [ -n "$UTOOLS_CHROOT" ]; then + [ -n "$UTOOLS_UNAME_SYSTEM" ] && eval "UNAME_SYSTEM=$UTOOLS_UNAME_SYSTEM" + [ -n "$UTOOLS_UNAME_MACHINE" ] && eval "UNAME_MACHINE=$UTOOLS_UNAME_MACHINE" +fi -function yesval() { - is_yes "$1" && echo 1 -} function setyesval() { is_yes "$2" && set_var "$1" 1 || set_var "$1" "" } @@ -465,35 +653,6 @@ function normyesvals() { is_yes "${!__nyv_yesvar}" && set_var "$__nyv_yesvar" 1 || set_var "$__nyv_yesvar" "" done } -function rawecho() { - local first - while [ "${1:0:1}" == "-" ]; do - echo_ - - first="${1:1}"; shift - set -- "$first" "$@" - done - echo "$*" -} -function rawecho_() { - local first - while [ "${1:0:1}" == "-" ]; do - echo_ - - first="${1:1}"; shift - set -- "$first" "$@" - done - echo_ "$*" -} -function quote_arg() { - local s="$1" - s="${s//\\/\\\\}" - s="${s//\"/\\\"}" - s="${s//\$/\\\$}" - s="${s//\`/\\\`}" - rawecho "$s" -} -function quoted_arg() { - should_quote "$1" && echo "\"$(quote_arg "$1")\"" || quote_arg "$1" -} function quote_in() { sed 's/\\/\\\\/g s/"/\\"/g @@ -509,13 +668,6 @@ function quote_sarg() { function quoted_sarg() { echo "'$(quote_sarg "$1")'" } -function quoted_args() { - local a s - for a in "$@"; do - s="${s:+$s }$(quoted_arg "$a")" - done - rawecho "$s" -} function quoted_sargs() { local a s for a in "$@"; do @@ -523,172 +675,6 @@ function quoted_sargs() { done rawecho "$s" } -function quote_awk() { - local s="$1" - s="${s//\\/\\\\}" - s="${s//\"/\\\"}" - s="${s// -/\\n}" - rawecho "$s" -} -function quoted_awk() { - rawecho "\"$(quote_awk "$1")\"" -} -function quote_seds() { - local s="$1" - s="${s//\\/\\\\}" - s="${s//\//\\/}" - rawecho "$s" -} -function quote_form() { - local s="$1" - s="${s//\%/%25}" - s="${s//+/%2B}" - s="${s//&/%26}" - s="${s//=/%3D}" - s="${s// /+}" - rawecho "$s" -} -function quoted_form() { - local n="${1%%=*}" - if [ "$n" != "$1" ]; then - local v="${1#*=}" - rawecho "$(quote_form "$n")=$(quote_form "$v")" - else - quote_form "$1" - fi -} -function first_char() { - rawecho "${1:0:1}" -} -function last_char() { - rawecho "${1:$((-1)):1}" -} -function first_chars() { - rawecho "${1:0:$((${#1}-1))}" -} -function last_chars() { - rawecho "${1:1}" -} -function first_char_is() { - [ "${1:0:1}" == "$2" ] -} -function last_char_is() { - [ "${1:$((-1)):1}" == "$2" ] -} -function beginswith() { - eval '[ "${1#'"$(quote_arg "$2")"'}" != "$1" ]' -} -function endswith() { - eval '[ "${1%'"$(quote_arg "$2")"'}" != "$1" ]' -} -function splitfsep() { - if [[ "$1" == *"$2"* ]]; then - set_var "${3:-first}" "${1%%$2*}" - set_var "${4:-second}" "${1#*$2}" - else - set_var "${3:-first}" "$1" - set_var "${4:-second}" - fi -} -function splitfsep2() { - if [[ "$1" == *"$2"* ]]; then - set_var "${3:-first}" "${1%%$2*}" - set_var "${4:-second}" "${1#*$2}" - else - set_var "${3:-first}" - set_var "${4:-second}" "$1" - fi -} -function splitlsep() { - if [[ "$1" == *"$2"* ]]; then - set_var "${3:-first}" "${1%$2*}" - set_var "${4:-second}" "${1##*$2}" - else - set_var "${3:-first}" "$1" - set_var "${4:-second}" - fi -} -function splitlsep2() { - if [[ "$1" == *"$2"* ]]; then - set_var "${3:-first}" "${1%$2*}" - set_var "${4:-second}" "${1##*$2}" - else - set_var "${3:-first}" - set_var "${4:-second}" "$1" - fi -} -function splitvar() { - splitfsep "$1" = "${2:-name}" "${3:-value}" -} -function splitname() { - splitlsep "$1" . "${2:-basename}" "${3:-ext}" -} -function splithost() { - splitfsep "$1" . "${2:-hostname}" "${3:-domain}" -} -function splituserhost() { - splitfsep2 "$1" @ "${2:-user}" "${3:-host}" -} -function splitpair() { - splitfsep "$1" : "${2:-src}" "${3:-dest}" -} -function splitproxy() { - local __sp_tmp __sp_host __sp_port __sp_creds __sp_user __sp_password - - __sp_tmp="${1#http://}" - if [[ "$__sp_tmp" == *@* ]]; then - __sp_creds="${__sp_tmp%%@*}" - __sp_tmp="${__sp_tmp#${__sp_creds}@}" - splitpair "$__sp_creds" __sp_user __sp_password - fi - __sp_tmp="${__sp_tmp%%/*}" - splitpair "$__sp_tmp" __sp_host __sp_port - [ -n "$__sp_port" ] || __sp_port=3128 - - set_var "${2:-host}" "$__sp_host" - set_var "${3:-port}" "$__sp_port" - set_var "${4:-user}" "$__sp_user" - set_var "${5:-password}" "$__sp_password" -} -function spliturl() { - local __su_tmp __su_scheme __su_creds __su_user __su_password __su_host __su_port __su_path - - __su_scheme="${1%%:*}" - __su_tmp="${1#${__su_scheme}://}" - if [[ "$__su_tmp" == */* ]]; then - __su_path="${__su_tmp#*/}" - __su_tmp="${__su_tmp%%/*}" - fi - if [[ "$__su_tmp" == *@* ]]; then - __su_creds="${__su_tmp%%@*}" - __su_tmp="${__su_tmp#${__su_creds}@}" - splitpair "$__su_creds" __su_user __su_password - fi - splitpair "$__su_tmp" __su_host __su_port - if [ -z "$__su_port" ]; then - [ "$__su_scheme" == "http" ] && __su_port=80 - [ "$__su_scheme" == "https" ] && __su_port=443 - [ "$__su_scheme" == "ftp" ] && __su_port=21 - fi - - set_var "${2:-scheme}" "$__su_scheme" - set_var "${3:-user}" "$__su_user" - set_var "${4:-password}" "$__su_password" - set_var "${5:-host}" "$__su_host" - set_var "${6:-port}" "$__su_port" - set_var "${7:-path}" "$__su_path" -} - -function set_var_cmd() { - echo "$1=$(quoted_arg "$2")" -} -function set_var() { - eval "$(set_var_cmd "$@")" -} -function set_var_literal() { - eval "$1=$2" -} function set_array_cmd() { [ $# -eq 1 ] && set -- "$1" "$1" @@ -1928,6 +1914,7 @@ function array_new(dest) { function array_newsize(dest, size, i) { dest[0] = 0 # forcer awk à considérer dest comme un tableau delete dest + size = int(size) for (i = 1; i <= size; i++) { dest[i] = "" } @@ -1957,11 +1944,12 @@ function array_add(dest, value, lastindex) { dest[lastindex + 1] = value } function array_deli(dest, i, l) { + i = int(i) if (i == 0) return l = length(dest) while (i < l) { dest[i] = dest[i + 1] - i = i + 1 + i++ } delete dest[l] } @@ -2017,11 +2005,11 @@ function key_index(value, values, ignoreCase, i) { if (ignoreCase) { value = tolower(value) for (i in values) { - if (tolower(values[i]) == value) return i + if (tolower(values[i]) == value) return int(i) } } else { for (i in values) { - if (values[i] == value) return i + if (values[i] == value) return int(i) } } return 0 @@ -2036,7 +2024,7 @@ function array2s(values, prefix, sep, suffix, noindices, first, i, s) { if (first) first = 0 else s = s sep if (!noindices) s = s "[" i "]=" - s = values[i] + s = s values[i] } s = s suffix return s @@ -2603,7 +2591,7 @@ HELP_OPTIONS= function genparse() { local -a names descs vars options - local i desc var option name value shortopt + local i desc var option name uname value shortopt for var in "$@"; do if [[ "$var" == *=* ]]; then @@ -2637,10 +2625,11 @@ function genparse() { i=0 while [ $i -lt ${#descs[*]} ]; do name="${names[$i]}" + uname="$(strupper "$name")" desc="${descs[$i]}" echo -n " -\${HELP_${name^^}_OPTION:- $desc\${HELP_${name^^}_DESC:+ - \${HELP_${name^^}_DESC// +\${HELP_${uname}_OPTION:- $desc\${HELP_${uname}_DESC:+ + \${HELP_${uname}_DESC// / }}}" i=$(($i + 1)) @@ -2778,9 +2767,6 @@ function utools_local() { done } -function echo_() { - echo -n "$*" -} function isatty() { tty -s <&1 } @@ -3524,15 +3510,6 @@ function myhost() { function myhostname() { hostname -s 2>/dev/null || echo "$MYHOSTNAME" } - -UNAME_SYSTEM=`uname -s` -[ "${UNAME_SYSTEM#CYGWIN}" != "$UNAME_SYSTEM" ] && UNAME_SYSTEM=Cygwin -[ "${UNAME_SYSTEM#MINGW32}" != "$UNAME_SYSTEM" ] && UNAME_SYSTEM=Mingw -UNAME_MACHINE=`uname -m` -if [ -n "$UTOOLS_CHROOT" ]; then - [ -n "$UTOOLS_UNAME_SYSTEM" ] && eval "UNAME_SYSTEM=$UTOOLS_UNAME_SYSTEM" - [ -n "$UTOOLS_UNAME_MACHINE" ] && eval "UNAME_MACHINE=$UTOOLS_UNAME_MACHINE" -fi ##@inc]base ##@inc[pretty ## Affichage en couleur, et support de niveaux de "verbosité" et d'interaction