Files
yait/t/t
2025-11-04 13:58:37 -05:00

155 lines
4.0 KiB
Bash
Executable File

#!/bin/sh
# SPDX-License-Identifier: BSD-3-Clause
me=$0
scriptversion="1.0.1"
version="$me $scriptversion
Copyright (C) 2025 GCK.
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law."
usage="\
Usage: $me [OPTION]...
Generate template headers
Options:
--help print this help and exit
--version output version information"
while test $# -gt 0; do
case $1 in
--help) echo "$usage"; exit 0;;
--version) echo "$version"; exit 0;;
-*)
echo "$0: Unknown option '$1'." >&2
echo "$0: Try '--help' for more information." >&2
exit 1;;
esac
shift
done
x_files=$(find . -maxdepth 1 -name "*.x" -type f | sort)
if test -z "$x_files"; then
echo "$0: No .x files found in current directory." >&2
exit 1
fi
output_file="../build/include/template.h"
mkdir -p ../build/include/
first_template=true
{
echo "/* DO NOT MODIFY THIS FILE! It was generated by t $scriptversion. */"
echo "#ifndef TEMPLATE_H"
echo "#define TEMPLATE_H"
for input_file in $x_files; do
base_name=$(basename "$input_file" .x)
template_name=$(echo "$base_name" | tr '[:lower:]' '[:upper:]')
tmp_format=$(mktemp)
tmp_template=$(mktemp)
tmp_params=$(mktemp)
format_index=$(head -n 1 "$input_file")
if ! echo "$format_index" | grep -q "^Format_Index:"; then
echo "$0: '$input_file': Missing Format_Index line." >&2
rm -f "$tmp_format" "$tmp_template" "$tmp_params"
continue
fi
if test "$first_template" = false; then
echo ""
fi
first_template=false
tmp_specs=$(mktemp)
echo "$format_index" | sed 's/Format_Index://' | tr ',' '\n' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//' > "$tmp_params"
tail -n +2 "$input_file" | sed '/^$/d' > "$tmp_template"
param_names=""
while IFS= read -r param; do
param_names="$param_names $param"
done < "$tmp_params"
while IFS= read -r line; do
echo "$line" | grep -o '%[sdifgxXoucpeEaAn]' >> "$tmp_specs"
done < "$tmp_template"
echo "/*"
param_idx=1
for param in $param_names; do
format_type=""
spec=$(sed -n "${param_idx}p" "$tmp_specs")
case "$spec" in
%s|%S) format_type="(s)" ;;
%d|%i|%u|%x|%X|%o) format_type="(d)" ;;
%f|%g|%e|%E|%a|%A) format_type="(f)" ;;
%c) format_type="(c)" ;;
%p) format_type="(p)" ;;
%n) format_type="(n)" ;;
*) format_type="(?)" ;;
esac
case "$param" in
a) param="author" ;;
y) param="year" ;;
l) param="license" ;;
d) param="description" ;;
p) param="package" ;;
esac
printf " * %-13s%s\n" "$param" "$format_type"
param_idx=$((param_idx + 1))
done
echo " */"
echo "const char templ_${template_name}[] = \"\\"
line_count=0
total_lines=$(wc -l < "$tmp_template")
while IFS= read -r line; do
line_count=$((line_count + 1))
escaped_line=$(echo "$line" | sed \
-e 's/\\\\/__BSLASH__/g' \
-e 's/\\"/__BQUOTE__/g' \
-e 's/\\n/__NEWLINE__/g' \
-e 's/\\t/__TAB__/g' \
-e 's/\\r/__RETURN__/g' \
-e 's/\\a/__ALERT__/g' \
-e 's/\\b/__BACKSPACE__/g' \
-e 's/\\f/__FORMFEED__/g' \
-e 's/\\v/__VTAB__/g' \
-e 's/\\0/__NULL__/g' \
-e 's/\\/\\\\/g' \
-e 's/"/\\"/g' \
-e 's/__BSLASH__/\\\\/g' \
-e 's/__BQUOTE__/\\"/g' \
-e 's/__NEWLINE__/\\n/g' \
-e 's/__TAB__/\\t/g' \
-e 's/__RETURN__/\\r/g' \
-e 's/__ALERT__/\\a/g' \
-e 's/__BACKSPACE__/\\b/g' \
-e 's/__FORMFEED__/\\f/g' \
-e 's/__VTAB__/\\v/g' \
-e 's/__NULL__/\\0/g')
if test $line_count -lt $total_lines; then
printf '%s\\n\\\n' "$escaped_line"
else
printf '%s";\n' "$escaped_line"
fi
done < "$tmp_template"
rm -f "$tmp_format" "$tmp_template" "$tmp_params" "$tmp_specs"
done
echo "#endif"
} > "$output_file"