users/daste/tiano

changeset 2:f4d166b8ae73

Debugging ok for real, variable storing and restoring
author Stefano D'Angelo <zanga.mail@gmail.com>
date Tue Oct 06 02:17:14 2009 +0300 (2009-10-06)
parents d50bdbad7125
children 1f59d86f46d5
files tiano/checks tiano/tiano
line diff
     1.1 --- a/tiano/checks	Mon Oct 05 21:03:39 2009 +0300
     1.2 +++ b/tiano/checks	Tue Oct 06 02:17:14 2009 +0300
     1.3 @@ -0,0 +1,1 @@
     1.4 +
     2.1 --- a/tiano/tiano	Mon Oct 05 21:03:39 2009 +0300
     2.2 +++ b/tiano/tiano	Tue Oct 06 02:17:14 2009 +0300
     2.3 @@ -75,25 +75,10 @@
     2.4  tiano_valid_id()
     2.5  {
     2.6  	tiano_debug_func_begin "tiano_valid_id" 1 1 $#
     2.7 -	
     2.8 -	case "$1" in
     2.9 -		"")
    2.10 -			tiano_debug_func_end
    2.11 -			return 1
    2.12 -			;;
    2.13 -		[!_a-zA-Z]*)
    2.14 -			tiano_debug_func_end
    2.15 -			return 1
    2.16 -			;;
    2.17 -		*[!_a-zA-Z0-9]*)
    2.18 -			tiano_debug_func_end
    2.19 -			return 1
    2.20 -			;;
    2.21 -		*)
    2.22 -			tiano_debug_func_end
    2.23 -			return 0
    2.24 -			;;
    2.25 -	esac
    2.26 +	tiano_debug_func_end
    2.27 +
    2.28 +	_tiano_valid_id "$1"
    2.29 +	return $?
    2.30  }
    2.31  
    2.32  # Checks whether $1 is valid decimal unsigned integer (0 prefixes not allowed).
    2.33 @@ -102,29 +87,10 @@
    2.34  tiano_valid_dec_uint()
    2.35  {
    2.36  	tiano_debug_func_begin "tiano_valid_dec_uint" 1 1 $#
    2.37 +	tiano_debug_func_end
    2.38  
    2.39 -	case "$1" in
    2.40 -		"")
    2.41 -			tiano_debug_func_end
    2.42 -			return 1
    2.43 -			;;
    2.44 -		0)
    2.45 -			tiano_debug_func_end
    2.46 -			return 0
    2.47 -			;;
    2.48 -		[!1-9]*)
    2.49 -			tiano_debug_func_end
    2.50 -			return 1
    2.51 -			;;
    2.52 -		*[!0-9]*)
    2.53 -			tiano_debug_func_end
    2.54 -			return 1
    2.55 -			;;
    2.56 -		*)
    2.57 -			tiano_debug_func_end
    2.58 -			return 0
    2.59 -			;;
    2.60 -	esac
    2.61 +	_tiano_valid_dec_uint "$1"
    2.62 +	return $?
    2.63  }
    2.64  
    2.65  # Checks whether the value IFS does not contain characters which can be used in
    2.66 @@ -133,17 +99,10 @@
    2.67  tiano_valid_IFS()
    2.68  {
    2.69  	tiano_debug_func_begin "tiano_valid_IFS" 0 0 $#
    2.70 +	tiano_debug_func_end
    2.71  
    2.72 -	case "$IFS" in
    2.73 -		*[_a-zA-Z0-9]*)
    2.74 -			tiano_debug_func_end
    2.75 -			return 1
    2.76 -			;;
    2.77 -		*)
    2.78 -			tiano_debug_func_end
    2.79 -			return 0
    2.80 -			;;
    2.81 -	esac
    2.82 +	_tiano_valid_IFS
    2.83 +	return $?
    2.84  }
    2.85  
    2.86  ###
    2.87 @@ -186,35 +145,32 @@
    2.88  {
    2.89  	[ "$tiano_debug_mode" = "on" ] || return
    2.90  
    2.91 -	[ -z "$_tiano_debug_func_begin_in" ] || return
    2.92 -	_tiano_debug_func_begin_in="yes"
    2.93 -
    2.94  	if [ -n "$_tiano_debug_func_stack" ]; then
    2.95  		_tiano_debug_func_stack="$_tiano_debug_func_stack "
    2.96  	fi
    2.97  	_tiano_debug_func_stack="${_tiano_debug_func_stack}__unknown_function__"
    2.98  
    2.99 -	tiano_debug_check_env
   2.100 +	_tiano_debug_check_env
   2.101  
   2.102  	[ $# -eq 4 ]
   2.103 -	tiano_debug_assert $? "wrong number of arguments when calling tiano_debug_func_begin() (expected: 4, given: $#)"
   2.104 +	_tiano_debug_assert $? "wrong number of arguments when calling tiano_debug_func_begin() (expected: 4, given: $#)"
   2.105  
   2.106 -	tiano_valid_id "$1"
   2.107 -	tiano_debug_assert $? "invalid function identifier \`%s' when calling tiano_debug_func_begin()" "$1"
   2.108 +	_tiano_valid_id "$1"
   2.109 +	_tiano_debug_assert $? "invalid function identifier \`%s' when calling tiano_debug_func_begin()" "$1"
   2.110  
   2.111 -	tiano_valid_dec_uint "$2"
   2.112 -	tiano_debug_assert $? "invalid value as minimum number of arguments \`%s' when calling tiano_debug_func_begin()" "$2"
   2.113 +	_tiano_valid_dec_uint "$2"
   2.114 +	_tiano_debug_assert $? "invalid value as minimum number of arguments \`%s' when calling tiano_debug_func_begin()" "$2"
   2.115  
   2.116  	if [ -n "$3" ]; then
   2.117 -		tiano_valid_dec_uint "$3"
   2.118 -		tiano_debug_assert  $? "invalid value as maximum number of arguments \`%s' when calling tiano_debug_func_begin()" "$3"
   2.119 +		_tiano_valid_dec_uint "$3"
   2.120 +		_tiano_debug_assert  $? "invalid value as maximum number of arguments \`%s' when calling tiano_debug_func_begin()" "$3"
   2.121  
   2.122  		[ $3 -ge $2 ]
   2.123 -		tiano_debug_assert $? "minimum number of arguments is greater than maximum number of arguments when calling tiano_debug_func_begin() (min: $2, max: $3)"
   2.124 +		_tiano_debug_assert $? "minimum number of arguments is greater than maximum number of arguments when calling tiano_debug_func_begin() (min: $2, max: $3)"
   2.125  	fi
   2.126  
   2.127 -	tiano_valid_dec_uint "$4"
   2.128 -	tiano_debug_assert $? "invalid value as number of arguments \`%s' when calling tiano_debug_func_begin()" "$4"
   2.129 +	_tiano_valid_dec_uint "$4"
   2.130 +	_tiano_debug_assert $? "invalid value as number of arguments \`%s' when calling tiano_debug_func_begin()" "$4"
   2.131  
   2.132  	if [ "$_tiano_debug_func_stack" != "${_tiano_debug_func_stack% *}" ]
   2.133  	then
   2.134 @@ -226,14 +182,12 @@
   2.135  
   2.136  	if [ -n $3 ]; then
   2.137  		[ $4 -ge $2 ]
   2.138 -		tiano_debug_assert $? \
   2.139 +		_tiano_debug_assert $? \
   2.140  			"wrong number of arguments (expected: $2+, given: $4)"
   2.141  	else
   2.142  		[ $4 -ge $2 -a $4 -le $3 ]
   2.143 -		tiano_debug_assert $? "wrong number of arguments (expected: $2 to $3, given: $4)"
   2.144 +		_tiano_debug_assert $? "wrong number of arguments (expected: $2 to $3, given: $4)"
   2.145  	fi
   2.146 -
   2.147 -	unset _tiano_debug_func_begin_in
   2.148  }
   2.149  
   2.150  # Pops a function from the function call stack trace and checks that the the
   2.151 @@ -242,11 +196,7 @@
   2.152  {
   2.153  	[ "$tiano_debug_mode" = "on" ] || return
   2.154  
   2.155 -	[ -z "$_tiano_debug_func_begin_in" ] || return
   2.156 -	[ -z "$_tiano_debug_func_end_in" ] || return
   2.157 -	_tiano_debug_func_end_in="yes"
   2.158 -
   2.159 -	tiano_debug_check_env
   2.160 +	_tiano_debug_check_env
   2.161  
   2.162  	[ $# != 0 ] && tiano_debug_fatal "wrong number of arguments when calling tiano_debug_func_end() (expected: 0, given $#)"
   2.163  
   2.164 @@ -256,8 +206,6 @@
   2.165  	else
   2.166  		_tiano_debug_func_stack=""
   2.167  	fi
   2.168 -
   2.169 -	unset _tiano_debug_func_end_in
   2.170  }
   2.171  
   2.172  # Prints an error message on the stderr and writes the `tiano.debug.log' dump
   2.173 @@ -324,10 +272,7 @@
   2.174  	tiano_debug_func_begin "tiano_debug_assert" 2 "" $#
   2.175  	tiano_debug_func_end
   2.176  
   2.177 -	if [ "$1" != 0 ]; then
   2.178 -		shift 1
   2.179 -		tiano_debug_fatal "$@"
   2.180 -	fi
   2.181 +	_tiano_debug_assert "$@"
   2.182  }
   2.183  
   2.184  # Checks whether the environment is in a sane state, which means that:
   2.185 @@ -337,12 +282,9 @@
   2.186  	[ "$tiano_debug_mode" = "on" ] || return
   2.187  
   2.188  	tiano_debug_func_begin "tiano_debug_check_env" 0 0 $#
   2.189 +	tiano_debug_func_end
   2.190  
   2.191 -	tiano_valid_IFS
   2.192 -	tiano_debug_assert $? \
   2.193 -		"IFS variable is set to the invalid value \`%s'" "$IFS"
   2.194 -
   2.195 -	tiano_debug_func_end
   2.196 +	_tiano_debug_check_env
   2.197  }
   2.198  
   2.199  ###
   2.200 @@ -360,14 +302,66 @@
   2.201  ### Thus, they are useful to emulate local scoping or limited lifetime for
   2.202  ### variables, which is not natively supported by the POSIX shell.
   2.203  
   2.204 +# Stores the value of a variable for future restoring.
   2.205 +#
   2.206 +# $1: Variable name.
   2.207  tiano_var_store()
   2.208  {
   2.209 -	echo
   2.210 +	tiano_debug_func_begin "tiano_var_store" 1 1 $#
   2.211 +
   2.212 +	tiano_valid_id "$1"
   2.213 +	tiano_debug_assert $? "invalid identifier \`%s'" "$1"
   2.214 +
   2.215 +	eval _tiano_var_stack_count_$1="\$((\${_tiano_var_stack_count_$1:-0} + 1))"
   2.216 +
   2.217 +	eval _tiano_var_store_name="_tiano_var_stack_plus_$1_\$_tiano_var_stack_count_$1"
   2.218 +	eval $_tiano_var_store_name="\"\${$1+noval}\""
   2.219 +
   2.220 +	eval _tiano_var_store_name="_tiano_var_stack_minus_$1_\$_tiano_var_stack_count_$1"
   2.221 +	eval $_tiano_var_store_name="\"\${$1-noval}\""
   2.222 +
   2.223 +	unset _tiano_var_store_name
   2.224 +
   2.225 +	tiano_debug_func_end
   2.226  }
   2.227  
   2.228 +# Restores the value of a variable which was previously stored.
   2.229 +#
   2.230 +# $1: Variable name.
   2.231  tiano_var_restore()
   2.232  {
   2.233 -	echo
   2.234 +	tiano_debug_func_begin "tiano_var_restore" 1 1 $#
   2.235 +
   2.236 +	tiano_valid_id "$1"
   2.237 +	tiano_debug_assert $? "invalid identifier \`%s'" "$1"
   2.238 +
   2.239 +	eval [ -n \"\$_tiano_var_stack_count_$1\" ]
   2.240 +	tiano_debug_assert $? "no value was stored for variable \`$1'"
   2.241 +
   2.242 +	eval _tiano_var_restore_name="_tiano_var_stack_plus_$1_\$_tiano_var_stack_count_$1"
   2.243 +	eval _tiano_var_restore_val="\"\$$_tiano_var_restore_name\""
   2.244 +	unset $_tiano_var_restore_name
   2.245 +	
   2.246 +	eval _tiano_var_restore_name="_tiano_var_stack_minus_$1_\$_tiano_var_stack_count_$1"
   2.247 +
   2.248 +	if [ -n "$_tiano_var_restore_val" ]; then
   2.249 +		eval $1="\"\$$_tiano_var_restore_name\""
   2.250 +	else
   2.251 +		unset $1
   2.252 +	fi
   2.253 +
   2.254 +	unset $_tiano_var_restore_name
   2.255 +	unset _tiano_var_restore_name
   2.256 +
   2.257 +	eval _tiano_var_stack_count_$1="\$((\$_tiano_var_stack_count_$1 - 1))"
   2.258 +
   2.259 +	eval _tiano_var_restore_val="\$_tiano_var_stack_count_$1"
   2.260 +	if [ $_tiano_var_restore_val -lt 1 ]; then
   2.261 +		unset _tiano_var_stack_count_$1
   2.262 +	fi
   2.263 +	unset _tiano_var_restore_val
   2.264 +
   2.265 +	tiano_debug_func_end
   2.266  }
   2.267  
   2.268  ###
   2.269 @@ -506,6 +500,67 @@
   2.270  }
   2.271  
   2.272  ################################################################################
   2.273 +###   Private API                                                            ###
   2.274 +################################################################################
   2.275 +
   2.276 +###
   2.277 +### Validation
   2.278 +###
   2.279 +
   2.280 +# tiano_valid_id() without debugging checks.
   2.281 +_tiano_valid_id()
   2.282 +{
   2.283 +	case "$1" in
   2.284 +		"")			return 1 ;;
   2.285 +		[!_a-zA-Z]*)		return 1 ;;
   2.286 +		*[!_a-zA-Z0-9]*)	return 1 ;;
   2.287 +		*)			return 0 ;;
   2.288 +	esac
   2.289 +}
   2.290 +
   2.291 +# tiano_valid_dec_uint() without debugging checks.
   2.292 +_tiano_valid_dec_uint()
   2.293 +{
   2.294 +	case "$1" in
   2.295 +		"")		return 1 ;;
   2.296 +		0)		return 0 ;;
   2.297 +		[!1-9]*)	return 1 ;;
   2.298 +		*[!0-9]*)	return 1 ;;
   2.299 +		*)		return 0 ;;
   2.300 +	esac
   2.301 +}
   2.302 +
   2.303 +# tiano_valid_IFS() without debugging checks.
   2.304 +_tiano_valid_IFS()
   2.305 +{
   2.306 +	case "$IFS" in
   2.307 +		*[_a-zA-Z0-9]*)	return 1 ;;
   2.308 +		*)		return 0 ;;
   2.309 +	esac
   2.310 +}
   2.311 +
   2.312 +###
   2.313 +### Debugging
   2.314 +###
   2.315 +
   2.316 +# tiano_debug_assert() without debugging checks.
   2.317 +_tiano_debug_assert()
   2.318 +{
   2.319 +	if [ "$1" != 0 ]; then
   2.320 +		shift 1
   2.321 +		tiano_debug_fatal "$@"
   2.322 +	fi
   2.323 +}
   2.324 +
   2.325 +# tiano_debug_check_env() without debugging checks.
   2.326 +_tiano_debug_check_env()
   2.327 +{
   2.328 +	_tiano_valid_IFS
   2.329 +	_tiano_debug_assert $? \
   2.330 +		"IFS variable is set to the invalid value \`%s'" "$IFS"
   2.331 +}
   2.332 +
   2.333 +################################################################################
   2.334  ###   Initialization                                                         ###
   2.335  ################################################################################
   2.336