-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathash.xlib
56 lines (53 loc) · 3.01 KB
/
ash.xlib
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
#!/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]
#
# option:
# --put [key] [value] put key value in map
# --append [key] [value] append value by key in map
# --incrby [key] [integer] increment by key (atomic integer)
# --get [key] print value by key
# --remove [key] remove value by key
# --size get map size
# --clear clear map
# --keys [[key's prefix]] list keys by po*l and key's pr*fix
# --2keys [[key's prefix]] list pool\*keys by po*l and key's pr*fix
__variable_map () {
[ "$1" ] || return 1;
__vmap_encode() {
[ "${1//[0-9A-Za-z_]/}" == "" ] && { printf %s "$1"; return 0; }
local idx=-1 args=; while [ $((idx+=2)) -le ${#1} ]; do args="$args '${1:$idx-1:1} '${1:$idx:1}"; done; printf x%x ${args%" '"}; # printf "${args//x/\\x}"
# printf d%d ${key%" '"}; # printf "`printf '\\\\x'%x ${key//d/ }`"
}
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}'`;;
--keys) printf `set | awk -F '_PREFIX_|=' '/^VARIABLE_MAP'${pool//x2a/".*"}'_PREFIX_'${key//x2a/".*"}'/{if($2 ~ /^(x[0-9a-f]{2})+$/) {printf gensub(/x/, "\\\\\\\x", "g", $2) "\\\n"} else {printf $2 "\\\n"}}'`;;
--2keys) printf `set | awk -F 'VARIABLE_MAP|_PREFIX_|=' '/^VARIABLE_MAP'${pool//x2a/".*"}'_PREFIX_'${key//x2a/".*"}'/{if($2 ~ /^(x[0-9a-f]{2})+$/) {printf gensub(/x/, "\\\\\\\x", "g", $2)} else {printf $2}; printf "*"; if($3 ~ /^(x[0-9a-f]{2})+$/) {printf gensub(/x/, "\\\\\\\x", "g", $3)} else {printf $3}; printf "\\\n"}'`;;
*) printf "%s\033[31m%s\033[0m%s\n" "[MAP:ERROR] invalid option: " "$1" >&2; return 1;;
esac
}