2 ############################################################
4 # author: CottonCandyOwner(CottonCandyOwner@126.com)
6 ############################################################
7 # Copyright (C) 2022- Free Software Foundation, Inc.
8 # This configure script is free software; the Free Software
9 # Foundation gives unlimited permission to copy, distribute
11 ############################################################
13 # this file is a part of catalog.shlib in catalog id
14 # operation. id is an digital id for catalog structure, it
15 # define functions step, stepover, stepinto, stepout, get,
16 # set, and so on, to walk throw the id tree like an anty.
17 # catalog id can be bind with dir/file name id prefix,
18 # and those code is moved to catadir.shlib.
20 ############################################################
25 # @ modify prefix CATA_ID_ to <XXX>_CID_, and change code in relative
26 # functions. use create catalog id function to create/release a catalog
27 # id. so, thare are more then one catalog id can work in a time.
38 ##############################
39 # section: public comment info
40 ##############################
44 ##############################
45 # section: variable define
46 ##############################
49 # fsyntax: cataid_create <cataid-name>
50 # fdesc: create variables about a cataid object.
56 declare -g ${cata}_CID_STATE
="lower"
57 declare -g -a ${cata}_CID
=""
58 declare -g -a ${cata}_CID_BEGIN
=""
59 declare -g -a ${cata}_CID_END
=""
61 declare -g ${cata}_DIR_DEPTH
=0
67 # save exlude id when compare id number to skip some id for function cataid_compare.
72 # it is used to label catalog structrue.
73 # it provide a set of functions to walkthrough in catalog structrue
74 # by cataid_step_xxx().
75 # and it use cataid_compare() to get the state of XXX_CID.
76 # the begin id can be rollup and rollback, to change the range.
78 # a catalog id is consisted by several digital id code. developer use
79 # cataid_get() to get the whole id string, and access ${XXX_CID[$i]}
83 # use null string name for default cataid.
96 # global variable init function invoking.
97 # invoke GVAR_INIT if it is running at first time.
102 ##############################
103 # section: private function
104 ##############################
108 ##############################
109 # section: public function
110 ##############################
113 # cataid can be create/release dynamically.
114 # the default cataid is named null string.
118 # fsyntax: cataid_release <cataid-name>
119 # fdesc: release variables about a cataid object.
125 unset ${cata}_CID_STATE
127 unset ${cata}_CID_BEGIN
128 unset ${cata}_CID_END
130 unset ${cata}_DIR_DEPTH
135 # fsyntax: cataid_set <cataid_name> <id-var-str>
141 eval ${1}_CID
=\
( \
$2 \
)
142 eval ${1}_DIR_DEPTH="\${#${1}_CID[@]}"
148 # fsyntax: cataid_set <cataid_name>
151 eval declare -g BAK_CID=( "\
${${1}_CID
[@
]}" )
152 eval declare -g BAK_DIR_DEPTH="\
${${1}_DIR_DEPTH
}"
156 # fsyntax: cataid_set <cataid_name>
159 eval ${1}_CID=\( \"\${BAK_CID[@]}\" \)
160 eval ${1}_DIR_DEPTH="$BAK_DIR_DEPTH"
173 cataid_get_by_depth ()
175 eval echo \${${1}_CID[\${${1}_DIR_DEPTH]}}
181 eval echo \${${1}_CID[@]} | tr ' ' '.'
185 # fsyntax: cataid_step_into <cataid_name> <id-num>
186 # fdesc: step into the specified id, new sub-id is 0.
190 [[ -n $2 ]] && eval ${1}_CID[\${${1}_DIR_DEPTH}]=$2
191 eval : \$\(\(${1}_DIR_DEPTH++\)\)
192 eval ${1}_CID[\${${1}_DIR_DEPTH}]=$CATAID_BEGIN_VAL
196 # fsyntax: cataid_step_into_id <cataid_name> <id-num>
197 # fdesc: step into the specified id, new sub-id is 0.
199 cataid_step_into_id ()
201 eval : \$\(\(${1}_DIR_DEPTH++\)\)
203 eval ${1}_CID[\${${1}_DIR_DEPTH}]=$2
205 eval ${1}_CID[\${${1}_DIR_DEPTH}]=$CATAID_BEGIN_VAL
210 # fsyntax: cataid_step_out <cataid_name>
211 # fdesc: step out of current catalog id.
215 eval unset ${1}_CID[\${${1}_DIR_DEPTH}]
216 eval : \$\(\(--${1}_DIR_DEPTH\)\)
220 # fsyntax: cataid_jump_to <cataid_name> <id-num>
221 # todo: this func to be modified, step_into or step_over automatically
222 # judged by function it self.
228 [[ -z $2 ]] && return
232 # fsyntax: cataid_step <cataid_name> <id-num>
233 # todo: this func to be modified, step_into or step_over automatically
234 # judged by function it self.
237 [[ -n $2 ]] && eval ${1}_CID[\${${1}_DIR_DEPTH}]=$2
241 # fsyntax: cataid_step_over <cataid_name> <id-num>
244 [[ -n $2 ]] && eval ${1}_CID[\${${1}_DIR_DEPTH}]=$2
245 eval ${1}_CID[\${${1}_DIR_DEPTH}]=\$\(\( \${${1}_CID[\${${1}_DIR_DEPTH}]} + 1 \)\)
249 # fsyntax: cataid_step_back <cataid_name> <id-num>
252 [[ -n $2 ]] && eval ${1}_CID[\${${1}_DIR_DEPTH}]=$2
253 eval ${1}_CID[\${${1}_DIR_DEPTH}]=\$\(\( \${${1}_CID[\${${1}_DIR_DEPTH}]} - 1 \)\)
258 # fsyntax: cataid_set_init_state <cataid_name> <id-num>
259 cataid_set_init_state ()
262 eval ${1}_CID_STATE="$2"
264 eval ${1}_CID_STATE="lower
"
269 # fsyntax: cataid_set_begin_id <cataid_name> <id-var-str>
270 cataid_set_begin_id ()
274 eval ${1}_CID_BEGIN=\( \$$2 \)
279 # fsyntax: cataid_set_end_id <cataid_name> <id-var-str>
284 eval ${1}_CID_END=\( "\$
$2" \)
285 # todo: dbgoutd running in exception
286 # eval dbgoutd "${1}_CID_END=(\${${1}_CID_END[@]})(\${#${1}_CID_END[@]})\n"
291 # fsyntax: cataid_begin_end_chk <cataid_name>
292 # fdesc: check the begin id if it is less then end id.
293 # freturn: return value is chk result.
294 # 0, begin id is littler then or equal to end id.
295 # 1, begin id is grater then end id.
297 cataid_begin_end_chk ()
304 [[ -z ${!name} ]] && return 1
306 [[ -z ${!name} ]] && return 1
308 eval cnt=\${#${1}_CID_BEGIN[@]}
309 for (( i=0; i<cnt; i++)); do
310 eval [[ -z \${${1}_CID_END[$i]} ]] && break
312 if eval [[ \${${1}_CID_BEGIN[$i]} -gt \${${1}_CID_END[$i]} ]]; then
313 eval err \"${1}_CID_BEGIN\(\${${1}_CID_BEGIN[$i]}\) is larger than ${1}_CID_END\(\${${1}_CID_END[$i]}\).\\\n\"
316 elif eval [[ \${${1}_CID_BEGIN[$i]} -eq \${${1}_CID_END[$i]} ]]; then
328 # fsyntax: cataid_compare <cataid_name> <curr_id>
329 # fdesc: compare current catalog id with begin id and end id,
330 # to get the range, lower then begin, or in the range between
331 # begin and end, or upper then end id. it helps developer to
332 # work between begin id and end id.
333 # freturn: the return vslue reflact id state.
334 # 0, current catalog id is in range between begin id and end id.
335 # 1, current id should be skipped.
336 # 2, current id is greater then end id, and stop ergodicing catalog.
347 eval state=\${${name}_CID_STATE}
348 eval dir_depth=\${${name}_DIR_DEPTH}
352 if eval [[ -z \${${name}_CID_BEGIN} ]]; then
353 eval ${name}_CID_STATE="inner
"
354 # exclude some catalog id. it's the same below.
355 [[ "$exclude_id" =~ " $
(cataid_get
${name} |
tr ' ' '.') " ]] && return 1
361 # littler then begin-id, skip, return 1.
362 # greater then or equal to begin-id, normal running, return 0.
363 # fully equal to begin id, switch state to 'inner', return 0.
364 # not fully equal to begin id, just only return 0.
366 if eval [[ \${${name}_CID_BEGIN[$dir_depth]} -gt \${${name}_CID[$dir_depth]} ]]; then
368 elif eval [[ \${${name}_CID_BEGIN[$dir_depth]} -eq \${${name}_CID[$dir_depth]} ]]; then
369 if eval [[ \$\(\( ${name}_DIR_DEPTH+1 \)\) == \${#${name}_CID_BEGIN[@]} ]]; then
370 # dbgout "${name}_CID_STATE
"
371 # eval ${name}_CID_STATE="inner
"
373 eval ${name}_CID_STATE="inner
"
375 [[ "$exclude_id" =~ " $
(cataid_get
${name} |
tr ' ' '.') " ]] && return 1
384 # exclude some catalog id. it's the same below.
385 [[ "$exclude_id" =~ " $
(cataid_get
${name} |
tr ' ' '.') " ]] && return 1
387 # if no end id limited, it always return 0 to running while end.
388 eval [[ -z \${${name}_CID_END} ]] && return 0
391 # littler then end-id, normal running, return 0.
392 # fully equal to end-id, normal running, return 0.
393 # not fully equal to end id,
394 # greater then end id, switch state to 'upper', return 2.
396 # todo: here report error.
397 eval cnt=\${#${name}_CID[@]}
398 for (( i=0 ; i < cnt ; i++ )); do
399 if eval [[ \${${name}_CID_END[$i]} -gt \${${name}_CID[$i]} ]]; then
401 elif eval [[ "\
${${name}_CID_END[$i]}" == "\${${name}_CID[$i]}" ]]; then
402 eval [[ $\(\( i+1 \)\) == \${#${name}_CID_END[@]} ]] && eval ${name}_CID_STATE="upper
" && return 0
405 ${name}_CID_STATE="upper
"
410 # not fully equal to end id.
421 ##############################
423 ##############################