xref: /netbsd-src/bin/sh/mknodenames.sh (revision 3bdfdc72d6e20e3977afff2cdc296007cbc71689)
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
12echo()
13{
14	command printf '%s\n' "$1"
15}
16
17if [ -z "$1" ]; then
18	echo "Usage: $0 nodes.h" 1>&2
19	exit 1
20fi
21
22NODES=$1
23
24test -t 1 && test -z "$2" && exec > nodenames.h
25
26echo "\
27/*
28 * Automatically generated by $0
29 * DO NOT EDIT. Do Not 'cvs add'.
30 */
31"
32echo "#ifndef NODENAMES_H_INCLUDED"
33echo "#define NODENAMES_H_INCLUDED"
34echo
35echo "#ifdef DEBUG"
36
37MAX=$(${AWK:-awk} < "$NODES" '
38	/#define/ {
39		if ($3 > MAX) MAX = $3
40	}
41	END { print MAX }
42')
43
44echo
45echo '#ifdef DEFINE_NODENAMES'
46echo "STATIC const char * const NodeNames[${MAX} + 1] = {"
47
48grep '^#define' "$NODES" | sort -k3n | while read define name number opt_comment
49do
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 ))
58done
59
60echo "};"
61echo '#else'
62echo "extern const char * const NodeNames[${MAX} + 1];"
63echo '#endif'
64echo
65echo '#define NODETYPENAME(type) \'
66echo '	((unsigned)(type) <= '"${MAX}"' ? NodeNames[(type)] : "??OOR??")'
67echo
68echo '#define NODETYPE(type)	NODETYPENAME(type), (type)'
69echo '#define PRIdsNT		"s(%d)"'
70echo
71echo '#else /* DEBUG */'
72echo
73echo '#define NODETYPE(type)	(type)'
74echo '#define PRIdsNT		"d"'
75echo
76echo '#endif /* DEBUG */'
77echo
78echo '#endif /* !NODENAMES_H_INCLUDED */'
79