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