api documentation for ec_parse
[protos/libecoli.git] / libshell / libecoli.sh
1 # SPDX-License-Identifier: BSD-3-Clause
2 # Copyright 2018, Olivier MATZ <zer0@droids-corp.org>
3
4 # Source this file from a shell script to add libecoli helpers.
5
6 # use a safer version of echo (no option)
7 ec_echo()
8 {
9         printf "%s\n" "$*"
10 }
11
12 ec_debug()
13 {
14         ec_echo "$@" >&2
15 }
16
17 # $1: node sequence number (ex: ec_node4)
18 ec_pnode_get_first_child()
19 {
20         local first_child=${1}_first_child
21         ec_echo $(eval 'ec_echo ${'$first_child'}')
22 }
23
24 # $1: node sequence number (ex: ec_node4)
25 ec_pnode_get_next()
26 {
27         local next=${1}_next
28         ec_echo $(eval 'ec_echo ${'$next'}')
29 }
30
31 # $1: node sequence number (ex: ec_node4)
32 ec_pnode_iter_next()
33 {
34         local seq=${1#ec_node}
35         seq=$((seq+1))
36         local next=ec_node${seq}
37         if [ "$(ec_pnode_get_id $next)" != "" ]; then
38                 ec_echo $next
39         fi
40 }
41
42 # $1: node sequence number (ex: ec_node4)
43 ec_pnode_get_id()
44 {
45         local id=${1}_id
46         ec_echo $(eval 'ec_echo ${'$id'}')
47 }
48
49 # $1: node sequence number (ex: ec_node4)
50 ec_pnode_get_strvec_len()
51 {
52         local strvec_len=${1}_strvec_len
53         ec_echo $(eval 'ec_echo ${'$strvec_len'}')
54 }
55
56 # $1: node sequence number (ex: ec_node4)
57 # $2: index in strvec
58 ec_pnode_get_str()
59 {
60         if [ $# -ne 2 ]; then
61                 return
62         fi
63         local str=${1}_str${2}
64         ec_echo $(eval 'ec_echo ${'$str'}')
65 }
66
67 # $1: node sequence number (ex: ec_node4)
68 # $2: node id (string)
69 ec_pnode_find_first()
70 {
71         if [ $# -ne 2 ]; then
72                 return
73         fi
74         local node_seq=$1
75         while [ "$node_seq" != "" ]; do
76                 local id=$(ec_pnode_get_id $node_seq)
77                 if [ "$id" = "$2" ]; then
78                         ec_echo $node_seq
79                         return 0
80                 fi
81                 node_seq=$(ec_pnode_iter_next $node_seq)
82         done
83 }