20230322
[shlib.git] / shlib / typedef.shlib
blobe106a8eaa339acec223fdb9d6aefd3e2297d047f
1 #!/bin/bash
2 ############################################################
3 # source: typedef.shlib
4 # author: devenkong(18151155@qq.com)
5 # date: 2022-12-02
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 # typedef is referenced from c language. there are no
14 # conception of type in shell script, all variable is string,
15 # except few variable defined as integer. one of the weakness
16 # of shell script is variable accessing of different type,
17 # and some structured data storage and accessing is very weak.
18 # this file implement structure type defination, it orgnize
19 # different environment variable as a same object.
21 ############################################################
25 # todo:
26 # @
29 . shlibinc
31 include stdio.shlib
32 include attr.shlib
35 ##############################
36 # section: public comment info
37 ##############################
40 ##############################
41 # section: variable define
42 ##############################
45 ##############################
46 # section: private function
47 ##############################
51 ##############################
52 # section: public function
53 ##############################
56 # fsyntax: attr_get_var_name var
57 # fdesc: it does not process domain only, the sub-relationship can be a member,
58 # or pointer's member, or reference var's member.
60 attr_get_name ()
62 local domain=
63 local name=$1
64 local vname=
66 # TBD: if there is a '@' pfx, use as an attr, or treat as a env var first.
67 if [[ "${1:0:1}" == '@' ]]; then
68 name="${1:1}"
69 else
70 name="${1}"
73 # try to match in domain list.
74 if [[ ${name:0:2} != '::' ]]; then
75 # domain list.
76 for domain in $attr_module_domain "" $attr_curr_domain $attr_domain_list; do
77 if [[ -n $domain ]]; then
78 vname="${domain}::$name"
79 vname="ATTR__${vname//::/__}"
80 else
81 vname="ATTR__${name//::/__}"
83 # dbgoutd "n.${vname}=${!vname}\n"
84 [[ -v "${vname}" ]] && echo "$vname" && return
85 done
87 # directly invoking to compactive to general using.
88 [[ -v "${name}" ]] && echo "$name" && return
89 else
90 # translate attr var name
91 # dbgoutd "name=$name\n"
92 vname=${name:2}
93 vname="ATTR__${vname//::/__}"
94 # dbgoutd "vname=$vname\n"
96 # dbgoutd "1.${vname}=${!vname}\n"
97 [[ -v "$vname" ]] && echo "$vname" && return
103 # fsync: type_name <tname>
104 # fdesc: get the env name of type and member with just only __T__ type.
105 type_name ()
107 local tname=
108 local vname=
109 local opt=
111 tname="${1}"
112 vname="${2//::/__}"
114 [[ ${vname:0:2} == '::' ]] && vname=${vname:2} # && return 0
116 dbgoutd "tname=$tname\n"
117 tname="$(attr_get_vname $tname)"
118 [[ -z $tname ]] && tname="$1"
120 echo -ne "${tname}"
122 # tname="${tname//__T__/__TD__}"
123 # declare -g $opt "${tname}${vname}"
127 # fsync: type_create <tname>
128 # fdesc: create type name attr var.
129 type_create ()
131 local name="$@"
132 local opt=
134 if [[ ${name:0:2} =~ "-A" ]]; then
135 name=${name#\ -A}
136 opt="-A"
137 shift
140 if [[ ${name:0:2} != '::' && -n ${attr_module_domain} ]]; then
141 # current module domain define.
142 dbgoutd "type_create T::${attr_module_domain}::$@\n"
143 name="T::${attr_module_domain}::$@"
144 else
145 # global define
146 dbgoutd "type_create T::$@\n"
147 name="T::$@"
149 attr_create $opt "$name"
153 # T::类型名称属性。
154 # TD::类型定义中的dir定义,通常是一个struct定义。
155 # TV::类型定义中的var定义。
156 # TF::类型定义中的func定义。
157 # TR::类型定义中的ref定义,变量中保存引用的变量或函数的类型和名称。类似于
158 # 指针的概念,包含类型信息。
159 # V::变量名称属性。
160 # VD::变量中的dir定义。
161 # VV::变量中的var定义。
162 # VF::变量中的func定义。
163 # VR::变量中的ref定义。
167 # fsync: type_dir_create <tname>
168 # fdesc: delete type type define.
169 type_delete ()
171 local name="$@"
172 local opt=
174 if [[ ${name:0:2} != '::' && -n ${attr_module_domain} ]]; then
175 # current module domain define.
176 dbgoutd "type_create T::${attr_module_domain}::$@\n"
177 name="T::${attr_module_domain}::$@"
178 else
179 # global define
180 dbgoutd "type_create T::$@\n"
181 name="T::$@"
183 attr_delete "$name"
187 # fsync: type_dir <tname> <dname>
188 # fdesc: create dir attr var in a type.
189 type_dir ()
191 local name="$1"
192 local opt=
194 if [[ ${name:0:2} != '::' && -n ${attr_module_domain} ]]; then
195 # current module domain define.
196 dbgoutd "type_create T::${attr_module_domain}::$1\n"
197 name="T::${attr_module_domain}::$1"
198 else
199 # global define
200 dbgoutd "type_create T::$1\n"
201 name="T::$1"
203 attr_set "$name" += "$2
208 # fsync: type_var <tname> <vname>
209 # fdesc: create var attr var in a type.
210 type_var ()
212 local name="$1"
213 local opt=
215 if [[ ${name:0:2} != '::' && -n ${attr_module_domain} ]]; then
216 # current module domain define.
217 dbgoutd "type_create T::${attr_module_domain}::$1\n"
218 name="T::${attr_module_domain}::$1"
219 else
220 # global define
221 dbgoutd "type_create T::$1\n"
222 name="T::$1"
224 attr_set "$name" += "var $2
229 # fsync: type_func <tname> <fname>
230 # fdesc: create func attr var in a type.
231 type_func ()
233 local name="$1"
234 local opt=
236 if [[ ${name:0:2} != '::' && -n ${attr_module_domain} ]]; then
237 # current module domain define.
238 dbgoutd "type_create T::${attr_module_domain}::$1\n"
239 name="T::${attr_module_domain}::$1"
240 else
241 # global define
242 dbgoutd "type_create T::$1\n"
243 name="T::$1"
245 attr_set "$name" += "$2
250 # fsync: type_ref <tname> <fname> <rname>
251 # fdesc: create func attr var in a type.
252 type_ref ()
254 local name="$1"
255 local opt=
257 if [[ ${name:0:2} != '::' && -n ${attr_module_domain} ]]; then
258 # current module domain define.
259 dbgoutd "type_create T::${attr_module_domain}::$1\n"
260 name="T::${attr_module_domain}::$1"
261 else
262 # global define
263 dbgoutd "type_create T::$1\n"
264 name="T::$1"
266 attr_set "$name" += "$2
271 # fsync: type_static_dir <tname> <dname>
272 # fdesc: create dir attr var in a type.
273 type_static_dir ()
275 local name="$1"
276 local opt=
278 if [[ ${name:0:2} != '::' && -n ${attr_module_domain} ]]; then
279 # current module domain define.
280 dbgoutd "type_static_create T::${attr_module_domain}::$1::$2\n"
281 name="TD::${attr_module_domain}::$1::$2"
282 else
283 # global define
284 dbgoutd "type_static_create T::$1::$2\n"
285 name="TD::$1::$2"
287 attr_create "$name"
291 # fsync: type_static_var <tname> <vname>
292 # fdesc: create var attr var in a type.
293 type_static_var ()
295 local name="$1"
296 local opt=
298 if [[ ${name:0:2} != '::' && -n ${attr_module_domain} ]]; then
299 # current module domain define.
300 dbgoutd "type_static_create T::${attr_module_domain}::$1::$2\n"
301 name="TV::${attr_module_domain}::$1::$2"
302 else
303 # global define
304 dbgoutd "type_static_create T::$1::$2\n"
305 name="TV::$1::$2"
307 attr_create "$name"
311 # fsync: type_static_func <tname> <fname>
312 # fdesc: create func attr var in a type.
313 type_static_func ()
315 local name="$1"
316 local opt=
318 if [[ ${name:0:2} != '::' && -n ${attr_module_domain} ]]; then
319 # current module domain define.
320 dbgoutd "type_static_create T::${attr_module_domain}::$1::$2\n"
321 name="TF::${attr_module_domain}::$1::$2"
322 else
323 # global define
324 dbgoutd "type_static_create T::$1::$2\n"
325 name="TF::$1::$2"
327 attr_create "$name"
331 # fsync: type_static_ref <tname> <fname> <rname>
332 # fdesc: create func attr var in a type.
333 type_static_ref ()
335 local name="$1"
336 local opt=
338 if [[ ${name:0:2} != '::' && -n ${attr_module_domain} ]]; then
339 # current module domain define.
340 dbgoutd "type_static_create T::${attr_module_domain}::$1::$2\n"
341 name="TR::${attr_module_domain}::$1::$2"
342 else
343 # global define
344 dbgoutd "type_static_create T::$1::$2\n"
345 name="TR::$1::$2"
347 attr_create "$name"
351 # fsync: struct <sname> { ... } <tname>
352 # fdesc: create structure type.
353 struct ()
355 local xxx=
357 # TBD:
362 # fsync: typedef struct <sname> { ... } <tname>
363 # fdesc: create var name attr var in a type.
364 typedef ()
366 if [[ "$1" == 'struct' ]]; then
367 shift
368 struct "$@"
369 elif [[ false ]]; then
371 else
377 # fsync: entity_define <tname> <vname>
378 # fdesc: create var entity with a type.
379 entity_define ()
381 for i in $(attr_listofvar $1); do
382 declare -g $i__$1
383 done
387 # fsync: entity_release <vname>
388 # fdesc: release var entity.
389 entity_release ()
391 attr_delete "$1"
395 # fsync: entity_dir <ename> <dname>
396 # fdesc: append dir in a entity.
397 entity_dir ()
399 local tname=
400 local opt=
402 if [[ ${1:0:2} == "-A" ]]; then
403 shift
404 opt="-A"
407 tname="$(type_name $1 $2)"
408 [[ -z $tname ]] && return
409 tname="${tname//__T__/__VD__}"
410 declare -g $opt "${tname}"
414 # fsync: entity_var <ename> <vname>
415 # fdesc: append var in a entity.
416 entity_var ()
417 { local tname=
418 local opt=
420 if [[ ${1:0:2} == "-A" ]]; then
421 shift
422 opt="-A"
425 tname="$(type_name $1 $2)"
426 [[ -z $tname ]] && return
427 tname="${tname//__T__/__VD__}"
428 declare -g $opt "${tname}"
432 # fsync: entity_func <tname> <fname>
433 # fdesc: create func attr var in a type.
434 entity_func ()
436 local tname=
437 local opt=
439 if [[ ${1:0:2} == "-A" ]]; then
440 shift
441 opt="-A"
444 tname="$(type_name $1 $2)"
445 [[ -z $tname ]] && return
446 tname="${tname//__T__/__TF__}"
447 declare -g $opt "${tname}"
451 # fsync: entity_ref <tname> <fname> <rname>
452 # fdesc: create func attr var in a type.
453 entity_ref ()
455 local tname=
456 local opt=
458 if [[ ${1:0:2} == "-A" ]]; then
459 shift
460 opt="-A"
463 tname="$(type_name $1 $2)"
464 [[ -z $tname ]] && return
465 tname="${tname//__T__/__TR__}"
466 declare -g $opt "${tname}"
470 # fsync: entity <sname> { .. } <vname>
471 # fdesc: it define a variable with structue.
472 entity ()
474 # TBD:
479 ##############################
480 # section: file tail
481 ##############################