improve of cmpl.
[bush.git] / tools / scripttest / cataid.shlib
blobfc6d068218285d077d49296f854ff78370b1e768
1 #!/bin/bash
2 ############################################################
3 # source: cataid.shlib
4 # author: CottonCandyOwner(CottonCandyOwner@126.com)
5 # date: 2022-03-23
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
10 # and modify it.
11 ############################################################
12 # note:
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 ############################################################
24 # todoļ¼š
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.
28 # todo: to be tested
31 . shlibinc
33 include stdio.shlib
34 include gplib.shlib
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.
52 cataid_create ()
54 local cata=$1
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
65 shlib_cataid_init ()
67 # save exlude id when compare id number to skip some id for function cataid_compare.
68 exclude_id=
71 # catalog id
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]}
80 # index the id code.
83 # use null string name for default cataid.
84 cataid_create
86 #DIR_ERGODIC_IDX=
87 #DIR_ERGODIC_PATH=
88 #DIR_ERGODIC_FILE=
90 CATAID_BEGIN_VAL=1
93 shlib_cataid_init
96 # global variable init function invoking.
97 # invoke GVAR_INIT if it is running at first time.
99 #GVAR_INIT cataid
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.
121 cataid_release ()
123 local cata=$1
125 unset ${cata}_CID_STATE
126 unset ${cata}_CID
127 unset ${cata}_CID_BEGIN
128 unset ${cata}_CID_END
130 unset ${cata}_DIR_DEPTH
135 # fsyntax: cataid_set <cataid_name> <id-var-str>
136 cataid_set ()
138 if [[ -n $2 ]]; then
139 OLD_IFS=$IFS
140 IFS='.'
141 eval ${1}_CID=\( \$2 \)
142 eval ${1}_DIR_DEPTH="\${#${1}_CID[@]}"
143 IFS=$OLD_IFS
148 # fsyntax: cataid_set <cataid_name>
149 cataid_bak ()
151 eval declare -g BAK_CID=( "\${${1}_CID[@]}" )
152 eval declare -g BAK_DIR_DEPTH="\${${1}_DIR_DEPTH}"
156 # fsyntax: cataid_set <cataid_name>
157 cataid_restore ()
159 eval ${1}_CID=\( \"\${BAK_CID[@]}\" \)
160 eval ${1}_DIR_DEPTH="$BAK_DIR_DEPTH"
161 unset BAK_CID
162 unset BAK_DIR_DEPTH
165 cataid_init ()
167 unset ${1}_CID
168 declare -g ${1}_CID
169 set ${1}_DIR_DEPTH=0
173 cataid_get_by_depth ()
175 eval echo \${${1}_CID[\${${1}_DIR_DEPTH]}}
179 cataid_get ()
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.
188 cataid_step_into ()
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++\)\)
202 if [[ -n $2 ]]; then
203 eval ${1}_CID[\${${1}_DIR_DEPTH}]=$2
204 else
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.
213 cataid_step_out ()
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.
223 cataid_jump_to ()
225 local i
226 local id=
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.
235 cataid_step_to ()
237 [[ -n $2 ]] && eval ${1}_CID[\${${1}_DIR_DEPTH}]=$2
241 # fsyntax: cataid_step_over <cataid_name> <id-num>
242 cataid_step_over ()
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>
250 cataid_step_back ()
252 [[ -n $2 ]] && eval ${1}_CID[\${${1}_DIR_DEPTH}]=$2
253 eval ${1}_CID[\${${1}_DIR_DEPTH}]=\$\(\( \${${1}_CID[\${${1}_DIR_DEPTH}]} - 1 \)\)
257 # init cata-id state
258 # fsyntax: cataid_set_init_state <cataid_name> <id-num>
259 cataid_set_init_state ()
261 if [[ -n $2 ]]; then
262 eval ${1}_CID_STATE="$2"
263 else
264 eval ${1}_CID_STATE="lower"
269 # fsyntax: cataid_set_begin_id <cataid_name> <id-var-str>
270 cataid_set_begin_id ()
272 OLD_IFS=$IFS
273 IFS='.'
274 eval ${1}_CID_BEGIN=\( \$$2 \)
275 IFS=$OLD_IFS
279 # fsyntax: cataid_set_end_id <cataid_name> <id-var-str>
280 cataid_set_end_id ()
282 OLD_IFS=$IFS
283 IFS='.'
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"
287 IFS=$OLD_IFS
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 ()
299 local tmp
300 local cnt
301 local name
303 name=${1}_CID_BEGIN
304 [[ -z ${!name} ]] && return 1
305 name=${1}_CID_END
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\"
314 exit
315 return 1
316 elif eval [[ \${${1}_CID_BEGIN[$i]} -eq \${${1}_CID_END[$i]} ]]; then
317 continue
318 else
319 break
321 done
323 return 0
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.
338 cataid_compare ()
340 local i
341 local cnt=
342 local state=
343 local dir_depth=
344 local name=$1
345 local tmp=
347 eval state=\${${name}_CID_STATE}
348 eval dir_depth=\${${name}_DIR_DEPTH}
350 case $state in
351 lower )
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
357 return 0
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
367 return 1
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
378 return 0
379 else
380 return 0
383 inner )
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
400 return 0
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
403 continue
404 else
405 ${name}_CID_STATE="upper"
406 return 2
408 done
410 # not fully equal to end id.
411 return 3
413 upper )
414 return 2
416 esac
421 ##############################
422 # section: file tail
423 ##############################