improve of cmpl.
[bush.git] / tools / build-srcpkg-bak-20240503 / shlib / incfile.shlib
blob54f40fa0ec11e03f4532e49bd423f8a186b1410c
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
15         
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 "
24 inc_info ()
26         echo "$1" | grep -E "inc_info" >/dev/null 2>&1
27         test "$?" != 0 && return
28         
29         echo -ne "$@" # "\n"
32 inc_lvl=0
34 inc_indent_info ()
36         local cnt=$inc_lvl
38         echo "$1" | grep -E "inc_info" >/dev/null 2>&1
39         test "$?" != 0 && return
41         # for debugging.
42         while [[ $cnt -gt 0 ]]; do
43                 inc_info "  "
44                 : $((cnt--))
45         done
46         echo -ne "inc ($1)\n"
49 inc_dbgout ()
51         echo "$1" | grep -E "inc_dbgout" >/dev/null 2>&1
52         test "$?" != 0 && return
53         echo -ne "$@" # "\n"
56 #alias inc="diffinc "
57 #incfile ()
58 inc ()
60         local pwd=$PWD
61         local vname=
63         [[ -z "$1" || ! -f "$1" ]] && return
64         
65         inc_indent_info
66         
67         # uniq load
68         vname="`basename $1`"
69         vname=`echo "$vname" | tr [[:punct:]] '_' | tr '[[:lower:]]' '[[:upper:]]'`
70         eval test \"\$$vname\" = 1 && return
71         eval $vname=1
72         
73 #       echo inc $1
74         : $((inc_lvl++))
76         cd $(dirname $1)
77         source $(basename $1)
78         cd $pwd
79         
80         : $((inc_lvl--))
83 # get func list in current process.
84 get_func_list ()
86         local data=
88         while read data; do
89                 data="${data##* }"
90                 echo -ne "${data}\n"
91         done < <(declare -F)
94 # get var list in current process.
95 get_var_list ()
97         local data=
98         
99         while read data; do
100                 data="${data%%=*}"
101                 data="${data##* }"
102                 echo "${data}"
103         done < <(declare -p | grep -e "declare" | sed -e "s/^.*-f.*$//g")
106 # get alias list in current process.
107 get_alias_list ()
109         local data=
110         
111         while read data; do
112                 echo "${data}"
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.
120 listvar_diff ()
122     local vout=$1
123     local vin1="$3"
124     local vin2="$5"
125     local data=
126     
127     if test -n "$1" && test -n "$3" && test -n "$5" && test "$2" = '=' && test "$4" = '-' ; then
128         declare -g $1=
129         declare "$3=$(echo "${!3}" | tr ' ' $'\n')";
130         declare "$5=$(echo "${!5}" | tr ' ' $'\n')";
131 #        echo "${!5}"
132 #        echo "${!3}"
133         while read data; do
134             data=${data#< }
135             # save to array.
136             declare -g $1+="${data}"$'\n'
137         done < <(echo "${!5}" | diff --from-file=<(echo "${!3}") - | grep -e "<";)
138                 declare -g $1+="${!1%$'\n'}"
139     fi
142 diffinc ()
144         local vname=${1##*/}
145         vname=${vname%.*}
146         vname=${vname//[^[:alnum:]_]/_}
147         
148         #
149         # declare var before source, or it will be append to varlist_xxx.
150         #
151         declare funclist1_$vname=""
152         declare funclist2_$vname=""
153         declare varlist1_$vname=""
154         declare varlist2_$vname=""
155         declare aliaslist1_$vname=""
156         declare aliaslist2_$vname=""
157         
158         #
159         # getting func/var/alias list before
160         #
161         eval funclist1_$vname=\"\$\(get_func_list\)\"
162         eval varlist1_$vname=\"\$\(get_var_list\)\"
163         eval aliaslist1_$vname=\"\$\(get_alias_list\)\"
164         
165         #
166         # file source.
167         #
168 #       echo include $1
169         inc $1
170         
171         #
172         # getting func/var/alias list after
173         #
174         eval funclist2_$vname=\"\$\(get_func_list\)\"
175         eval varlist2_$vname=\"\$\(get_var_list\)\"
176         eval aliaslist2_$vname=\"\$\(get_alias_list\)\"
177         
178         #
179         # diff list between before and after.
180         #
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
184         
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>
193 loadimi ()
195         local data=
196         local bak=
197         local idx=
198         local vlist=
199         local flag=""
200         local tmp=
201         local i=
202         local pwd=$PWD
203         local file=$(basename $1)
204         local cnt=$inc_lvl
206         test -z "$1" || test ! -f "$1" && return
208         # for debugging.
209         while test "$cnt" -gt 0 ; do
210                 inc_info "  "
211                 : $((cnt--))
212         done
213         inc_info "loadimi ($@)\n"
215         : $((inc_lvl++))
216         
217         # it's normal if file is not exist.
218         test ! -e "${1}" && 
219                 inc_dbgout "parameter file($1) is not exist.\n" && 
220                 unset $name && 
221                 return 0
222         
223         test "`echo "$1" | grep -o -P '([^\.]*)' | tail -n 1`" != "imi" && 
224                 err "specified file $1 is not .imi file.\n" && 
225                 unset $name && 
226                 return 1
227         
228         cd $(dirname $1)
230         if test -e "$file" ; then
231                 # it need -r param to receive '\'.
232                 while read -r data; do
233                         : $(( i++ ))
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:]]*[#].*/}"
240                         fi
241                         
242                         # ignore [xxx] section setting, use it as an ini file.
243                         [[ ${data} =~ ^[[:space:]]*\[ ]] && continue
244                         
245                         # ignore [xxx] section setting, use it as an ini file.
246                         [[ ${data} =~ ^[[:space:]]*$ ]] && continue
247                         
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
257                                 tmp="${data}"$'\n'
258 #                               [[ ! "${data}" =~ ')' ]] && continue
259                                 data="$(echo "$data" | grep -zoE "^.*[=][\(]([^\$\(]*(['][^']['])*|([\"][^\"]*[\"])|([\`][^\`]*[\`])|([\$][\(][^\)]*[\)])*)*[\)]")"
260                                 if [[ -z $data ]]; then
261                                         # TBD: array grep
262                                         # '' string recognizing.
263                                         idx=0
264                                         while read data; do
265                                                 # [[ -z $tmp ]] && break
266                                                 tmp+="$data"$'\n'
267                                                 if [[ "$data" =~ ')' ]]; then
268                                                         data="$(echo "$tmp" | grep -zoE "^.*[=][\(]([^\$\(]*(['][^']['])*|([\"][^\"]*[\"])|([\`][^\`]*[\`])|([\$][\(][^\)]*[\)])*)*[\)]")"
269                                                         [[ -n $data ]] && break
270                                                 fi
271                                                 : $((idx++))
272                                                 [[ "$idx" -gt 1000 ]] && echo "[error] array defination longer then 1000." >&2 && exit -1
273                                         done
274                                         #"[\=][\(]([^']*['][^']*['])*\)"
275                                         eval "$tmp"
276                                         continue
277                                 fi
278                         fi
279                         
280                         if [[ "${data}" =~ =\" ]]; then
281                                 tmp="${data}"$'\n'
282 #                               [[ ! "${data}" =~ ')' ]] && continue
283                                 data="$(echo "$data" | grep -zoE "^.*[=][\"](.*(['][^']['])*|([\"][^\"][\"])*|([\`][^\`][\`])*.*)*[\"]")"
284                                 if [[ -z $data ]]; then
285                                         # TBD: array grep
286                                         # '' string recognizing.
287                                         idx=0
288                                         while read data; do
289                                                 # [[ -z $tmp ]] && break
290                                                 tmp+="$data"$'\n'
291                                                 if [[ "$data" =~ '"' ]]; then
292                                                         data="$(echo "$tmp" | grep -zoE "^.*[=][\"](.*(['][^']['])*|([\"][^\"][\"])*|([\`][^\`][\`])*.*)*[\"]")"
293                                                         [[ -n $data ]] && break
294                                                 fi
295                                                 : $((idx++))
296                                                 [[ "$idx" -gt 1000 ]] && echo "[error] array defination longer then 1000." >&2 && exit -1
297                                         done
298                                         #"[\=][\(]([^']*['][^']*['])*\)"
299                                         eval "$tmp"
300                                         continue
301                                 fi
302                         fi
303                         
304                         if [[ "${data}" =~ =\' ]]; then
305                                 tmp="${data}"$'\n'
306 #                               [[ ! "${data}" =~ ')' ]] && continue
307                                 data="$(echo "$data" | grep -zoE "^.*[=][\'].*[\']")"
308                                 if [[ -z $data ]]; then
309                                         # TBD: array grep
310                                         # '' string recognizing.
311                                         idx=0
312                                         while read data; do
313                                                 # [[ -z $tmp ]] && break
314                                                 tmp+="$data"$'\n'
315                                                 if [[ "$data" =~ \' ]]; then
316                                                         data="$(echo "$tmp" | grep -zoE "^.*[=][\'].*[\']")"
317                                                         [[ -n $data ]] && break
318                                                 fi
319                                                 : $((idx++))
320                                                 [[ "$idx" -gt 1000 ]] && echo "[error] array defination longer then 1000." >&2 && exit -1
321                                         done
322                                         #"[\=][\(]([^']*['][^']*['])*\)"
323                                         eval "$tmp"
324                                         continue
325                                 fi
326                         fi
327                         
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'
335                                                 data="$tmp"
336                                         else
337                                                 tmp+="${data}"$'\n'
338                                         fi
339                                 fi
340                                 
341                                 if [[ "${data}" =~ ')' ]]; then
342                                         flag=""
343                                 else
344                                         continue
345                                 fi
346                         fi
347                         
348                         [[ ! "$data" =~ '=' ]] && echo -e "\033[1m\033[31m[error][$file:line$i]\033[0m: '$data' not a valid assign statement." && exit -1
349                         
350                         inc_dbgout "data=\"${data}\"\n"
351                         
352                         if [[ ${data} =~ _EVL ]]; then
353                                 # define _EVL
354                                 eval declare -g "${data}"
356                                 # define without _EVL
357                                 data="${data/_EVL=/=}"
358                                 #
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.
362                                 #
363                                 data="${data//\"/\\\"}"
364 #                               data="${data//\'/\\\'}"
365 #                               inc_dbgout "=== ${data}\n"
366                                 # \$\'\\\"\"\\\n\\\"\"\'
367                                 eval "$(eval echo \"${data}\")"
368                         else
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
372                                 eval "${data}"
373                                 #$'"\n"'
374                         fi
375                         
376                         # backup var name.
377                         tmp="$data"
378                         local dqcnt=0
379                         local qcnt=0
380                         local charcnt=0
381                         while [[ "${tmp}" =~ '=' ]]; do
382                                 bak="${tmp%=*}"
383                                 bak2="${tmp##*=}"
384                                 [[ ${bak: -1,1} == '+' ]] && bak="${bak:0:-1}"
385                                 tmp="$bak"
387 #                               [[ "$abc" =~ (\=.*\=.*)*(\=.*) && $(($(expr length ${abc//[^=]/})%2)) == 1 ]] && echo bbb ${BASH_REMATCH[@]}
388 #                               [[ "${tmp##*=}" =~ '"'{1,3,2} ]] && tmp="$bak" && continue
390                                 #
391                                 # matching quote
392                                 #
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;
402                                 break
403                         done
404                         data="$tmp"
405                         
406                         # generate var in auto by _EVL suffix.
407                         if [[ "${data}" =~ _EVL$ ]]; then
408                                 eval "${data}=${!data}"
409                         fi
411                         vlist+="${data}"$'\n'
412 #                       inc_dbgout "vlist+=\"${data}$'\n'\""
413                 done < "$file"
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}\""
422                         done
423                 done <<< "$vlist"
424         else
425                 :
426                 err "list file '$1' is not exist.\n"
427         fi
428         
429         cd $pwd
430         
431         : $((inc_lvl--))
435 # fsyntax: loadlist <list-file-name>
436 # fdesc: load .list file. if not exist, load .lst file.
438 loadlist ()
440         local tmp=
441         local data=
442         local bak=
443         local bak2=
444         local i=0
445         local name=
446         local pwd=$PWD
447         local file=$(basename $1)
448         local cnt=$inc_lvl
450         [[ -z "$1" || ! -f "$1" ]] && return
452         # for debugging.
453         while [[ $cnt -gt 0 ]]; do
454                 inc_info "  "
455                 : $((cnt--))
456         done
457         inc_info -ne "loadlist ($1)\n"
459         : $((inc_lvl++))
460         
461         #
462         # skip nestly loaded file.
463         #
464         name=${1//[^[:alnum:]_]/_}
465         if [[ -v ${name} && ${!name} == Y ]]; then
466 #               echo "file $name loaded nestly."
467 #               exit
468                 return
469         fi
470         declare -g "$name=Y"
472         [[ ${1##*\.} != "lst" && ${1##*\.} != "list" ]] && err "specified file '$1' is not .list or .lst file.\n" && unset $name && return 1
474         cd $(dirname $1)
476         if [[ -e "$file" ]]; then
477                 # it need -r param to receive '\'.
478                 while read -r data; do
479                         : $(( i++ ))
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
486                         fi
487                         
488                         [[ "${data}" =~ '^inc ' || "${data}" =~ '^loadimi ' || "${data}" =~ '^loadlist ' ]] && eval $data && continue;
489             
490                         #
491                         # append '\' for '\"'
492                         #
493                         data="${data//\\\"/\\\\\\\"}"
494                         
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
501                         # get var name.
502                         tmp="$data"
503                         local dqcnt=0
504                         local qcnt=0
505                         local charcnt=0
506                         while [[ "${tmp}" =~ '=' ]]; do
507                                 bak="${tmp%=*}"
508                                 bak2="${tmp##*=}"
509                                 [[ ${bak: -1,1} == '+' ]] && bak="${bak:0:-1}"
510                                 tmp="$bak"
512 #                               [[ "$abc" =~ (\=.*\=.*)*(\=.*) && $(($(expr length ${abc//[^=]/})%2)) == 1 ]] && echo bbb ${BASH_REMATCH[@]}
513 #                               [[ "${tmp##*=}" =~ '"'{1,3,2} ]] && tmp="$bak" && continue
515                                 #
516                                 # matching quote
517                                 #
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;
527                                 break
528                         done
529                         data="$tmp"
531                         bak="$data"
533                         # get var name, and delete redundent "\n".
534                         bak="${bak%=*}"
535                         [[ ${bak: -1:1} == '+' ]] && bak="${bak:0:-1}"
537                         while [[ "${!bak: -1:1}" == $'\n' ]]; do
538                                 eval "$bak=\"\${!bak:0:-1}\""
539                         done
541                         #
542                         # CFLAGS_DEF_LIST_Y+=" -DMACHTYPE=\"$MACHTYPE\" "
543                         # disable '\"' to '\\\"'.
544                         #
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'
549                 done < $file
550         else
551                 :
552 #               err "list file is not exist.\n"
553         fi
555         #
556         # it can be loaded next time un-nestly.
557         #
558         unset $name
560         cd $pwd
561         
562         : $((inc_lvl--))