3 # if source file direct nestly, the sub file source path is not correct.
4 # use this alias cmd to fix this problem.
5 # it stores $PWD, entering dir of include file.
9 # return if has been sourced before.
10 # this code is disabled, it should be putted in application program.
12 #alias include >/dev/null 2>&1
13 #[[ $? == 0 ]] && alias inc="include" return
16 # TBD: write value in tmp, it should be generated by _EVL
17 # SRCPKG_DIR_FULL=$PWD
20 shopt -s expand_aliases
21 alias include="diffinc "
26 echo "$1" | grep -E "inc_info" >/dev/null 2>&1
27 test "$?" != 0 && return
38 echo "$1" | grep -E "inc_info" >/dev/null 2>&1
39 test "$?" != 0 && return
42 while [[ $cnt -gt 0 ]]; do
51 echo "$1" | grep -E "inc_dbgout" >/dev/null 2>&1
52 test "$?" != 0 && return
63 [[ -z "$1" || ! -f "$1" ]] && return
69 vname=`echo "$vname" | tr [[:punct:]] '_' | tr '[[:lower:]]' '[[:upper:]]'`
70 eval test \"\$$vname\" = 1 && return
83 # get func list in current process.
94 # get var list in current process.
103 done < <(declare -p | grep -e "declare" | sed -e "s/^.*-f.*$//g")
106 # get alias list in current process.
113 done < <(alias | grep -e "^alias")
117 # syntax: var_list_diff <outputvarname> = <listvarname1> - <listvarname2>
118 # desc: it diff two variable data, and get the new appended info.
127 if test -n "$1" && test -n "$3" && test -n "$5" && test "$2" = '=' && test "$4" = '-' ; then
129 declare "$3=$(echo "${!3}" | tr ' ' $'\n')";
130 declare "$5=$(echo "${!5}" | tr ' ' $'\n')";
136 declare -g $1+="${data}"$'\n'
137 done < <(echo "${!5}" | diff --from-file=<(echo "${!3}") - | grep -e "<";)
138 declare -g $1+="${!1%$'\n'}"
146 vname=${vname//[^[:alnum:]_]/_}
149 # declare var before source, or it will be append to varlist_xxx.
151 declare funclist1_$vname=""
152 declare funclist2_$vname=""
153 declare varlist1_$vname=""
154 declare varlist2_$vname=""
155 declare aliaslist1_$vname=""
156 declare aliaslist2_$vname=""
159 # getting func/var/alias list before
161 eval funclist1_$vname=\"\$\(get_func_list\)\"
162 eval varlist1_$vname=\"\$\(get_var_list\)\"
163 eval aliaslist1_$vname=\"\$\(get_alias_list\)\"
172 # getting func/var/alias list after
174 eval funclist2_$vname=\"\$\(get_func_list\)\"
175 eval varlist2_$vname=\"\$\(get_var_list\)\"
176 eval aliaslist2_$vname=\"\$\(get_alias_list\)\"
179 # diff list between before and after.
181 listvar_diff funclist_$vname = funclist2_$vname - funclist1_$vname
182 listvar_diff varlist_$vname = varlist2_$vname - varlist1_$vname
183 listvar_diff aliaslist_$vname = aliaslist2_$vname - aliaslist1_$vname
185 # eval echo funclist_$vname=\${funclist_$vname} > 3.txt
186 # eval echo varlist_$vname=\${varlist_$vname} >> 3.txt
187 # eval echo aliaslist_$vname=\${aliaslist_$vname} >> 3.txt
191 # fsyntax: loadimi <imi-file-name>
203 local file=$(basename $1)
206 test -z "$1" || test ! -f "$1" && return
209 while test "$cnt" -gt 0 ; do
213 inc_info "loadimi ($@)\n"
217 # it's normal if file is not exist.
219 inc_dbgout "parameter file($1) is not exist.\n" &&
223 test "`echo "$1" | grep -o -P '([^\.]*)' | tail -n 1`" != "imi" &&
224 err "specified file $1 is not .imi file.\n" &&
230 if test -e "$file" ; then
231 # it need -r param to receive '\'.
232 while read -r data; do
234 [[ "${data}" =~ $'\r' ]] && data=$(echo ${data:0:-1})
235 # filter comment, and skip blank line.
236 [[ -z ${data//[[:space:]]/} ]] && continue
237 if [[ ${data} =~ '#' ]]; then
238 [[ ${data} =~ ^[[:space:]]*[#] ]] && continue
239 data="${data//[[:space:]]*[#].*/}"
242 # ignore [xxx] section setting, use it as an ini file.
243 [[ ${data} =~ ^[[:space:]]*\[ ]] && continue
245 # ignore [xxx] section setting, use it as an ini file.
246 [[ ${data} =~ ^[[:space:]]*$ ]] && continue
248 [[ "${data}" =~ ^[[:blank:]]*inc[[:blank:]] ||
249 "${data}" =~ ^[[:blank:]]*loadimi[[:blank:]] ||
250 "${data}" =~ ^[[:blank:]]*loadlist[[:blank:]] ||
251 "${data}" =~ ^[[:blank:]]*\$\( ||
252 "${data}" =~ ^[[:blank:]]*\` ||
253 "${data}" =~ ^[[:blank:]]*\:[[:blank:]] ]] &&
254 eval "$data" && continue;
256 if [[ "${data}" =~ '=(' ]]; then
258 # [[ ! "${data}" =~ ')' ]] && continue
259 data="$(echo "$data" | grep -zoE "^.*[=][\(]([^\$\(]*(['][^']['])*|([\"][^\"]*[\"])|([\`][^\`]*[\`])|([\$][\(][^\)]*[\)])*)*[\)]")"
260 if [[ -z $data ]]; then
262 # '' string recognizing.
265 # [[ -z $tmp ]] && break
267 if [[ "$data" =~ ')' ]]; then
268 data="$(echo "$tmp" | grep -zoE "^.*[=][\(]([^\$\(]*(['][^']['])*|([\"][^\"]*[\"])|([\`][^\`]*[\`])|([\$][\(][^\)]*[\)])*)*[\)]")"
269 [[ -n $data ]] && break
272 [[ "$idx" -gt 1000 ]] && echo "[error] array defination longer then 1000." >&2 && exit -1
274 #"[\=][\(]([^']*['][^']*['])*\)"
280 if [[ "${data}" =~ =\" ]]; then
282 # [[ ! "${data}" =~ ')' ]] && continue
283 data="$(echo "$data" | grep -zoE "^.*[=][\"](.*(['][^']['])*|([\"][^\"][\"])*|([\`][^\`][\`])*.*)*[\"]")"
284 if [[ -z $data ]]; then
286 # '' string recognizing.
289 # [[ -z $tmp ]] && break
291 if [[ "$data" =~ '"' ]]; then
292 data="$(echo "$tmp" | grep -zoE "^.*[=][\"](.*(['][^']['])*|([\"][^\"][\"])*|([\`][^\`][\`])*.*)*[\"]")"
293 [[ -n $data ]] && break
296 [[ "$idx" -gt 1000 ]] && echo "[error] array defination longer then 1000." >&2 && exit -1
298 #"[\=][\(]([^']*['][^']*['])*\)"
304 if [[ "${data}" =~ =\' ]]; then
306 # [[ ! "${data}" =~ ')' ]] && continue
307 data="$(echo "$data" | grep -zoE "^.*[=][\'].*[\']")"
308 if [[ -z $data ]]; then
310 # '' string recognizing.
313 # [[ -z $tmp ]] && break
315 if [[ "$data" =~ \' ]]; then
316 data="$(echo "$tmp" | grep -zoE "^.*[=][\'].*[\']")"
317 [[ -n $data ]] && break
320 [[ "$idx" -gt 1000 ]] && echo "[error] array defination longer then 1000." >&2 && exit -1
322 #"[\=][\(]([^']*['][^']*['])*\)"
328 # TBD: add ':' recognize and executing
329 # TBD: add content var output to file.
331 if [[ "${flag}" == 'array' ]]; then
332 if [[ ! "${data}" =~ '=(' ]]; then
333 if [[ "${data}" =~ ')' ]]; then
334 tmp+="${data%%\)*}"$'\n'
341 if [[ "${data}" =~ ')' ]]; then
348 [[ ! "$data" =~ '=' ]] && echo -e "\033[1m\033[31m[error][$file:line$i]\033[0m: '$data' not a valid assign statement." && exit -1
350 inc_dbgout "data=\"${data}\"\n"
352 if [[ ${data} =~ _EVL ]]; then
354 eval declare -g "${data}"
356 # define without _EVL
357 data="${data/_EVL=/=}"
359 # it can works with "" or '' in config file.
360 # multi-line paramter putted in a single line,
361 # and assign to the specified variable.
363 data="${data//\"/\\\"}"
364 # data="${data//\'/\\\'}"
365 # inc_dbgout "=== ${data}\n"
366 # \$\'\\\"\"\\\n\\\"\"\'
367 eval "$(eval echo \"${data}\")"
369 # append newline after every one.
370 # inc_dbgout "=== ${data}\n"
371 [[ ! "$data" =~ '=' ]] && echo -e "\033[1m\033[31m[error][$file:line$i]\033[0m: '$data' not a valid assign statement." && exit -1
381 while [[ "${tmp}" =~ '=' ]]; do
384 [[ ${bak: -1,1} == '+' ]] && bak="${bak:0:-1}"
387 # [[ "$abc" =~ (\=.*\=.*)*(\=.*) && $(($(expr length ${abc//[^=]/})%2)) == 1 ]] && echo bbb ${BASH_REMATCH[@]}
388 # [[ "${tmp##*=}" =~ '"'{1,3,2} ]] && tmp="$bak" && continue
393 charcnt="${bak2//[^\']/}"
394 qcnt=$((${#charcnt} + qcnt))
395 charcnt="${bak2//[^\"]/}"
396 dqcnt=$((${#charcnt} + dqcnt))
397 [[ "${bak2}" =~ (\'.*\'.*)*(\'.*) && $((qcnt%2)) == 1 ]] && continue
398 [[ "${bak2}" =~ (\".*\".*)*(\".*) && $((dqcnt%2)) == 1 ]] && continue
400 [[ "${tmp}" =~ '#' ]] && continue;
401 [[ "${tmp: -1:1}" == ':' ]] && continue;
406 # generate var in auto by _EVL suffix.
407 if [[ "${data}" =~ _EVL$ ]]; then
408 eval "${data}=${!data}"
411 vlist+="${data}"$'\n'
412 # inc_dbgout "vlist+=\"${data}$'\n'\""
415 # get var name, and delete redundent "\n".
416 # it need -r param to receive '\'.
417 while read -r data; do
418 [[ -z $data ]] && continue
420 while [[ "${!data: -1:1}" == $'\n' ]]; do
421 eval "$data=\"\${!data:0:-1}\""
426 err "list file '$1' is not exist.\n"
435 # fsyntax: loadlist <list-file-name>
436 # fdesc: load .list file. if not exist, load .lst file.
447 local file=$(basename $1)
450 [[ -z "$1" || ! -f "$1" ]] && return
453 while [[ $cnt -gt 0 ]]; do
457 inc_info -ne "loadlist ($1)\n"
462 # skip nestly loaded file.
464 name=${1//[^[:alnum:]_]/_}
465 if [[ -v ${name} && ${!name} == Y ]]; then
466 # echo "file $name loaded nestly."
472 [[ ${1##*\.} != "lst" && ${1##*\.} != "list" ]] && err "specified file '$1' is not .list or .lst file.\n" && unset $name && return 1
476 if [[ -e "$file" ]]; then
477 # it need -r param to receive '\'.
478 while read -r data; do
480 # filter comment, and skip blank line.
481 [[ -z ${data//[[:space:]]/} ]] && continue
482 if [[ ${data} =~ '#' ]]; then
483 [[ ${data} =~ ^[[:space:]]*[#] ]] && continue
484 data="${data%%*([[:space:]])[#]*}"
485 [[ -z "${data}" ]] && continue
488 [[ "${data}" =~ '^inc ' || "${data}" =~ '^loadimi ' || "${data}" =~ '^loadlist ' ]] && eval $data && continue;
491 # append '\' for '\"'
493 data="${data//\\\"/\\\\\\\"}"
495 # append newline after every one.
496 inc_dbgout "list: ${data}\n"
497 # eval "${data}"$'"\n"'
498 eval declare -g "${data}"
499 [[ $? != 0 ]] && err "[$file:line$i]: '$data' failed.\n" && exit -1
506 while [[ "${tmp}" =~ '=' ]]; do
509 [[ ${bak: -1,1} == '+' ]] && bak="${bak:0:-1}"
512 # [[ "$abc" =~ (\=.*\=.*)*(\=.*) && $(($(expr length ${abc//[^=]/})%2)) == 1 ]] && echo bbb ${BASH_REMATCH[@]}
513 # [[ "${tmp##*=}" =~ '"'{1,3,2} ]] && tmp="$bak" && continue
518 charcnt="${bak2//[^\']/}"
519 qcnt=$((${#charcnt} + qcnt))
520 charcnt="${bak2//[^\"]/}"
521 dqcnt=$((${#charcnt} + dqcnt))
522 [[ "${bak2}" =~ (\'.*\'.*)*(\'.*) && $((qcnt%2)) == 1 ]] && continue
523 [[ "${bak2}" =~ (\".*\".*)*(\".*) && $((dqcnt%2)) == 1 ]] && continue
525 [[ "${tmp}" =~ '#' ]] && continue;
526 [[ "${tmp: -1:1}" == ':' ]] && continue;
533 # get var name, and delete redundent "\n".
535 [[ ${bak: -1:1} == '+' ]] && bak="${bak:0:-1}"
537 while [[ "${!bak: -1:1}" == $'\n' ]]; do
538 eval "$bak=\"\${!bak:0:-1}\""
542 # CFLAGS_DEF_LIST_Y+=" -DMACHTYPE=\"$MACHTYPE\" "
543 # disable '\"' to '\\\"'.
545 # declare -g "$bak=${!bak//\\\"/\\\\\\\"}"
547 # append blank to the end of string in auto.
548 [[ "${!bak: -1:1}" != ' ' || "${!bak: -1:1}" != $'\n' ]] && declare -g $bak+=$'\n'
552 # err "list file is not exist.\n"
556 # it can be loaded next time un-nestly.