#!/bin/sh
# 
#  Copyright 1985, Todd Brunhoff.
# 
#  This software was written at Tektronix Computer Research Laboratories
#  as partial fulfillment of a Master's degree at the University of Denver.
#  This software is not Tektronix proprietary software and should not be
#  confused with any software product sold by Tektronix.  No warranty is
#  expressed or implied on the reliability of this software; the author,
#  the University of Denver, and Tektronix, inc. accept no liability for
#  any damage done directly or indirectly by this software.  This software
#  may be copied, modified or used in any way, without fee, provided this
#  comment remains an unaltered part of the software.
#

if [ ! -f "$1" ]
then
	echo "Usage:"
	echo "	/lib/cpp -DKERNEL ... ..../init_sysent.c | $0 ..../remotefs.h"
	exit 1
fi

REMOTE=$1

#
# First, get the complete list of remote system calls and put them
# into memory for awk.
#
MEM=`sed -e '/^#define[ 	]*RSYS_/!d' \
	-e 's/.*RSYS_\([^ 	]*\)	\([0-9]*\)$/mem[\2]="\1";last=\2;/' \
	< $REMOTE`
#
# Then, compile a list of all system calls from a cpp expanded listing
# of sys/init_sysent.c which should be on standard input.
# The only kludge here is that we must change internal names for system
# calls:
#      internal	      changed
#	name		 to
#     ----------     ----------
#	rexit		exit
#	saccess		access
#
sed	-e '1,/^struct[ 	]*sysent[ 	]*sysent[ 	]*\[\]/d' \
	-e '/^};/,$d' \
	-e '/^#/d' \
	-e 's/	*[0-9], *//' \
	-e 's/,.*//' \
	-e 's/^rexit$/exit/' \
	-e 's/^saccess$/access/' \
	-e '/^[ 	]*$/d' \
| tail +2 \
| cat -n \
| awk '
BEGIN {
	'"$MEM"'
	syscall = 0;
	column = 0;
	printf "u_char\tremote_sysmap[] = {\n"
}
{
	while (syscall < $1) {
		if (column % 2 == 0)
			printf "\t"
		printf "%-31s", "RSYS_nosys,"
		if (column % 2 == 1)
			printf "\n"
		syscall++
		column++
	}

	if (column % 2 == 0)
		printf "\t"
	len = length($2);
	found = 0;
	for (i=0; i <= last; i++) {
		if (mem[ i ] == $2) {
			found = 1;
			break;
		}
	}
	if (found) {
		printf "RSYS_%s,", $2
		len = 25-len;
	}
	else {
		printf "RSYS_nosys,  /* %s */", $2
		len = 12 - len;
	}
	if (column % 2 == 1)
		printf "\n"
	else
		while (len-- > 0)
			printf " "
	column++;
	syscall++
} END {
	if (column % 2 == 0)
		printf "\n"
	printf "};\n"
}'
