-
Notifications
You must be signed in to change notification settings - Fork 0
/
_core
137 lines (123 loc) · 4.31 KB
/
_core
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#! /hint/sh
# i_was_not_set__check=
alias \
\
puts='printf %s\\n' \
command.='command >/dev/null -v' \
\
fori='for i in' fori{{='for i;{{' '{{=do' '}}=done' \
loop\{='while :; do' \
and='(exit $?)&&' or='(exit $?)||' \
goto_continue='while :;' \
goto_break='break;' \
\
\
print-error-trace='printf %s "warning: ${BASH_SOURCE+"(in file $BASH_SOURCE +$BASH_LINENO ) "}${FUNCNAME+"(in fn $FUNCNAME) "}"' \
expect-unseted-i='{ case ${i+x} in x) print-error-trace >&2; puts >&2 "i is defined, i=${i+"\'\''"}${i-(unseted)}${i+"\'\''"}"; esac; }' \
expect-seted-i='{ case ${i+x} in "") print-error-trace >&2; puts >&2 "i is unset, i=${i+"\'\''"}${i-(unseted)}${i+"\'\''"}"; esac; }' \
unset-unseted-i='{ expect-unseted-i; unset i; }' \
unset-seted-i='{ expect-seted-i; unset i; }' \
i{=unset-unseted-i \
}i=unset-seted-i \
i='print-error-trace >&2 ; arg "i called" >&2' \
ii='print-error-trace >&2 ; arg "ii called" >&2' \
I='print-error-trace >&2 ; arg "I called" >&2' \
;
# alternative to local command, (local is not POSIX)
_vars=':'
var_local() {
case $_vars: in *:$1:*)
printf %s\\n >&2 "atempted to local var that is already local: $1"
return 1
esac
eval "case \${${1}+x} in x) false; esac" || { # return 2 if var is defined, can not requre it as owned
printf %s\\n >&2 "atempted to local var that is already defined: $1"
return 3
}
_vars=${_vars%:}:$1:
}
var_unlocal() {
case $_vars in *:$1:*) ;; *)
printf %s\\n >&2 "atempted to unlocal var: $1"
return 1
esac
_vars=${_vars%%":$1:"*}:${_vars#*":$1:"}
eval "unset $1"
}
case $- in
-|'')
case $PS1 in
'') unset shell_is_interactive;;
*) shell_is_interactive='';;
esac
;;
*i*) shell_is_interactive='';;
*) unset shell_is_interactive;;
esac
# if ( i=_; : "${!i}" ) 2>/dev/null 1>/dev/null; then
# DONT: unset i inside of _toi fns. instead the top level fn/script should check if it is set.
case $sh_is in
bash|zsh) shuf_1_toi() { expect-unseted-i; i=$(( ( ${RANDOM:?} % $# ) + 1 )); i="${!i}"; unset shuf_1_ind; };;
*) shuf_1_toi() { expect-unseted-i; i=$(( ( ${RANDOM:-$$} % $# ) + 1 )); eval "i=\"\${${i}}\""; unset shuf_1_ind; };; # "$$" still got for 1 time usage
esac
# GNUtector detect if GNU coreutils options are supported
unset os_gnu os_an os_busybox os_toybox os_win
case ${OSTYPE:-$(uname -o)} in
linux-gnu|GNU/Linux) os_gnu='';; # check for GNU+Busybox is not important for now
linux-android|Android) os_an='';; # Termux? but it could be busybox toybox termux or any other shell app ..
MS/Windows) os_win='';;
linux-musl|Linux) # Alpine, KISS
unset-unseted-i
i=$(unalias ls 2>/dev/null; command -v ls)
case $i in # note: this will fail if ls is alias or fn
ls) os_bb='';; # applet is build in
*/ls) false;;
*)
puts >&2 \
"\$B/_core: got unexpected output" \
"\$B/_core: \$ command -v ls -> $i" \
"\$B/_core: \$?:$?" \
;
unset-seted-i
return 1
esac || { [ -L "$i" ] && os_bb=''; } || {
i=$(readlink -- "$i" 2>/dev/null)
case $?:$i in
0:*/busybox|busybox) os_bb='';;
0:*/toybox|toybox) os_toybox='';;
*)
puts >&2 \
"\$B/_core: well idk then what this OS this is, 'ls' seems to not be a symlink" \
"\$B/_core: \$ readlink -- \$(command -v ls) -> $i" \
"\$B/_core: \$?:$?" \
;
unset-seted-i
return 1
esac
}
unset-seted-i
;;
*) puts >&2 "\$B/_core: cannot detect current os ${OSTYPE:-$(uname -o)} in GNU || (Busybox+Musl) || Android || WINDOWS"; return 1;;
esac
return
#command_fn() { # never used , uncomment when needed
# # type "$1" 2>/dev/null | grep -qe function\$ # POSIX
# # [[ `type -- "$1" 2>/dev/null` = *\ function ]] # bashism
# # WARING: tested in (bash,zsh,dash,ksh,yash), test more?
#
# case "$(command -V -- "$1" 2>/dev/null | head -n 1)" in
# (*\ function) return 0;;
# (*) return 1;
# esac
#}
#
# shuf_1(){
# eval "printf %s\\n \"\${10}\""
# eval "printf %s\\n \"\${$(( ( ${RANDOM:-1$(\dd if=/dev/urandom count=3 2>&- | tr -d -c '0-9' | head -c 10)} % $# ) + 1 ))}\""
# shift "$(( ${RANDOM:-1$(\dd if=/dev/urandom count=8 bs=1000 2>&- | tr -d -c '0-9' | head -c 10)} % $# ))"
# printf %s\\n "$1"
# }
#bash shuf:
_tr_s(){ ( set -f; IFS=$1; set -- $(cat); IFS=$2; printf %s\\n "$*"; ); }
# command. env: || alias env:='IFS="${PATHseparator:-":"}" var_1l'
# alias alias--='alias 2>&- --' # was used only 1 and now no mire since `alias cd-='cd -' -='cd -'`