Home | History | Annotate | Line # | Download | only in sh
mknodenames.sh revision 1.7
      1  1.1  christos #! /bin/sh
      2  1.1  christos 
      3  1.7       kre # $NetBSD: mknodenames.sh,v 1.7 2024/10/14 08:27:19 kre Exp $
      4  1.5       kre 
      5  1.5       kre # Use this script however you like, but it would be amazing if
      6  1.5       kre # it has any purpose other than as part of building the shell...
      7  1.5       kre 
      8  1.7       kre # All (like every single one) uses of echo in this script
      9  1.7       kre # have at most 1 argument (string) to print, and none use -n
     10  1.7       kre # hence replacing echo with printf is trivial...
     11  1.7       kre 
     12  1.7       kre echo()
     13  1.7       kre {
     14  1.7       kre 	command printf '%s\n' "$1"
     15  1.7       kre }
     16  1.7       kre 
     17  1.2  christos if [ -z "$1" ]; then
     18  1.2  christos 	echo "Usage: $0 nodes.h" 1>&2
     19  1.2  christos 	exit 1
     20  1.2  christos fi
     21  1.2  christos 
     22  1.2  christos NODES=$1
     23  1.2  christos 
     24  1.4       kre test -t 1 && test -z "$2" && exec > nodenames.h
     25  1.1  christos 
     26  1.5       kre echo "\
     27  1.1  christos /*
     28  1.5       kre  * Automatically generated by $0
     29  1.5       kre  * DO NOT EDIT. Do Not 'cvs add'.
     30  1.1  christos  */
     31  1.5       kre "
     32  1.5       kre echo "#ifndef NODENAMES_H_INCLUDED"
     33  1.5       kre echo "#define NODENAMES_H_INCLUDED"
     34  1.5       kre echo
     35  1.5       kre echo "#ifdef DEBUG"
     36  1.1  christos 
     37  1.7       kre MAX=$(${AWK:-awk} < "$NODES" '
     38  1.1  christos 	/#define/ {
     39  1.1  christos 		if ($3 > MAX) MAX = $3
     40  1.1  christos 	}
     41  1.1  christos 	END { print MAX }
     42  1.1  christos ')
     43  1.1  christos 
     44  1.1  christos echo
     45  1.2  christos echo '#ifdef DEFINE_NODENAMES'
     46  1.1  christos echo "STATIC const char * const NodeNames[${MAX} + 1] = {"
     47  1.1  christos 
     48  1.4       kre grep '^#define' "$NODES" | sort -k3n | while read define name number opt_comment
     49  1.1  christos do
     50  1.1  christos 	: ${next:=0}
     51  1.1  christos 	while [ "$number" -gt "$next" ]
     52  1.1  christos 	do
     53  1.1  christos 		echo '	"???",'
     54  1.1  christos 		next=$(( next + 1))
     55  1.1  christos 	done
     56  1.1  christos 	echo '	"'"$name"'",'
     57  1.1  christos 	next=$(( number + 1 ))
     58  1.1  christos done
     59  1.1  christos 
     60  1.1  christos echo "};"
     61  1.2  christos echo '#else'
     62  1.2  christos echo "extern const char * const NodeNames[${MAX} + 1];"
     63  1.2  christos echo '#endif'
     64  1.1  christos echo
     65  1.1  christos echo '#define NODETYPENAME(type) \'
     66  1.1  christos echo '	((unsigned)(type) <= '"${MAX}"' ? NodeNames[(type)] : "??OOR??")'
     67  1.1  christos echo
     68  1.6       kre echo '#define NODETYPE(type)	NODETYPENAME(type), (type)'
     69  1.6       kre echo '#define PRIdsNT		"s(%d)"'
     70  1.5       kre echo
     71  1.5       kre echo '#else /* DEBUG */'
     72  1.5       kre echo
     73  1.5       kre echo '#define NODETYPE(type)	(type)'
     74  1.6       kre echo '#define PRIdsNT		"d"'
     75  1.5       kre echo
     76  1.5       kre echo '#endif /* DEBUG */'
     77  1.5       kre echo
     78  1.5       kre echo '#endif /* !NODENAMES_H_INCLUDED */'
     79