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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
#!/bin/sh
# vim: set filetype=sh :
# file: /usr/share/shellia/ia.basic
# copyright: Bernd Schumacher <bernd.schumacher@hpe.com> (2007-2021)
# license: GNU General Public License, version 3
# description: ia.basic - basic functions needed by shellia
# ia_find_lib <lib>
# example:
# program "/nfs/host1/usr/local/bin/program" with line "ia_source_lib /usr/share/shellia/ia" called
# from directory "/tmp" will try "/tmp/ia", ... "/nfs/host1/usr/share/shellia/ia", ... and
# "/usr/share/shellia/ia" in the listed order.
ia_find_lib()
{
local i
local p
for i in "$@"; do
[ -f "./$(basename "$i")" ] && /bin/echo "./$(basename "$i")" && return 0
p="$0"
while :; do
[ "$p" != "$(dirname p)" ] || break
p="$(dirname "$p")"
[ -f "$p/$i" ] && /bin/echo "$p/$i" && return 0
done
[ -f "$i" ] && /bin/echo "$i" && return 0
return 1
done
}
if [ "${ia_prefix:=ia}" != "ia" ] || [ "${dbg_prefix:=dbg}" != "dbg" ]; then
[ -n "$(eval "/bin/echo \${${ia_prefix}_basic_already_sourced:=}")" ] && return
eval "$(sed -e "s/ia_/${ia_prefix}_/g" -e "s/ia()/${ia_prefix}()/g" \
-e "s/dbg/${dbg_prefix}/g" "$(ia_find_lib /usr/share/shellia/ia.basic)")"
return
fi
[ -n "${ia_basic_already_sourced:=}" ] && return || ia_basic_already_sourced=1
# colours could be overwritten after reading this library
# color vars are already interpreded with echo -e option,
# so that it does not matter if option -e is set when color vars are used
# Black 0;30 | Dark Gray 1;30 | Red 0;31 | Light Red 1;31
# Green 0;32 | Light Green 1;32 | Brown/Orange 0;33 | Yellow 1;33
# Blue 0;34 | Light Blue 1;34 | Purple 0;35 | Light Purple 1;35
# Cyan 0;36 | Light Cyan 1;36 | Light Gray 0;37 | White 1;37
# respect colors already set (even if set to empty)
[ -n "${ia_red+x}" ] || ia_red="$(/bin/echo -e "\033[0;31m")"
[ -n "${ia_green+x}" ] || ia_green="$(/bin/echo -e "\033[0;32m")"
[ -n "${ia_orange+x}" ] || ia_orange="$(/bin/echo -e "\033[0;33m")"
[ -n "${ia_cyan+x}" ] || ia_cyan="$(/bin/echo -e "\033[0;36m")"
[ -n "${ia_no_color+x}" ] || ia_no_color="$(/bin/echo -e "\033[0m")"
ia_nl="
"
# ia_col_regex can be used to filter out color control codes
[ -n "${ia_col_regex+x}" ] || ia_col_regex="$(/bin/echo -e "\033[[0-9;]+m")"
# functions to be overwritten from ia.log
if [ -z "${ia_log_already_sourced:=}" ]; then
ia_direct_stderr_fd_calc() { :; };
ia_check_logfile() { :; };
ia_log() { :; };
ia_logerr() { local l; /bin/echo "$@" | ia_easy_backslash --backslash | while read l; do ia_err "$l"; done; }
ia_exiterr() { code=$1; shift; /bin/echo "$@" >&2; exit $code; }
ia_err() { /bin/echo "$@" >&2; };
ia_dev_dbg() { /bin/echo "ia_dev_dbg: $1" >&2; };
fi
# ia_easy_backslash [--dollar] [--backslash] [--quote] [--backquote] [--backquote] [--all]
# --dollar ensure $ will not be interpreted by /bin/echo in strings starting and ending with "
# --backslash ensure \ will not be interpreted by /bin/echo in strings starting and ending with "
# --quote ensure " will not be interpreted by /bin/echo in strings starting and ending with "
# --backquote ensure ` will not be interpreted by /bin/echo in strings starting and ending with "
# --all same as options --dollar --backslash --quote --backquote
# without options same as options --dollar --backslash --quote
ia_easy_backslash()
{
local SED colon dollar backslash backquote
backslash="1"; quote="1"; dollar="1"; backquote="1"
if [ $# -gt 0 ]; then
backslash=""; quote=""; dollar=""; backquote=""
while [ $# -gt 0 ]; do
if [ "$1" = "--dollar" ]; then dollar=1
elif [ "$1" = "--backslash" ]; then backslash=1
elif [ "$1" = "--quote" ]; then quote=1
elif [ "$1" = "--backquote" ]; then backquote=1
#elif [ "$1" = "--all" ]; then backslash="1"; quote="1"; dollar="1"; backquote="1"
else ia_exiterr 1 "ia_easy_backslash: bad option <$1>"; fi
shift
done
fi
# because "busybox sh"'s sed has no option --unbuffered, we use external sed
[ -f /usr/bin/sed ] && SED="/usr/bin/sed" || SED="/bin/sed"
{ [ -n "$backslash" ] && $SED --unbuffered -e 's#\\#\\\\#g' || cat; } |
{ [ -n "$quote" ] && $SED --unbuffered -e 's#"#\\"#g' || cat; } |
{ [ -n "$dollar" ] && $SED --unbuffered -e 's#\$#\\\$#g' || cat; } |
{ [ -n "$backquote" ] && $SED --unbuffered -e 's#`#\\`#g' || cat; } |
{ [ -n "$backslash" ] && $SED --unbuffered -e 's#\\\$<#$<#g' || cat; }
}
#a="one\"two\\three\$four\'five"
#[ "$(/bin/echo "$a")" = "$a" ] && echo "ok ia_easy_backslash-1" || echo "err ia_easy_backslash-1"
#[ "$(eval "/bin/echo \"$(/bin/echo "$a" | ia_easy_backslash)\"")" = "$a" ] && echo "ok ia_easy_backslash-2" || echo "err ia_easy_backslash-2"
#exit 0
# save "$@" to a variable, that can later be loaded to "$@" again
# exp: save "$@" to variable v:
# v="$(ia_set2var "$@")"
# exp: load variable v to "$@" again:
# eval "set -- $v"
ia_set2var()
{
for i in "$@"; do
/bin/echo -n "\"$(/bin/echo "$i" | ia_easy_backslash)\" "
done | sed "s/ $//"
}
#a="$(ia_set2var "normal" "blank blank" "doublequote\"doublequote" "dollar\$dollar" "newline
#newline" "backslash\\backslash" "backquote\`backquote")"
#b="$(ia_set2var "normal" blank\ blank doublequote\"doublequote dollar\$dollar "newline
#newline" backslash\\backslash backquote\`backquote)"
#c="\"normal\" \"blank blank\" \"doublequote\\\"doublequote\" \"dollar\\\$dollar\" \"newline
#newline\" \"backslash\\\\backslash\" \"backquote\\\`backquote\""
#eval "set -- $a"; d="$(ia_set2var "$@")"
#[ "$a" = "$b" ] && echo "ok ia_set2var-1" || echo "err ia_set2var-1"
#[ "$a" = "$c" ] && echo "ok ia_set2var-2" || echo "err ia_set2var-2"
#[ "$a" = "$d" ] && echo "ok ia_set2var-3" || echo "err ia_set2var-3"
#exit 0
# ia_set_minus <v1> <v2>
# if v1 is created with ia_set2var from a first "$@" list, and
# if v2 is created with ia_set2var from a second "$@" list, and
# and v3 is created with v3="$(ia_set_minus "$v1" "$v2")" then
# v3 is v1 without elements from v2
ia_set_minus()
{
v1="$1"
v2="$2"
eval "set -- $v1"
for i1 in "$@"; do
eval "set -- $v2"
for i2 in "$@"; do
[ "$i1" != "$i2" ] || continue 2
done
/bin/echo -n "\"$(/bin/echo "$i1" | ia_easy_backslash)\" "
done | sed "s/ $//"
}
#a="$(ia_set2var "normal" "blank blank" "doublequote\"doublequote" "dollar\$dollar" "newline
#newline" "backslash\\backslash" "backquote\`backquote")"
#b="$(ia_set2var "newline
#newline" "blank blank" normal)"
#c="$(ia_set_minus "$a" "$b")"
#exp="$(ia_set2var "doublequote\"doublequote" "dollar\$dollar" "backslash\\backslash" "backquote\`backquote")"
#[ "$c" = "$exp" ] && echo "ok ia_set_minus-1" || echo "err ia_set_minus-1"
#c="$(ia_set_minus "$c" "$a")"
#[ "$c" = "" ] && echo "ok ia_set_minus-2" || echo "err ia_set_minus-2"
#exit 0
ia_prep_args4eval()
{
local args
args=""
for i in "$@"; do
[ -n "$args" ] && args="$args \"$(/bin/echo "$i"|ia_easy_backslash)\"" ||
args="\"$(/bin/echo "$i"|ia_easy_backslash)\""
done
/bin/echo "$args"
}
#t() { echo "<$1> <$2> <$3>"; }
#[ "$(eval "t $(ia_prep_args4eval 1 "2 2" "3 3 3")")" = "<1> <2 2> <3 3 3>" ] && echo "ok ia_prep_args4eval-1" || echo "err ia_prep_args4eval-1"
#[ "$(eval "t $(ia_prep_args4eval "'" "\$ \\" "\" \" \"")")" = "<'> <\$ \\> <\" \" \">" ] && echo "ok ia_prep_args4eval-2" || echo "err ia_prep_args4eval-2"
#exit 0
# ia_ask [-f] <question> <default> <problem> <prompt> <regex>
# question the text to be displayed to the user
# default the default answer returned if the user just enters <RETURN>
# problem the answer returned if there was a problem (eg. Ctrl-D/EOF pressed)
# prompt the prompt the user is presented
# regex a regex that must be matched by the users answer
# -f force asking if it is technically possible, even if ia_use_silent is set
ia_ask()
{
local ia_verify # no verification: "", first ask: "1: ", second ask: "2: "
local ia_question
local ia_default
local ia_problem # no stdin or user pressed Ctrl-D or other technical problem
local ia_prompt
local ia_regex # extended regex
local ia_input
local ia_force
local ia_read_exit
local ia_first
local ia_tty
ia_verify=""
ia_echo_off=""
ia_force=""
while [ $# -gt 5 ]; do
if [ "$1" = "-f" ]; then
ia_force="$1"
shift
elif [ "$1" = "-p" ]; then
ia_echo_off="$1"
shift
elif [ "$1" = "-2" ]; then
ia_verify="1: "
shift
else
ia_exiterr 1 "ia_ask: internal err: unknonw option <$1>"
fi
done
ia_question="$1"
ia_default="$2"
ia_problem="$3"
ia_prompt="$4"
ia_regex="$5"
ia_first=""
tty -s && ia_tty="0" || ia_tty="$?"
while :; do
while :; do
ia_read_exit="1"
ia_log "$ia_question"
if [ -n "${ia_use_silent:-}" -a -z "$ia_force" ]; then
[ -n "$ia_default" ] && ia_input="$ia_default" || ia_input="$ia_problem"
ia_log "silent => $ia_input"
break
elif [ -n "$ia_echo_off" -a "$ia_tty" != "0" ]; then
ia_input="$ia_problem"
ia_log "no-tty => $ia_input"
break
else
ia_err "$ia_question"
if test -t 0; then
# direct stdin -> we can use option -n
ia_err -n "$ia_verify$ia_prompt"
else
# stdin is redirected -> we can not use option -n
# exp: works: '{ /bin/echo -n -e "one\ntwo " ; read x; echo; }'
# does not work: '{ /bin/echo -n -e "one\ntwo " ; read x; echo; } | sed -u -n p'
if [ -n "${ia_ssh_running:-}" ]; then
ia_err "$ia_verify${ia_prompt}ia_ssh_no_nl"
else
ia_err "$ia_verify$ia_prompt"
fi
fi
[ -n "$ia_echo_off" ] && stty -echo
read -r ia_input || ia_input=""
ia_read_exit=$?
[ -n "$ia_echo_off" ] && { stty echo; ia_err ""; }
fi
if [ $ia_read_exit -ne 0 ]; then
ia_input="$ia_problem"
ia_log "$ia_prompt<EOF> => $ia_input"
break
elif [ -z "$ia_input" -a -n "$ia_default" ]; then
ia_input="$ia_default"
ia_log "$ia_prompt<RETURN> => $ia_input"
break
elif [ "$ia_verify" = "2: " ]; then
# also everything, alse empty input to start from scratch
[ -n "$ia_echo_off" ] && ia_log "$ia_prompt<pass2><RETURN>" || ia_log "$ia_prompt$ia_input<RETURN>"
break
else
[ -n "$ia_echo_off" ] && ia_log "$ia_prompt<pass><RETURN>" || ia_log "$ia_prompt$ia_input<RETURN>"
/bin/echo "$ia_input" | grep -E -e "$ia_regex" >/dev/null && break
fi
done
if [ "$ia_verify" = "" ]; then
break
elif [ "$ia_verify" = "1: " ]; then
ia_first="$ia_input"
ia_verify="2: "
elif [ "$ia_first" = "$ia_input" ]; then
break
else
ia_verify="1: "
fi
done
/bin/echo "$ia_input"
}
#ia_ask "ha${ia_green}ll${ia_no_color}o" "17" "q" "? " "^(1|2|3)$"
#exit 0
|