summaryrefslogtreecommitdiffstats
path: root/research/research2.sh
blob: 99084953c9b0ce515338cc7019cb21186cc67719 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/sh

################################################################################
# shld
################################################################################
__last_tu=''

static()
{
	local arg=
	local var=
	local val=
	local exp=

	for arg in "${@}"; do
		case "${arg}" in
			*=*)
				var="${arg%%=*}"
				val="$(printf "${arg#*=}" | \
					sed "s|'|'\\\\''|g")"
				;;
			*)
				var="${arg}"
				val=''
				;;
		esac
		# Sanitize variable name.
		case "${var}" in
			[!A-Za-z_]* | *[!0-9A-Za-z_]*)
				printf 'Syntax error: %s %s\n' \
					'illegal static variable name:' \
					"${var}" >&2
				;;
		esac
		# Set up static variable assignments to be evaluated on scope
		# change.
		exp="__${__tu}_static_var_vals="
		exp="${exp}\"\${__${__tu}_static_var_vals} ${var}='${val}'\""
		eval "${exp}"
		# Set up list of static variables to be cleaned up on scope
		# change.
		exp="__${__tu}_static_vars=\"\${__${__tu}_static_vars} ${var}\""
		eval "${exp}"
		# Set up static variables for current scope.
		eval "${var}='${val}'"
	done

	# Clean up static variables from previous scope.
	if ! [ "x${__last_tu}" = 'x' ]; then
		eval "unset \${__${__last_tu}_static_vars}"
	fi
	__last_tu="${__tu}"
}

__set_up_static_vars()
{
	local tu="${1}"

	# Clean up static variables from previous scope.
	if ! [ "x${__last_tu}" = 'x' ]; then
		eval "unset \${__${__last_tu}_static_vars}"
	fi
	__last_tu="${tu}"

	# Set up static variables for specified scope.
	eval "$(eval printf '%s' "\"$(eval printf '%s' \
		"'\${__${tu}_static_var_vals}'")\"")"
}

################################################################################
# TU 0
################################################################################
__tu='tu0'
__tu0_locals=''

static static_var0='tu0 static_var0'

tu0_fn0()
{ __set_up_static_vars 'tu0'
	echo "tu0"
	echo "\tstatic_var0: $static_var0"
	echo "\tstatic_var1: $static_var1"
}

################################################################################
# TU 1
################################################################################
__tu='tu1'
__tu1_locals=''

static static_var0='tu1 static_var0'
static static_var1='tu1 static_var1'

tu1_fn0()
{ __set_up_static_vars 'tu1'
	echo "tu1"
	echo "\tstatic_var0: $static_var0"
	echo "\tstatic_var1: $static_var1"
}

################################################################################
# TU 2
################################################################################
main()
{
	tu0_fn0
	tu1_fn0
	tu0_fn0
}

main "${@}"