Skip to content

Commit 38c6726

Browse files
committed
* fix IFS restore in r_reverse_lines
* add filepath_contains_filepath function to check if a path contains another * add r_url_path_encode function that preserves forward slashes * improve PATH fallback in mulle-bash * add globals command to list MULLE_ variables * fix dir_list_files IFS restore * fix sed and grep escaping painfully for zsh
1 parent bc6cbd9 commit 38c6726

14 files changed

+895
-265
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
cmake_minimum_required (VERSION 3.13)
22

3-
project( mulle-bashfunctions VERSION 6.5.1 LANGUAGES NONE)
3+
project( mulle-bashfunctions VERSION 6.6.0 LANGUAGES NONE)
44

55

66
#

mulle-bash

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ EOF
125125
#
126126
# bash/zsh will use arg after -c <arg> as $0, convenient!
127127
#
128-
# MEMO: this used to be PATH="/bin:/usr/bin:${PATH}" but i can't
129-
# remember why, and it also tripped up our path search
130-
PATH="${PATH:-/bin:/usr/bin}" exec "${exe_shell}" -c ". ${script} --no-auto-shell ${args}" "${script}"
128+
# MEMO: this used to be PATH="/bin:/usr/bin:${PATH}" but this tripped up
129+
# our path search. It's now in the back as a fallback
130+
PATH="${PATH}:/usr/bin:/bin}" exec "${exe_shell}" -c ". ${script} --no-auto-shell ${args}" "${script}"
131131
fi
132132
else
133133
no_auto_shell='YES'
@@ -151,7 +151,7 @@ fi
151151
# this is "our" version
152152
# the actual loaded version may differ (and will change this variable)
153153
#
154-
MULLE_BASHFUNCTIONS_VERSION="6.5.1"
154+
MULLE_BASHFUNCTIONS_VERSION="6.6.0"
155155
MULLE_BASHFUNCTIONS_LIBEXEC_DIRNAME="libexec"
156156
MULLE_EXECUTABLE="$1"
157157

mulle-bashfunctions

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,25 @@ usage()
4646
Usage:
4747
${MULLE_EXECUTABLE_NAME} [command]
4848

49-
mulle-bashfunctions main purpose is to help load the library functions
50-
into an interactive shell. The library functions are almalgamated in a
51-
file called "mulle-bashfunctions.sh". There are variants though (none,
52-
minimal, all) with different sizes.
53-
54-
But mulle-bashfunctions can also
49+
Use ${MULLE_EXECUTABLE_NAME} to
5550

51+
* load the mulle-bashfunctions into an interactive shell
52+
* show documentation for the provided shell functions
5653
* embed "mulle-bashfunctions.sh" into another shell script
5754
* show the path to "mulle-bashfunctions.sh"
5855
* locate the "mulle-bashfunctions.sh" install path for a desired variant
59-
* show documentation for any of the defined functions
6056
* list the available libraries, which you can "include"
6157
* run an arbitrary mulle-bashfunction with eval and r-eval
58+
* get some information about the execution environment
6259

63-
Examples:
64-
Load "mulle-bashfunctions-all.sh" into the current interactive shell:
60+
Example:
61+
Load mulle-bashfunctions into the current interactive shell:
6562

6663
eval \`mulle-bashfunctions load all\`
64+
65+
Now try out an r_function, a function which returns a value in the global
66+
variable RVAL:
67+
6768
r_escaped_json '{ "url": "https://www.mulle-kybernetik.com" }'
6869
echo \$RVAL
6970

@@ -73,16 +74,19 @@ Commands:
7374
common-unames : list of known values for uname
7475
env : print environment needed for "mulle-bashfunctions.sh"
7576
eval <cmd> : evaluate cmd inside of mulle-bashfunctions
77+
globals [-a] : list global MULLE_ prefixed variables, -a for all globals
7678
functions : list defined functions
7779
hostname : show system hostname as used by mulle-bashfunctions
7880
libraries : list available libraries
79-
load [variant] : use eval \`mulle-bashfunctions load\` to load
81+
load [variant] : eval \`mulle-bashfunctions load <none|minimal|all>\`
8082
man <function> : show documention for function, if available
8183
new <name> : prints a mulle-bash shell script
8284
path [variant] : path to "mulle-bashfunctions.sh"
8385
r-eval <cmd> : evaluate cmd inside of mulle-bashfunctions and print RVAL
86+
shell : path of executing shell
8487
username : show username as used by mulle-bashfunctions
8588
uname : show system short name as used by mulle-bashfunctions
89+
uuid : generate a UUID
8690
version : print currently used version
8791
versions : list installed versions
8892

@@ -1236,6 +1240,15 @@ MULLE_UNAME=\"${MULLE_UNAME}\""
12361240
list_functions "$@" || exit 1
12371241
;;
12381242

1243+
globals)
1244+
if [ $# -ne 0 ]
1245+
then
1246+
declare -p | sed 's/^declare -[^ ]*[ ]*//p' | sort | sort -u
1247+
else
1248+
declare -p | sed -n 's/^declare -[^ ]* \(MULLE_[^=]*\)=\(.*\)$/\1=\2/p' | sort | sort -u
1249+
fi
1250+
;;
1251+
12391252
'hostname')
12401253
printf "%s\n" "${MULLE_HOSTNAME}"
12411254
;;

src/mulle-bashfunctions-all-embed.sh

Lines changed: 135 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,13 +1805,11 @@ function r_reverse_lines()
18051805
delim=""
18061806
RVAL=
18071807

1808-
IFS=$'\n'
1809-
while read -r line
1808+
while IFS=$'\n' read -r line
18101809
do
18111810
RVAL="${line}${delim}${RVAL}"
18121811
delim=$'\n'
18131812
done <<< "${lines}"
1814-
IFS="${DEFAULT_IFS}"
18151813
}
18161814

18171815

@@ -1871,54 +1869,111 @@ function r_escaped_grep_pattern()
18711869
{
18721870
local s="$1"
18731871

1874-
s="${s//\\/\\\\}"
1875-
s="${s//\[/\\[}"
1876-
s="${s//\]/\\]}"
1877-
s="${s//\$/\\$}"
1878-
s="${s//\*/\\*}"
1879-
s="${s//\./\\.}"
1880-
s="${s//\^/\\^}"
1881-
s="${s//\|/\\|}"
18821872

1883-
RVAL="$s"
1873+
if [ ${ZSH_VERSION+x} ]
1874+
then
1875+
local i
1876+
local c
1877+
1878+
RVAL=
1879+
for (( i=0; i < ${#s}; i++ ))
1880+
do
1881+
c="${s:$i:1}"
1882+
case "$c" in
1883+
$'\n'|$'\r'|$'\t'|$'\f'|"\\"|'['|']'|'$'|'*'|'.'|'^'|'|')
1884+
RVAL+="\\"
1885+
;;
1886+
esac
1887+
RVAL+="$c"
1888+
done
1889+
else
1890+
s="${s//\\/\\\\}"
1891+
s="${s//\[/\\[}"
1892+
s="${s//\]/\\]}"
1893+
s="${s//\$/\\$}"
1894+
s="${s//\*/\\*}"
1895+
s="${s//\./\\.}"
1896+
s="${s//\^/\\^}"
1897+
s="${s//\|/\\|}"
1898+
1899+
s="${s//$'\n'/\\$'\n'}"
1900+
s="${s//$'\t'/\\$'\t'}"
1901+
s="${s//$'\r'/\\$'\r'}"
1902+
s="${s//$'\f'/\\$'\f'}"
1903+
RVAL="$s"
1904+
fi
18841905
}
18851906

18861907

18871908
function r_escaped_sed_pattern()
18881909
{
18891910
local s="$1"
18901911

1891-
s="${s//\\/\\\\}"
1892-
s="${s//\[/\\[}"
1893-
s="${s//\]/\\]}"
1894-
s="${s//\//\\/}"
1895-
s="${s//\$/\\$}"
1896-
s="${s//\*/\\*}"
1897-
s="${s//\./\\.}"
1898-
s="${s//\^/\\^}"
1899-
s="${s//$'\n'/\\$'\n'}"
1900-
s="${s//$'\t'/\\$'\t'}"
1901-
s="${s//$'\r'/\\$'\r'}"
1902-
s="${s//$'\f'/\\$'\f'}"
1912+
if [ ${ZSH_VERSION+x} ]
1913+
then
1914+
local i
1915+
local c
19031916

1904-
RVAL="$s"
1917+
RVAL=
1918+
for (( i=0; i < ${#s}; i++ ))
1919+
do
1920+
c="${s:$i:1}"
1921+
case "$c" in
1922+
$'\n'|$'\r'|$'\t'|$'\f'|"\\"|'['|']'|'/'|'$'|'*'|'.'|'^')
1923+
RVAL+="\\"
1924+
;;
1925+
esac
1926+
RVAL+="$c"
1927+
done
1928+
else
1929+
s="${s//\\/\\\\}"
1930+
s="${s//\[/\\[}"
1931+
s="${s//\]/\\]}"
1932+
s="${s//\//\\/}"
1933+
s="${s//\$/\\$}"
1934+
s="${s//\*/\\*}"
1935+
s="${s//\./\\.}"
1936+
s="${s//\^/\\^}"
1937+
s="${s//$'\n'/\\$'\n'}"
1938+
s="${s//$'\t'/\\$'\t'}"
1939+
s="${s//$'\r'/\\$'\r'}"
1940+
s="${s//$'\f'/\\$'\f'}"
1941+
RVAL="$s"
1942+
fi
19051943
}
19061944

19071945

19081946
function r_escaped_sed_replacement()
19091947
{
19101948
local s="$1"
19111949

1912-
s="${s//\\/\\\\}" # escape backslashes first
1913-
s="${s//\//\\/}" # escape forward slashes
1914-
s="${s//\'/\'\\\'\'}" # escape single quotes by closing and reopening the quote
1915-
s="${s//&/\\&}" # escape ampersands
1916-
s="${s//$'\t'/\\t}" # escape tabs returns
1917-
s="${s//$'\r'/\\r}" # escape crlf returns
1918-
s="${s//$'\n'/\\n}" # escape newlines returns
1919-
s="${s//$'\f'/\\f}" # escape form feeds returns
1950+
if [ ${ZSH_VERSION+x} ]
1951+
then
1952+
local i
1953+
local c
19201954

1921-
RVAL="$s"
1955+
RVAL=
1956+
for (( i=0; i < ${#s}; i++ ))
1957+
do
1958+
c="${s:$i:1}"
1959+
case "$c" in
1960+
$'\n'|$'\r'|$'\t'|$'\f'|"\\"|'/'|'&')
1961+
RVAL+="\\"
1962+
;;
1963+
esac
1964+
RVAL+="$c"
1965+
done
1966+
else
1967+
s="${s//\\/\\\\}" # escape backslashes first
1968+
s="${s//\//\\/}" # escape forward slashes
1969+
s="${s//&/\\&}" # escape ampersands
1970+
1971+
s="${s//$'\n'/\\$'\n'}"
1972+
s="${s//$'\t'/\\$'\t'}"
1973+
s="${s//$'\r'/\\$'\r'}"
1974+
s="${s//$'\f'/\\$'\f'}"
1975+
RVAL="$s"
1976+
fi
19221977
}
19231978

19241979

@@ -3401,6 +3456,24 @@ function r_assert_sane_path()
34013456
esac
34023457
}
34033458

3459+
filepath_contains_filepath()
3460+
{
3461+
local string1="${1%/}" # Path to check, remove trailing slash
3462+
local string2="${2%/}" # Directory path, remove trailing slash
3463+
3464+
case "${string2}" in
3465+
${string1})
3466+
return 0
3467+
;;
3468+
${string1}/*)
3469+
return 0
3470+
;;
3471+
*)
3472+
return 1
3473+
;;
3474+
esac
3475+
}
3476+
34043477
fi
34053478
:
34063479
if ! [ ${MULLE_FILE_SH+x} ]
@@ -4297,7 +4370,7 @@ function dir_list_files()
42974370
-name "'${pattern:-*}'" \
42984371
${flags} \
42994372
-print | sort -n
4300-
IFS=' '$'\t'$'\n'
4373+
IFS="${DEFAULT_IFS}"
43014374
}
43024375

43034376

@@ -6119,6 +6192,33 @@ function r_url_encode()
61196192
}
61206193

61216194

6195+
function r_url_path_encode()
6196+
{
6197+
local s="$1"
6198+
6199+
local c
6200+
local safe
6201+
local encode
6202+
6203+
RVAL=
6204+
while :
6205+
do
6206+
safe="${s%%[^a-zA-Z0-9.~/_-]*}"
6207+
RVAL="${RVAL}${safe}"
6208+
s="${s#"${safe}"}"
6209+
if [ -z "${s}" ]
6210+
then
6211+
break
6212+
fi
6213+
6214+
c="${s:0:1}"
6215+
s="${s:1}"
6216+
printf -v encode '%%%02X' "'${c}'"
6217+
RVAL="${RVAL}${encode}"
6218+
done
6219+
}
6220+
6221+
61226222
function r_url_remove_scheme()
61236223
{
61246224
RVAL="${1#*:}"

0 commit comments

Comments
 (0)