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