2 __get_file_top_valid_id ()
7 for (( i=$((i-1)); i>0; i-- )); do
9 [[ ! -e /proc/self/fd/$i ]] && echo $i && return
13 f_ttyout=$(ps -p $$ -o tty | tail -n 1 | cut -d ' ' -f1)
14 # echo -ne "f_ttyout=$f_ttyout\n"
15 f_ttyout="/dev/$f_ttyout"
17 vch_ttyout=$(__get_file_top_valid_id)
18 # echo f_ttyout=$f_ttyout
19 eval "exec $vch_ttyout<>$f_ttyout"
20 [[ $? != 0 ]] && vch_ttyout=1
24 echo -ne "$2" >&$vch_ttyout
26 [[ -z "$1" ]] && echo "please specify parameter for apuse()." && return
28 if [[ -n $3 && $DEF_INPUT == "true" ]]; then
33 read -u $vch_ttyout -r $1
35 [[ -n $3 ]] && [[ -z ${!1} || ${!1} == "$'\n'" ]] && eval "$1=$3"
40 # return if has been sourced before.
41 # this code is disabled, it should be putted in application program.
43 #alias include >/dev/null 2>&1
44 #[[ $? == 0 ]] && alias inc="include" return
47 # TBD: write value in tmp, it should be generated by _EVL
48 # SRCPKG_DIR_FULL=$PWD
51 shopt -s expand_aliases
52 alias include="diffinc "
57 echo "$1" | grep -E "inc_info" >/dev/null 2>&1
58 test "$?" != 0 && return
69 echo "$1" | grep -E "inc_info" >/dev/null 2>&1
70 test "$?" != 0 && return
73 while [[ $cnt -gt 0 ]]; do
82 echo "$1" | grep -E "inc_dbgout" >/dev/null 2>&1
83 test "$?" != 0 && return
90 # if source file direct nestly, the sub file source path is not correct.
91 # use this alias cmd to fix this problem.
92 # it stores $PWD, entering dir of include file.
99 [[ -z "$1" || ! -f "$1" ]] && return
104 vname="`basename $1`"
105 vname=`echo "$vname" | tr '[[:punct:]]' '_' | tr '[[:lower:]]' '[[:upper:]]'`
107 # XXX: redundent include check will causes parameter can not be updated.
108 # eval test \"\$$vname\" = 1 && return
115 source $(basename $1)
121 # get func list in current process.
134 # get var list in current process.
144 `declare -p | grep -e "declare" | sed -e "s/^.*-f.*$//g"`
148 # get alias list in current process.
156 `alias | grep -e "^alias"`
161 # fsyntax: list_diff <type> <name>
162 # <outputvarname> = <listvarname1> - <listvarname2>
163 # fdesc: type is one of func/var/alias, name is shlib.
172 list_name="${1}list1_$vname"
173 list2_name="${1}list2_$vname"
175 export $list2_name=""
176 # echo "$list2_name define:"
177 # declare -p $list2_name
179 # diff with the list saved before source operation.
183 data="$(echo "{data}" | sed -E "s/[[:blank:]]*< //g")"
184 if [[ "$1" == func ]]; then
185 # add -x option to function, so that sub-proc can use it without load again.
186 declare -f -x ${data}
187 elif [[ "$1" == var ]]; then
188 if [[ "$data" =~ 'funclist' || "$data" =~ 'varlist' || "$data" =~ 'aliaslist' ]]; then
191 elif [[ "$1" == alias ]]; then
193 data="${data##*alias }"
196 eval $list2_name+="\"${data} \""
197 done < <(echo "${!list_name}" | diff --from-file=<(get_$1_list) - | grep -e "<")
199 # ctx_xxx, just for alias recovery.
200 [[ "$1" == alias ]] && eval ctx_$list2_name="\$(alias \${$list2_name})"
203 eval list_dbgout \"\${$list2_name[@]}\" | tr ' ' $'\n'
207 # syntax: var_list_diff <outputvarname> = <listvarname1> - <listvarname2>
208 # desc: it diff two variable data, and get the new appended info.
217 if test -n "$1" && test -n "$3" && test -n "$5" && test "$2" = '=' && test "$4" = '-' ; then
219 eval "$3=\"$(echo "${!3}" | tr ' ' $'\n')\""
220 eval "$5=\"$(eval echo \"\${$5}\" | tr ' ' $'\n')\""
226 eval $1=\"\${$1}${data}\"\$'\n'
228 `echo "${!5}" | diff --from-file=<(echo "${!3}") - | grep -e "<";`
230 eval $1="$(eval echo \"\${$1}\" | sed -E "/^\$/d")"
236 local vname=$(basename $1)
237 vname="$(echo "${vname}" | sed -E "s/[.]([[:alnum:]_]*)//g; s/[^[:alnum:]_]/_/g")"
240 # declare var before source, or it will be append to varlist_xxx.
242 eval funclist1_$vname=""
243 eval funclist2_$vname=""
244 eval varlist1_$vname=""
245 eval varlist2_$vname=""
246 eval aliaslist1_$vname=""
247 eval aliaslist2_$vname=""
250 # getting func/var/alias list before
252 eval funclist1_$vname=\"\$\(get_func_list\)\"
253 eval varlist1_$vname=\"\$\(get_var_list\)\"
254 eval aliaslist1_$vname=\"\$\(get_alias_list\)\"
263 # getting func/var/alias list after
265 eval funclist2_$vname=\"\$\(get_func_list\)\"
266 eval varlist2_$vname=\"\$\(get_var_list\)\"
267 eval aliaslist2_$vname=\"\$\(get_alias_list\)\"
270 # diff list between before and after.
272 listvar_diff funclist_$vname = funclist2_$vname - funclist1_$vname
273 listvar_diff varlist_$vname = varlist2_$vname - varlist1_$vname
274 listvar_diff aliaslist_$vname = aliaslist2_$vname - aliaslist1_$vname
277 # list_diff func $vname
279 # list_diff var $vname
281 # list_diff alias $vname
283 # declare -p funclist_$vname varlist_$vname aliaslist_$vname
285 # eval echo funclist_$vname=\${funclist_$vname} > 3.txt
286 # eval echo varlist_$vname=\${varlist_$vname} >> 3.txt
287 # eval echo aliaslist_$vname=\${aliaslist_$vname} >> 3.txt
314 test -n "$on_imi_set_var" &&
315 $on_imi_set_var "$imivname" "$imiopr" "$imivalstr" &&
320 [[ "$LOAD_DBG" = 1 ]] && echo $1 "$imivname" "$imiopr" "$imivalstr"
321 $1 "$imivname" "$imiopr" "$imivalstr"
323 imivlist="$imivlist"$'\n'"${imivname}"
325 # echo "#########################################"
326 # echo -n "${imivname}="
330 if test "$(echo ${imivname} | tail -c -5)" = _EVL; then
331 imivname="$(echo ${imivname} | head -c -5)"
332 eval eval "$imivname=\"\\\"\${${imivname}_EVL}\\\"\""
333 imivlist="$imivlist"$'\n'"${imivname}"
341 # @ init with "" string, ' in it should add like '\'', as it is in '' string.
342 # @ do not add comment after assignment, especially after "" or '' or ().
343 # @ ${XXX:1:2}, envar opr like slice and others are not supported.
347 # fsyntax: loadimi <imi-file-name>
353 local file=$(basename $1)
358 test -z "$1" || test ! -f "$1" && inc_dbgout "invalid file($1).\n" && return
360 # test "`echo "$1" | grep -o -P '([^\.]*)' | tail -n 1`" != "imi" && echo "[error]: specified file $1 is not .imi file.\n" >&2 && unset $name && return 1
363 while test "$cnt" -gt 0 ; do
367 inc_info -ne "loadimi ($@)\n"
373 if test ! -e "$file" ; then
374 echo "list file '$1' is not exist.\n" >&2
378 # directly source if no interpreter loadimi setted.
380 if strstr ! "$imilinedata" =~ 'loadimi'; then
381 test "$LOAD_DBG" = 1 && set -x
391 # echo "loadimi ($file)"
393 # it need -r param to receive '\'.
394 while read -r imilinedata; do
400 envar_name_parsing || continue
401 # declare -p imilinedata imicmd imicmdopt imivname imiopr imivalpfx imivalstr imivalsfx
402 # pause "testvar" "envar_name_parsing:" ""
407 test -n "$imicmd" && echo "$imicmdlist" | grep -q -E "$imicmd" &&
408 eval "$imicmd \"$imicmdopt\" \"$imivname\" \"$imiopr\" \"$imivalstr\"" &&
412 { eval $imilinedata ; continue; }
413 # echo -e "\033[1m\033[31m[error][$file:line$i]\033[0m: '$imilinedata' not a valid assign statement." &&
417 # load multiple line data init. set if it is an array init.
419 load_var_string && imi_set_var loadenv && continue
424 imivlist="$(echo "$imivlist" | uniq)"
429 unset LOAD_DBG on_imi_set_var
433 # fdesc: load imi by cmd of 'source', to decrease cpu cost.
434 # it's for parameter without array.
442 local file=$(basename $1)
445 test -z "$1" || test ! -f "$1" && inc_dbgout "invalid file($1).\n" && return
446 test "`echo "$1" | grep -o -P '([^\.]*)' | tail -n 1`" != "imi" &&
447 echo "specified file $1 is not a valid .imi file.\n" >&2 &&
451 while test "$cnt" -gt 0 ; do
455 inc_info "loadimi ($@)\n"
467 # fdesc: load imi by cmd of 'source', to decrease cpu cost.
468 # it's for parameter without array.
476 local file=$(basename $1)
480 test -z "$1" || test ! -f "$1" && inc_dbgout "invalid file($1).\n" && return
481 test "`echo "$1" | grep -o -P '([^\.]*)' | tail -n 1`" != "list" &&
482 echo "specified file $1 is not a valid .list file.\n" >&2 &&
485 vname="$(echo "$file" | tr '[[:lower:]]' '[[:upper:]]' | tr '[^[:punct:]_]' '_')_Y"
488 while test "$cnt" -gt 0 ; do
492 inc_info "loadimi ($@)\n"
497 # load without comment.
498 eval ${vname}="\"$(cat $file | sed -E "s/[[:blank:]]*([#].*)?[\r]?$//g; /^$/d; s/\\\\/\\\\\\\\/g; s/[\"]/\\\\\"/g;")\""
500 # TBD: store them into arr?
506 # put this code into porting.shlib
508 #test "$SHELL" = '/bin/bash' && STDSH=
510 # use __loadimi in standard shell
511 if [[ -n "$STDSH" ]]; then
512 alias loadimi=__loadimi
513 elif [[ false ]]; then
516 alias loadimi=sourceimi