17323adacSDevin Teskeif [ ! "$_STRUCT_SUBR" ]; then _STRUCT_SUBR=1 27323adacSDevin Teske# 37323adacSDevin Teske# Copyright (c) 2012-2013 Devin Teske 4f8ea072aSDevin Teske# All rights reserved. 57323adacSDevin Teske# 67323adacSDevin Teske# Redistribution and use in source and binary forms, with or without 77323adacSDevin Teske# modification, are permitted provided that the following conditions 87323adacSDevin Teske# are met: 97323adacSDevin Teske# 1. Redistributions of source code must retain the above copyright 107323adacSDevin Teske# notice, this list of conditions and the following disclaimer. 117323adacSDevin Teske# 2. Redistributions in binary form must reproduce the above copyright 127323adacSDevin Teske# notice, this list of conditions and the following disclaimer in the 137323adacSDevin Teske# documentation and/or other materials provided with the distribution. 147323adacSDevin Teske# 157323adacSDevin Teske# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 168e37a7c8SDevin Teske# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 177323adacSDevin Teske# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 187323adacSDevin Teske# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 197323adacSDevin Teske# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 208e37a7c8SDevin Teske# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 217323adacSDevin Teske# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 227323adacSDevin Teske# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 237323adacSDevin Teske# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 247323adacSDevin Teske# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 257323adacSDevin Teske# SUCH DAMAGE. 267323adacSDevin Teske# 277323adacSDevin Teske# 287323adacSDevin Teske############################################################ INCLUDES 297323adacSDevin Teske 307323adacSDevin TeskeBSDCFG_SHARE="/usr/share/bsdconfig" 317323adacSDevin Teske. $BSDCFG_SHARE/common.subr || exit 1 327323adacSDevin Teske 337323adacSDevin Teske############################################################ FUNCTIONS 347323adacSDevin Teske 357323adacSDevin Teske# f_struct_define $type $member_name1 ... 367323adacSDevin Teske# 377323adacSDevin Teske# Define a new `structure' type $type made up of the properties $member_name1 387323adacSDevin Teske# $member_name2 and so-on. Properties are not typed and can hold any type of 397323adacSDevin Teske# data (including names of other structs). 407323adacSDevin Teske# 417323adacSDevin Teske# Before creating instances of a struct (using f_struct_new $type $name) you 427323adacSDevin Teske# should use this function to define $type. 437323adacSDevin Teske# 447323adacSDevin Teske# Both $type and member names should consist only of alpha-numeric letters or 457323adacSDevin Teske# the underscore. 467323adacSDevin Teske# 477323adacSDevin Teskef_struct_define() 487323adacSDevin Teske{ 497323adacSDevin Teske local type="$1" 507323adacSDevin Teske [ "$type" ] || return $FAILURE 517323adacSDevin Teske shift 527323adacSDevin Teske setvar "_struct_typedef_$type" "$*" 537323adacSDevin Teske} 547323adacSDevin Teske 557323adacSDevin Teske# f_struct_new $type $name 567323adacSDevin Teske# 577323adacSDevin Teske# Create a new `structure' named $name of type $type. There are two ways to 587323adacSDevin Teske# access properties of a struct, but they are not equal (each method has its 597323adacSDevin Teske# own unique benefits, discussed below). 607323adacSDevin Teske# 617323adacSDevin Teske# The primary method of accessing (both setting and getting) properties of any 627323adacSDevin Teske# struct is through the f_struct() function below. 637323adacSDevin Teske# 647323adacSDevin Teske# The secondary method of accessing data is by using $name as a function. 657323adacSDevin Teske# 667323adacSDevin Teske# Both access methods are cross-platform compatible with any version of sh(1). 677323adacSDevin Teske# Below is an example of the primary access method: 687323adacSDevin Teske# 697323adacSDevin Teske# f_struct_new MY_STRUCT_TYPE my_struct 707323adacSDevin Teske# f_struct my_struct set abc 123 717323adacSDevin Teske# f_struct my_struct get abc # prints 123 to stdout 727323adacSDevin Teske# f_struct my_struct get abc abc # sets local variable $abc to 123 737323adacSDevin Teske# 747323adacSDevin Teske# Alternatively, the secondary access method (details below): 757323adacSDevin Teske# 767323adacSDevin Teske# f_struct_new MY_STRUCT_TYPE my_struct 777323adacSDevin Teske# my_struct set abc 123 787323adacSDevin Teske# my_struct get abc # prints 123 to stdout 797323adacSDevin Teske# my_struct get abc abc # sets local variable $abc to 123 807323adacSDevin Teske# 817323adacSDevin Teske# The secondary form should only be used if/when: 827323adacSDevin Teske# + You are certain that the structure already exists 837323adacSDevin Teske# + You want a syntax error if/when the struct does not exist 847323adacSDevin Teske# 857323adacSDevin Teske# The primary benefit to the secondary form is syntax cleanliness and read- 867323adacSDevin Teske# ability. If you are unsure if a given struct exists (which would cause a 877323adacSDevin Teske# syntax error when using this form), you can use the primary access method to 887323adacSDevin Teske# first test for the existence of the struct. For example: 897323adacSDevin Teske# 907323adacSDevin Teske# if f_struct my_struct; then 917323adacSDevin Teske# my_struct get abc # only executed if my_struct exists 927323adacSDevin Teske# fi 937323adacSDevin Teske# 947323adacSDevin Teske# For more information, see the f_struct() function. 957323adacSDevin Teske# 967323adacSDevin Teskef_struct_new() 977323adacSDevin Teske{ 987323adacSDevin Teske local type="$1" name="$2" 997323adacSDevin Teske f_dprintf "f_struct_new: type=[%s] name=[%s]" "$type" "$name" 1007323adacSDevin Teske [ "$name" ] || return $FAILURE 1017323adacSDevin Teske setvar "_struct_type_$name" "$type" || return $FAILURE 1027323adacSDevin Teske # OK to use bare $name at this point 1037323adacSDevin Teske eval $name\(\){ f_struct $name \"\$@\"\; } 1047323adacSDevin Teske} 1057323adacSDevin Teske 1067323adacSDevin Teske# f_struct $name 1077323adacSDevin Teske# f_struct $name get $property [$var_to_set] 1087323adacSDevin Teske# f_struct $name set $property $new_value 1097323adacSDevin Teske# f_struct $name unset $property 1107323adacSDevin Teske# 1117323adacSDevin Teske# Access routine for getting, setting, unsetting, and testing properties of 1127323adacSDevin Teske# `structures'. 1137323adacSDevin Teske# 1147323adacSDevin Teske# If only given $name, returns success if struct $name has been created (using 1157323adacSDevin Teske# the f_struct_new() function above). 1167323adacSDevin Teske# 1177323adacSDevin Teske# For getting properties of a struct (versus setting) there are two methods of 1187323adacSDevin Teske# access. If $var_to_set is missing or NULL, the value of the property is 1197323adacSDevin Teske# printed to standard output for capturing in a sub-shell (which is less- 1207323adacSDevin Teske# recommended because of performance degredation; for example, when called in a 1217323adacSDevin Teske# loop). Returns success unless the property is unset. 1227323adacSDevin Teske# 1237323adacSDevin Teske# For setting properties of a struct, sets the value of $property to $new_value 1247323adacSDevin Teske# and returns success. 1257323adacSDevin Teske# 1267323adacSDevin Teske# For unsetting, the underlying environment variable associated with the given 1277323adacSDevin Teske# $property is unset. 1287323adacSDevin Teske# 1297323adacSDevin Teskef_struct() 1307323adacSDevin Teske{ 1317323adacSDevin Teske local __name="$1" __action="$2" __property="$3" 1327323adacSDevin Teske case $# in 1337323adacSDevin Teske 0) return $FAILURE ;; 134*9ecd54f2SDevin Teske 1) f_have "$__name" ;; 1357323adacSDevin Teske *) case "$__action" in 1367323adacSDevin Teske get) local __var_to_set="$4" 1377323adacSDevin Teske f_getvar "_struct_value_${__name}_$__property" "$__var_to_set" 1387323adacSDevin Teske ;; 1397323adacSDevin Teske set) local new_value="$4" 1407323adacSDevin Teske setvar "_struct_value_${__name}_$__property" "$new_value" ;; 1417323adacSDevin Teske unset) unset "_struct_value_${__name}_$__property" ;; 1427323adacSDevin Teske esac 1437323adacSDevin Teske esac 1447323adacSDevin Teske # Return the status of the last command above 1457323adacSDevin Teske} 1467323adacSDevin Teske 1477323adacSDevin Teske# f_struct_free $name 1487323adacSDevin Teske# 1497323adacSDevin Teske# Unset the collection of environment variables and accessor-function 1507323adacSDevin Teske# associated with struct $name. 1517323adacSDevin Teske# 1527323adacSDevin Teskef_struct_free() 1537323adacSDevin Teske{ 1547323adacSDevin Teske local name="$1" type member members 1557323adacSDevin Teske f_getvar "_struct_type_$name" type 1567323adacSDevin Teske f_dprintf "f_struct_free: name=[%s] type=[%s]" "$name" "$type" 1577323adacSDevin Teske [ "$name" ] || return $FAILURE 1587323adacSDevin Teske f_getvar "_struct_typedef_$type" members 1597323adacSDevin Teske for member in $members; do 1607323adacSDevin Teske f_struct "$name" unset $member 1617323adacSDevin Teske done 1627323adacSDevin Teske unset -f "$name" 1637323adacSDevin Teske unset "_struct_type_$name" 1647323adacSDevin Teske} 1657323adacSDevin Teske 1667323adacSDevin Teske# f_struct_copy $from_name $to_name 1677323adacSDevin Teske# 1687323adacSDevin Teske# Copy the properties of one struct to another. If struct $to_name does not 1697323adacSDevin Teske# exist, it is created. If struct $from_name does not exist, nothing is done 1707323adacSDevin Teske# and struct $to_name remains unmodified. 1717323adacSDevin Teske# 1727323adacSDevin Teske# Returns success unless struct $to_name did not exist and f_struct_new() was 1737323adacSDevin Teske# unable to create it. 1747323adacSDevin Teske# 1757323adacSDevin Teskef_struct_copy() 1767323adacSDevin Teske{ 1777323adacSDevin Teske local from_name="$1" to_name="$2" type 1787323adacSDevin Teske f_dprintf "f_struct_copy: from_name=[%s] to_name=[%s]" \ 1797323adacSDevin Teske "$from_name" "$to_name" 1807323adacSDevin Teske f_getvar "_struct_type_$from_name" type 1817323adacSDevin Teske f_struct "$to_name" || 1827323adacSDevin Teske f_struct_new "$type" "$to_name" || return $FAILURE 1837323adacSDevin Teske f_struct "$from_name" || return $SUCCESS 1847323adacSDevin Teske f_dprintf "f_struct_copy: copying properties from %s to %s" \ 1857323adacSDevin Teske "$from_name" "$to_name" 1867323adacSDevin Teske local property properties from_value n=0 k=0 1877323adacSDevin Teske f_getvar "_struct_typedef_$type" properties 1887323adacSDevin Teske for property in $properties; do 1897323adacSDevin Teske k=$(( $k + 1 )) 1907323adacSDevin Teske if f_struct "$from_name" get $property from_value; then 1917323adacSDevin Teske f_struct "$to_name" set $property "$from_value" 1927323adacSDevin Teske n=$(( $n + 1 )) 1937323adacSDevin Teske else 1947323adacSDevin Teske f_struct "$to_name" unset $property 1957323adacSDevin Teske fi 1967323adacSDevin Teske done 1977323adacSDevin Teske f_dprintf "f_struct_copy: copied %u of %u properties from %s to %s" \ 1987323adacSDevin Teske "$n" "$k" "$from_name" "$to_name" 1997323adacSDevin Teske} 2007323adacSDevin Teske 2017323adacSDevin Teske############################################################ MAIN 2027323adacSDevin Teske 2037323adacSDevin Teskef_dprintf "%s: Successfully loaded." struct.subr 2047323adacSDevin Teske 2057323adacSDevin Teskefi # ! $_STRUCT_SUBR 206