NetBSD/bin/sh/mknodenames.sh
kre 467f8e2cd3 It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...

So as it happens these fixes change nothing, but they are needed anyway.

(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
2017-06-19 11:55:07 +00:00

52 lines
884 B
Bash
Executable File

#! /bin/sh
if [ -z "$1" ]; then
echo "Usage: $0 nodes.h" 1>&2
exit 1
fi
NODES=$1
test -t 1 && test -z "$2" && exec > nodenames.h
echo "#ifdef DEBUG"
echo '
/*
* Automatically generated by '"$0"'
* DO NOT EDIT. Do Not "cvs add".
*/
'
MAX=$(awk < "$NODES" '
/#define/ {
if ($3 > MAX) MAX = $3
}
END { print MAX }
')
echo
echo '#ifdef DEFINE_NODENAMES'
echo "STATIC const char * const NodeNames[${MAX} + 1] = {"
grep '^#define' "$NODES" | sort -k3n | while read define name number opt_comment
do
: ${next:=0}
while [ "$number" -gt "$next" ]
do
echo ' "???",'
next=$(( next + 1))
done
echo ' "'"$name"'",'
next=$(( number + 1 ))
done
echo "};"
echo '#else'
echo "extern const char * const NodeNames[${MAX} + 1];"
echo '#endif'
echo
echo '#define NODETYPENAME(type) \'
echo ' ((unsigned)(type) <= '"${MAX}"' ? NodeNames[(type)] : "??OOR??")'
echo
echo '#endif'