#!/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 "${@}"