Repository navigation
Expand file tree
/
Copy pathash.xlib
More file actions
70 lines (67 loc) · 3.61 KB
/
Copy pathash.xlib
File metadata and controls
70 lines (67 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/busybox ash
# Copyright 2021 bin jin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Variable map.
# env:
# VARIABLE_MAP*_PREFIX_*
#
# Usage: __variable_map [[pool]] [option] [arguments]
#
# Options:
# --put [key] [value] put the key and value in the map.
# --append [key] [value] append the value to the value for KEY in the map.
# --incrby [key] [integer] increment the value for KEY by INTEGER (atomic).
# --get [key] print the value for KEY.
# --remove [key] remove the value for KEY.
# --size print the map size.
# --clear clear the map.
# --keys [[key's prefix]] list keys filtered by pool and key prefix.
# --2keys [[key's prefix]] list pool*key pairs filtered by pool and key prefix.
__variable_map () {
[ "$1" ] || return 1;
__vmap_encode() {
[ "${1//[0-9A-Za-z_]/}" == "" ] && { printf %s "$1"; return 0; }
# One argument per character; an unquoted expansion drops the space key.
local s=$1 idx=0; set --;
while [ $idx -lt ${#s} ]; do set -- "$@" "'${s:$idx:1}"; idx=$((idx+1)); done;
printf x%02x "$@"
}
local key pool value;
[ "${1:0:2}" == "--" ] || { pool=`__vmap_encode "$1"`; shift; };
key=`__vmap_encode "$2"` value="$3";
case $1 in
--put) [ "$value" ] && eval VARIABLE_MAP${pool}_PREFIX_$key=\"\$value\";;
--incrby) [ "$value" ] && eval ": \$((VARIABLE_MAP${pool}_PREFIX_$key += $value))";;
--append) [ "$value" ] && eval VARIABLE_MAP${pool}_PREFIX_$key=\"\$VARIABLE_MAP${pool}_PREFIX_$key\$value\";;
--get) [ "$key" ] && eval printf %s \"\$VARIABLE_MAP${pool}_PREFIX_$key\";;
--remove) [ "$key" ] && unset VARIABLE_MAP${pool}_PREFIX_$key;;
--size) printf %d $(set | grep -c '^VARIABLE_MAP'${pool}_PREFIX_);;
--clear) unset `set | awk -F '=' '/^VARIABLE_MAP'${pool}'_PREFIX_/{printf " " $1}'`;;
# WARNING: a backquote eats a backslash before the command is parsed, so
# [$(...)] is required here; decode the [xHH] pairs with 'printf %b'.
--keys)
local k;
for k in $(set | awk -F '_PREFIX_|=' '/^VARIABLE_MAP'${pool//x2a/".*"}'_PREFIX_'${key//x2a/".*"}'/{if ($2 ~ /^(x[0-9a-f]{2})+$/) {s=$2; o=""; while (s != "") {o = o "\\" "x" substr(s, 2, 2); s = substr(s, 4)}; printf "%s\n", o} else {printf "%s\n", $2}}'); do
printf '%b\n' "$k"
done
;;
--2keys)
local k;
for k in $(set | awk -F 'VARIABLE_MAP|_PREFIX_|=' '/^VARIABLE_MAP'${pool//x2a/".*"}'_PREFIX_'${key//x2a/".*"}'/{if ($2 ~ /^(x[0-9a-f]{2})+$/) {s=$2; o=""; while (s != "") {o = o "\\" "x" substr(s, 2, 2); s = substr(s, 4)}; printf "%s", o} else {printf "%s", $2}; printf "*"; if ($3 ~ /^(x[0-9a-f]{2})+$/) {s=$3; o=""; while (s != "") {o = o "\\" "x" substr(s, 2, 2); s = substr(s, 4)}; printf "%s", o} else {printf "%s", $3}; printf "\n"}'); do
printf '%b\n' "$k"
done
;;
*) printf "%s\033[31m%s\033[0m%s\n" "[MAP:ERROR] invalid option: " "$1" >&2; return 1;;
esac
}