2021-10-19 23:20:44 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
|
|
|
# flags
|
|
|
|
# -a append to data file (default off = truncate)
|
|
|
|
# -p pad sizes within data file to next block size (default off)
|
|
|
|
|
|
|
|
# parameters
|
2022-09-06 00:38:43 +00:00
|
|
|
# stdin - input containing list of files (e.g. FX.CONF)
|
2021-10-23 05:36:04 +00:00
|
|
|
# stdout - binary OKVS data structure
|
|
|
|
# 1 - output filename for data file
|
|
|
|
# 2 - input directory of files to merge into data file
|
2021-11-16 01:25:32 +00:00
|
|
|
# 3 - (optional) output filename for log of key,offset,size
|
2021-10-19 23:20:44 +00:00
|
|
|
|
|
|
|
pad=false
|
|
|
|
append=false
|
2021-10-21 17:04:01 +00:00
|
|
|
standardoffset=0
|
2021-10-19 23:20:44 +00:00
|
|
|
standardsize=0
|
|
|
|
while getopts ":ap" opt; do
|
|
|
|
case $opt in
|
|
|
|
a) append=true
|
|
|
|
;;
|
|
|
|
p) pad=true
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
|
2021-10-21 17:04:01 +00:00
|
|
|
if [ "$append" = false ]; then
|
2021-10-23 05:36:04 +00:00
|
|
|
rm -f "$1"
|
2021-10-19 23:20:44 +00:00
|
|
|
fi
|
2021-10-23 05:36:04 +00:00
|
|
|
touch "$1"
|
2021-10-19 23:20:44 +00:00
|
|
|
|
2021-11-16 01:25:32 +00:00
|
|
|
if [ "${#3}" -ne "0" ]; then
|
|
|
|
rm -f "$3"
|
|
|
|
touch "$3"
|
|
|
|
fi
|
|
|
|
|
2021-10-21 17:04:01 +00:00
|
|
|
# if there is a file called "STANDARD" in the input directory, add it now
|
|
|
|
# because we will reuse it for any files that don't exist
|
2021-10-23 05:36:04 +00:00
|
|
|
if [ -f "$2"/STANDARD ]; then
|
|
|
|
standardoffset=$(wc -c < "$1")
|
|
|
|
standardsize=$(wc -c < "$2/STANDARD")
|
|
|
|
cat "$2"/STANDARD >> "$1"
|
2021-10-21 17:04:01 +00:00
|
|
|
fi
|
|
|
|
|
2021-10-19 23:20:44 +00:00
|
|
|
# make temp file with list of lines that contain keys
|
|
|
|
records=$(mktemp)
|
2024-05-24 20:24:55 +00:00
|
|
|
tr -d "\r" | awk '!/^$|^#/' > "$records"
|
2021-10-19 23:20:44 +00:00
|
|
|
|
|
|
|
# make temp assembly source file that represents the binary OKVS data structure
|
|
|
|
source=$(mktemp)
|
2022-09-06 00:38:43 +00:00
|
|
|
(echo "*=0" # dummy program counter for assembler
|
|
|
|
echo "!le16 $(wc -l <"$records"), 0" # OKVS header
|
|
|
|
while IFS="=" read -r filename dummy; do
|
|
|
|
key=$(echo "$filename" | awk -F'#' '{ print $1 }')
|
|
|
|
addr=$(echo "$filename" | awk -F'#' '{ print $2 }')
|
|
|
|
if [ "${#addr}" -ne "0" ]; then # if filename is in the form 'NAME#06ADDR' then create extended index record
|
|
|
|
addr=$(echo "$addr" | cut -c3-) # trim '06' so we get just the starting address
|
|
|
|
echo "!byte ${#key}+9" # OKVS record length
|
|
|
|
else
|
|
|
|
echo "!byte ${#key}+7" # OKVS record length
|
|
|
|
fi
|
|
|
|
echo "!byte ${#key}" # OKVS key length
|
|
|
|
echo "!text \"$key\"" # OKVS key
|
|
|
|
if [ ! -e "$2/$filename" ]; then # if file does not exist, use standard offset and size
|
2021-11-16 01:25:32 +00:00
|
|
|
offset="$standardoffset"
|
|
|
|
size="$standardsize"
|
2022-09-06 00:38:43 +00:00
|
|
|
else # otherwise calculate offset and size from file and options
|
2021-10-23 05:36:04 +00:00
|
|
|
offset=$(wc -c < "$1")
|
2022-09-06 00:38:43 +00:00
|
|
|
size=$(wc -c < "$2/$filename")
|
2021-10-19 23:20:44 +00:00
|
|
|
if [ "$pad" = true ]; then
|
|
|
|
# If offset+size does not cross a block boundary, use file's true size.
|
|
|
|
# Otherwise, round up size to the next block boundary.
|
|
|
|
# This padding does not get added to the file; it is just an
|
|
|
|
# optimization to avoid a partial copy on the last block read.
|
2021-11-16 01:25:32 +00:00
|
|
|
if [ $(($offset / 512)) -ne $((($offset + $size) / 512)) ]; then
|
|
|
|
size=$(((($offset + $size + 511) & -512) - $offset))
|
2021-10-19 23:20:44 +00:00
|
|
|
fi
|
|
|
|
fi
|
2022-09-06 00:38:43 +00:00
|
|
|
cat "$2/$filename" >> "$1" # append this file to the end of the merged data file
|
2021-10-19 23:20:44 +00:00
|
|
|
fi
|
2021-11-16 01:25:32 +00:00
|
|
|
echo "!be24 $offset"
|
|
|
|
echo "!le16 $size"
|
2022-09-06 00:38:43 +00:00
|
|
|
[ "${#addr}" -ne "0" ] && echo '!le16 $'"$addr"
|
2021-11-16 01:25:32 +00:00
|
|
|
[ "${#3}" -ne "0" ] && echo "$key,$offset,$size" >> "$3"
|
2021-10-19 23:20:44 +00:00
|
|
|
done < "$records") > "$source"
|
|
|
|
|
2021-10-23 05:36:04 +00:00
|
|
|
# assemble temp source file into binary OKVS data structure, then output that
|
|
|
|
out=$(mktemp)
|
|
|
|
acme -o "$out" "$source"
|
|
|
|
cat "$out"
|
2021-10-19 23:20:44 +00:00
|
|
|
|
|
|
|
# clean up
|
2021-10-23 05:36:04 +00:00
|
|
|
rm "$out"
|
2021-10-19 23:20:44 +00:00
|
|
|
rm "$source"
|
|
|
|
rm "$records"
|