diff options
| author | Samuel Holland <[email protected]> | 2024-11-11 14:02:48 -0800 |
|---|---|---|
| committer | Anup Patel <[email protected]> | 2024-11-28 17:23:36 +0530 |
| commit | 10df2d6fb5dba486a74cb47bbc062f3383bba80d (patch) | |
| tree | 4767419353d07d193159bb0d293291cfdfd976d6 | |
| parent | bb008e5d27069925adf88a6af1e6fdcddf93a36f (diff) | |
scripts/carray.sh: Allow referencing a struct member
It can be useful to embed the objects referenced by a carray inside
another struct. To avoid type punning, the generated carray code must
use the correct type for the enclosing struct and member access to
compute the desired object address.
Signed-off-by: Samuel Holland <[email protected]>
Reviewed-by: Anup Patel <[email protected]>
| -rwxr-xr-x | scripts/carray.sh | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/scripts/carray.sh b/scripts/carray.sh index 16c6c136..fb985c86 100755 --- a/scripts/carray.sh +++ b/scripts/carray.sh @@ -61,6 +61,13 @@ if [ -z "${ARRAY_NAME}" ]; then usage fi +MEMBER_NAME=$(awk '{ if ($1 == "MEMBER-NAME:") { printf $2; exit 0; } }' "${CONFIG_FILE}") +MEMBER_TYPE=$(awk '{ if ($1 == "MEMBER-TYPE:") { printf $2; for (i=3; i<=NF; i++) printf " %s", $i; exit 0; } }' "${CONFIG_FILE}") +if [ -n "${MEMBER_NAME}" ] && [ -z "${MEMBER_TYPE}" ]; then + echo "Must specify MEMBER-TYPE: when using MEMBER-NAME:" + usage +fi + printf "// Generated with $(basename $0) from $(basename ${CONFIG_FILE})\n" printf "#include <%s>\n\n" "${TYPE_HEADER}" @@ -69,9 +76,9 @@ for VAR in ${VAR_LIST}; do done printf "\n" -printf "%s *const %s[] = {\n" "${TYPE_NAME}" "${ARRAY_NAME}" +printf "%s *const %s[] = {\n" "${MEMBER_TYPE:-${TYPE_NAME}}" "${ARRAY_NAME}" for VAR in ${VAR_LIST}; do - printf "\t&%s,\n" "${VAR}" + printf "\t&%s,\n" "${VAR}${MEMBER_NAME:+.}${MEMBER_NAME}" done printf "\tNULL\n" printf "};\n" |
