From 4124f35044b810719c6aae8d3ca148f927482e46 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Sun, 16 Apr 2017 02:18:51 +0300 Subject: bugfix & code style --- %HOME%/.bash_logout | 2 +- %HOME%/.bash_utils/cxx.sh | 14 +++++-- %HOME%/.bash_utils/distr.sh | 3 ++ %HOME%/.bash_utils/file.sh | 4 +- %HOME%/.bash_utils/text.sh | 89 +++++++++++++++++++++++++-------------------- 5 files changed, 65 insertions(+), 47 deletions(-) diff --git a/%HOME%/.bash_logout b/%HOME%/.bash_logout index 9bf0c89..9113117 100644 --- a/%HOME%/.bash_logout +++ b/%HOME%/.bash_logout @@ -1 +1 @@ -type cls > /dev/null 2>&1 && cls +type cls &> /dev/null && cls diff --git a/%HOME%/.bash_utils/cxx.sh b/%HOME%/.bash_utils/cxx.sh index 4e332e7..633e154 100644 --- a/%HOME%/.bash_utils/cxx.sh +++ b/%HOME%/.bash_utils/cxx.sh @@ -25,12 +25,15 @@ runcxx_compiler=g++ _runc_usage() ( set -o errexit -o nounset -o pipefail + local prefix="${FUNCNAME[0]}" + [ "${#FUNCNAME[@]}" -gt 1 ] && prefix="${FUNCNAME[1]}" + local msg for msg; do - echo "${FUNCNAME[1]}: $msg" + echo "$prefix: $msg" done - echo "usage: ${FUNCNAME[1]} [-h|--help] [-c|--comp-arg ARG]... C_PATH... [-- [PROG_ARG]...]" + echo "usage: $prefix [-h|--help] [-c|--comp-arg ARG]... C_PATH... [-- [PROG_ARG]...]" ) runc() ( @@ -103,12 +106,15 @@ runc() ( _runcxx_usage() ( set -o errexit -o nounset -o pipefail + local prefix="${FUNCNAME[0]}" + [ "${#FUNCNAME[@]}" -gt 1 ] && prefix="${FUNCNAME[1]}" + local msg for msg; do - echo "${FUNCNAME[1]}: $msg" + echo "$prefix: $msg" done - echo "usage: ${FUNCNAME[1]} [-h|--help] [-c|--comp-arg ARG]... CPP_PATH... [-- [PROG_ARG]...]" + echo "usage: $prefix [-h|--help] [-c|--comp-arg ARG]... CPP_PATH... [-- [PROG_ARG]...]" ) runcxx() ( diff --git a/%HOME%/.bash_utils/distr.sh b/%HOME%/.bash_utils/distr.sh index ff32c0a..810783e 100644 --- a/%HOME%/.bash_utils/distr.sh +++ b/%HOME%/.bash_utils/distr.sh @@ -9,10 +9,13 @@ sums_path='sha1sums.txt' sums_name="$( basename -- "$sums_path" )" sums_unescape_path() { + set -o errexit -o nounset -o pipefail + if [ "$#" -ne 1 ]; then echo "usage: ${FUNCNAME[0]} PATH" >&2 return 1 fi + local path="$1" path="${path//'\\'/$'\\'}" path="${path//'\n'/$'\n'}" diff --git a/%HOME%/.bash_utils/file.sh b/%HOME%/.bash_utils/file.sh index 6034713..6df20d0 100644 --- a/%HOME%/.bash_utils/file.sh +++ b/%HOME%/.bash_utils/file.sh @@ -17,12 +17,12 @@ swap_files() ( local path2="$2" if [ ! -r "$path1" ] || [ ! -w "$path1" ]; then - echo "${FUNCNAME[0]}: must be a regular file: $path1" >&2 + echo "${FUNCNAME[0]}: must be readable and writable: $path1" >&2 return 1 fi if [ ! -r "$path2" ] || [ ! -w "$path2" ]; then - echo "${FUNCNAME[0]}: must be a regular file: $path2" >&2 + echo "${FUNCNAME[0]}: must be readable and writable: $path2" >&2 return 1 fi diff --git a/%HOME%/.bash_utils/text.sh b/%HOME%/.bash_utils/text.sh index 0f51b4a..be197e1 100644 --- a/%HOME%/.bash_utils/text.sh +++ b/%HOME%/.bash_utils/text.sh @@ -26,37 +26,43 @@ doslint() { trim "$@" && trimdoseol "$@" && doseol "$@" } -escape_pattern_sed() ( +_sed_escape_pattern() ( set -o errexit -o nounset -o pipefail + if [ "$#" -ne 1 ]; then + echo "usage: ${FUNCNAME[0]} STR" >&2 + return 1 + fi + # Only $^*./\[] need to be escaped according to this: # https://unix.stackexchange.com/a/33005/60124 - local pattern - for pattern; do - pattern="${pattern//'\'/'\\'}" - pattern="${pattern//'/'/'\/'}" - pattern="${pattern//'$'/'\$'}" - pattern="${pattern//'^'/'\^'}" - pattern="${pattern//'*'/'\*'}" - pattern="${pattern//'.'/'\.'}" - pattern="${pattern//'['/'\['}" - pattern="${pattern//']'/'\]'}" - pattern="${pattern//$'\n'/'\n'}" - echo "$pattern" - done + local s="$1" + s="${s//'\'/'\\'}" + s="${s//'/'/'\/'}" + s="${s//'$'/'\$'}" + s="${s//'^'/'\^'}" + s="${s//'*'/'\*'}" + s="${s//'.'/'\.'}" + s="${s//'['/'\['}" + s="${s//']'/'\]'}" + s="${s//$'\n'/'\n'}" + echo "$s" ) -escape_substitution_sed() ( +_sed_escape_substitution() ( set -o errexit -o nounset -o pipefail - local pattern - for pattern; do - pattern="${pattern//'\'/'\\'}" - pattern="${pattern//'/'/'\/'}" - pattern="${pattern//'&'/'\&'}" - pattern="${pattern//$'\n'/'\n'}" - echo "$pattern" - done + if [ "$#" -ne 1 ]; then + echo "usage: ${FUNCNAME[0]} STR" >&2 + return 1 + fi + + local s="$1" + s="${s//'\'/'\\'}" + s="${s//'/'/'\/'}" + s="${s//'&'/'\&'}" + s="${s//$'\n'/'\n'}" + echo "$s" ) str_replace() ( @@ -68,10 +74,10 @@ str_replace() ( fi local old="$1" - old="$( escape_pattern_sed "$old" )" + old="$( _sed_escape_pattern "$old" )" shift local new="$1" - new="$( escape_substitution_sed "$new" )" + new="$( _sed_escape_substitution "$new" )" shift sed --binary --in-place -- "s/$old/$new/g" "$@" @@ -86,10 +92,10 @@ str_replace_word() ( fi local old="$1" - old="$( escape_pattern_sed "$old" )" + old="$( _sed_escape_pattern "$old" )" shift local new="$1" - new="$( escape_substitution_sed "$new" )" + new="$( _sed_escape_substitution "$new" )" shift sed --binary --in-place -- "s/\\b$old\\b/$new/g" "$@" @@ -113,20 +119,23 @@ str_toupper() ( done ) -escape_pattern_bash() ( +_bash_escape_pattern() ( set -o errexit -o nounset -o pipefail + if [ "$#" -ne 1 ]; then + echo "usage: ${FUNCNAME[0]} STR" >&2 + return 1 + fi + # Only *?\[] need to be escaped according to: # http://wiki.bash-hackers.org/syntax/pattern#normal_pattern_language - local pattern - for pattern; do - pattern="${pattern//'\'/'\\'}" - pattern="${pattern//'*'/'\*'}" - pattern="${pattern//'?'/'\?'}" - pattern="${pattern//'['/'\['}" - pattern="${pattern//']'/'\]'}" - echo "$pattern" - done + local s="$1" + s="${s//'\'/'\\'}" + s="${s//'*'/'\*'}" + s="${s//'?'/'\?'}" + s="${s//'['/'\['}" + s="${s//']'/'\]'}" + echo "$s" ) str_contains() ( @@ -141,7 +150,7 @@ str_contains() ( local sub="$2" [ -z "$sub" ] && return 0 - sub="$( escape_pattern_bash "$sub" )" + sub="$( _bash_escape_pattern "$sub" )" test "$str" != "${str#*$sub}" ) @@ -158,7 +167,7 @@ str_starts_with() ( local sub="$2" [ -z "$sub" ] && return 0 - sub="$( escape_pattern_bash "$sub" )" + sub="$( _bash_escape_pattern "$sub" )" test "$str" != "${str#$sub}" ) @@ -175,7 +184,7 @@ str_ends_with() ( local sub="$2" [ -z "$sub" ] && return 0 - sub="$( escape_pattern_bash "$sub" )" + sub="$( _bash_escape_pattern "$sub" )" test "$str" != "${str%$sub}" ) -- cgit v1.2.3